Skip to content

Commit f0934fd

Browse files
committed
nsapi - Added optional API for static configuration of network interfaces
This accomplishes two things: 1. Provides a simple route for adding static IP address support to the base NetworkInterface 2. Provides a simple route for adding configurability to network interfaces and unifies the network interface to consistent connect call, allowing network interfaces to be passed around abstractly NetworkInterface - set_network - set_dhcp WiFiInterface - set_credentials CellularInterface - set_credentials
1 parent e9c556d commit f0934fd

File tree

8 files changed

+320
-49
lines changed

8 files changed

+320
-49
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Socket
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may 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,
12+
* WITHOUT 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 "network-socket/CellularInterface.h"
18+
#include <stdlib.h>
19+
#include <string.h>
20+
21+
22+
CellularInterface::CellularInterface()
23+
: _apn(0), _user(0), _pass(0)
24+
{
25+
}
26+
27+
CellularInterface::~CellularInterface()
28+
{
29+
free(_apn);
30+
free(_user);
31+
free(_pass);
32+
}
33+
34+
int CellularInterface::set_credentials(const char *apn, const char *user, const char *pass)
35+
{
36+
free(_apn);
37+
_apn = 0;
38+
free(_user);
39+
_user = 0;
40+
free(_pass);
41+
_pass = 0;
42+
43+
if (apn) {
44+
_apn = (char*)malloc(strlen(apn)+1);
45+
if (!_apn) {
46+
return NSAPI_ERROR_NO_MEMORY;
47+
}
48+
49+
strcpy(_apn, apn);
50+
}
51+
52+
if (user) {
53+
_user = (char*)malloc(strlen(user)+1);
54+
if (!_user) {
55+
return NSAPI_ERROR_NO_MEMORY;
56+
}
57+
58+
strcpy(_user, user);
59+
}
60+
61+
if (pass) {
62+
_pass = (char*)malloc(strlen(pass)+1);
63+
if (!_pass) {
64+
return NSAPI_ERROR_NO_MEMORY;
65+
}
66+
67+
strcpy(_pass, pass);
68+
}
69+
70+
return 0;
71+
}
72+
73+
int CellularInterface::connect()
74+
{
75+
return connect(_apn, _user, _pass);
76+
}
77+

features/net/network-socket/CellularInterface.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,46 @@
2727
class CellularInterface : public NetworkInterface
2828
{
2929
public:
30+
/** CellularInterface lifetime
31+
*/
32+
CellularInterface();
33+
virtual ~CellularInterface();
34+
35+
/** Set the cellular network APN and credentials
36+
*
37+
* @param apn Optional name of the network to connect to
38+
* @param user Optional username for the APN
39+
* @param pass Optional password fot the APN
40+
*/
41+
virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0);
42+
3043
/** Start the interface
3144
*
3245
* @param apn Optional name of the network to connect to
3346
* @param username Optional username for your APN
3447
* @param password Optional password for your APN
3548
* @return 0 on success, negative error code on failure
3649
*/
37-
virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0) = 0;
50+
virtual int connect(const char *apn, const char *username = 0, const char *password = 0) = 0;
51+
52+
/** Start the interface
53+
*
54+
* Attempts to connect to a cellular network based on supplied credentials
55+
*
56+
* @return 0 on success, negative error code on failure
57+
*/
58+
virtual int connect();
3859

3960
/** Stop the interface
4061
*
4162
* @return 0 on success, negative error code on failure
4263
*/
4364
virtual int disconnect() = 0;
4465

45-
/** Get the local MAC address
46-
*
47-
* @return Null-terminated representation of the local MAC address
48-
*/
49-
virtual const char *get_mac_address() = 0;
66+
private:
67+
char *_apn;
68+
char *_user;
69+
char *_pass;
5070
};
5171

5272

features/net/network-socket/EthInterface.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@
2626
*/
2727
class EthInterface : public NetworkInterface
2828
{
29-
public:
30-
/** Start the interface
31-
*
32-
* @return 0 on success, negative error code on failure
33-
*/
34-
virtual int connect() = 0;
35-
36-
/** Stop the interface
37-
*
38-
* @return 0 on success, negative error code on failure
39-
*/
40-
virtual int disconnect() = 0;
41-
42-
/** Get the local MAC address
43-
*
44-
* @return Null-terminated representation of the local MAC address
45-
*/
46-
virtual const char *get_mac_address() = 0;
4729
};
4830

4931

features/net/network-socket/MeshInterface.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,6 @@
2626
*/
2727
class MeshInterface : public NetworkInterface
2828
{
29-
public:
30-
/** Start the interface
31-
*
32-
* @return 0 on success, negative on failure
33-
*/
34-
virtual int connect() = 0;
35-
36-
/** Stop the interface
37-
*
38-
* @return 0 on success, negative on failure
39-
*/
40-
virtual int disconnect() = 0;
41-
42-
/** Get the local MAC address
43-
*
44-
* @return Null-terminated representation of the local MAC address
45-
*/
46-
virtual const char *get_mac_address() = 0;
4729
};
4830

4931

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Socket
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may 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,
12+
* WITHOUT 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 "network-socket/NetworkInterface.h"
18+
#include <string.h>
19+
20+
21+
const char *NetworkInterface::get_mac_address()
22+
{
23+
return 0;
24+
}
25+
26+
const char *NetworkInterface::get_ip_address()
27+
{
28+
return 0;
29+
}
30+
31+
const char *NetworkInterface::get_netmask()
32+
{
33+
return 0;
34+
}
35+
36+
const char *NetworkInterface::get_gateway()
37+
{
38+
return 0;
39+
}
40+
41+
int NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
42+
{
43+
return NSAPI_ERROR_UNSUPPORTED;
44+
}
45+
46+
int NetworkInterface::set_dhcp(bool dhcp)
47+
{
48+
if (!dhcp) {
49+
return NSAPI_ERROR_UNSUPPORTED;
50+
} else {
51+
return 0;
52+
}
53+
}
54+

features/net/network-socket/NetworkInterface.h

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef NETWORK_INTERFACE_H
1818
#define NETWORK_INTERFACE_H
1919

20+
#include "network-socket/nsapi_types.h"
21+
2022
// Predeclared class
2123
class NetworkStack;
2224

@@ -29,12 +31,73 @@ class NetworkInterface {
2931
public:
3032
virtual ~NetworkInterface() {};
3133

34+
/** Get the local MAC address
35+
*
36+
* Provided MAC address is intended for info or debug purposes and
37+
* may not be provided if the underlying network interface does not
38+
* provide a MAC address
39+
*
40+
* @return Null-terminated representation of the local MAC address
41+
* or null if no MAC address is available
42+
*/
43+
virtual const char *get_mac_address();
44+
3245
/** Get the local IP address
3346
*
3447
* @return Null-terminated representation of the local IP address
35-
* or null if not yet connected
48+
* or null if no IP address has been recieved
49+
*/
50+
virtual const char *get_ip_address();
51+
52+
/** Get the local network mask
53+
*
54+
* @return Null-terminated representation of the local network mask
55+
* or null if no network mask has been recieved
56+
*/
57+
virtual const char *get_netmask();
58+
59+
/** Get the local gateway
60+
*
61+
* @return Null-terminated representation of the local gateway
62+
* or null if no network mask has been recieved
63+
*/
64+
virtual const char *get_gateway();
65+
66+
/** Set a static IP address
67+
*
68+
* Configures this network interface to use a static IP address.
69+
* Implicitly disables DHCP, which can be enabled in set_dhcp.
70+
* Requires that the network is disconnected.
71+
*
72+
* @param address Null-terminated representation of the local IP address
73+
* @param netmask Null-terminated representation of the local network mask
74+
* @param gateway Null-terminated representation of the local gateway
75+
* @return 0 on success, negative error code on failure
76+
*/
77+
virtual int set_network(const char *ip_address, const char *netmask, const char *gateway);
78+
79+
/** Enable or disable DHCP on the network
80+
*
81+
* Enables DHCP on connecting the network. Defaults to enabled unless
82+
* a static IP address has been assigned. Requires that the network is
83+
* disconnected.
84+
*
85+
* @param dhcp True to enable DHCP
86+
* @return 0 on success, negative error code on failure
87+
*/
88+
virtual int set_dhcp(bool dhcp);
89+
90+
/** Start the interface
91+
*
92+
* @return 0 on success, negative error code on failure
93+
*/
94+
virtual int connect() = 0;
95+
96+
/** Stop the interface
97+
*
98+
* @return 0 on success, negative error code on failure
3699
*/
37-
virtual const char *get_ip_address() = 0;
100+
virtual int disconnect() = 0;
38101

39102
protected:
40103
friend class Socket;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Socket
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may 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,
12+
* WITHOUT 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 "network-socket/WiFiInterface.h"
18+
#include <stdlib.h>
19+
#include <string.h>
20+
21+
22+
WiFiInterface::WiFiInterface()
23+
: _ssid(0), _pass(0), _security(NSAPI_SECURITY_NONE)
24+
{
25+
}
26+
27+
WiFiInterface::~WiFiInterface()
28+
{
29+
free(_ssid);
30+
free(_pass);
31+
}
32+
33+
int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
34+
{
35+
free(_ssid);
36+
_ssid = 0;
37+
free(_pass);
38+
_pass = 0;
39+
40+
if (ssid) {
41+
_ssid = (char*)malloc(strlen(ssid)+1);
42+
if (!_ssid) {
43+
return NSAPI_ERROR_NO_MEMORY;
44+
}
45+
46+
strcpy(_ssid, ssid);
47+
}
48+
49+
if (pass) {
50+
_pass = (char*)malloc(strlen(pass)+1);
51+
if (!_pass) {
52+
return NSAPI_ERROR_NO_MEMORY;
53+
}
54+
55+
strcpy(_pass, pass);
56+
}
57+
58+
_security = security;
59+
60+
return 0;
61+
}
62+
63+
int WiFiInterface::connect()
64+
{
65+
if (!_ssid || !_pass) {
66+
return NSAPI_ERROR_PARAMETER;
67+
}
68+
69+
return connect(_ssid, _pass, _security);
70+
}
71+

0 commit comments

Comments
 (0)