53
53
#include "6LoWPAN/Thread/thread_extension_bootstrap.h"
54
54
#include "6LoWPAN/Thread/thread_extension_constants.h"
55
55
#include "6LoWPAN/Thread/thread_neighbor_class.h"
56
+ #include "6LoWPAN/Thread/thread_nvm_store.h"
56
57
#include "6LoWPAN/MAC/mac_helper.h"
57
58
#include "Common_Protocols/icmpv6.h"
58
59
#include "NWK_INTERFACE/Include/protocol.h"
@@ -74,6 +75,7 @@ typedef struct thread_extension_info {
74
75
uint8_t sequence_number ;
75
76
uint32_t delay_timer ;
76
77
uint32_t mlr_timer ;
78
+ timeout_t * reset_timeout ;
77
79
uint16_t rloc ;
78
80
uint16_t relay_port_ae ;
79
81
uint16_t relay_port_nmkp ;
@@ -195,6 +197,114 @@ static int thread_extension_mtd_dua_ntf_cb(int8_t service_id, uint8_t source_add
195
197
return 0 ;
196
198
}
197
199
200
+ static void thread_extension_reset_timeout_cb (void * arg )
201
+ {
202
+ protocol_interface_info_entry_t * cur = (protocol_interface_info_entry_t * )arg ;
203
+
204
+ if (!cur ) {
205
+ return ;
206
+ }
207
+
208
+ // Delete all domain stuff and start discovery.
209
+ thread_extension_bootstrap_network_certificate_set (cur , NULL , 0 );
210
+ thread_extension_bootstrap_network_private_key_set (cur , NULL , 0 );
211
+ thread_nvm_store_active_configuration_remove ();
212
+ thread_nvm_store_mleid_rloc_map_remove ();
213
+ thread_nvm_store_pending_configuration_remove ();
214
+ thread_nvm_store_link_info_clear ();
215
+ thread_joiner_application_link_configuration_delete (cur -> id );
216
+ thread_bootstrap_connection_error (cur -> id , CON_ERROR_NETWORK_KICK , NULL );
217
+ }
218
+
219
+ /*
220
+ * Commissioner requests to leave this domain
221
+ */
222
+ static int thread_extension_reset_req_cb (int8_t service_id , uint8_t source_address [static 16 ], uint16_t source_port , sn_coap_hdr_s * request_ptr )
223
+ {
224
+ (void ) source_address ;
225
+ (void ) source_port ;
226
+
227
+ uint8_t payload [3 ] = {0 };
228
+ uint8_t * ptr = payload ;
229
+ uint8_t * signature_ptr = NULL ;
230
+ uint16_t session_id = 0 ;
231
+ protocol_interface_info_entry_t * cur = protocol_stack_interface_info_get_by_id (thread_management_server_interface_id_get (service_id ));
232
+
233
+ if (!cur || !request_ptr ) {
234
+ return -1 ;
235
+ }
236
+
237
+ tr_debug ("Received MGMT_RESET.req" );
238
+
239
+ // Verify request TLV's: Commissioner Session ID TLV - Commissioner Token TLV (optional) - Commissioner Signature TLV
240
+ thread_meshcop_tlv_data_get_uint16 (request_ptr -> payload_ptr , request_ptr -> payload_len , MESHCOP_TLV_COMMISSIONER_SESSION_ID , & session_id );
241
+ thread_meshcop_tlv_find (request_ptr -> payload_ptr , request_ptr -> payload_len , MESHCOP_TLV_COMM_SIGNATURE , & signature_ptr );
242
+
243
+ if ((session_id != thread_info (cur )-> registered_commissioner .session_id ) /*|| (signature_ptr == NULL)*/ ) { // todo: signature may not come in this early phase of implementors
244
+ tr_debug ("Request parse failed" );
245
+ ptr = thread_meshcop_tlv_data_write_uint8 (ptr , MESHCOP_TLV_STATE , -1 );
246
+ }
247
+
248
+ // Downgrade if router
249
+ if (thread_am_router (cur )) {
250
+ thread_bootstrap_attached_downgrade_router (cur );
251
+ }
252
+
253
+ // Delete all data and start reattach
254
+ // Get some time to send response and keep the Commissioner happy
255
+ cur -> thread_info -> extension_info -> reset_timeout = eventOS_timeout_ms (thread_extension_reset_timeout_cb , 5000 , cur );
256
+
257
+ // Send response
258
+ coap_service_response_send (service_id , COAP_REQUEST_OPTIONS_NONE , request_ptr , COAP_MSG_CODE_RESPONSE_CHANGED , COAP_CT_NONE , payload , ptr - payload );
259
+
260
+ return 0 ;
261
+ }
262
+
263
+
264
+ /*
265
+ * Commissioner requests to get new certificate from Registrar, but to stay in the same domain
266
+ */
267
+ static int thread_extension_reenroll_req_cb (int8_t service_id , uint8_t source_address [static 16 ], uint16_t source_port , sn_coap_hdr_s * request_ptr )
268
+ {
269
+ (void ) source_address ;
270
+ (void ) source_port ;
271
+
272
+ uint8_t pbbr_addr [16 ] = {0 };
273
+ uint8_t status_tlv [3 ] = {0 };
274
+ uint8_t * ptr = status_tlv ;
275
+ uint8_t * signature_ptr = NULL ;
276
+ uint16_t session_id = 0 ;
277
+ protocol_interface_info_entry_t * cur = protocol_stack_interface_info_get_by_id (thread_management_server_interface_id_get (service_id ));
278
+
279
+ if (!cur || !request_ptr ) {
280
+ return -1 ;
281
+ }
282
+
283
+ tr_debug ("Received MGMT_REENROLL.req" );
284
+
285
+ // Verify request TLV's: Commissioner Session ID TLV - Commissioner Token TLV (optional) - Commissioner Signature TLV
286
+ thread_meshcop_tlv_data_get_uint16 (request_ptr -> payload_ptr , request_ptr -> payload_len , MESHCOP_TLV_COMMISSIONER_SESSION_ID , & session_id );
287
+ thread_meshcop_tlv_find (request_ptr -> payload_ptr , request_ptr -> payload_len , MESHCOP_TLV_COMM_SIGNATURE , & signature_ptr );
288
+
289
+ if ((session_id != thread_info (cur )-> registered_commissioner .session_id ) || /* (signature_ptr == NULL) || */
290
+ thread_extension_primary_bbr_get (cur , pbbr_addr , NULL , NULL , NULL )) {
291
+ tr_debug ("Request parse failed" );
292
+ ptr = thread_meshcop_tlv_data_write_uint8 (ptr , MESHCOP_TLV_STATE , -1 );
293
+ goto send_response ;
294
+ }
295
+
296
+
297
+
298
+ thread_extension_bootstrap_reenrollment_start (cur , service_id , pbbr_addr );
299
+
300
+ send_response :
301
+
302
+ // send response
303
+ coap_service_response_send (service_id , COAP_REQUEST_OPTIONS_NONE , request_ptr , COAP_MSG_CODE_RESPONSE_CHANGED , COAP_CT_NONE , status_tlv , ptr - status_tlv );
304
+ return 0 ;
305
+ }
306
+
307
+
198
308
static int thread_extension_primary_bbr_update_needed (struct protocol_interface_info_entry * cur , uint16_t rloc , uint8_t seq )
199
309
{
200
310
if (!cur -> thread_info -> extension_info ) {
@@ -444,6 +554,8 @@ void thread_extension_init(int8_t interface_id, int8_t coap_service_id)
444
554
return ;
445
555
}
446
556
cur -> thread_info -> extension_info -> coap_service_id = coap_service_id ;
557
+ coap_service_register_uri (thread_info (cur )-> extension_info -> coap_service_id , THREAD_URI_RESET_REQ , COAP_SERVICE_ACCESS_POST_ALLOWED , thread_extension_reset_req_cb );
558
+ coap_service_register_uri (thread_info (cur )-> extension_info -> coap_service_id , THREAD_URI_REENROLL_REQ , COAP_SERVICE_ACCESS_POST_ALLOWED , thread_extension_reenroll_req_cb );
447
559
}
448
560
449
561
void thread_extension_mtd_service_register (protocol_interface_info_entry_t * cur )
0 commit comments