From f0934fd0f1fbfc9a813d71e4faacd7aa625295fd Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 8 Sep 2016 13:44:11 -0500 Subject: [PATCH] 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 --- .../net/network-socket/CellularInterface.cpp | 77 +++++++++++++++++++ .../net/network-socket/CellularInterface.h | 32 ++++++-- features/net/network-socket/EthInterface.h | 18 ----- features/net/network-socket/MeshInterface.h | 18 ----- .../net/network-socket/NetworkInterface.cpp | 54 +++++++++++++ .../net/network-socket/NetworkInterface.h | 67 +++++++++++++++- features/net/network-socket/WiFiInterface.cpp | 71 +++++++++++++++++ features/net/network-socket/WiFiInterface.h | 32 ++++++-- 8 files changed, 320 insertions(+), 49 deletions(-) create mode 100644 features/net/network-socket/CellularInterface.cpp create mode 100644 features/net/network-socket/NetworkInterface.cpp create mode 100644 features/net/network-socket/WiFiInterface.cpp diff --git a/features/net/network-socket/CellularInterface.cpp b/features/net/network-socket/CellularInterface.cpp new file mode 100644 index 0000000000..206eba032f --- /dev/null +++ b/features/net/network-socket/CellularInterface.cpp @@ -0,0 +1,77 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * 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 "network-socket/CellularInterface.h" +#include +#include + + +CellularInterface::CellularInterface() + : _apn(0), _user(0), _pass(0) +{ +} + +CellularInterface::~CellularInterface() +{ + free(_apn); + free(_user); + free(_pass); +} + +int CellularInterface::set_credentials(const char *apn, const char *user, const char *pass) +{ + free(_apn); + _apn = 0; + free(_user); + _user = 0; + free(_pass); + _pass = 0; + + if (apn) { + _apn = (char*)malloc(strlen(apn)+1); + if (!_apn) { + return NSAPI_ERROR_NO_MEMORY; + } + + strcpy(_apn, apn); + } + + if (user) { + _user = (char*)malloc(strlen(user)+1); + if (!_user) { + return NSAPI_ERROR_NO_MEMORY; + } + + strcpy(_user, user); + } + + if (pass) { + _pass = (char*)malloc(strlen(pass)+1); + if (!_pass) { + return NSAPI_ERROR_NO_MEMORY; + } + + strcpy(_pass, pass); + } + + return 0; +} + +int CellularInterface::connect() +{ + return connect(_apn, _user, _pass); +} + diff --git a/features/net/network-socket/CellularInterface.h b/features/net/network-socket/CellularInterface.h index 45364fd562..2eaffdf71a 100644 --- a/features/net/network-socket/CellularInterface.h +++ b/features/net/network-socket/CellularInterface.h @@ -27,6 +27,19 @@ class CellularInterface : public NetworkInterface { public: + /** CellularInterface lifetime + */ + CellularInterface(); + virtual ~CellularInterface(); + + /** Set the cellular network APN and credentials + * + * @param apn Optional name of the network to connect to + * @param user Optional username for the APN + * @param pass Optional password fot the APN + */ + virtual int set_credentials(const char *apn, const char *user = 0, const char *pass = 0); + /** Start the interface * * @param apn Optional name of the network to connect to @@ -34,7 +47,15 @@ public: * @param password Optional password for your APN * @return 0 on success, negative error code on failure */ - virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0) = 0; + virtual int connect(const char *apn, const char *username = 0, const char *password = 0) = 0; + + /** Start the interface + * + * Attempts to connect to a cellular network based on supplied credentials + * + * @return 0 on success, negative error code on failure + */ + virtual int connect(); /** Stop the interface * @@ -42,11 +63,10 @@ public: */ virtual int disconnect() = 0; - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address - */ - virtual const char *get_mac_address() = 0; +private: + char *_apn; + char *_user; + char *_pass; }; diff --git a/features/net/network-socket/EthInterface.h b/features/net/network-socket/EthInterface.h index 7a094e7c4c..0d3e597a0c 100644 --- a/features/net/network-socket/EthInterface.h +++ b/features/net/network-socket/EthInterface.h @@ -26,24 +26,6 @@ */ class EthInterface : public NetworkInterface { -public: - /** Start the interface - * - * @return 0 on success, negative error code on failure - */ - virtual int connect() = 0; - - /** Stop the interface - * - * @return 0 on success, negative error code on failure - */ - virtual int disconnect() = 0; - - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address - */ - virtual const char *get_mac_address() = 0; }; diff --git a/features/net/network-socket/MeshInterface.h b/features/net/network-socket/MeshInterface.h index 01e737871d..0e25b06f27 100644 --- a/features/net/network-socket/MeshInterface.h +++ b/features/net/network-socket/MeshInterface.h @@ -26,24 +26,6 @@ */ class MeshInterface : public NetworkInterface { -public: - /** Start the interface - * - * @return 0 on success, negative on failure - */ - virtual int connect() = 0; - - /** Stop the interface - * - * @return 0 on success, negative on failure - */ - virtual int disconnect() = 0; - - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address - */ - virtual const char *get_mac_address() = 0; }; diff --git a/features/net/network-socket/NetworkInterface.cpp b/features/net/network-socket/NetworkInterface.cpp new file mode 100644 index 0000000000..4effce5c09 --- /dev/null +++ b/features/net/network-socket/NetworkInterface.cpp @@ -0,0 +1,54 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * 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 "network-socket/NetworkInterface.h" +#include + + +const char *NetworkInterface::get_mac_address() +{ + return 0; +} + +const char *NetworkInterface::get_ip_address() +{ + return 0; +} + +const char *NetworkInterface::get_netmask() +{ + return 0; +} + +const char *NetworkInterface::get_gateway() +{ + return 0; +} + +int NetworkInterface::set_network(const char *ip_address, const char *netmask, const char *gateway) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +int NetworkInterface::set_dhcp(bool dhcp) +{ + if (!dhcp) { + return NSAPI_ERROR_UNSUPPORTED; + } else { + return 0; + } +} + diff --git a/features/net/network-socket/NetworkInterface.h b/features/net/network-socket/NetworkInterface.h index 4defc0c5ec..0757dea130 100644 --- a/features/net/network-socket/NetworkInterface.h +++ b/features/net/network-socket/NetworkInterface.h @@ -17,6 +17,8 @@ #ifndef NETWORK_INTERFACE_H #define NETWORK_INTERFACE_H +#include "network-socket/nsapi_types.h" + // Predeclared class class NetworkStack; @@ -29,12 +31,73 @@ class NetworkInterface { public: virtual ~NetworkInterface() {}; + /** Get the local MAC address + * + * Provided MAC address is intended for info or debug purposes and + * may not be provided if the underlying network interface does not + * provide a MAC address + * + * @return Null-terminated representation of the local MAC address + * or null if no MAC address is available + */ + virtual const char *get_mac_address(); + /** Get the local IP address * * @return Null-terminated representation of the local IP address - * or null if not yet connected + * or null if no IP address has been recieved */ - virtual const char *get_ip_address() = 0; + virtual const char *get_ip_address(); + + /** Get the local network mask + * + * @return Null-terminated representation of the local network mask + * or null if no network mask has been recieved + */ + virtual const char *get_netmask(); + + /** Get the local gateway + * + * @return Null-terminated representation of the local gateway + * or null if no network mask has been recieved + */ + virtual const char *get_gateway(); + + /** Set a static IP address + * + * Configures this network interface to use a static IP address. + * Implicitly disables DHCP, which can be enabled in set_dhcp. + * Requires that the network is disconnected. + * + * @param address Null-terminated representation of the local IP address + * @param netmask Null-terminated representation of the local network mask + * @param gateway Null-terminated representation of the local gateway + * @return 0 on success, negative error code on failure + */ + virtual int set_network(const char *ip_address, const char *netmask, const char *gateway); + + /** Enable or disable DHCP on the network + * + * Enables DHCP on connecting the network. Defaults to enabled unless + * a static IP address has been assigned. Requires that the network is + * disconnected. + * + * @param dhcp True to enable DHCP + * @return 0 on success, negative error code on failure + */ + virtual int set_dhcp(bool dhcp); + + /** Start the interface + * + * @return 0 on success, negative error code on failure + */ + virtual int connect() = 0; + + /** Stop the interface + * + * @return 0 on success, negative error code on failure + */ + virtual int disconnect() = 0; protected: friend class Socket; diff --git a/features/net/network-socket/WiFiInterface.cpp b/features/net/network-socket/WiFiInterface.cpp new file mode 100644 index 0000000000..128220845d --- /dev/null +++ b/features/net/network-socket/WiFiInterface.cpp @@ -0,0 +1,71 @@ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * 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 "network-socket/WiFiInterface.h" +#include +#include + + +WiFiInterface::WiFiInterface() + : _ssid(0), _pass(0), _security(NSAPI_SECURITY_NONE) +{ +} + +WiFiInterface::~WiFiInterface() +{ + free(_ssid); + free(_pass); +} + +int WiFiInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) +{ + free(_ssid); + _ssid = 0; + free(_pass); + _pass = 0; + + if (ssid) { + _ssid = (char*)malloc(strlen(ssid)+1); + if (!_ssid) { + return NSAPI_ERROR_NO_MEMORY; + } + + strcpy(_ssid, ssid); + } + + if (pass) { + _pass = (char*)malloc(strlen(pass)+1); + if (!_pass) { + return NSAPI_ERROR_NO_MEMORY; + } + + strcpy(_pass, pass); + } + + _security = security; + + return 0; +} + +int WiFiInterface::connect() +{ + if (!_ssid || !_pass) { + return NSAPI_ERROR_PARAMETER; + } + + return connect(_ssid, _pass, _security); +} + diff --git a/features/net/network-socket/WiFiInterface.h b/features/net/network-socket/WiFiInterface.h index dada562aec..5a5ac88fce 100644 --- a/features/net/network-socket/WiFiInterface.h +++ b/features/net/network-socket/WiFiInterface.h @@ -41,6 +41,20 @@ enum nsapi_security_t { class WiFiInterface: public NetworkInterface { public: + /** WiFiInterface lifetime + */ + WiFiInterface(); + virtual ~WiFiInterface(); + + /** Set the WiFi network credentials + * + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption for connection + * (defaults to NSAPI_SECURITY_NONE) + */ + virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); + /** Start the interface * * Attempts to connect to a WiFi network. If passphrase is invalid, @@ -53,17 +67,25 @@ public: */ virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0; + /** Start the interface + * + * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. + * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. + * + * @return 0 on success, negative error code on failure + */ + virtual int connect(); + /** Stop the interface * * @return 0 on success, negative error code on failure */ virtual int disconnect() = 0; - /** Get the local MAC address - * - * @return Null-terminated representation of the local MAC address - */ - virtual const char *get_mac_address() = 0; +private: + char *_ssid; + char *_pass; + nsapi_security_t _security; };