Skip to content

Commit fb35d79

Browse files
authored
Merge pull request #526 from LeeLeahy2/vc-unique-id
VC: Pass the unique ID to the PC during VC state changes
2 parents a0f8066 + 8fac078 commit fb35d79

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
lines changed

Firmware/LoRaSerial/Commands.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ bool sendRemoteCommand(const char * commandString)
787787
systemPrintln(" from commandBuffer into commandTXBuffer");
788788
}
789789
remoteCommandResponse = false;
790+
waitRemoteCommandResponse = true;
790791
return true;
791792
}
792793

Firmware/LoRaSerial/LoRaSerial.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ const long minEscapeTime_ms = 2000; //Serial traffic must stop this amount befor
230230
bool inCommandMode = false; //Normal data is prevented from entering serial output when in command mode
231231
uint8_t commandLength = 0;
232232
bool remoteCommandResponse;
233+
bool waitRemoteCommandResponse;
233234

234235
bool rtsAsserted; //When RTS is asserted, host says it's ok to send data
235236
bool forceRadioReset = false; //Goes true when a setting requires a link/radio reset to work

Firmware/LoRaSerial/Serial.ino

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ void updateSerial()
354354
{
355355
commandLength = availableRXCommandBytes();
356356

357+
//Don't overflow the command buffer, save space for the zero termination
358+
if (commandLength >= sizeof(commandBuffer))
359+
commandLength = sizeof(commandBuffer) - 1;
360+
357361
for (x = 0 ; x < commandLength ; x++)
358362
{
359363
commandBuffer[x] = commandRXBuffer[commandRXTail++];
@@ -395,6 +399,7 @@ void processSerialInput()
395399
radioHead = radioTxHead;
396400
while (availableRXBytes()
397401
&& (availableRadioTXBytes() < (sizeof(radioTxBuffer) - maxEscapeCharacters))
402+
&& ((!inCommandMode) || (!waitRemoteCommandResponse))
398403
&& (transactionComplete == false))
399404
{
400405
//Take a break if there are ISRs to attend to
@@ -413,17 +418,22 @@ void processSerialInput()
413418
//Process serial into either rx buffer or command buffer
414419
if (inCommandMode == true)
415420
{
416-
if (incoming == '\r' && commandLength > 0)
421+
//Check for end of command
422+
if ((incoming == '\r') || (incoming == ';'))
417423
{
418-
printerEndpoint = PRINT_TO_SERIAL;
419-
systemPrintln();
420-
if (settings.debugSerial)
424+
//Ignore end of command if no command in the buffer
425+
if (commandLength > 0)
421426
{
422-
systemPrint("processSerialInput moved ");
423-
systemPrint(commandLength);
424-
systemPrintln(" from serialReceiveBuffer into commandBuffer");
427+
systemPrintln();
428+
if (settings.debugSerial)
429+
{
430+
systemPrint("processSerialInput moved ");
431+
systemPrint(commandLength);
432+
systemPrintln(" from serialReceiveBuffer into commandBuffer");
433+
}
434+
checkCommand(); //Process command buffer
435+
break;
425436
}
426-
checkCommand(); //Process command buffer
427437
}
428438
else if (incoming == '\n')
429439
; //Do nothing
@@ -448,7 +458,23 @@ void processSerialInput()
448458
{
449459
//Move this character into the command buffer
450460
commandBuffer[commandLength++] = incoming;
451-
commandLength %= sizeof(commandBuffer);
461+
462+
//Don't allow the command to overflow the command buffer
463+
//Process the long command instead
464+
//Save room for the zero termination
465+
if (commandLength >= (sizeof(commandBuffer) - 1))
466+
{
467+
printerEndpoint = PRINT_TO_SERIAL;
468+
systemPrintln();
469+
if (settings.debugSerial)
470+
{
471+
systemPrint("processSerialInput moved ");
472+
systemPrint(commandLength);
473+
systemPrintln(" from serialReceiveBuffer into commandBuffer");
474+
}
475+
checkCommand(); //Process command buffer
476+
break;
477+
}
452478
}
453479
}
454480
}

Firmware/LoRaSerial/States.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ void updateRadioState()
806806
outputSerialData(true);
807807
}
808808
serialBufferOutput(rxData, rxDataBytes);
809+
waitRemoteCommandResponse = false;
809810

810811
//Transmit ACK
811812
P2P_SEND_ACK(TRIGGER_TX_ACK);
@@ -2982,6 +2983,7 @@ void discardPreviousData()
29822983
txTail = txHead;
29832984
commandRXTail = commandRXHead;
29842985
commandTXTail = commandTXHead;
2986+
waitRemoteCommandResponse = false;
29852987
}
29862988

29872989
//Output VC link status
@@ -3047,6 +3049,10 @@ void vcSendPcStateMessage(int8_t vcIndex, uint8_t state)
30473049
//Build the VC state message
30483050
VC_STATE_MESSAGE message;
30493051
message.vcState = state;
3052+
if (virtualCircuitList[vcIndex].flags.valid)
3053+
memcpy(&message.uniqueId[0], &virtualCircuitList[vcIndex].uniqueId[0], sizeof(message.uniqueId));
3054+
else
3055+
memset(message.uniqueId, UNIQUE_ID_ERASE_VALUE, sizeof(message.uniqueId));
30503056

30513057
//Build the message header
30523058
VC_SERIAL_MESSAGE_HEADER header;

Firmware/LoRaSerial/Virtual_Circuit_Protocol.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ typedef struct _VC_SERIAL_MESSAGE_HEADER
140140
} VC_SERIAL_MESSAGE_HEADER;
141141

142142
#define VC_SERIAL_HEADER_BYTES (sizeof(VC_SERIAL_MESSAGE_HEADER)) //Length of the serial VC header in bytes
143+
#define UNIQUE_ID_ERASE_VALUE 0xff
144+
#ifndef UNIQUE_ID_BYTES
145+
#define UNIQUE_ID_BYTES 16
146+
#endif //UNIQUE_ID_BYTES
143147

144148
typedef struct _VC_STATE_MESSAGE
145149
{
146-
uint8_t vcState; //VC state
150+
uint8_t vcState; //VC state
151+
uint8_t uniqueId[UNIQUE_ID_BYTES]; //Unique ID for the LoRaSerial radio, all 0xFF if unknown
147152
} VC_STATE_MESSAGE;
148153

149154
typedef enum

Firmware/Tools/VcServerTest.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
typedef struct _VIRTUAL_CIRCUIT
2929
{
3030
int vcState;
31+
uint8_t uniqueId[UNIQUE_ID_BYTES];
32+
bool valid;
3133
} VIRTUAL_CIRCUIT;
3234

3335
bool commandStatus;
@@ -304,6 +306,7 @@ void radioToPcLinkStatus(VC_SERIAL_MESSAGE_HEADER * header, uint8_t length)
304306
int newState;
305307
int previousState;
306308
int srcVc;
309+
uint8_t uniqueId[UNIQUE_ID_BYTES];
307310
VC_STATE_MESSAGE * vcMsg;
308311

309312
//Remember the previous state
@@ -315,6 +318,47 @@ void radioToPcLinkStatus(VC_SERIAL_MESSAGE_HEADER * header, uint8_t length)
315318
newState = vcMsg->vcState;
316319
virtualCircuitList[srcVc].vcState = newState;
317320

321+
//Save the LoRaSerial radio's unique ID
322+
//Determine if the PC's value is valid
323+
memset(uniqueId, UNIQUE_ID_ERASE_VALUE, sizeof(uniqueId));
324+
if (!virtualCircuitList[srcVc].valid)
325+
{
326+
//Determine if the radio knows the value
327+
if (memcmp(vcMsg->uniqueId, uniqueId, sizeof(uniqueId)) != 0)
328+
{
329+
//The radio knows the value, save it in the PC
330+
memcpy(virtualCircuitList[srcVc].uniqueId, vcMsg->uniqueId, sizeof(vcMsg->uniqueId));
331+
virtualCircuitList[srcVc].valid = true;
332+
333+
//Display this ID value
334+
printf("VC %d unique ID: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
335+
srcVc,
336+
vcMsg->uniqueId[0], vcMsg->uniqueId[1], vcMsg->uniqueId[2], vcMsg->uniqueId[3],
337+
vcMsg->uniqueId[4], vcMsg->uniqueId[5], vcMsg->uniqueId[6], vcMsg->uniqueId[7],
338+
vcMsg->uniqueId[8], vcMsg->uniqueId[9], vcMsg->uniqueId[10], vcMsg->uniqueId[11],
339+
vcMsg->uniqueId[12], vcMsg->uniqueId[13], vcMsg->uniqueId[14], vcMsg->uniqueId[15]);
340+
}
341+
}
342+
else
343+
{
344+
//Determine if the radio has changed for this VC
345+
if ((memcmp(vcMsg->uniqueId, virtualCircuitList[srcVc].uniqueId, sizeof(vcMsg->uniqueId)) != 0)
346+
&& (memcmp(vcMsg->uniqueId, uniqueId, sizeof(uniqueId)) != 0))
347+
{
348+
//The radio knows the value, save it in the PC
349+
memcpy(virtualCircuitList[srcVc].uniqueId, vcMsg->uniqueId, sizeof(vcMsg->uniqueId));
350+
virtualCircuitList[srcVc].valid = true;
351+
352+
//Display this ID value
353+
printf("VC %d unique ID: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
354+
srcVc,
355+
vcMsg->uniqueId[0], vcMsg->uniqueId[1], vcMsg->uniqueId[2], vcMsg->uniqueId[3],
356+
vcMsg->uniqueId[4], vcMsg->uniqueId[5], vcMsg->uniqueId[6], vcMsg->uniqueId[7],
357+
vcMsg->uniqueId[8], vcMsg->uniqueId[9], vcMsg->uniqueId[10], vcMsg->uniqueId[11],
358+
vcMsg->uniqueId[12], vcMsg->uniqueId[13], vcMsg->uniqueId[14], vcMsg->uniqueId[15]);
359+
}
360+
}
361+
318362
//Display the state if requested
319363
if (DISPLAY_STATE_TRANSITION)
320364
printf("VC%d: %s --> %s\n", srcVc, vcStateNames[previousState], vcStateNames[newState]);

0 commit comments

Comments
 (0)