1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Added WiFi Udp Implementation

This commit is contained in:
Mimmo La Fauci 2013-02-08 19:22:23 +01:00
parent a0c96bebdd
commit 863b47ad5e
13 changed files with 1510 additions and 1185 deletions

View File

@ -8,7 +8,7 @@ extern "C" {
} }
// XXX: don't make assumptions about the value of MAX_SOCK_NUM. // XXX: don't make assumptions about the value of MAX_SOCK_NUM.
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; int16_t WiFiClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE };
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
WiFiClass::WiFiClass() WiFiClass::WiFiClass()
@ -71,8 +71,7 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
{ {
delay(WL_DELAY_START_CONNECTION); delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus(); status = WiFiDrv::getConnectionStatus();
} }while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{ }else{
status = WL_CONNECT_FAILED; status = WL_CONNECT_FAILED;
} }

View File

@ -129,6 +129,7 @@ void WiFiClient::stop() {
return; return;
ServerDrv::stopClient(_sock); ServerDrv::stopClient(_sock);
WiFiClass::_state[_sock] = NA_STATE;
unsigned long start = millis(); unsigned long start = millis();
@ -169,7 +170,7 @@ WiFiClient::operator bool() {
uint8_t WiFiClient::getFirstSocket() uint8_t WiFiClient::getFirstSocket()
{ {
for (int i = 0; i < MAX_SOCK_NUM; i++) { for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiClass::_state[i] == 0) if (WiFiClass::_state[i] == NA_STATE)
{ {
return i; return i;
} }

View File

@ -21,6 +21,7 @@ void WiFiServer::begin()
{ {
ServerDrv::startServer(_port, _sock); ServerDrv::startServer(_port, _sock);
WiFiClass::_server_port[_sock] = _port; WiFiClass::_server_port[_sock] = _port;
WiFiClass::_state[_sock] = _sock;
} }
} }

163
libraries/WiFi/WiFiUdp.cpp Normal file
View File

@ -0,0 +1,163 @@
extern "C" {
#include "utility/debug.h"
#include "utility/wifi_spi.h"
}
#include <string.h>
#include "server_drv.h"
#include "wifi_drv.h"
#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
/* Constructor */
WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}
/* Start WiFiUDP socket, listening at local port PORT */
uint8_t WiFiUDP::begin(uint16_t port) {
uint8_t sock = WiFiClass::getSocket();
if (sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(port, sock, UDP_MODE);
WiFiClass::_server_port[sock] = port;
_sock = sock;
_port = port;
return 1;
}
return 0;
}
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiUDP::available() {
if (_sock != NO_SOCKET_AVAIL)
{
return ServerDrv::availData(_sock);
}
return 0;
}
/* Release any resources being used by this WiFiUDP instance */
void WiFiUDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;
ServerDrv::stopClient(_sock);
_sock = NO_SOCKET_AVAIL;
}
int WiFiUDP::beginPacket(const char *host, uint16_t port)
{
// Look up the host first
int ret = 0;
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr))
{
return beginPacket(remote_addr, port);
}
return ret;
}
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = WiFiClass::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE);
WiFiClass::_state[_sock] = _sock;
return 1;
}
return 0;
}
int WiFiUDP::endPacket()
{
return ServerDrv::sendUdpData(_sock);
}
size_t WiFiUDP::write(uint8_t byte)
{
return write(&byte, 1);
}
size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
{
ServerDrv::insertDataBuf(_sock, buffer, size);
return size;
}
int WiFiUDP::parsePacket()
{
return available();
}
int WiFiUDP::read()
{
uint8_t b;
if (available())
{
ServerDrv::getData(_sock, &b);
return b;
}else{
return -1;
}
}
int WiFiUDP::read(unsigned char* buffer, size_t len)
{
if (available())
{
size_t size = 0;
if (!ServerDrv::getDataBuf(_sock, buffer, &size))
return -1;
// TODO check if the buffer is too smal respect to buffer size
return size;
}else{
return -1;
}
}
int WiFiUDP::peek()
{
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b, 1);
return b;
}
void WiFiUDP::flush()
{
while (available())
read();
}
IPAddress WiFiUDP::remoteIP()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
IPAddress ip(_remoteIp);
return ip;
}
uint16_t WiFiUDP::remotePort()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
return port;
}

61
libraries/WiFi/WiFiUdp.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef wifiudp_h
#define wifiudp_h
#include <Udp.h>
#define UDP_TX_PACKET_MAX_SIZE 24
class WiFiUDP : public UDP {
private:
uint8_t _sock; // socket ID for Wiz5100
uint16_t _port; // local port to listen on
public:
WiFiUDP(); // Constructor
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
virtual void stop(); // Finish with the UDP socket
// Sending UDP packets
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port);
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port);
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket();
// Write a single byte into the packet
virtual size_t write(uint8_t);
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket();
// Number of bytes remaining in the current packet
virtual int available();
// Read a single byte from the current packet
virtual int read();
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len);
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
// Return the next byte from the current packet without moving on to the next byte
virtual int peek();
virtual void flush(); // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP();
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort();
friend class WiFiDrv;
};
#endif

View File

@ -36,6 +36,12 @@ getResult KEYWORD2
getSocket KEYWORD2 getSocket KEYWORD2
WiFiClient KEYWORD2 WiFiClient KEYWORD2
WiFiServer KEYWORD2 WiFiServer KEYWORD2
WiFiUDP KEYWORD2
beginPacket KEYWORD2
endPacket KEYWORD2
parsePacket KEYWORD2
remoteIP KEYWORD2
remotePort KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)

View File

@ -1,209 +1,210 @@
//#define _DEBUG_ //#define _DEBUG_
#include "server_drv.h" #include "server_drv.h"
#include "Arduino.h" #include "Arduino.h"
#include "spi_drv.h" #include "spi_drv.h"
extern "C" { extern "C" {
#include "wl_types.h" #include "wl_types.h"
#include "debug.h" #include "debug.h"
} }
// Start server TCP on port specified // Start server TCP on port specified
void ServerDrv::startServer(uint16_t port, uint8_t sock) 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;
}
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);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
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)
{
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(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); SpiDrv::sendCmd(INSERT_DATABUF_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)
{
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);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
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)
{
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(&sock, sizeof(sock));
SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); SpiDrv::sendBuffer((uint8_t *)data, _len, LAST_PARAM);
//Wait the reply elaboration //Wait the reply elaboration
SpiDrv::waitForSlaveReady(); SpiDrv::waitForSlaveReady();
@ -211,7 +212,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
// Wait for reply // Wait for reply
uint8_t _data = 0; uint8_t _data = 0;
uint8_t _dataLen = 0; uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) if (!SpiDrv::waitResponseData8(INSERT_DATABUF_CMD, &_data, &_dataLen))
{ {
WARN("error waitResponse"); WARN("error waitResponse");
} }
@ -223,38 +224,90 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
return false; return false;
} }
bool ServerDrv::sendUdpData(uint8_t sock)
uint8_t ServerDrv::checkDataSent(uint8_t sock)
{ {
const uint16_t TIMEOUT_DATA_SENT = 25; WAIT_FOR_SLAVE_SELECT();
uint16_t timeout = 0; // Send Command
uint8_t _data = 0; SpiDrv::sendCmd(SEND_DATA_UDP_CMD, PARAM_NUMS_1);
uint8_t _dataLen = 0; SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
do { //Wait the reply elaboration
WAIT_FOR_SLAVE_SELECT(); SpiDrv::waitForSlaveReady();
// Send Command
SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration // Wait for reply
SpiDrv::waitForSlaveReady(); uint8_t _data = 0;
uint8_t _dataLen = 0;
// Wait for reply if (!SpiDrv::waitResponseData8(SEND_DATA_UDP_CMD, &_data, &_dataLen))
if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) {
{ WARN("error waitResponse");
WARN("error waitResponse isDataSent"); }
} SpiDrv::spiSlaveDeselect();
SpiDrv::spiSlaveDeselect(); if (_dataLen!=0)
{
if (_data) timeout = 0; return (_data == 1);
else{ }
++timeout; return false;
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;

View File

@ -1,34 +1,41 @@
#ifndef Server_Drv_h #ifndef Server_Drv_h
#define Server_Drv_h #define Server_Drv_h
#include <inttypes.h>
#include "wifi_spi.h"
typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode;
#include <inttypes.h> class ServerDrv
#include "wifi_spi.h" {
public:
class ServerDrv // Start server TCP on port specified
{ static void startServer(uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
public:
// Start server TCP on port specified static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
static void startServer(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 insertDataBuf(uint8_t sock, const uint8_t *_data, uint16_t _dataLen);
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock); static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len);
static bool sendUdpData(uint8_t sock);
static void stopClient(uint8_t sock); static uint8_t availData(uint8_t sock);
static uint8_t getServerState(uint8_t sock); static uint8_t checkDataSent(uint8_t sock);
};
static uint8_t getClientState(uint8_t sock);
extern ServerDrv serverDrv;
static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0);
#endif
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

View File

@ -1,83 +1,83 @@
#ifndef SPI_Drv_h #ifndef SPI_Drv_h
#define SPI_Drv_h #define SPI_Drv_h
#include <inttypes.h> #include <inttypes.h>
#include "wifi_spi.h" #include "wifi_spi.h"
#define SPI_START_CMD_DELAY 12 #define SPI_START_CMD_DELAY 10
#define NO_LAST_PARAM 0 #define NO_LAST_PARAM 0
#define LAST_PARAM 1 #define LAST_PARAM 1
#define DUMMY_DATA 0xFF #define DUMMY_DATA 0xFF
#define WAIT_FOR_SLAVE_SELECT() \ #define WAIT_FOR_SLAVE_SELECT() \
SpiDrv::waitForSlaveReady(); \ SpiDrv::waitForSlaveReady(); \
SpiDrv::spiSlaveSelect(); SpiDrv::spiSlaveSelect();
class SpiDrv class SpiDrv
{ {
private: private:
//static bool waitSlaveReady(); //static bool waitSlaveReady();
static void waitForSlaveSign(); static void waitForSlaveSign();
static void getParam(uint8_t* param); static void getParam(uint8_t* param);
public: public:
static void begin(); static void begin();
static void end(); static void end();
static void spiDriverInit(); static void spiDriverInit();
static void spiSlaveSelect(); static void spiSlaveSelect();
static void spiSlaveDeselect(); static void spiSlaveDeselect();
static char spiTransfer(volatile char data); static char spiTransfer(volatile char data);
static void waitForSlaveReady(); static void waitForSlaveReady();
//static int waitSpiChar(char waitChar, char* readChar); //static int waitSpiChar(char waitChar, char* readChar);
static int waitSpiChar(unsigned char waitChar); static int waitSpiChar(unsigned char waitChar);
static int readAndCheckChar(char checkChar, char* readChar); static int readAndCheckChar(char checkChar, char* readChar);
static char readChar(); static char readChar();
static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params); 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 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 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 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, 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 numParam, uint8_t* param, uint16_t* param_len);
*/ */
static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams); 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 sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM);
static void sendParamLen8(uint8_t param_len); static void sendParamLen8(uint8_t param_len);
static void sendParamLen16(uint16_t param_len); static void sendParamLen16(uint16_t param_len);
static uint8_t readParamLen8(uint8_t* param_len = NULL); static uint8_t readParamLen8(uint8_t* param_len = NULL);
static uint16_t readParamLen16(uint16_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 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 sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM);
static void sendCmd(uint8_t cmd, uint8_t numParam); static void sendCmd(uint8_t cmd, uint8_t numParam);
}; };
extern SpiDrv spiDrv; extern SpiDrv spiDrv;
#endif #endif

View File

@ -1,491 +1,511 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include "Arduino.h" #include "Arduino.h"
#include "spi_drv.h" #include "spi_drv.h"
#include "wifi_drv.h" #include "wifi_drv.h"
#define _DEBUG_ #define _DEBUG_
extern "C" { extern "C" {
#include "wifi_spi.h" #include "wifi_spi.h"
#include "wl_types.h" #include "wl_types.h"
#include "debug.h" #include "debug.h"
} }
// Array of data to cache the information related to the networks discovered // Array of data to cache the information related to the networks discovered
char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}};
int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 }; int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 };
uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 }; uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 };
// Cached values of retrieved data // Cached values of retrieved data
char WiFiDrv::_ssid[] = {0}; char WiFiDrv::_ssid[] = {0};
uint8_t WiFiDrv::_bssid[] = {0}; uint8_t WiFiDrv::_bssid[] = {0};
uint8_t WiFiDrv::_mac[] = {0}; uint8_t WiFiDrv::_mac[] = {0};
uint8_t WiFiDrv::_localIp[] = {0}; uint8_t WiFiDrv::_localIp[] = {0};
uint8_t WiFiDrv::_subnetMask[] = {0}; uint8_t WiFiDrv::_subnetMask[] = {0};
uint8_t WiFiDrv::_gatewayIp[] = {0}; uint8_t WiFiDrv::_gatewayIp[] = {0};
// Firmware version // Firmware version
char WiFiDrv::fwVersion[] = {0}; char WiFiDrv::fwVersion[] = {0};
// Private Methods // Private Methods
void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) 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(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1); SpiDrv::sendCmd(GET_REMOTE_DATA_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM);
//Wait the reply elaboration //Wait the reply elaboration
SpiDrv::waitForSlaveReady(); SpiDrv::waitForSlaveReady();
// Wait for reply // Wait for reply
SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); SpiDrv::waitResponseParams(GET_REMOTE_DATA_CMD, PARAM_NUMS_2, params);
SpiDrv::spiSlaveDeselect(); SpiDrv::spiSlaveDeselect();
} }
// Public Methods
// Public Methods
void WiFiDrv::wifiDriverInit()
{
SpiDrv::begin(); void WiFiDrv::wifiDriverInit()
} {
SpiDrv::begin();
int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) }
{
WAIT_FOR_SLAVE_SELECT(); int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len)
// Send Command {
SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1); WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM); // Send Command
SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1);
//Wait the reply elaboration SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) uint8_t _data = 0;
{ uint8_t _dataLen = 0;
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen))
_data = WL_FAILURE; {
} WARN("error waitResponse");
SpiDrv::spiSlaveDeselect(); _data = WL_FAILURE;
}
return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : 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(); int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len)
// Send Command {
SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2); WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); // Send Command
SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM); SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2);
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
//Wait the reply elaboration SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) uint8_t _data = 0;
{ uint8_t _dataLen = 0;
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen))
_data = WL_FAILURE; {
} WARN("error waitResponse");
SpiDrv::spiSlaveDeselect(); _data = WL_FAILURE;
return _data; }
} 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(); int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len)
// Send Command {
SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3); WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); // Send Command
SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM); SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3);
SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM); SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM);
//Wait the reply elaboration SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) uint8_t _data = 0;
{ uint8_t _dataLen = 0;
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen))
_data = WL_FAILURE; {
} WARN("error waitResponse");
SpiDrv::spiSlaveDeselect(); _data = WL_FAILURE;
return _data; }
} SpiDrv::spiSlaveDeselect();
return _data;
int8_t WiFiDrv::disconnect() }
{
WAIT_FOR_SLAVE_SELECT(); int8_t WiFiDrv::disconnect()
// Send Command {
SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1); WAIT_FOR_SLAVE_SELECT();
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); uint8_t _data = 0;
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen);
return result; SpiDrv::spiSlaveDeselect();
}
return result;
uint8_t WiFiDrv::getConnectionStatus() }
{
WAIT_FOR_SLAVE_SELECT(); uint8_t WiFiDrv::getConnectionStatus()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0);
// Send Command
//Wait the reply elaboration SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = -1;
uint8_t _dataLen = 0; // Wait for reply
SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); uint8_t _data = -1;
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen);
return _data; SpiDrv::spiSlaveDeselect();
}
return _data;
uint8_t* WiFiDrv::getMacAddress() }
{
WAIT_FOR_SLAVE_SELECT(); uint8_t* WiFiDrv::getMacAddress()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1);
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); // Wait for reply
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen);
return _mac; SpiDrv::spiSlaveDeselect();
}
return _mac;
void WiFiDrv::getIpAddress(IPAddress& ip) }
{
getNetworkData(_localIp, _subnetMask, _gatewayIp); void WiFiDrv::getIpAddress(IPAddress& ip)
ip = _localIp; {
} getNetworkData(_localIp, _subnetMask, _gatewayIp);
ip = _localIp;
void WiFiDrv::getSubnetMask(IPAddress& mask) }
{
getNetworkData(_localIp, _subnetMask, _gatewayIp); void WiFiDrv::getSubnetMask(IPAddress& mask)
mask = _subnetMask; {
} getNetworkData(_localIp, _subnetMask, _gatewayIp);
mask = _subnetMask;
void WiFiDrv::getGatewayIP(IPAddress& ip) }
{
getNetworkData(_localIp, _subnetMask, _gatewayIp); void WiFiDrv::getGatewayIP(IPAddress& ip)
ip = _gatewayIp; {
} getNetworkData(_localIp, _subnetMask, _gatewayIp);
ip = _gatewayIp;
char* WiFiDrv::getCurrentSSID() }
{
WAIT_FOR_SLAVE_SELECT(); char* WiFiDrv::getCurrentSSID()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1);
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); // Wait for reply
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen);
return _ssid; SpiDrv::spiSlaveDeselect();
}
return _ssid;
uint8_t* WiFiDrv::getCurrentBSSID() }
{
WAIT_FOR_SLAVE_SELECT(); uint8_t* WiFiDrv::getCurrentBSSID()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1);
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); // Wait for reply
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen);
return _bssid; SpiDrv::spiSlaveDeselect();
}
return _bssid;
int32_t WiFiDrv::getCurrentRSSI() }
{
WAIT_FOR_SLAVE_SELECT(); int32_t WiFiDrv::getCurrentRSSI()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1);
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
int32_t rssi = 0; // Wait for reply
SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); uint8_t _dataLen = 0;
int32_t rssi = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen);
return rssi; SpiDrv::spiSlaveDeselect();
}
return rssi;
uint8_t WiFiDrv::getCurrentEncryptionType() }
{
WAIT_FOR_SLAVE_SELECT(); uint8_t WiFiDrv::getCurrentEncryptionType()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1);
// Send Command
uint8_t _dummy = DUMMY_DATA; SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
uint8_t _dummy = DUMMY_DATA;
//Wait the reply elaboration SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t dataLen = 0;
uint8_t encType = 0; // Wait for reply
SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); uint8_t dataLen = 0;
uint8_t encType = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
return encType; SpiDrv::spiSlaveDeselect();
}
return encType;
int8_t WiFiDrv::startScanNetworks() }
{
WAIT_FOR_SLAVE_SELECT(); int8_t WiFiDrv::startScanNetworks()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0);
// Send Command
//Wait the reply elaboration SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
uint8_t _data = 0;
if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) uint8_t _dataLen = 0;
{
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen))
_data = WL_FAILURE; {
} WARN("error waitResponse");
_data = WL_FAILURE;
SpiDrv::spiSlaveDeselect(); }
return (_data == WL_FAILURE)? _data : WL_SUCCESS; SpiDrv::spiSlaveDeselect();
}
return (_data == WL_FAILURE)? _data : WL_SUCCESS;
}
uint8_t WiFiDrv::getScanNetworks()
{
WAIT_FOR_SLAVE_SELECT(); uint8_t WiFiDrv::getScanNetworks()
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0);
// Send Command
//Wait the reply elaboration SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t ssidListNum = 0;
SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); // Wait for reply
uint8_t ssidListNum = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM);
return ssidListNum; SpiDrv::spiSlaveDeselect();
}
return ssidListNum;
char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) }
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM) char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem)
return NULL; {
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
return _networkSsid[networkItem]; return NULL;
}
return _networkSsid[networkItem];
uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) }
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM) uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem)
return NULL; {
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
WAIT_FOR_SLAVE_SELECT(); return NULL;
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1);
// Send Command
SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1);
//Wait the reply elaboration SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t dataLen = 0;
uint8_t encType = 0; // Wait for reply
SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); uint8_t dataLen = 0;
uint8_t encType = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
return encType; SpiDrv::spiSlaveDeselect();
}
return encType;
int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) }
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM) int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem)
return NULL; {
int32_t networkRssi = 0; if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
return NULL;
WAIT_FOR_SLAVE_SELECT(); int32_t networkRssi = 0;
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1);
// Send Command
SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1);
//Wait the reply elaboration SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t dataLen = 0;
SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen); // Wait for reply
uint8_t dataLen = 0;
SpiDrv::spiSlaveDeselect(); SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen);
return networkRssi; SpiDrv::spiSlaveDeselect();
}
return networkRssi;
uint8_t WiFiDrv::reqHostByName(const char* aHostname) }
{
WAIT_FOR_SLAVE_SELECT(); uint8_t WiFiDrv::reqHostByName(const char* aHostname)
{
// Send Command WAIT_FOR_SLAVE_SELECT();
SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1);
SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); // Send Command
SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1);
//Wait the reply elaboration SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _data = 0;
uint8_t _dataLen = 0; // Wait for reply
uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen); uint8_t _data = 0;
uint8_t _dataLen = 0;
SpiDrv::spiSlaveDeselect(); uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen);
return result; SpiDrv::spiSlaveDeselect();
}
return result;
int WiFiDrv::getHostByName(IPAddress& aResult) }
{
uint8_t _ipAddr[WL_IPV4_LENGTH]; int WiFiDrv::getHostByName(IPAddress& aResult)
IPAddress dummy(0xFF,0xFF,0xFF,0xFF); {
int result = 0; uint8_t _ipAddr[WL_IPV4_LENGTH];
IPAddress dummy(0xFF,0xFF,0xFF,0xFF);
WAIT_FOR_SLAVE_SELECT(); int result = 0;
// Send Command
SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); WAIT_FOR_SLAVE_SELECT();
// Send Command
//Wait the reply elaboration SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen)) // Wait for reply
{ uint8_t _dataLen = 0;
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen))
}else{ {
aResult = _ipAddr; WARN("error waitResponse");
result = (aResult != dummy); }else{
} aResult = _ipAddr;
SpiDrv::spiSlaveDeselect(); result = (aResult != dummy);
return result; }
} SpiDrv::spiSlaveDeselect();
return result;
int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) }
{
uint8_t retry = 10; int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult)
if (reqHostByName(aHostname)) {
{ uint8_t retry = 10;
while(!getHostByName(aResult) && --retry > 0) if (reqHostByName(aHostname))
{ {
delay(1000); while(!getHostByName(aResult) && --retry > 0)
} {
}else{ delay(1000);
return 0; }
} }else{
return (retry>0); return 0;
} }
return (retry>0);
char* WiFiDrv::getFwVersion() }
{
WAIT_FOR_SLAVE_SELECT(); char* WiFiDrv::getFwVersion()
// Send Command {
SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0); WAIT_FOR_SLAVE_SELECT();
// Send Command
//Wait the reply elaboration SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0);
SpiDrv::waitForSlaveReady();
//Wait the reply elaboration
// Wait for reply SpiDrv::waitForSlaveReady();
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen)) // Wait for reply
{ uint8_t _dataLen = 0;
WARN("error waitResponse"); if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen))
} {
SpiDrv::spiSlaveDeselect(); WARN("error waitResponse");
return fwVersion; }
} SpiDrv::spiSlaveDeselect();
return fwVersion;
WiFiDrv wiFiDrv; }
WiFiDrv wiFiDrv;

View File

@ -1,219 +1,227 @@
#ifndef WiFi_Drv_h #ifndef WiFi_Drv_h
#define WiFi_Drv_h #define WiFi_Drv_h
#include <inttypes.h> #include <inttypes.h>
#include "wifi_spi.h" #include "wifi_spi.h"
#include "IPAddress.h" #include "IPAddress.h"
#include "../WiFiUdp.h"
// Key index length
#define KEY_IDX_LEN 1 // Key index length
// 5 secs of delay to have the connection established #define KEY_IDX_LEN 1
#define WL_DELAY_START_CONNECTION 5000 // 5 secs of delay to have the connection established
// firmware version string length #define WL_DELAY_START_CONNECTION 5000
#define WL_FW_VER_LENGTH 6 // firmware version string length
#define WL_FW_VER_LENGTH 6
class WiFiDrv
{ class WiFiDrv
private: {
// settings of requested network private:
static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH]; // settings of requested network
static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH];
static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; 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]; // 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]; // settings of current selected network
static uint8_t _bssid[WL_MAC_ADDR_LENGTH]; static char _ssid[WL_SSID_MAX_LENGTH];
static uint8_t _mac[WL_MAC_ADDR_LENGTH]; static uint8_t _bssid[WL_MAC_ADDR_LENGTH];
static uint8_t _localIp[WL_IPV4_LENGTH]; static uint8_t _mac[WL_MAC_ADDR_LENGTH];
static uint8_t _subnetMask[WL_IPV4_LENGTH]; static uint8_t _localIp[WL_IPV4_LENGTH];
static uint8_t _gatewayIp[WL_IPV4_LENGTH]; static uint8_t _subnetMask[WL_IPV4_LENGTH];
static uint8_t _gatewayIp[WL_IPV4_LENGTH];
/*
* Get network Data information /*
*/ * Get network Data information
static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); */
static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
static uint8_t reqHostByName(const char* aHostname);
static uint8_t reqHostByName(const char* aHostname);
static int getHostByName(IPAddress& aResult);
static int getHostByName(IPAddress& aResult);
public:
/* /*
* Driver initialization * Get remote Data information on UDP socket
*/ */
static void wifiDriverInit(); static void getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port);
/* public:
* Set the desired network which the connection manager should try to
* connect to. /*
* * Driver initialization
* The ssid of the desired network should be specified. */
* static void wifiDriverInit();
* param ssid: The ssid of the desired network.
* param ssid_len: Lenght of ssid string. /*
* return: WL_SUCCESS or WL_FAILURE * Set the desired network which the connection manager should try to
*/ * connect to.
static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); *
* 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();
friend class WiFiUDP;
/* Start Wifi connection with passphrase };
* the most secure supported mode will be automatically selected
* extern WiFiDrv wiFiDrv;
* param ssid: Pointer to the SSID string.
* param ssid_len: Lenght of ssid string. #endif
* 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

View File

@ -1,144 +1,148 @@
#ifndef WiFi_Spi_h #ifndef WiFi_Spi_h
#define WiFi_Spi_h #define WiFi_Spi_h
#include "wl_definitions.h" #include "wl_definitions.h"
#define CMD_FLAG 0 #define CMD_FLAG 0
#define REPLY_FLAG 1<<7 #define REPLY_FLAG 1<<7
#define DATA_FLAG 0x40 #define DATA_FLAG 0x40
#define WIFI_SPI_ACK 1 #define WIFI_SPI_ACK 1
#define WIFI_SPI_ERR 0xFF #define WIFI_SPI_ERR 0xFF
#define TIMEOUT_CHAR 1000 #define TIMEOUT_CHAR 1000
//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ //#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */
#define NO_SOCKET_AVAIL 255 #define NO_SOCKET_AVAIL 255
#define START_CMD 0xE0 #define START_CMD 0xE0
#define END_CMD 0xEE #define END_CMD 0xEE
#define ERR_CMD 0xEF #define ERR_CMD 0xEF
enum { enum {
SET_NET_CMD = 0x10, SET_NET_CMD = 0x10,
SET_PASSPHRASE_CMD = 0x11, SET_PASSPHRASE_CMD = 0x11,
SET_KEY_CMD = 0x12, SET_KEY_CMD = 0x12,
TEST_CMD = 0x13, TEST_CMD = 0x13,
GET_CONN_STATUS_CMD = 0x20, GET_CONN_STATUS_CMD = 0x20,
GET_IPADDR_CMD = 0x21, GET_IPADDR_CMD = 0x21,
GET_MACADDR_CMD = 0x22, GET_MACADDR_CMD = 0x22,
GET_CURR_SSID_CMD = 0x23, GET_CURR_SSID_CMD = 0x23,
GET_CURR_BSSID_CMD = 0x24, GET_CURR_BSSID_CMD = 0x24,
GET_CURR_RSSI_CMD = 0x25, GET_CURR_RSSI_CMD = 0x25,
GET_CURR_ENCT_CMD = 0x26, GET_CURR_ENCT_CMD = 0x26,
SCAN_NETWORKS = 0x27, SCAN_NETWORKS = 0x27,
START_SERVER_TCP_CMD= 0x28, START_SERVER_TCP_CMD= 0x28,
GET_STATE_TCP_CMD = 0x29, GET_STATE_TCP_CMD = 0x29,
DATA_SENT_TCP_CMD = 0x2A, DATA_SENT_TCP_CMD = 0x2A,
AVAIL_DATA_TCP_CMD = 0x2B, AVAIL_DATA_TCP_CMD = 0x2B,
GET_DATA_TCP_CMD = 0x2C, GET_DATA_TCP_CMD = 0x2C,
START_CLIENT_TCP_CMD= 0x2D, START_CLIENT_TCP_CMD= 0x2D,
STOP_CLIENT_TCP_CMD = 0x2E, STOP_CLIENT_TCP_CMD = 0x2E,
GET_CLIENT_STATE_TCP_CMD= 0x2F, GET_CLIENT_STATE_TCP_CMD= 0x2F,
DISCONNECT_CMD = 0x30, DISCONNECT_CMD = 0x30,
GET_IDX_SSID_CMD = 0x31, GET_IDX_SSID_CMD = 0x31,
GET_IDX_RSSI_CMD = 0x32, GET_IDX_RSSI_CMD = 0x32,
GET_IDX_ENCT_CMD = 0x33, GET_IDX_ENCT_CMD = 0x33,
REQ_HOST_BY_NAME_CMD= 0x34, REQ_HOST_BY_NAME_CMD= 0x34,
GET_HOST_BY_NAME_CMD= 0x35, GET_HOST_BY_NAME_CMD= 0x35,
START_SCAN_NETWORKS = 0x36, START_SCAN_NETWORKS = 0x36,
GET_FW_VERSION_CMD = 0x37, GET_FW_VERSION_CMD = 0x37,
GET_TEST_CMD = 0x38,
// All command with DATA_FLAG 0x40 send a 16bit Len SEND_DATA_UDP_CMD = 0x39,
GET_REMOTE_DATA_CMD = 0x3A,
SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45, // All command with DATA_FLAG 0x40 send a 16bit Len
};
SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45,
enum wl_tcp_state { INSERT_DATABUF_CMD = 0x46,
CLOSED = 0, };
LISTEN = 1,
SYN_SENT = 2,
SYN_RCVD = 3, enum wl_tcp_state {
ESTABLISHED = 4, CLOSED = 0,
FIN_WAIT_1 = 5, LISTEN = 1,
FIN_WAIT_2 = 6, SYN_SENT = 2,
CLOSE_WAIT = 7, SYN_RCVD = 3,
CLOSING = 8, ESTABLISHED = 4,
LAST_ACK = 9, FIN_WAIT_1 = 5,
TIME_WAIT = 10 FIN_WAIT_2 = 6,
}; CLOSE_WAIT = 7,
CLOSING = 8,
LAST_ACK = 9,
enum numParams{ TIME_WAIT = 10
PARAM_NUMS_0, };
PARAM_NUMS_1,
PARAM_NUMS_2,
PARAM_NUMS_3, enum numParams{
PARAM_NUMS_4, PARAM_NUMS_0,
PARAM_NUMS_5, PARAM_NUMS_1,
MAX_PARAM_NUMS PARAM_NUMS_2,
}; PARAM_NUMS_3,
PARAM_NUMS_4,
#define MAX_PARAMS MAX_PARAM_NUMS-1 PARAM_NUMS_5,
#define PARAM_LEN_SIZE 1 MAX_PARAM_NUMS
};
typedef struct __attribute__((__packed__))
{ #define MAX_PARAMS MAX_PARAM_NUMS-1
uint8_t paramLen; #define PARAM_LEN_SIZE 1
char* param;
}tParam; typedef struct __attribute__((__packed__))
{
typedef struct __attribute__((__packed__)) uint8_t paramLen;
{ char* param;
uint16_t dataLen; }tParam;
char* data;
}tDataParam; typedef struct __attribute__((__packed__))
{
uint16_t dataLen;
typedef struct __attribute__((__packed__)) char* data;
{ }tDataParam;
unsigned char cmd;
unsigned char tcmd;
unsigned char nParam; typedef struct __attribute__((__packed__))
tParam params[MAX_PARAMS]; {
}tSpiMsg; unsigned char cmd;
unsigned char tcmd;
typedef struct __attribute__((__packed__)) unsigned char nParam;
{ tParam params[MAX_PARAMS];
unsigned char cmd; }tSpiMsg;
unsigned char tcmd;
unsigned char nParam; typedef struct __attribute__((__packed__))
tDataParam params[MAX_PARAMS]; {
}tSpiMsgData; unsigned char cmd;
unsigned char tcmd;
unsigned char nParam;
typedef struct __attribute__((__packed__)) tDataParam params[MAX_PARAMS];
{ }tSpiMsgData;
unsigned char cmd;
unsigned char tcmd;
//unsigned char totLen; typedef struct __attribute__((__packed__))
unsigned char nParam; {
}tSpiHdr; unsigned char cmd;
unsigned char tcmd;
typedef struct __attribute__((__packed__)) //unsigned char totLen;
{ unsigned char nParam;
uint8_t paramLen; }tSpiHdr;
uint32_t param;
}tLongParam; typedef struct __attribute__((__packed__))
{
typedef struct __attribute__((__packed__)) uint8_t paramLen;
{ uint32_t param;
uint8_t paramLen; }tLongParam;
uint16_t param;
}tIntParam; typedef struct __attribute__((__packed__))
{
typedef struct __attribute__((__packed__)) uint8_t paramLen;
{ uint16_t param;
uint8_t paramLen; }tIntParam;
uint8_t param;
}tByteParam; typedef struct __attribute__((__packed__))
{
#endif uint8_t paramLen;
uint8_t param;
}tByteParam;
#endif

View File

@ -22,6 +22,8 @@
#define WL_NETWORKS_LIST_MAXNUM 10 #define WL_NETWORKS_LIST_MAXNUM 10
// Maxmium number of socket // Maxmium number of socket
#define MAX_SOCK_NUM 4 #define MAX_SOCK_NUM 4
// Default state value for Wifi state field
#define NA_STATE -1
//Maximum number of attempts to establish wifi connection //Maximum number of attempts to establish wifi connection
#define WL_MAX_ATTEMPT_CONNECTION 10 #define WL_MAX_ATTEMPT_CONNECTION 10