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: nRF51_Vdd TextLCD BME280
MeshInterfaceNanostack.cpp
00001 /* 00002 * Copyright (c) 2016 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 "MeshInterfaceNanostack.h" 00018 #include "Nanostack.h" 00019 #include "NanostackLockGuard.h" 00020 #include "mesh_system.h" 00021 #include "nanostack/net_interface.h" 00022 #include "thread_management_if.h" 00023 #include "ip6string.h" 00024 00025 char *Nanostack::Interface::get_ip_address(char *buf, nsapi_size_t buflen) 00026 { 00027 NanostackLockGuard lock; 00028 uint8_t binary_ipv6[16]; 00029 00030 if (buflen >= 40 && arm_net_address_get(interface_id, ADDR_IPV6_GP, binary_ipv6) == 0) { 00031 ip6tos(binary_ipv6, buf); 00032 return buf; 00033 } else { 00034 return NULL; 00035 } 00036 } 00037 00038 char *Nanostack::Interface::get_mac_address(char *buf, nsapi_size_t buflen) 00039 { 00040 NanostackLockGuard lock; 00041 link_layer_address_s addr; 00042 if (buflen >= 24 && arm_nwk_mac_address_read(interface_id, &addr) == 0) { 00043 snprintf(buf, buflen, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", addr.mac_long[0], addr.mac_long[1], addr.mac_long[2], addr.mac_long[3], addr.mac_long[4], addr.mac_long[5], addr.mac_long[6], addr.mac_long[7]); 00044 return buf; 00045 } else { 00046 return NULL; 00047 } 00048 } 00049 00050 char *Nanostack::Interface::get_netmask(char *, nsapi_size_t) 00051 { 00052 return NULL; 00053 } 00054 00055 char *Nanostack::Interface::get_gateway(char *, nsapi_size_t) 00056 { 00057 return NULL; 00058 } 00059 00060 nsapi_connection_status_t Nanostack::Interface::get_connection_status() const 00061 { 00062 return _connect_status; 00063 } 00064 00065 void Nanostack::Interface::attach( 00066 mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) 00067 { 00068 _connection_status_cb = status_cb; 00069 } 00070 00071 Nanostack::Interface::Interface(NanostackPhy &phy) : interface_phy(phy), interface_id(-1), _device_id(-1), 00072 _connect_status(NSAPI_STATUS_DISCONNECTED ), _previous_connection_status(NSAPI_STATUS_DISCONNECTED ), _blocking(true) 00073 { 00074 mesh_system_init(); 00075 } 00076 00077 00078 InterfaceNanostack::InterfaceNanostack() 00079 : _interface(NULL), 00080 ip_addr_str(), mac_addr_str(), _blocking(true) 00081 { 00082 // Nothing to do 00083 } 00084 00085 int InterfaceNanostack::connect() 00086 { 00087 nsapi_error_t error = do_initialize(); 00088 if (error) { 00089 return error; 00090 } 00091 00092 return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking); 00093 } 00094 00095 int InterfaceNanostack::disconnect() 00096 { 00097 if (!_interface) { 00098 return NSAPI_ERROR_NO_CONNECTION ; 00099 } 00100 return _interface->bringdown(); 00101 } 00102 00103 nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy) 00104 { 00105 if (_phy && phy && _phy != phy) { 00106 error("Phy already set"); 00107 return NSAPI_ERROR_IS_CONNECTED ; 00108 } 00109 if (phy) { 00110 _phy = phy; 00111 } 00112 if (_phy) { 00113 return do_initialize(); 00114 } else { 00115 return NSAPI_ERROR_PARAMETER ; 00116 } 00117 } 00118 00119 void Nanostack::Interface::network_handler(mesh_connection_status_t status) 00120 { 00121 if ((status == MESH_CONNECTED || status == MESH_CONNECTED_LOCAL || 00122 status == MESH_CONNECTED_GLOBAL) && _blocking) { 00123 connect_semaphore.release(); 00124 } 00125 00126 00127 if (status == MESH_CONNECTED) { 00128 uint8_t temp_ipv6_global[16]; 00129 uint8_t temp_ipv6_local[16]; 00130 if (arm_net_address_get(interface_id, ADDR_IPV6_LL, temp_ipv6_local) == 0) { 00131 _connect_status = NSAPI_STATUS_LOCAL_UP ; 00132 } 00133 if (arm_net_address_get(interface_id, ADDR_IPV6_GP, temp_ipv6_global) == 0 00134 && (memcmp(temp_ipv6_global, temp_ipv6_local, 16) != 0)) { 00135 _connect_status = NSAPI_STATUS_GLOBAL_UP ; 00136 } 00137 } else if (status == MESH_CONNECTED_LOCAL ) { 00138 _connect_status = NSAPI_STATUS_LOCAL_UP ; 00139 } else if (status == MESH_CONNECTED_GLOBAL) { 00140 _connect_status = NSAPI_STATUS_GLOBAL_UP ; 00141 } else if (status == MESH_BOOTSTRAP_STARTED || status == MESH_BOOTSTRAP_FAILED) { 00142 _connect_status = NSAPI_STATUS_CONNECTING ; 00143 } else { 00144 _connect_status = NSAPI_STATUS_DISCONNECTED ; 00145 } 00146 00147 if (_connection_status_cb && _previous_connection_status != _connect_status) { 00148 00149 _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , _connect_status); 00150 } 00151 _previous_connection_status = _connect_status; 00152 } 00153 00154 nsapi_error_t Nanostack::Interface::register_phy() 00155 { 00156 NanostackLockGuard lock; 00157 00158 if (_device_id < 0) { 00159 _device_id = interface_phy.phy_register(); 00160 } 00161 if (_device_id < 0) { 00162 return NSAPI_ERROR_DEVICE_ERROR ; 00163 } 00164 00165 return NSAPI_ERROR_OK ; 00166 } 00167 00168 Nanostack *InterfaceNanostack::get_stack() 00169 { 00170 return &Nanostack::get_instance(); 00171 } 00172 00173 const char *InterfaceNanostack::get_ip_address() 00174 { 00175 if (_interface->get_ip_address(ip_addr_str, sizeof(ip_addr_str))) { 00176 return ip_addr_str; 00177 } 00178 return NULL; 00179 } 00180 00181 const char *InterfaceNanostack::get_mac_address() 00182 { 00183 if (_interface->get_mac_address(mac_addr_str, sizeof(mac_addr_str))) { 00184 return mac_addr_str; 00185 } 00186 return NULL; 00187 } 00188 00189 nsapi_connection_status_t InterfaceNanostack::get_connection_status() const 00190 { 00191 if (_interface) { 00192 return _interface->get_connection_status(); 00193 } else { 00194 return NSAPI_STATUS_DISCONNECTED ; 00195 } 00196 } 00197 00198 void InterfaceNanostack::attach( 00199 mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) 00200 { 00201 _connection_status_cb = status_cb; 00202 if (_interface) { 00203 _interface->attach(status_cb); 00204 } 00205 } 00206 00207 nsapi_error_t InterfaceNanostack::set_blocking(bool blocking) 00208 { 00209 _blocking = blocking; 00210 return NSAPI_ERROR_OK ; 00211 } 00212 00213 #if !DEVICE_802_15_4_PHY 00214 MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance() 00215 { 00216 return NULL; 00217 } 00218 #endif
Generated on Tue Jul 12 2022 15:15:52 by
