1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

Changes on WiFi API after review. Add driver utility implementation

This commit is contained in:
mlafauci 2011-03-08 22:13:54 +01:00
parent 9d60bbb0dc
commit 765e848fdb
22 changed files with 1904 additions and 350 deletions

View File

@ -1,5 +1,6 @@
extern "C" {
#include "wl_types.h"
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
#include "socket.h"
#include "string.h"
}
@ -11,45 +12,28 @@ extern "C" {
#include "Server.h"
#include "server_drv.h"
uint16_t Client::_srcport = 0;
uint16_t Client::_srcport = 1024;
Client::Client(uint8_t sock) {
_sock = sock;
Client::Client(uint8_t sock) : _sock(sock) {
}
Client::Client(uint8_t *ip, uint16_t port) {
_ip = ip;
_port = port;
_sock = 255;
}
uint8_t Client::getFirstSock()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiClass::_state[i] < 0)
{
return i;
}
}
return SOCK_NOT_AVAIL;
Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) {
}
uint8_t Client::connect() {
_sock = getFirstSock();
// _srcport++;
// if (_srcport + 1024 == 0) _srcport = 0;
_sock = getFirstSocket();
//TODO implementation
_socket = socket(TCP_SOCKET);
if (_socket<0)
{
return 0;
return 0;
}else{
WiFiClass::_state[_sock] = _socket;
}
if (!::connect(_socket, _ip, _port)) {
return 0;
}
//
// if (!::connect(_socket, _ip, _port)) {
// return 0;
// }
return 1;
}
@ -97,8 +81,8 @@ int Client::read() {
}
int Client::readBuf(uint8_t* buf, uint16_t* len) {
if (!ServerDrv::getDataBuf(_sock, buf, len))
int Client::read(uint8_t* buf, size_t size) {
if (!ServerDrv::getDataBuf(_sock, buf, &size))
return -1;
return 0;
}
@ -146,19 +130,19 @@ uint8_t Client::status() {
}
}
// the next three functions are a hack so we can compare the client returned
// by Server::available() to null, or use it as the condition in an
// if-statement. this lets us stay compatible with the Processing network
// library.
uint8_t Client::operator==(int p) {
return _sock == 255;
}
uint8_t Client::operator!=(int p) {
return _sock != 255;
}
Client::operator bool() {
return _sock != 255;
}
// Private Methods
uint8_t Client::getFirstSocket()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiClass::_state[i] < 0)
{
return i;
}
}
return SOCK_NOT_AVAIL;
}

View File

@ -1,35 +1,37 @@
#ifndef Client_h
#define Client_h
#include "IPAddress.h"
#include "Print.h"
class Client : public Print {
private:
static uint16_t _srcport;
uint8_t _sock; //not used
uint8_t *_ip;
uint16_t _port;
uint16_t _socket;
public:
Client(uint8_t);
Client(uint8_t *, uint16_t);
Client(uint8_t sock);
Client(IPAddress& ip, uint16_t port);
uint8_t status();
uint8_t connect();
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
int available();
int read();
int readBuf(uint8_t* buf, uint16_t* len);
virtual int available();
virtual int read();
virtual int read(uint8_t* buf, size_t size);
void flush();
void stop();
uint8_t connected();
uint8_t operator==(int);
uint8_t operator!=(int);
operator bool();
uint8_t getFirstSock();
friend class Server;
private:
static uint16_t _srcport;
uint8_t _sock; //not used
IPAddress _ip;
uint16_t _port;
uint16_t _socket;
uint8_t getFirstSocket();
};
#endif

View File

@ -1,6 +1,4 @@
#include <string.h>
#include "HardwareSerial.h"
#include "wifi_spi.h"
#include "Server.h"
#include "Client.h"
#include "WiFi.h"

View File

@ -2,7 +2,7 @@
#define Server_h
extern "C" {
#include "utility/wl_types.h"
#include "utility/wl_definitions.h"
}
#include "Print.h"

View File

@ -1,42 +1,32 @@
#include "WProgram.h"
#include "WiFi.h"
#include <EEPROM.h>
#include "HardwareSerial.h"
#include "wifi_drv.h"
#include "WiFi.h"
#include "wiring.h"
extern "C" {
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
}
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
char WiFiClass::ssid[WL_SSID_MAX_LENGTH] = { 0 };
uint8_t WiFiClass::ssid_len;
char WiFiClass::key[13] = { 0 };
uint8_t WiFiClass::key_len;
char WiFiClass::passph[63] = { 0 };
uint8_t WiFiClass::passph_len;
wl_status_t WiFiClass::status;
char WiFiClass::_ssid[] = { 0 };
char WiFiClass::_key[] = { 0 };
char WiFiClass::_passph[] = { 0 };
wl_status_t WiFiClass::_status;
void readEEdata(int addr, uint8_t* data, uint8_t len)
{
for (int i = 0; i < len; ++i)
{
data[i]= EEPROM.read(addr);
}
}
WiFiClass::WiFiClass()
{
init();
}
void WiFiClass::init()
{
Serial.begin(9600);
Serial.println("WiFi initializing...");
WiFiDrv::wifi_drv_init();
WiFiDrv::wifiDriverInit();
}
uint8_t WiFiClass::getSocket()
{
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
@ -49,136 +39,118 @@ uint8_t WiFiClass::getSocket()
return NO_SOCKET_AVAIL;
}
void WiFiClass::begin()
int WiFiClass::begin()
{
init();
Serial.println("WiFi Starting...");
ssid_len = EEPROM.read(EE_WIFI_SSID_LEN);
if ((ssid_len == 0)||(ssid_len > WL_SSID_MAX_LENGTH))
{
Serial.println("No SSID in EEPROM");
status = WL_NO_SSID_AVAIL;
return;
}
readEEdata(EE_WIFI_SSID_DATA, (uint8_t*)&ssid[0], ssid_len);
Serial.print("SSID: ");
Serial.print(ssid_len, 10);
Serial.print(" - ");
Serial.print(ssid[0]);
Serial.println("");
key_len = EEPROM.read(EE_WIFI_KEY_LEN);
if (key_len == 0)
{
Serial.println("No PASSPHRASE in EEPROM");
passph_len = EEPROM.read(EE_WIFI_PASSPH_LEN);
if (passph_len == 0)
{
begin(ssid, ssid_len);
}else{
readEEdata(EE_WIFI_PASSPH_DATA, (uint8_t*)&passph[0], passph_len);
beginp(ssid,ssid_len,&passph[0],passph_len);
}
}else{
Serial.println("No KEY in EEPROM");
readEEdata(EE_WIFI_KEY_DATA, (uint8_t*)&key[0], key_len);
begink(ssid,ssid_len, 0, &key[0],key_len);
}
}
void WiFiClass::begin(char* ssid, uint8_t ssid_len)
int WiFiClass::begin(char* ssid)
{
wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_net(ssid, ssid_len);
if (result == WL_SUCCESS)
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
{
status = WL_CONNECTED;
Serial.println("WiFi Connected!");
}else{
status = WL_CONNECT_FAILED;
Serial.println("WiFi Connection failed!");
delay(WL_DELAY_START_CONNECTION);
return WiFiDrv::getConnectionStatus();
}else
{
return WL_CONNECT_FAILED;
}
}
void WiFiClass::begink(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t key_len)
int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
{
// set encryption key
wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_key(ssid, ssid_len, key_idx, key, key_len);
if (result == WL_SUCCESS)
{
//begin(ssid, ssid_len);
}else{
// Error setting passphrase
}
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
{
delay(WL_DELAY_START_CONNECTION);
return WiFiDrv::getConnectionStatus();
}else{
return WL_CONNECT_FAILED;
}
}
void WiFiClass::beginp(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len)
int WiFiClass::begin(char* ssid, const char *passphrase)
{
// set passphrase
wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_passpharse(ssid, ssid_len, passphrase, len);
if (result == WL_SUCCESS)
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
{
//begin(ssid, ssid_len);
}else{
// Error setting passphrase
delay(WL_DELAY_START_CONNECTION);
return WiFiDrv::getConnectionStatus();
}else{
return WL_CONNECT_FAILED;
}
}
wl_status_t WiFiClass::get_status()
{
return status;
}
uint8_t WiFiClass::get_result(uint8_t dummy)
{
uint8_t result = WiFiDrv::wifi_get_result(dummy);
return result;
}
void WiFiClass::getIpAddr(uint8_t *ip, uint8_t *mask, uint8_t *gwip)
{
WiFiDrv::getIpAddr(ip,mask,gwip);
}
uint8_t WiFiClass::getMacAddr(uint8_t* mac)
{
return WiFiDrv::wl_get_mac_addr(mac);
}
void WiFiClass::getSSIDList(char** ssid_list, uint8_t* ssidListNum)
{
WiFiDrv::getSSIDList(ssid_list, ssidListNum);
}
void WiFiClass::getCurrSSID(char* ssid)
{
WiFiDrv::getCurrSSID(ssid);
}
void WiFiClass::getCurrBSSID(uint8_t* bssid)
{
WiFiDrv::getCurrBSSID(bssid);
}
void WiFiClass::getCurrRSSI(int32_t* rssi)
{
WiFiDrv::getCurrRSSI(rssi);
}
void WiFiClass::getCurrEncType(uint8_t* enc_type)
{
WiFiDrv::getCurrEncType(enc_type);
}
uint8_t WiFiClass::disconnect()
int WiFiClass::disconnect()
{
return WiFiDrv::disconnect();
}
uint8_t* WiFiClass::macAddress(uint8_t* mac)
{
mac = WiFiDrv::getMacAddress();
return mac;
}
IPAddress WiFiClass::localIp()
{
IPAddress ret;
WiFiDrv::getIpAddress(ret.raw_address());
return ret;
}
IPAddress WiFiClass::subnetMask()
{
IPAddress ret;
WiFiDrv::getSubnetMask(ret.raw_address());
return ret;
}
IPAddress WiFiClass::gatewayIP()
{
IPAddress ret;
WiFiDrv::getGatewayIP(ret.raw_address());
return ret;
}
char* WiFiClass::SSID()
{
return WiFiDrv::getCurrentSSID();
}
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
{
bssid = WiFiDrv::getCurrentBSSID();
return bssid;
}
int32_t WiFiClass::RSSI()
{
return WiFiDrv::getCurrentRSSI();
}
uint8_t WiFiClass::encryptionType()
{
return WiFiDrv::getCurrentEncryptionType();
}
uint8_t WiFiClass::scanNetworks()
{
return WiFiDrv::scanNetworks();
}
char* WiFiClass::SSID(uint8_t networkItem)
{
return WiFiDrv::getSSIDNetoworks(networkItem);
}
int32_t WiFiClass::RSSI(uint8_t networkItem)
{
return WiFiDrv::getRSSINetoworks(networkItem);
}
uint8_t WiFiClass::encryptionType(uint8_t networkItem)
{
return WiFiDrv::getEncTypeNetowrks(networkItem);
}
WiFiClass WiFi;

View File

@ -4,9 +4,7 @@
#include <inttypes.h>
extern "C" {
#include "utility/wl_types.h"
#include "utility/wifi_spi.h"
#include "utility/debug.h" // only for test, not released
#include "utility/wl_definitions.h"
}
#include "IPAddress.h"
@ -18,39 +16,39 @@ class WiFiClass
private:
// this data are stored in EEPROM and loaded at begin
// The next connect overwrite these values
static char _ssid[WL_SSID_MAX_LENGTH];
static uint8_t _ssid_len;
static char _key[WL_WEP_KEY_MAX_LENGTH];
static uint8_t _key_len;
static char _passph[WL_WPA_KEY_MAX_LENGTH];
static uint8_t _passph_len;
static char _ssid[WL_SSID_MAX_LENGTH];
static char _key[WL_WEP_KEY_MAX_LENGTH];
static char _passph[WL_WPA_KEY_MAX_LENGTH];
static wl_status_t _status;
void init();
public:
static int16_t _state[MAX_SOCK_NUM];
static int16_t _state[MAX_SOCK_NUM];
static uint16_t _server_port[MAX_SOCK_NUM];
WiFiClass();
// Get thefirst socket available
static uint8_t getSocket();
// Start Wifi connection with latest settings
int begin();
// Start Wifi connection with no encryption
int begin(char* ssid, uint8_t ssid_len);
int begin(char* ssid);
// Start Wifi connection with WEP encryption
int begin(char* ssid, uint8_t ssid_len, uint8_t key_idx, const char* key, const uint8_t key_len);
int begin(char* ssid, uint8_t key_idx, const char* key);
// Start Wifi connection with passphrase
// the most secure supported mode will be automatically selected
int begin(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len);
int begin(char* ssid, const char *passphrase);
// Disconnect from the network
int disconnect(void);
//Get the interface MAC address.
uint8_t* macAddress();
uint8_t* macAddress(uint8_t* mac);
//Get the DHCP information related to IP
IPAddress localIp();
@ -65,19 +63,25 @@ public:
char* SSID();
// Return the current BSSID associated with the network
uint8_t* BSSID();
// Start scan SSIDs available and return the number of SSID discovered
uint8_t scanSSID();
// Return SSID item available
char* SSIDListItem(uint8_t ssidListItem);
// Return the current Encryption Type associated with the network
uint8_t encType(uint8_t ssidListItem);
uint8_t* BSSID(uint8_t* bssid);
// Return the current RSSI /Received Signal Strength in dBm) associated with the network
int32_t RSSI(uint8_t ssidListItem);
int32_t RSSI();
// Return the Encryption Type associated with the network
uint8_t encryptionType();
// Start scan WiFi networks available and return the discovered number
uint8_t scanNetworks();
// Return SSID item associated with the network identified with networkItem
char* SSID(uint8_t networkItem);
// Return the Encryption Type associated with the network identified with networkItem
uint8_t encryptionType(uint8_t networkItem);
// Return the current RSSI /Received Signal Strength in dBm) associated with the network identified with networkItem
int32_t RSSI(uint8_t networkItem);
friend class Client;
friend class Server;

View File

@ -0,0 +1,157 @@
/*
WiFi Server example
A simple connection with WiFi AP with Wireless Security
information try to access with WPA or WEP security keys
A simple server is setup to exchange data.
created 13 July 2010
by Domenico La Fauci
*/
#include <WiFi.h>
#include <IPAddress.h>
byte mac[6] = { 0 };
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
byte dataBuf[80] = { 0 };
char ssid[32] = { 0 };
int status = WL_IDLE_STATUS;
Server server(23);
void printIpData()
{
ip = WiFi.localIp();
Serial.print("IP: ");
Serial.print(ip[3],10);Serial.print(".");
Serial.print(ip[2],10);Serial.print(".");
Serial.print(ip[1],10);Serial.print(".");
Serial.println(ip[0],10);
subnet = WiFi.subnetMask();
Serial.print("NETMASK: ");
Serial.print(subnet[3],10);Serial.print(".");
Serial.print(subnet[2],10);Serial.print(".");
Serial.print(subnet[1],10);Serial.print(".");
Serial.println(subnet[0],10);
gateway = WiFi.gatewayIP();
Serial.print("GATEWAY: ");
Serial.print(gateway[3],10);Serial.print(".");
Serial.print(gateway[2],10);Serial.print(".");
Serial.print(gateway[1],10);Serial.print(".");
Serial.println(gateway[0],10);
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],16);Serial.print(":");
Serial.print(mac[4],16);Serial.print(":");
Serial.print(mac[3],16);Serial.print(":");
Serial.print(mac[2],16);Serial.print(":");
Serial.print(mac[1],16);Serial.print(":");
Serial.println(mac[0],16);
}
void printCurrNet()
{
//WiFi.getCurrSSID(&ssid[0]);
//Serial.print("SSID:");
//Serial.println(ssid);
byte bssid[6];
WiFi.BSSID(bssid);
//delay(200);
Serial.print("BSSID:");
Serial.print(bssid[5],16);Serial.print(":");
Serial.print(bssid[4],16);Serial.print(":");
Serial.print(bssid[3],16);Serial.print(":");
Serial.print(bssid[2],16);Serial.print(":");
Serial.print(bssid[1],16);Serial.print(":");
Serial.println(bssid[0],16);
int32_t rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi,10);
uint8_t enct = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(enct,16);
char ssid[20][32] = { {0} };
byte numSsid = WiFi.scanNetworks();
Serial.print("SSID List:");
Serial.println(numSsid, 10);
for (int i = 0; i<numSsid; ++i)
{
Serial.println(WiFi.SSID(i));
}
}
void setup()
{
Serial.begin(9600);
Serial.println("Setup WiFi...");
char ssid[] = "Cariddi";
Serial.println(ssid);
int status = WiFi.begin(ssid);
if ( status != WL_CONNECTED)
{
// Using WPA
Serial.println("Trying with Passphrase...");
const char *pass = "12345678";
status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED)
{
// using WEP
Serial.println("Trying with Key...");
uint8_t key_idx = 0;
const char *key = "12345678";
status = WiFi.begin(ssid,key_idx, key);
if ( status != WL_CONNECTED)
{
Serial.println("Wifi Connection failed!");
return;
}
}
}
Serial.println("Wifi Connected!");
printIpData();
printCurrNet();
Serial.println("Starting server...");
server.begin();
delay(1000);
}
void loop()
{
static uint8_t count = 0;
if (status == WL_CONNECTED)
{
byte status = 0;
Client client = server.available(&status);
if (client) {
//Serial.print("Status: ");
//Serial.println(status, 16);
byte idx = 0;
while (client.available())
{
dataBuf[idx++] = client.read();
}
if (idx>0)
{
dataBuf[idx]=0;
//Serial.println((char*)&dataBuf[0]);
server.write((char*)&dataBuf[0]);
}
return;
}
}
}

View File

@ -1,26 +1,81 @@
/*
WiFi example
A simple connection with WiFi AP with Wireless Security
information loaded in EEPROM
A simple connection with WiFi AP with Wireless Security
information try to access with WPA or WEP security keys
created 13 July 2010
by Domenico La Fauci
*/
#include <WiFi.h>
#include <EEPROM.h>
#include <IPAddress.h>
byte mac[6] = { 0 };
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
void setup()
{
Serial.begin(9600);
WiFi.begin();
if (WiFi.get_status() == WL_NO_SSID_AVAIL)
{
Serial.begin(9600);
Serial.println("Setup WiFi...");
char ssid[] = "Cariddi";
Serial.println(ssid);
int status = WiFi.begin(ssid);
if ( status != WL_CONNECTED)
{
// SSID not present in EEPROM
char ssid[] = "Cariddi";
WiFi.begin(ssid, strlen(ssid));
// Using WPA
Serial.println("Trying with Passphrase...");
const char *pass = "12345678";
status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED)
{
// using WEP
Serial.println("Trying with Key...");
uint8_t key_idx = 0;
const char *key = "12345678";
status = WiFi.begin(ssid,key_idx, key);
if ( status != WL_CONNECTED)
{
Serial.println("Wifi Connection failed!");
return;
}
}
}
Serial.println("Wifi Connected!");
ip = WiFi.localIp();
Serial.print("IP: ");
Serial.print(ip[3],10);Serial.print(".");
Serial.print(ip[2],10);Serial.print(".");
Serial.print(ip[1],10);Serial.print(".");
Serial.println(ip[0],10);
subnet = WiFi.subnetMask();
Serial.print("NETMASK: ");
Serial.print(subnet[3],10);Serial.print(".");
Serial.print(subnet[2],10);Serial.print(".");
Serial.print(subnet[1],10);Serial.print(".");
Serial.println(subnet[0],10);
gateway = WiFi.gatewayIP();
Serial.print("GATEWAY: ");
Serial.print(gateway[3],10);Serial.print(".");
Serial.print(gateway[2],10);Serial.print(".");
Serial.print(gateway[1],10);Serial.print(".");
Serial.println(gateway[0],10);
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],16);Serial.print(":");
Serial.print(mac[4],16);Serial.print(":");
Serial.print(mac[3],16);Serial.print(":");
Serial.print(mac[2],16);Serial.print(":");
Serial.print(mac[1],16);Serial.print(":");
Serial.println(mac[0],16);
}
void loop()

View File

@ -1,107 +0,0 @@
/*
WiFi example
A simple connection with WiFi AP with Wireless Security
information loaded in EEPROM, if not available
try to access with WPA or WEP security keys
created 13 July 2010
by Domenico La Fauci
*/
#include <EEPROM.h>
#include <WiFi.h>
#include <utility/wl_types.h>
// network configuration. gateway and subnet are optional.
byte mac[6] = { 0 };
byte ip[4] = { 0 };
byte gateway[4] = { 0 };
byte subnet[4] = { 0 };
void setup()
{
Serial.begin(9600);
Serial.println("Setup WiFi...");
WiFi.begin();
if (WiFi.get_status() == WL_NO_SSID_AVAIL)
{
//uint8_t result = WiFi.get_result();
//Serial.println(result,16);
// SSID not present in EEPROM
char ssid[] = "Cariddi";
Serial.println(ssid);
delay(2000);
// Using WPA
WiFi.begin(ssid, strlen(ssid));
delay(10000);
if (WiFi.get_result() == WL_CONNECT_FAILED)
{
Serial.println("Trying with Passphrase...");
uint8_t key_idx = 0;
const char *pass = "12345678";
uint8_t pass_len = strlen(pass);
WiFi.beginp(ssid, strlen(ssid), pass, pass_len);
}
// wait to trying connection...
delay(5000);
// using WEP
if (WiFi.get_result() == WL_CONNECT_FAILED)
{
Serial.println("Trying with Key...");
uint8_t key_idx = 0;
const char *key = "12345678";
uint8_t key_len = strlen(key);
WiFi.begink(ssid, strlen(ssid),key_idx, key, key_len);
}
// wait to trying connection...
delay(5000);
{
WiFi.getIpAddr(ip,subnet,gateway);
Serial.print("IP: ");
Serial.print(ip[3],10);Serial.print(".");
Serial.print(ip[2],10);Serial.print(".");
Serial.print(ip[1],10);Serial.print(".");
Serial.println(ip[0],10);
Serial.print("NETMASK: ");
Serial.print(subnet[3],10);Serial.print(".");
Serial.print(subnet[2],10);Serial.print(".");
Serial.print(subnet[1],10);Serial.print(".");
Serial.println(subnet[0],10);
Serial.print("GATEWAY: ");
Serial.print(gateway[3],10);Serial.print(".");
Serial.print(gateway[2],10);Serial.print(".");
Serial.print(gateway[1],10);Serial.print(".");
Serial.println(gateway[0],10);
WiFi.getMacAddr(mac);
Serial.print("MAC: ");
Serial.print(mac[5],16);Serial.print(":");
Serial.print(mac[4],16);Serial.print(":");
Serial.print(mac[3],16);Serial.print(":");
Serial.print(mac[2],16);Serial.print(":");
Serial.print(mac[1],16);Serial.print(":");
Serial.println(mac[0],16);
}
}
}
void loop()
{
static uint8_t count = 0;
while (WiFi.get_status() != WL_CONNECTED)
{
uint8_t result = WiFi.get_result(count++);
Serial.println(result);
delay(3000);
}
}

View File

@ -23,19 +23,16 @@ flush KEYWORD2
stop KEYWORD2
connected KEYWORD2
begin KEYWORD2
begink KEYWORD2
beginp KEYWORD2
disconnect KEYWORD2
getMacAddr KEYWORD2
getIpAddr KEYWORD2
getSSIDList KEYWORD2
getCurrSSID KEYWORD2
getCurrBSSID KEYWORD2
getCurrRSSI KEYWORD2
getCurrEncType KEYWORD2
wl_get_network_list KEYWORD2
get_result KEYWORD2
get_status KEYWORD2
macAddress KEYWORD2
localIp KEYWORD2
subnetMask KEYWORD2
gatewayIP KEYWORD2
SSID KEYWORD2
BSSID KEYWORD2
RSSI KEYWORD2
encryptionType KEYWORD2
getResult KEYWORD2
getSocket KEYWORD2

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

@ -0,0 +1,67 @@
//*********************************************/
//
// File: debug.h
//
// Author: Domenico La Fauci
//
//********************************************/
#ifndef Debug_H
#define Debug_H
#include <stdio.h>
#include <string.h>
#define INFO_0 1
#define INFO_1 2
#define INFO_2 4
#define INFO_3 8
#define INFO_4 16
#define INFO_5 32
#define INFO_D (1<<0xD) // Debug
#define INFO_E (1<<0xE) // Error
#define INFO_F (1<<0xF) // Warning
#define PRINT_FILE_LINE() do { \
Serial.print("[");Serial.print(__FILE__); \
Serial.print("::");Serial.print(__LINE__);Serial.print("]");\
}while (0);
#ifdef _DEBUG_
#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-W-");\
Serial.println(x); \
}while (0);
#define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\
Serial.print(x);Serial.print(",");Serial.println(y); \
}while (0);
#else
#define INFO1(x) do {} while(0);
#define INFO(format, args...) do {} while(0);
#endif
#define WARN(args) do { PRINT_FILE_LINE() \
Serial.print("-W-"); Serial.println(args); \
}while (0);
#define DBG_PIN2 3
#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()
#endif

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

@ -0,0 +1,187 @@
#include "server_drv.h"
#include "WProgram.h"
#include "spi_drv.h"
extern "C" {
#include "wl_types.h"
#include "debug.h"
}
// Start server TCP on port specified
void ServerDrv::StartServer(uint16_t port, uint8_t sock)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2);
SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
}
uint8_t ServerDrv::getState(uint8_t sock)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
delayMicroseconds(DELAY_POST_CMD);
return _data;
}
uint8_t ServerDrv::availData(uint8_t sock)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
delayMicroseconds(DELAY_POST_CMD);
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::getData(uint8_t sock, uint8_t *data)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(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)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
if (!SpiDrv::waitResponse(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)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2);
SpiDrv::sendParam(&sock, sizeof(sock));
SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(SEND_DATA_TCP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
uint8_t ServerDrv::isDataSent(uint8_t sock)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse isDataSent");
}
SpiDrv::spiSlaveDeselect();
delayMicroseconds(DELAY_POST_CMD);
return _data;
}
ServerDrv serverDrv;

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

@ -0,0 +1,28 @@
#ifndef Server_Drv_h
#define Server_Drv_h
#include <inttypes.h>
#include "wifi_spi.h"
class ServerDrv
{
public:
// Start server TCP on port specified
static void StartServer(uint16_t port, uint8_t sock);
static uint8_t getState(uint8_t sock);
static bool getData(uint8_t sock, uint8_t *data);
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 isDataSent(uint8_t sock);
};
extern ServerDrv serverDrv;
#endif

20
WiFi/utility/socket.c Normal file
View File

@ -0,0 +1,20 @@
/*
*
@file socket.c
@brief define function of socket API
*
*/
#include <inttypes.h>
#include "socket.h"
SOCKET socket(uint8 protocol) {} // Opens a socket(TCP or UDP or IP_RAW mode)
void close(SOCKET s) {} // Close socket
uint8 connect(SOCKET s, uint8 * addr, uint16 port) {} // Establish TCP connection (Active connection)
void disconnect(SOCKET s) {} // disconnect the connection
uint8 listen(SOCKET s) {} // Establish TCP connection (Passive connection)
uint16 send(SOCKET s, const uint8 * buf, uint16 len) {} // Send data (TCP)
uint16 recv(SOCKET s, uint8 * buf, uint16 len) {} // Receive data (TCP)
uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port) {} // Send data (UDP/IP RAW)
uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port) {} // Receive data (UDP/IP RAW)
uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) {}

87
WiFi/utility/socket.h Normal file
View File

@ -0,0 +1,87 @@
/*
*
@file socket.h
@brief define function of socket API
*
*/
#ifndef _SOCKET_H_
#define _SOCKET_H_
#define TCP_SOCKET 1
#define UDP_SOCKET 2
#define RAW_SOCKET 3
#define SOCK_NOT_AVAIL 255
#include "wl_definitions.h"
/**
* The 8-bit signed data type.
*/
typedef char int8;
/**
* The volatile 8-bit signed data type.
*/
typedef volatile char vint8;
/**
* The 8-bit unsigned data type.
*/
typedef unsigned char uint8;
/**
* The volatile 8-bit unsigned data type.
*/
typedef volatile unsigned char vuint8;
/**
* The 16-bit signed data type.
*/
typedef int int16;
/**
* The volatile 16-bit signed data type.
*/
typedef volatile int vint16;
/**
* The 16-bit unsigned data type.
*/
typedef unsigned int uint16;
/**
* The volatile 16-bit unsigned data type.
*/
typedef volatile unsigned int vuint16;
/**
* The 32-bit signed data type.
*/
typedef long int32;
/**
* The volatile 32-bit signed data type.
*/
typedef volatile long vint32;
/**
* The 32-bit unsigned data type.
*/
typedef unsigned long uint32;
/**
* The volatile 32-bit unsigned data type.
*/
typedef volatile unsigned long vuint32;
/* bsd */
typedef uint8 u_char; /**< 8-bit value */
typedef uint16_t SOCKET;
typedef uint16 u_short; /**< 16-bit value */
typedef uint16 u_int; /**< 16-bit value */
typedef uint32 u_long; /**< 32-bit value */
extern SOCKET socket(uint8 protocol); // Opens a socket(TCP or UDP or IP_RAW mode)
extern void close(SOCKET s); // Close socket
extern uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection)
extern void disconnect(SOCKET s); // disconnect the connection
extern uint8 listen(SOCKET s); // Establish TCP connection (Passive connection)
extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP)
extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP)
extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW)
extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW)
extern uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len);
#endif
/* _SOCKET_H_ */

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

@ -0,0 +1,434 @@
#include "WProgram.h"
#include "spi_drv.h"
extern "C" {
#include "debug.h"
}
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
void SpiDrv::spiSetup()
{
int clr = 0;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
}
void SpiDrv::spiDriverInit()
{
spiSetup();
}
void SpiDrv::spiSlaveSelect()
{
digitalWrite(SLAVESELECT,LOW);
}
void SpiDrv::spiSlaveDeselect()
{
digitalWrite(SLAVESELECT,HIGH);
}
char SpiDrv::spiTransfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
char result = SPDR;
delayMicroseconds(SPI_TX_DELAY);
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 == WAIT_CMD)
{
delayMicroseconds(WAIT_CHAR_DELAY);
}else
{
delayMicroseconds(TIMEOUT_CHAR_DELAY);
}
// else if (_readChar != waitChar)
// {
// Serial.println(_readChar,16);
// }
}while((timeout-- > 0) && (_readChar != waitChar));
if ((_readChar != waitChar)&&(timeout >=0))
{
INFO1("*C*");
Serial.println(_readChar,16);
}else if (timeout == 0)
{
INFO1("*T*");
}
return (_readChar == waitChar);
}
int SpiDrv::waitSpiChar(char waitChar, char* readChar)
{
int timeout = TIMEOUT_CHAR;
do{
*readChar = spiTransfer(DUMMY_DATA); //get data byte
if (*readChar == WAIT_CMD)
{
INFO1("WAIT");
delayMicroseconds(WAIT_CHAR_DELAY);
}
}while((timeout-- > 0) && (*readChar != waitChar));
return (*readChar == waitChar);
}
int SpiDrv::readAndCheckChar(char checkChar, char* readChar)
{
*readChar = spiTransfer(DUMMY_DATA); //get data byte
return (*readChar == checkChar);
}
char SpiDrv::readChar()
{
return spiTransfer(DUMMY_DATA); //get data byte
}
//#define WAIT_START_CMD(x) waitSpiChar(START_CMD, &x)
//#define WAIT_START_CMD(x) readAndCheckChar(START_CMD, &x)
#define WAIT_START_CMD(x) waitSpiChar(START_CMD)
#define IF_CHECK_START_CMD(x) \
if (!WAIT_START_CMD(_data)) \
{ \
TOGGLE_TRIGGER() \
WARN("Error waiting START_CMD"); \
Serial.println(cmd, 16); \
return 0; \
}else \
#define CHECK_DATA(check, x) \
if (!readAndCheckChar(check, &x)) \
{ \
WARN("Reply error"); \
return 0; \
}else \
int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_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);
{
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::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::waitResponse(uint8_t cmd, 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){};
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::waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len)
{
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)
{
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::waitResponse(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");
Serial.println(cmd, 16);
return 0;
}
if (numParam != _numParam)
{
WARN("Mismatch numParam");
Serial.println(cmd, 16);
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;
}
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)
{
int 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)
{
int i = 0;
// 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);
// 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;

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

@ -0,0 +1,70 @@
#ifndef SPI_Drv_h
#define SPI_Drv_h
#include <inttypes.h>
#include "wifi_spi.h"
#define WAIT_CHAR_DELAY 100
#define TIMEOUT_CHAR_DELAY 10
#define SPI_TX_DELAY 2
#define NO_LAST_PARAM 0
#define LAST_PARAM 1
#define DUMMY_DATA 0xFF
class SpiDrv
{
public:
static void spiSetup();
static void spiDriverInit();
static void spiSlaveSelect();
static void spiSlaveDeselect();
static char spiTransfer(volatile char data);
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 waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams);
static int waitResponse(uint8_t cmd, uint8_t numParam, tParam* params);
static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_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* param, uint8_t* param_len);
static int waitResponse(uint8_t cmd, uint8_t* param, uint16_t* param_len);
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

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

@ -0,0 +1,351 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "WProgram.h"
#include "spi_drv.h"
#include "wifi_drv.h"
#define _DEBUG_
extern "C" {
#include "wifi_spi.h"
#include "wl_types.h"
#include "debug.h"
}
char WiFiDrv::_networkSsid[] = {0};
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};
// 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}};
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
SpiDrv::waitResponse(GET_IPADDR_CMD, PARAM_NUMS_3, params);
SpiDrv::spiSlaveDeselect();
}
// Public Methods
void WiFiDrv::wifiDriverInit()
{
SpiDrv::spiDriverInit();
}
// If ssid == NULL execute a wifi scan, otherwise try to connect to the network specified
uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len)
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1);
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE;
}
uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len)
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
return _data;
}
uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len)
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponse(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
return _data;
}
uint8_t WiFiDrv::disconnect()
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1);
uint8_t _dummy = DUMMY_DATA;
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
uint8_t result = SpiDrv::waitResponse(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen);
SpiDrv::spiSlaveDeselect();
return result;
}
uint8_t WiFiDrv::getConnectionStatus()
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1);
uint8_t _dummy = DUMMY_DATA;
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
SpiDrv::waitResponse(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen);
SpiDrv::spiSlaveDeselect();
return _data;
}
uint8_t* WiFiDrv::getMacAddress()
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _dataLen = 0;
uint8_t result = SpiDrv::waitResponse(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen);
SpiDrv::spiSlaveDeselect();
return _mac;
}
void WiFiDrv::getIpAddress(uint8_t *ip)
{
getNetworkData(_localIp, _subnetMask, _gatewayIp);
memcpy(ip, _localIp, WL_IPV4_LENGTH);
}
void WiFiDrv::getSubnetMask(uint8_t *ip)
{
getNetworkData(_localIp, _subnetMask, _gatewayIp);
memcpy(ip, _subnetMask, WL_IPV4_LENGTH);
}
void WiFiDrv::getGatewayIP(uint8_t *ip)
{
getNetworkData(_localIp, _subnetMask, _gatewayIp);
memcpy(ip, _gatewayIp, WL_IPV4_LENGTH);
}
char* WiFiDrv::getCurrentSSID()
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _dataLen = 0;
uint8_t result = SpiDrv::waitResponse(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen);
SpiDrv::spiSlaveDeselect();
return _ssid;
}
uint8_t* WiFiDrv::getCurrentBSSID()
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _dataLen = 0;
uint8_t result = SpiDrv::waitResponse(GET_CURR_BSSID_CMD, PARAM_NUMS_1, (uint8_t*)_bssid, &_dataLen);
SpiDrv::spiSlaveDeselect();
return _bssid;
}
int32_t WiFiDrv::getCurrentRSSI()
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t _dataLen = 0;
int32_t rssi = 0;
uint8_t result = SpiDrv::waitResponse(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)rssi, &_dataLen);
SpiDrv::spiSlaveDeselect();
return rssi;
}
uint8_t WiFiDrv::getCurrentEncryptionType()
{
SpiDrv::spiSlaveSelect();
// 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
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint8_t dataLen = 0;
uint8_t encType = 0;
uint8_t result = SpiDrv::waitResponse(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen);
SpiDrv::spiSlaveDeselect();
return encType;
}
uint8_t WiFiDrv::scanNetworks()
{
SpiDrv::spiSlaveSelect();
// Send Command
SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_1);
uint8_t _dummy = DUMMY_DATA;
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
//Wait the reply elaboration
delayMicroseconds(DELAY_RX_TX);
// Wait for reply
uint16_t _dataLen = 0;
tParam params[WL_NETWORKS_LIST_MAXNUM];
uint8_t ssidListNum = 0;
uint8_t result = SpiDrv::waitResponse(SCAN_NETWORKS, params, &ssidListNum, WL_NETWORKS_LIST_MAXNUM);
SpiDrv::spiSlaveDeselect();
return ssidListNum;
}
char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem)
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
return NULL;
//TODO make an RPC call to get the ssid associated with networkItem
return _networkSsid;
}
uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem)
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
return NULL;
uint8_t networkEncType = 0;
//TODO make an RPC call to get the encryption type associated with networkItem
return networkEncType;
}
int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem)
{
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
return NULL;
int32_t networkRssi = 0;
//TODO make an RPC call to get the rssi associated with networkItem
return networkRssi;
}
WiFiDrv wiFiDrv;

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

@ -0,0 +1,68 @@
#ifndef WiFi_Drv_h
#define WiFi_Drv_h
#include <inttypes.h>
#include "wifi_spi.h"
#define KEY_IDX_LEN 1
#define WL_DELAY_START_CONNECTION 5000
class WiFiDrv
{
private:
// settings of requested network
static char _networkSsid[WL_SSID_MAX_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];
static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
public:
static void wifiDriverInit();
static uint8_t wifiSetNetwork(char* ssid, uint8_t ssid_len);
static uint8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len);
static uint8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len);
static uint8_t disconnect();
static uint8_t getConnectionStatus();
static uint8_t* getMacAddress();
static void getIpAddress(uint8_t *ip);
static void getSubnetMask(uint8_t *ip);
static void getGatewayIP(uint8_t *ip);
static char* getCurrentSSID();
static uint8_t* getCurrentBSSID();
static int32_t getCurrentRSSI();
static uint8_t getCurrentEncryptionType();
static uint8_t scanNetworks();
static char* getSSIDNetoworks(uint8_t networkItem);
static int32_t getRSSINetoworks(uint8_t networkItem);
static uint8_t getEncTypeNetowrks(uint8_t networkItem);
};
extern WiFiDrv wiFiDrv;
#endif

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

@ -0,0 +1,101 @@
#ifndef WiFi_Spi_h
#define WiFi_Spi_h
#include "wl_definitions.h"
#define CMD_FLAG 0
#define REPLY_FLAG 1<<7
#define WIFI_SPI_ACK 1
#define WIFI_SPI_ERR 0xFF
#define TIMEOUT_CHAR 1000
#define DELAY_RX_TX 200 //usec
#define DELAY_POST_CMD 0 //usec
//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */
#define NO_SOCKET_AVAIL 255
#define START_CMD 0xE0
#define WAIT_CMD 0xE1
#define END_CMD 0xEE
enum {
SET_NET_CMD = 0x10,
SET_PASSPHRASE_CMD = 0x11,
SET_KEY_CMD = 0x12,
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,
DISCONNECT_CMD = 0x30,
START_SERVER_TCP_CMD = 0x40,
GET_STATE_TCP_CMD = 0x41,
GET_DATA_TCP_CMD = 0x42,
AVAIL_DATA_TCP_CMD = 0x43,
SEND_DATA_TCP_CMD = 0x44,
DATA_SENT_TCP_CMD = 0x45,
GET_DATABUF_TCP_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__))
{
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 totLen;
unsigned char nParam;
}tSpiHdr;
#endif

View File

@ -0,0 +1,37 @@
/*
* 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 5
// Maxmium number of socket
#define MAX_SOCK_NUM 4
typedef enum {
WL_IDLE_STATUS,
WL_NO_SSID_AVAIL,
WL_SCAN_COMPLETED,
WL_CONNECTED,
WL_CONNECT_FAILED,
WL_CONNECTION_LOST,
WL_DISCONNECTED
} wl_status_t;
#endif /* WL_DEFINITIONS_H_ */

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

@ -0,0 +1,42 @@
/*
* 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
};
/* 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_TYPES_H_