11#define ADD_VC_STATE_NAMES_TABLE
2+ #include <sys/resource.h>
3+ #include <sys/time.h>
24#include "settings.h"
35
46#define BUFFER_SIZE 2048
2830typedef struct _VIRTUAL_CIRCUIT
2931{
3032 int vcState ;
33+ uint8_t uniqueId [UNIQUE_ID_BYTES ];
34+ bool valid ;
3135} VIRTUAL_CIRCUIT ;
3236
3337bool commandStatus ;
@@ -74,7 +78,7 @@ int cmdToRadio(uint8_t * buffer, int length)
7478 bytesSent = 0 ;
7579 while (bytesSent < length )
7680 {
77- bytesWritten = write (radio , buffer , length );
81+ bytesWritten = write (radio , & buffer [ bytesSent ] , length - bytesSent );
7882 if (bytesWritten < 0 )
7983 {
8084 perror ("ERROR: Write of data to radio failed!" );
@@ -118,7 +122,7 @@ int hostToRadio(uint8_t destVc, uint8_t * buffer, int length)
118122 bytesSent = 0 ;
119123 while (bytesSent < length )
120124 {
121- bytesWritten = write (radio , buffer , length );
125+ bytesWritten = write (radio , & buffer [ bytesSent ] , length - bytesSent );
122126 if (bytesWritten < 0 )
123127 {
124128 perror ("ERROR: Write of data to radio failed!" );
@@ -219,7 +223,7 @@ int stdinToRadio()
219223 return status ;
220224}
221225
222- int hostToStdout (uint8_t * data , uint8_t bytesToSend )
226+ int hostToStdout (VC_SERIAL_MESSAGE_HEADER * header , uint8_t * data , uint8_t bytesToSend )
223227{
224228 uint8_t * buffer ;
225229 uint8_t * bufferEnd ;
@@ -298,12 +302,13 @@ int hostToStdout(uint8_t * data, uint8_t bytesToSend)
298302 return status ;
299303}
300304
301- void radioToPcLinkStatus (VC_SERIAL_MESSAGE_HEADER * header , uint8_t length )
305+ void radioToPcLinkStatus (VC_SERIAL_MESSAGE_HEADER * header , uint8_t * data , uint8_t length )
302306{
303307 char cmdBuffer [128 ];
304308 int newState ;
305309 int previousState ;
306310 int srcVc ;
311+ uint8_t uniqueId [UNIQUE_ID_BYTES ];
307312 VC_STATE_MESSAGE * vcMsg ;
308313
309314 //Remember the previous state
@@ -315,6 +320,47 @@ void radioToPcLinkStatus(VC_SERIAL_MESSAGE_HEADER * header, uint8_t length)
315320 newState = vcMsg -> vcState ;
316321 virtualCircuitList [srcVc ].vcState = newState ;
317322
323+ //Save the LoRaSerial radio's unique ID
324+ //Determine if the PC's value is valid
325+ memset (uniqueId , UNIQUE_ID_ERASE_VALUE , sizeof (uniqueId ));
326+ if (!virtualCircuitList [srcVc ].valid )
327+ {
328+ //Determine if the radio knows the value
329+ if (memcmp (vcMsg -> uniqueId , uniqueId , sizeof (uniqueId )) != 0 )
330+ {
331+ //The radio knows the value, save it in the PC
332+ memcpy (virtualCircuitList [srcVc ].uniqueId , vcMsg -> uniqueId , sizeof (vcMsg -> uniqueId ));
333+ virtualCircuitList [srcVc ].valid = true;
334+
335+ //Display this ID value
336+ printf ("VC %d unique ID: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n" ,
337+ srcVc ,
338+ vcMsg -> uniqueId [0 ], vcMsg -> uniqueId [1 ], vcMsg -> uniqueId [2 ], vcMsg -> uniqueId [3 ],
339+ vcMsg -> uniqueId [4 ], vcMsg -> uniqueId [5 ], vcMsg -> uniqueId [6 ], vcMsg -> uniqueId [7 ],
340+ vcMsg -> uniqueId [8 ], vcMsg -> uniqueId [9 ], vcMsg -> uniqueId [10 ], vcMsg -> uniqueId [11 ],
341+ vcMsg -> uniqueId [12 ], vcMsg -> uniqueId [13 ], vcMsg -> uniqueId [14 ], vcMsg -> uniqueId [15 ]);
342+ }
343+ }
344+ else
345+ {
346+ //Determine if the radio has changed for this VC
347+ if ((memcmp (vcMsg -> uniqueId , virtualCircuitList [srcVc ].uniqueId , sizeof (vcMsg -> uniqueId )) != 0 )
348+ && (memcmp (vcMsg -> uniqueId , uniqueId , sizeof (uniqueId )) != 0 ))
349+ {
350+ //The radio knows the value, save it in the PC
351+ memcpy (virtualCircuitList [srcVc ].uniqueId , vcMsg -> uniqueId , sizeof (vcMsg -> uniqueId ));
352+ virtualCircuitList [srcVc ].valid = true;
353+
354+ //Display this ID value
355+ printf ("VC %d unique ID: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n" ,
356+ srcVc ,
357+ vcMsg -> uniqueId [0 ], vcMsg -> uniqueId [1 ], vcMsg -> uniqueId [2 ], vcMsg -> uniqueId [3 ],
358+ vcMsg -> uniqueId [4 ], vcMsg -> uniqueId [5 ], vcMsg -> uniqueId [6 ], vcMsg -> uniqueId [7 ],
359+ vcMsg -> uniqueId [8 ], vcMsg -> uniqueId [9 ], vcMsg -> uniqueId [10 ], vcMsg -> uniqueId [11 ],
360+ vcMsg -> uniqueId [12 ], vcMsg -> uniqueId [13 ], vcMsg -> uniqueId [14 ], vcMsg -> uniqueId [15 ]);
361+ }
362+ }
363+
318364 //Display the state if requested
319365 if (DISPLAY_STATE_TRANSITION )
320366 printf ("VC%d: %s --> %s\n" , srcVc , vcStateNames [previousState ], vcStateNames [newState ]);
@@ -383,7 +429,7 @@ void radioToPcLinkStatus(VC_SERIAL_MESSAGE_HEADER * header, uint8_t length)
383429 }
384430}
385431
386- void radioDataAck (uint8_t * data , uint8_t length )
432+ void radioDataAck (VC_SERIAL_MESSAGE_HEADER * header , uint8_t * data , uint8_t length )
387433{
388434 VC_DATA_ACK_NACK_MESSAGE * vcMsg ;
389435
@@ -392,19 +438,26 @@ void radioDataAck(uint8_t * data, uint8_t length)
392438 printf ("ACK from VC %d\n" , vcMsg -> msgDestVc );
393439}
394440
395- void radioDataNack (uint8_t * data , uint8_t length )
441+ void radioDataNack (VC_SERIAL_MESSAGE_HEADER * header , uint8_t * data , uint8_t length )
396442{
443+ int vcIndex ;
397444 VC_DATA_ACK_NACK_MESSAGE * vcMsg ;
398445
399446 vcMsg = (VC_DATA_ACK_NACK_MESSAGE * )data ;
447+ vcIndex = vcMsg -> msgDestVc & VCAB_NUMBER_MASK ;
400448 if (DISPLAY_DATA_NACK )
401- printf ("NACK from VC %d\n" , vcMsg -> msgDestVc );
449+ printf ("NACK from VC %d\n" , vcIndex );
450+
451+ //Set the VC state to down
452+ virtualCircuitList [vcIndex ].vcState = VC_STATE_LINK_DOWN ;
402453}
403454
404- void radioCommandComplete (uint8_t srcVc , uint8_t * data , uint8_t length )
455+ void radioCommandComplete (VC_SERIAL_MESSAGE_HEADER * header , uint8_t * data , uint8_t length )
405456{
406457 VC_COMMAND_COMPLETE_MESSAGE * vcMsg ;
458+ uint8_t srcVc ;
407459
460+ srcVc = header -> radio .srcVc ;
408461 vcMsg = (VC_COMMAND_COMPLETE_MESSAGE * )data ;
409462 if (DISPLAY_COMMAND_COMPLETE )
410463 printf ("Command complete from VC %d: %s\n" , srcVc ,
@@ -461,7 +514,7 @@ int radioToHost()
461514 length = data - dataStart ;
462515 if (length )
463516 //Output the debug data
464- hostToStdout (dataStart , length );
517+ hostToStdout (NULL , dataStart , length );
465518
466519 //Determine if this is the beginning of a virtual circuit message
467520 length = dataEnd - data ;
@@ -501,8 +554,8 @@ int radioToHost()
501554 {
502555 printf ("VC Header:\n" );
503556 printf (" length: %d\n" , header -> radio .length );
504- printf (" destVc: %d\n" , header -> radio .destVc );
505- printf (" srcVc: %d\n" , header -> radio .srcVc );
557+ printf (" destVc: %d (0x%02x) \n" , ( uint8_t ) header -> radio . destVc , ( uint8_t ) header -> radio .destVc );
558+ printf (" srcVc: %d (0x%02x) \n" , header -> radio . srcVc , header -> radio .srcVc );
506559 if (length > 0 )
507560 dumpBuffer (data , length );
508561 }
@@ -513,29 +566,29 @@ int radioToHost()
513566
514567 //Display link status
515568 if (header -> radio .destVc == PC_LINK_STATUS )
516- radioToPcLinkStatus (header , VC_SERIAL_HEADER_BYTES + length );
569+ radioToPcLinkStatus (header , data , VC_SERIAL_HEADER_BYTES + length );
517570
518571 //Display remote command response
519572 else if (header -> radio .destVc == (PC_REMOTE_RESPONSE | myVc ))
520- status = hostToStdout (data , length );
573+ status = hostToStdout (header , data , length );
521574
522575 //Display command completion status
523576 else if (header -> radio .destVc == PC_COMMAND_COMPLETE )
524- radioCommandComplete (header -> radio . srcVc , data , length );
577+ radioCommandComplete (header , data , length );
525578
526579 //Display ACKs for transmitted messages
527580 else if (header -> radio .destVc == PC_DATA_ACK )
528- radioDataAck (data , length );
581+ radioDataAck (header , data , length );
529582
530583 //Display NACKs for transmitted messages
531584 else if (header -> radio .destVc == PC_DATA_NACK )
532- radioDataNack (data , length );
585+ radioDataNack (header , data , length );
533586
534587 //Display received messages
535588 else if ((header -> radio .destVc == myVc ) || (header -> radio .destVc == VC_BROADCAST ))
536589 {
537590 //Output this message
538- status = hostToStdout (data , length );
591+ status = hostToStdout (header , data , length );
539592 }
540593
541594 //Unknown messages
@@ -545,8 +598,8 @@ int radioToHost()
545598 {
546599 printf ("Unknown message, VC Header:\n" );
547600 printf (" length: %d\n" , header -> radio .length );
548- printf (" destVc: %d\n" , header -> radio .destVc );
549- printf (" srcVc: %d\n" , header -> radio .srcVc );
601+ printf (" destVc: %d (0x%02x) \n" , ( uint8_t ) header -> radio . destVc , ( uint8_t ) header -> radio .destVc );
602+ printf (" srcVc: %d (0x%02x) \n" , header -> radio . srcVc , header -> radio .srcVc );
550603 if (length > 0 )
551604 dumpBuffer (data , length );
552605 }
0 commit comments