Skip to content

Add SocketAddress-based API and deprecate string-based one #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions ESP32Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,24 @@ ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) :
_esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb));
}

nsapi_error_t ESP32Interface::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
{
_dhcp = false;
_ip_address = ip_address;
_netmask = netmask;
_gateway = gateway;

return NSAPI_ERROR_OK;
}

nsapi_error_t ESP32Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;

strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
_ip_address[sizeof(_ip_address) - 1] = '\0';
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
_netmask[sizeof(_netmask) - 1] = '\0';
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
_gateway[sizeof(_gateway) - 1] = '\0';
// Don't check return values, so user can clear the addresses by passing empty strings.
_ip_address.set_ip_address(ip_address);
_netmask.set_ip_address(netmask);
_gateway.set_ip_address(gateway);

return NSAPI_ERROR_OK;
}
Expand Down Expand Up @@ -114,7 +122,7 @@ int ESP32Interface::connect()
}

if (!_dhcp) {
if (!_esp->set_network(_ip_address, _netmask, _gateway)) {
if (!_esp->set_network(_ip_address.get_ip_address(), _netmask.get_ip_address(), _gateway.get_ip_address())) {
return NSAPI_ERROR_DEVICE_ERROR;
}
}
Expand Down Expand Up @@ -204,6 +212,14 @@ int ESP32Interface::disconnect()
return NSAPI_ERROR_OK;
}

nsapi_error_t ESP32Interface::get_ip_address(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getIPAddress())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP32Interface::get_ip_address()
{
return _esp->getIPAddress();
Expand All @@ -214,11 +230,27 @@ const char *ESP32Interface::get_mac_address()
return _esp->getMACAddress();
}

nsapi_error_t ESP32Interface::get_gateway(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getGateway())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP32Interface::get_gateway()
{
return _esp->getGateway();
}

nsapi_error_t ESP32Interface::get_netmask(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getNetmask())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP32Interface::get_netmask()
{
return _esp->getNetmask();
Expand Down
46 changes: 35 additions & 11 deletions ESP32Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
* Implicitly disables DHCP, which can be enabled in set_dhcp.
* Requires that the network is disconnected.
*
* @param ip_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
* @param ip_address SocketAddress representation of the local IP address
* @param netmask SocketAddress representation of the local network mask
* @param gateway SocketAddress representation of the local gateway
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_network(
const SocketAddress &ip_address, const SocketAddress &netmask,
const SocketAddress &gateway) override;

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);

Expand Down Expand Up @@ -123,8 +128,15 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
virtual int disconnect();

/** Get the internally stored IP address
* @return IP address of the interface or null if not yet connected
* @param sockAddr SocketAddress pointer to store the local IP address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_ip_address(SocketAddress *sockAddr);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_ip_address();

/** Get the internally stored MAC address
Expand All @@ -134,16 +146,28 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface

/** Get the local gateway
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
* @param sockAddr SocketAddress representation of gateway address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_gateway(SocketAddress *sockAddr);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_gateway();

/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
* @param sockAddr SocketAddress representation of netmask
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_netmask(SocketAddress *sockAddr);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_netmask();

/** Gets the current radio signal strength for active connection
Expand Down Expand Up @@ -221,9 +245,9 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
char _ap_pass[64]; /* The longest allowed passphrase */
nsapi_security_t _ap_sec;
char _ip_address[NSAPI_IPv6_SIZE];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
SocketAddress _ip_address;
SocketAddress _netmask;
SocketAddress _gateway;
nsapi_connection_status_t _connection_status;
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;

Expand Down
46 changes: 39 additions & 7 deletions ESP32InterfaceAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,23 @@ ESP32InterfaceAP::ESP32InterfaceAP(PinName tx, PinName rx, bool debug) :
{
}

nsapi_error_t ESP32InterfaceAP::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
{
_dhcp = false;
_ip_address = ip_address;
_netmask = netmask;
_gateway = gateway;

return NSAPI_ERROR_OK;
}

nsapi_error_t ESP32InterfaceAP::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;

strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address));
_ip_address[sizeof(_ip_address) - 1] = '\0';
strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask));
_netmask[sizeof(_netmask) - 1] = '\0';
strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway));
_gateway[sizeof(_gateway) - 1] = '\0';
_ip_address.set_ip_address(ip_address);
_netmask.set_ip_address(netmask);
_gateway.set_ip_address(gateway);

return NSAPI_ERROR_OK;
}
Expand Down Expand Up @@ -115,7 +122,7 @@ int ESP32InterfaceAP::connect()
}

if (!_dhcp) {
if (!_esp->set_network_ap(_ip_address, _netmask, _gateway)) {
if (!_esp->set_network_ap(_ip_address.get_ip_address(), _netmask.get_ip_address(), _gateway.get_ip_address())) {
return NSAPI_ERROR_DEVICE_ERROR;
}
}
Expand Down Expand Up @@ -179,6 +186,14 @@ int ESP32InterfaceAP::disconnect()
return NSAPI_ERROR_OK;
}

nsapi_error_t ESP32InterfaceAP::get_ip_address(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getIPAddress_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP32InterfaceAP::get_ip_address()
{
return _esp->getIPAddress_ap();
Expand All @@ -189,11 +204,28 @@ const char *ESP32InterfaceAP::get_mac_address()
return _esp->getMACAddress_ap();
}

nsapi_error_t ESP32InterfaceAP::get_gateway(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getGateway_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}

const char *ESP32InterfaceAP::get_gateway()
{
return _esp->getGateway_ap();
}

nsapi_error_t ESP32InterfaceAP::get_netmask(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getNetmask_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}


const char *ESP32InterfaceAP::get_netmask()
{
return _esp->getNetmask_ap();
Expand Down
47 changes: 36 additions & 11 deletions ESP32InterfaceAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
* Implicitly disables DHCP, which can be enabled in set_dhcp.
* Requires that the network is disconnected.
*
* @param ip_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
* @param ip_address SocketAddress representation of the local IP address
* @param netmask SocketAddress representation of the local network mask
* @param gateway SocketAddress representation of the local gateway
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_network(
const SocketAddress &ip_address, const SocketAddress &netmask,
const SocketAddress &gateway) override;

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);

Expand Down Expand Up @@ -124,8 +129,16 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
virtual int disconnect();

/** Get the internally stored IP address
* @return IP address of the interface or null if not yet connected
*
* @param address SocketAddress pointer to store the local IP address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_ip_address(SocketAddress *address);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_ip_address();

/** Get the internally stored MAC address
Expand All @@ -135,16 +148,28 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface

/** Get the local gateway
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
* @param address SocketAddress representation of gateway address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_gateway(SocketAddress *address);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_gateway();

/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
* @param address SocketAddress representation of netmask
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_netmask(SocketAddress *address);

MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_netmask();

/** Gets the current radio signal strength for active connection
Expand Down Expand Up @@ -224,9 +249,9 @@ class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
char _own_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
char _own_pass[64]; /* The longest allowed passphrase */
nsapi_security_t _own_sec;
char _ip_address[NSAPI_IPv6_SIZE];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
SocketAddress _ip_address;
SocketAddress _netmask;
SocketAddress _gateway;
nsapi_connection_status_t _connection_status;
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
};
Expand Down