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
ethernet_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/static_config.h" 00024 #include "include/mesh_system.h" 00025 #include "ns_event_loop.h" 00026 #include "mesh_interface_types.h" 00027 #include "eventOS_event.h" 00028 00029 // For tracing we need to define flag, have include and define group 00030 #include "ns_trace.h" 00031 #define TRACE_GROUP "IPV6" 00032 00033 #include "ethernet_mac_api.h" 00034 00035 #define INTERFACE_NAME "eth0" 00036 00037 // Tasklet timer events 00038 #define TIMER_EVENT_START_BOOTSTRAP 1 00039 00040 #define INVALID_INTERFACE_ID (-1) 00041 00042 #define STR_HELPER(x) #x 00043 #define STR(x) STR_HELPER(x) 00044 00045 /* 00046 * Mesh tasklet states. 00047 */ 00048 typedef enum { 00049 TASKLET_STATE_CREATED = 0, 00050 TASKLET_STATE_INITIALIZED, 00051 TASKLET_STATE_BOOTSTRAP_STARTED, 00052 TASKLET_STATE_BOOTSTRAP_FAILED, 00053 TASKLET_STATE_BOOTSTRAP_READY 00054 } tasklet_state_t; 00055 00056 /* 00057 * Mesh tasklet data structure. 00058 */ 00059 typedef struct { 00060 void (*mesh_api_cb)(mesh_connection_status_t nwk_status); 00061 tasklet_state_t tasklet_state; 00062 int8_t node_main_tasklet_id; 00063 int8_t network_interface_id; 00064 int8_t tasklet; 00065 } tasklet_data_str_t; 00066 00067 /* Tasklet data */ 00068 static tasklet_data_str_t *tasklet_data_ptr = NULL; 00069 static eth_mac_api_t *eth_mac_api = NULL; 00070 typedef void (*mesh_interface_cb)(mesh_connection_status_t mesh_status); 00071 00072 00073 /* private function prototypes */ 00074 static void enet_tasklet_main(arm_event_s *event); 00075 static void enet_tasklet_network_state_changed(mesh_connection_status_t status); 00076 static void enet_tasklet_parse_network_event(arm_event_s *event); 00077 static void enet_tasklet_configure_and_connect_to_network(void); 00078 00079 /* 00080 * \brief A function which will be eventually called by NanoStack OS when ever the OS has an event to deliver. 00081 * @param event, describes the sender, receiver and event type. 00082 * 00083 * NOTE: Interrupts requested by HW are possible during this function! 00084 */ 00085 void enet_tasklet_main(arm_event_s *event) 00086 { 00087 arm_library_event_type_e event_type; 00088 event_type = (arm_library_event_type_e) event->event_type; 00089 00090 switch (event_type) { 00091 case ARM_LIB_NWK_INTERFACE_EVENT: 00092 /* This event is delivered every and each time when there is new 00093 * information of network connectivity. 00094 */ 00095 enet_tasklet_parse_network_event(event); 00096 break; 00097 00098 case ARM_LIB_TASKLET_INIT_EVENT: 00099 /* Event with type EV_INIT is an initializer event of NanoStack OS. 00100 * The event is delivered when the NanoStack OS is running fine. 00101 * This event should be delivered ONLY ONCE. 00102 */ 00103 tasklet_data_ptr->node_main_tasklet_id = event->receiver; 00104 mesh_system_send_connect_event(tasklet_data_ptr->tasklet); 00105 break; 00106 00107 case ARM_LIB_SYSTEM_TIMER_EVENT: 00108 eventOS_event_timer_cancel(event->event_id, 00109 tasklet_data_ptr->node_main_tasklet_id); 00110 00111 if (event->event_id == TIMER_EVENT_START_BOOTSTRAP) { 00112 tr_debug("Restart bootstrap"); 00113 enet_tasklet_configure_and_connect_to_network(); 00114 } 00115 break; 00116 00117 case APPLICATION_EVENT: 00118 if (event->event_id == APPL_EVENT_CONNECT) { 00119 enet_tasklet_configure_and_connect_to_network(); 00120 } 00121 break; 00122 00123 default: 00124 break; 00125 } // switch(event_type) 00126 } 00127 00128 /** 00129 * \brief Network state event handler. 00130 * \param event show network start response or current network state. 00131 * 00132 * - ARM_NWK_BOOTSTRAP_READY: Save NVK persistent data to NVM and Net role 00133 * - ARM_NWK_NWK_SCAN_FAIL: Link Layer Active Scan Fail, Stack is Already at Idle state 00134 * - ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: No ND Router at current Channel Stack is Already at Idle state 00135 * - ARM_NWK_NWK_CONNECTION_DOWN: Connection to Access point is lost wait for Scan Result 00136 * - ARM_NWK_NWK_PARENT_POLL_FAIL: Host should run net start without any PAN-id filter and all channels 00137 * - ARM_NWK_AUHTENTICATION_FAIL: Pana Authentication fail, Stack is Already at Idle state 00138 */ 00139 void enet_tasklet_parse_network_event(arm_event_s *event) 00140 { 00141 arm_nwk_interface_status_type_e status = (arm_nwk_interface_status_type_e) event->event_data; 00142 tr_debug("app_parse_network_event() %d", status); 00143 switch (status) { 00144 case ARM_NWK_BOOTSTRAP_READY: 00145 /* Network is ready and node is connected to Access Point */ 00146 if (tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) { 00147 tr_info("IPv6 bootstrap ready"); 00148 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_READY; 00149 enet_tasklet_network_state_changed(MESH_CONNECTED); 00150 } 00151 break; 00152 case ARM_NWK_IP_ADDRESS_ALLOCATION_FAIL: 00153 /* No ND Router at current Channel Stack is Already at Idle state */ 00154 tr_info("Bootstrap fail"); 00155 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00156 break; 00157 case ARM_NWK_NWK_CONNECTION_DOWN: 00158 /* Connection to Access point is lost wait for Scan Result */ 00159 tr_info("Connection lost"); 00160 tasklet_data_ptr->tasklet_state = TASKLET_STATE_BOOTSTRAP_FAILED; 00161 break; 00162 default: 00163 tr_warn("Unknown event %d", status); 00164 break; 00165 } 00166 00167 if (tasklet_data_ptr->tasklet_state != TASKLET_STATE_BOOTSTRAP_READY) { 00168 // Set 5s timer for new network scan 00169 eventOS_event_timer_request(TIMER_EVENT_START_BOOTSTRAP, 00170 ARM_LIB_SYSTEM_TIMER_EVENT, 00171 tasklet_data_ptr->node_main_tasklet_id, 00172 5000); 00173 } 00174 } 00175 00176 /* 00177 * \brief Configure and establish network connection 00178 * 00179 */ 00180 void enet_tasklet_configure_and_connect_to_network(void) 00181 { 00182 arm_nwk_interface_up(tasklet_data_ptr->network_interface_id); 00183 } 00184 00185 /* 00186 * Inform application about network state change 00187 */ 00188 void enet_tasklet_network_state_changed(mesh_connection_status_t status) 00189 { 00190 if (tasklet_data_ptr->mesh_api_cb) { 00191 (tasklet_data_ptr->mesh_api_cb)(status); 00192 } 00193 } 00194 00195 /* Public functions */ 00196 int8_t enet_tasklet_get_ip_address(char *address, int8_t len) 00197 { 00198 uint8_t binary_ipv6[16]; 00199 00200 if ((len >= 40) && (0 == arm_net_address_get( 00201 tasklet_data_ptr->network_interface_id, ADDR_IPV6_GP, binary_ipv6))) { 00202 ip6tos(binary_ipv6, address); 00203 //tr_debug("IP address: %s", address); 00204 return 0; 00205 } else { 00206 return -1; 00207 } 00208 } 00209 00210 int8_t enet_tasklet_connect(mesh_interface_cb callback, int8_t nwk_interface_id) 00211 { 00212 int8_t re_connecting = true; 00213 int8_t tasklet_id = tasklet_data_ptr->tasklet; 00214 00215 if (tasklet_data_ptr->tasklet_state == TASKLET_STATE_CREATED) { 00216 re_connecting = false; 00217 } 00218 00219 memset(tasklet_data_ptr, 0, sizeof(tasklet_data_str_t)); 00220 tasklet_data_ptr->mesh_api_cb = callback; 00221 tasklet_data_ptr->network_interface_id = nwk_interface_id; 00222 tasklet_data_ptr->tasklet_state = TASKLET_STATE_INITIALIZED; 00223 00224 if (re_connecting == false) { 00225 tasklet_data_ptr->tasklet = eventOS_event_handler_create(&enet_tasklet_main, 00226 ARM_LIB_TASKLET_INIT_EVENT); 00227 if (tasklet_data_ptr->tasklet < 0) { 00228 // -1 handler already used by other tasklet 00229 // -2 memory allocation failure 00230 return tasklet_data_ptr->tasklet; 00231 } 00232 } else { 00233 tasklet_data_ptr->tasklet = tasklet_id; 00234 mesh_system_send_connect_event(tasklet_data_ptr->tasklet); 00235 } 00236 00237 return 0; 00238 } 00239 00240 int8_t enet_tasklet_disconnect(bool send_cb) 00241 { 00242 int8_t status = -1; 00243 if (tasklet_data_ptr != NULL) { 00244 if (tasklet_data_ptr->network_interface_id != INVALID_INTERFACE_ID) { 00245 status = arm_nwk_interface_down(tasklet_data_ptr->network_interface_id); 00246 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00247 if (send_cb == true) { 00248 enet_tasklet_network_state_changed(MESH_DISCONNECTED); 00249 } 00250 } 00251 tasklet_data_ptr->mesh_api_cb = NULL; 00252 } 00253 return status; 00254 } 00255 00256 void enet_tasklet_init(void) 00257 { 00258 if (tasklet_data_ptr == NULL) { 00259 tasklet_data_ptr = ns_dyn_mem_alloc(sizeof(tasklet_data_str_t)); 00260 tasklet_data_ptr->tasklet_state = TASKLET_STATE_CREATED; 00261 tasklet_data_ptr->network_interface_id = INVALID_INTERFACE_ID; 00262 } 00263 } 00264 00265 int8_t enet_tasklet_network_init(int8_t device_id) 00266 { 00267 if (tasklet_data_ptr->network_interface_id != -1) { 00268 tr_debug("Interface already at active state\n"); 00269 return tasklet_data_ptr->network_interface_id; 00270 } 00271 if (!eth_mac_api) { 00272 eth_mac_api = ethernet_mac_create(device_id); 00273 } 00274 00275 tasklet_data_ptr->network_interface_id = arm_nwk_interface_ethernet_init(eth_mac_api, "eth0"); 00276 00277 tr_debug("interface ID: %d", tasklet_data_ptr->network_interface_id); 00278 arm_nwk_interface_configure_ipv6_bootstrap_set( 00279 tasklet_data_ptr->network_interface_id, NET_IPV6_BOOTSTRAP_AUTONOMOUS, NULL); 00280 return tasklet_data_ptr->network_interface_id; 00281 }
Generated on Tue Jul 12 2022 11:02:37 by
