Move MeshInterfaceNanostack to own file.

+Also nanostack_lock() moved to mesh_system.h
+Added includes into NanostackInterface.h to be backward
compatible
pull/3267/head
Seppo Takalo 2016-11-14 13:21:08 +02:00
parent 27cced74fb
commit 6ba755e444
9 changed files with 216 additions and 182 deletions

View File

@ -17,7 +17,7 @@
#ifndef LOWPANNDINTERFACE_H
#define LOWPANNDINTERFACE_H
#include "NanostackInterface.h"
#include "MeshInterfaceNanostack.h"
class LoWPANNDInterface : public MeshInterfaceNanostack {
public:

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2016 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MESHINTERFACENANOSTACK_H
#define MESHINTERFACENANOSTACK_H
#include "mbed.h"
#include "MeshInterface.h"
#include "NanostackRfPhy.h"
#include "mesh_interface_types.h"
class MeshInterfaceNanostack : public MeshInterface {
public:
/** Attach phy and initialize the mesh
*
* Initializes a mesh interface on the given phy. Not needed if
* the phy is passed to the mesh's constructor.
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t initialize(NanostackRfPhy *phy);
/** Start the interface
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t connect() = 0;
/** Stop the interface
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t disconnect() = 0;
/** Get the internally stored IP address
/return IP address of the interface or null if not yet connected
*/
virtual const char *get_ip_address();
/** Get the internally stored MAC address
/return MAC address of the interface
*/
virtual const char *get_mac_address();
/**
* \brief Callback from C-layer
* \param state state of the network
* */
void mesh_network_handler(mesh_connection_status_t status);
protected:
MeshInterfaceNanostack();
MeshInterfaceNanostack(NanostackRfPhy *phy);
nsapi_error_t register_rf();
virtual NetworkStack * get_stack(void);
/**
* \brief Connect interface to the mesh network
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_PARAM in case of illegal parameters.
* \return MESH_ERROR_MEMORY in case of memory error.
* \return MESH_ERROR_STATE if interface is already connected to network.
* \return MESH_ERROR_UNKNOWN in case of unspecified error.
* */
virtual mesh_error_t mesh_connect() = 0;
/**
* \brief Disconnect interface from the mesh network
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_UNKNOWN in case of error.
* */
virtual mesh_error_t mesh_disconnect() = 0;
/**
* \brief Read own global IP address
*
* \param address is where the IP address will be copied
* \param len is the length of the address buffer, must be at least 40 bytes
* \return true if address is read successfully, false otherwise
*/
virtual bool getOwnIpAddress(char *address, int8_t len) = 0;
NanostackRfPhy *phy;
/** Network interface ID */
int8_t _network_interface_id;
/** Registered device ID */
int8_t _device_id;
uint8_t eui64[8];
char ip_addr_str[40];
char mac_addr_str[24];
Semaphore connect_semaphore;
};
#endif /* MESHINTERFACENANOSTACK_H */

View File

@ -17,7 +17,7 @@
#ifndef THREADINTERFACE_H
#define THREADINTERFACE_H
#include "NanostackInterface.h"
#include "MeshInterfaceNanostack.h"
class ThreadInterface : public MeshInterfaceNanostack {
public:

View File

@ -7,12 +7,6 @@
#include "ns_trace.h"
#define TRACE_GROUP "nslp"
#include "nanostack-event-loop/eventOS_scheduler.h"
#define nanostack_lock() eventOS_scheduler_mutex_wait()
#define nanostack_unlock() eventOS_scheduler_mutex_release()
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
int LoWPANNDInterface::connect()
{
nanostack_lock();

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2016 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MeshInterfaceNanostack.h"
#include "NanostackInterface.h"
#include "mesh_system.h"
MeshInterfaceNanostack::MeshInterfaceNanostack()
: phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(),
ip_addr_str(), mac_addr_str(), connect_semaphore(0)
{
// Nothing to do
}
MeshInterfaceNanostack::MeshInterfaceNanostack(NanostackRfPhy *phy)
: phy(phy), _network_interface_id(-1), _device_id(-1), connect_semaphore(0)
{
// Nothing to do
}
nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
{
mesh_system_init();
if (this->phy != NULL) {
error("Phy already set");
}
this->phy = phy;
return 0;
}
void MeshInterfaceNanostack::mesh_network_handler(mesh_connection_status_t status)
{
nanostack_lock();
if (status == MESH_CONNECTED) {
connect_semaphore.release();
}
nanostack_unlock();
}
nsapi_error_t MeshInterfaceNanostack::register_rf()
{
nanostack_lock();
_device_id = phy->rf_register();
if (_device_id < 0) {
nanostack_unlock();
return -1;
}
// Read mac address after registering the device.
phy->get_mac_address(eui64);
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]);
nanostack_unlock();
return 0;
}
NetworkStack * MeshInterfaceNanostack::get_stack()
{
return NanostackInterface::get_stack();
}
const char *MeshInterfaceNanostack::get_ip_address()
{
nanostack_lock();
const char *ret = NULL;
if (getOwnIpAddress(ip_addr_str, sizeof ip_addr_str)) {
ret = ip_addr_str;
}
nanostack_unlock();
return ret;
}
const char *MeshInterfaceNanostack::get_mac_address()
{
return mac_addr_str;
}

View File

@ -7,12 +7,6 @@
#include "ns_trace.h"
#define TRACE_GROUP "nsth"
#include "nanostack-event-loop/eventOS_scheduler.h"
#define nanostack_lock() eventOS_scheduler_mutex_wait()
#define nanostack_unlock() eventOS_scheduler_mutex_release()
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
int ThreadInterface::connect()
{
nanostack_lock();

View File

@ -42,4 +42,11 @@ void mesh_system_init(void);
#ifdef __cplusplus
}
#endif
#include "nanostack-event-loop/eventOS_scheduler.h"
#define nanostack_lock() eventOS_scheduler_mutex_wait()
#define nanostack_unlock() eventOS_scheduler_mutex_release()
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
#endif /* __INCLUDE_MESH_SYSTEM__ */

View File

@ -32,12 +32,6 @@
#include "ns_trace.h"
#define TRACE_GROUP "nsif"
#include "nanostack-event-loop/eventOS_scheduler.h"
#define nanostack_lock() eventOS_scheduler_mutex_wait()
#define nanostack_unlock() eventOS_scheduler_mutex_release()
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
#define NS_INTERFACE_SOCKETS_MAX 16 //same as NanoStack SOCKET_MAX
#define NANOSTACK_SOCKET_UDP 17 // same as nanostack SOCKET_UDP
#define NANOSTACK_SOCKET_TCP 6 // same as nanostack SOCKET_TCP
@ -445,82 +439,6 @@ void NanostackSocket::event_connnect_closed(socket_callback_t *sock_cb)
close();
}
MeshInterfaceNanostack::MeshInterfaceNanostack()
: phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(),
ip_addr_str(), mac_addr_str(), connect_semaphore(0)
{
// Nothing to do
}
MeshInterfaceNanostack::MeshInterfaceNanostack(NanostackRfPhy *phy)
: phy(phy), _network_interface_id(-1), _device_id(-1), connect_semaphore(0)
{
// Nothing to do
}
nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
{
mesh_system_init();
if (this->phy != NULL) {
error("Phy already set");
}
this->phy = phy;
return 0;
}
void MeshInterfaceNanostack::mesh_network_handler(mesh_connection_status_t status)
{
nanostack_lock();
if (status == MESH_CONNECTED) {
connect_semaphore.release();
}
nanostack_unlock();
}
nsapi_error_t MeshInterfaceNanostack::register_rf()
{
nanostack_lock();
_device_id = phy->rf_register();
if (_device_id < 0) {
nanostack_unlock();
return -1;
}
// Read mac address after registering the device.
phy->get_mac_address(eui64);
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]);
nanostack_unlock();
return 0;
}
NetworkStack * MeshInterfaceNanostack::get_stack()
{
return NanostackInterface::get_stack();
}
const char *MeshInterfaceNanostack::get_ip_address()
{
nanostack_lock();
const char *ret = NULL;
if (getOwnIpAddress(ip_addr_str, sizeof ip_addr_str)) {
ret = ip_addr_str;
}
nanostack_unlock();
return ret;
}
const char *MeshInterfaceNanostack::get_mac_address()
{
return mac_addr_str;
}
NanostackInterface * NanostackInterface::_ns_interface;
NanostackInterface * NanostackInterface::get_stack()

View File

@ -20,8 +20,10 @@
#include "mbed.h"
#include "NetworkStack.h"
#include "MeshInterface.h"
#include "NanostackRfPhy.h"
#include "mesh_interface_types.h"
// Include here for backward compatibility
#include "LoWPANNDInterface.h"
#include "ThreadInterface.h"
#include "MeshInterfaceNanostack.h"
class NanostackInterface : public NetworkStack {
public:
@ -229,90 +231,6 @@ private:
static NanostackInterface * _ns_interface;
};
class MeshInterfaceNanostack : public MeshInterface {
public:
/** Attach phy and initialize the mesh
*
* Initializes a mesh interface on the given phy. Not needed if
* the phy is passed to the mesh's constructor.
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t initialize(NanostackRfPhy *phy);
/** Start the interface
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t connect() = 0;
/** Stop the interface
*
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t disconnect() = 0;
/** Get the internally stored IP address
/return IP address of the interface or null if not yet connected
*/
virtual const char *get_ip_address();
/** Get the internally stored MAC address
/return MAC address of the interface
*/
virtual const char *get_mac_address();
/**
* \brief Callback from C-layer
* \param state state of the network
* */
void mesh_network_handler(mesh_connection_status_t status);
protected:
MeshInterfaceNanostack();
MeshInterfaceNanostack(NanostackRfPhy *phy);
nsapi_error_t register_rf();
virtual NetworkStack * get_stack(void);
/**
* \brief Connect interface to the mesh network
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_PARAM in case of illegal parameters.
* \return MESH_ERROR_MEMORY in case of memory error.
* \return MESH_ERROR_STATE if interface is already connected to network.
* \return MESH_ERROR_UNKNOWN in case of unspecified error.
* */
virtual mesh_error_t mesh_connect() = 0;
/**
* \brief Disconnect interface from the mesh network
* \return MESH_ERROR_NONE on success.
* \return MESH_ERROR_UNKNOWN in case of error.
* */
virtual mesh_error_t mesh_disconnect() = 0;
/**
* \brief Read own global IP address
*
* \param address is where the IP address will be copied
* \param len is the length of the address buffer, must be at least 40 bytes
* \return true if address is read successfully, false otherwise
*/
virtual bool getOwnIpAddress(char *address, int8_t len) = 0;
NanostackRfPhy *phy;
/** Network interface ID */
int8_t _network_interface_id;
/** Registered device ID */
int8_t _device_id;
uint8_t eui64[8];
char ip_addr_str[40];
char mac_addr_str[24];
Semaphore connect_semaphore;
};
nsapi_error_t map_mesh_error(mesh_error_t err);
#endif /* NANOSTACK_INTERFACE_H_ */