@@ -86,7 +86,7 @@ typedef struct{
86
86
bool send_motion ;
87
87
bool send_periodic ;
88
88
bool update_time ;
89
- bool up_time_done ;
89
+ bool up_time_wait ;
90
90
bool resolve_addr ;
91
91
bool resolve_wait ;
92
92
bool dfu_trigger ;
@@ -180,8 +180,6 @@ void dfu_monitor_callback(void* m) {
180
180
coap_dfu_diagnostic_get (& dfu_state );
181
181
NRF_LOG_INFO ("state: %s" , background_dfu_state_to_string (dfu_state .state ));
182
182
NRF_LOG_INFO ("prev state: %s" , background_dfu_state_to_string (dfu_state .prev_state ));
183
- uint32_t poll = otLinkGetPollPeriod (thread_get_instance ());
184
- NRF_LOG_INFO ("poll period: %d" , poll );
185
183
186
184
// if this state combination, there isn't a new image for us, or server not
187
185
// responding, so return to normal operation
@@ -191,7 +189,6 @@ void dfu_monitor_callback(void* m) {
191
189
//app_timer_stop(coap_tick);
192
190
coap_dfu_reset_state ();
193
191
nrf_dfu_settings_progress_reset ();
194
- otLinkSetPollPeriod (thread_get_instance (), DEFAULT_POLL_PERIOD );
195
192
app_timer_stop (dfu_monitor );
196
193
NRF_LOG_INFO ("Aborted DFU operation: Image already installed or server not responding" );
197
194
}
@@ -252,22 +249,34 @@ void ntp_response_handler(void* context, uint64_t time, otError error) {
252
249
else {
253
250
NRF_LOG_INFO ("ntp error: %d" , error );
254
251
}
255
- state .up_time_done = true;
252
+ state .up_time_wait = false;
253
+
254
+ // if we haven't performed initial setup of rand
255
+ if (!seed ) {
256
+ srand ((uint32_t )time & UINT32_MAX );
257
+ // set jitter for next dfu check
258
+ dfu_jitter_hours = (int )(rand () / (float ) RAND_MAX * DFU_CHECK_HOURS );
259
+ NRF_LOG_INFO ("JITTER: %u" , dfu_jitter_hours );
260
+ // send version and discover because why not
261
+ send_thread_info ();
262
+ send_version ();
263
+ seed = true;
264
+ }
256
265
}
257
266
258
- void __attribute__(( weak )) thread_state_changed_callback (uint32_t flags , void * p_context ) {
267
+ void thread_state_changed_callback (uint32_t flags , void * p_context ) {
259
268
uint8_t role = otThreadGetDeviceRole (p_context );
260
269
261
270
NRF_LOG_INFO ("State changed! Flags: 0x%08lx Current role: %d" ,
262
- flags , role )
263
-
264
- if (flags & OT_CHANGED_IP6_ADDRESS_ADDED && role == 2 ) {
265
- NRF_LOG_INFO ("We have internet connectivity!" );
266
- addresses_print (p_context );
267
- state .external_route_added = true;
268
- APP_ERROR_CHECK (app_timer_start (dns_delay , APP_TIMER_TICKS (5000 ), NULL ));
269
- //state.resolve_addr = true;
270
- //state.update_time = true;
271
+ flags , role )
272
+
273
+ if (flags & OT_CHANGED_IP6_ADDRESS_ADDED && role == 2 ) {
274
+ NRF_LOG_INFO ("We have internet connectivity!" );
275
+ addresses_print (p_context );
276
+ state .external_route_added = true;
277
+ APP_ERROR_CHECK (app_timer_start (dns_delay , APP_TIMER_TICKS (5000 ), NULL ));
278
+ //state.resolve_addr = true;
279
+ //state.update_time = true;
271
280
}
272
281
if (state .external_route_added && role == 2 ) {
273
282
state .has_internet = true;
@@ -542,11 +551,11 @@ void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) {
542
551
void state_step (void ) {
543
552
otInstance * thread_instance = thread_get_instance ();
544
553
bool ready_to_send = state .has_internet && are_addresses_resolved ();
554
+ uint32_t poll = otLinkGetPollPeriod (thread_get_instance ());
545
555
546
556
if (state .send_periodic ) {
547
557
ab1815_tickle_watchdog ();
548
558
period_count ++ ;
549
- uint32_t poll = otLinkGetPollPeriod (thread_get_instance ());
550
559
NRF_LOG_INFO ("poll period: %d" , poll );
551
560
552
561
if (!ready_to_send ) {
@@ -579,7 +588,6 @@ void state_step(void) {
579
588
// Send discovery packet and version before updating
580
589
send_version ();
581
590
582
- otLinkSetPollPeriod (thread_instance , DFU_POLL_PERIOD );
583
591
coap_remote_t remote ;
584
592
memcpy (& remote .addr , & m_up_address , OT_IP6_ADDRESS_SIZE );
585
593
remote .port_number = 5683 ;
@@ -600,7 +608,6 @@ void state_step(void) {
600
608
if (state .has_internet ) {
601
609
if (state .resolve_addr ) {
602
610
NRF_LOG_INFO ("Resolving Addresses" );
603
- otLinkSetPollPeriod (thread_instance , RECV_POLL_PERIOD );
604
611
thread_dns_hostname_resolve (thread_instance ,
605
612
DNS_SERVER_ADDR ,
606
613
NTP_SERVER_HOSTNAME ,
@@ -623,7 +630,6 @@ void state_step(void) {
623
630
if (are_addresses_resolved () || did_resolution_fail ())
624
631
{
625
632
state .resolve_wait = false;
626
- otLinkSetPollPeriod (thread_instance , DEFAULT_POLL_PERIOD );
627
633
}
628
634
}
629
635
}
@@ -640,31 +646,24 @@ void state_step(void) {
640
646
641
647
if (state .update_time && !state .resolve_wait && !state .resolve_addr && are_addresses_resolved ()) {
642
648
state .update_time = false;
649
+ state .up_time_wait = true;
643
650
NRF_LOG_INFO ("RTC UPDATE" );
644
651
645
652
// request ntp time update
646
653
otError error = thread_ntp_request (thread_instance , & m_ntp_address , NULL , ntp_response_handler );
647
654
NRF_LOG_INFO ("sent ntp poll!" );
648
655
NRF_LOG_INFO ("error: %d" , error );
649
- otLinkSetPollPeriod (thread_instance , RECV_POLL_PERIOD );
650
656
}
651
- if (state .up_time_done ) {
652
- // if not performing a dfu, return poll period to default
653
- if (!state .performing_dfu ) {
654
- otLinkSetPollPeriod (thread_get_instance (), DEFAULT_POLL_PERIOD );
655
- }
656
- // if we haven't performed initial setup of rand
657
- if (!seed ) {
658
- srand ((uint32_t )time & UINT32_MAX );
659
- // set jitter for next dfu check
660
- dfu_jitter_hours = (int )(rand () / (float ) RAND_MAX * DFU_CHECK_HOURS );
661
- NRF_LOG_INFO ("JITTER: %u" , dfu_jitter_hours );
662
- // send version and discover because why not
663
- send_thread_info ();
664
- send_version ();
665
- seed = true;
666
- }
657
+ }
658
+
659
+ // manage poll period. if we are waiting for any transfer (dns, ntp, etc.) we want a fast poll rate, otherwise, a slow one.
660
+ if (state .resolve_wait || state .up_time_wait || state .performing_dfu ) {
661
+ if (poll != RECV_POLL_PERIOD ) {
662
+ otLinkSetPollPeriod (thread_instance , RECV_POLL_PERIOD );
667
663
}
664
+
665
+ } else if (poll != DEFAULT_POLL_PERIOD ){
666
+ otLinkSetPollPeriod (thread_instance , DEFAULT_POLL_PERIOD );
668
667
}
669
668
670
669
if (state .do_reset ) {
0 commit comments