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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
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 #ifndef YOTTA_CFG 00027 #include "ns_event_loop.h" 00028 #endif 00029 // For tracing we need to define flag, have include and define group 00030 #define HAVE_DEBUG 1 00031 #include "ns_trace.h" 00032 #define TRACE_GROUP "m6LND" 00033 00034 #include "mac_api.h" 00035 #include "sw_mac.h" 00036 00037 #define INTERFACE_NAME "6L-ND" 00038 00039 // Tasklet timer events 00040 #define TIMER_EVENT_START_BOOTSTRAP 1 00041 00042 #define INVALID_INTERFACE_ID (-1) 00043 00044 #define STR_HELPER(x) #x 00045 #define STR(x) STR_HELPER(x) 00046 00047 /* 00048 * Mesh tasklet states. 00049 */ 00050 typedef enum { 00051 TASKLET_STATE_CREATED = 0, 00052 TASKLET_STATE_INITIALIZED, 00053 TASKLET_STATE_BOOTSTRAP_STARTED, 00054 TASKLET_STATE_BOOTSTRAP_FAILED, 00055 TASKLET_STATE_BOOTSTRAP_READY 00056 } tasklet_state_t; 00057 00058 /* 00059 * Mesh tasklet data structure. 00060 */ 00061 typedef struct { 00062 void (*mesh_api_cb)(mesh_connection_status_t nwk_status); 00063 channel_list_s channel_list; 00064 tasklet_state_t tasklet_state; 00065 net_6lowpan_mode_e mode; 00066 net_6lowpan_link_layer_sec_mode_e sec_mode; 00067 net_link_layer_psk_security_info_s psk_sec_info; 00068 int8_t node_main_tasklet_id; 00069 int8_t network_interface_id; 00070 int8_t tasklet; 00071 } tasklet_data_str_t; 00072 00073 /* Tasklet data */ 00074 static tasklet_data_str_t *tasklet_data_ptr = 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 status = arm_nwk_interface_up(tasklet_data_ptr->network_interface_id); 00266 if (status >= 0) { 00267 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED; 00268 tr_info("Start 6LoWPAN ND Bootstrap"); 00269 } else { 00270 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00271 tr_err("Bootstrap start failed, %d", status); 00272 nd_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED); 00273 } 00274 } 00275 00276 /* 00277 * Inform application about network state change 00278 */ 00279 void nd_tasklet_network_state_changed(mesh_connection_status_t status) 00280 { 00281 if (tasklet_data_ptr->mesh_api_cb) { 00282 (tasklet_data_ptr->mesh_api_cb)(status); 00283 } 00284 } 00285 00286 /* 00287 * Trace bootstrap information. 00288 */ 00289 #ifdef TRACE_ND_TASKLET 00290 void nd_tasklet_trace_bootstrap_info() 00291 { 00292 network_layer_address_s app_nd_address_info; 00293 link_layer_address_s app_link_address_info; 00294 uint8_t temp_ipv6[16]; 00295 if (arm_nwk_nd_address_read(tasklet_data_ptr->network_interface_id, 00296 &app_nd_address_info) != 0) { 00297 tr_error("ND Address read fail"); 00298 } else { 00299 tr_debug("ND Access Point: %s", trace_ipv6(app_nd_address_info.border_router)); 00300 tr_debug("ND Prefix 64: %s", trace_array(app_nd_address_info.prefix, 8)); 00301 00302 if (arm_net_address_get(tasklet_data_ptr->network_interface_id, 00303 ADDR_IPV6_GP, temp_ipv6) == 0) { 00304 tr_debug("GP IPv6: %s", trace_ipv6(temp_ipv6)); 00305 } 00306 } 00307 00308 if (arm_nwk_mac_address_read(tasklet_data_ptr->network_interface_id, 00309 &app_link_address_info) != 0) { 00310 tr_error("MAC Address read fail\n"); 00311 } else { 00312 uint8_t temp[2]; 00313 common_write_16_bit(app_link_address_info.mac_short,temp); 00314 tr_debug("MAC 16-bit: %s", trace_array(temp, 2)); 00315 common_write_16_bit(app_link_address_info.PANId, temp); 00316 tr_debug("PAN ID: %s", trace_array(temp, 2)); 00317 tr_debug("MAC 64-bit: %s", trace_array(app_link_address_info.mac_long, 8)); 00318 tr_debug("IID (Based on MAC 64-bit address): %s", trace_array(app_link_address_info.iid_eui64, 8)); 00319 } 00320 00321 tr_debug("Channel: %d", arm_net_get_current_channel(tasklet_data_ptr->network_interface_id)); 00322 } 00323 #endif /* #define TRACE_ND_TASKLET */ 00324 00325 /* Public functions */ 00326 int8_t nd_tasklet_get_ip_address(char *address, int8_t len) 00327 { 00328 uint8_t binary_ipv6[16]; 00329 00330 if ((len >= 40) && (0 == arm_net_address_get( 00331 tasklet_data_ptr->network_interface_id, ADDR_IPV6_GP, binary_ipv6))) { 00332 ip6tos(binary_ipv6, address); 00333 //tr_debug("IP address: %s", address); 00334 return 0; 00335 } else { 00336 return -1; 00337 } 00338 } 00339 00340 int8_t nd_tasklet_get_router_ip_address(char *address, int8_t len) 00341 { 00342 network_layer_address_s nd_address; 00343 00344 if ((len >= 40) && (0 == arm_nwk_nd_address_read( 00345 tasklet_data_ptr->network_interface_id, &nd_address))) { 00346 ip6tos(nd_address.border_router, address); 00347 //tr_debug("Router IP address: %s", address); 00348 return 0; 00349 } else { 00350 return -1; 00351 } 00352 } 00353 00354 int8_t nd_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id) 00355 { 00356 int8_t re_connecting = true; 00357 int8_t tasklet_id = tasklet_data_ptr->tasklet; 00358 00359 if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) { 00360 return -3; // already connected to network 00361 } 00362 00363 if (tasklet_data_ptr->tasklet_state == TASKLET_STATE_CREATED) { 00364 re_connecting = false; 00365 } 00366 00367 memset(tasklet_data_ptr, 0, sizeof(tasklet_data_str_t)); 00368 tasklet_data_ptr->mesh_api_cb = callback; 00369 tasklet_data_ptr->network_interface_id = nwk_interface_id; 00370 tasklet_data_ptr->tasklet_state = TASKLET_STATE_INITIALIZED; 00371 00372 //TODO: Fetch these values from device config api 00373 tasklet_data_ptr->mode = NET_6LOWPAN_ROUTER; 00374 tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY; 00375 //tasklet_data_ptr->psk_sec_info.key_id = 0; 00376 00377 if (re_connecting == false) { 00378 tasklet_data_ptr->tasklet = eventOS_event_handler_create(&nd_tasklet_main, 00379 ARM_LIB_TASKLET_INIT_EVENT); 00380 if (tasklet_data_ptr->tasklet < 0) { 00381 // -1 handler already used by other tasklet 00382 // -2 memory allocation failure 00383 return tasklet_data_ptr->tasklet; 00384 } 00385 #ifndef YOTTA_CFG 00386 ns_event_loop_thread_start(); 00387 #endif 00388 } else { 00389 tasklet_data_ptr->tasklet = tasklet_id; 00390 mesh_system_send_connect_event(tasklet_data_ptr->tasklet); 00391 } 00392 00393 return tasklet_data_ptr->tasklet; 00394 } 00395 00396 int8_t nd_tasklet_disconnect(bool send_cb) 00397 { 00398 int8_t status = -1; 00399 if (tasklet_data_ptr != NULL) { 00400 if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) { 00401 status = arm_nwk_interface_down(tasklet_data_ptr->network_interface_id); 00402 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00403 if (send_cb == true) { 00404 nd_tasklet_network_state_changed(MESH_DISCONNECTED); 00405 } 00406 } 00407 tasklet_data_ptr->mesh_api_cb = NULL; 00408 } 00409 return status; 00410 } 00411 00412 void nd_tasklet_init(void) 00413 { 00414 if (tasklet_data_ptr == NULL) { 00415 // memory allocation will not fail as memory was just initialized 00416 tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(tasklet_data_str_t)); 00417 tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED; 00418 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00419 } 00420 } 00421 00422 int8_t nd_tasklet_network_init(int8_t device_id) 00423 { 00424 // TODO, read interface name from configuration 00425 mac_description_storage_size_t storage_sizes; 00426 storage_sizes.device_decription_table_size = 32; 00427 storage_sizes.key_description_table_size = 3; 00428 storage_sizes.key_lookup_size = 1; 00429 storage_sizes.key_usage_size = 3; 00430 mac_api_t *api = ns_sw_mac_create(device_id, &storage_sizes); 00431 return arm_nwk_interface_lowpan_init(api, INTERFACE_NAME); 00432 } 00433
Generated on Tue Jul 12 2022 12:28:44 by
