/* * \brief Configure mesh network * */ void thread_tasklet_configure_and_connect_to_network(void) { int8_t status; link_configuration_s* temp_link_config=NULL; if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_MINIMAL_END_DEVICE) { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_HOST; } else if (MBED_CONF_MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_SLEEPY_END_DEVICE) { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_SLEEPY_HOST; } else { thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER; } arm_nwk_interface_configure_6lowpan_bootstrap_set( thread_tasklet_data_ptr->nwk_if_id, thread_tasklet_data_ptr->operating_mode, NET_6LOWPAN_THREAD); thread_tasklet_data_ptr->channel_list.channel_page = (channel_page_e)MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_PAGE; thread_tasklet_data_ptr->channel_list.channel_mask[0] = MBED_CONF_MBED_MESH_API_THREAD_CONFIG_CHANNEL_MASK; TRACE_DETAIL("channel page: %d", thread_tasklet_data_ptr->channel_list.channel_page); TRACE_DETAIL("channel mask: 0x%.8lx", thread_tasklet_data_ptr->channel_list.channel_mask[0]); // PSKd const char PSKd[] = MBED_CONF_MBED_MESH_API_THREAD_PSKD; MBED_ASSERT(sizeof(PSKd) > 5 && sizeof(PSKd) < 33); char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd)); strcpy(dyn_buf, PSKd); ns_dyn_mem_free(device_configuration.PSKd_ptr); device_configuration.PSKd_ptr = (uint8_t*)dyn_buf; device_configuration.PSKd_len = sizeof(PSKd) - 1; if (true == MBED_CONF_MBED_MESH_API_THREAD_USE_STATIC_LINK_CONFIG) { read_link_configuration(); temp_link_config = &thread_tasklet_data_ptr->link_config; } thread_management_node_init(thread_tasklet_data_ptr->nwk_if_id, &thread_tasklet_data_ptr->channel_list, &device_configuration, temp_link_config); status = arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id); if (status >= 0) { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; tr_info("Start Thread bootstrap (%s mode)", thread_tasklet_data_ptr->operating_mode == NET_6LOWPAN_SLEEPY_HOST ? "SED" : "Router"); } else { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; tr_err("Bootstrap start failed, %d", status); thread_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); } }
/* * \brief Configure and establish network connection * */ void nd_tasklet_configure_and_connect_to_network(void) { int8_t status; char *sec_mode; // configure bootstrap arm_nwk_interface_configure_6lowpan_bootstrap_set( tasklet_data_ptr->network_interface_id, tasklet_data_ptr->mode, NET_6LOWPAN_ND_WITH_MLE); sec_mode = STR(MBED_MESH_API_6LOWPAN_ND_SECURITY_MODE); if (strcmp(sec_mode, "PSK") == 0) { tr_debug("Using PSK security mode."); tasklet_data_ptr->sec_mode = NET_SEC_MODE_PSK_LINK_SECURITY; tasklet_data_ptr->psk_sec_info.key_id = MBED_MESH_API_6LOWPAN_ND_PSK_KEY_ID; memcpy(tasklet_data_ptr->psk_sec_info.security_key, (const uint8_t[16])MBED_MESH_API_6LOWPAN_ND_PSK_KEY, 16); } else { tr_debug("Link-layer security NOT enabled."); tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY; } // configure link layer security arm_nwk_link_layer_security_mode( tasklet_data_ptr->network_interface_id, tasklet_data_ptr->sec_mode, MBED_MESH_API_6LOWPAN_ND_SEC_LEVEL, &tasklet_data_ptr->psk_sec_info); // configure scan parameters arm_nwk_6lowpan_link_scan_parameter_set(tasklet_data_ptr->network_interface_id, 5); // configure scan channels initialize_channel_list(); // Configure scan options (NULL disables filter) arm_nwk_6lowpan_link_nwk_id_filter_for_nwk_scan( tasklet_data_ptr->network_interface_id, NULL); arm_nwk_6lowpan_link_panid_filter_for_nwk_scan( tasklet_data_ptr->network_interface_id, MBED_MESH_API_6LOWPAN_ND_PANID_FILTER); status = arm_nwk_interface_up(tasklet_data_ptr->network_interface_id); if (status >= 0) { tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; tr_info("Start 6LoWPAN ND Bootstrap"); } else { tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; tr_err("Bootstrap start failed, %d", status); nd_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); } }
/* * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver. * @param event, describes the sender, receiver and event type. * * NOTE: Interrupts requested by HW are possible during this function! */ void thread_tasklet_main(arm_event_s *event) { arm_library_event_type_e event_type; event_type = (arm_library_event_type_e) event->event_type; switch (event_type) { case ARM_LIB_NWK_INTERFACE_EVENT: /* This event is delivered every and each time when there is new * information of network connectivity. */ thread_tasklet_parse_network_event(event); break; case ARM_LIB_TASKLET_INIT_EVENT: /* Event with type EV_INIT is an initializer event of NanoStack OS. * The event is delivered when the NanoStack OS is running fine. * This event should be delivered ONLY ONCE. */ mesh_system_send_connect_event(thread_tasklet_data_ptr->tasklet); break; case ARM_LIB_SYSTEM_TIMER_EVENT: eventOS_event_timer_cancel(event->event_id, thread_tasklet_data_ptr->tasklet); if (event->event_id == TIMER_EVENT_START_BOOTSTRAP) { int8_t status; tr_debug("Restart bootstrap"); status = arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id); if (status >= 0) { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; tr_info("Start Thread bootstrap (%s mode)", thread_tasklet_data_ptr->operating_mode == NET_6LOWPAN_SLEEPY_HOST ? "SED" : "Router"); thread_tasklet_network_state_changed(MESH_BOOTSTRAP_STARTED); } else { thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; tr_err("Bootstrap start failed, %d", status); thread_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); } } break; case APPLICATION_EVENT: if (event->event_id == APPL_EVENT_CONNECT) { thread_tasklet_configure_and_connect_to_network(); } break; default: break; } // switch(event_type) }
/* * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver. * @param event, describes the sender, receiver and event type. * * NOTE: Interrupts requested by HW are possible during this function! */ void nd_tasklet_main(arm_event_s *event) { arm_library_event_type_e event_type; event_type = (arm_library_event_type_e) event->event_type; switch (event_type) { case ARM_LIB_NWK_INTERFACE_EVENT: /* This event is delivered every and each time when there is new * information of network connectivity. */ nd_tasklet_parse_network_event(event); break; case ARM_LIB_TASKLET_INIT_EVENT: /* Event with type EV_INIT is an initializer event of NanoStack OS. * The event is delivered when the NanoStack OS is running fine. * This event should be delivered ONLY ONCE. */ tasklet_data_ptr->node_main_tasklet_id = event->receiver; mesh_system_send_connect_event(tasklet_data_ptr->tasklet); break; case ARM_LIB_SYSTEM_TIMER_EVENT: eventOS_event_timer_cancel(event->event_id, tasklet_data_ptr->node_main_tasklet_id); if (event->event_id == TIMER_EVENT_START_BOOTSTRAP) { tr_debug("Restart bootstrap"); arm_nwk_interface_up(tasklet_data_ptr->network_interface_id); } break; case APPLICATION_EVENT: if (event->event_id == APPL_EVENT_CONNECT) { nd_tasklet_configure_and_connect_to_network(); } break; default: break; } // switch(event_type) }
/* * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver. * @param event, describes the sender, receiver and event type. * * NOTE: Interrupts requested by HW are possible during this function! */ void tasklet_main(arm_event_s *event) { uint8_t master_key[16]; /**Border Router ND setup*/ static link_configuration_s link_setup; device_configuration_s device_config; uint8_t networkid[16]; uint32_t channel_mask; channel_mask = (uint32_t) 1 << RF_CHANNEL; char psdK[] = "Kukkuu!"; device_config.PSKd_ptr = psdK; device_config.leaderCap = true; int8_t retval; if (event->sender == 0) { arm_library_event_type_e event_type; event_type = (arm_library_event_type_e) event->event_type; switch (event_type) { //This event is delivered every and each time when there is new information of network connectivity. case ARM_LIB_NWK_INTERFACE_EVENT: /* Network Event state event handler */ app_parse_network_event(event); break; case ARM_LIB_TASKLET_INIT_EVENT: /*Event with type EV_INIT is an initialiser event of NanoStack OS. * The event is delivered when the NanoStack OS is running fine. This event should be delivered ONLY ONCE. */ node_main_tasklet_id = event->receiver; /* Bootsrapping Thread network */ arm_nwk_interface_configure_6lowpan_bootstrap_set(net_rf_id, NET_6LOWPAN_ROUTER, NET_6LOWPAN_THREAD); arm_nwk_6lowpan_link_scan_paramameter_set(net_rf_id, channel_list, 5); rf_read_mac_address(link_setup.extended_random_mac); link_setup.extended_random_mac[0] |= 0x02; //Initialize Thread stack to leader mode link_setup.steering_data_len = 0; memcpy(networkid, "Arm Powered Core", 16); memcpy(link_setup.name, networkid, 16); link_setup.panId = THREAD_NTW_PAN_ID; link_setup.rfChannel = RF_CHANNEL; link_setup.Protocol_id = THREAD_PROTOCOL_ID; link_setup.version = THREAD_PROTOCOL_VERSION; memcpy(link_setup.mesh_local_ula_prefix,br_thread_ml_prefix, 8); arm_nwk_6lowpan_gp_address_mode(net_rf_id, NET_6LOWPAN_GP16_ADDRESS, 0xffff, 1); memcpy(master_key, default_net_security_key, 16); memcpy(link_setup.master_key,default_net_security_key, 16 ); link_setup.key_rotation = 3600; link_setup.key_sequence = 0; memcpy(link_setup.mesh_local_ula_prefix,br_thread_ml_prefix, 8); thread_managenet_node_init(net_rf_id, channel_mask, &device_config, &link_setup); retval = arm_nwk_interface_up(net_rf_id); if (retval != 0) { //debug_int(retval); tr_debug("Start Fail code: %d", retval); } else { tr_debug("6LoWPAN IP Bootstrap started"); network_ready(); } break; case ARM_LIB_SYSTEM_TIMER_EVENT: eventOS_event_timer_cancel(event->event_id, node_main_tasklet_id); if(event->event_id == 1) { if(arm_nwk_interface_up(net_rf_id) == 0) { tr_debug("Start Network bootstrap Again"); } } break; default: break; } } }