Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
27d36d8
Fix MacOS (case) filename clashes
manchoz Jul 6, 2021
357dba7
Add examples
manchoz Jul 6, 2021
c39cfee
Add helpers begin methods
manchoz Feb 1, 2021
05a1b80
Fix WebClient example
manchoz Feb 1, 2021
540e0ab
Add Static IP support
manchoz Jul 5, 2021
a5e512a
Manage Client status
manchoz Jul 5, 2021
d1501d0
Add support for Static Network configuration
manchoz Jul 5, 2021
b04124a
Update examples
manchoz Jul 5, 2021
817b47a
Add forgotten mac parameter
manchoz Jul 5, 2021
cf184fd
Add checks on socket validity before operations
manchoz Jul 6, 2021
9172c17
Manage sockets already connected
manchoz Jul 6, 2021
4aad72b
Add client socket management
manchoz Jul 6, 2021
5f802c1
WiFi: Cleanup libraries and error handling
facchinm Jul 7, 2021
4ded56c
WiFi: make library compliant with Arduino APIs
facchinm Jul 7, 2021
ec4109d
Initial: start moving WiFi generic classes into a standalone library
facchinm Jul 8, 2021
e0e8647
WiFi: port Client and Server to the new wrapper class
facchinm Jul 8, 2021
02b1239
TEMP: MbedClient: add horrible Client "constructor"
facchinm Jul 8, 2021
73cbc6b
WiFi: port UDP to new MbedUdp class
facchinm Jul 8, 2021
175d09f
Ethernet: port library to SocketWrapper
facchinm Jul 8, 2021
37edb74
MbedClient: fix copy constructor
facchinm Jul 8, 2021
974f4c2
Network: cleanup libraries and licenses
facchinm Jul 9, 2021
d4bb9d8
Network: apply clang-format on libraries
facchinm Jul 9, 2021
4340946
MbedUdp: make socket non blocking
facchinm Jul 9, 2021
123694b
SocketWrapper: fix download() API visibility
facchinm Jul 9, 2021
bc92047
MbedUDP: take into account write() errors
facchinm Jul 9, 2021
7fd7c9a
Remove protected leftovers
manchoz Jul 13, 2021
d9fa625
Add EthernetServer::available()
manchoz Jul 13, 2021
8c7962f
Add ::dnsServerIP
manchoz Jul 13, 2021
a175f35
Unlock mutexes before return
manchoz Jul 13, 2021
15ca271
Fix Typo
manchoz Jul 13, 2021
d3e8ee3
Manage copy and assignement
manchoz Jul 13, 2021
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
Prev Previous commit
Next Next commit
WiFi: port Client and Server to the new wrapper class
  • Loading branch information
facchinm authored and manchoz committed Jul 14, 2021
commit e0e8647d3a97ae6bce03f6b48fa0890199cc90ff
4 changes: 0 additions & 4 deletions libraries/SocketWrapper/src/MbedServer.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "MbedServer.h"
#include "MbedClient.h"

arduino::MbedServer::MbedServer(uint16_t port) {
_port = port;
}

uint8_t arduino::MbedServer::status() {
return 0;
}
Expand Down
10 changes: 6 additions & 4 deletions libraries/SocketWrapper/src/MbedServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ namespace arduino {
class MbedClient;

class MbedServer : public arduino::Server {
private:
uint16_t _port;
TCPSocket* sock = nullptr;

protected:
virtual NetworkInterface *getNetwork() = 0;
TCPSocket* sock = nullptr;
uint16_t _port;

public:
MbedServer(uint16_t);
MbedServer(uint16_t port) : _port(port) {};

virtual ~MbedServer() {
if (sock) {
delete sock;
Expand All @@ -51,6 +51,8 @@ class MbedServer : public arduino::Server {
virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();

//virtual MbedClient available(uint8_t* status) = 0;

using Print::write;

friend class MbedSocketClass;
Expand Down
23 changes: 23 additions & 0 deletions libraries/SocketWrapper/src/SocketHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ void body_callback(const char* data, uint32_t data_len)
fwrite(data, 1, data_len, target);
}

uint8_t* arduino::MbedSocketClass::macAddress(uint8_t* mac) {
const char *mac_str = getNetwork()->get_mac_address();
for( int b = 0; b < 6; b++ )
{
uint32_t tmp;
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
mac[5-b] = (uint8_t)tmp ;
}
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
return mac;
}

int arduino::MbedSocketClass::hostByName(const char* aHostname, IPAddress& aResult){
SocketAddress socketAddress = SocketAddress();
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
nsapi_addr_t address = socketAddress.get_addr();
aResult[0] = address.bytes[0];
aResult[1] = address.bytes[1];
aResult[2] = address.bytes[2];
aResult[3] = address.bytes[3];
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
}

arduino::IPAddress arduino::MbedSocketClass::localIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
Expand Down
4 changes: 4 additions & 0 deletions libraries/SocketWrapper/src/SocketHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class MbedSocketClass {

int download(char* url, const char* target, bool const is_https = false);

int hostByName(const char* aHostname, IPAddress& aResult);

uint8_t* macAddress(uint8_t* mac);

friend class MbedUDP;
friend class MbedServer;
friend class MbedClient;
Expand Down
91 changes: 0 additions & 91 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ bool arduino::WiFiClass::isVisible(const char* ssid) {
return false;
}

arduino::IPAddress arduino::WiFiClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
nsapi_addr_t address = socketAddress.get_addr();
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
}

SocketAddress arduino::WiFiClass::socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port) {
nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}};
return SocketAddress(convertedIP, port);
}

int arduino::WiFiClass::begin(const char* ssid, const char *passphrase) {
if (wifi_if == nullptr) {
//Q: What is the callback for?
Expand Down Expand Up @@ -124,43 +114,6 @@ int arduino::WiFiClass::disconnect() {
return _currentNetworkStatus;
}

void arduino::WiFiClass::config(arduino::IPAddress local_ip){
nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}};
_ip = SocketAddress(convertedIP);
}

void arduino::WiFiClass::config(const char *local_ip){
_ip = SocketAddress(local_ip);
}

void arduino::WiFiClass::config(IPAddress local_ip, IPAddress dns_server){
config(local_ip);
setDNS(dns_server);
}

void arduino::WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
config(local_ip, dns_server);
nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}};
_gateway = SocketAddress(convertedGatewayIP);
}

void arduino::WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
config(local_ip, dns_server, gateway);
nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}};
_netmask = SocketAddress(convertedSubnetMask);
}

void arduino::WiFiClass::setDNS(IPAddress dns_server1){
nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}};
_dnsServer1 = SocketAddress(convertedDNSServer);
}

void arduino::WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){
setDNS(dns_server1);
nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}};
_dnsServer2 = SocketAddress(convertedDNSServer2);
}

char* arduino::WiFiClass::SSID() {
return _ssid;
}
Expand Down Expand Up @@ -246,17 +199,6 @@ uint8_t arduino::WiFiClass::status() {
return _currentNetworkStatus;
}

int arduino::WiFiClass::hostByName(const char* aHostname, IPAddress& aResult){
SocketAddress socketAddress = SocketAddress();
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
nsapi_addr_t address = socketAddress.get_addr();
aResult[0] = address.bytes[0];
aResult[1] = address.bytes[1];
aResult[2] = address.bytes[2];
aResult[3] = address.bytes[3];
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
}

uint8_t arduino::WiFiClass::encryptionType() {
return sec2enum(ap_list[connected_ap].get_security());
}
Expand All @@ -269,39 +211,6 @@ uint8_t* arduino::WiFiClass::BSSID(unsigned char* bssid) {
return bssid;
}

uint8_t* arduino::WiFiClass::macAddress(uint8_t* mac) {
const char *mac_str = getNetwork()->get_mac_address();
for( int b = 0; b < 6; b++ )
{
uint32_t tmp;
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
mac[5-b] = (uint8_t)tmp ;
}
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
return mac;
}

arduino::IPAddress arduino::WiFiClass::localIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_ip_address(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::WiFiClass::subnetMask() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_netmask(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::WiFiClass::gatewayIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_gateway(&ip);
return ipAddressFromSocketAddress(ip);
}

NetworkInterface *arduino::WiFiClass::getNetwork() {
if (_softAP != nullptr) {
return _softAP;
Expand Down
105 changes: 6 additions & 99 deletions libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ extern "C" {
#include "utility/wl_types.h"
}

#include "Arduino.h"
#include "api/IPAddress.h"

#include "netsocket/NetworkInterface.h"
#include "SocketHelpers.h"

#if defined(COMPONENT_4343W_FS)
#include "WhdSoftAPInterface.h"
Expand All @@ -52,7 +49,7 @@ namespace arduino {
typedef void* (*voidPrtFuncPtr)(void);
typedef void (*ArduinoPortentaH7WiFiFeedWatchdogFuncPtr)(void);

class WiFiClass
class WiFiClass : public MbedSocketClass
{
public:
static int16_t _state[MAX_SOCK_NUM];
Expand All @@ -62,17 +59,11 @@ class WiFiClass

WiFiClass(voidPrtFuncPtr _cb) : _initializerCallback(_cb) {};

/*
* Get the first socket available
*/
static uint8_t getSocket();

/*
* Get firmware version
*/
static char* firmwareVersion();


/* Start Wifi connection for OPEN networks
*
* param ssid: Pointer to the SSID string.
Expand Down Expand Up @@ -100,57 +91,6 @@ class WiFiClass

int beginAP(const char *ssid, const char* passphrase, uint8_t channel = DEFAULT_AP_CHANNEL);

/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
*/
void config(IPAddress local_ip);

/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration as string
*/
void config(const char *local_ip);

/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
*/
void config(IPAddress local_ip, IPAddress dns_server);

/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
* param gateway : Static gateway configuration
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);

/* Change Ip configuration settings disabling the dhcp client
*
* param local_ip: Static ip configuration
* param dns_server: IP configuration for DNS server 1
* param gateway: Static gateway configuration
* param subnet: Static Subnet mask
*/
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);

/* Change DNS Ip configuration
*
* param dns_server1: ip configuration for DNS server 1
*/
void setDNS(IPAddress dns_server1);

/* Change DNS Ip configuration
*
* param dns_server1: ip configuration for DNS server 1
* param dns_server2: ip configuration for DNS server 2
*
*/
void setDNS(IPAddress dns_server1, IPAddress dns_server2);


/* Set the hostname used for DHCP requests
*
* param name: hostname to set
Expand All @@ -174,27 +114,6 @@ class WiFiClass
*/
uint8_t* macAddress(uint8_t* mac);

/*
* Get the interface IP address.
*
* return: Ip address value
*/
IPAddress localIP();

/*
* Get the interface subnet mask address.
*
* return: subnet mask address value
*/
IPAddress subnetMask();

/*
* Get the gateway ip address.
*
* return: gateway ip address value
*/
IPAddress gatewayIP();

/*
* Return the current SSID associated with the network
*
Expand Down Expand Up @@ -269,15 +188,6 @@ class WiFiClass
*/
uint8_t status();

/*
* Resolve the given hostname to an IP address.
* param aHostname: Name to be resolved
* param aResult: IPAddress structure to store the returned IP address
* result: 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int hostByName(const char* aHostname, IPAddress& aResult);

unsigned long getTime();

void lowPowerMode();
Expand All @@ -301,11 +211,6 @@ class WiFiClass
private:

EMACInterface* _softAP = nullptr;
SocketAddress _ip = nullptr;
SocketAddress _gateway = nullptr;
SocketAddress _netmask = nullptr;
SocketAddress _dnsServer1 = nullptr;
SocketAddress _dnsServer2 = nullptr;
char* _ssid = nullptr;
volatile wl_status_t _currentNetworkStatus = WL_IDLE_STATUS;
WiFiInterface* wifi_if = nullptr;
Expand All @@ -316,13 +221,15 @@ class WiFiClass
void ensureDefaultAPNetworkConfiguration();
static void * handleAPEvents(whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data);
bool isVisible(const char* ssid);
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
ArduinoPortentaH7WiFiFeedWatchdogFuncPtr _feed_watchdog_func = 0;
};

}

extern WiFiClass WiFi;

#include "WiFiClient.h"
#include "WiFiServer.h"
#include "WiFiUdp.h"

#endif
Loading