diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 8bb66f215..c0cb0016d 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -34,6 +34,11 @@ uint8_t WiFiClass::getSocket() return NO_SOCKET_AVAIL; } +char* WiFiClass::firmwareVersion() +{ + return WiFiDrv::getFwVersion(); +} + int WiFiClass::begin(char* ssid) { uint8_t status = WL_IDLE_STATUS; @@ -150,9 +155,20 @@ uint8_t WiFiClass::encryptionType() } -uint8_t WiFiClass::scanNetworks() +int8_t WiFiClass::scanNetworks() { - return WiFiDrv::scanNetworks(); + uint8_t attempts = 10; + uint8_t numOfNetworks = 0; + + if (WiFiDrv::startScanNetworks() == WL_FAILURE) + return WL_FAILURE; + do + { + delay(2000); + numOfNetworks = WiFiDrv::getScanNetworks(); + } + while (( numOfNetworks == 0)&&(--attempts>0)); + return numOfNetworks; } char* WiFiClass::SSID(uint8_t networkItem) diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 16d6a0ee7..9a86701a0 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -28,6 +28,12 @@ public: */ static uint8_t getSocket(); + /* + * Get firmware version + */ + static char* firmwareVersion(); + + /* Start Wifi connection for OPEN networks * * param ssid: Pointer to the SSID string. @@ -123,7 +129,7 @@ public: * * return: Number of discovered networks */ - uint8_t scanNetworks(); + int8_t scanNetworks(); /* * Return the SSID discovered during the network scan. diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 8f517cc19..83c0d10b9 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -35,8 +35,19 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { { ServerDrv::startClient(uint32_t(ip), port, _sock); WiFiClass::_state[_sock] = _sock; - while(!connected()); + + 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; @@ -59,12 +70,17 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { } - if ((!ServerDrv::sendData(_sock, buf, size)) || - (!ServerDrv::checkDataSent(_sock))) + if (!ServerDrv::sendData(_sock, buf, size)) { setWriteError(); return 0; } + if (!ServerDrv::checkDataSent(_sock)) + { + setWriteError(); + return 0; + } + return size; } @@ -94,8 +110,12 @@ int WiFiClient::read(uint8_t* buf, size_t size) { } int WiFiClient::peek() { - //TODO to be implemented - return 0; + uint8_t b; + if (!available()) + return -1; + + ServerDrv::getData(_sock, &b, 1); + return b; } void WiFiClient::flush() { @@ -112,10 +132,10 @@ void WiFiClient::stop() { unsigned long start = millis(); + // wait a second for the connection to close while (status() != CLOSED && millis() - start < 1000) delay(1); - _sock = 255; } @@ -128,6 +148,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())); } } diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index cf1eb432d..f42a7f377 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -9,8 +9,8 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include @@ -18,25 +18,33 @@ char ssid[] = "yourNetwork"; // the name of your network int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to open network: "); - Serial.println(ssid); - status = WiFi.begin(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + //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); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to open SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index 1159e37f4..19736b5b2 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -19,7 +19,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 4 Mar 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -30,25 +30,33 @@ int keyIndex = 0; // your network key Index numbe int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } - // attempt to connect to an open network: - Serial.print("Attempting to connect to WEP network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, keyIndex, key); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WEP network, SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, keyIndex, key); + + // wait 10 seconds for connection: + delay(10000); } + + // once you are connected : + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { @@ -60,7 +68,7 @@ void loop() { void printWifiData() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); Serial.println(ip); @@ -115,3 +123,4 @@ void printCurrentNet() { } + diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index 7b5752cc7..fcc33ecaa 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -9,35 +9,45 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to WPA network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + } void loop() { diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 2c10fed33..93b30006e 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -10,8 +10,8 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 22 April 2012 - by Tom Igoe + modified 21 Junn 2012 + by Tom Igoe and Jaymes Dec */ @@ -19,11 +19,20 @@ #include void setup() { - // initialize serial and wait for the port to open: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } - // attempt to connect using WEP encryption: - Serial.println("Initializing Wifi..."); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // Print WiFi MAC address: printMacAddress(); // scan for existing networks: @@ -61,7 +70,12 @@ void printMacAddress() { void listNetworks() { // scan for nearby networks: Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); + int numSsid = WiFi.scanNetworks(); + if (numSsid == -1) + { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // print the list of networks seen: Serial.print("number of available networks:"); @@ -76,9 +90,30 @@ void listNetworks() { Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); - Serial.println(WiFi.encryptionType(thisNet)); + printEncryptionType(WiFi.encryptionType(thisNet)); } } +void printEncryptionType(int thisType) { + // read the encryption type and print out the name: + switch (thisType) { + case ENC_TYPE_WEP: + Serial.println("WEP"); + break; + case ENC_TYPE_TKIP: + Serial.println("WPA"); + break; + case ENC_TYPE_CCMP: + Serial.println("WPA2"); + break; + case ENC_TYPE_NONE: + Serial.println("None"); + break; + case ENC_TYPE_AUTO: + Serial.println("Auto"); + break; + } +} + diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 8b6698f40..e4b1d1a3b 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -14,7 +14,7 @@ created 18 Dec 2009 by David A. Mellis - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -22,8 +22,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +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) @@ -34,14 +34,26 @@ WiFiServer server(23); boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { - // start serial port: - Serial.begin(9600); - + //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, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index bf590c998..f8ffc0745 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -16,7 +16,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 13 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe modified 8 Sept 2012 by Scott Fitzgerald @@ -48,14 +48,26 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to pachube.com void setup() { - // start serial port: - Serial.begin(9600); + //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, pass); + // wait 10 seconds for connection: delay(10000); } @@ -71,7 +83,7 @@ void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: - if (client.available()) { + while (client.available()) { char c = client.read(); Serial.print(c); } diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 0a939d417..243fe8383 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -19,7 +19,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 16 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe modified 8 Sept 2012 by Scott Fitzgerald @@ -53,14 +53,26 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to pachube.com void setup() { - // start serial port: - Serial.begin(9600); - + //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, pass); + // wait 10 seconds for connection: delay(10000); } @@ -85,7 +97,7 @@ void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: - if (client.available()) { + while (client.available()) { char c = client.read(); Serial.print(c); } diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 8bf7bb2c2..3dc2c8d33 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -14,6 +14,7 @@ * WiFi shield attached to pins 10, 11, 12, 13 created 23 apr 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -22,7 +23,7 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) +char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) @@ -49,14 +50,26 @@ void setup() { // reserve space for the strings: currentLine.reserve(256); tweet.reserve(150); - // start serial port: - Serial.begin(9600); - + //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); - status = WiFi.begin(ssid, pass); + // 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); } diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index f6a3cdf23..17f44a3aa 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -16,7 +16,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -24,8 +24,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +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) int status = WL_IDLE_STATUS; @@ -40,13 +40,26 @@ IPAddress server(173,194,73,105); // numeric IP for Google (no DNS) WiFiClient client; void setup() { - Serial.begin(9600); - + //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, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index 8e5b88547..96eb6283d 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -7,7 +7,8 @@ Circuit: * Wifi shield attached to pins 10, 11, 12, 13 - created 23 Apr 2012 + created 23 April 2012 + modifide 31 May 2012 by Tom Igoe http://arduino.cc/en/Tutorial/WifiWebClientRepeating @@ -35,14 +36,26 @@ boolean lastConnected = false; // state of the connection last const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds void setup() { - // start serial port: - Serial.begin(9600); + //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, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 34a231b5c..ac5f056f1 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -13,7 +13,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -29,14 +29,26 @@ int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { - // start serial port: - Serial.begin(9600); + //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, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 437ec6c94..9f71055b2 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -50,6 +50,7 @@ #define WARN(args) do {} while (0); #endif +#if _DEBUG_SPI_ #define DBG_PIN2 5 #define DBG_PIN 4 @@ -64,5 +65,13 @@ #define TOGGLE_TRIGGER() SET_TRIGGER() \ delayMicroseconds(2); \ RST_TRIGGER() +#else +#define START() +#define END() +#define SET_TRIGGER() +#define RST_TRIGGER() +#define INIT_TRIGGER() +#define TOGGLE_TRIGGER() +#endif #endif diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index c11e94109..ce03604b4 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -146,12 +146,13 @@ uint8_t ServerDrv::availData(uint8_t sock) return false; } -bool ServerDrv::getData(uint8_t sock, uint8_t *data) +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_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + 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(); @@ -225,8 +226,8 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) uint8_t ServerDrv::checkDataSent(uint8_t sock) { - const uint16_t TIMEOUT_DATA_SENT = 250; - static uint16_t timeout = 0; + const uint16_t TIMEOUT_DATA_SENT = 25; + uint16_t timeout = 0; uint8_t _data = 0; uint8_t _dataLen = 0; @@ -249,14 +250,10 @@ uint8_t ServerDrv::checkDataSent(uint8_t sock) if (_data) timeout = 0; else{ ++timeout; - if (timeout > TIMEOUT_DATA_SENT) - { - timeout = 0; - INFO1("Timeout wainting for data sent"); - } + delay(100); } - }while((_data==0)&&(timeout 0) && (*readChar != waitChar)); -// -// return (*readChar == waitChar); -//} - - int SpiDrv::readAndCheckChar(char checkChar, char* readChar) { - //*readChar = spiTransfer(DUMMY_DATA); //get data byte getParam((uint8_t*)readChar); return (*readChar == checkChar); @@ -128,11 +115,8 @@ char SpiDrv::readChar() uint8_t readChar = 0; getParam(&readChar); return readChar; - //return spiTransfer(DUMMY_DATA); //get data byte } -//#define WAIT_START_CMD(x) waitSpiChar(START_CMD, &x) -//#define WAIT_START_CMD(x) readAndCheckChar(START_CMD, &x) #define WAIT_START_CMD(x) waitSpiChar(START_CMD) #define IF_CHECK_START_CMD(x) \ @@ -171,10 +155,7 @@ void SpiDrv::getParam(uint8_t* param) { // Get Params data *param = spiTransfer(DUMMY_DATA); - DELAY_100NS; - DELAY_100NS; - DELAY_100NS; - DELAY_100NS; + DELAY_TRANSFER(); } int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) @@ -399,6 +380,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, u } else { WARN("Error numParams == 0"); + readAndCheckChar(END_CMD, &_data); return 0; } readAndCheckChar(END_CMD, &_data); diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h index 60497b585..5c2e7063f 100644 --- a/WiFi/utility/spi_drv.h +++ b/WiFi/utility/spi_drv.h @@ -4,7 +4,7 @@ #include #include "wifi_spi.h" -#define SPI_START_CMD_DELAY 10 +#define SPI_START_CMD_DELAY 12 #define NO_LAST_PARAM 0 #define LAST_PARAM 1 diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 180633852..1ca169682 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -26,6 +26,8 @@ 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 @@ -59,7 +61,7 @@ void WiFiDrv::wifiDriverInit() SpiDrv::begin(); } -uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) +int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -75,13 +77,14 @@ uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) 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; } -uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -98,13 +101,14 @@ uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pas if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; } -uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) +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 @@ -122,12 +126,13 @@ uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; } -uint8_t WiFiDrv::disconnect() +int8_t WiFiDrv::disconnect() { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -142,7 +147,7 @@ uint8_t WiFiDrv::disconnect() // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -160,7 +165,7 @@ uint8_t WiFiDrv::getConnectionStatus() SpiDrv::waitForSlaveReady(); // Wait for reply - uint8_t _data = 0; + uint8_t _data = -1; uint8_t _dataLen = 0; SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); @@ -299,7 +304,33 @@ uint8_t WiFiDrv::getCurrentEncryptionType() return encType; } -uint8_t WiFiDrv::scanNetworks() +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(); @@ -438,4 +469,23 @@ int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) 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/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 056e6126a..c4f04dbe7 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -9,6 +9,8 @@ #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 { @@ -18,6 +20,9 @@ private: 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]; @@ -38,7 +43,7 @@ private: public: /* - * Driver initialization + * Driver initialization */ static void wifiDriverInit(); @@ -52,7 +57,7 @@ public: * param ssid_len: Lenght of ssid string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); /* Start Wifi connection with passphrase * the most secure supported mode will be automatically selected @@ -64,7 +69,7 @@ public: * param len: Lenght of passphrase string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + 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) @@ -77,14 +82,14 @@ public: * param len: Lenght of key string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + 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 + * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t disconnect(); + static int8_t disconnect(); /* * Disconnect from the network @@ -156,7 +161,14 @@ public: * * return: Number of discovered networks */ - static uint8_t scanNetworks(); + 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. @@ -194,6 +206,12 @@ public: */ 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; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 01e1acd0b..bf479e2ec 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -47,6 +47,8 @@ enum { 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 diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index e3db8b608..15de781fc 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -26,7 +26,8 @@ #define WL_MAX_ATTEMPT_CONNECTION 10 typedef enum { - WL_IDLE_STATUS, + WL_NO_SHIELD = 255, + WL_IDLE_STATUS = 0, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED, WL_CONNECTED,