ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thread_tasklet.c Source File

thread_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 "thread_management_if.h"
00024 #include "net_polling_api.h"
00025 #include "include/thread_tasklet.h"
00026 #include "include/static_config.h"
00027 #include "include/mesh_system.h"
00028 #include "ns_event_loop.h"
00029 
00030 // For tracing we need to define flag, have include and define group
00031 #define HAVE_DEBUG 1
00032 #include "ns_trace.h"
00033 #define TRACE_GROUP  "m6Thread"
00034 
00035 #include "mac_api.h"
00036 #include "sw_mac.h"
00037 
00038 #define DETAILED_TRACES
00039 #ifdef DETAILED_TRACES
00040 #define TRACE_DETAIL    tr_debug
00041 #else
00042 #define TRACE_DETAIL(...)
00043 #endif
00044 
00045 #define INTERFACE_NAME   "6L-THREAD"
00046 
00047 // Tasklet timer events
00048 #define TIMER_EVENT_START_BOOTSTRAP   1
00049 
00050 #define INVALID_INTERFACE_ID        (-1)
00051 
00052 /*
00053  * Thread tasklet states.
00054  */
00055 typedef enum {
00056     TASKLET_STATE_CREATED = 0,
00057     TASKLET_STATE_INITIALIZED,
00058     TASKLET_STATE_BOOTSTRAP_STARTED,
00059     TASKLET_STATE_BOOTSTRAP_FAILED,
00060     TASKLET_STATE_BOOTSTRAP_READY
00061 } tasklet_state_t;
00062 
00063 /*
00064  * Mesh tasklet data structure.
00065  */
00066 typedef struct {
00067     void (*mesh_api_cb)(mesh_connection_status_t nwk_status);
00068     channel_list_s channel_list;
00069     tasklet_state_t tasklet_state;
00070     int8_t tasklet;
00071 
00072     net_6lowpan_mode_e operating_mode;
00073     int8_t nwk_if_id;
00074     link_configuration_s link_config;
00075 
00076     /** Default network ID*/
00077     uint8_t networkid[16];
00078     uint8_t extented_panid[8];
00079     uint32_t pan_id;
00080     uint32_t rfChannel;
00081     uint8_t scan_time;
00082     net_6lowpan_gp_address_mode_e address_mode;
00083 } thread_tasklet_data_str_t;
00084 
00085 
00086 /* Tasklet data */
00087 static thread_tasklet_data_str_t *thread_tasklet_data_ptr = NULL;
00088 static device_configuration_s device_configuration;
00089 
00090 /* private function prototypes */
00091 void thread_tasklet_main(arm_event_s *event);
00092 void thread_tasklet_network_state_changed(mesh_connection_status_t status);
00093 void thread_tasklet_parse_network_event(arm_event_s *event);
00094 void thread_tasklet_configure_and_connect_to_network(void);
00095 #define TRACE_THREAD_TASKLET
00096 #ifndef TRACE_THREAD_TASKLET
00097 #define thread_tasklet_trace_bootstrap_info() ((void) 0)
00098 #else
00099 void thread_tasklet_trace_bootstrap_info(void);
00100 #endif
00101 
00102 /*
00103  * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver.
00104  * @param event, describes the sender, receiver and event type.
00105  *
00106  * NOTE: Interrupts requested by HW are possible during this function!
00107  */
00108 void thread_tasklet_main(arm_event_s *event)
00109 {
00110     arm_library_event_type_e event_type;
00111     event_type = (arm_library_event_type_e) event->event_type;
00112 
00113     switch (event_type) {
00114         case ARM_LIB_NWK_INTERFACE_EVENT:
00115             /* This event is delivered every and each time when there is new
00116              * information of network connectivity.
00117              */
00118             thread_tasklet_parse_network_event(event);
00119             break;
00120 
00121         case ARM_LIB_TASKLET_INIT_EVENT:
00122             /* Event with type EV_INIT is an initializer event of NanoStack OS.
00123              * The event is delivered when the NanoStack OS is running fine.
00124              * This event should be delivered ONLY ONCE.
00125              */
00126             mesh_system_send_connect_event(thread_tasklet_data_ptr->tasklet);
00127             break;
00128 
00129         case ARM_LIB_SYSTEM_TIMER_EVENT:
00130             eventOS_event_timer_cancel(event->event_id,
00131                                        thread_tasklet_data_ptr->tasklet);
00132 
00133             if (event->event_id == TIMER_EVENT_START_BOOTSTRAP) {
00134                 tr_debug("Restart bootstrap");
00135                 arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id);
00136             }
00137             break;
00138 
00139         case APPLICATION_EVENT:
00140             if (event->event_id == APPL_EVENT_CONNECT) {
00141                 thread_tasklet_configure_and_connect_to_network();
00142             }
00143             break;
00144 
00145         default:
00146             break;
00147     } // switch(event_type)
00148 }
00149 
00150 /**
00151  * \brief Network state event handler.
00152  * \param event show network start response or current network state.
00153  *
00154  * - ARM_NWK_BOOTSTRAP_READY: Save NVK persistent data to NVM and Net role
00155  * - ARM_NWK_NWK_SCAN_FAIL: Link Layer Active Scan Fail, Stack is Already at Idle state
00156  * - ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: No ND Router at current Channel Stack is Already at Idle state
00157  * - ARM_NWK_NWK_CONNECTION_DOWN: Connection to Access point is lost wait for Scan Result
00158  * - ARM_NWK_NWK_PARENT_POLL_FAIL: Host should run net start without any PAN-id filter and all channels
00159  * - ARM_NWK_AUHTENTICATION_FAIL: Pana Authentication fail, Stack is Already at Idle state
00160  */
00161 void thread_tasklet_parse_network_event(arm_event_s *event)
00162 {
00163     arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e) event->event_data;
00164     tr_debug("app_parse_network_event() %d", status);
00165     switch (status) {
00166         case ARM_NWK_BOOTSTRAP_READY:
00167             /* Network is ready and node is connected to Access Point */
00168             if (thread_tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) {
00169                 tr_info("Thread bootstrap ready");
00170                 thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_READY;
00171                 thread_tasklet_trace_bootstrap_info();
00172                 thread_tasklet_network_state_changed(MESH_CONNECTED);
00173             }
00174             break;
00175         case ARM_NWK_NWK_SCAN_FAIL:
00176             /* Link Layer Active Scan Fail, Stack is Already at Idle state */
00177             tr_debug("Link Layer Scan Fail: No Beacons");
00178             thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00179             break;
00180         case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL:
00181             /* No ND Router at current Channel Stack is Already at Idle state */
00182             tr_debug("ND Scan/ GP REG fail");
00183             thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00184             break;
00185         case ARM_NWK_NWK_CONNECTION_DOWN:
00186             /* Connection to Access point is lost wait for Scan Result */
00187             tr_debug("ND/RPL scan new network");
00188             thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00189             break;
00190         case ARM_NWK_NWK_PARENT_POLL_FAIL:
00191             thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00192             break;
00193         case ARM_NWK_AUHTENTICATION_FAIL:
00194             tr_debug("Network authentication fail");
00195             thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00196             break;
00197         default:
00198             tr_warn("Unknown event %d", status);
00199             break;
00200     }
00201 
00202     if (thread_tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) {
00203         // Set 5s timer for a new network scan
00204         eventOS_event_timer_request(TIMER_EVENT_START_BOOTSTRAP,
00205                                     ARM_LIB_SYSTEM_TIMER_EVENT,
00206                                     thread_tasklet_data_ptr->tasklet,
00207                                     5000);
00208     }
00209 }
00210 
00211 /*
00212  * \brief Configure mesh network
00213  *
00214  */
00215 void thread_tasklet_configure_and_connect_to_network(void)
00216 {
00217     int8_t status;
00218 
00219     if (MBED_MESH_API_THREAD_DEVICE_TYPE == MESH_DEVICE_TYPE_THREAD_SLEEPY_END_DEVICE) {
00220         thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_SLEEPY_HOST;
00221     } else {
00222         thread_tasklet_data_ptr->operating_mode = NET_6LOWPAN_ROUTER;
00223     }
00224 
00225     arm_nwk_interface_configure_6lowpan_bootstrap_set(
00226         thread_tasklet_data_ptr->nwk_if_id,
00227         thread_tasklet_data_ptr->operating_mode,
00228         NET_6LOWPAN_THREAD);
00229 
00230     // Link configuration
00231     memcpy(thread_tasklet_data_ptr->link_config.name, "Arm Powered Core", 16);
00232 
00233     thread_tasklet_data_ptr->link_config.panId = MBED_MESH_API_THREAD_CONFIG_PANID;
00234     TRACE_DETAIL("PANID %x", thread_tasklet_data_ptr->link_config.panId);
00235 
00236     // channel
00237     if (MBED_MESH_API_THREAD_CONFIG_CHANNEL > 27) {
00238         tr_error("Bad channel %d", MBED_MESH_API_THREAD_CONFIG_CHANNEL);
00239         return;
00240     }
00241 
00242     thread_tasklet_data_ptr->link_config.rfChannel = MBED_MESH_API_THREAD_CONFIG_CHANNEL;
00243     thread_tasklet_data_ptr->channel_list.channel_page = (channel_page_e)MBED_MESH_API_THREAD_CONFIG_CHANNEL_PAGE;
00244     thread_tasklet_data_ptr->channel_list.channel_mask[0] = MBED_MESH_API_THREAD_CONFIG_CHANNEL_MASK;
00245     TRACE_DETAIL("channel: %d", thread_tasklet_data_ptr->link_config.rfChannel);
00246     TRACE_DETAIL("channel page: %d", thread_tasklet_data_ptr->channel_list.channel_page);
00247     TRACE_DETAIL("channel mask: %d", (int)thread_tasklet_data_ptr->channel_list.channel_mask[0]);
00248 
00249 
00250     // Beacon data setting
00251     thread_tasklet_data_ptr->link_config.Protocol_id = 0x03;
00252     thread_tasklet_data_ptr->link_config.version = 1;
00253     memcpy(thread_tasklet_data_ptr->link_config.extended_random_mac, device_configuration.eui64, 8);
00254     thread_tasklet_data_ptr->link_config.extended_random_mac[0] |= 0x02;
00255 
00256     // Mesh prefix
00257     const uint8_t mesh_local_prefix[] = MBED_MESH_API_THREAD_CONFIG_ML_PREFIX;
00258     if (sizeof(mesh_local_prefix) == 8) {
00259         memcpy(thread_tasklet_data_ptr->link_config.mesh_local_ula_prefix, mesh_local_prefix, 8);
00260         TRACE_DETAIL("Mesh prefix: %s", trace_array(mesh_local_prefix, 8));
00261     } else {
00262         tr_error("Mesh prefix, must be 8 hex chars: %s", mesh_local_prefix);
00263         return;
00264     }
00265 
00266     // Master Key
00267     const uint8_t master_key[] = MBED_MESH_API_THREAD_MASTER_KEY;
00268     if (sizeof(master_key) == 16) {
00269         memcpy(thread_tasklet_data_ptr->link_config.master_key, master_key, 16);
00270         TRACE_DETAIL("Master key: %s", trace_array(master_key, 16));
00271     } else {
00272         tr_error("Master key must be 16 hex chars: %s", master_key);
00273         return;
00274     }
00275 
00276     // PSKc
00277     const uint8_t PSKc[] = MBED_MESH_API_THREAD_CONFIG_PSKC;
00278     if (sizeof(PSKc) == 16) {
00279         memcpy(thread_tasklet_data_ptr->link_config.PSKc, PSKc, 16);
00280         TRACE_DETAIL("PSKc: %s", trace_array(PSKc, 16));
00281     } else {
00282         tr_error("PSKc must be 16 hex chars: %s", PSKc);
00283         return;
00284     }
00285 
00286     // PSKd
00287     const char PSKd[] = MBED_MESH_API_THREAD_PSKD;
00288     if (sizeof(PSKd) < 7) {
00289         tr_error("PSKd length must be > 6: %s", PSKd);
00290         return;
00291     }
00292 
00293     char *dyn_buf = ns_dyn_mem_alloc(sizeof(PSKd));
00294     strcpy(dyn_buf, PSKd);
00295     ns_dyn_mem_free(device_configuration.PSKd_ptr);
00296     device_configuration.PSKd_ptr = (uint8_t*)dyn_buf;
00297     device_configuration.PSKd_len = sizeof(PSKd) - 1;
00298     TRACE_DETAIL("PSKd: %s", device_configuration.PSKd_ptr);
00299 
00300     thread_tasklet_data_ptr->link_config.key_rotation = 3600;
00301     thread_tasklet_data_ptr->link_config.key_sequence = 0;
00302 
00303     thread_management_node_init(thread_tasklet_data_ptr->nwk_if_id,
00304                                &thread_tasklet_data_ptr->channel_list,
00305                                &device_configuration,
00306                                &thread_tasklet_data_ptr->link_config);
00307 
00308     status = arm_nwk_interface_up(thread_tasklet_data_ptr->nwk_if_id);
00309 
00310     if (status >= 0) {
00311         thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
00312         tr_info("Start Thread bootstrap (%s mode)", thread_tasklet_data_ptr->operating_mode == NET_6LOWPAN_SLEEPY_HOST ? "SED" : "Router");
00313     } else {
00314         thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00315         tr_err("Bootstrap start failed, %d", status);
00316         thread_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED);
00317     }
00318 }
00319 
00320 /*
00321  * Inform application about network state change
00322  */
00323 void thread_tasklet_network_state_changed(mesh_connection_status_t status)
00324 {
00325     if (thread_tasklet_data_ptr->mesh_api_cb) {
00326         (thread_tasklet_data_ptr->mesh_api_cb)(status);
00327     }
00328 }
00329 
00330 /*
00331  * Trace bootstrap information.
00332  */
00333 #ifdef TRACE_THREAD_TASKLET
00334 void thread_tasklet_trace_bootstrap_info()
00335 {
00336     link_layer_address_s app_link_address_info;
00337     uint8_t temp_ipv6[16];
00338     if (arm_net_address_get(thread_tasklet_data_ptr->nwk_if_id,
00339                             ADDR_IPV6_GP, temp_ipv6) == 0) {
00340         tr_debug("GP IPv6: %s", trace_ipv6(temp_ipv6));
00341     }
00342 
00343     if (arm_nwk_mac_address_read(thread_tasklet_data_ptr->nwk_if_id,
00344                                  &app_link_address_info) != 0) {
00345         tr_error("MAC Address read fail\n");
00346     } else {
00347         uint8_t temp[2];
00348         common_write_16_bit(app_link_address_info.mac_short,temp);
00349         tr_debug("MAC 16-bit: %s", trace_array(temp, 2));
00350         common_write_16_bit(app_link_address_info.PANId, temp);
00351         tr_debug("PAN ID: %s", trace_array(temp, 2));
00352         tr_debug("MAC 64-bit: %s", trace_array(app_link_address_info.mac_long, 8));
00353         tr_debug("IID (Based on MAC 64-bit address): %s", trace_array(app_link_address_info.iid_eui64, 8));
00354     }
00355 }
00356 #endif /* #define TRACE_THREAD_TASKLET */
00357 
00358 int8_t thread_tasklet_get_ip_address(char *address, int8_t len)
00359 {
00360     uint8_t binary_ipv6[16];
00361 
00362     if ((len >= 40) && (0 == arm_net_address_get(
00363                             thread_tasklet_data_ptr->nwk_if_id, ADDR_IPV6_GP, binary_ipv6))) {
00364         ip6tos(binary_ipv6, address);
00365         //tr_debug("IP address: %s", address);
00366         return 0;
00367     } else {
00368         return -1;
00369     }
00370 }
00371 
00372 int8_t thread_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id)
00373 {
00374     int8_t re_connecting = true;
00375     int8_t tasklet = thread_tasklet_data_ptr->tasklet;
00376 
00377     if (thread_tasklet_data_ptr->nwk_if_id != INVALID_INTERFACE_ID) {
00378         return -3;  // already connected to network
00379     }
00380 
00381     if (thread_tasklet_data_ptr->tasklet_state == TASKLET_STATE_CREATED) {
00382         re_connecting = false;
00383     }
00384 
00385     memset(thread_tasklet_data_ptr, 0, sizeof(thread_tasklet_data_str_t));
00386     thread_tasklet_data_ptr->mesh_api_cb = callback;
00387     thread_tasklet_data_ptr->nwk_if_id = nwk_interface_id;
00388     thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_INITIALIZED;
00389 
00390     if (re_connecting == false) {
00391         thread_tasklet_data_ptr->tasklet = eventOS_event_handler_create(&thread_tasklet_main,
00392                         ARM_LIB_TASKLET_INIT_EVENT);
00393         if (thread_tasklet_data_ptr->tasklet < 0) {
00394             // -1 handler already used by other tasklet
00395             // -2 memory allocation failure
00396             return thread_tasklet_data_ptr->tasklet;
00397         }
00398         ns_event_loop_thread_start();
00399     } else {
00400         thread_tasklet_data_ptr->tasklet = tasklet;
00401         mesh_system_send_connect_event(thread_tasklet_data_ptr->tasklet);
00402     }
00403 
00404     return thread_tasklet_data_ptr->tasklet;
00405 }
00406 
00407 int8_t thread_tasklet_disconnect(bool send_cb)
00408 {
00409     int8_t status = -1;
00410     // check that module is initialized
00411     if (thread_tasklet_data_ptr != NULL) {
00412         if (thread_tasklet_data_ptr->nwk_if_id != INVALID_INTERFACE_ID) {
00413             status = arm_nwk_interface_down(thread_tasklet_data_ptr->nwk_if_id);
00414             thread_tasklet_data_ptr->nwk_if_id = INVALID_INTERFACE_ID;
00415             if (send_cb == true) {
00416                 thread_tasklet_network_state_changed(MESH_DISCONNECTED);
00417             }
00418         }
00419 
00420         // Clear callback, it will be set again in next connect
00421         thread_tasklet_data_ptr->mesh_api_cb = NULL;
00422     }
00423     return status;
00424 }
00425 
00426 void thread_tasklet_init(void)
00427 {
00428     if (thread_tasklet_data_ptr == NULL) {
00429         thread_tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(thread_tasklet_data_str_t));
00430         thread_tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED;
00431         thread_tasklet_data_ptr->nwk_if_id = INVALID_INTERFACE_ID;
00432     }
00433 }
00434 
00435 int8_t thread_tasklet_network_init(int8_t device_id)
00436 {
00437     // TODO, read interface name from configuration
00438     mac_description_storage_size_t storage_sizes;
00439     storage_sizes.device_decription_table_size = 32;
00440     storage_sizes.key_description_table_size = 6;
00441     storage_sizes.key_lookup_size = 1;
00442     storage_sizes.key_usage_size = 3;
00443     mac_api_t *api = ns_sw_mac_create(device_id, &storage_sizes);
00444     return arm_nwk_interface_lowpan_init(api, INTERFACE_NAME);
00445 }
00446 
00447 void thread_tasklet_device_config_set(uint8_t *eui64, char *pskd)
00448 {
00449     (void) pskd; // this parameter is delivered via mbed configuration
00450     memcpy(device_configuration.eui64, eui64, 8);
00451 }
00452 
00453 int8_t thread_tasklet_data_poll_rate_set(uint32_t timeout)
00454 {
00455     int8_t status = -1;
00456     if (thread_tasklet_data_ptr) {
00457         if (timeout != 0) {
00458             status = arm_nwk_host_mode_set(thread_tasklet_data_ptr->nwk_if_id, NET_HOST_SLOW_POLL_MODE, timeout);
00459         } else {
00460             status = arm_nwk_host_mode_set(thread_tasklet_data_ptr->nwk_if_id, NET_HOST_RX_ON_IDLE, timeout);
00461         }
00462     }
00463 
00464     return status;
00465 }
00466