Machine mode: respond to commands to signal success

After processing a machine-mode instruction, respond with "::ok\r\n".
This way the host side knows if an instruction was processed or not.

Also instructions can carry an optional "sequence number" now, which
will be echoed in the "ok" response.

Example:
  ::lightnode:23[...]
responds with:
  :🆗23

The sequence number can be used on the host side to synchronize
specific function calls with their response.

Unknown instructions respond with
  ::error
instead.
This commit is contained in:
Niklas Cathor 2024-01-03 07:50:38 +01:00
parent a93901fbe0
commit 96e09c47a4
3 changed files with 30 additions and 7 deletions

View File

@ -24,7 +24,7 @@ enum machineModeInstruction lastReceivedInstruction = unknown;
char machineModeInstructionString[NUMBEROFINSTRUCTIONS][20] = {"unknown", "netlist", "getnetlist", "bridgelist", "getbridgelist", "lightnode", "lightnet", "getmeasurement", "gpio", "uart", "arduinoflash", "setnetcolor", "setnodecolor", "setsupplyswitch"};
enum machineModeInstruction parseMachineInstructions(void)
enum machineModeInstruction parseMachineInstructions(int *sequenceNumber)
{
int doneReading = 0;
@ -55,17 +55,26 @@ enum machineModeInstruction parseMachineInstructions(void)
char instructionBuffer[20] = {0};
int seqNumberSeparatorPos = -1;
for (int i = 0; i < 20; i++)
{
if (inputBuffer[i] == '[')
{
if (inputBuffer[i] == '[') {
// inputBuffer[i] = ' ';
break;
}
if (inputBuffer[i] == ':') {
seqNumberSeparatorPos = i;
}
instructionBuffer[i] = inputBuffer[i];
inputBuffer[i] = ' ';
}
if (seqNumberSeparatorPos > 0) {
instructionBuffer[seqNumberSeparatorPos] = 0;
*sequenceNumber = atoi(instructionBuffer + seqNumberSeparatorPos + 1);
}
for (int i = 0; i < NUMBEROFINSTRUCTIONS; i++)
{
if (strcasecmp(instructionBuffer, machineModeInstructionString[i]) == 0)
@ -107,6 +116,15 @@ enum machineModeInstruction parseMachineInstructions(void)
return lastReceivedInstruction;
}
void machineModeRespond(int sequenceNumber, bool ok) {
Serial.print(ok ? "::ok" : "::error");
if (sequenceNumber >= 0) {
Serial.print(":");
Serial.print(sequenceNumber);
}
Serial.println("");
}
void machineNetlistToNetstruct(void)
{
char names[MAX_NETS][32] = {0};

View File

@ -24,7 +24,8 @@ enum machineModeInstruction
extern char inputBuffer[INPUTBUFFERLENGTH];
extern char machineModeInstructionString[NUMBEROFINSTRUCTIONS][20];
enum machineModeInstruction parseMachineInstructions(void);
enum machineModeInstruction parseMachineInstructions(int *sequenceNumber);
void machineModeRespond(int sequenceNumber, bool ok);
void machineNetlistToNetstruct(void);
void populateBridgesFromNodes(void);
int nodeTokenToInt(char *);

View File

@ -475,8 +475,8 @@ skipinput:
void machineMode(void) // read in commands in machine readable format
{
enum machineModeInstruction receivedInstruction = parseMachineInstructions();
int sequenceNumber = -1;
enum machineModeInstruction receivedInstruction = parseMachineInstructions(&sequenceNumber);
switch (receivedInstruction)
{
@ -545,9 +545,13 @@ void machineMode(void) // read in commands in machine readable format
// case gpio:
// break;
case unknown:
machineModeRespond(sequenceNumber, false);
return;
}
machineModeRespond(sequenceNumber, true);
}
unsigned long logoFlashTimer = 0;