Skip to content

Commit 6ba755e

Browse files
author
Seppo Takalo
committed
Move MeshInterfaceNanostack to own file.
+Also nanostack_lock() moved to mesh_system.h +Added includes into NanostackInterface.h to be backward compatible
1 parent 27cced7 commit 6ba755e

File tree

9 files changed

+216
-182
lines changed

9 files changed

+216
-182
lines changed

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/LoWPANNDInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef LOWPANNDINTERFACE_H
1818
#define LOWPANNDINTERFACE_H
1919

20-
#include "NanostackInterface.h"
20+
#include "MeshInterfaceNanostack.h"
2121

2222
class LoWPANNDInterface : public MeshInterfaceNanostack {
2323
public:
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2016 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MESHINTERFACENANOSTACK_H
18+
#define MESHINTERFACENANOSTACK_H
19+
#include "mbed.h"
20+
21+
#include "MeshInterface.h"
22+
#include "NanostackRfPhy.h"
23+
#include "mesh_interface_types.h"
24+
25+
class MeshInterfaceNanostack : public MeshInterface {
26+
public:
27+
28+
/** Attach phy and initialize the mesh
29+
*
30+
* Initializes a mesh interface on the given phy. Not needed if
31+
* the phy is passed to the mesh's constructor.
32+
*
33+
* @return 0 on success, negative on failure
34+
*/
35+
virtual nsapi_error_t initialize(NanostackRfPhy *phy);
36+
37+
/** Start the interface
38+
*
39+
* @return 0 on success, negative on failure
40+
*/
41+
virtual nsapi_error_t connect() = 0;
42+
43+
/** Stop the interface
44+
*
45+
* @return 0 on success, negative on failure
46+
*/
47+
virtual nsapi_error_t disconnect() = 0;
48+
49+
/** Get the internally stored IP address
50+
/return IP address of the interface or null if not yet connected
51+
*/
52+
virtual const char *get_ip_address();
53+
54+
/** Get the internally stored MAC address
55+
/return MAC address of the interface
56+
*/
57+
virtual const char *get_mac_address();
58+
59+
/**
60+
* \brief Callback from C-layer
61+
* \param state state of the network
62+
* */
63+
void mesh_network_handler(mesh_connection_status_t status);
64+
65+
protected:
66+
MeshInterfaceNanostack();
67+
MeshInterfaceNanostack(NanostackRfPhy *phy);
68+
nsapi_error_t register_rf();
69+
virtual NetworkStack * get_stack(void);
70+
71+
/**
72+
* \brief Connect interface to the mesh network
73+
* \return MESH_ERROR_NONE on success.
74+
* \return MESH_ERROR_PARAM in case of illegal parameters.
75+
* \return MESH_ERROR_MEMORY in case of memory error.
76+
* \return MESH_ERROR_STATE if interface is already connected to network.
77+
* \return MESH_ERROR_UNKNOWN in case of unspecified error.
78+
* */
79+
virtual mesh_error_t mesh_connect() = 0;
80+
81+
/**
82+
* \brief Disconnect interface from the mesh network
83+
* \return MESH_ERROR_NONE on success.
84+
* \return MESH_ERROR_UNKNOWN in case of error.
85+
* */
86+
virtual mesh_error_t mesh_disconnect() = 0;
87+
88+
/**
89+
* \brief Read own global IP address
90+
*
91+
* \param address is where the IP address will be copied
92+
* \param len is the length of the address buffer, must be at least 40 bytes
93+
* \return true if address is read successfully, false otherwise
94+
*/
95+
virtual bool getOwnIpAddress(char *address, int8_t len) = 0;
96+
97+
NanostackRfPhy *phy;
98+
/** Network interface ID */
99+
int8_t _network_interface_id;
100+
/** Registered device ID */
101+
int8_t _device_id;
102+
uint8_t eui64[8];
103+
char ip_addr_str[40];
104+
char mac_addr_str[24];
105+
Semaphore connect_semaphore;
106+
};
107+
108+
#endif /* MESHINTERFACENANOSTACK_H */

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/mbed-mesh-api/ThreadInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef THREADINTERFACE_H
1818
#define THREADINTERFACE_H
1919

20-
#include "NanostackInterface.h"
20+
#include "MeshInterfaceNanostack.h"
2121

2222
class ThreadInterface : public MeshInterfaceNanostack {
2323
public:

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/LoWPANNDInterface.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
#include "ns_trace.h"
88
#define TRACE_GROUP "nslp"
99

10-
#include "nanostack-event-loop/eventOS_scheduler.h"
11-
12-
#define nanostack_lock() eventOS_scheduler_mutex_wait()
13-
#define nanostack_unlock() eventOS_scheduler_mutex_release()
14-
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
15-
1610
int LoWPANNDInterface::connect()
1711
{
1812
nanostack_lock();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2016 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "MeshInterfaceNanostack.h"
18+
#include "NanostackInterface.h"
19+
#include "mesh_system.h"
20+
21+
MeshInterfaceNanostack::MeshInterfaceNanostack()
22+
: phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(),
23+
ip_addr_str(), mac_addr_str(), connect_semaphore(0)
24+
{
25+
// Nothing to do
26+
}
27+
28+
MeshInterfaceNanostack::MeshInterfaceNanostack(NanostackRfPhy *phy)
29+
: phy(phy), _network_interface_id(-1), _device_id(-1), connect_semaphore(0)
30+
{
31+
// Nothing to do
32+
}
33+
34+
nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
35+
{
36+
mesh_system_init();
37+
if (this->phy != NULL) {
38+
error("Phy already set");
39+
}
40+
this->phy = phy;
41+
return 0;
42+
}
43+
44+
void MeshInterfaceNanostack::mesh_network_handler(mesh_connection_status_t status)
45+
{
46+
nanostack_lock();
47+
48+
if (status == MESH_CONNECTED) {
49+
connect_semaphore.release();
50+
}
51+
52+
nanostack_unlock();
53+
}
54+
55+
nsapi_error_t MeshInterfaceNanostack::register_rf()
56+
{
57+
nanostack_lock();
58+
59+
_device_id = phy->rf_register();
60+
if (_device_id < 0) {
61+
nanostack_unlock();
62+
return -1;
63+
}
64+
// Read mac address after registering the device.
65+
phy->get_mac_address(eui64);
66+
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]);
67+
68+
nanostack_unlock();
69+
70+
return 0;
71+
}
72+
73+
NetworkStack * MeshInterfaceNanostack::get_stack()
74+
{
75+
return NanostackInterface::get_stack();
76+
}
77+
78+
const char *MeshInterfaceNanostack::get_ip_address()
79+
{
80+
nanostack_lock();
81+
82+
const char *ret = NULL;
83+
if (getOwnIpAddress(ip_addr_str, sizeof ip_addr_str)) {
84+
ret = ip_addr_str;
85+
}
86+
87+
nanostack_unlock();
88+
89+
return ret;
90+
}
91+
92+
const char *MeshInterfaceNanostack::get_mac_address()
93+
{
94+
return mac_addr_str;
95+
}

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/ThreadInterface.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
#include "ns_trace.h"
88
#define TRACE_GROUP "nsth"
99

10-
#include "nanostack-event-loop/eventOS_scheduler.h"
11-
12-
#define nanostack_lock() eventOS_scheduler_mutex_wait()
13-
#define nanostack_unlock() eventOS_scheduler_mutex_release()
14-
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
15-
1610
int ThreadInterface::connect()
1711
{
1812
nanostack_lock();

features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/include/mesh_system.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ void mesh_system_init(void);
4242
#ifdef __cplusplus
4343
}
4444
#endif
45+
46+
#include "nanostack-event-loop/eventOS_scheduler.h"
47+
48+
#define nanostack_lock() eventOS_scheduler_mutex_wait()
49+
#define nanostack_unlock() eventOS_scheduler_mutex_release()
50+
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
51+
4552
#endif /* __INCLUDE_MESH_SYSTEM__ */

features/nanostack/FEATURE_NANOSTACK/nanostack-interface/NanostackInterface.cpp

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
#include "ns_trace.h"
3333
#define TRACE_GROUP "nsif"
3434

35-
#include "nanostack-event-loop/eventOS_scheduler.h"
36-
37-
#define nanostack_lock() eventOS_scheduler_mutex_wait()
38-
#define nanostack_unlock() eventOS_scheduler_mutex_release()
39-
#define nanostack_assert_locked() //MBED_ASSERT(eventOS_scheduler_mutex_is_owner())
40-
4135
#define NS_INTERFACE_SOCKETS_MAX 16 //same as NanoStack SOCKET_MAX
4236
#define NANOSTACK_SOCKET_UDP 17 // same as nanostack SOCKET_UDP
4337
#define NANOSTACK_SOCKET_TCP 6 // same as nanostack SOCKET_TCP
@@ -445,82 +439,6 @@ void NanostackSocket::event_connnect_closed(socket_callback_t *sock_cb)
445439
close();
446440
}
447441

448-
MeshInterfaceNanostack::MeshInterfaceNanostack()
449-
: phy(NULL), _network_interface_id(-1), _device_id(-1), eui64(),
450-
ip_addr_str(), mac_addr_str(), connect_semaphore(0)
451-
{
452-
// Nothing to do
453-
}
454-
455-
MeshInterfaceNanostack::MeshInterfaceNanostack(NanostackRfPhy *phy)
456-
: phy(phy), _network_interface_id(-1), _device_id(-1), connect_semaphore(0)
457-
{
458-
// Nothing to do
459-
}
460-
461-
nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
462-
{
463-
mesh_system_init();
464-
if (this->phy != NULL) {
465-
error("Phy already set");
466-
}
467-
this->phy = phy;
468-
return 0;
469-
}
470-
471-
void MeshInterfaceNanostack::mesh_network_handler(mesh_connection_status_t status)
472-
{
473-
nanostack_lock();
474-
475-
if (status == MESH_CONNECTED) {
476-
connect_semaphore.release();
477-
}
478-
479-
nanostack_unlock();
480-
}
481-
482-
nsapi_error_t MeshInterfaceNanostack::register_rf()
483-
{
484-
nanostack_lock();
485-
486-
_device_id = phy->rf_register();
487-
if (_device_id < 0) {
488-
nanostack_unlock();
489-
return -1;
490-
}
491-
// Read mac address after registering the device.
492-
phy->get_mac_address(eui64);
493-
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]);
494-
495-
nanostack_unlock();
496-
497-
return 0;
498-
}
499-
500-
NetworkStack * MeshInterfaceNanostack::get_stack()
501-
{
502-
return NanostackInterface::get_stack();
503-
}
504-
505-
const char *MeshInterfaceNanostack::get_ip_address()
506-
{
507-
nanostack_lock();
508-
509-
const char *ret = NULL;
510-
if (getOwnIpAddress(ip_addr_str, sizeof ip_addr_str)) {
511-
ret = ip_addr_str;
512-
}
513-
514-
nanostack_unlock();
515-
516-
return ret;
517-
}
518-
519-
const char *MeshInterfaceNanostack::get_mac_address()
520-
{
521-
return mac_addr_str;
522-
}
523-
524442
NanostackInterface * NanostackInterface::_ns_interface;
525443

526444
NanostackInterface * NanostackInterface::get_stack()

0 commit comments

Comments
 (0)