takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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 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) {
00142         _connect_status = NSAPI_STATUS_CONNECTING ;
00143     } else {
00144         _connect_status = NSAPI_STATUS_DISCONNECTED ;
00145     }
00146 
00147     if (_connection_status_cb) {
00148         _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , _connect_status);
00149     }
00150 }
00151 
00152 nsapi_error_t Nanostack::Interface::register_phy()
00153 {
00154     NanostackLockGuard lock;
00155 
00156     if (_device_id < 0) {
00157         _device_id = interface_phy.phy_register();
00158     }
00159     if (_device_id < 0) {
00160         return NSAPI_ERROR_DEVICE_ERROR ;
00161     }
00162 
00163     return NSAPI_ERROR_OK ;
00164 }
00165 
00166 Nanostack *InterfaceNanostack::get_stack()
00167 {
00168     return &Nanostack::get_instance();
00169 }
00170 
00171 const char *InterfaceNanostack::get_ip_address()
00172 {
00173     if (_interface->get_ip_address(ip_addr_str, sizeof(ip_addr_str))) {
00174         return ip_addr_str;
00175     }
00176     return NULL;
00177 }
00178 
00179 const char *InterfaceNanostack::get_mac_address()
00180 {
00181     if (_interface->get_mac_address(mac_addr_str, sizeof(mac_addr_str))) {
00182         return mac_addr_str;
00183     }
00184     return NULL;
00185 }
00186 
00187 nsapi_connection_status_t InterfaceNanostack::get_connection_status() const
00188 {
00189     if (_interface) {
00190         return _interface->get_connection_status();
00191     } else {
00192         return NSAPI_STATUS_DISCONNECTED ;
00193     }
00194 }
00195 
00196 void InterfaceNanostack::attach(
00197     mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00198 {
00199     _connection_status_cb = status_cb;
00200     if (_interface) {
00201         _interface->attach(status_cb);
00202     }
00203 }
00204 
00205 nsapi_error_t InterfaceNanostack::set_blocking(bool blocking)
00206 {
00207     _blocking = blocking;
00208     return NSAPI_ERROR_OK ;
00209 }
00210 
00211 #if !DEVICE_802_15_4_PHY
00212 MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
00213 {
00214     return NULL;
00215 }
00216 #endif