Skip to content

Commit 3c34515

Browse files
committed
refine poll period management
1 parent 6c88c9b commit 3c34515

File tree

1 file changed

+35
-36
lines changed
  • software/apps/permamote_coap

1 file changed

+35
-36
lines changed

software/apps/permamote_coap/main.c

+35-36
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef struct{
8686
bool send_motion;
8787
bool send_periodic;
8888
bool update_time;
89-
bool up_time_done;
89+
bool up_time_wait;
9090
bool resolve_addr;
9191
bool resolve_wait;
9292
bool dfu_trigger;
@@ -180,8 +180,6 @@ void dfu_monitor_callback(void* m) {
180180
coap_dfu_diagnostic_get(&dfu_state);
181181
NRF_LOG_INFO("state: %s", background_dfu_state_to_string(dfu_state.state));
182182
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);
185183

186184
// if this state combination, there isn't a new image for us, or server not
187185
// responding, so return to normal operation
@@ -191,7 +189,6 @@ void dfu_monitor_callback(void* m) {
191189
//app_timer_stop(coap_tick);
192190
coap_dfu_reset_state();
193191
nrf_dfu_settings_progress_reset();
194-
otLinkSetPollPeriod(thread_get_instance(), DEFAULT_POLL_PERIOD);
195192
app_timer_stop(dfu_monitor);
196193
NRF_LOG_INFO("Aborted DFU operation: Image already installed or server not responding");
197194
}
@@ -252,22 +249,34 @@ void ntp_response_handler(void* context, uint64_t time, otError error) {
252249
else {
253250
NRF_LOG_INFO("ntp error: %d", error);
254251
}
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+
}
256265
}
257266

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) {
259268
uint8_t role = otThreadGetDeviceRole(p_context);
260269

261270
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;
271280
}
272281
if (state.external_route_added && role == 2) {
273282
state.has_internet = true;
@@ -542,11 +551,11 @@ void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) {
542551
void state_step(void) {
543552
otInstance * thread_instance = thread_get_instance();
544553
bool ready_to_send = state.has_internet && are_addresses_resolved();
554+
uint32_t poll = otLinkGetPollPeriod(thread_get_instance());
545555

546556
if (state.send_periodic) {
547557
ab1815_tickle_watchdog();
548558
period_count ++;
549-
uint32_t poll = otLinkGetPollPeriod(thread_get_instance());
550559
NRF_LOG_INFO("poll period: %d", poll);
551560

552561
if (!ready_to_send) {
@@ -579,7 +588,6 @@ void state_step(void) {
579588
// Send discovery packet and version before updating
580589
send_version();
581590

582-
otLinkSetPollPeriod(thread_instance, DFU_POLL_PERIOD);
583591
coap_remote_t remote;
584592
memcpy(&remote.addr, &m_up_address, OT_IP6_ADDRESS_SIZE);
585593
remote.port_number = 5683;
@@ -600,7 +608,6 @@ void state_step(void) {
600608
if (state.has_internet) {
601609
if (state.resolve_addr) {
602610
NRF_LOG_INFO("Resolving Addresses");
603-
otLinkSetPollPeriod(thread_instance, RECV_POLL_PERIOD);
604611
thread_dns_hostname_resolve(thread_instance,
605612
DNS_SERVER_ADDR,
606613
NTP_SERVER_HOSTNAME,
@@ -623,7 +630,6 @@ void state_step(void) {
623630
if (are_addresses_resolved() || did_resolution_fail())
624631
{
625632
state.resolve_wait = false;
626-
otLinkSetPollPeriod(thread_instance, DEFAULT_POLL_PERIOD);
627633
}
628634
}
629635
}
@@ -640,31 +646,24 @@ void state_step(void) {
640646

641647
if (state.update_time && !state.resolve_wait && !state.resolve_addr && are_addresses_resolved()) {
642648
state.update_time = false;
649+
state.up_time_wait = true;
643650
NRF_LOG_INFO("RTC UPDATE");
644651

645652
// request ntp time update
646653
otError error = thread_ntp_request(thread_instance, &m_ntp_address, NULL, ntp_response_handler);
647654
NRF_LOG_INFO("sent ntp poll!");
648655
NRF_LOG_INFO("error: %d", error);
649-
otLinkSetPollPeriod(thread_instance, RECV_POLL_PERIOD);
650656
}
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);
667663
}
664+
665+
} else if (poll != DEFAULT_POLL_PERIOD){
666+
otLinkSetPollPeriod(thread_instance, DEFAULT_POLL_PERIOD);
668667
}
669668

670669
if (state.do_reset) {

0 commit comments

Comments
 (0)