Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MeshInterfaceNanostack.cpp Source File

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 ), _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 nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
00086 {
00087     if (_phy) {
00088         error("Phy already set");
00089         return NSAPI_ERROR_IS_CONNECTED ;
00090     }
00091     _phy = phy;
00092     return NSAPI_ERROR_OK ;
00093 }
00094 
00095 
00096 void Nanostack::Interface::network_handler(mesh_connection_status_t status)
00097 {
00098     if ((status == MESH_CONNECTED || status == MESH_CONNECTED_LOCAL ||
00099          status == MESH_CONNECTED_GLOBAL) && _blocking) {
00100         connect_semaphore.release();
00101     }
00102 
00103 
00104     if (status == MESH_CONNECTED) {
00105         uint8_t temp_ipv6_global[16];
00106         uint8_t temp_ipv6_local[16];
00107         if (arm_net_address_get(interface_id, ADDR_IPV6_LL, temp_ipv6_local) == 0) {
00108             _connect_status = NSAPI_STATUS_LOCAL_UP ;
00109         }
00110         if (arm_net_address_get(interface_id, ADDR_IPV6_GP, temp_ipv6_global) == 0
00111             && (memcmp(temp_ipv6_global, temp_ipv6_local, 16) != 0)) {
00112             _connect_status = NSAPI_STATUS_GLOBAL_UP ;
00113         }
00114     } else if (status == MESH_CONNECTED_LOCAL ) {
00115         _connect_status = NSAPI_STATUS_LOCAL_UP ;
00116     } else if (status == MESH_CONNECTED_GLOBAL) {
00117         _connect_status = NSAPI_STATUS_GLOBAL_UP ;
00118     } else if (status == MESH_BOOTSTRAP_STARTED) {
00119         _connect_status = NSAPI_STATUS_CONNECTING ;
00120     } else {
00121         _connect_status = NSAPI_STATUS_DISCONNECTED ;
00122     }
00123 
00124     if (_connection_status_cb) {
00125         _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , _connect_status);
00126     }
00127 }
00128 
00129 nsapi_error_t Nanostack::Interface::register_phy()
00130 {
00131     NanostackLockGuard lock;
00132 
00133     _device_id = interface_phy.phy_register();
00134     if (_device_id < 0) {
00135         return NSAPI_ERROR_DEVICE_ERROR ;
00136     }
00137 
00138     return NSAPI_ERROR_OK ;
00139 }
00140 
00141 Nanostack *InterfaceNanostack::get_stack()
00142 {
00143     return &Nanostack::get_instance();
00144 }
00145 
00146 const char *InterfaceNanostack::get_ip_address()
00147 {
00148     if (_interface->get_ip_address(ip_addr_str, sizeof(ip_addr_str))) {
00149         return ip_addr_str;
00150     }
00151     return NULL;
00152 }
00153 
00154 const char *InterfaceNanostack::get_mac_address()
00155 {
00156     if (_interface->get_mac_address(mac_addr_str, sizeof(mac_addr_str))) {
00157         return mac_addr_str;
00158     }
00159     return NULL;
00160 }
00161 
00162 nsapi_connection_status_t InterfaceNanostack::get_connection_status() const
00163 {
00164     if (_interface) {
00165         return _interface->get_connection_status();
00166     } else {
00167         return NSAPI_STATUS_DISCONNECTED ;
00168     }
00169 }
00170 
00171 void InterfaceNanostack::attach(
00172     mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00173 {
00174     _connection_status_cb = status_cb;
00175     if (_interface) {
00176         _interface->attach(status_cb);
00177     }
00178 }
00179 
00180 nsapi_error_t InterfaceNanostack::set_blocking(bool blocking)
00181 {
00182     _blocking = blocking;
00183     return NSAPI_ERROR_OK ;
00184 }
00185 
00186 #if !DEVICE_802_15_4_PHY
00187 MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
00188 {
00189     return NULL;
00190 }
00191 #endif