diff options
author | Cristian Maglie <c.maglie@bug.st> | 2013-04-03 13:51:04 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2013-04-03 13:51:04 +0200 |
commit | ee90e68e86dd61d86f5d17b69080338328765b22 (patch) | |
tree | e620c0edc2690ab789b665e567910640597aa6fe /libraries/WiFi | |
parent | 0ecdc5ebc96ad4c7c548c438a03d9ce00679db8b (diff) | |
parent | f50c307be280dc6ece9e70c43b301c1db36291a0 (diff) |
Merged 1.0.5
Merge remote-tracking branch 'arduino/master' into ide-1.5.x
Conflicts:
app/src/processing/app/Base.java
build/shared/revisions.txt
hardware/arduino/avr/cores/arduino/malloc.c
hardware/arduino/cores/arduino/avr-libc/malloc.c
hardware/arduino/cores/arduino/malloc.c
todo.txt
Diffstat (limited to 'libraries/WiFi')
-rw-r--r-- | libraries/WiFi/WiFi.cpp | 38 | ||||
-rw-r--r-- | libraries/WiFi/WiFi.h | 44 | ||||
-rw-r--r-- | libraries/WiFi/WiFiClient.cpp | 14 | ||||
-rw-r--r-- | libraries/WiFi/WiFiServer.cpp | 1 | ||||
-rw-r--r-- | libraries/WiFi/WiFiUdp.cpp | 163 | ||||
-rw-r--r-- | libraries/WiFi/WiFiUdp.h | 61 | ||||
-rw-r--r-- | libraries/WiFi/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino | 182 | ||||
-rw-r--r-- | libraries/WiFi/examples/WifiUdpSendReceiveString/WifiUdpSendReceiveString.ino | 112 | ||||
-rw-r--r-- | libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino | 4 | ||||
-rw-r--r-- | libraries/WiFi/keywords.txt | 8 | ||||
-rw-r--r-- | libraries/WiFi/utility/server_drv.cpp | 484 | ||||
-rw-r--r-- | libraries/WiFi/utility/server_drv.h | 75 | ||||
-rw-r--r-- | libraries/WiFi/utility/spi_drv.h | 166 | ||||
-rw-r--r-- | libraries/WiFi/utility/wifi_drv.cpp | 1021 | ||||
-rw-r--r-- | libraries/WiFi/utility/wifi_drv.h | 467 | ||||
-rw-r--r-- | libraries/WiFi/utility/wifi_spi.h | 297 | ||||
-rw-r--r-- | libraries/WiFi/utility/wl_definitions.h | 2 |
17 files changed, 1954 insertions, 1185 deletions
diff --git a/libraries/WiFi/WiFi.cpp b/libraries/WiFi/WiFi.cpp index c0cb001..f209280 100644 --- a/libraries/WiFi/WiFi.cpp +++ b/libraries/WiFi/WiFi.cpp @@ -8,7 +8,7 @@ extern "C" { } // XXX: don't make assumptions about the value of MAX_SOCK_NUM. -int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; +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() @@ -71,8 +71,7 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) { delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); - } - while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ status = WL_CONNECT_FAILED; } @@ -99,6 +98,39 @@ int WiFiClass::begin(char* ssid, const char *passphrase) return status; } +void WiFiClass::config(IPAddress local_ip) +{ + WiFiDrv::config(1, (uint32_t)local_ip, 0, 0); +} + +void WiFiClass::config(IPAddress local_ip, IPAddress dns_server) +{ + WiFiDrv::config(1, (uint32_t)local_ip, 0, 0); + WiFiDrv::setDNS(1, (uint32_t)dns_server, 0); +} + +void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) +{ + WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0); + WiFiDrv::setDNS(1, (uint32_t)dns_server, 0); +} + +void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) +{ + WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet); + WiFiDrv::setDNS(1, (uint32_t)dns_server, 0); +} + +void WiFiClass::setDNS(IPAddress dns_server1) +{ + WiFiDrv::setDNS(1, (uint32_t)dns_server1, 0); +} + +void WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2) +{ + WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2); +} + int WiFiClass::disconnect() { return WiFiDrv::disconnect(); diff --git a/libraries/WiFi/WiFi.h b/libraries/WiFi/WiFi.h index 9a86701..ef36a84 100644 --- a/libraries/WiFi/WiFi.h +++ b/libraries/WiFi/WiFi.h @@ -59,6 +59,50 @@ public: */ int begin(char* ssid, const char *passphrase); + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + */ + void config(IPAddress local_ip); + + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param dns_server: IP configuration for DNS server 1 + */ + void config(IPAddress local_ip, IPAddress dns_server); + + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param dns_server: IP configuration for DNS server 1 + * param gateway : Static gateway configuration + */ + void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway); + + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param dns_server: IP configuration for DNS server 1 + * param gateway: Static gateway configuration + * param subnet: Static Subnet mask + */ + void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); + + /* Change DNS Ip configuration + * + * param dns_server1: ip configuration for DNS server 1 + */ + void setDNS(IPAddress dns_server1); + + /* Change DNS Ip configuration + * + * param dns_server1: ip configuration for DNS server 1 + * param dns_server2: ip configuration for DNS server 2 + * + */ + void setDNS(IPAddress dns_server1, IPAddress dns_server2); + /* * Disconnect from the network * diff --git a/libraries/WiFi/WiFiClient.cpp b/libraries/WiFi/WiFiClient.cpp index 83c0d10..0b4b6dc 100644 --- a/libraries/WiFi/WiFiClient.cpp +++ b/libraries/WiFi/WiFiClient.cpp @@ -129,13 +129,13 @@ void WiFiClient::stop() { return; ServerDrv::stopClient(_sock); + WiFiClass::_state[_sock] = NA_STATE; - unsigned long start = millis(); - + int count = 0; + // wait maximum 5 secs for the connection to close + while (status() != CLOSED && ++count < 50) + delay(100); - // wait a second for the connection to close - while (status() != CLOSED && millis() - start < 1000) - delay(1); _sock = 255; } @@ -149,7 +149,7 @@ uint8_t WiFiClient::connected() { return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || s == TIME_WAIT || s == SYN_SENT || s== SYN_RCVD || - (s == CLOSE_WAIT && !available())); + (s == CLOSE_WAIT)); } } @@ -169,7 +169,7 @@ WiFiClient::operator bool() { uint8_t WiFiClient::getFirstSocket() { for (int i = 0; i < MAX_SOCK_NUM; i++) { - if (WiFiClass::_state[i] == 0) + if (WiFiClass::_state[i] == NA_STATE) { return i; } diff --git a/libraries/WiFi/WiFiServer.cpp b/libraries/WiFi/WiFiServer.cpp index 77dbac0..2f03bc1 100644 --- a/libraries/WiFi/WiFiServer.cpp +++ b/libraries/WiFi/WiFiServer.cpp @@ -21,6 +21,7 @@ void WiFiServer::begin() { ServerDrv::startServer(_port, _sock); WiFiClass::_server_port[_sock] = _port; + WiFiClass::_state[_sock] = _sock; } } diff --git a/libraries/WiFi/WiFiUdp.cpp b/libraries/WiFi/WiFiUdp.cpp new file mode 100644 index 0000000..7020df8 --- /dev/null +++ b/libraries/WiFi/WiFiUdp.cpp @@ -0,0 +1,163 @@ + +extern "C" { + #include "utility/debug.h" + #include "utility/wifi_spi.h" +} +#include <string.h> +#include "server_drv.h" +#include "wifi_drv.h" + +#include "WiFi.h" +#include "WiFiUdp.h" +#include "WiFiClient.h" +#include "WiFiServer.h" + + +/* Constructor */ +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(); + if (sock != NO_SOCKET_AVAIL) + { + ServerDrv::startServer(port, sock, UDP_MODE); + WiFiClass::_server_port[sock] = port; + _sock = sock; + _port = port; + return 1; + } + return 0; + +} + +/* return number of bytes available in the current packet, + will return zero if parsePacket hasn't been called yet */ +int WiFiUDP::available() { + if (_sock != NO_SOCKET_AVAIL) + { + return ServerDrv::availData(_sock); + } + return 0; +} + +/* Release any resources being used by this WiFiUDP instance */ +void WiFiUDP::stop() +{ + if (_sock == NO_SOCKET_AVAIL) + return; + + ServerDrv::stopClient(_sock); + + _sock = NO_SOCKET_AVAIL; +} + +int WiFiUDP::beginPacket(const char *host, uint16_t port) +{ + // Look up the host first + int ret = 0; + IPAddress remote_addr; + if (WiFi.hostByName(host, remote_addr)) + { + return beginPacket(remote_addr, port); + } + return ret; +} + +int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) +{ + if (_sock == NO_SOCKET_AVAIL) + _sock = WiFiClass::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE); + WiFiClass::_state[_sock] = _sock; + return 1; + } + return 0; +} + +int WiFiUDP::endPacket() +{ + return ServerDrv::sendUdpData(_sock); +} + +size_t WiFiUDP::write(uint8_t byte) +{ + return write(&byte, 1); +} + +size_t WiFiUDP::write(const uint8_t *buffer, size_t size) +{ + ServerDrv::insertDataBuf(_sock, buffer, size); + return size; +} + +int WiFiUDP::parsePacket() +{ + return available(); +} + +int WiFiUDP::read() +{ + uint8_t b; + if (available()) + { + ServerDrv::getData(_sock, &b); + return b; + }else{ + return -1; + } +} + +int WiFiUDP::read(unsigned char* buffer, size_t len) +{ + if (available()) + { + size_t size = 0; + 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; + } +} + +int WiFiUDP::peek() +{ + uint8_t b; + if (!available()) + return -1; + + ServerDrv::getData(_sock, &b, 1); + return b; +} + +void WiFiUDP::flush() +{ + while (available()) + read(); +} + +IPAddress WiFiUDP::remoteIP() +{ + uint8_t _remoteIp[4] = {0}; + uint8_t _remotePort[2] = {0}; + + WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort); + IPAddress ip(_remoteIp); + return ip; +} + +uint16_t WiFiUDP::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/libraries/WiFi/WiFiUdp.h b/libraries/WiFi/WiFiUdp.h new file mode 100644 index 0000000..1b31693 --- /dev/null +++ b/libraries/WiFi/WiFiUdp.h @@ -0,0 +1,61 @@ +#ifndef wifiudp_h +#define wifiudp_h + +#include <Udp.h> + +#define UDP_TX_PACKET_MAX_SIZE 24 + +class WiFiUDP : public UDP { +private: + uint8_t _sock; // socket ID for Wiz5100 + uint16_t _port; // local port to listen on + +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 void stop(); // Finish with the UDP socket + + // Sending UDP packets + + // Start building up a packet to send to the remote host specific in ip and port + // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port + virtual int beginPacket(IPAddress ip, uint16_t port); + // Start building up a packet to send to the remote host specific in host and port + // Returns 1 if successful, 0 if there was a problem resolving the hostname or port + virtual int beginPacket(const char *host, uint16_t port); + // Finish off this packet and send it + // Returns 1 if the packet was sent successfully, 0 if there was an error + virtual int endPacket(); + // Write a single byte into the packet + virtual size_t write(uint8_t); + // Write size bytes from buffer into the packet + virtual size_t write(const uint8_t *buffer, size_t size); + + using Print::write; + + // Start processing the next available incoming packet + // Returns the size of the packet in bytes, or 0 if no packets are available + virtual int parsePacket(); + // Number of bytes remaining in the current packet + virtual int available(); + // Read a single byte from the current packet + virtual int read(); + // Read up to len bytes from the current packet and place them into buffer + // Returns the number of bytes read, or 0 if none are available + virtual int read(unsigned char* buffer, size_t len); + // Read up to len characters from the current packet and place them into buffer + // Returns the number of characters read, or 0 if none are available + virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); }; + // Return the next byte from the current packet without moving on to the next byte + virtual int peek(); + virtual void flush(); // Finish reading the current packet + + // Return the IP address of the host who sent the current incoming packet + virtual IPAddress remoteIP(); + // Return the port of the host who sent the current incoming packet + virtual uint16_t remotePort(); + + friend class WiFiDrv; +}; + +#endif diff --git a/libraries/WiFi/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino b/libraries/WiFi/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino new file mode 100644 index 0000000..dd8b003 --- /dev/null +++ b/libraries/WiFi/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino @@ -0,0 +1,182 @@ +/* + + Udp NTP Client + + Get the time from a Network Time Protocol (NTP) time server + Demonstrates use of UDP sendPacket and ReceivePacket + For more on NTP time servers and the messages needed to communicate with them, + see http://en.wikipedia.org/wiki/Network_Time_Protocol + + created 4 Sep 2010 + by Michael Margolis + modified 9 Apr 2012 + by Tom Igoe + + This code is in the public domain. + + */ + +#include <SPI.h> +#include <WiFi.h> +#include <WiFiUdp.h> + +int status = WL_IDLE_STATUS; +char ssid[] = "mynetwork"; // your network SSID (name) +char pass[] = "mypassword"; // your network password +int keyIndex = 0; // your network key Index number (needed only for WEP) + +unsigned int localPort = 2390; // local port to listen for UDP packets + +IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server + +const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message + +byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets + +// A UDP instance to let us send and receive packets over UDP +WiFiUDP Udp; + +void setup() +{ + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + 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, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + Serial.println("Connected to wifi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + Udp.begin(localPort); +} + +void loop() +{ + sendNTPpacket(timeServer); // send an NTP packet to a time server + // wait to see if a reply is available + delay(1000); + Serial.println( Udp.parsePacket() ); + if ( Udp.parsePacket() ) { + Serial.println("packet received"); + // We've received a packet, read the data from it + Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer + + //the timestamp starts at byte 40 of the received packet and is four bytes, + // or two words, long. First, esxtract the two words: + + unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); + unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); + // combine the four bytes (two words) into a long integer + // this is NTP time (seconds since Jan 1 1900): + unsigned long secsSince1900 = highWord << 16 | lowWord; + Serial.print("Seconds since Jan 1 1900 = " ); + Serial.println(secsSince1900); + + // now convert NTP time into everyday time: + Serial.print("Unix time = "); + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: + const unsigned long seventyYears = 2208988800UL; + // subtract seventy years: + unsigned long epoch = secsSince1900 - seventyYears; + // print Unix time: + Serial.println(epoch); + + + // print the hour, minute and second: + Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) + Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) + Serial.print(':'); + if ( ((epoch % 3600) / 60) < 10 ) { + // In the first 10 minutes of each hour, we'll want a leading '0' + Serial.print('0'); + } + Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) + Serial.print(':'); + if ( (epoch % 60) < 10 ) { + // In the first 10 seconds of each minute, we'll want a leading '0' + Serial.print('0'); + } + Serial.println(epoch %60); // print the second + } + // wait ten seconds before asking for the time again + delay(10000); +} + +// send an NTP request to the time server at the given address +unsigned long sendNTPpacket(IPAddress& address) +{ + //Serial.println("1"); + // set all bytes in the buffer to 0 + memset(packetBuffer, 0, NTP_PACKET_SIZE); + // Initialize values needed to form NTP request + // (see URL above for details on the packets) + //Serial.println("2"); + packetBuffer[0] = 0b11100011; // LI, Version, Mode + packetBuffer[1] = 0; // Stratum, or type of clock + packetBuffer[2] = 6; // Polling Interval + packetBuffer[3] = 0xEC; // Peer Clock Precision + // 8 bytes of zero for Root Delay & Root Dispersion + packetBuffer[12] = 49; + packetBuffer[13] = 0x4E; + packetBuffer[14] = 49; + packetBuffer[15] = 52; + + //Serial.println("3"); + + // all NTP fields have been given values, now + // you can send a packet requesting a timestamp: + Udp.beginPacket(address, 123); //NTP requests are to port 123 + //Serial.println("4"); + Udp.write(packetBuffer,NTP_PACKET_SIZE); + //Serial.println("5"); + Udp.endPacket(); + //Serial.println("6"); +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + + + + + + + diff --git a/libraries/WiFi/examples/WifiUdpSendReceiveString/WifiUdpSendReceiveString.ino b/libraries/WiFi/examples/WifiUdpSendReceiveString/WifiUdpSendReceiveString.ino new file mode 100644 index 0000000..eb11295 --- /dev/null +++ b/libraries/WiFi/examples/WifiUdpSendReceiveString/WifiUdpSendReceiveString.ino @@ -0,0 +1,112 @@ + +/* + WiFi UDP Send and Receive String + + This sketch wait an UDP packet on localPort using a WiFi shield. + 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) + + */ + + +#include <SPI.h> +#include <WiFi.h> +#include <WiFiUdp.h> + +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) +int keyIndex = 0; // your network key Index number (needed only for WEP) + +unsigned int localPort = 2390; // local port to listen on + +char packetBuffer[255]; //buffer to hold incoming packet +char ReplyBuffer[] = "acknowledged"; // a string to send back + +WiFiUDP Udp; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + 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); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to wifi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + Udp.begin(localPort); +} + +void loop() { + + // if there's data available, read a packet + int packetSize = Udp.parsePacket(); + if(packetSize) + { + Serial.print("Received packet of size "); + Serial.println(packetSize); + Serial.print("From "); + IPAddress remoteIp = Udp.remoteIP(); + Serial.print(remoteIp); + Serial.print(", port "); + Serial.println(Udp.remotePort()); + + // read the packet into packetBufffer + int len = Udp.read(packetBuffer,255); + if (len >0) packetBuffer[len]=0; + Serial.println("Contents:"); + Serial.println(packetBuffer); + + // send a reply, to the IP address and port that sent us the packet we received + Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); + Udp.write(ReplyBuffer); + Udp.endPacket(); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + diff --git a/libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino b/libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino index a514e15..7d7a247 100644 --- a/libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -1,5 +1,5 @@ /* - Web Server + WiFi Web Server A simple web server that shows the value of the analog input pins. using a WiFi shield. @@ -15,7 +15,9 @@ by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe + */ + #include <SPI.h> #include <WiFi.h> diff --git a/libraries/WiFi/keywords.txt b/libraries/WiFi/keywords.txt index 47704cd..4106a7d 100644 --- a/libraries/WiFi/keywords.txt +++ b/libraries/WiFi/keywords.txt @@ -18,6 +18,8 @@ status KEYWORD2 connect KEYWORD2 write KEYWORD2 available KEYWORD2 +config KEYWORD2 +setDNS KEYWORD2 read KEYWORD2 flush KEYWORD2 stop KEYWORD2 @@ -36,6 +38,12 @@ getResult KEYWORD2 getSocket KEYWORD2 WiFiClient KEYWORD2 WiFiServer KEYWORD2 +WiFiUDP KEYWORD2 +beginPacket KEYWORD2 +endPacket KEYWORD2 +parsePacket KEYWORD2 +remoteIP KEYWORD2 +remotePort KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/libraries/WiFi/utility/server_drv.cpp b/libraries/WiFi/utility/server_drv.cpp index ce03604..4a6d293 100644 --- a/libraries/WiFi/utility/server_drv.cpp +++ b/libraries/WiFi/utility/server_drv.cpp @@ -1,131 +1,205 @@ -//#define _DEBUG_
-
-#include "server_drv.h"
-
-#include "Arduino.h"
-#include "spi_drv.h"
-
-extern "C" {
-#include "wl_types.h"
-#include "debug.h"
-}
-
-
-// Start server TCP on port specified
-void ServerDrv::startServer(uint16_t port, uint8_t sock)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2);
- SpiDrv::sendParam(port);
- SpiDrv::sendParam(&sock, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // 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();
-}
-
-// Start server TCP on port specified
-void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock)
+//#define _DEBUG_ + +#include "server_drv.h" + +#include "Arduino.h" +#include "spi_drv.h" + +extern "C" { +#include "wl_types.h" +#include "debug.h" +} + + +// Start server TCP on port specified +void ServerDrv::startServer(uint16_t port, uint8_t sock, uint8_t protMode)
+{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_3);
+ SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1);
+ SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
+ + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // 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(); +} + +// Start server TCP on port specified +void ServerDrv::startClient(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_4);
+ SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1);
+ SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
+ + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // 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) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + + +uint8_t ServerDrv::getServerState(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +uint8_t ServerDrv::getClientState(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +uint16_t ServerDrv::availData(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + uint16_t len = 0; + + SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, (uint8_t*)&len, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return len; +} + +bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(&sock, sizeof(sock)); + SpiDrv::sendParam(peek, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (_dataLen!=0) + { + *data = _data; + return true; + } + return false; +} + +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); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (*_dataLen!=0) + { + return true; + } + return false; +} + +bool ServerDrv::insertDataBuf(uint8_t sock, const uint8_t *data, uint16_t _len)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
- SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3);
- SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
- SpiDrv::sendParam(port);
- SpiDrv::sendParam(&sock, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // 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)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam(&sock, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
-}
-
-
-uint8_t ServerDrv::getServerState(uint8_t sock)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
- return _data;
-}
-
-uint8_t ServerDrv::getClientState(uint8_t sock)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
- return _data;
-}
-
-uint8_t ServerDrv::availData(uint8_t sock)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
+ SpiDrv::sendCmd(INSERT_DATABUF_CMD, PARAM_NUMS_2);
+ SpiDrv::sendBuffer(&sock, sizeof(sock));
+ SpiDrv::sendBuffer((uint8_t *)data, _len, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
@@ -133,12 +207,11 @@ uint8_t ServerDrv::availData(uint8_t sock) // Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
+ if (!SpiDrv::waitResponseData8(INSERT_DATABUF_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
-
if (_dataLen!=0)
{
return (_data == 1);
@@ -146,64 +219,12 @@ uint8_t ServerDrv::availData(uint8_t sock) return false;
}
-bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2);
- SpiDrv::sendParam(&sock, sizeof(sock));
- SpiDrv::sendParam(peek, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
- if (_dataLen!=0)
- {
- *data = _data;
- return true;
- }
- return false;
-}
-
-bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen)
+bool ServerDrv::sendUdpData(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
- SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
- if (*_dataLen!=0)
- {
- return true;
- }
- return false;
-}
-
-
-bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2);
- SpiDrv::sendBuffer(&sock, sizeof(sock));
- SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM);
+ SpiDrv::sendCmd(SEND_DATA_UDP_CMD, PARAM_NUMS_1);
+ SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
@@ -211,7 +232,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) // Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen))
+ if (!SpiDrv::waitResponseData8(SEND_DATA_UDP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
@@ -223,38 +244,65 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) return false;
}
-
-uint8_t ServerDrv::checkDataSent(uint8_t sock)
-{
- const uint16_t TIMEOUT_DATA_SENT = 25;
- uint16_t timeout = 0;
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
-
- do {
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse isDataSent");
- }
- SpiDrv::spiSlaveDeselect();
-
- if (_data) timeout = 0;
- else{
- ++timeout;
- delay(100);
- }
-
- }while((_data==0)&&(timeout<TIMEOUT_DATA_SENT));
- return (timeout==TIMEOUT_DATA_SENT)?0:1;
-}
-
-ServerDrv serverDrv;
+ +bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendBuffer(&sock, sizeof(sock)); + SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (_dataLen!=0) + { + return (_data == 1); + } + return false; +} + + +uint8_t ServerDrv::checkDataSent(uint8_t sock) +{ + const uint16_t TIMEOUT_DATA_SENT = 25; + uint16_t timeout = 0; + uint8_t _data = 0; + uint8_t _dataLen = 0; + + do { + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse isDataSent"); + } + SpiDrv::spiSlaveDeselect(); + + if (_data) timeout = 0; + else{ + ++timeout; + delay(100); + } + + }while((_data==0)&&(timeout<TIMEOUT_DATA_SENT)); + return (timeout==TIMEOUT_DATA_SENT)?0:1; +} + +ServerDrv serverDrv; diff --git a/libraries/WiFi/utility/server_drv.h b/libraries/WiFi/utility/server_drv.h index 69ba593..50ba7e3 100644 --- a/libraries/WiFi/utility/server_drv.h +++ b/libraries/WiFi/utility/server_drv.h @@ -1,34 +1,41 @@ -#ifndef Server_Drv_h
-#define Server_Drv_h
-
-#include <inttypes.h>
-#include "wifi_spi.h"
-
-class ServerDrv
-{
-public:
- // Start server TCP on port specified
- static void startServer(uint16_t port, uint8_t sock);
-
- static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock);
-
- static void stopClient(uint8_t sock);
-
- static uint8_t getServerState(uint8_t sock);
-
- static uint8_t getClientState(uint8_t sock);
-
- static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0);
-
- static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len);
-
- static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len);
-
- static uint8_t availData(uint8_t sock);
-
- static uint8_t checkDataSent(uint8_t sock);
-};
-
-extern ServerDrv serverDrv;
-
-#endif
+#ifndef Server_Drv_h +#define Server_Drv_h + +#include <inttypes.h> +#include "wifi_spi.h" + +typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode;
+
+class ServerDrv +{ +public: +
+ // Start server TCP on port specified + static void startServer(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 stopClient(uint8_t sock); + + static uint8_t getServerState(uint8_t sock); + + static uint8_t getClientState(uint8_t sock); + + static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0); + + static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len); + + 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 bool sendUdpData(uint8_t sock);
+
+ static uint16_t availData(uint8_t sock); + + static uint8_t checkDataSent(uint8_t sock); +}; + +extern ServerDrv serverDrv; + +#endif diff --git a/libraries/WiFi/utility/spi_drv.h b/libraries/WiFi/utility/spi_drv.h index 5c2e706..b7e4cb7 100644 --- a/libraries/WiFi/utility/spi_drv.h +++ b/libraries/WiFi/utility/spi_drv.h @@ -1,83 +1,83 @@ -#ifndef SPI_Drv_h
-#define SPI_Drv_h
-
-#include <inttypes.h>
-#include "wifi_spi.h"
-
-#define SPI_START_CMD_DELAY 12
-
-#define NO_LAST_PARAM 0
-#define LAST_PARAM 1
-
-#define DUMMY_DATA 0xFF
-
-#define WAIT_FOR_SLAVE_SELECT() \
- SpiDrv::waitForSlaveReady(); \
- SpiDrv::spiSlaveSelect();
-
-
-
-class SpiDrv
-{
-private:
- //static bool waitSlaveReady();
- static void waitForSlaveSign();
- static void getParam(uint8_t* param);
-public:
-
- static void begin();
-
- static void end();
-
- static void spiDriverInit();
-
- static void spiSlaveSelect();
-
- static void spiSlaveDeselect();
-
- static char spiTransfer(volatile char data);
-
- static void waitForSlaveReady();
-
- //static int waitSpiChar(char waitChar, char* readChar);
-
- static int waitSpiChar(unsigned char waitChar);
-
- static int readAndCheckChar(char checkChar, char* readChar);
-
- static char readChar();
-
- static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params);
-
- static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len);
-
- static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len);
-
- static int waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len);
- /*
- static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams);
-
- static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len);
-*/
- static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams);
-
- static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM);
-
- static void sendParamLen8(uint8_t param_len);
-
- static void sendParamLen16(uint16_t param_len);
-
- static uint8_t readParamLen8(uint8_t* param_len = NULL);
-
- static uint16_t readParamLen16(uint16_t* param_len = NULL);
-
- static void sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam = NO_LAST_PARAM);
-
- static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM);
-
- static void sendCmd(uint8_t cmd, uint8_t numParam);
-};
-
-extern SpiDrv spiDrv;
-
-#endif
+#ifndef SPI_Drv_h +#define SPI_Drv_h + +#include <inttypes.h> +#include "wifi_spi.h" + +#define SPI_START_CMD_DELAY 10
+ +#define NO_LAST_PARAM 0 +#define LAST_PARAM 1 + +#define DUMMY_DATA 0xFF + +#define WAIT_FOR_SLAVE_SELECT() \ + SpiDrv::waitForSlaveReady(); \ + SpiDrv::spiSlaveSelect(); + + + +class SpiDrv +{ +private: + //static bool waitSlaveReady(); + static void waitForSlaveSign(); + static void getParam(uint8_t* param); +public: + + static void begin(); + + static void end(); + + static void spiDriverInit(); + + static void spiSlaveSelect(); + + static void spiSlaveDeselect(); + + static char spiTransfer(volatile char data); + + static void waitForSlaveReady(); + + //static int waitSpiChar(char waitChar, char* readChar); + + static int waitSpiChar(unsigned char waitChar); + + static int readAndCheckChar(char checkChar, char* readChar); + + static char readChar(); + + static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params); + + static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); + + static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len); + + static int waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len); + /* + static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams); + + static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len); +*/ + static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams); + + static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM); + + static void sendParamLen8(uint8_t param_len); + + static void sendParamLen16(uint16_t param_len); + + static uint8_t readParamLen8(uint8_t* param_len = NULL); + + static uint16_t readParamLen16(uint16_t* param_len = NULL); + + static void sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam = NO_LAST_PARAM); + + static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM); + + static void sendCmd(uint8_t cmd, uint8_t numParam); +}; + +extern SpiDrv spiDrv; + +#endif diff --git a/libraries/WiFi/utility/wifi_drv.cpp b/libraries/WiFi/utility/wifi_drv.cpp index 1ca1696..ccd5f25 100644 --- a/libraries/WiFi/utility/wifi_drv.cpp +++ b/libraries/WiFi/utility/wifi_drv.cpp @@ -1,491 +1,560 @@ -#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-
-#include "Arduino.h"
-#include "spi_drv.h"
-#include "wifi_drv.h"
-
-#define _DEBUG_
-
-extern "C" {
-#include "wifi_spi.h"
-#include "wl_types.h"
-#include "debug.h"
-}
-
-// 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};
-uint8_t WiFiDrv::_bssid[] = {0};
-uint8_t WiFiDrv::_mac[] = {0};
-uint8_t WiFiDrv::_localIp[] = {0};
-uint8_t WiFiDrv::_subnetMask[] = {0};
-uint8_t WiFiDrv::_gatewayIp[] = {0};
-// Firmware version
-char WiFiDrv::fwVersion[] = {0};
-
-
-// Private Methods
-
-void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip)
+#include <stdio.h> +#include <string.h> +#include <stdint.h> + +#include "Arduino.h" +#include "spi_drv.h" +#include "wifi_drv.h" + +#define _DEBUG_ + +extern "C" { +#include "wifi_spi.h" +#include "wl_types.h" +#include "debug.h" +} + +// 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}; +uint8_t WiFiDrv::_bssid[] = {0}; +uint8_t WiFiDrv::_mac[] = {0}; +uint8_t WiFiDrv::_localIp[] = {0}; +uint8_t WiFiDrv::_subnetMask[] = {0}; +uint8_t WiFiDrv::_gatewayIp[] = {0}; +// Firmware version +char WiFiDrv::fwVersion[] = {0}; + + +// Private Methods + +void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) +{ + tParam params[PARAM_NUMS_3] = { {0, (char*)ip}, {0, (char*)mask}, {0, (char*)gwip}}; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); + + SpiDrv::spiSlaveDeselect(); +} + +void WiFiDrv::getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port)
{
- tParam params[PARAM_NUMS_3] = { {0, (char*)ip}, {0, (char*)mask}, {0, (char*)gwip}};
+ tParam params[PARAM_NUMS_2] = { {0, (char*)ip}, {0, (char*)port} };
WAIT_FOR_SLAVE_SELECT();
// Send Command
- SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params);
-
- SpiDrv::spiSlaveDeselect();
-}
-
-// Public Methods
-
-
-void WiFiDrv::wifiDriverInit()
-{
- SpiDrv::begin();
-}
-
-int8_t WiFiDrv::wifiSetNetwork(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);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(SET_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::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2);
- SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
- SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- _data = WL_FAILURE;
- }
- SpiDrv::spiSlaveDeselect();
- return _data;
-}
-
-
-int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len)
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3);
- SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
- SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM);
- SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- _data = WL_FAILURE;
- }
- SpiDrv::spiSlaveDeselect();
- return _data;
-}
-
-int8_t WiFiDrv::disconnect()
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return result;
-}
-
-uint8_t WiFiDrv::getConnectionStatus()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = -1;
- uint8_t _dataLen = 0;
- SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return _data;
-}
-
-uint8_t* WiFiDrv::getMacAddress()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return _mac;
-}
-
-void WiFiDrv::getIpAddress(IPAddress& ip)
-{
- getNetworkData(_localIp, _subnetMask, _gatewayIp);
- ip = _localIp;
-}
-
- void WiFiDrv::getSubnetMask(IPAddress& mask)
- {
- getNetworkData(_localIp, _subnetMask, _gatewayIp);
- mask = _subnetMask;
- }
-
- void WiFiDrv::getGatewayIP(IPAddress& ip)
- {
- getNetworkData(_localIp, _subnetMask, _gatewayIp);
- ip = _gatewayIp;
- }
-
-char* WiFiDrv::getCurrentSSID()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return _ssid;
-}
-
-uint8_t* WiFiDrv::getCurrentBSSID()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return _bssid;
-}
-
-int32_t WiFiDrv::getCurrentRSSI()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- int32_t rssi = 0;
- SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return rssi;
-}
-
-uint8_t WiFiDrv::getCurrentEncryptionType()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1);
-
- uint8_t _dummy = DUMMY_DATA;
- SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t dataLen = 0;
- uint8_t encType = 0;
- SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return encType;
-}
-
-int8_t WiFiDrv::startScanNetworks()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
-
- if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen))
- {
- WARN("error waitResponse");
- _data = WL_FAILURE;
- }
-
- SpiDrv::spiSlaveDeselect();
-
- return (_data == WL_FAILURE)? _data : WL_SUCCESS;
-}
-
-
-uint8_t WiFiDrv::getScanNetworks()
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t ssidListNum = 0;
- SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM);
-
- SpiDrv::spiSlaveDeselect();
-
- return ssidListNum;
-}
-
-char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem)
-{
- if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
- return NULL;
-
- return _networkSsid[networkItem];
-}
-
-uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem)
-{
- if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
- return NULL;
-
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1);
-
- SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
+ SpiDrv::sendCmd(GET_REMOTE_DATA_CMD, PARAM_NUMS_1);
+ SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
- uint8_t dataLen = 0;
- uint8_t encType = 0;
- SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
+ SpiDrv::waitResponseParams(GET_REMOTE_DATA_CMD, PARAM_NUMS_2, params);
SpiDrv::spiSlaveDeselect();
-
- return encType;
}
-int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem)
-{
- if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
- return NULL;
- int32_t networkRssi = 0;
-
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1);
-
- SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t dataLen = 0;
- SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return networkRssi;
-}
-
-uint8_t WiFiDrv::reqHostByName(const char* aHostname)
-{
- WAIT_FOR_SLAVE_SELECT();
-
- // Send Command
- SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1);
- SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _data = 0;
- uint8_t _dataLen = 0;
- uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen);
-
- SpiDrv::spiSlaveDeselect();
-
- return result;
-}
-
-int WiFiDrv::getHostByName(IPAddress& aResult)
-{
- uint8_t _ipAddr[WL_IPV4_LENGTH];
- IPAddress dummy(0xFF,0xFF,0xFF,0xFF);
- int result = 0;
-
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen))
- {
- WARN("error waitResponse");
- }else{
- aResult = _ipAddr;
- result = (aResult != dummy);
- }
- SpiDrv::spiSlaveDeselect();
- return result;
-}
-
-int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult)
-{
- uint8_t retry = 10;
- if (reqHostByName(aHostname))
- {
- while(!getHostByName(aResult) && --retry > 0)
- {
- delay(1000);
- }
- }else{
- return 0;
- }
- return (retry>0);
-}
-
-char* WiFiDrv::getFwVersion()
-{
- WAIT_FOR_SLAVE_SELECT();
- // Send Command
- SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0);
-
- //Wait the reply elaboration
- SpiDrv::waitForSlaveReady();
-
- // Wait for reply
- uint8_t _dataLen = 0;
- if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen))
- {
- WARN("error waitResponse");
- }
- SpiDrv::spiSlaveDeselect();
- return fwVersion;
-}
-WiFiDrv wiFiDrv;
+// Public Methods + + +void WiFiDrv::wifiDriverInit() +{ + SpiDrv::begin(); +} + +int8_t WiFiDrv::wifiSetNetwork(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); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_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::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + + +int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); + SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +void WiFiDrv::config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, uint32_t subnet) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_IP_CONFIG_CMD, PARAM_NUMS_4); + SpiDrv::sendParam((uint8_t*)&validParams, 1, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&local_ip, 4, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&gateway, 4, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&subnet, 4, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_IP_CONFIG_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} + +void WiFiDrv::setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_DNS_CONFIG_CMD, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)&validParams, 1, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&dns_server1, 4, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)&dns_server2, 4, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_DNS_CONFIG_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); +} + + + +int8_t WiFiDrv::disconnect() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +uint8_t WiFiDrv::getConnectionStatus() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = -1; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + +uint8_t* WiFiDrv::getMacAddress() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _mac; +} + +void WiFiDrv::getIpAddress(IPAddress& ip) +{ + getNetworkData(_localIp, _subnetMask, _gatewayIp); + ip = _localIp; +} + + void WiFiDrv::getSubnetMask(IPAddress& mask) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + mask = _subnetMask; + } + + void WiFiDrv::getGatewayIP(IPAddress& ip) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + ip = _gatewayIp; + } + +char* WiFiDrv::getCurrentSSID() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _ssid; +} + +uint8_t* WiFiDrv::getCurrentBSSID() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _bssid; +} + +int32_t WiFiDrv::getCurrentRSSI() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + int32_t rssi = 0; + SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return rssi; +} + +uint8_t WiFiDrv::getCurrentEncryptionType() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t encType = 0; + SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return encType; +} + +int8_t WiFiDrv::startScanNetworks() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + + if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + + SpiDrv::spiSlaveDeselect(); + + return (_data == WL_FAILURE)? _data : WL_SUCCESS; +} + + +uint8_t WiFiDrv::getScanNetworks() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t ssidListNum = 0; + SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); + + SpiDrv::spiSlaveDeselect(); + + return ssidListNum; +} + +char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + return _networkSsid[networkItem]; +} + +uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t encType = 0; + SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return encType; +} + +int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + int32_t networkRssi = 0; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return networkRssi; +} + +uint8_t WiFiDrv::reqHostByName(const char* aHostname) +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +int WiFiDrv::getHostByName(IPAddress& aResult) +{ + uint8_t _ipAddr[WL_IPV4_LENGTH]; + IPAddress dummy(0xFF,0xFF,0xFF,0xFF); + int result = 0; + + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen)) + { + WARN("error waitResponse"); + }else{ + aResult = _ipAddr; + result = (aResult != dummy); + } + SpiDrv::spiSlaveDeselect(); + return result; +} + +int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) +{ + uint8_t retry = 10; + if (reqHostByName(aHostname)) + { + while(!getHostByName(aResult) && --retry > 0) + { + delay(1000); + } + }else{ + return 0; + } + return (retry>0); +} + +char* WiFiDrv::getFwVersion() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return fwVersion; +} + +WiFiDrv wiFiDrv; diff --git a/libraries/WiFi/utility/wifi_drv.h b/libraries/WiFi/utility/wifi_drv.h index c4f04db..d6ec029 100644 --- a/libraries/WiFi/utility/wifi_drv.h +++ b/libraries/WiFi/utility/wifi_drv.h @@ -1,219 +1,248 @@ -#ifndef WiFi_Drv_h
-#define WiFi_Drv_h
-
-#include <inttypes.h>
-#include "wifi_spi.h"
-#include "IPAddress.h"
-
-// Key index length
-#define KEY_IDX_LEN 1
-// 5 secs of delay to have the connection established
-#define WL_DELAY_START_CONNECTION 5000
-// firmware version string length
-#define WL_FW_VER_LENGTH 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];
-
- // settings of current selected network
- static char _ssid[WL_SSID_MAX_LENGTH];
- static uint8_t _bssid[WL_MAC_ADDR_LENGTH];
- static uint8_t _mac[WL_MAC_ADDR_LENGTH];
- static uint8_t _localIp[WL_IPV4_LENGTH];
- static uint8_t _subnetMask[WL_IPV4_LENGTH];
- static uint8_t _gatewayIp[WL_IPV4_LENGTH];
-
- /*
- * Get network Data information
- */
- static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
-
- static uint8_t reqHostByName(const char* aHostname);
-
- static int getHostByName(IPAddress& aResult);
-
-public:
-
- /*
- * Driver initialization
- */
- static void wifiDriverInit();
-
- /*
- * Set the desired network which the connection manager should try to
- * connect to.
- *
- * The ssid of the desired network should be specified.
- *
- * param ssid: The ssid of the desired network.
- * param ssid_len: Lenght of ssid string.
- * return: WL_SUCCESS or WL_FAILURE
- */
- static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len);
-
- /* Start Wifi connection with passphrase
- * the most secure supported mode will be automatically selected
- *
- * param ssid: Pointer to the SSID string.
- * param ssid_len: Lenght of ssid string.
- * param passphrase: Passphrase. Valid characters in a passphrase
- * must be between ASCII 32-126 (decimal).
- * 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);
-
- /* Start Wifi connection with WEP encryption.
- * Configure a key into the device. The key type (WEP-40, WEP-104)
- * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
- *
- * param ssid: Pointer to the SSID string.
- * param ssid_len: Lenght of ssid string.
- * param key_idx: The key index to set. Valid values are 0-3.
- * param key: Key input buffer.
- * 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);
-
- /*
- * Disconnect from the network
- *
- * return: WL_SUCCESS or WL_FAILURE
- */
- static int8_t disconnect();
-
- /*
- * Disconnect from the network
- *
- * return: one value of wl_status_t enum
- */
- static uint8_t getConnectionStatus();
-
- /*
- * Get the interface MAC address.
- *
- * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
- */
- static uint8_t* getMacAddress();
-
- /*
- * Get the interface IP address.
- *
- * return: copy the ip address value in IPAddress object
- */
- static void getIpAddress(IPAddress& ip);
-
- /*
- * Get the interface subnet mask address.
- *
- * return: copy the subnet mask address value in IPAddress object
- */
- static void getSubnetMask(IPAddress& mask);
-
- /*
- * Get the gateway ip address.
- *
- * return: copy the gateway ip address value in IPAddress object
- */
- static void getGatewayIP(IPAddress& ip);
-
- /*
- * Return the current SSID associated with the network
- *
- * return: ssid string
- */
- static char* getCurrentSSID();
-
- /*
- * Return the current BSSID associated with the network.
- * It is the MAC address of the Access Point
- *
- * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
- */
- static uint8_t* getCurrentBSSID();
-
- /*
- * Return the current RSSI /Received Signal Strength in dBm)
- * associated with the network
- *
- * return: signed value
- */
- static int32_t getCurrentRSSI();
-
- /*
- * Return the Encryption Type associated with the network
- *
- * return: one value of wl_enc_type enum
- */
- static uint8_t getCurrentEncryptionType();
-
- /*
- * Start scan WiFi networks available
- *
- * return: Number of discovered networks
- */
- static int8_t startScanNetworks();
-
- /*
- * Get the networks available
- *
- * return: Number of discovered networks
- */
- static uint8_t getScanNetworks();
-
- /*
- * Return the SSID discovered during the network scan.
- *
- * param networkItem: specify from which network item want to get the information
- *
- * return: ssid string of the specified item on the networks scanned list
- */
- static char* getSSIDNetoworks(uint8_t networkItem);
-
- /*
- * Return the RSSI of the networks discovered during the scanNetworks
- *
- * param networkItem: specify from which network item want to get the information
- *
- * return: signed value of RSSI of the specified item on the networks scanned list
- */
- static int32_t getRSSINetoworks(uint8_t networkItem);
-
- /*
- * Return the encryption type of the networks discovered during the scanNetworks
- *
- * param networkItem: specify from which network item want to get the information
- *
- * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
- */
- static uint8_t getEncTypeNetowrks(uint8_t networkItem);
-
- /*
- * Resolve the given hostname to an IP address.
- * param aHostname: Name to be resolved
- * param aResult: IPAddress structure to store the returned IP address
- * result: 1 if aIPAddrString was successfully converted to an IP address,
- * else error code
- */
- static int getHostByName(const char* aHostname, IPAddress& aResult);
-
- /*
- * Get the firmware version
- * result: version as string with this format a.b.c
- */
- static char* getFwVersion();
-
-};
-
-extern WiFiDrv wiFiDrv;
-
-#endif
+#ifndef WiFi_Drv_h +#define WiFi_Drv_h + +#include <inttypes.h> +#include "wifi_spi.h" +#include "IPAddress.h" +#include "../WiFiUdp.h"
+ +// Key index length +#define KEY_IDX_LEN 1 +// 5 secs of delay to have the connection established +#define WL_DELAY_START_CONNECTION 5000 +// firmware version string length +#define WL_FW_VER_LENGTH 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]; + + // settings of current selected network + static char _ssid[WL_SSID_MAX_LENGTH]; + static uint8_t _bssid[WL_MAC_ADDR_LENGTH]; + static uint8_t _mac[WL_MAC_ADDR_LENGTH]; + static uint8_t _localIp[WL_IPV4_LENGTH]; + static uint8_t _subnetMask[WL_IPV4_LENGTH]; + static uint8_t _gatewayIp[WL_IPV4_LENGTH]; + + /* + * Get network Data information + */ + static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + + static uint8_t reqHostByName(const char* aHostname); + + static int getHostByName(IPAddress& aResult); + + /*
+ * Get remote Data information on UDP socket
+ */
+ static void getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port);
+
+public: + + /* + * Driver initialization + */ + static void wifiDriverInit(); + + /* + * Set the desired network which the connection manager should try to + * connect to. + * + * The ssid of the desired network should be specified. + * + * param ssid: The ssid of the desired network. + * param ssid_len: Lenght of ssid string. + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + + /* Start Wifi connection with passphrase + * the most secure supported mode will be automatically selected + * + * param ssid: Pointer to the SSID string. + * param ssid_len: Lenght of ssid string. + * param passphrase: Passphrase. Valid characters in a passphrase + * must be between ASCII 32-126 (decimal). + * 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); + + /* Start Wifi connection with WEP encryption. + * Configure a key into the device. The key type (WEP-40, WEP-104) + * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). + * + * param ssid: Pointer to the SSID string. + * param ssid_len: Lenght of ssid string. + * param key_idx: The key index to set. Valid values are 0-3. + * param key: Key input buffer. + * 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); + + /* Set ip configuration disabling dhcp client + * + * param validParams: set the number of parameters that we want to change + * i.e. validParams = 1 means that we'll change only ip address + * validParams = 3 means that we'll change ip address, gateway and netmask + * param local_ip: Static ip configuration + * param gateway: Static gateway configuration + * param subnet: Static subnet mask configuration + */ + static void config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, uint32_t subnet); + + /* Set DNS ip configuration + * + * param validParams: set the number of parameters that we want to change + * i.e. validParams = 1 means that we'll change only dns_server1 + * validParams = 2 means that we'll change dns_server1 and dns_server2 + * param dns_server1: Static DNS server1 configuration + * param dns_server2: Static DNS server2 configuration + */ + static void setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2); + + /* + * Disconnect from the network + * + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t disconnect(); + + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ + static uint8_t getConnectionStatus(); + + /* + * Get the interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + static uint8_t* getMacAddress(); + + /* + * Get the interface IP address. + * + * return: copy the ip address value in IPAddress object + */ + static void getIpAddress(IPAddress& ip); + + /* + * Get the interface subnet mask address. + * + * return: copy the subnet mask address value in IPAddress object + */ + static void getSubnetMask(IPAddress& mask); + + /* + * Get the gateway ip address. + * + * return: copy the gateway ip address value in IPAddress object + */ + static void getGatewayIP(IPAddress& ip); + + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ + static char* getCurrentSSID(); + + /* + * Return the current BSSID associated with the network. + * It is the MAC address of the Access Point + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + static uint8_t* getCurrentBSSID(); + + /* + * Return the current RSSI /Received Signal Strength in dBm) + * associated with the network + * + * return: signed value + */ + static int32_t getCurrentRSSI(); + + /* + * Return the Encryption Type associated with the network + * + * return: one value of wl_enc_type enum + */ + static uint8_t getCurrentEncryptionType(); + + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ + static int8_t startScanNetworks(); + + /* + * Get the networks available + * + * return: Number of discovered networks + */ + static uint8_t getScanNetworks(); + + /* + * Return the SSID discovered during the network scan. + * + * param networkItem: specify from which network item want to get the information + * + * return: ssid string of the specified item on the networks scanned list + */ + static char* getSSIDNetoworks(uint8_t networkItem); + + /* + * Return the RSSI of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: signed value of RSSI of the specified item on the networks scanned list + */ + static int32_t getRSSINetoworks(uint8_t networkItem); + + /* + * Return the encryption type of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ + static uint8_t getEncTypeNetowrks(uint8_t networkItem); + + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + static int getHostByName(const char* aHostname, IPAddress& aResult); + + /* + * Get the firmware version + * result: version as string with this format a.b.c + */ + static char* getFwVersion(); + + friend class WiFiUDP;
+
+}; + +extern WiFiDrv wiFiDrv; + +#endif diff --git a/libraries/WiFi/utility/wifi_spi.h b/libraries/WiFi/utility/wifi_spi.h index bf479e2..8856e33 100644 --- a/libraries/WiFi/utility/wifi_spi.h +++ b/libraries/WiFi/utility/wifi_spi.h @@ -1,144 +1,153 @@ -#ifndef WiFi_Spi_h
-#define WiFi_Spi_h
-
-#include "wl_definitions.h"
-
-#define CMD_FLAG 0
-#define REPLY_FLAG 1<<7
-#define DATA_FLAG 0x40
-
-#define WIFI_SPI_ACK 1
-#define WIFI_SPI_ERR 0xFF
-
-#define TIMEOUT_CHAR 1000
-
-//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */
-#define NO_SOCKET_AVAIL 255
-
-#define START_CMD 0xE0
-#define END_CMD 0xEE
-#define ERR_CMD 0xEF
-
-enum {
- SET_NET_CMD = 0x10,
- SET_PASSPHRASE_CMD = 0x11,
- SET_KEY_CMD = 0x12,
- TEST_CMD = 0x13,
-
- GET_CONN_STATUS_CMD = 0x20,
- GET_IPADDR_CMD = 0x21,
- GET_MACADDR_CMD = 0x22,
- GET_CURR_SSID_CMD = 0x23,
- GET_CURR_BSSID_CMD = 0x24,
- GET_CURR_RSSI_CMD = 0x25,
- GET_CURR_ENCT_CMD = 0x26,
- SCAN_NETWORKS = 0x27,
- START_SERVER_TCP_CMD= 0x28,
- GET_STATE_TCP_CMD = 0x29,
- DATA_SENT_TCP_CMD = 0x2A,
- AVAIL_DATA_TCP_CMD = 0x2B,
- GET_DATA_TCP_CMD = 0x2C,
- START_CLIENT_TCP_CMD= 0x2D,
- STOP_CLIENT_TCP_CMD = 0x2E,
- GET_CLIENT_STATE_TCP_CMD= 0x2F,
- DISCONNECT_CMD = 0x30,
- 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,
-
- // All command with DATA_FLAG 0x40 send a 16bit Len
-
- SEND_DATA_TCP_CMD = 0x44,
- GET_DATABUF_TCP_CMD = 0x45,
-};
-
-
-enum wl_tcp_state {
- CLOSED = 0,
- LISTEN = 1,
- SYN_SENT = 2,
- SYN_RCVD = 3,
- ESTABLISHED = 4,
- FIN_WAIT_1 = 5,
- FIN_WAIT_2 = 6,
- CLOSE_WAIT = 7,
- CLOSING = 8,
- LAST_ACK = 9,
- TIME_WAIT = 10
-};
-
-
-enum numParams{
- PARAM_NUMS_0,
- PARAM_NUMS_1,
- PARAM_NUMS_2,
- PARAM_NUMS_3,
- PARAM_NUMS_4,
- PARAM_NUMS_5,
- MAX_PARAM_NUMS
-};
-
-#define MAX_PARAMS MAX_PARAM_NUMS-1
-#define PARAM_LEN_SIZE 1
-
-typedef struct __attribute__((__packed__))
-{
- uint8_t paramLen;
- char* param;
-}tParam;
-
-typedef struct __attribute__((__packed__))
-{
- uint16_t dataLen;
- char* data;
-}tDataParam;
-
-
-typedef struct __attribute__((__packed__))
-{
- unsigned char cmd;
- unsigned char tcmd;
- unsigned char nParam;
- tParam params[MAX_PARAMS];
-}tSpiMsg;
-
-typedef struct __attribute__((__packed__))
-{
- unsigned char cmd;
- unsigned char tcmd;
- unsigned char nParam;
- tDataParam params[MAX_PARAMS];
-}tSpiMsgData;
-
-
-typedef struct __attribute__((__packed__))
-{
- unsigned char cmd;
- unsigned char tcmd;
- //unsigned char totLen;
- unsigned char nParam;
-}tSpiHdr;
-
-typedef struct __attribute__((__packed__))
-{
- uint8_t paramLen;
- uint32_t param;
-}tLongParam;
-
-typedef struct __attribute__((__packed__))
-{
- uint8_t paramLen;
- uint16_t param;
-}tIntParam;
-
-typedef struct __attribute__((__packed__))
-{
- uint8_t paramLen;
- uint8_t param;
-}tByteParam;
-
-#endif
+#ifndef WiFi_Spi_h +#define WiFi_Spi_h + +#include "wl_definitions.h" + +#define CMD_FLAG 0 +#define REPLY_FLAG 1<<7 +#define DATA_FLAG 0x40 + +#define WIFI_SPI_ACK 1 +#define WIFI_SPI_ERR 0xFF + +#define TIMEOUT_CHAR 1000 + +//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ +#define NO_SOCKET_AVAIL 255 + +#define START_CMD 0xE0 +#define END_CMD 0xEE +#define ERR_CMD 0xEF +#define CMD_POS 1 // Position of Command OpCode on SPI stream +#define PARAM_LEN_POS 2 // Position of Param len on SPI stream + + +enum { + SET_NET_CMD = 0x10, + SET_PASSPHRASE_CMD = 0x11, + SET_KEY_CMD = 0x12, + TEST_CMD = 0x13, + SET_IP_CONFIG_CMD = 0x14, + SET_DNS_CONFIG_CMD = 0x15, + + GET_CONN_STATUS_CMD = 0x20, + GET_IPADDR_CMD = 0x21, + GET_MACADDR_CMD = 0x22, + GET_CURR_SSID_CMD = 0x23, + GET_CURR_BSSID_CMD = 0x24, + GET_CURR_RSSI_CMD = 0x25, + GET_CURR_ENCT_CMD = 0x26, + SCAN_NETWORKS = 0x27, + START_SERVER_TCP_CMD= 0x28, + GET_STATE_TCP_CMD = 0x29, + DATA_SENT_TCP_CMD = 0x2A, + AVAIL_DATA_TCP_CMD = 0x2B, + GET_DATA_TCP_CMD = 0x2C, + START_CLIENT_TCP_CMD= 0x2D, + STOP_CLIENT_TCP_CMD = 0x2E, + GET_CLIENT_STATE_TCP_CMD= 0x2F, + DISCONNECT_CMD = 0x30, + 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, + SEND_DATA_UDP_CMD = 0x39, + GET_REMOTE_DATA_CMD = 0x3A,
+ + // All command with DATA_FLAG 0x40 send a 16bit Len + + SEND_DATA_TCP_CMD = 0x44, + GET_DATABUF_TCP_CMD = 0x45, + INSERT_DATABUF_CMD = 0x46, +}; + + +enum wl_tcp_state { + CLOSED = 0, + LISTEN = 1, + SYN_SENT = 2, + SYN_RCVD = 3, + ESTABLISHED = 4, + FIN_WAIT_1 = 5, + FIN_WAIT_2 = 6, + CLOSE_WAIT = 7, + CLOSING = 8, + LAST_ACK = 9, + TIME_WAIT = 10 +}; + + +enum numParams{ + PARAM_NUMS_0, + PARAM_NUMS_1, + PARAM_NUMS_2, + PARAM_NUMS_3, + PARAM_NUMS_4, + PARAM_NUMS_5, + MAX_PARAM_NUMS +}; + +#define MAX_PARAMS MAX_PARAM_NUMS-1 +#define PARAM_LEN_SIZE 1 + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + char* param; +}tParam; + +typedef struct __attribute__((__packed__)) +{ + uint16_t dataLen; + char* data; +}tDataParam; + + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + unsigned char nParam; + tParam params[MAX_PARAMS]; +}tSpiMsg; + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + unsigned char nParam; + tDataParam params[MAX_PARAMS]; +}tSpiMsgData; + + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + //unsigned char totLen; + unsigned char nParam; +}tSpiHdr; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint32_t param; +}tLongParam; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint16_t param; +}tIntParam; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint8_t param; +}tByteParam; + +#endif diff --git a/libraries/WiFi/utility/wl_definitions.h b/libraries/WiFi/utility/wl_definitions.h index 15de781..1ec8e71 100644 --- a/libraries/WiFi/utility/wl_definitions.h +++ b/libraries/WiFi/utility/wl_definitions.h @@ -22,6 +22,8 @@ #define WL_NETWORKS_LIST_MAXNUM 10 // Maxmium number of socket #define MAX_SOCK_NUM 4 +// Default state value for Wifi state field +#define NA_STATE -1 //Maximum number of attempts to establish wifi connection #define WL_MAX_ATTEMPT_CONNECTION 10 |