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/utility | |
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/utility')
-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 |
7 files changed, 1338 insertions, 1174 deletions
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 |