Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nd_tasklet.c Source File

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/mesh_system.h"
00025 #include "ns_event_loop.h"
00026 #include "multicast_api.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_CONF_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_CONF_MBED_MESH_API_6LOWPAN_ND_CHANNEL_PAGE;
00098     tasklet_data_ptr->channel_list.channel_mask[0] = MBED_CONF_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: 0x%.8lx", 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             nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00191             break;
00192         case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL:
00193             /* No ND Router at current Channel Stack is Already at Idle state */
00194             tr_debug("ND Scan/ GP REG fail");
00195             tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00196             nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00197             break;
00198         case ARM_NWK_NWK_CONNECTION_DOWN:
00199             /* Connection to Access point is lost wait for Scan Result */
00200             tr_debug("ND/RPL scan new network");
00201             tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00202             nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00203             break;
00204         case ARM_NWK_NWK_PARENT_POLL_FAIL:
00205             tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00206             nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00207             break;
00208         case ARM_NWK_AUHTENTICATION_FAIL:
00209             tr_debug("Network authentication fail");
00210             tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00211             nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00212             break;
00213         default:
00214             tr_warn("Unknown event %d", status);
00215             break;
00216     }
00217 
00218     if (tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) {
00219         // Set 5s timer for new network scan
00220         eventOS_event_timer_request(TIMER_EVENT_START_BOOTSTRAP,
00221                                     ARM_LIB_SYSTEM_TIMER_EVENT,
00222                                     tasklet_data_ptr->node_main_tasklet_id,
00223                                     5000);
00224     }
00225 }
00226 
00227 /*
00228  * \brief Configure and establish network connection
00229  *
00230  */
00231 void nd_tasklet_configure_and_connect_to_network(void)
00232 {
00233     int8_t status;
00234     char *sec_mode;
00235 
00236     // configure bootstrap
00237     arm_nwk_interface_configure_6lowpan_bootstrap_set(
00238         tasklet_data_ptr->network_interface_id, tasklet_data_ptr->mode,
00239         NET_6LOWPAN_ND_WITH_MLE);
00240 
00241     sec_mode = STR(MBED_CONF_MBED_MESH_API_6LOWPAN_ND_SECURITY_MODE);
00242 
00243     if (strcmp(sec_mode, "PSK") == 0) {
00244         tr_debug("Using PSK security mode.");
00245         tasklet_data_ptr->sec_mode = NET_SEC_MODE_PSK_LINK_SECURITY;
00246         tasklet_data_ptr->psk_sec_info.key_id = MBED_CONF_MBED_MESH_API_6LOWPAN_ND_PSK_KEY_ID;
00247         memcpy(tasklet_data_ptr->psk_sec_info.security_key, (const uint8_t[16])MBED_CONF_MBED_MESH_API_6LOWPAN_ND_PSK_KEY, 16);
00248     } else {
00249         tr_debug("Link-layer security NOT enabled.");
00250         tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY;
00251     }
00252 
00253     // configure link layer security
00254     arm_nwk_link_layer_security_mode(
00255         tasklet_data_ptr->network_interface_id,
00256         tasklet_data_ptr->sec_mode,
00257         MBED_CONF_MBED_MESH_API_6LOWPAN_ND_SEC_LEVEL,
00258         &tasklet_data_ptr->psk_sec_info);
00259 
00260     // configure scan parameters
00261     arm_nwk_6lowpan_link_scan_parameter_set(tasklet_data_ptr->network_interface_id, 5);
00262 
00263     // configure scan channels
00264     initialize_channel_list();
00265 
00266     // Configure scan options (NULL disables filter)
00267     arm_nwk_6lowpan_link_nwk_id_filter_for_nwk_scan(
00268         tasklet_data_ptr->network_interface_id, NULL);
00269 
00270     arm_nwk_6lowpan_link_panid_filter_for_nwk_scan(
00271          tasklet_data_ptr->network_interface_id,
00272          MBED_CONF_MBED_MESH_API_6LOWPAN_ND_PANID_FILTER);
00273 
00274     // Enable MPL by default
00275     const uint8_t all_mpl_forwarders[16] = {0xff, 0x03, [15]=0xfc};
00276     multicast_mpl_domain_subscribe(tasklet_data_ptr->network_interface_id,
00277                                       all_mpl_forwarders,
00278                                       MULTICAST_MPL_SEED_ID_DEFAULT,
00279                                       NULL);
00280 
00281     status = arm_nwk_interface_up(tasklet_data_ptr->network_interface_id);
00282     if (status >= 0) {
00283         tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_STARTED;
00284         tr_info("Start 6LoWPAN ND Bootstrap");
00285         nd_tasklet_network_state_changed(MESH_BOOTSTRAP_STARTED);
00286     } else {
00287         tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED;
00288         tr_err("Bootstrap start failed, %d", status);
00289         nd_tasklet_network_state_changed(MESH_BOOTSTRAP_START_FAILED);
00290     }
00291 }
00292 
00293 /*
00294  * Inform application about network state change
00295  */
00296 void nd_tasklet_network_state_changed(mesh_connection_status_t status)
00297 {
00298     if (tasklet_data_ptr->mesh_api_cb) {
00299         (tasklet_data_ptr->mesh_api_cb)(status);
00300     }
00301 }
00302 
00303 /*
00304  * Trace bootstrap information.
00305  */
00306 #ifdef TRACE_ND_TASKLET
00307 void nd_tasklet_trace_bootstrap_info()
00308 {
00309     network_layer_address_s app_nd_address_info;
00310     link_layer_address_s app_link_address_info;
00311     uint8_t temp_ipv6[16];
00312     if (arm_nwk_nd_address_read(tasklet_data_ptr->network_interface_id,
00313                                 &app_nd_address_info) != 0) {
00314         tr_error("ND Address read fail");
00315     } else {
00316         tr_debug("ND Access Point: %s", trace_ipv6(app_nd_address_info.border_router));
00317         tr_debug("ND Prefix 64: %s", trace_array(app_nd_address_info.prefix, 8));
00318 
00319         if (arm_net_address_get(tasklet_data_ptr->network_interface_id,
00320                                 ADDR_IPV6_GP, temp_ipv6) == 0) {
00321             tr_debug("GP IPv6: %s", trace_ipv6(temp_ipv6));
00322         }
00323     }
00324 
00325     if (arm_nwk_mac_address_read(tasklet_data_ptr->network_interface_id,
00326                                  &app_link_address_info) != 0) {
00327         tr_error("MAC Address read fail\n");
00328     } else {
00329         uint8_t temp[2];
00330         common_write_16_bit(app_link_address_info.mac_short,temp);
00331         tr_debug("MAC 16-bit: %s", trace_array(temp, 2));
00332         common_write_16_bit(app_link_address_info.PANId, temp);
00333         tr_debug("PAN ID: %s", trace_array(temp, 2));
00334         tr_debug("MAC 64-bit: %s", trace_array(app_link_address_info.mac_long, 8));
00335         tr_debug("IID (Based on MAC 64-bit address): %s", trace_array(app_link_address_info.iid_eui64, 8));
00336     }
00337 
00338     tr_debug("Channel: %d", arm_net_get_current_channel(tasklet_data_ptr->network_interface_id));
00339 }
00340 #endif /* #define TRACE_ND_TASKLET */
00341 
00342 /* Public functions */
00343 int8_t nd_tasklet_get_ip_address(char *address, int8_t len)
00344 {
00345     uint8_t binary_ipv6[16];
00346 
00347     if ((len >= 40) && (0 == arm_net_address_get(
00348                             tasklet_data_ptr->network_interface_id, ADDR_IPV6_GP, binary_ipv6))) {
00349         ip6tos(binary_ipv6, address);
00350         //tr_debug("IP address: %s", address);
00351         return 0;
00352     } else {
00353         return -1;
00354     }
00355 }
00356 
00357 int8_t nd_tasklet_get_router_ip_address(char *address, int8_t len)
00358 {
00359     network_layer_address_s nd_address;
00360 
00361     if ((len >= 40) && (0 == arm_nwk_nd_address_read(
00362                             tasklet_data_ptr->network_interface_id, &nd_address))) {
00363         ip6tos(nd_address.border_router, address);
00364         //tr_debug("Router IP address: %s", address);
00365         return 0;
00366     } else {
00367         return -1;
00368     }
00369 }
00370 
00371 int8_t nd_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id)
00372 {
00373     int8_t re_connecting = true;
00374     int8_t tasklet_id = tasklet_data_ptr->tasklet;
00375 
00376     if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) {
00377         return -3;  // already connected to network
00378     }
00379 
00380     if (tasklet_data_ptr->tasklet_state == TASKLET_STATE_CREATED) {
00381         re_connecting = false;
00382     }
00383 
00384     memset(tasklet_data_ptr, 0, sizeof(tasklet_data_str_t));
00385     tasklet_data_ptr->mesh_api_cb = callback;
00386     tasklet_data_ptr->network_interface_id = nwk_interface_id;
00387     tasklet_data_ptr->tasklet_state = TASKLET_STATE_INITIALIZED;
00388 
00389     tasklet_data_ptr->mode = MBED_CONF_MBED_MESH_API_6LOWPAN_ND_DEVICE_TYPE;
00390     tasklet_data_ptr->sec_mode = NET_SEC_MODE_NO_LINK_SECURITY;
00391     //tasklet_data_ptr->psk_sec_info.key_id = 0;
00392 
00393     if (re_connecting == false) {
00394         tasklet_data_ptr->tasklet = eventOS_event_handler_create(&nd_tasklet_main,
00395                 ARM_LIB_TASKLET_INIT_EVENT);
00396         if (tasklet_data_ptr->tasklet < 0) {
00397             // -1 handler already used by other tasklet
00398             // -2 memory allocation failure
00399             return tasklet_data_ptr->tasklet;
00400         }
00401         ns_event_loop_thread_start();
00402     } else {
00403         tasklet_data_ptr->tasklet = tasklet_id;
00404         mesh_system_send_connect_event(tasklet_data_ptr->tasklet);
00405     }
00406 
00407     return tasklet_data_ptr->tasklet;
00408 }
00409 
00410 int8_t nd_tasklet_disconnect(bool send_cb)
00411 {
00412     int8_t status = -1;
00413     if (tasklet_data_ptr != NULL) {
00414         if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) {
00415             status = arm_nwk_interface_down(tasklet_data_ptr->network_interface_id);
00416             tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID;
00417             if (send_cb == true) {
00418                 nd_tasklet_network_state_changed(MESH_DISCONNECTED);
00419             }
00420         }
00421         tasklet_data_ptr->mesh_api_cb = NULL;
00422     }
00423     return status;
00424 }
00425 
00426 void nd_tasklet_init(void)
00427 {
00428     if (tasklet_data_ptr == NULL) {
00429         // memory allocation will not fail as memory was just initialized
00430         tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(tasklet_data_str_t));
00431         tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED;
00432         tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID;
00433     }
00434 }
00435 
00436 int8_t nd_tasklet_network_init(int8_t device_id)
00437 {
00438     // TODO, read interface name from configuration
00439     mac_description_storage_size_t storage_sizes;
00440     storage_sizes.device_decription_table_size = 32;
00441     storage_sizes.key_description_table_size = 3;
00442     storage_sizes.key_lookup_size = 1;
00443     storage_sizes.key_usage_size = 3;
00444     if (!mac_api) {
00445         mac_api = ns_sw_mac_create(device_id, &storage_sizes);
00446     }
00447     return arm_nwk_interface_lowpan_init(mac_api, INTERFACE_NAME);
00448 }
00449