From 9676cfecc39db744fe64316ce63ed2eb48d2724d Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Thu, 28 Jun 2018 12:09:14 +0200 Subject: [PATCH] Initial changes to support u-blox NINA-W102 with Arduino firmware --- README.adoc | 9 +- .../ConnectNoEncryption.ino | 20 +- .../ConnectNoEncryption/arduino_secrets.h | 1 + examples/ConnectWithWEP/ConnectWithWEP.ino | 25 +- examples/ConnectWithWEP/arduino_secrets.h | 2 + examples/ConnectWithWPA/ConnectWithWPA.ino | 23 +- examples/ConnectWithWPA/arduino_secrets.h | 2 + examples/ScanNetworks/ScanNetworks.ino | 14 +- .../SimpleWebServerWiFi.ino | 23 +- .../SimpleWebServerWiFi/arduino_secrets.h | 2 + examples/WiFiChatServer/WiFiChatServer.ino | 18 +- examples/WiFiChatServer/arduino_secrets.h | 2 + .../WiFiUdpNtpClient/WiFiUdpNtpClient.ino | 18 +- examples/WiFiUdpNtpClient/arduino_secrets.h | 2 + .../WiFiUdpSendReceiveString.ino | 25 +- .../arduino_secrets.h | 2 + examples/WiFiWebClient/WiFiWebClient.ino | 20 +- examples/WiFiWebClient/arduino_secrets.h | 2 + .../WiFiWebClientRepeating.ino | 23 +- .../WiFiWebClientRepeating/arduino_secrets.h | 2 + examples/WiFiWebServer/WiFiWebServer.ino | 20 +- examples/WiFiWebServer/arduino_secrets.h | 2 + keywords.txt | 31 +- library.properties | 11 +- src/WiFi.cpp | 135 ++++- src/WiFi.h | 50 +- src/WiFi1010.h | 20 + src/WiFiClient.cpp | 89 ++- src/WiFiClient.h | 9 +- src/WiFiSSLClient.cpp | 40 ++ src/WiFiSSLClient.h | 35 ++ src/WiFiServer.cpp | 80 ++- src/WiFiServer.h | 2 + src/WiFiUdp.cpp | 22 +- src/WiFiUdp.h | 2 + src/utility/server_drv.cpp | 211 +++++++- src/utility/server_drv.h | 13 +- src/utility/spi_drv.cpp | 94 +++- src/utility/spi_drv.h | 9 +- src/utility/wifi_drv.cpp | 505 +++++++++++++++++- src/utility/wifi_drv.h | 44 +- src/utility/wifi_spi.h | 23 +- src/utility/wl_definitions.h | 11 +- src/utility/wl_types.h | 8 + 44 files changed, 1392 insertions(+), 309 deletions(-) create mode 100644 examples/ConnectNoEncryption/arduino_secrets.h create mode 100644 examples/ConnectWithWEP/arduino_secrets.h create mode 100644 examples/ConnectWithWPA/arduino_secrets.h create mode 100644 examples/SimpleWebServerWiFi/arduino_secrets.h create mode 100644 examples/WiFiChatServer/arduino_secrets.h create mode 100644 examples/WiFiUdpNtpClient/arduino_secrets.h create mode 100644 examples/WiFiUdpSendReceiveString/arduino_secrets.h create mode 100644 examples/WiFiWebClient/arduino_secrets.h create mode 100644 examples/WiFiWebClientRepeating/arduino_secrets.h create mode 100644 examples/WiFiWebServer/arduino_secrets.h create mode 100644 src/WiFi1010.h create mode 100644 src/WiFiSSLClient.cpp create mode 100644 src/WiFiSSLClient.h diff --git a/README.adoc b/README.adoc index 82a56c2..7edd6ce 100644 --- a/README.adoc +++ b/README.adoc @@ -1,12 +1,15 @@ -= WiFi Library for Arduino = += WiFi1010 Library for Arduino = -With the Arduino WiFi Shield, this library allows an Arduino board to connect to the internet. +Enables network connection (local and Internet) with the Arduino MKR WiFi 1010, Arduino MKR VIDOR 4000 and Arduino UNO WiFi Rev.2. + +With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi. The board can connect either to open or encrypted networks (WEP, WPA). The IP address can be assigned statically or through a DHCP. The library can also manage DNS. For more information about this library please visit us at -http://www.arduino.cc/en/Reference/WiFi +http://www.arduino.cc/en/Reference/WiFi1010 == License == +Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. Copyright (C) 2006-2008, Atmel Corporation All rights reserved. Copyright (c) 2001-2004 Swedish Institute of Computer Science. diff --git a/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/examples/ConnectNoEncryption/ConnectNoEncryption.ino index e12dfe3..79adc20 100644 --- a/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -4,18 +4,17 @@ Then it prints the MAC address of the Wifi shield, the IP address obtained, and other network details. - Circuit: - * WiFi shield attached - created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe */ #include -#include +#include -char ssid[] = "yourNetwork"; // the name of your network +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { @@ -24,16 +23,15 @@ void setup() { while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/ConnectNoEncryption/arduino_secrets.h b/examples/ConnectNoEncryption/arduino_secrets.h new file mode 100644 index 0000000..07c1148 --- /dev/null +++ b/examples/ConnectNoEncryption/arduino_secrets.h @@ -0,0 +1 @@ +#define SECRET_SSID "" diff --git a/examples/ConnectWithWEP/ConnectWithWEP.ino b/examples/ConnectWithWEP/ConnectWithWEP.ino index 93070f5..4378706 100644 --- a/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -1,7 +1,7 @@ /* This example connects to a WEP-encrypted Wifi network. - Then it prints the MAC address of the Wifi shield, + Then it prints the MAC address of the Wifi module, the IP address obtained, and other network details. If you use 40-bit WEP, you need a key that is 10 characters long, @@ -14,19 +14,18 @@ D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters, all in the 0-9, A-F range. - Circuit: - * WiFi shield attached - created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number int status = WL_IDLE_STATUS; // the Wifi radio's status @@ -37,15 +36,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } @@ -53,7 +52,7 @@ void setup() { while (status != WL_CONNECTED) { Serial.print("Attempting to connect to WEP network, SSID: "); Serial.println(ssid); - status = WiFi.begin(ssid, keyIndex, key); + status = WiFi.begin(ssid, keyIndex, pass); // wait 10 seconds for connection: delay(10000); diff --git a/examples/ConnectWithWEP/arduino_secrets.h b/examples/ConnectWithWEP/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/ConnectWithWEP/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/ConnectWithWPA/ConnectWithWPA.ino b/examples/ConnectWithWPA/ConnectWithWPA.ino index 56ae372..b0d6de1 100644 --- a/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -1,22 +1,21 @@ /* This example connects to an unencrypted Wifi network. - Then it prints the MAC address of the Wifi shield, + Then it prints the MAC address of the Wifi module, the IP address obtained, and other network details. - Circuit: - * WiFi shield attached - created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { @@ -26,15 +25,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/ConnectWithWPA/arduino_secrets.h b/examples/ConnectWithWPA/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/ConnectWithWPA/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/ScanNetworks/ScanNetworks.ino b/examples/ScanNetworks/ScanNetworks.ino index 7203207..0e42dd9 100644 --- a/examples/ScanNetworks/ScanNetworks.ino +++ b/examples/ScanNetworks/ScanNetworks.ino @@ -1,7 +1,7 @@ /* This example prints the Wifi shield's MAC address, and - scans for available Wifi networks using the Wifi shield. + scans for available Wifi networks using the Wifi module. Every ten seconds, it scans again. It doesn't actually connect to any network, so no encryption scheme is specified. @@ -16,7 +16,7 @@ #include -#include +#include void setup() { //Initialize serial and wait for port to open: @@ -25,15 +25,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino b/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino index c9cc5a6..36fd032 100644 --- a/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino +++ b/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino @@ -2,11 +2,11 @@ WiFi Web Server LED Blink A simple web server that lets you blink an LED via the web. - This sketch will print the IP address of your WiFi Shield (once connected) + This sketch will print the IP address of your WiFi module (once connected) to the Serial monitor. From there, you can open that address in a web browser to turn on and off the LED on pin 9. - If the IP address of your shield is yourAddress: + If the IP address of your board is yourAddress: http://yourAddress/H turns the LED on http://yourAddress/L turns it off @@ -21,10 +21,12 @@ by Tom Igoe */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; @@ -34,14 +36,15 @@ void setup() { Serial.begin(9600); // initialize serial communication pinMode(9, OUTPUT); // set the LED pin mode - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - while (true); // don't continue + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/SimpleWebServerWiFi/arduino_secrets.h b/examples/SimpleWebServerWiFi/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/SimpleWebServerWiFi/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiChatServer/WiFiChatServer.ino b/examples/WiFiChatServer/WiFiChatServer.ino index 11b3268..e0524c8 100644 --- a/examples/WiFiChatServer/WiFiChatServer.ino +++ b/examples/WiFiChatServer/WiFiChatServer.ino @@ -20,10 +20,12 @@ */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) @@ -40,15 +42,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/WiFiChatServer/arduino_secrets.h b/examples/WiFiChatServer/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiChatServer/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino b/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino index 118279d..71bd992 100644 --- a/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino +++ b/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino @@ -17,12 +17,14 @@ */ #include -#include +#include #include int status = WL_IDLE_STATUS; -char ssid[] = "mynetwork"; // your network SSID (name) -char pass[] = "mypassword"; // your network password +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) unsigned int localPort = 2390; // local port to listen for UDP packets @@ -43,15 +45,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/WiFiUdpNtpClient/arduino_secrets.h b/examples/WiFiUdpNtpClient/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiUdpNtpClient/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino b/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino index 1cd384b..f48fbc3 100644 --- a/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino +++ b/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino @@ -2,12 +2,9 @@ /* WiFi UDP Send and Receive String - This sketch wait an UDP packet on localPort using a WiFi shield. + This sketch wait an UDP packet on localPort using the WiFi module. When a packet is received an Acknowledge packet is sent to the client on port remotePort - Circuit: - * WiFi shield attached - created 30 December 2012 by dlf (Metodo2 srl) @@ -15,12 +12,14 @@ #include -#include +#include #include int status = WL_IDLE_STATUS; -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) unsigned int localPort = 2390; // local port to listen on @@ -37,15 +36,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } @@ -54,7 +53,7 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid); + status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); diff --git a/examples/WiFiUdpSendReceiveString/arduino_secrets.h b/examples/WiFiUdpSendReceiveString/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiUdpSendReceiveString/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiWebClient/WiFiWebClient.ino b/examples/WiFiWebClient/WiFiWebClient.ino index 33285c1..3ca4e31 100644 --- a/examples/WiFiWebClient/WiFiWebClient.ino +++ b/examples/WiFiWebClient/WiFiWebClient.ino @@ -3,7 +3,7 @@ Web client This sketch connects to a website (http://www.google.com) - using a WiFi shield. + using the WiFi module. This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly. @@ -22,10 +22,12 @@ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; @@ -46,15 +48,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/WiFiWebClient/arduino_secrets.h b/examples/WiFiWebClient/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiWebClient/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino b/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino index 9953824..9e07e59 100644 --- a/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino +++ b/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino @@ -2,10 +2,7 @@ Repeating Wifi Web Client This sketch connects to a a web server and makes a request - using an Arduino Wifi shield. - - Circuit: - * WiFi shield attached to pins SPI pins and pin 7 + using a WiFi equipped Arduino board. created 23 April 2012 modified 31 May 2012 @@ -18,10 +15,12 @@ */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; @@ -43,15 +42,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/WiFiWebClientRepeating/arduino_secrets.h b/examples/WiFiWebClientRepeating/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiWebClientRepeating/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/examples/WiFiWebServer/WiFiWebServer.ino b/examples/WiFiWebServer/WiFiWebServer.ino index c777d23..6a1459c 100644 --- a/examples/WiFiWebServer/WiFiWebServer.ino +++ b/examples/WiFiWebServer/WiFiWebServer.ino @@ -2,13 +2,11 @@ WiFi Web Server A simple web server that shows the value of the analog input pins. - using a WiFi shield. This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly. Circuit: - * WiFi shield attached * Analog inputs attached to pins A0 through A5 (optional) created 13 July 2010 @@ -19,11 +17,13 @@ */ #include -#include +#include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; @@ -37,15 +37,15 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue while (true); } String fv = WiFi.firmwareVersion(); - if (fv != "1.1.0") { + if (fv != "1.0.0") { Serial.println("Please upgrade the firmware"); } diff --git a/examples/WiFiWebServer/arduino_secrets.h b/examples/WiFiWebServer/arduino_secrets.h new file mode 100644 index 0000000..0c9fdd5 --- /dev/null +++ b/examples/WiFiWebServer/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/keywords.txt b/keywords.txt index 341114d..19cf907 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,20 +1,19 @@ ####################################### -# Syntax Coloring Map For WiFi +# Syntax Coloring Map For WiFi1010 ####################################### ####################################### # Library (KEYWORD1) ####################################### -WiFi KEYWORD1 WiFi -WiFiUdp KEYWORD1 WiFiUDPConstructor +WiFi KEYWORD1 +WiFi1010 KEYWORD1 +WiFiUdp KEYWORD1 +WiFiClient KEYWORD1 +WiFiSSLClient KEYWORD1 +WiFiServer KEYWORD1 +WiFiUDP KEYWORD1 -####################################### -# Datatypes (KEYWORD1) -####################################### - -Client KEYWORD1 WiFiClientConstructor -Server KEYWORD1 WiFiServerConstructor ####################################### # Methods and Functions (KEYWORD2) @@ -41,17 +40,21 @@ SSID KEYWORD2 BSSID KEYWORD2 RSSI KEYWORD2 encryptionType KEYWORD2 -getResult KEYWORD2 -getSocket KEYWORD2 -WiFiClient KEYWORD2 WiFiClient -WiFiServer KEYWORD2 WiFiServer -WiFiUDP KEYWORD2 WiFiUDPConstructor beginPacket KEYWORD2 endPacket KEYWORD2 parsePacket KEYWORD2 remoteIP KEYWORD2 remotePort KEYWORD2 +beginAP KEYWORD2 +setHostname KEYWORD2 +end KEYWORD2 +getTime KEYWORD2 +lowPowerMode KEYWORD2 +noLowPowerMode KEYWORD2 +ping KEYWORD2 +beginMulticast KEYWORD2 + ####################################### # Constants (LITERAL1) diff --git a/library.properties b/library.properties index 94e8308..34e9fc0 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,10 @@ -name=WiFi -version=1.2.7 +name=WiFi1010 +version=1.0.0 author=Arduino maintainer=Arduino -sentence=Enables network connection (local and Internet) using the Arduino WiFi shield. -paragraph=With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi. The shield can connect either to open or encrypted networks (WEP, WPA). The IP address can be assigned statically or through a DHCP. The library can also manage DNS. +sentence=Enables network connection (local and Internet) with the Arduino MKR WiFi 1010, Arduino MKR VIDOR 4000 and Arduino UNO WiFi Rev.2. +paragraph=With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi. The board can connect either to open or encrypted networks (WEP, WPA). The IP address can be assigned statically or through a DHCP. The library can also manage DNS. category=Communication -url=http://www.arduino.cc/en/Reference/WiFi +url=http://www.arduino.cc/en/Reference/WiFi1010 architectures=* +includes=WiFi1010.h diff --git a/src/WiFi.cpp b/src/WiFi.cpp index 88b18b0..fdcb58a 100644 --- a/src/WiFi.cpp +++ b/src/WiFi.cpp @@ -1,5 +1,6 @@ /* WiFi.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -26,10 +27,6 @@ extern "C" { #include "utility/debug.h" } -// XXX: don't make assumptions about the value of MAX_SOCK_NUM. -int16_t WiFiClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE }; -uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; - WiFiClass::WiFiClass() { } @@ -39,24 +36,12 @@ void WiFiClass::init() WiFiDrv::wifiDriverInit(); } -uint8_t WiFiClass::getSocket() -{ - for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) - { - if (WiFiClass::_server_port[i] == 0) - { - return i; - } - } - return NO_SOCKET_AVAIL; -} - -char* WiFiClass::firmwareVersion() +const char* WiFiClass::firmwareVersion() { return WiFiDrv::getFwVersion(); } -int WiFiClass::begin(char* ssid) +int WiFiClass::begin(const char* ssid) { uint8_t status = WL_IDLE_STATUS; uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; @@ -76,7 +61,7 @@ int WiFiClass::begin(char* ssid) return status; } -int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) +int WiFiClass::begin(const char* ssid, uint8_t key_idx, const char *key) { uint8_t status = WL_IDLE_STATUS; uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; @@ -95,7 +80,7 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) return status; } -int WiFiClass::begin(char* ssid, const char *passphrase) +int WiFiClass::begin(const char* ssid, const char *passphrase) { uint8_t status = WL_IDLE_STATUS; uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; @@ -115,6 +100,56 @@ int WiFiClass::begin(char* ssid, const char *passphrase) return status; } +uint8_t WiFiClass::beginAP(const char *ssid) +{ + return beginAP(ssid, 1); +} + +uint8_t WiFiClass::beginAP(const char *ssid, uint8_t channel) +{ + uint8_t status = WL_IDLE_STATUS; + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + if (WiFiDrv::wifiSetApNetwork(ssid, strlen(ssid), channel) != WL_FAILURE) + { + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }else + { + status = WL_AP_FAILED; + } + return status; +} + +uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) +{ + return beginAP(ssid, passphrase, 1); +} + +uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase, uint8_t channel) +{ + uint8_t status = WL_IDLE_STATUS; + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + // set passphrase + if (WiFiDrv::wifiSetApPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase), channel)!= WL_FAILURE) + { + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }else{ + status = WL_AP_FAILED; + } + return status; +} + void WiFiClass::config(IPAddress local_ip) { WiFiDrv::config(1, (uint32_t)local_ip, 0, 0); @@ -148,11 +183,21 @@ void WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2) WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2); } +void WiFiClass::setHostname(const char* name) +{ + WiFiDrv::setHostname(name); +} + int WiFiClass::disconnect() { return WiFiDrv::disconnect(); } +void WiFiClass::end(void) +{ + WiFiDrv::wifiDriverDeinit(); +} + uint8_t* WiFiClass::macAddress(uint8_t* mac) { uint8_t* _mac = WiFiDrv::getMacAddress(); @@ -181,7 +226,7 @@ IPAddress WiFiClass::gatewayIP() return ret; } -char* WiFiClass::SSID() +const char* WiFiClass::SSID() { return WiFiDrv::getCurrentSSID(); } @@ -220,7 +265,7 @@ int8_t WiFiClass::scanNetworks() return numOfNetworks; } -char* WiFiClass::SSID(uint8_t networkItem) +const char* WiFiClass::SSID(uint8_t networkItem) { return WiFiDrv::getSSIDNetoworks(networkItem); } @@ -235,6 +280,16 @@ uint8_t WiFiClass::encryptionType(uint8_t networkItem) return WiFiDrv::getEncTypeNetowrks(networkItem); } +uint8_t* WiFiClass::BSSID(uint8_t networkItem, uint8_t* bssid) +{ + return WiFiDrv::getBSSIDNetowrks(networkItem, bssid); +} + +uint8_t WiFiClass::channel(uint8_t networkItem) +{ + return WiFiDrv::getChannelNetowrks(networkItem); +} + uint8_t WiFiClass::status() { return WiFiDrv::getConnectionStatus(); @@ -245,4 +300,40 @@ int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) return WiFiDrv::getHostByName(aHostname, aResult); } +unsigned long WiFiClass::getTime() +{ + return WiFiDrv::getTime(); +} + +void WiFiClass::lowPowerMode() +{ + WiFiDrv::setPowerMode(1); +} + +void WiFiClass::noLowPowerMode() +{ + WiFiDrv::setPowerMode(0); +} + +int WiFiClass::ping(const char* hostname, uint8_t ttl) +{ + IPAddress ip; + + if (!hostByName(hostname, ip)) { + return WL_PING_UNKNOWN_HOST; + } + + return ping(ip, ttl); +} + +int WiFiClass::ping(const String &hostname, uint8_t ttl) +{ + return ping(hostname.c_str(), ttl); +} + +int WiFiClass::ping(IPAddress host, uint8_t ttl) +{ + return WiFiDrv::ping(host, ttl); +} + WiFiClass WiFi; diff --git a/src/WiFi.h b/src/WiFi.h index ef49428..1b7045c 100644 --- a/src/WiFi.h +++ b/src/WiFi.h @@ -1,5 +1,6 @@ /* WiFi.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -29,6 +30,7 @@ extern "C" { #include "IPAddress.h" #include "WiFiClient.h" +#include "WiFiSSLClient.h" #include "WiFiServer.h" class WiFiClass @@ -37,27 +39,19 @@ class WiFiClass static void init(); public: - static int16_t _state[MAX_SOCK_NUM]; - static uint16_t _server_port[MAX_SOCK_NUM]; - WiFiClass(); - /* - * Get the first socket available - */ - static uint8_t getSocket(); - /* * Get firmware version */ - static char* firmwareVersion(); + static const char* firmwareVersion(); /* Start Wifi connection for OPEN networks * * param ssid: Pointer to the SSID string. */ - int begin(char* ssid); + int begin(const char* ssid); /* Start Wifi connection with WEP encryption. * Configure a key into the device. The key type (WEP-40, WEP-104) @@ -67,7 +61,7 @@ class WiFiClass * param key_idx: The key index to set. Valid values are 0-3. * param key: Key input buffer. */ - int begin(char* ssid, uint8_t key_idx, const char* key); + int begin(const char* ssid, uint8_t key_idx, const char* key); /* Start Wifi connection with passphrase * the most secure supported mode will be automatically selected @@ -76,7 +70,12 @@ class WiFiClass * param passphrase: Passphrase. Valid characters in a passphrase * must be between ASCII 32-126 (decimal). */ - int begin(char* ssid, const char *passphrase); + int begin(const char* ssid, const char *passphrase); + + uint8_t beginAP(const char *ssid); + uint8_t beginAP(const char *ssid, uint8_t channel); + uint8_t beginAP(const char *ssid, const char* passphrase); + uint8_t beginAP(const char *ssid, const char* passphrase, uint8_t channel); /* Change Ip configuration settings disabling the dhcp client * @@ -122,6 +121,14 @@ class WiFiClass */ void setDNS(IPAddress dns_server1, IPAddress dns_server2); + + /* Set the hostname used for DHCP requests + * + * param name: hostname to set + * + */ + void setHostname(const char* name); + /* * Disconnect from the network * @@ -129,6 +136,8 @@ class WiFiClass */ int disconnect(void); + void end(void); + /* * Get the interface MAC address. * @@ -162,7 +171,7 @@ class WiFiClass * * return: ssid string */ - char* SSID(); + const char* SSID(); /* * Return the current BSSID associated with the network. @@ -201,7 +210,7 @@ class WiFiClass * * return: ssid string of the specified item on the networks scanned list */ - char* SSID(uint8_t networkItem); + const char* SSID(uint8_t networkItem); /* * Return the encryption type of the networks discovered during the scanNetworks @@ -212,6 +221,9 @@ class WiFiClass */ uint8_t encryptionType(uint8_t networkItem); + uint8_t* BSSID(uint8_t networkItem, uint8_t* bssid); + uint8_t channel(uint8_t networkItem); + /* * Return the RSSI of the networks discovered during the scanNetworks * @@ -237,8 +249,14 @@ class WiFiClass */ int hostByName(const char* aHostname, IPAddress& aResult); - friend class WiFiClient; - friend class WiFiServer; + unsigned long getTime(); + + void lowPowerMode(); + void noLowPowerMode(); + + int ping(const char* hostname, uint8_t ttl = 128); + int ping(const String &hostname, uint8_t ttl = 128); + int ping(IPAddress host, uint8_t ttl = 128); }; extern WiFiClass WiFi; diff --git a/src/WiFi1010.h b/src/WiFi1010.h new file mode 100644 index 0000000..7743d9d --- /dev/null +++ b/src/WiFi1010.h @@ -0,0 +1,20 @@ +/* + This file is part of the WiFi1010 library. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "WiFi.h" diff --git a/src/WiFiClient.cpp b/src/WiFiClient.cpp index eb8e6af..18d78a9 100644 --- a/src/WiFiClient.cpp +++ b/src/WiFiClient.cpp @@ -1,5 +1,6 @@ /* WiFiClient.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -28,11 +29,12 @@ extern "C" { #include "WiFiClient.h" #include "WiFiServer.h" #include "utility/server_drv.h" +#include "utility/wifi_drv.h" uint16_t WiFiClient::_srcport = 1024; -WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { +WiFiClient::WiFiClient() : _sock(NO_SOCKET_AVAIL) { } WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { @@ -48,11 +50,10 @@ int WiFiClient::connect(const char* host, uint16_t port) { } int WiFiClient::connect(IPAddress ip, uint16_t port) { - _sock = getFirstSocket(); + _sock = ServerDrv::getSocket(); if (_sock != NO_SOCKET_AVAIL) { ServerDrv::startClient(uint32_t(ip), port, _sock); - WiFiClass::_state[_sock] = _sock; unsigned long start = millis(); @@ -71,6 +72,54 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { return 1; } +int WiFiClient::connectSSL(IPAddress ip, uint16_t port) +{ + _sock = ServerDrv::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startClient(uint32_t(ip), port, _sock, TLS_MODE); + + unsigned long start = millis(); + + // wait 4 second for the connection to close + while (!connected() && millis() - start < 10000) + delay(1); + + if (!connected()) + { + return 0; + } + }else{ + Serial.println("No Socket available"); + return 0; + } + return 1; +} + +int WiFiClient::connectSSL(const char *host, uint16_t port) +{ + _sock = ServerDrv::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startClient(host, strlen(host), uint32_t(0), port, _sock, TLS_MODE); + + unsigned long start = millis(); + + // wait 4 second for the connection to close + while (!connected() && millis() - start < 10000) + delay(1); + + if (!connected()) + { + return 0; + } + }else{ + Serial.println("No Socket available"); + return 0; + } + return 1; +} + size_t WiFiClient::write(uint8_t b) { return write(&b, 1); } @@ -87,8 +136,8 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { return 0; } - - if (!ServerDrv::sendData(_sock, buf, size)) + size_t written = ServerDrv::sendData(_sock, buf, size); + if (!written) { setWriteError(); return 0; @@ -99,7 +148,7 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { return 0; } - return size; + return written; } int WiFiClient::available() { @@ -127,7 +176,7 @@ int WiFiClient::read(uint8_t* buf, size_t size) { uint16_t _size = size; if (!ServerDrv::getDataBuf(_sock, buf, &_size)) return -1; - return 0; + return _size; } int WiFiClient::peek() { @@ -149,7 +198,6 @@ void WiFiClient::stop() { return; ServerDrv::stopClient(_sock); - WiFiClass::_state[_sock] = NA_STATE; int count = 0; // wait maximum 5 secs for the connection to close @@ -185,15 +233,22 @@ WiFiClient::operator bool() { return _sock != 255; } -// Private Methods -uint8_t WiFiClient::getFirstSocket() +IPAddress WiFiClient::remoteIP() { - for (int i = 0; i < MAX_SOCK_NUM; i++) { - if (WiFiClass::_state[i] == NA_STATE) - { - return i; - } - } - return SOCK_NOT_AVAIL; + uint8_t _remoteIp[4] = {0}; + uint8_t _remotePort[2] = {0}; + + WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort); + IPAddress ip(_remoteIp); + return ip; } +uint16_t WiFiClient::remotePort() +{ + uint8_t _remoteIp[4] = {0}; + uint8_t _remotePort[2] = {0}; + + WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort); + uint16_t port = (_remotePort[0]<<8)+_remotePort[1]; + return port; +} diff --git a/src/WiFiClient.h b/src/WiFiClient.h index caac8fd..9ac95a6 100644 --- a/src/WiFiClient.h +++ b/src/WiFiClient.h @@ -1,5 +1,6 @@ /* WiFiClient.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -33,6 +34,8 @@ class WiFiClient : public Client { uint8_t status(); virtual int connect(IPAddress ip, uint16_t port); virtual int connect(const char *host, uint16_t port); + virtual int connectSSL(IPAddress ip, uint16_t port); + virtual int connectSSL(const char *host, uint16_t port); virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buf, size_t size); virtual int available(); @@ -44,7 +47,11 @@ class WiFiClient : public Client { virtual uint8_t connected(); virtual operator bool(); + virtual IPAddress remoteIP(); + virtual uint16_t remotePort(); + friend class WiFiServer; + friend class WiFiDrv; using Print::write; @@ -52,8 +59,6 @@ class WiFiClient : public Client { static uint16_t _srcport; uint8_t _sock; //not used uint16_t _socket; - - uint8_t getFirstSocket(); }; #endif diff --git a/src/WiFiSSLClient.cpp b/src/WiFiSSLClient.cpp new file mode 100644 index 0000000..31ba4e0 --- /dev/null +++ b/src/WiFiSSLClient.cpp @@ -0,0 +1,40 @@ +/* + This file is part of the WiFi1010 library. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "WiFiSSLClient.h" + +WiFiSSLClient::WiFiSSLClient() : + WiFiClient() +{ +} + +WiFiSSLClient::WiFiSSLClient(uint8_t sock) : + WiFiClient(sock) +{ +} + +int WiFiSSLClient::connect(IPAddress ip, uint16_t port) +{ + return WiFiClient::connectSSL(ip, port); +} + +int WiFiSSLClient::connect(const char* host, uint16_t port) +{ + return WiFiClient::connectSSL(host, port); +} diff --git a/src/WiFiSSLClient.h b/src/WiFiSSLClient.h new file mode 100644 index 0000000..1b3c452 --- /dev/null +++ b/src/WiFiSSLClient.h @@ -0,0 +1,35 @@ +/* + This file is part of the WiFi1010 library. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef WIFISSLCLIENT_H +#define WIFISSLCLIENT_H + +#include "WiFiClient.h" + +class WiFiSSLClient : public WiFiClient { + +public: + WiFiSSLClient(); + WiFiSSLClient(uint8_t sock); + + virtual int connect(IPAddress ip, uint16_t port); + virtual int connect(const char* host, uint16_t port); +}; + +#endif /* WIFISSLCLIENT_H */ diff --git a/src/WiFiServer.cpp b/src/WiFiServer.cpp index cab1dee..d0e55b9 100644 --- a/src/WiFiServer.cpp +++ b/src/WiFiServer.cpp @@ -1,5 +1,6 @@ /* WiFiServer.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -28,57 +29,48 @@ extern "C" { #include "WiFiClient.h" #include "WiFiServer.h" -WiFiServer::WiFiServer(uint16_t port) +WiFiServer::WiFiServer(uint16_t port) : + _sock(NO_SOCKET_AVAIL) { _port = port; } void WiFiServer::begin() { - uint8_t _sock = WiFiClass::getSocket(); + _sock = ServerDrv::getSocket(); if (_sock != NO_SOCKET_AVAIL) { ServerDrv::startServer(_port, _sock); - WiFiClass::_server_port[_sock] = _port; - WiFiClass::_state[_sock] = _sock; } } WiFiClient WiFiServer::available(byte* status) { - static int cycle_server_down = 0; - const int TH_SERVER_DOWN = 50; + int sock = NO_SOCKET_AVAIL; - for (int sock = 0; sock < MAX_SOCK_NUM; sock++) - { - if (WiFiClass::_server_port[sock] == _port) - { - WiFiClient client(sock); - uint8_t _status = client.status(); - uint8_t _ser_status = this->status(); - - if (status != NULL) - *status = _status; - - //server not in listen state, restart it - if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) - { - ServerDrv::startServer(_port, sock); - cycle_server_down = 0; - } - - if (_status == ESTABLISHED) - { - return client; //TODO - } + if (_sock != NO_SOCKET_AVAIL) { + sock = ServerDrv::availServer(_sock); + } + + if (sock != NO_SOCKET_AVAIL) { + WiFiClient client(sock); + + if (status != NULL) { + *status = client.status(); } + + return client; } return WiFiClient(255); } uint8_t WiFiServer::status() { - return ServerDrv::getServerState(0); + if (_sock == NO_SOCKET_AVAIL) { + return CLOSED; + } else { + return ServerDrv::getServerState(_sock); + } } @@ -89,20 +81,24 @@ size_t WiFiServer::write(uint8_t b) size_t WiFiServer::write(const uint8_t *buffer, size_t size) { - size_t n = 0; + if (size==0) + { + setWriteError(); + return 0; + } - for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + size_t written = ServerDrv::sendData(_sock, buffer, size); + if (!written) { - if (WiFiClass::_server_port[sock] != 0) - { - WiFiClient client(sock); - - if (WiFiClass::_server_port[sock] == _port && - client.status() == ESTABLISHED) - { - n+=client.write(buffer, size); - } - } + setWriteError(); + return 0; + } + + if (!ServerDrv::checkDataSent(_sock)) + { + setWriteError(); + return 0; } - return n; + + return written; } diff --git a/src/WiFiServer.h b/src/WiFiServer.h index d2adea2..2b1c608 100644 --- a/src/WiFiServer.h +++ b/src/WiFiServer.h @@ -1,5 +1,6 @@ /* WiFiServer.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -30,6 +31,7 @@ class WiFiClient; class WiFiServer : public Server { private: + uint8_t _sock; uint16_t _port; void* pcb; public: diff --git a/src/WiFiUdp.cpp b/src/WiFiUdp.cpp index 9540243..5be367b 100644 --- a/src/WiFiUdp.cpp +++ b/src/WiFiUdp.cpp @@ -1,5 +1,6 @@ /* WiFiUdp.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -37,17 +38,28 @@ WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {} /* Start WiFiUDP socket, listening at local port PORT */ uint8_t WiFiUDP::begin(uint16_t port) { - uint8_t sock = WiFiClass::getSocket(); + uint8_t sock = ServerDrv::getSocket(); if (sock != NO_SOCKET_AVAIL) { ServerDrv::startServer(port, sock, UDP_MODE); - WiFiClass::_server_port[sock] = port; _sock = sock; _port = port; return 1; } return 0; +} + +uint8_t WiFiUDP::beginMulticast(IPAddress ip, uint16_t port) { + uint8_t sock = ServerDrv::getSocket(); + if (sock != NO_SOCKET_AVAIL) + { + ServerDrv::startServer(ip, port, sock, UDP_MULTICAST_MODE); + _sock = sock; + _port = port; + return 1; + } + return 0; } /* return number of bytes available in the current packet, @@ -86,11 +98,10 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port) int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) { if (_sock == NO_SOCKET_AVAIL) - _sock = WiFiClass::getSocket(); + _sock = ServerDrv::getSocket(); if (_sock != NO_SOCKET_AVAIL) { ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE); - WiFiClass::_state[_sock] = _sock; return 1; } return 0; @@ -133,10 +144,9 @@ int WiFiUDP::read(unsigned char* buffer, size_t len) { if (available()) { - uint16_t size = 0; + uint16_t size = len; if (!ServerDrv::getDataBuf(_sock, buffer, &size)) return -1; - // TODO check if the buffer is too smal respect to buffer size return size; }else{ return -1; diff --git a/src/WiFiUdp.h b/src/WiFiUdp.h index 039b804..9308cc3 100644 --- a/src/WiFiUdp.h +++ b/src/WiFiUdp.h @@ -1,5 +1,6 @@ /* WiFiUdp.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino LLC. All right reserved. This library is free software; you can redistribute it and/or @@ -32,6 +33,7 @@ class WiFiUDP : public UDP { public: WiFiUDP(); // Constructor virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use + virtual uint8_t beginMulticast(IPAddress, uint16_t); // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 if there are no sockets available to use virtual void stop(); // Finish with the UDP socket // Sending UDP packets diff --git a/src/utility/server_drv.cpp b/src/utility/server_drv.cpp index fc96473..93b9c55 100644 --- a/src/utility/server_drv.cpp +++ b/src/utility/server_drv.cpp @@ -1,5 +1,6 @@ /* server_drv.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -40,8 +41,38 @@ void ServerDrv::startServer(uint16_t port, uint8_t sock, uint8_t protMode) SpiDrv::sendParam(&sock, 1); SpiDrv::sendParam(&protMode, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + +void ServerDrv::startServer(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_4); + SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1); + SpiDrv::sendParam(&protMode, 1, LAST_PARAM); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -64,8 +95,10 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin SpiDrv::sendParam(&sock, 1); SpiDrv::sendParam(&protMode, 1, LAST_PARAM); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -77,6 +110,39 @@ void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uin SpiDrv::spiSlaveDeselect(); } +void ServerDrv::startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_5); + SpiDrv::sendParam((uint8_t*)host, host_len); + SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1); + SpiDrv::sendParam(&protMode, 1, LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 17 + host_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + // Start server TCP on port specified void ServerDrv::stopClient(uint8_t sock) { @@ -85,8 +151,14 @@ void ServerDrv::stopClient(uint8_t sock) SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -106,8 +178,14 @@ uint8_t ServerDrv::getServerState(uint8_t sock) SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -127,8 +205,14 @@ uint8_t ServerDrv::getClientState(uint8_t sock) SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -143,13 +227,23 @@ uint8_t ServerDrv::getClientState(uint8_t sock) uint16_t ServerDrv::availData(uint8_t sock) { + if (!SpiDrv::available()) { + return 0; + } + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -162,6 +256,37 @@ uint16_t ServerDrv::availData(uint8_t sock) return len; } +uint8_t ServerDrv::availServer(uint8_t sock) +{ + if (!SpiDrv::available()) { + return 255; + } + + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _dataLen = 0; + uint16_t socket = 0; + + SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, (uint8_t*)&socket, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return socket; +} + bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) { WAIT_FOR_SLAVE_SELECT(); @@ -170,8 +295,15 @@ bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) SpiDrv::sendParam(&sock, sizeof(sock)); SpiDrv::sendParam(peek, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -193,11 +325,17 @@ bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) { WAIT_FOR_SLAVE_SELECT(); // Send Command - SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM); + SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendBuffer(&sock, sizeof(sock)); + SpiDrv::sendBuffer((uint8_t *)_dataLen, sizeof(*_dataLen), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen)) @@ -220,8 +358,17 @@ bool ServerDrv::insertDataBuf(uint8_t sock, const uint8_t *data, uint16_t _len) SpiDrv::sendBuffer(&sock, sizeof(sock)); SpiDrv::sendBuffer((uint8_t *)data, _len, LAST_PARAM); + // pad to multiple of 4 + int commandSize = 9 + _len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -245,8 +392,14 @@ bool ServerDrv::sendUdpData(uint8_t sock) SpiDrv::sendCmd(SEND_DATA_UDP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -264,7 +417,7 @@ bool ServerDrv::sendUdpData(uint8_t sock) } -bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) +uint16_t ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -272,22 +425,28 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) SpiDrv::sendBuffer(&sock, sizeof(sock)); SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); + // pad to multiple of 4 + int commandSize = 9 + len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply - uint8_t _data = 0; + uint16_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) + if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, (uint8_t*)&_data, &_dataLen)) { WARN("error waitResponse"); } SpiDrv::spiSlaveDeselect(); - if (_dataLen!=0) - { - return (_data == 1); - } - return false; + + return _data; } @@ -304,8 +463,14 @@ uint8_t ServerDrv::checkDataSent(uint8_t sock) SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) @@ -324,4 +489,26 @@ uint8_t ServerDrv::checkDataSent(uint8_t sock) return (timeout==TIMEOUT_DATA_SENT)?0:1; } +uint8_t ServerDrv::getSocket() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_SOCKET_CMD, PARAM_NUMS_0); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = -1; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_SOCKET_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + ServerDrv serverDrv; diff --git a/src/utility/server_drv.h b/src/utility/server_drv.h index 7288e61..0cae252 100644 --- a/src/utility/server_drv.h +++ b/src/utility/server_drv.h @@ -1,5 +1,6 @@ /* server_drv.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -23,7 +24,7 @@ #include #include "utility/wifi_spi.h" -typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode; +typedef enum eProtMode {TCP_MODE, UDP_MODE, TLS_MODE, UDP_MULTICAST_MODE}tProtMode; class ServerDrv { @@ -32,8 +33,12 @@ class ServerDrv // Start server TCP on port specified static void startServer(uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE); + static void startServer(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE); + static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE); + static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE); + static void stopClient(uint8_t sock); static uint8_t getServerState(uint8_t sock); @@ -46,13 +51,17 @@ class ServerDrv static bool insertDataBuf(uint8_t sock, const uint8_t *_data, uint16_t _dataLen); - static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len); + static uint16_t sendData(uint8_t sock, const uint8_t *data, uint16_t len); static bool sendUdpData(uint8_t sock); static uint16_t availData(uint8_t sock); + static uint8_t availServer(uint8_t sock); + static uint8_t checkDataSent(uint8_t sock); + + static uint8_t getSocket(); }; extern ServerDrv serverDrv; diff --git a/src/utility/spi_drv.cpp b/src/utility/spi_drv.cpp index 2bf1bb7..273eef9 100644 --- a/src/utility/spi_drv.cpp +++ b/src/utility/spi_drv.cpp @@ -1,5 +1,6 @@ /* spi_drv.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -19,60 +20,102 @@ #include "Arduino.h" #include -#include "utility/spi_drv.h" +#include "utility/spi_drv.h" #include "pins_arduino.h" //#define _DEBUG_ extern "C" { #include "utility/debug.h" } -#define DATAOUT 11 // MOSI -#define DATAIN 12 // MISO -#define SPICLOCK 13 // sck -#define SLAVESELECT 10 // ss -#define SLAVEREADY 7 // handshake pin -#define WIFILED 9 // led on wifi shield +static uint8_t SLAVESELECT = 10; // ss +static uint8_t SLAVEREADY = 7; // handshake pin +static uint8_t SLAVERESET = 5; // reset pin -#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); } while (++ii < (X*F_CPU/16000000)); } -#define DELAY_TRANSFER() DELAY_SPI(10) +static bool inverted_reset = false; + +#define DELAY_TRANSFER() + +#ifndef SPIWIFI +#define SPIWIFI SPI +#endif + +bool SpiDrv::initialized = false; void SpiDrv::begin() { - SPI.begin(); - pinMode(SLAVESELECT, OUTPUT); - pinMode(SLAVEREADY, INPUT); - pinMode(WIFILED, OUTPUT); - // digitalWrite(SCK, LOW); - // digitalWrite(MOSI, LOW); - digitalWrite(SS, HIGH); - digitalWrite(SLAVESELECT, HIGH); - digitalWrite(WIFILED, LOW); +#ifdef SPIWIFI_SS + SLAVESELECT = SPIWIFI_SS; +#endif + +#ifdef SPIWIFI_ACK + SLAVEREADY = SPIWIFI_ACK; +#endif + +#ifdef SPIWIFI_RESET + SLAVERESET = (uint8_t)SPIWIFI_RESET; +#endif + + if (SLAVERESET > PINS_COUNT) { + inverted_reset = true; + SLAVERESET = ~SLAVERESET; + } + + SPIWIFI.begin(); + pinMode(SLAVESELECT, OUTPUT); + pinMode(SLAVEREADY, INPUT); + pinMode(SLAVERESET, OUTPUT); + pinMode(NINA_GPIO0, OUTPUT); + + digitalWrite(NINA_GPIO0, HIGH); + digitalWrite(SLAVESELECT, HIGH); + digitalWrite(SLAVERESET, inverted_reset ? HIGH : LOW); + delay(10); + digitalWrite(SLAVERESET, inverted_reset ? LOW : HIGH); + delay(750); + + digitalWrite(NINA_GPIO0, LOW); + pinMode(NINA_GPIO0, INPUT); #ifdef _DEBUG_ INIT_TRIGGER() #endif + + initialized = true; } void SpiDrv::end() { - SPI.end(); + digitalWrite(SLAVERESET, inverted_reset ? HIGH : LOW); + + pinMode(SLAVESELECT, INPUT); + pinMode(SLAVERESET, INPUT); + + SPIWIFI.end(); + + initialized = false; } void SpiDrv::spiSlaveSelect() { + SPIWIFI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); digitalWrite(SLAVESELECT,LOW); + + // wait for up to 5 ms for the NINA to indicate it is not ready for transfer + // the timeout is only needed for the case when the shield or module is not present + for (unsigned long start = millis(); (digitalRead(SLAVEREADY) != HIGH) && (millis() - start) < 5;); } void SpiDrv::spiSlaveDeselect() { digitalWrite(SLAVESELECT,HIGH); + SPIWIFI.endTransaction(); } char SpiDrv::spiTransfer(volatile char data) { - char result = SPI.transfer(data); + char result = SPIWIFI.transfer(data); DELAY_TRANSFER(); return result; // return the received byte @@ -157,7 +200,7 @@ int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8 { CHECK_DATA(cmd | REPLY_FLAG, _data){}; - CHECK_DATA(numParam, _data); + CHECK_DATA(numParam, _data) { readParamLen8(param_len); for (ii=0; ii<(*param_len); ++ii) @@ -474,10 +517,6 @@ void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam) // Send Spi START CMD spiTransfer(START_CMD); - //waitForSlaveSign(); - //wait the interrupt trigger on slave - delayMicroseconds(SPI_START_CMD_DELAY); - // Send Spi C + cmd spiTransfer(cmd & ~(REPLY_FLAG)); @@ -493,4 +532,9 @@ void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam) } +int SpiDrv::available() +{ + return (digitalRead(NINA_GPIO0) != LOW); +} + SpiDrv spiDrv; diff --git a/src/utility/spi_drv.h b/src/utility/spi_drv.h index ab8d97d..5fc76ad 100644 --- a/src/utility/spi_drv.h +++ b/src/utility/spi_drv.h @@ -1,5 +1,6 @@ /* spi_drv.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -31,15 +32,12 @@ #define DUMMY_DATA 0xFF #define WAIT_FOR_SLAVE_SELECT() \ - if (!initialized) { \ + if (!SpiDrv::initialized) { \ SpiDrv::begin(); \ - initialized = true; \ } \ SpiDrv::waitForSlaveReady(); \ SpiDrv::spiSlaveSelect(); -static bool initialized = false; - class SpiDrv { private: @@ -47,6 +45,7 @@ class SpiDrv static void waitForSlaveSign(); static void getParam(uint8_t* param); public: + static bool initialized; static void begin(); @@ -99,6 +98,8 @@ class SpiDrv static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM); static void sendCmd(uint8_t cmd, uint8_t numParam); + + static int available(); }; extern SpiDrv spiDrv; diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index bee61bf..3c4777c 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1,5 +1,6 @@ /* wifi_drv.cpp - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -35,8 +36,6 @@ extern "C" { // Array of data to cache the information related to the networks discovered char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; -int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 }; -uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 }; // Cached values of retrieved data char WiFiDrv::_ssid[] = {0}; @@ -63,8 +62,14 @@ void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); @@ -82,8 +87,14 @@ void WiFiDrv::getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port) SpiDrv::sendCmd(GET_REMOTE_DATA_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply SpiDrv::waitResponseParams(GET_REMOTE_DATA_CMD, PARAM_NUMS_2, params); @@ -100,15 +111,29 @@ void WiFiDrv::wifiDriverInit() SpiDrv::begin(); } -int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) +void WiFiDrv::wifiDriverDeinit() +{ + SpiDrv::end(); +} + +int8_t WiFiDrv::wifiSetNetwork(const char* ssid, uint8_t ssid_len) { WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1); SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM); + // pad to multiple of 4 + int commandSize = 5 + ssid_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -123,7 +148,7 @@ int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE; } -int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +int8_t WiFiDrv::wifiSetPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -131,8 +156,17 @@ int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pass SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM); + // pad to multiple of 4 + int commandSize = 6 + ssid_len + len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -147,7 +181,7 @@ int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pass } -int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) +int8_t WiFiDrv::wifiSetKey(const char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -156,8 +190,17 @@ int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM); SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM); + // pad to multiple of 4 + int commandSize = 8 + ssid_len + len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -181,8 +224,15 @@ void WiFiDrv::config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, u SpiDrv::sendParam((uint8_t*)&gateway, 4, NO_LAST_PARAM); SpiDrv::sendParam((uint8_t*)&subnet, 4, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -204,8 +254,10 @@ void WiFiDrv::setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_ser SpiDrv::sendParam((uint8_t*)&dns_server1, 4, NO_LAST_PARAM); SpiDrv::sendParam((uint8_t*)&dns_server2, 4, LAST_PARAM); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -218,7 +270,35 @@ void WiFiDrv::setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_ser SpiDrv::spiSlaveDeselect(); } +void WiFiDrv::setHostname(const char* hostname) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_HOSTNAME_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)hostname, strlen(hostname), LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 5 + strlen(hostname); + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_HOSTNAME_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} int8_t WiFiDrv::disconnect() { @@ -229,8 +309,14 @@ int8_t WiFiDrv::disconnect() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -249,8 +335,10 @@ uint8_t WiFiDrv::getConnectionStatus() // Send Command SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = -1; @@ -271,9 +359,15 @@ uint8_t* WiFiDrv::getMacAddress() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -302,7 +396,7 @@ void WiFiDrv::getIpAddress(IPAddress& ip) ip = _gatewayIp; } -char* WiFiDrv::getCurrentSSID() +const char* WiFiDrv::getCurrentSSID() { WAIT_FOR_SLAVE_SELECT(); @@ -312,8 +406,16 @@ char* WiFiDrv::getCurrentSSID() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + memset(_ssid, 0x00, sizeof(_ssid)); // Wait for reply uint8_t _dataLen = 0; @@ -334,8 +436,14 @@ uint8_t* WiFiDrv::getCurrentBSSID() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -356,8 +464,14 @@ int32_t WiFiDrv::getCurrentRSSI() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -379,8 +493,14 @@ uint8_t WiFiDrv::getCurrentEncryptionType() uint8_t _dummy = DUMMY_DATA; SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t dataLen = 0; @@ -399,8 +519,10 @@ int8_t WiFiDrv::startScanNetworks() // Send Command SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -414,7 +536,7 @@ int8_t WiFiDrv::startScanNetworks() SpiDrv::spiSlaveDeselect(); - return (_data == WL_FAILURE)? _data : WL_SUCCESS; + return ((int8_t)_data == WL_FAILURE)? _data : (int8_t)WL_SUCCESS; } @@ -425,8 +547,10 @@ uint8_t WiFiDrv::getScanNetworks() // Send Command SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t ssidListNum = 0; @@ -437,10 +561,10 @@ uint8_t WiFiDrv::getScanNetworks() return ssidListNum; } -char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) +const char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) { if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; + return (char*)NULL; return _networkSsid[networkItem]; } @@ -448,7 +572,7 @@ char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) { if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; + return ENC_TYPE_UNKNOWN; WAIT_FOR_SLAVE_SELECT(); @@ -457,8 +581,14 @@ uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t dataLen = 0; @@ -470,10 +600,71 @@ uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) return encType; } +uint8_t* WiFiDrv::getBSSIDNetowrks(uint8_t networkItem, uint8_t* bssid) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_BSSID, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t dataLen = 0; + SpiDrv::waitResponseCmd(GET_IDX_BSSID, PARAM_NUMS_1, (uint8_t*)bssid, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return bssid; +} + +uint8_t WiFiDrv::getChannelNetowrks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return 0; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_CHANNEL_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t channel = 0; + SpiDrv::waitResponseCmd(GET_IDX_CHANNEL_CMD, PARAM_NUMS_1, (uint8_t*)&channel, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return channel; +} + int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) { if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; + return 0; int32_t networkRssi = 0; WAIT_FOR_SLAVE_SELECT(); @@ -483,8 +674,14 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t dataLen = 0; @@ -503,8 +700,17 @@ uint8_t WiFiDrv::reqHostByName(const char* aHostname) SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1); SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); + // pad to multiple of 4 + int commandSize = 5 + strlen(aHostname); + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _data = 0; @@ -526,8 +732,10 @@ int WiFiDrv::getHostByName(IPAddress& aResult) // Send Command SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -557,14 +765,16 @@ int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) return (retry>0); } -char* WiFiDrv::getFwVersion() +const char* WiFiDrv::getFwVersion() { WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0); + SpiDrv::spiSlaveDeselect(); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); // Wait for reply uint8_t _dataLen = 0; @@ -576,4 +786,275 @@ char* WiFiDrv::getFwVersion() return fwVersion; } +uint32_t WiFiDrv::getTime() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_TIME_CMD, PARAM_NUMS_0); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _dataLen = 0; + uint32_t _data = 0; + if (!SpiDrv::waitResponseCmd(GET_TIME_CMD, PARAM_NUMS_1, (uint8_t*)&_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +void WiFiDrv::setPowerMode(uint8_t mode) +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(SET_POWER_MODE_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&mode, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t data = 0; + SpiDrv::waitResponseCmd(SET_POWER_MODE_CMD, PARAM_NUMS_1, &data, &dataLen); + + SpiDrv::spiSlaveDeselect(); +} + +int8_t WiFiDrv::wifiSetApNetwork(const char* ssid, uint8_t ssid_len, uint8_t channel) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_AP_NET_CMD, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len); + SpiDrv::sendParam(&channel, 1, LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 3 + ssid_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_AP_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + + return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE; +} + +int8_t WiFiDrv::wifiSetApPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len, uint8_t channel) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_AP_PASSPHRASE_CMD, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)passphrase, len, NO_LAST_PARAM); + SpiDrv::sendParam(&channel, 1, LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 4 + ssid_len + len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_AP_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +int16_t WiFiDrv::ping(uint32_t ipAddress, uint8_t ttl) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(PING_CMD, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress), NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&ttl, sizeof(ttl), LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint16_t _data; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(PING_CMD, PARAM_NUMS_1, (uint8_t*)&_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_PING_ERROR; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +void WiFiDrv::debug(uint8_t on) +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(SET_DEBUG_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&on, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t data = 0; + SpiDrv::waitResponseCmd(SET_DEBUG_CMD, PARAM_NUMS_1, &data, &dataLen); + + SpiDrv::spiSlaveDeselect(); +} + +float WiFiDrv::getTemperature() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_TEMPERATURE_CMD, PARAM_NUMS_0); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _dataLen = 0; + float _data = 0; + if (!SpiDrv::waitResponseCmd(GET_TEMPERATURE_CMD, PARAM_NUMS_1, (uint8_t*)&_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +void WiFiDrv::pinMode(uint8_t pin, uint8_t mode) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_PIN_MODE, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&mode, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_PIN_MODE, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} + +void WiFiDrv::digitalWrite(uint8_t pin, uint8_t value) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_DIGITAL_WRITE, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&value, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_DIGITAL_WRITE, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} + +void WiFiDrv::analogWrite(uint8_t pin, uint8_t value) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_ANALOG_WRITE, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)&pin, 1, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&value, 1, LAST_PARAM); + + // pad to multiple of 4 + SpiDrv::readChar(); + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_ANALOG_WRITE, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} + WiFiDrv wiFiDrv; diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index 423865f..7fca194 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -1,5 +1,6 @@ /* wifi_drv.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -24,6 +25,7 @@ #include "utility/wifi_spi.h" #include "IPAddress.h" #include "WiFiUdp.h" +#include "WiFiClient.h" // Key index length #define KEY_IDX_LEN 1 @@ -37,8 +39,6 @@ class WiFiDrv private: // settings of requested network static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH]; - static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; - static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; // firmware version string in the format a.b.c static char fwVersion[WL_FW_VER_LENGTH]; @@ -72,6 +72,8 @@ class WiFiDrv */ static void wifiDriverInit(); + static void wifiDriverDeinit(); + /* * Set the desired network which the connection manager should try to * connect to. @@ -82,7 +84,7 @@ class WiFiDrv * param ssid_len: Lenght of ssid string. * return: WL_SUCCESS or WL_FAILURE */ - static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + static int8_t wifiSetNetwork(const char* ssid, uint8_t ssid_len); /* Start Wifi connection with passphrase * the most secure supported mode will be automatically selected @@ -94,7 +96,7 @@ class WiFiDrv * param len: Lenght of passphrase string. * return: WL_SUCCESS or WL_FAILURE */ - static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + static int8_t wifiSetPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); /* Start Wifi connection with WEP encryption. * Configure a key into the device. The key type (WEP-40, WEP-104) @@ -107,7 +109,10 @@ class WiFiDrv * param len: Lenght of key string. * return: WL_SUCCESS or WL_FAILURE */ - static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + static int8_t wifiSetKey(const char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + + static int8_t wifiSetApNetwork(const char* ssid, uint8_t ssid_len); + static int8_t wifiSetApPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); /* Set ip configuration disabling dhcp client * @@ -130,6 +135,8 @@ class WiFiDrv */ static void setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2); + static void setHostname(const char* hostname); + /* * Disconnect from the network * @@ -177,7 +184,7 @@ class WiFiDrv * * return: ssid string */ - static char* getCurrentSSID(); + static const char* getCurrentSSID(); /* * Return the current BSSID associated with the network. @@ -223,7 +230,7 @@ class WiFiDrv * * return: ssid string of the specified item on the networks scanned list */ - static char* getSSIDNetoworks(uint8_t networkItem); + static const char* getSSIDNetoworks(uint8_t networkItem); /* * Return the RSSI of the networks discovered during the scanNetworks @@ -243,6 +250,10 @@ class WiFiDrv */ static uint8_t getEncTypeNetowrks(uint8_t networkItem); + static uint8_t* getBSSIDNetowrks(uint8_t networkItem, uint8_t* bssid); + + static uint8_t getChannelNetowrks(uint8_t networkItem); + /* * Resolve the given hostname to an IP address. * param aHostname: Name to be resolved @@ -256,10 +267,25 @@ class WiFiDrv * Get the firmware version * result: version as string with this format a.b.c */ - static char* getFwVersion(); + static const char* getFwVersion(); - friend class WiFiUDP; + static uint32_t getTime(); + static void setPowerMode(uint8_t mode); + + static int8_t wifiSetApNetwork(const char* ssid, uint8_t ssid_len, uint8_t channel); + static int8_t wifiSetApPassphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len, uint8_t channel); + + static int16_t ping(uint32_t ipAddress, uint8_t ttl); + + static void debug(uint8_t on); + static float getTemperature(); + static void pinMode(uint8_t pin, uint8_t mode); + static void digitalWrite(uint8_t pin, uint8_t value); + static void analogWrite(uint8_t pin, uint8_t value); + + friend class WiFiUDP; + friend class WiFiClient; }; extern WiFiDrv wiFiDrv; diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index d0cc754..9d5e117 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -1,5 +1,6 @@ /* wifi_spi.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -46,9 +47,15 @@ enum { SET_NET_CMD = 0x10, SET_PASSPHRASE_CMD = 0x11, SET_KEY_CMD = 0x12, - TEST_CMD = 0x13, +// TEST_CMD = 0x13, SET_IP_CONFIG_CMD = 0x14, SET_DNS_CONFIG_CMD = 0x15, + SET_HOSTNAME_CMD = 0x16, + SET_POWER_MODE_CMD = 0x17, + SET_AP_NET_CMD = 0x18, + SET_AP_PASSPHRASE_CMD = 0x19, + SET_DEBUG_CMD = 0x1A, + GET_TEMPERATURE_CMD = 0x1B, GET_CONN_STATUS_CMD = 0x20, GET_IPADDR_CMD = 0x21, @@ -67,22 +74,32 @@ enum { STOP_CLIENT_TCP_CMD = 0x2E, GET_CLIENT_STATE_TCP_CMD= 0x2F, DISCONNECT_CMD = 0x30, - GET_IDX_SSID_CMD = 0x31, +// GET_IDX_SSID_CMD = 0x31, GET_IDX_RSSI_CMD = 0x32, GET_IDX_ENCT_CMD = 0x33, REQ_HOST_BY_NAME_CMD= 0x34, GET_HOST_BY_NAME_CMD= 0x35, START_SCAN_NETWORKS = 0x36, GET_FW_VERSION_CMD = 0x37, - GET_TEST_CMD = 0x38, +// GET_TEST_CMD = 0x38, SEND_DATA_UDP_CMD = 0x39, GET_REMOTE_DATA_CMD = 0x3A, + GET_TIME_CMD = 0x3B, + GET_IDX_BSSID = 0x3C, + GET_IDX_CHANNEL_CMD = 0x3D, + PING_CMD = 0x3E, + GET_SOCKET_CMD = 0x3F, // All command with DATA_FLAG 0x40 send a 16bit Len SEND_DATA_TCP_CMD = 0x44, GET_DATABUF_TCP_CMD = 0x45, INSERT_DATABUF_CMD = 0x46, + + // regular format commands + SET_PIN_MODE = 0x50, + SET_DIGITAL_WRITE = 0x51, + SET_ANALOG_WRITE = 0x52, }; diff --git a/src/utility/wl_definitions.h b/src/utility/wl_definitions.h index 70d1de4..1ab8a61 100644 --- a/src/utility/wl_definitions.h +++ b/src/utility/wl_definitions.h @@ -1,5 +1,6 @@ /* wl_definitions.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -49,13 +50,17 @@ typedef enum { WL_NO_SHIELD = 255, + WL_NO_MODULE = WL_NO_SHIELD, WL_IDLE_STATUS = 0, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED, WL_CONNECTED, WL_CONNECT_FAILED, WL_CONNECTION_LOST, - WL_DISCONNECTED + WL_DISCONNECTED, + WL_AP_LISTENING, + WL_AP_CONNECTED, + WL_AP_FAILED } wl_status_t; /* Encryption modes */ @@ -65,7 +70,9 @@ enum wl_enc_type { /* Values map to 802.11 encryption suites... */ ENC_TYPE_CCMP = 4, /* ... except these two, 7 and 8 are reserved in 802.11-2007 */ ENC_TYPE_NONE = 7, - ENC_TYPE_AUTO = 8 + ENC_TYPE_AUTO = 8, + + ENC_TYPE_UNKNOWN = 255 }; diff --git a/src/utility/wl_types.h b/src/utility/wl_types.h index b9fd5fa..0f2cfd7 100644 --- a/src/utility/wl_types.h +++ b/src/utility/wl_types.h @@ -1,5 +1,6 @@ /* wl_types.h - Library for Arduino Wifi shield. + Copyright (C) 2018 Arduino AG (http://www.arduino.cc/) Copyright (c) 2011-2014 Arduino. All right reserved. This library is free software; you can redistribute it and/or @@ -46,4 +47,11 @@ enum wl_auth_mode { AUTH_MODE_WPA2_PSK }; +typedef enum { + WL_PING_DEST_UNREACHABLE = -1, + WL_PING_TIMEOUT = -2, + WL_PING_UNKNOWN_HOST = -3, + WL_PING_ERROR = -4 +} wl_ping_result_t; + #endif //_WL_TYPES_H_