This commit is contained in:
Wolfgang Hottgenroth
2015-05-02 00:14:40 +02:00
commit 40e18d4172
40 changed files with 6547 additions and 0 deletions

95
WiFi/utility/debug.h Normal file
View File

@ -0,0 +1,95 @@
/*
debug.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//*********************************************/
//
// File: debug.h
//
// Author: dlf (Metodo2 srl)
//
//********************************************/
#ifndef Debug_H
#define Debug_H
#include <stdio.h>
#include <string.h>
#define PRINT_FILE_LINE() do { \
Serial.print("[");Serial.print(__FILE__); \
Serial.print("::");Serial.print(__LINE__);Serial.print("]");\
}while (0);
#ifdef _DEBUG_
#define INFO(format, args...) do { \
char buf[250]; \
sprintf(buf, format, args); \
Serial.println(buf); \
} while(0);
#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\
Serial.println(x); \
}while (0);
#define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\
Serial.print(x,16);Serial.print(",");Serial.println(y,16); \
}while (0);
#else
#define INFO1(x) do {} while(0);
#define INFO2(x,y) do {} while(0);
#define INFO(format, args...) do {} while(0);
#endif
#if 0
#define WARN(args) do { PRINT_FILE_LINE() \
Serial.print("-W-"); Serial.println(args); \
}while (0);
#else
#define WARN(args) do {} while (0);
#endif
#if _DEBUG_SPI_
#define DBG_PIN2 5
#define DBG_PIN 4
#define START() digitalWrite(DBG_PIN2, HIGH);
#define END() digitalWrite(DBG_PIN2, LOW);
#define SET_TRIGGER() digitalWrite(DBG_PIN, HIGH);
#define RST_TRIGGER() digitalWrite(DBG_PIN, LOW);
#define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \
pinMode(DBG_PIN2, OUTPUT); \
RST_TRIGGER()
#define TOGGLE_TRIGGER() SET_TRIGGER() \
delayMicroseconds(2); \
RST_TRIGGER()
#else
#define START()
#define END()
#define SET_TRIGGER()
#define RST_TRIGGER()
#define INIT_TRIGGER()
#define TOGGLE_TRIGGER()
#endif
#endif

327
WiFi/utility/server_drv.cpp Normal file
View File

@ -0,0 +1,327 @@
/*
server_drv.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#define _DEBUG_
#include "utility/server_drv.h"
#include "Arduino.h"
#include "utility/spi_drv.h"
extern "C" {
#include "utility/wl_types.h"
#include "utility/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(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();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(INSERT_DATABUF_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::sendUdpData(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(SEND_DATA_UDP_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::waitResponseData8(SEND_DATA_UDP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
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);
//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;

60
WiFi/utility/server_drv.h Normal file
View File

@ -0,0 +1,60 @@
/*
server_drv.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef Server_Drv_h
#define Server_Drv_h
#include <inttypes.h>
#include "utility/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

496
WiFi/utility/spi_drv.cpp Normal file
View File

@ -0,0 +1,496 @@
/*
spi_drv.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Arduino.h"
#include <SPI.h>
#include "utility/spi_drv.h"
#include "pins_arduino.h"
//#define _DEBUG_
extern "C" {
#include "utility/debug.h"
}
#define DATAOUT 11 // MOSI
#define DATAIN 12 // MISO
#define SPICLOCK 13 // sck
#define SLAVESELECT 10 // ss
#define SLAVEREADY 7 // handshake pin
#define WIFILED 9 // led on wifi shield
#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); } while (++ii < (X*F_CPU/16000000)); }
#define DELAY_TRANSFER() DELAY_SPI(10)
void SpiDrv::begin()
{
SPI.begin();
pinMode(SLAVESELECT, OUTPUT);
pinMode(SLAVEREADY, INPUT);
pinMode(WIFILED, OUTPUT);
digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(SS, HIGH);
digitalWrite(SLAVESELECT, HIGH);
digitalWrite(WIFILED, LOW);
#ifdef _DEBUG_
INIT_TRIGGER()
#endif
}
void SpiDrv::end() {
SPI.end();
}
void SpiDrv::spiSlaveSelect()
{
digitalWrite(SLAVESELECT,LOW);
}
void SpiDrv::spiSlaveDeselect()
{
digitalWrite(SLAVESELECT,HIGH);
}
char SpiDrv::spiTransfer(volatile char data)
{
char result = SPI.transfer(data);
DELAY_TRANSFER();
return result; // return the received byte
}
int SpiDrv::waitSpiChar(unsigned char waitChar)
{
int timeout = TIMEOUT_CHAR;
unsigned char _readChar = 0;
do{
_readChar = readChar(); //get data byte
if (_readChar == ERR_CMD)
{
WARN("Err cmd received\n");
return -1;
}
}while((timeout-- > 0) && (_readChar != waitChar));
return (_readChar == waitChar);
}
int SpiDrv::readAndCheckChar(char checkChar, char* readChar)
{
getParam((uint8_t*)readChar);
return (*readChar == checkChar);
}
char SpiDrv::readChar()
{
uint8_t readChar = 0;
getParam(&readChar);
return readChar;
}
#define WAIT_START_CMD(x) waitSpiChar(START_CMD)
#define IF_CHECK_START_CMD(x) \
if (!WAIT_START_CMD(_data)) \
{ \
TOGGLE_TRIGGER() \
WARN("Error waiting START_CMD"); \
return 0; \
}else \
#define CHECK_DATA(check, x) \
if (!readAndCheckChar(check, &x)) \
{ \
TOGGLE_TRIGGER() \
WARN("Reply error"); \
INFO2(check, (uint8_t)x); \
return 0; \
}else \
#define waitSlaveReady() (digitalRead(SLAVEREADY) == LOW)
#define waitSlaveSign() (digitalRead(SLAVEREADY) == HIGH)
#define waitSlaveSignalH() while(digitalRead(SLAVEREADY) != HIGH){}
#define waitSlaveSignalL() while(digitalRead(SLAVEREADY) != LOW){}
void SpiDrv::waitForSlaveSign()
{
while (!waitSlaveSign());
}
void SpiDrv::waitForSlaveReady()
{
while (!waitSlaveReady());
}
void SpiDrv::getParam(uint8_t* param)
{
// Get Params data
*param = spiTransfer(DUMMY_DATA);
DELAY_TRANSFER();
}
int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len)
{
char _data = 0;
int ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
CHECK_DATA(numParam, _data);
{
readParamLen8(param_len);
for (ii=0; ii<(*param_len); ++ii)
{
// Get Params data
//param[ii] = spiTransfer(DUMMY_DATA);
getParam(&param[ii]);
}
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
/*
int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len)
{
char _data = 0;
int i =0, ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
CHECK_DATA(numParam, _data);
{
readParamLen16(param_len);
for (ii=0; ii<(*param_len); ++ii)
{
// Get Params data
param[ii] = spiTransfer(DUMMY_DATA);
}
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
*/
int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len)
{
char _data = 0;
uint16_t ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
uint8_t numParam = readChar();
if (numParam != 0)
{
readParamLen16(param_len);
for (ii=0; ii<(*param_len); ++ii)
{
// Get Params data
param[ii] = spiTransfer(DUMMY_DATA);
}
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len)
{
char _data = 0;
int ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
uint8_t numParam = readChar();
if (numParam != 0)
{
readParamLen8(param_len);
for (ii=0; ii<(*param_len); ++ii)
{
// Get Params data
param[ii] = spiTransfer(DUMMY_DATA);
}
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params)
{
char _data = 0;
int i =0, ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
uint8_t _numParam = readChar();
if (_numParam != 0)
{
for (i=0; i<_numParam; ++i)
{
params[i].paramLen = readParamLen8();
for (ii=0; ii<params[i].paramLen; ++ii)
{
// Get Params data
params[i].param[ii] = spiTransfer(DUMMY_DATA);
}
}
} else
{
WARN("Error numParam == 0");
return 0;
}
if (numParam != _numParam)
{
WARN("Mismatch numParam");
return 0;
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
/*
int SpiDrv::waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams)
{
char _data = 0;
int i =0, ii = 0;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
uint8_t numParam = readChar();
if (numParam > maxNumParams)
{
numParam = maxNumParams;
}
*numParamRead = numParam;
if (numParam != 0)
{
for (i=0; i<numParam; ++i)
{
params[i].paramLen = readParamLen8();
for (ii=0; ii<params[i].paramLen; ++ii)
{
// Get Params data
params[i].param[ii] = spiTransfer(DUMMY_DATA);
}
}
} else
{
WARN("Error numParams == 0");
Serial.println(cmd, 16);
return 0;
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
*/
int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams)
{
char _data = 0;
int i =0, ii = 0;
char *index[WL_SSID_MAX_LENGTH];
for (i = 0 ; i < WL_NETWORKS_LIST_MAXNUM ; i++)
index[i] = (char *)params + WL_SSID_MAX_LENGTH*i;
IF_CHECK_START_CMD(_data)
{
CHECK_DATA(cmd | REPLY_FLAG, _data){};
uint8_t numParam = readChar();
if (numParam > maxNumParams)
{
numParam = maxNumParams;
}
*numParamRead = numParam;
if (numParam != 0)
{
for (i=0; i<numParam; ++i)
{
uint8_t paramLen = readParamLen8();
for (ii=0; ii<paramLen; ++ii)
{
//ssid[ii] = spiTransfer(DUMMY_DATA);
// Get Params data
index[i][ii] = (uint8_t)spiTransfer(DUMMY_DATA);
}
index[i][ii]=0;
}
} else
{
WARN("Error numParams == 0");
readAndCheckChar(END_CMD, &_data);
return 0;
}
readAndCheckChar(END_CMD, &_data);
}
return 1;
}
void SpiDrv::sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam)
{
int i = 0;
// Send Spi paramLen
sendParamLen8(param_len);
// Send Spi param data
for (i=0; i<param_len; ++i)
{
spiTransfer(param[i]);
}
// if lastParam==1 Send Spi END CMD
if (lastParam == 1)
spiTransfer(END_CMD);
}
void SpiDrv::sendParamLen8(uint8_t param_len)
{
// Send Spi paramLen
spiTransfer(param_len);
}
void SpiDrv::sendParamLen16(uint16_t param_len)
{
// Send Spi paramLen
spiTransfer((uint8_t)((param_len & 0xff00)>>8));
spiTransfer((uint8_t)(param_len & 0xff));
}
uint8_t SpiDrv::readParamLen8(uint8_t* param_len)
{
uint8_t _param_len = spiTransfer(DUMMY_DATA);
if (param_len != NULL)
{
*param_len = _param_len;
}
return _param_len;
}
uint16_t SpiDrv::readParamLen16(uint16_t* param_len)
{
uint16_t _param_len = spiTransfer(DUMMY_DATA)<<8 | (spiTransfer(DUMMY_DATA)& 0xff);
if (param_len != NULL)
{
*param_len = _param_len;
}
return _param_len;
}
void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam)
{
uint16_t i = 0;
// Send Spi paramLen
sendParamLen16(param_len);
// Send Spi param data
for (i=0; i<param_len; ++i)
{
spiTransfer(param[i]);
}
// if lastParam==1 Send Spi END CMD
if (lastParam == 1)
spiTransfer(END_CMD);
}
void SpiDrv::sendParam(uint16_t param, uint8_t lastParam)
{
// Send Spi paramLen
sendParamLen8(2);
spiTransfer((uint8_t)((param & 0xff00)>>8));
spiTransfer((uint8_t)(param & 0xff));
// if lastParam==1 Send Spi END CMD
if (lastParam == 1)
spiTransfer(END_CMD);
}
/* Cmd Struct Message */
/* _________________________________________________________________________________ */
/*| START CMD | C/R | CMD |[TOT LEN]| N.PARAM | PARAM LEN | PARAM | .. | END CMD | */
/*|___________|______|______|_________|_________|___________|________|____|_________| */
/*| 8 bit | 1bit | 7bit | 8bit | 8bit | 8bit | nbytes | .. | 8bit | */
/*|___________|______|______|_________|_________|___________|________|____|_________| */
void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam)
{
// Send Spi START CMD
spiTransfer(START_CMD);
//waitForSlaveSign();
//wait the interrupt trigger on slave
delayMicroseconds(SPI_START_CMD_DELAY);
// Send Spi C + cmd
spiTransfer(cmd & ~(REPLY_FLAG));
// Send Spi totLen
//spiTransfer(totLen);
// Send Spi numParam
spiTransfer(numParam);
// If numParam == 0 send END CMD
if (numParam == 0)
spiTransfer(END_CMD);
}
SpiDrv spiDrv;

102
WiFi/utility/spi_drv.h Normal file
View File

@ -0,0 +1,102 @@
/*
spi_drv.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SPI_Drv_h
#define SPI_Drv_h
#include <inttypes.h>
#include "utility/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

579
WiFi/utility/wifi_drv.cpp Normal file
View File

@ -0,0 +1,579 @@
/*
wifi_drv.cpp - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "Arduino.h"
#include "utility/spi_drv.h"
#include "utility/wifi_drv.h"
#define _DEBUG_
extern "C" {
#include "utility/wifi_spi.h"
#include "utility/wl_types.h"
#include "utility/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_2] = { {0, (char*)ip}, {0, (char*)port} };
WAIT_FOR_SLAVE_SELECT();
// Send Command
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
SpiDrv::waitResponseParams(GET_REMOTE_DATA_CMD, PARAM_NUMS_2, 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;
}
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;

267
WiFi/utility/wifi_drv.h Normal file
View File

@ -0,0 +1,267 @@
/*
wifi_drv.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef WiFi_Drv_h
#define WiFi_Drv_h
#include <inttypes.h>
#include "utility/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

173
WiFi/utility/wifi_spi.h Normal file
View File

@ -0,0 +1,173 @@
/*
wifi_spi.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef WiFi_Spi_h
#define WiFi_Spi_h
#include <inttypes.h>
#include "utility/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

View File

@ -0,0 +1,72 @@
/*
wl_definitions.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* wl_definitions.h
*
* Created on: Mar 6, 2011
* Author: dlafauci
*/
#ifndef WL_DEFINITIONS_H_
#define WL_DEFINITIONS_H_
// Maximum size of a SSID
#define WL_SSID_MAX_LENGTH 32
// Length of passphrase. Valid lengths are 8-63.
#define WL_WPA_KEY_MAX_LENGTH 63
// Length of key in bytes. Valid values are 5 and 13.
#define WL_WEP_KEY_MAX_LENGTH 13
// Size of a MAC-address or BSSID
#define WL_MAC_ADDR_LENGTH 6
// Size of a MAC-address or BSSID
#define WL_IPV4_LENGTH 4
// Maximum size of a SSID list
#define WL_NETWORKS_LIST_MAXNUM 10
// Maxmium number of socket
#define MAX_SOCK_NUM 4
// Socket not available constant
#define SOCK_NOT_AVAIL 255
// 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
typedef enum {
WL_NO_SHIELD = 255,
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL,
WL_SCAN_COMPLETED,
WL_CONNECTED,
WL_CONNECT_FAILED,
WL_CONNECTION_LOST,
WL_DISCONNECTED
} wl_status_t;
/* Encryption modes */
enum wl_enc_type { /* Values map to 802.11 encryption suites... */
ENC_TYPE_WEP = 5,
ENC_TYPE_TKIP = 2,
ENC_TYPE_CCMP = 4,
/* ... except these two, 7 and 8 are reserved in 802.11-2007 */
ENC_TYPE_NONE = 7,
ENC_TYPE_AUTO = 8
};
#endif /* WL_DEFINITIONS_H_ */

49
WiFi/utility/wl_types.h Normal file
View File

@ -0,0 +1,49 @@
/*
wl_types.h - Library for Arduino Wifi shield.
Copyright (c) 2011-2014 Arduino. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* wl_types.h
*
* Created on: Jul 30, 2010
* Author: dlafauci
*/
#ifndef _WL_TYPES_H_
#define _WL_TYPES_H_
#include <inttypes.h>
typedef enum {
WL_FAILURE = -1,
WL_SUCCESS = 1,
} wl_error_code_t;
/* Authentication modes */
enum wl_auth_mode {
AUTH_MODE_INVALID,
AUTH_MODE_AUTO,
AUTH_MODE_OPEN_SYSTEM,
AUTH_MODE_SHARED_KEY,
AUTH_MODE_WPA,
AUTH_MODE_WPA2,
AUTH_MODE_WPA_PSK,
AUTH_MODE_WPA2_PSK
};
#endif //_WL_TYPES_H_