@@ -101,7 +101,7 @@ bool dn_qsl_init(void)
101101 sizeof (dn_fsm_vars .notifBuf ),
102102 dn_ipmt_reply_cb
103103 );
104-
104+
105105 dn_fsm_enterState (DN_FSM_STATE_DISCONNECTED , 0 );
106106 return TRUE;
107107}
@@ -254,7 +254,8 @@ uint8_t dn_qsl_read(uint8_t* readBuffer)
254254 */
255255static void dn_fsm_run (void )
256256{
257- if (dn_fsm_vars .fsmArmed && (dn_time_ms () - dn_fsm_vars .fsmEventScheduled_ms > dn_fsm_vars .fsmDelay_ms ))
257+ uint32_t timePassed_ms = dn_time_ms () - dn_fsm_vars .fsmEventScheduled_ms ; // Handle dn_time_ms wrap around
258+ if (dn_fsm_vars .fsmArmed && (timePassed_ms > dn_fsm_vars .fsmDelay_ms ))
258259 {
259260 // Scheduled event is due; execute it
260261 dn_fsm_vars .fsmArmed = FALSE;
@@ -305,7 +306,7 @@ static void dn_fsm_setReplyCallback(dn_fsm_reply_cbt cb)
305306 dn_fsm_vars .replyCb = cb ;
306307}
307308
308- //===== setReplyCallback
309+ //===== enterState
309310
310311/**
311312 Transition FSM to new state and schedule any default entry events.
@@ -316,8 +317,8 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
316317 uint16_t delay = DN_CMD_PERIOD_MS ;
317318 static uint32_t lastTransition = 0 ;
318319 if (lastTransition == 0 )
319- lastTransition = dn_time_ms () ;
320-
320+ lastTransition = now ;
321+
321322 // Use default delay if none given
322323 if (spesificDelay > 0 )
323324 delay = spesificDelay ;
@@ -361,7 +362,7 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
361362 }
362363
363364 debug ("FSM state transition: %#.2x --> %#.2x (%u ms)" ,
364- dn_fsm_vars .state , newState , now - lastTransition );
365+ dn_fsm_vars .state , newState , ( uint32_t )( now - lastTransition ) );
365366 lastTransition = now ;
366367 dn_fsm_vars .state = newState ;
367368}
@@ -374,7 +375,8 @@ static void dn_fsm_enterState(uint8_t newState, uint16_t spesificDelay)
374375 */
375376static bool dn_fsm_cmd_timeout (uint32_t cmdStart_ms , uint32_t cmdTimeout_ms )
376377{
377- bool timeout = (dn_time_ms () - cmdStart_ms ) > cmdTimeout_ms ;
378+ uint32_t timePassed_ms = dn_time_ms () - cmdStart_ms ; // Handle dn_time_ms wrap around
379+ bool timeout = timePassed_ms > cmdTimeout_ms ;
378380 if (timeout )
379381 {
380382 // Cancel any ongoing transmission or scheduled event and reset reply cb
@@ -502,7 +504,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
502504 case CMDID_RECEIVE :
503505 notif_receive = (dn_ipmt_receive_nt * )dn_fsm_vars .notifBuf ;
504506 debug ("Received downstream data" );
505-
507+
506508 // Push payload at tail of inbox
507509 memcpy
508510 (
@@ -520,7 +522,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
520522 dn_fsm_vars .inbox .unreadPackets ++ ;
521523 }
522524 debug ("Inbox capacity at %u / %u" , dn_fsm_vars .inbox .unreadPackets , DN_INBOX_SIZE );
523-
525+
524526 break ;
525527 case CMDID_MACRX :
526528 // Not implemented
@@ -531,7 +533,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
531533 case CMDID_ADVRECEIVED :
532534 notif_advReceived = (dn_ipmt_advReceived_nt * )dn_fsm_vars .notifBuf ;
533535 debug ("Received network advertisement" );
534-
536+
535537 if (dn_fsm_vars .state == DN_FSM_STATE_PROMISCUOUS
536538 && dn_fsm_vars .networkId == DN_PROMISCUOUS_NET_ID )
537539 {
@@ -540,7 +542,7 @@ static void dn_ipmt_notif_cb(uint8_t cmdId, uint8_t subCmdId)
540542 dn_fsm_vars .networkId = notif_advReceived -> netId ;
541543 dn_fsm_scheduleEvent (DN_CMD_PERIOD_MS , dn_event_setNetworkId );
542544 }
543-
545+
544546 break ;
545547 default :
546548 log_warn ("Unknown notification ID" );
@@ -993,7 +995,7 @@ static void dn_reply_setNetworkId(void)
993995static void dn_event_search (void )
994996{
995997 debug ("Search" );
996-
998+
997999 // Arm reply callback
9981000 dn_fsm_setReplyCallback (dn_reply_search );
9991001
@@ -1199,7 +1201,7 @@ static void dn_reply_getServiceInfo(void)
11991201 log_warn ("Only granted service of %u ms (requested %u ms)" , reply -> value , dn_fsm_vars .service_ms );
12001202 dn_fsm_enterState (DN_FSM_STATE_DISCONNECTED , 0 );
12011203 }
1202-
1204+
12031205 } else
12041206 {
12051207 debug ("Service request still pending" );
@@ -1304,7 +1306,7 @@ static dn_err_t checkAndSaveNetConfig(uint16_t netID, const uint8_t* joinKey, ui
13041306 {
13051307 memcpy (dn_fsm_vars .joinKey , joinKey , DN_JOIN_KEY_LEN );
13061308 }
1307-
1309+
13081310 if (srcPort == 0 )
13091311 {
13101312 debug ("No source port given; using default" );
@@ -1328,7 +1330,7 @@ static uint8_t getPayloadLimit(uint16_t destPort)
13281330 bool destIsF0Bx = (destPort >= DN_WELL_KNOWN_PORT_1 && destPort <= DN_WELL_KNOWN_PORT_8 );
13291331 bool srcIsF0Bx = (dn_fsm_vars .srcPort >= DN_WELL_KNOWN_PORT_1 && dn_fsm_vars .srcPort <= DN_WELL_KNOWN_PORT_8 );
13301332 int8_t destIsMng = memcmp (DN_DEST_IP , DN_DEFAULT_DEST_IP , DN_IPv6ADDR_LEN );
1331-
1333+
13321334 if (destIsMng == 0 )
13331335 {
13341336 if (destIsF0Bx && srcIsF0Bx )
0 commit comments