Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
nd_tasklet.c
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <string.h> //memset 00018 #include "eventOS_event_timer.h" 00019 #include "common_functions.h" 00020 #include "net_interface.h" 00021 #include "ip6string.h" //ip6tos 00022 #include "nsdynmemLIB.h" 00023 #include "include/nd_tasklet.h" 00024 #include "include/static_config.h" 00025 #include "include/mesh_system.h" 00026 #include "ns_event_loop.h" 00027 00028 // For tracing we need to define flag, have include and define group 00029 #define HAVE_DEBUG 1 00030 #include "ns_trace.h" 00031 #define TRACE_GROUP "m6LND" 00032 00033 #include "mac_api.h" 00034 #include "sw_mac.h" 00035 00036 #define INTERFACE_NAME "6L-ND" 00037 00038 // Tasklet timer events 00039 #define TIMER_EVENT_START_BOOTSTRAP 1 00040 00041 #define INVALID_INTERFACE_ID (-1) 00042 00043 #define STR_HELPER(x) #x 00044 #define STR(x) STR_HELPER(x) 00045 00046 /* 00047 * Mesh tasklet states. 00048 */ 00049 typedef enum { 00050 TASKLET_STATE_CREATED = 0, 00051 TASKLET_STATE_INITIALIZED, 00052 TASKLET_STATE_BOOTSTRAP_STARTED, 00053 TASKLET_STATE_BOOTSTRAP_FAILED, 00054 TASKLET_STATE_BOOTSTRAP_READY 00055 } tasklet_state_t; 00056 00057 /* 00058 * Mesh tasklet data structure. 00059 */ 00060 typedef struct { 00061 void (*mesh_api_cb)(mesh_connection_status_t nwk_status); 00062 channel_list_s channel_list; 00063 tasklet_state_t tasklet_state; 00064 net_6lowpan_mode_e mode; 00065 net_6lowpan_link_layer_sec_mode_e sec_mode; 00066 net_link_layer_psk_security_info_s psk_sec_info; 00067 int8_t node_main_tasklet_id; 00068 int8_t network_interface_id; 00069 int8_t tasklet; 00070 } tasklet_data_str_t; 00071 00072 /* Tasklet data */ 00073 static tasklet_data_str_t *tasklet_data_ptr = NULL; 00074 static mac_api_t *mac_api = NULL; 00075 00076 /* private function prototypes */ 00077 void nd_tasklet_main(arm_event_s *event); 00078 void nd_tasklet_network_state_changed(mesh_connection_status_t status); 00079 void nd_tasklet_parse_network_event(arm_event_s *event); 00080 void nd_tasklet_configure_and_connect_to_network(void); 00081 #define TRACE_ND_TASKLET 00082 #ifndef TRACE_ND_TASKLET 00083 #define nd_tasklet_trace_bootstrap_info() ((void) 0) 00084 #else 00085 void nd_tasklet_trace_bootstrap_info(void); 00086 #endif 00087 00088 static void initialize_channel_list(void) 00089 { 00090 uint32_t channel = MBED_MESH_API_6LOWPAN_ND_CHANNEL; 00091 00092 const int_fast8_t word_index = channel / 32; 00093 const int_fast8_t bit_index = channel % 32; 00094 00095 memset(&tasklet_data_ptr->channel_list, 0, sizeof(tasklet_data_ptr->channel_list)); 00096 00097 tasklet_data_ptr->channel_list.channel_page = (channel_page_e)MBED_MESH_API_6LOWPAN_ND_CHANNEL_PAGE; 00098 tasklet_data_ptr->channel_list.channel_mask[0] = MBED_MESH_API_6LOWPAN_ND_CHANNEL_MASK; 00099 00100 if (channel > 0) { 00101 memset(&tasklet_data_ptr->channel_list.channel_mask, 0, sizeof(tasklet_data_ptr->channel_list.channel_mask)); 00102 tasklet_data_ptr->channel_list.channel_mask[word_index] |= ((uint32_t) 1 << bit_index); 00103 } 00104 00105 arm_nwk_set_channel_list(tasklet_data_ptr->network_interface_id, &tasklet_data_ptr->channel_list); 00106 00107 tr_debug("Channel: %ld", channel); 00108 tr_debug("Channel page: %d", tasklet_data_ptr->channel_list.channel_page); 00109 tr_debug("Channel mask: %ld", tasklet_data_ptr->channel_list.channel_mask[word_index]); 00110 } 00111 00112 /* 00113 * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver. 00114 * @param event, describes the sender, receiver and event type. 00115 * 00116 * NOTE: Interrupts requested by HW are possible during this function! 00117 */ 00118 void nd_tasklet_main(arm_event_s *event) 00119 { 00120 arm_library_event_type_e event_type; 00121 event_type = (arm_library_event_type_e) event->event_type; 00122 00123 switch (event_type) { 00124 case ARM_LIB_NWK_INTERFACE_EVENT: 00125 /* This event is delivered every and each time when there is new 00126 * information of network connectivity. 00127 */ 00128 nd_tasklet_parse_network_event(event); 00129 break; 00130 00131 case ARM_LIB_TASKLET_INIT_EVENT: 00132 /* Event with type EV_INIT is an initializer event of NanoStack OS. 00133 * The event is delivered when the NanoStack OS is running fine. 00134 * This event should be delivered ONLY ONCE. 00135 */ 00136 tasklet_data_ptr->node_main_tasklet_id = event->receiver; 00137 mesh_system_send_connect_event(tasklet_data_ptr->tasklet); 00138 break; 00139 00140 case ARM_LIB_SYSTEM_TIMER_EVENT: 00141 eventOS_event_timer_cancel(event->event_id, 00142 tasklet_data_ptr->node_main_tasklet_id); 00143 00144 if (event->event_id == TIMER_EVENT_START_BOOTSTRAP) { 00145 tr_debug("Restart bootstrap"); 00146 nd_tasklet_configure_and_connect_to_network(); 00147 } 00148 break; 00149 00150 case APPLICATION_EVENT: 00151 if (event->event_id == APPL_EVENT_CONNECT) { 00152 nd_tasklet_configure_and_connect_to_network(); 00153 } 00154 break; 00155 00156 default: 00157 break; 00158 } // switch(event_type) 00159 } 00160 00161 /** 00162 * \brief Network state event handler. 00163 * \param event show network start response or current network state. 00164 * 00165 * - ARM_NWK_BOOTSTRAP_READY: Save NVK persistent data to NVM and Net role 00166 * - ARM_NWK_NWK_SCAN_FAIL: Link Layer Active Scan Fail, Stack is Already at Idle state 00167 * - ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: No ND Router at current Channel Stack is Already at Idle state 00168 * - ARM_NWK_NWK_CONNECTION_DOWN: Connection to Access point is lost wait for Scan Result 00169 * - ARM_NWK_NWK_PARENT_POLL_FAIL: Host should run net start without any PAN-id filter and all channels 00170 * - ARM_NWK_AUHTENTICATION_FAIL: Pana Authentication fail, Stack is Already at Idle state 00171 */ 00172 void nd_tasklet_parse_network_event(arm_event_s *event) 00173 { 00174 arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e) event->event_data; 00175 tr_debug("app_parse_network_event() %d", status); 00176 switch (status) { 00177 case ARM_NWK_BOOTSTRAP_READY: 00178 /* Network is ready and node is connected to Access Point */ 00179 if (tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) { 00180 tr_info("6LoWPAN ND bootstrap ready"); 00181 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_READY; 00182 nd_tasklet_trace_bootstrap_info(); 00183 nd_tasklet_network_state_changed(MESH_CONNECTED); 00184 } 00185 break; 00186 case ARM_NWK_NWK_SCAN_FAIL: 00187 /* Link Layer Active Scan Fail, Stack is Already at Idle state */ 00188 tr_debug("Link Layer Scan Fail: No Beacons"); 00189 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00190 break; 00191 case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: 00192 /* No ND Router at current Channel Stack is Already at Idle state */ 00193 tr_debug("ND Scan/ GP REG fail"); 00194 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00195 break; 00196 case ARM_NWK_NWK_CONNECTION_DOWN: 00197 /* Connection to Access point is lost wait for Scan Result */ 00198 tr_debug("ND/RPL scan new network"); 00199 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00200 break; 00201 case ARM_NWK_NWK_PARENT_POLL_FAIL: 00202 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00203 break; 00204 case ARM_NWK_AUHTENTICATION_FAIL: 00205 tr_debug("Network authentication fail"); 00206 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00207 break; 00208 default: 00209 tr_warn("Unknown event %d", status); 00210 break; 00211 } 00212 00213 if (tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) { 00214 // Set 5s timer for new network scan 00215 eventOS_event_timer_request(TIMER_EVENT_START_BOOTSTRAP, 00216 ARM_LIB_SYSTEM_TIMER_EVENT, 00217 tasklet_data_ptr->node_main_tasklet_id, 00218 5000); 00219 } 00220 } 00221 00222 /* 00223 * \brief Configure and establish network connection 00224 * 00225 */ 00226 void nd_tasklet_configure_and_connect_to_network(void) 00227 { 00228 int8_t status; 00229 char *sec_mode; 00230 00231 // configure bootstrap 00232 arm_nwk_interface_configure_6lowpan_bootstrap_set( 00233 tasklet_data_ptr->network_interface_id, tasklet_data_ptr->mode, 00234 NET_6LOWPAN_ND_WITH_MLE); 00235 00236 sec_mode = STR(MBED_MESH_API_6LOWPAN_ND_SECURITY_MODE); 00237 00238 if (strcmp(sec_mode, "PSK") == 0) { 00239 tr_debug("Using PSK security mode."); 00240 tasklet_data_ptr->sec_mode = NET_SEC_MODE_PSK_LINK_SECURITY; 00241 tasklet_data_ptr->psk_sec_info.key_id = MBED_MESH_API_6LOWPAN_ND_PSK_KEY_ID; 00242 memcpy(tasklet_data_ptr->psk_sec_info.security_key, (const uint8_t[16])MBED_MESH_API_6LOWPAN_ND_PSK_KEY, 16); 00243 } else { 00244 tr_debug("Link-layer security NOT enabled."); 00245 tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY; 00246 } 00247 00248 // configure link layer security 00249 arm_nwk_link_layer_security_mode( 00250 tasklet_data_ptr->network_interface_id, 00251 tasklet_data_ptr->sec_mode, 00252 MBED_MESH_API_6LOWPAN_ND_SEC_LEVEL, 00253 &tasklet_data_ptr->psk_sec_info); 00254 00255 // configure scan parameters 00256 arm_nwk_6lowpan_link_scan_parameter_set(tasklet_data_ptr->network_interface_id, 5); 00257 00258 // configure scan channels 00259 initialize_channel_list(); 00260 00261 // Configure scan options (NULL disables filter) 00262 arm_nwk_6lowpan_link_nwk_id_filter_for_nwk_scan( 00263 tasklet_data_ptr->network_interface_id, NULL); 00264 00265 arm_nwk_6lowpan_link_panid_filter_for_nwk_scan( 00266 tasklet_data_ptr->network_interface_id, 00267 MBED_MESH_API_6LOWPAN_ND_PANID_FILTER); 00268 00269 status = arm_nwk_interface_up(tasklet_data_ptr->network_interface_id); 00270 if (status >= 0) { 00271 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; 00272 tr_info("Start 6LoWPAN ND Bootstrap"); 00273 } else { 00274 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00275 tr_err("Bootstrap start failed, %d", status); 00276 nd_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); 00277 } 00278 } 00279 00280 /* 00281 * Inform application about network state change 00282 */ 00283 void nd_tasklet_network_state_changed(mesh_connection_status_t status) 00284 { 00285 if (tasklet_data_ptr->mesh_api_cb) { 00286 (tasklet_data_ptr->mesh_api_cb)(status); 00287 } 00288 } 00289 00290 /* 00291 * Trace bootstrap information. 00292 */ 00293 #ifdef TRACE_ND_TASKLET 00294 void nd_tasklet_trace_bootstrap_info() 00295 { 00296 network_layer_address_s app_nd_address_info; 00297 link_layer_address_s app_link_address_info; 00298 uint8_t temp_ipv6[16]; 00299 if (arm_nwk_nd_address_read(tasklet_data_ptr->network_interface_id, 00300 &app_nd_address_info) != 0) { 00301 tr_error("ND Address read fail"); 00302 } else { 00303 tr_debug("ND Access Point: %s", trace_ipv6(app_nd_address_info.border_router)); 00304 tr_debug("ND Prefix 64: %s", trace_array(app_nd_address_info.prefix, 8)); 00305 00306 if (arm_net_address_get(tasklet_data_ptr->network_interface_id, 00307 ADDR_IPV6_GP, temp_ipv6) == 0) { 00308 tr_debug("GP IPv6: %s", trace_ipv6(temp_ipv6)); 00309 } 00310 } 00311 00312 if (arm_nwk_mac_address_read(tasklet_data_ptr->network_interface_id, 00313 &app_link_address_info) != 0) { 00314 tr_error("MAC Address read fail\n"); 00315 } else { 00316 uint8_t temp[2]; 00317 common_write_16_bit(app_link_address_info.mac_short,temp); 00318 tr_debug("MAC 16-bit: %s", trace_array(temp, 2)); 00319 common_write_16_bit(app_link_address_info.PANId, temp); 00320 tr_debug("PAN ID: %s", trace_array(temp, 2)); 00321 tr_debug("MAC 64-bit: %s", trace_array(app_link_address_info.mac_long, 8)); 00322 tr_debug("IID (Based on MAC 64-bit address): %s", trace_array(app_link_address_info.iid_eui64, 8)); 00323 } 00324 00325 tr_debug("Channel: %d", arm_net_get_current_channel(tasklet_data_ptr->network_interface_id)); 00326 } 00327 #endif /* #define TRACE_ND_TASKLET */ 00328 00329 /* Public functions */ 00330 int8_t nd_tasklet_get_ip_address(char *address, int8_t len) 00331 { 00332 uint8_t binary_ipv6[16]; 00333 00334 if ((len >= 40) && (0 == arm_net_address_get( 00335 tasklet_data_ptr->network_interface_id, ADDR_IPV6_GP, binary_ipv6))) { 00336 ip6tos(binary_ipv6, address); 00337 //tr_debug("IP address: %s", address); 00338 return 0; 00339 } else { 00340 return -1; 00341 } 00342 } 00343 00344 int8_t nd_tasklet_get_router_ip_address(char *address, int8_t len) 00345 { 00346 network_layer_address_s nd_address; 00347 00348 if ((len >= 40) && (0 == arm_nwk_nd_address_read( 00349 tasklet_data_ptr->network_interface_id, &nd_address))) { 00350 ip6tos(nd_address.border_router, address); 00351 //tr_debug("Router IP address: %s", address); 00352 return 0; 00353 } else { 00354 return -1; 00355 } 00356 } 00357 00358 int8_t nd_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id) 00359 { 00360 int8_t re_connecting = true; 00361 int8_t tasklet_id = tasklet_data_ptr->tasklet; 00362 00363 if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) { 00364 return -3; // already connected to network 00365 } 00366 00367 if (tasklet_data_ptr->tasklet_state == TASKLET_STATE_CREATED) { 00368 re_connecting = false; 00369 } 00370 00371 memset(tasklet_data_ptr, 0, sizeof(tasklet_data_str_t)); 00372 tasklet_data_ptr->mesh_api_cb = callback; 00373 tasklet_data_ptr->network_interface_id = nwk_interface_id; 00374 tasklet_data_ptr->tasklet_state = TASKLET_STATE_INITIALIZED; 00375 00376 tasklet_data_ptr->mode = MBED_CONF_MBED_MESH_API_6LOWPAN_ND_DEVICE_TYPE; 00377 tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY; 00378 //tasklet_data_ptr->psk_sec_info.key_id = 0; 00379 00380 if (re_connecting == false) { 00381 tasklet_data_ptr->tasklet = eventOS_event_handler_create(&nd_tasklet_main, 00382 ARM_LIB_TASKLET_INIT_EVENT); 00383 if (tasklet_data_ptr->tasklet < 0) { 00384 // -1 handler already used by other tasklet 00385 // -2 memory allocation failure 00386 return tasklet_data_ptr->tasklet; 00387 } 00388 ns_event_loop_thread_start(); 00389 } else { 00390 tasklet_data_ptr->tasklet = tasklet_id; 00391 mesh_system_send_connect_event(tasklet_data_ptr->tasklet); 00392 } 00393 00394 return tasklet_data_ptr->tasklet; 00395 } 00396 00397 int8_t nd_tasklet_disconnect(bool send_cb) 00398 { 00399 int8_t status = -1; 00400 if (tasklet_data_ptr != NULL) { 00401 if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) { 00402 status = arm_nwk_interface_down(tasklet_data_ptr->network_interface_id); 00403 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00404 if (send_cb == true) { 00405 nd_tasklet_network_state_changed(MESH_DISCONNECTED); 00406 } 00407 } 00408 tasklet_data_ptr->mesh_api_cb = NULL; 00409 } 00410 return status; 00411 } 00412 00413 void nd_tasklet_init(void) 00414 { 00415 if (tasklet_data_ptr == NULL) { 00416 // memory allocation will not fail as memory was just initialized 00417 tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(tasklet_data_str_t)); 00418 tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED; 00419 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00420 } 00421 } 00422 00423 int8_t nd_tasklet_network_init(int8_t device_id) 00424 { 00425 // TODO, read interface name from configuration 00426 mac_description_storage_size_t storage_sizes; 00427 storage_sizes.device_decription_table_size = 32; 00428 storage_sizes.key_description_table_size = 3; 00429 storage_sizes.key_lookup_size = 1; 00430 storage_sizes.key_usage_size = 3; 00431 if (!mac_api) { 00432 mac_api = ns_sw_mac_create(device_id, &storage_sizes); 00433 } 00434 return arm_nwk_interface_lowpan_init(mac_api, INTERFACE_NAME); 00435 } 00436
Generated on Tue Jul 12 2022 11:02:47 by
