Skip to content

Commit 3a238bd

Browse files
committed
Streamline Nanostack interface initialisation
Can unify quite a log of the setup here, and that would be useful to allow initialisation other than in "connect".
1 parent 3c25b96 commit 3a238bd

File tree

8 files changed

+47
-44
lines changed

8 files changed

+47
-44
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class LoWPANNDInterface : public MeshInterfaceNanostack {
3737
*/
3838
LoWPANNDInterface(NanostackRfPhy *phy) : MeshInterfaceNanostack(phy) { }
3939

40-
virtual int connect();
41-
virtual int disconnect();
4240
bool getRouterIpAddress(char *address, int8_t len);
41+
protected:
42+
Nanostack::LoWPANNDInterface *get_interface() const;
43+
virtual nsapi_error_t do_initialize();
4344
};
4445

4546
#endif

features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ class Nanostack::MeshInterface : public Nanostack::Interface {
6767

6868
class InterfaceNanostack : public virtual NetworkInterface {
6969
public:
70+
/** Start the interface
71+
*
72+
* @return 0 on success, negative error code on failure
73+
*/
74+
virtual nsapi_error_t connect();
75+
76+
/** Stop the interface
77+
*
78+
* @return 0 on success, negative error code on failure
79+
*/
80+
virtual nsapi_error_t disconnect();
81+
7082
/** Get the internally stored IP address
7183
/return IP address of the interface or null if not yet connected
7284
*/
@@ -109,6 +121,7 @@ class InterfaceNanostack : public virtual NetworkInterface {
109121
InterfaceNanostack();
110122
virtual Nanostack *get_stack(void);
111123
Nanostack::Interface *get_interface() const { return _interface; }
124+
virtual nsapi_error_t do_initialize() = 0;
112125

113126
Nanostack::Interface *_interface;
114127

features/nanostack/mbed-mesh-api/mbed-mesh-api/NanostackEthernetInterface.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,9 @@ class NanostackEthernetInterface : public InterfaceNanostack, public EthInterfac
4949

5050
nsapi_error_t initialize(NanostackEthernetPhy *phy);
5151

52-
/** Start the interface
53-
*
54-
* @return 0 on success, negative on failure
55-
*/
56-
virtual nsapi_error_t connect();
57-
58-
/** Stop the interface
59-
*
60-
* @return 0 on success, negative on failure
61-
*/
62-
virtual nsapi_error_t disconnect();
63-
6452
protected:
6553
Nanostack::EthernetInterface *get_interface() const { return static_cast<Nanostack::EthernetInterface *>(_interface); }
54+
virtual nsapi_error_t do_initialize();
6655

6756
};
6857

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ class ThreadInterface : public MeshInterfaceNanostack {
6262

6363
mesh_error_t device_pskd_set(const char *pskd);
6464

65-
virtual int connect();
66-
virtual int disconnect();
6765
protected:
6866
Nanostack::ThreadInterface *get_interface() const;
67+
virtual nsapi_error_t do_initialize();
6968
};
7069

7170
#endif // THREADINTERFACE_H

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class Nanostack::LoWPANNDInterface : public Nanostack::MeshInterface
2727
mesh_error_t mesh_disconnect();
2828
};
2929

30-
int LoWPANNDInterface::connect()
30+
Nanostack::LoWPANNDInterface *LoWPANNDInterface::get_interface() const
31+
{
32+
return static_cast<Nanostack::LoWPANNDInterface*>(_interface);
33+
}
34+
35+
nsapi_error_t LoWPANNDInterface::do_initialize()
3136
{
3237
if (!_interface) {
3338
_interface = new (nothrow) Nanostack::LoWPANNDInterface(*_phy);
@@ -36,9 +41,7 @@ int LoWPANNDInterface::connect()
3641
}
3742
_interface->attach(_connection_status_cb);
3843
}
39-
40-
return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking);
41-
44+
return NSAPI_ERROR_OK;
4245
}
4346

4447
nsapi_error_t Nanostack::LoWPANNDInterface::bringup(bool dhcp, const char *ip,
@@ -84,11 +87,6 @@ nsapi_error_t Nanostack::LoWPANNDInterface::bringup(bool dhcp, const char *ip,
8487

8588
}
8689

87-
int LoWPANNDInterface::disconnect()
88-
{
89-
return _interface->bringdown();
90-
}
91-
9290
nsapi_error_t Nanostack::LoWPANNDInterface::bringdown()
9391
{
9492
NanostackLockGuard lock;

features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ InterfaceNanostack::InterfaceNanostack()
8282
// Nothing to do
8383
}
8484

85+
int InterfaceNanostack::connect()
86+
{
87+
nsapi_error_t error = do_initialize();
88+
if (error) {
89+
return error;
90+
}
91+
92+
return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking);
93+
}
94+
95+
int InterfaceNanostack::disconnect()
96+
{
97+
if (!_interface) {
98+
return NSAPI_ERROR_NO_CONNECTION;
99+
}
100+
return _interface->bringdown();
101+
}
102+
85103
nsapi_error_t MeshInterfaceNanostack::initialize(NanostackRfPhy *phy)
86104
{
87105
if (_phy) {

features/nanostack/mbed-mesh-api/source/NanostackEthernetInterface.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,16 @@ nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip,
8787
return 0;
8888
}
8989

90-
int NanostackEthernetInterface::connect()
90+
nsapi_error_t NanostackEthernetInterface::do_initialize()
9191
{
9292
if (!_interface) {
9393
return NSAPI_ERROR_PARAMETER;
9494
}
95-
return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking);
95+
return NSAPI_ERROR_OK;
9696
}
9797

9898
nsapi_error_t Nanostack::EthernetInterface::bringdown()
9999
{
100100
enet_tasklet_disconnect();
101101
return 0;
102102
}
103-
104-
105-
int NanostackEthernetInterface::disconnect()
106-
{
107-
if (!_interface) {
108-
return NSAPI_ERROR_NO_CONNECTION;
109-
}
110-
return _interface->bringdown();
111-
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Nanostack::ThreadInterface *ThreadInterface::get_interface() const
7676
return static_cast<Nanostack::ThreadInterface*>(_interface);
7777
}
7878

79-
int ThreadInterface::connect()
79+
nsapi_error_t ThreadInterface::do_initialize()
8080
{
8181
if (!_interface) {
8282
_interface = new (nothrow) Nanostack::ThreadInterface(*_phy);
@@ -85,8 +85,7 @@ int ThreadInterface::connect()
8585
}
8686
_interface->attach(_connection_status_cb);
8787
}
88-
89-
return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking);
88+
return NSAPI_ERROR_OK;
9089
}
9190

9291
nsapi_error_t Nanostack::ThreadInterface::bringup(bool dhcp, const char *ip,
@@ -146,11 +145,6 @@ nsapi_error_t Nanostack::ThreadInterface::bringup(bool dhcp, const char *ip,
146145
return 0;
147146
}
148147

149-
int ThreadInterface::disconnect()
150-
{
151-
return _interface->bringdown();
152-
}
153-
154148
nsapi_error_t Nanostack::ThreadInterface::bringdown()
155149
{
156150
nanostack_lock();

0 commit comments

Comments
 (0)