|
55 | 55 | #include <openthread/thread.h>
|
56 | 56 | #include <openthread/icmp6.h>
|
57 | 57 | #include <openthread/platform/alarm-milli.h>
|
| 58 | +#include <openthread/platform/misc.h> |
58 | 59 |
|
59 | 60 | APP_TIMER_DEF(m_led_send_timer);
|
60 | 61 | APP_TIMER_DEF(m_led_recv_timer);
|
61 | 62 | APP_TIMER_DEF(m_boot_timer);
|
| 63 | +APP_TIMER_DEF(m_poll_period_fast_timer); |
62 | 64 |
|
63 | 65 | static otIcmp6Handler m_icmp6_handler;
|
64 | 66 |
|
@@ -96,7 +98,7 @@ static void icmp_receive_callback(void * p_context,
|
96 | 98 | }
|
97 | 99 | }
|
98 | 100 |
|
99 |
| -static uint32_t m_poll_period; |
| 101 | +static uint32_t m_poll_period = 0; |
100 | 102 |
|
101 | 103 | static void boot_request_handler(void *, otMessage *, const otMessageInfo *);
|
102 | 104 | static void info_request_handler(void *, otMessage *, const otMessageInfo *);
|
@@ -172,6 +174,63 @@ int16_t get_sensor_index(char sensor_name)
|
172 | 174 | return -1;
|
173 | 175 | }
|
174 | 176 |
|
| 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 | + |
175 | 234 | static void coap_default_handler(void * p_context, otMessage * p_message, const otMessageInfo * p_message_info)
|
176 | 235 | {
|
177 | 236 | UNUSED_PARAMETER(p_context);
|
@@ -272,6 +331,10 @@ size_t fill_info_packet(uint8_t *pBuffer, size_t stBufferSize)
|
272 | 331 | if (cborError != CborNoError)
|
273 | 332 | return 0;
|
274 | 333 |
|
| 334 | + cborError = cbor_encode_map_set_int(&encoderMap, "r", otPlatGetResetReason(thread_ot_instance_get())); |
| 335 | + if (cborError != CborNoError) |
| 336 | + return 0; |
| 337 | + |
275 | 338 | uint8_t macAddr[8];
|
276 | 339 | otPlatRadioGetIeeeEui64(thread_ot_instance_get(), macAddr);
|
277 | 340 |
|
@@ -509,8 +572,7 @@ static otError get_response_send(otMessage *p_request_message, const otMessageIn
|
509 | 572 | otMessage *p_response;
|
510 | 573 | otInstance *p_instance = thread_ot_instance_get();
|
511 | 574 |
|
512 |
| - do |
513 |
| - { |
| 575 | + do { |
514 | 576 | p_response = otCoapNewMessage(p_instance, NULL);
|
515 | 577 | if (p_response == NULL)
|
516 | 578 | break;
|
@@ -545,8 +607,7 @@ static otError get_response_send(otMessage *p_request_message, const otMessageIn
|
545 | 607 |
|
546 | 608 | static void get_request_handler(void *p_context, otMessage *p_message, const otMessageInfo *p_message_info)
|
547 | 609 | {
|
548 |
| - do |
549 |
| - { |
| 610 | + do { |
550 | 611 | if (otCoapMessageGetType(p_message) != OT_COAP_TYPE_CONFIRMABLE)
|
551 | 612 | break;
|
552 | 613 |
|
@@ -856,15 +917,22 @@ static void sub_request_handler(void * p_context, otMessage * p_message, const o
|
856 | 917 | blink_recv_led();
|
857 | 918 | }
|
858 | 919 |
|
| 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 | + |
859 | 928 | static void send_subscription_broadcast()
|
860 | 929 | {
|
861 | 930 | otError error = OT_ERROR_NONE;
|
862 | 931 | otMessage * p_request;
|
863 | 932 | otMessageInfo message_info;
|
864 | 933 | otInstance * p_instance = thread_ot_instance_get();
|
865 | 934 |
|
866 |
| - do |
867 |
| - { |
| 935 | + do { |
868 | 936 | p_request = otCoapNewMessage(p_instance, NULL);
|
869 | 937 | if (p_request == NULL)
|
870 | 938 | break;
|
@@ -900,10 +968,12 @@ static void send_subscription_broadcast()
|
900 | 968 | if (error != OT_ERROR_NONE)
|
901 | 969 | break;
|
902 | 970 |
|
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); |
904 | 972 | if (error != OT_ERROR_NONE)
|
905 | 973 | break;
|
906 | 974 |
|
| 975 | + poll_period_fast_set(); |
| 976 | + |
907 | 977 | blink_send_led();
|
908 | 978 | } while (false);
|
909 | 979 |
|
@@ -1003,8 +1073,7 @@ static void subscription_timeout_handler(void *p_context)
|
1003 | 1073 | otMessageInfo message_info;
|
1004 | 1074 | otInstance * p_instance = thread_ot_instance_get();
|
1005 | 1075 |
|
1006 |
| - do |
1007 |
| - { |
| 1076 | + do { |
1008 | 1077 | p_request = otCoapNewMessage(p_instance, NULL);
|
1009 | 1078 | if (p_request == NULL)
|
1010 | 1079 | break;
|
@@ -1091,6 +1160,9 @@ void thread_coap_utils_init()
|
1091 | 1160 | error_code = app_timer_create(&m_boot_timer, APP_TIMER_MODE_SINGLE_SHOT, boot_timer_handler);
|
1092 | 1161 | APP_ERROR_CHECK(error_code);
|
1093 | 1162 |
|
| 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 | + |
1094 | 1166 | memset(&m_icmp6_handler, 0, sizeof(m_icmp6_handler));
|
1095 | 1167 | m_icmp6_handler.mReceiveCallback = icmp_receive_callback;
|
1096 | 1168 |
|
|
0 commit comments