Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

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 "NanostackInterface.h"
00019 #include "mesh_system.h"
00020 #include "net_interface.h"
00021 #include "thread_management_if.h"
00022 
00023 
00024 MeshInterfaceNanostack::MeshInterfaceNanostack()
00025     : phy(NULL), _network_interface_id(-1), _device_id(-1), _eui64(),
00026       ip_addr_str(), mac_addr_str(), connect_semaphore(0), 
00027       _connection_status_cb(NULL), _connect_status(NSAPI_STATUS_DISCONNECTED ),
00028       _blocking(true)
00029 {
00030     // Nothing to do
00031 }
00032 
00033 MeshInterfaceNanostack::MeshInterfaceNanostack(NanostackPhy *phy)
00034     : phy(phy), _network_interface_id(-1), _device_id(-1), connect_semaphore(0)
00035 {
00036     // Nothing to do
00037 }
00038 
00039 nsapi_error_t MeshInterfaceNanostack::initialize(NanostackPhy *phy)
00040 {
00041     mesh_system_init();
00042     if (this->phy != NULL) {
00043         error("Phy already set");
00044     }
00045     this->phy = phy;
00046     return 0;
00047 }
00048 
00049 void MeshInterfaceNanostack::mesh_network_handler(mesh_connection_status_t status)
00050 {
00051     nanostack_lock();
00052 
00053     if ((status == MESH_CONNECTED || status == MESH_CONNECTED_LOCAL ||
00054          status == MESH_CONNECTED_GLOBAL) && _blocking) {
00055         connect_semaphore.release();
00056     }
00057 
00058     nanostack_unlock();
00059 
00060 
00061     if (status == MESH_CONNECTED) {
00062         uint8_t temp_ipv6_global[16];
00063         uint8_t temp_ipv6_local[16];
00064         if (arm_net_address_get(_network_interface_id, ADDR_IPV6_LL, temp_ipv6_local) == 0) {
00065             _connect_status = NSAPI_STATUS_LOCAL_UP ;
00066         }
00067         if (arm_net_address_get(_network_interface_id, ADDR_IPV6_GP, temp_ipv6_global) == 0
00068             && (memcmp(temp_ipv6_global, temp_ipv6_local, 16) != 0)) {
00069             _connect_status = NSAPI_STATUS_GLOBAL_UP ;
00070         }
00071     } else if (status == MESH_CONNECTED_LOCAL ) {
00072         _connect_status = NSAPI_STATUS_LOCAL_UP ;
00073     } else if (status == MESH_CONNECTED_GLOBAL) {
00074         _connect_status = NSAPI_STATUS_GLOBAL_UP ;
00075     } else if (status == MESH_BOOTSTRAP_STARTED) {
00076         _connect_status = NSAPI_STATUS_CONNECTING ;
00077     } else {
00078         _connect_status = NSAPI_STATUS_DISCONNECTED ;
00079     }
00080 
00081     if (_connection_status_cb) {
00082         _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , _connect_status);
00083     }
00084 }
00085 
00086 nsapi_error_t MeshInterfaceNanostack::register_phy()
00087 {
00088     nanostack_lock();
00089 
00090     _device_id = phy->phy_register();
00091     if (_device_id < 0) {
00092         nanostack_unlock();
00093         return -1;
00094     }
00095     // Read mac address after registering the device.
00096     const uint8_t empty_eui64[8] = {0,0,0,0,0,0,0,0};
00097     // if not set by application then read from rf driver
00098     if(!memcmp(_eui64, empty_eui64,8)) {
00099         phy->get_mac_address(_eui64);
00100     }
00101 
00102     sprintf(mac_addr_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", _eui64[0], _eui64[1], _eui64[2], _eui64[3], _eui64[4], _eui64[5], _eui64[6], _eui64[7]);
00103 
00104     nanostack_unlock();
00105 
00106     return 0;
00107 }
00108 
00109 NetworkStack * MeshInterfaceNanostack::get_stack()
00110 {
00111     return NanostackInterface::get_stack();
00112 }
00113 
00114 const char *MeshInterfaceNanostack::get_ip_address()
00115 {
00116     nanostack_lock();
00117 
00118     const char *ret = NULL;
00119     if (getOwnIpAddress(ip_addr_str, sizeof ip_addr_str)) {
00120         ret = ip_addr_str;
00121     }
00122 
00123     nanostack_unlock();
00124 
00125     return ret;
00126 }
00127 
00128 const char *MeshInterfaceNanostack::get_mac_address()
00129 {
00130     return mac_addr_str;
00131 }
00132 
00133 nsapi_connection_status_t MeshInterfaceNanostack::get_connection_status() const
00134 {
00135     return _connect_status;
00136 }
00137 
00138 void MeshInterfaceNanostack::attach(
00139     Callback<void(nsapi_event_t, intptr_t)> status_cb)
00140 {
00141     _connection_status_cb = status_cb;
00142 }
00143 
00144 nsapi_error_t MeshInterfaceNanostack::set_blocking(bool blocking)
00145 {
00146     _blocking = blocking;
00147     return NSAPI_ERROR_OK ;
00148 }