Skip to content

Commit 030c658

Browse files
committed
Fast poll for sleepy devices, send reset reason
1 parent 277a304 commit 030c658

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

settings.h

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#define VOLTAGE_TIMER_INTERVAL 1000
1010
#define HDC1080_TIMER_INTERVAL 5000
1111

12+
#define DEFAULT_POLL_PERIOD 120000
13+
#define DEFAULT_POLL_PERIOD_FAST 50
14+
#define DEFAULT_POLL_PERIOD_FAST_TIMEOUT 500
15+
#define DEFAULT_CHILD_TIMEOUT 240
16+
1217
#define ADC_SAMPLES_PER_CHANNEL 32
1318

1419
#define LED_SEND_NOTIFICATION BSP_BOARD_LED_0

thread_coap_utils.c

+82-10
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@
5555
#include <openthread/thread.h>
5656
#include <openthread/icmp6.h>
5757
#include <openthread/platform/alarm-milli.h>
58+
#include <openthread/platform/misc.h>
5859

5960
APP_TIMER_DEF(m_led_send_timer);
6061
APP_TIMER_DEF(m_led_recv_timer);
6162
APP_TIMER_DEF(m_boot_timer);
63+
APP_TIMER_DEF(m_poll_period_fast_timer);
6264

6365
static otIcmp6Handler m_icmp6_handler;
6466

@@ -96,7 +98,7 @@ static void icmp_receive_callback(void * p_context,
9698
}
9799
}
98100

99-
static uint32_t m_poll_period;
101+
static uint32_t m_poll_period = 0;
100102

101103
static void boot_request_handler(void *, otMessage *, const otMessageInfo *);
102104
static void info_request_handler(void *, otMessage *, const otMessageInfo *);
@@ -172,6 +174,63 @@ int16_t get_sensor_index(char sensor_name)
172174
return -1;
173175
}
174176

177+
static uint32_t poll_period_fast_set(void)
178+
{
179+
uint32_t error;
180+
otError ot_error;
181+
otInstance * p_instance = thread_ot_instance_get();
182+
183+
do {
184+
if (otThreadGetLinkMode(p_instance).mRxOnWhenIdle) {
185+
error = NRF_ERROR_INVALID_STATE;
186+
break;
187+
}
188+
189+
if (!m_poll_period) {
190+
m_poll_period = otLinkGetPollPeriod(p_instance);
191+
192+
ot_error = otLinkSetPollPeriod(p_instance, DEFAULT_POLL_PERIOD_FAST);
193+
ASSERT(ot_error == OT_ERROR_NONE);
194+
195+
error = NRF_SUCCESS;
196+
197+
app_timer_start(m_poll_period_fast_timer, APP_TIMER_TICKS(DEFAULT_POLL_PERIOD_FAST_TIMEOUT), NULL);
198+
} else {
199+
error = NRF_ERROR_BUSY;
200+
}
201+
} while (false);
202+
203+
return error;
204+
}
205+
206+
static void poll_period_restore(void)
207+
{
208+
otError error;
209+
otInstance * p_instance = thread_ot_instance_get();
210+
211+
do {
212+
if (otThreadGetLinkMode(p_instance).mRxOnWhenIdle) {
213+
break;
214+
}
215+
216+
if (m_poll_period) {
217+
error = otLinkSetPollPeriod(p_instance, m_poll_period);
218+
ASSERT(error == OT_ERROR_NONE);
219+
220+
m_poll_period = 0;
221+
222+
app_timer_stop(m_poll_period_fast_timer);
223+
}
224+
} while (false);
225+
}
226+
227+
static void poll_period_fast_timer_handler(void * p_context)
228+
{
229+
UNUSED_PARAMETER(p_context);
230+
231+
poll_period_restore();
232+
}
233+
175234
static void coap_default_handler(void * p_context, otMessage * p_message, const otMessageInfo * p_message_info)
176235
{
177236
UNUSED_PARAMETER(p_context);
@@ -272,6 +331,10 @@ size_t fill_info_packet(uint8_t *pBuffer, size_t stBufferSize)
272331
if (cborError != CborNoError)
273332
return 0;
274333

334+
cborError = cbor_encode_map_set_int(&encoderMap, "r", otPlatGetResetReason(thread_ot_instance_get()));
335+
if (cborError != CborNoError)
336+
return 0;
337+
275338
uint8_t macAddr[8];
276339
otPlatRadioGetIeeeEui64(thread_ot_instance_get(), macAddr);
277340

@@ -509,8 +572,7 @@ static otError get_response_send(otMessage *p_request_message, const otMessageIn
509572
otMessage *p_response;
510573
otInstance *p_instance = thread_ot_instance_get();
511574

512-
do
513-
{
575+
do {
514576
p_response = otCoapNewMessage(p_instance, NULL);
515577
if (p_response == NULL)
516578
break;
@@ -545,8 +607,7 @@ static otError get_response_send(otMessage *p_request_message, const otMessageIn
545607

546608
static void get_request_handler(void *p_context, otMessage *p_message, const otMessageInfo *p_message_info)
547609
{
548-
do
549-
{
610+
do {
550611
if (otCoapMessageGetType(p_message) != OT_COAP_TYPE_CONFIRMABLE)
551612
break;
552613

@@ -856,15 +917,22 @@ static void sub_request_handler(void * p_context, otMessage * p_message, const o
856917
blink_recv_led();
857918
}
858919

920+
static void subscription_response_handler(void * p_context,
921+
otMessage * p_message,
922+
const otMessageInfo * p_message_info,
923+
otError result)
924+
{
925+
poll_period_restore();
926+
}
927+
859928
static void send_subscription_broadcast()
860929
{
861930
otError error = OT_ERROR_NONE;
862931
otMessage * p_request;
863932
otMessageInfo message_info;
864933
otInstance * p_instance = thread_ot_instance_get();
865934

866-
do
867-
{
935+
do {
868936
p_request = otCoapNewMessage(p_instance, NULL);
869937
if (p_request == NULL)
870938
break;
@@ -900,10 +968,12 @@ static void send_subscription_broadcast()
900968
if (error != OT_ERROR_NONE)
901969
break;
902970

903-
error = otCoapSendRequest(p_instance, p_request, &message_info, NULL, NULL);
971+
error = otCoapSendRequest(p_instance, p_request, &message_info, subscription_response_handler, p_instance);
904972
if (error != OT_ERROR_NONE)
905973
break;
906974

975+
poll_period_fast_set();
976+
907977
blink_send_led();
908978
} while (false);
909979

@@ -1003,8 +1073,7 @@ static void subscription_timeout_handler(void *p_context)
10031073
otMessageInfo message_info;
10041074
otInstance * p_instance = thread_ot_instance_get();
10051075

1006-
do
1007-
{
1076+
do {
10081077
p_request = otCoapNewMessage(p_instance, NULL);
10091078
if (p_request == NULL)
10101079
break;
@@ -1091,6 +1160,9 @@ void thread_coap_utils_init()
10911160
error_code = app_timer_create(&m_boot_timer, APP_TIMER_MODE_SINGLE_SHOT, boot_timer_handler);
10921161
APP_ERROR_CHECK(error_code);
10931162

1163+
error_code = app_timer_create(&m_poll_period_fast_timer, APP_TIMER_MODE_SINGLE_SHOT, poll_period_fast_timer_handler);
1164+
APP_ERROR_CHECK(error_code);
1165+
10941166
memset(&m_icmp6_handler, 0, sizeof(m_icmp6_handler));
10951167
m_icmp6_handler.mReceiveCallback = icmp_receive_callback;
10961168

0 commit comments

Comments
 (0)