From 9666fc21a8bd48a0d0d5814c29be468c4447a06c Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 27 May 2024 17:11:17 +0200 Subject: [PATCH 1/2] codestyle unification --- libraries/WiFiS3/src/Modem.cpp | 1 + libraries/WiFiS3/src/WiFi.cpp | 145 ++++++++++++++++----------------- libraries/WiFiS3/src/WiFi.h | 56 ++++++------- 3 files changed, 98 insertions(+), 104 deletions(-) diff --git a/libraries/WiFiS3/src/Modem.cpp b/libraries/WiFiS3/src/Modem.cpp index 3dd1a87c..c8345637 100644 --- a/libraries/WiFiS3/src/Modem.cpp +++ b/libraries/WiFiS3/src/Modem.cpp @@ -207,6 +207,7 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) { while( _serial->available() ){ char c = _serial->read(); data_res += c; + if(_serial_debug && _debug_level >= 1) { _serial_debug->print(c); diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index 0a3a80c8..ceef6c99 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -20,10 +20,8 @@ const char* CWifi::firmwareVersion() { return fw_version; } return "99.99.99"; - } - /* -------------------------------------------------------------------------- */ int CWifi::begin(const char* ssid) { /* -------------------------------------------------------------------------- */ @@ -54,7 +52,7 @@ int CWifi::begin(const char* ssid, const char *passphrase) { return WL_CONNECTED; } } - return WL_CONNECT_FAILED; + return WL_CONNECT_FAILED; } /* passphrase is needed so a default one will be set */ @@ -78,7 +76,7 @@ uint8_t CWifi::beginAP(const char *ssid, const char* passphrase) { /* -------------------------------------------------------------------------- */ uint8_t CWifi::beginAP(const char *ssid, const char* passphrase, uint8_t channel) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ string res = ""; modem.begin(); modem.write(string(PROMPT(_MODE)),res, "%s%d\r\n" , CMD_WRITE(_MODE), 2); @@ -97,10 +95,10 @@ uint8_t CWifi::beginAP(const char *ssid, const char* passphrase, uint8_t channel /* -------------------------------------------------------------------------- */ void CWifi::config(IPAddress local_ip) { /* -------------------------------------------------------------------------- */ - IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1); - IPAddress _sm(255,255,255,0); - IPAddress dns(0,0,0,0); - return _config(local_ip, _gw, _sm, _gw, dns); + IPAddress _gw(local_ip[0],local_ip[1], local_ip[2], 1); + IPAddress _sm(255,255,255,0); + IPAddress dns(0,0,0,0); + return _config(local_ip, _gw, _sm, _gw, dns); } /* -------------------------------------------------------------------------- */ @@ -116,7 +114,7 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPA string gw = to_string(gateway[0]) + "."; gw += to_string(gateway[1]) + "."; gw += to_string(gateway[2]) + "."; - gw += to_string(gateway[3]); + gw += to_string(gateway[3]); string nm = to_string(subnet[0]) + "."; nm += to_string(subnet[1]) + "."; @@ -127,18 +125,18 @@ void CWifi::_config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPA _dns1 += to_string(dns1[1]) + "."; _dns1 += to_string(dns1[2]) + "."; _dns1 += to_string(dns1[3]); - + string _dns2 = to_string(dns2[0]) + "."; _dns2 += to_string(dns2[1]) + "."; _dns2 += to_string(dns2[2]) + "."; - _dns2 += to_string(dns2[3]); + _dns2 += to_string(dns2[3]); ip_ap = local_ip; gw_ap = gateway; nm_ap = subnet; modem.write(PROMPT(_SOFTAPCONFIG),res, "%s%s,%s,%s\r\n" , CMD_WRITE(_SOFTAPCONFIG), ip.c_str(), ip.c_str(), nm.c_str()); - modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str(),_dns1.c_str(),_dns2.c_str()); + modem.write(string(PROMPT(_SETIP)),res, "%s%s,%s,%s,%s,%s\r\n" , CMD_WRITE(_SETIP), ip.c_str(), gw.c_str(), nm.c_str(),_dns1.c_str(),_dns2.c_str()); } /* -------------------------------------------------------------------------- */ @@ -152,32 +150,32 @@ void CWifi::config(IPAddress local_ip, IPAddress dns_server) { /* -------------------------------------------------------------------------- */ void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) { -/* -------------------------------------------------------------------------- */ - +/* -------------------------------------------------------------------------- */ + IPAddress _sm(255,255,255,0); IPAddress dns(0,0,0,0); - return _config(local_ip, gateway, _sm,dns_server,dns); + return _config(local_ip, gateway, _sm,dns_server,dns); } /* -------------------------------------------------------------------------- */ void CWifi::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) { /* -------------------------------------------------------------------------- */ - + IPAddress dns(0,0,0,0); - return _config(local_ip, gateway, subnet,dns_server,dns); + return _config(local_ip, gateway, subnet,dns_server,dns); } /* -------------------------------------------------------------------------- */ void CWifi::setDNS(IPAddress dns_server1) { /* -------------------------------------------------------------------------- */ IPAddress dns(0,0,0,0); - return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns); + return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns); } /* -------------------------------------------------------------------------- */ void CWifi::setDNS(IPAddress dns_server1, IPAddress dns_server2) { /* -------------------------------------------------------------------------- */ - return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns_server2); + return _config(localIP(), gatewayIP(), subnetMask(),dns_server1,dns_server2); } /* -------------------------------------------------------------------------- */ @@ -189,29 +187,28 @@ void CWifi::setHostname(const char* name) { /* -------------------------------------------------------------------------- */ int CWifi::disconnect() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ string res = ""; modem.begin(); if(modem.write(string(PROMPT(_DISCONNECT)),res,CMD(_DISCONNECT))) { return 1; - } + } return 0; } /* -------------------------------------------------------------------------- */ void CWifi::end(void) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ string res = ""; modem.begin(); modem.write(string(PROMPT(_SOFTRESETWIFI)),res, "%s" , CMD(_SOFTRESETWIFI)); } - static bool macStr2macArray(uint8_t *mac_out, const char *mac_in) { - if(mac_in[2] != ':' || + if(mac_in[2] != ':' || mac_in[5] != ':' || mac_in[8] != ':' || mac_in[11] != ':' || @@ -227,13 +224,12 @@ static bool macStr2macArray(uint8_t *mac_out, const char *mac_in) { return true; } - /* -------------------------------------------------------------------------- */ uint8_t* CWifi::macAddress(uint8_t* _mac) { -/* -------------------------------------------------------------------------- */ - string res = ""; - modem.begin(); - if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) { +/* -------------------------------------------------------------------------- */ + string res = ""; + modem.begin(); + if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) { if(atoi(res.c_str()) == 1) { if(modem.write(string(PROMPT(_MACSTA)),res, "%s" , CMD_READ(_MACSTA))) { macStr2macArray(_mac, res.c_str()); @@ -249,9 +245,6 @@ uint8_t* CWifi::macAddress(uint8_t* _mac) { return _mac; } - - - /* -------------------------------------------------------------------------- */ int8_t CWifi::scanNetworks() { /* -------------------------------------------------------------------------- */ @@ -280,7 +273,7 @@ int8_t CWifi::scanNetworks() { ap.encryption_mode = tokens[4]; access_points.push_back(ap); } - } + } } return (int8_t)access_points.size(); @@ -317,10 +310,9 @@ IPAddress CWifi::dnsIP(int n) { return dns_IP; } } - return IPAddress(0,0,0,0); + return IPAddress(0,0,0,0); } - /* -------------------------------------------------------------------------- */ IPAddress CWifi::localIP() { /* -------------------------------------------------------------------------- */ @@ -334,14 +326,14 @@ IPAddress CWifi::localIP() { if(modem.write(string(PROMPT(_MODE)),res, "%s" , CMD_READ(_MODE))) { if(atoi(res.c_str()) == 1) { if(modem.write(string(PROMPT(_IPSTA)),res, "%s%d\r\n" , CMD_WRITE(_IPSTA), IP_ADDR)) { - + local_IP.fromString(res.c_str()); - + } } else if(atoi(res.c_str()) == 2) { if(modem.write(string(PROMPT(_IPSOFTAP)),res, CMD(_IPSOFTAP))) { - + local_IP.fromString(res.c_str()); } } @@ -376,27 +368,26 @@ IPAddress CWifi::gatewayIP() { gateway_IP.fromString(res.c_str()); return gateway_IP; } - return IPAddress(0,0,0,0); + return IPAddress(0,0,0,0); } /* -------------------------------------------------------------------------- */ const char* CWifi::SSID(uint8_t networkItem) { /* -------------------------------------------------------------------------- */ - if(networkItem < access_points.size()) { - return access_points[networkItem].ssid.c_str(); - } - return nullptr; + if(networkItem < access_points.size()) { + return access_points[networkItem].ssid.c_str(); + } + return nullptr; } -/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int32_t CWifi::RSSI(uint8_t networkItem) { - if(networkItem < access_points.size()) { - return atoi(access_points[networkItem].rssi.c_str()); - } - return -1000; +/* -------------------------------------------------------------------------- */ + if(networkItem < access_points.size()) { + return atoi(access_points[networkItem].rssi.c_str()); + } + return -1000; } -/* -------------------------------------------------------------------------- */ static uint8_t Encr2wl_enc(string e) { if (e == string("open")) { @@ -418,54 +409,52 @@ static uint8_t Encr2wl_enc(string e) { } else { return ENC_TYPE_UNKNOWN; } - } - +} /* -------------------------------------------------------------------------- */ uint8_t CWifi::encryptionType() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ scanNetworks(); string myssid(SSID()); for(unsigned int i = 0; i < access_points.size(); i++) { if(myssid == access_points[i].ssid) { - return Encr2wl_enc(access_points[i].encryption_mode); + return Encr2wl_enc(access_points[i].encryption_mode); } } return ENC_TYPE_UNKNOWN; } - /* -------------------------------------------------------------------------- */ uint8_t CWifi::encryptionType(uint8_t networkItem) { - if(networkItem < access_points.size()) { - return Encr2wl_enc(access_points[networkItem].encryption_mode); - } - return 0; +/* -------------------------------------------------------------------------- */ + if(networkItem < access_points.size()) { + return Encr2wl_enc(access_points[networkItem].encryption_mode); + } + return 0; } -/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ uint8_t* CWifi::BSSID(uint8_t networkItem, uint8_t* bssid) { +/* -------------------------------------------------------------------------- */ if(networkItem < access_points.size()) { for(int i = 0; i < 6; i++) { *(bssid + i) = access_points[networkItem].uint_bssid[i]; } - return bssid; + return bssid; } - return nullptr; + return nullptr; } -/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -uint8_t CWifi::channel(uint8_t networkItem) { +uint8_t CWifi::channel(uint8_t networkItem) { +/* -------------------------------------------------------------------------- */ if(networkItem < access_points.size()) { - return atoi(access_points[networkItem].channel.c_str()); + return atoi(access_points[networkItem].channel.c_str()); } return 0; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ const char* CWifi::SSID() { /* -------------------------------------------------------------------------- */ string res = ""; @@ -486,9 +475,9 @@ const char* CWifi::SSID() { return ""; } -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ uint8_t* CWifi::BSSID(uint8_t* bssid) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ string res = ""; if(modem.write(string(PROMPT(_GETBSSID)), res, CMD_READ(_GETBSSID))) { macStr2macArray(bssid, res.c_str()); @@ -497,7 +486,7 @@ uint8_t* CWifi::BSSID(uint8_t* bssid) { return nullptr; } -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ int32_t CWifi::RSSI() { /* -------------------------------------------------------------------------- */ string res = ""; @@ -507,9 +496,9 @@ int32_t CWifi::RSSI() { return 0; } -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ const char* CWifi::softAPSSID() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ string res = ""; if(modem.write(string(PROMPT(_GETSOFTAPSSID)), res, CMD_READ(_GETSOFTAPSSID))) { apssid = res; @@ -520,7 +509,7 @@ const char* CWifi::softAPSSID() { /* -------------------------------------------------------------------------- */ uint8_t CWifi::status() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ modem.begin(); string res = ""; if(modem.write(string(PROMPT(_GETSTATUS)), res, CMD_READ(_GETSTATUS))) { @@ -532,7 +521,7 @@ uint8_t CWifi::status() { /* -------------------------------------------------------------------------- */ int CWifi::hostByName(const char* aHostname, IPAddress& aResult) { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ modem.begin(); string res = ""; if(modem.write(string(PROMPT(_GETHOSTBYNAME)),res, "%s%s\r\n" , CMD_WRITE(_GETHOSTBYNAME), aHostname)) { @@ -542,13 +531,15 @@ int CWifi::hostByName(const char* aHostname, IPAddress& aResult) { return 0; } +/* -------------------------------------------------------------------------- */ uint8_t CWifi::reasonCode() { - return 0; +/* -------------------------------------------------------------------------- */ + return 0; } -/* ----------------------------------------------*/ +/* -------------------------------------------------------------------------- */ unsigned long CWifi::getTime() { -/* ----------------------------------------------*/ +/* -------------------------------------------------------------------------- */ modem.begin(); string res = ""; if(modem.write(string(PROMPT(_GETTIME)),res,"%s\r\n", CMD_WRITE(_GETTIME))) { @@ -557,7 +548,9 @@ unsigned long CWifi::getTime() { return 0; } +/* -------------------------------------------------------------------------- */ void CWifi::setTimeout(unsigned long timeout) { +/* -------------------------------------------------------------------------- */ _timeout = timeout; } diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index bbacc5f1..6cf797c5 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -22,30 +22,30 @@ #define WL_MAC_ADDR_LENGTH 6 class CAccessPoint { - public: - std::string ssid; - std::string bssid; - uint8_t uint_bssid[6]; - std::string rssi; - std::string channel; - std::string encryption_mode; +public: + std::string ssid; + std::string bssid; + uint8_t uint_bssid[6]; + std::string rssi; + std::string channel; + std::string encryption_mode; }; class CWifi { -private: - void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2); - unsigned long _timeout; - std::vector access_points; - std::string ssid; - std::string apssid; - +private: + void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2); + unsigned long _timeout; + std::vector access_points; + std::string ssid; + std::string apssid; + - IPAddress ip_ap = DEFAULT_IP_AP_ADDRESS; - IPAddress gw_ap = DEFAULT_GW_AP_ADDRESS; - IPAddress nm_ap = DEFAULT_NM_AP_ADDRESS; + IPAddress ip_ap = DEFAULT_IP_AP_ADDRESS; + IPAddress gw_ap = DEFAULT_GW_AP_ADDRESS; + IPAddress nm_ap = DEFAULT_NM_AP_ADDRESS; @@ -65,13 +65,13 @@ class CWifi { int ping(const String &hostname, uint8_t ttl = 128, uint8_t count = 1); int ping(const char* host, uint8_t ttl = 128, uint8_t count = 1); - /* - * Start WiFi connection for OPEN networks + /* + * Start WiFi connection for OPEN networks * param ssid: Pointer to the SSID string. */ int begin(const char* ssid); - + /* Start WiFi connection with passphrase * the most secure supported mode will be automatically selected @@ -118,7 +118,7 @@ class CWifi { * 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 @@ -154,8 +154,8 @@ class CWifi { * Get the interface MAC address. * * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - * - * the value returned by this function is meaningfull only if called + * + * the value returned by this function is meaningfull only if called * afert a begin (both begin or beginAP) or a ScanNetwork function * otherwise an empty mac address is returned */ @@ -180,9 +180,9 @@ class CWifi { * * return: gateway IP address value */ - IPAddress gatewayIP(); + IPAddress gatewayIP(); - IPAddress dnsIP(int n = 0); + IPAddress dnsIP(int n = 0); /* * Get the interface the AP IP address. @@ -244,8 +244,8 @@ class CWifi { * * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - - + + */ uint8_t encryptionType(uint8_t networkItem); uint8_t encryptionType(); @@ -289,7 +289,7 @@ class CWifi { void setTimeout(unsigned long timeout); - + }; extern CWifi WiFi; From af0787a991a2befed636e1d3365a5d269dacad80 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 27 May 2024 17:12:09 +0200 Subject: [PATCH 2/2] added possibility to get fw version of wifi module as uint32 --- libraries/WiFiS3/src/WiFi.cpp | 12 ++++++++++++ libraries/WiFiS3/src/WiFi.h | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index ceef6c99..a43d32d5 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -22,6 +22,18 @@ const char* CWifi::firmwareVersion() { return "99.99.99"; } +/* -------------------------------------------------------------------------- */ +uint32_t CWifi::firmwareVersionU32() { +/* -------------------------------------------------------------------------- */ + uint8_t ret[4]; + string res = ""; + modem.begin(); + if(modem.write(string(PROMPT(_FWVERSION_U32)), res, CMD_READ(_FWVERSION_U32))) { + return res[0] << 16| res[1] << 8 | res[2]; + } + return 0x636363; +} + /* -------------------------------------------------------------------------- */ int CWifi::begin(const char* ssid) { /* -------------------------------------------------------------------------- */ diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index 6cf797c5..2b733cfa 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -57,6 +57,14 @@ class CWifi { * Get firmware version */ static const char* firmwareVersion(); + /* + * Get firmware version U32 + * + * Since version is made in a semver fashion, thus in an integer it will be represented as + * byte 1 (MSB) | byte 2 | byte 3 | byte 4 + * 0 | MAJOR | MINOR | PATCH + */ + uint32_t firmwareVersionU32(); /* * PING