15
15
* limitations under the License.
16
16
*/
17
17
18
+ #include <string.h>
18
19
#include "nsconfig.h"
19
20
#include "ns_types.h"
20
21
#include "ns_trace.h"
21
22
#include "net_interface.h"
22
23
#include "eventOS_event.h"
23
24
#include "NWK_INTERFACE/Include/protocol.h"
25
+ #include "6LoWPAN/Bootstraps/protocol_6lowpan.h"
24
26
#include "6LoWPAN/Bootstraps/protocol_6lowpan_interface.h"
27
+ #include "6LoWPAN/MAC/mac_helper.h"
28
+ #include "6LoWPAN/MAC/mac_data_poll.h"
25
29
#include "6LoWPAN/ws/ws_common.h"
26
30
#include "6LoWPAN/ws/ws_bootstrap.h"
31
+ #include "RPL/rpl_control.h"
32
+ #include "RPL/rpl_data.h"
33
+ #include "Common_Protocols/icmpv6.h"
34
+ #include "Common_Protocols/icmpv6_radv.h"
27
35
36
+ #include "net_rpl.h"
28
37
#include "mac_api.h"
29
38
30
39
#define TRACE_GROUP "wsbs"
31
40
32
41
#ifdef HAVE_WS
33
-
34
- static void ws_bootstrap_tasklet (arm_event_s * event )
35
- {
36
- ws_bootsrap_event_type_e event_type ;
37
- event_type = (ws_bootsrap_event_type_e )event -> event_type ;
38
- protocol_interface_info_entry_t * cur ;
39
- cur = protocol_stack_interface_info_get_by_bootstrap_id (event -> receiver );
40
- if (!cur ) {
41
- return ;
42
- }
43
-
44
- switch (event_type ) {
45
- case WS_INIT_EVENT :
46
- tr_debug ("tasklet init" );
47
- break ;
48
- case WS_DISCOVERY_START :
49
- tr_debug ("Discovery start" );
50
- ws_bootstrap_configuration_start (cur );
51
-
52
- break ;
53
- case WS_CONFIGURATION_START :
54
- tr_debug ("Configuration start" );
55
- ws_bootstrap_authentication_start (cur );
56
- break ;
57
- case WS_AUTHENTICATION_START :
58
- tr_debug ("authentication start" );
59
- ws_bootstrap_operation_start (cur );
60
- break ;
61
- case WS_OPERATION_START :
62
- tr_debug ("operation start" );
63
- // Activate RPL
64
- // Activate IPv6 stack
65
- break ;
66
- default :
67
- tr_err ("Invalid event received" );
68
- break ;
69
- }
70
-
71
- }
42
+ static void ws_bootstrap_event_handler (arm_event_s * event );
43
+ static void ws_bootstrap_state_change (protocol_interface_info_entry_t * cur , icmp_state_t nwk_bootstrap_state );
72
44
73
45
static int ws_bootstrap_tasklet_init (protocol_interface_info_entry_t * cur )
74
46
{
75
47
if (cur -> bootStrapId < 0 ) {
76
- cur -> bootStrapId = eventOS_event_handler_create (& ws_bootstrap_tasklet , WS_INIT_EVENT );
48
+ cur -> bootStrapId = eventOS_event_handler_create (& ws_bootstrap_event_handler , WS_INIT_EVENT );
77
49
tr_debug ("WS tasklet init" );
78
50
}
79
51
@@ -116,13 +88,13 @@ static int8_t ws_bootsrap_event_trig(ws_bootsrap_event_type_e event_type, int8_t
116
88
protocol_6lowpan_register_handlers (cur );
117
89
addr_interface_set_ll64 (cur , NULL );
118
90
cur -> nwk_nd_re_scan_count = 0 ;
119
- //thread_interface_up (cur);
91
+ //WS_interface_up (cur);
120
92
// Trigger discovery for bootstrap
121
93
ret_val = nwk_6lowpan_up (cur );
122
94
if (ret_val ) {
123
95
goto cleanup ;
124
96
}
125
- ws_bootstrap_discovery_start (cur );
97
+ ws_bootstrap_event_discovery_start (cur );
126
98
127
99
return 0 ;
128
100
cleanup :
@@ -149,6 +121,7 @@ void ws_bootstrap_configuration_reset(protocol_interface_info_entry_t *cur)
149
121
150
122
// Do not process beacons
151
123
cur -> mac_parameters -> beacon_ind = NULL ;
124
+ cur -> mac_parameters -> mac_security_level = 0 ;
152
125
153
126
// Set default parameters to interface
154
127
cur -> configure_flags = INTERFACE_BOOTSTRAP_DEFINED ;
@@ -169,6 +142,7 @@ void ws_bootstrap_configuration_reset(protocol_interface_info_entry_t *cur)
169
142
tr_err ("Invalid bootstrap_mode" );
170
143
}
171
144
145
+ cur -> nwk_bootstrap_state = ER_ACTIVE_SCAN ;
172
146
//cur->mac_security_key_usage_update_cb = ws_management_mac_security_key_update_cb;
173
147
return ;
174
148
}
@@ -226,21 +200,213 @@ int ws_bootstrap_init(int8_t interface_id, net_6lowpan_mode_e bootstrap_mode)
226
200
return 0 ;
227
201
}
228
202
229
- void ws_bootstrap_discovery_start (protocol_interface_info_entry_t * cur )
203
+ static void ws_bootstrap_mac_activate (protocol_interface_info_entry_t * cur , uint16_t channel , uint16_t panid , bool coordinator )
204
+ {
205
+ mlme_start_t start_req ;
206
+ memset (& start_req , 0 , sizeof (mlme_start_t ));
207
+
208
+ cur -> mac_parameters -> pan_id = panid ;
209
+ cur -> mac_parameters -> mac_channel = channel ;
210
+
211
+ start_req .PANId = panid ;
212
+ start_req .LogicalChannel = channel ;
213
+ start_req .BeaconOrder = 0x0f ;
214
+ start_req .SuperframeOrder = 0x0f ;
215
+ start_req .PANCoordinator = coordinator ;
216
+
217
+ if (cur -> mac_api ) {
218
+ cur -> mac_api -> mlme_req (cur -> mac_api , MLME_START , (void * )& start_req );
219
+ }
220
+ }
221
+ static void ws_bootstrap_mac_interface_activate (protocol_interface_info_entry_t * cur )
222
+ {
223
+ tr_debug ("MAC init" );
224
+ mac_helper_pib_boolean_set (cur , macRxOnWhenIdle , true);
225
+ cur -> lowpan_info &= ~INTERFACE_NWK_CONF_MAC_RX_OFF_IDLE ;
226
+
227
+ //mac_data_poll_init(cur);
228
+ //mac_helper_mac16_address_set(cur, 0xffff);
229
+ //mac_helper_default_security_level_set(cur, cur->mac_parameters->mac_configured_sec_level);
230
+ //mac_helper_default_security_key_id_mode_set(cur,MAC_KEY_ID_MODE_IDX);
231
+ ws_bootstrap_mac_activate (cur , 11 , 0xabab , true);
232
+ return ;
233
+ }
234
+ static void ws_bootstrap_ip_stack_activate (protocol_interface_info_entry_t * cur )
235
+ {
236
+ tr_debug ("ip stack init" );
237
+ clear_power_state (ICMP_ACTIVE );
238
+ cur -> lowpan_info |= INTERFACE_NWK_BOOTSRAP_ACTIVE ;
239
+ }
240
+ static void ws_bootstrap_rpl_callback (rpl_event_t event , void * handle )
241
+ {
242
+
243
+ protocol_interface_info_entry_t * cur = handle ;
244
+ if (!cur -> rpl_domain || cur -> interface_mode != INTERFACE_UP ) {
245
+ return ;
246
+ }
247
+ tr_warn ("RPL callback event %d" , event );
248
+ }
249
+ static uint8_t dodag_id [16 ] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 };
250
+ static uint8_t dodag_prefix [16 ] = {0xfd ,00 ,0xaa };
251
+
252
+ static void ws_bootstrap_rpl_root_activate (protocol_interface_info_entry_t * cur )
253
+ {
254
+ tr_debug ("RPL root Activate" );
255
+ rpl_dodag_conf_t new_conf ;
256
+ new_conf .default_lifetime = 64 ;
257
+ new_conf .lifetime_unit = 60 ;
258
+ new_conf .dag_max_rank_increase = 3072 ;
259
+ new_conf .min_hop_rank_increase = 128 ;// 24 HOP?
260
+ new_conf .dio_interval_min = 9 ;
261
+ new_conf .dio_interval_doublings = 12 ;
262
+ new_conf .dio_redundancy_constant = 10 ;
263
+ new_conf .objective_code_point = 1 ;
264
+ new_conf .authentication = 0 ;
265
+ new_conf .path_control_size = 0 ;
266
+ rpl_data_init_root ();
267
+ protocol_6lowpan_rpl_root_dodag = rpl_control_create_dodag_root (protocol_6lowpan_rpl_domain , 1 , dodag_id , & new_conf , new_conf .min_hop_rank_increase , RPL_GROUNDED | RPL_MODE_NON_STORING | RPL_DODAG_PREF (0 ));
268
+ if (!protocol_6lowpan_rpl_root_dodag ) {
269
+ tr_err ("RPL dodag init failed" );
270
+ }
271
+ uint8_t t_flags = PIO_A ;
272
+
273
+ icmpv6_slaac_address_add (cur , dodag_prefix , 64 , 0xffffffff , 0xffffffff , true, SLAAC_IID_FIXED );
274
+
275
+ rpl_control_update_dodag_prefix (protocol_6lowpan_rpl_root_dodag , dodag_prefix , 64 , t_flags , 0xffffffff , 0xffffffff , false);
276
+ rpl_control_update_dodag_route (protocol_6lowpan_rpl_root_dodag , dodag_prefix , 64 , 0 , 0xffffffff , false);
277
+ rpl_control_update_dodag_route (protocol_6lowpan_rpl_root_dodag , NULL , 0 , 0 , 0xffffffff , false);
278
+ }
279
+
280
+ static void ws_bootstrap_rpl_activate (protocol_interface_info_entry_t * cur )
281
+ {
282
+ tr_debug ("RPL Activate" );
283
+ bool downstream = true;
284
+ bool leaf = false;
285
+
286
+ addr_add_router_groups (cur );
287
+ rpl_control_set_domain_on_interface (cur , protocol_6lowpan_rpl_domain , downstream );
288
+ rpl_control_set_callback (protocol_6lowpan_rpl_domain , ws_bootstrap_rpl_callback , cur );
289
+ // If i am router I Do this
290
+ rpl_control_force_leaf (protocol_6lowpan_rpl_domain , leaf );
291
+
292
+ if (cur -> bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_ROUTER ) {
293
+ rpl_control_transmit_dis (cur -> rpl_domain , cur , 0 , 0 , NULL , 0 , ADDR_LINK_LOCAL_ALL_ROUTERS );
294
+ } else if (cur -> bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER ) {
295
+ ws_bootstrap_rpl_root_activate (cur );
296
+ }
297
+
298
+ }
299
+
300
+
301
+ static void ws_bootstrap_icmp_rs_msg_tx (protocol_interface_info_entry_t * cur )
230
302
{
231
- ws_bootsrap_event_trig (WS_DISCOVERY_START , cur -> bootStrapId , ARM_LIB_HIGH_PRIORITY_EVENT );
303
+ tr_debug ("send RS" );
304
+ buffer_t * buf = icmpv6_build_rs (cur , NULL );
305
+
306
+ protocol_push (buf );
232
307
}
233
- void ws_bootstrap_configuration_start (protocol_interface_info_entry_t * cur )
308
+
309
+ /*
310
+ * Event transitions
311
+ *
312
+ * */
313
+
314
+ void ws_bootstrap_event_discovery_start (protocol_interface_info_entry_t * cur )
234
315
{
235
- ws_bootsrap_event_trig (WS_CONFIGURATION_START , cur -> bootStrapId , ARM_LIB_HIGH_PRIORITY_EVENT );
316
+ ws_bootsrap_event_trig (WS_DISCOVERY_START , cur -> bootStrapId , ARM_LIB_LOW_PRIORITY_EVENT );
236
317
}
237
- void ws_bootstrap_authentication_start (protocol_interface_info_entry_t * cur )
318
+ void ws_bootstrap_event_configuration_start (protocol_interface_info_entry_t * cur )
238
319
{
239
- ws_bootsrap_event_trig (WS_AUTHENTICATION_START , cur -> bootStrapId , ARM_LIB_HIGH_PRIORITY_EVENT );
320
+ ws_bootsrap_event_trig (WS_CONFIGURATION_START , cur -> bootStrapId , ARM_LIB_LOW_PRIORITY_EVENT );
321
+ }
322
+ void ws_bootstrap_event_authentication_start (protocol_interface_info_entry_t * cur )
323
+ {
324
+ ws_bootsrap_event_trig (WS_AUTHENTICATION_START , cur -> bootStrapId , ARM_LIB_LOW_PRIORITY_EVENT );
325
+ }
326
+ void ws_bootstrap_event_operation_start (protocol_interface_info_entry_t * cur )
327
+ {
328
+ ws_bootsrap_event_trig (WS_OPERATION_START , cur -> bootStrapId , ARM_LIB_LOW_PRIORITY_EVENT );
329
+ }
330
+ void ws_bootstrap_event_routing_ready (protocol_interface_info_entry_t * cur )
331
+ {
332
+ ws_bootsrap_event_trig (WS_ROUTING_READY , cur -> bootStrapId , ARM_LIB_LOW_PRIORITY_EVENT );
333
+ }
334
+
335
+ static void ws_bootstrap_event_handler (arm_event_s * event )
336
+ {
337
+ ws_bootsrap_event_type_e event_type ;
338
+ event_type = (ws_bootsrap_event_type_e )event -> event_type ;
339
+ protocol_interface_info_entry_t * cur ;
340
+ cur = protocol_stack_interface_info_get_by_bootstrap_id (event -> receiver );
341
+ if (!cur ) {
342
+ return ;
343
+ }
344
+
345
+ switch (event_type ) {
346
+ case WS_INIT_EVENT :
347
+ tr_debug ("tasklet init" );
348
+ break ;
349
+ case WS_DISCOVERY_START :
350
+ tr_info ("Discovery start" );
351
+ ws_bootstrap_mac_interface_activate (cur );
352
+ ws_bootstrap_state_change (cur , ER_ACTIVE_SCAN );
353
+ break ;
354
+ case WS_CONFIGURATION_START :
355
+ tr_info ("Configuration start" );
356
+ ws_bootstrap_event_authentication_start (cur );
357
+ break ;
358
+ case WS_AUTHENTICATION_START :
359
+ tr_info ("authentication start" );
360
+ ws_bootstrap_event_operation_start (cur );
361
+ break ;
362
+ case WS_OPERATION_START :
363
+ tr_info ("operation start" );
364
+ // Activate RPL
365
+ // Activate IPv6 stack
366
+ ws_bootstrap_ip_stack_activate (cur );
367
+ ws_bootstrap_rpl_activate (cur );
368
+ ws_bootstrap_event_routing_ready (cur );
369
+ break ;
370
+ case WS_ROUTING_READY :
371
+ tr_info ("Routing ready" );
372
+ ws_bootstrap_state_change (cur , ER_BOOTSRAP_DONE );
373
+ break ;
374
+ default :
375
+ tr_err ("Invalid event received" );
376
+ break ;
377
+ }
378
+
240
379
}
241
- void ws_bootstrap_operation_start (protocol_interface_info_entry_t * cur )
380
+
381
+
382
+ /*
383
+ * State machine
384
+ *
385
+ * */
386
+
387
+ static void ws_bootstrap_state_change (protocol_interface_info_entry_t * cur , icmp_state_t nwk_bootstrap_state )
242
388
{
243
- ws_bootsrap_event_trig (WS_OPERATION_START , cur -> bootStrapId , ARM_LIB_HIGH_PRIORITY_EVENT );
389
+ cur -> bootsrap_state_machine_cnt = 1 ;
390
+ cur -> nwk_bootstrap_state = nwk_bootstrap_state ;
244
391
}
392
+ void ws_bootstrap_state_machine (protocol_interface_info_entry_t * cur )
393
+ {
394
+
395
+ switch (cur -> nwk_bootstrap_state ) {
396
+ case ER_ACTIVE_SCAN :
397
+ tr_debug ("WS SM:Active Scan" );
398
+ ws_bootstrap_event_configuration_start (cur );
399
+ break ;
400
+
401
+ case ER_BOOTSRAP_DONE :
402
+ tr_debug ("WS SM:Bootstrap Done" );
403
+ // Bootstrap_done event to application
404
+ nwk_bootsrap_state_update (ARM_NWK_BOOTSTRAP_READY , cur );
405
+ break ;
245
406
407
+ default :
408
+ tr_warn ("WS SM:Invalid state %d" ,cur -> nwk_bootstrap_state );
409
+
410
+ }
411
+ }
246
412
#endif //HAVE_WS
0 commit comments