2012-06-06 11:28:21 +02:00
|
|
|
#include "wifi_drv.h"
|
|
|
|
#include "WiFi.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "utility/wl_definitions.h"
|
|
|
|
#include "utility/wl_types.h"
|
|
|
|
#include "debug.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
2013-02-08 19:22:23 +01:00
|
|
|
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE };
|
2012-06-06 11:28:21 +02:00
|
|
|
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
WiFiClass::WiFiClass()
|
|
|
|
{
|
|
|
|
// Driver initialization
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClass::init()
|
|
|
|
{
|
|
|
|
WiFiDrv::wifiDriverInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WiFiClass::getSocket()
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
|
|
|
|
{
|
|
|
|
if (WiFiClass::_server_port[i] == 0)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO_SOCKET_AVAIL;
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:56:47 +02:00
|
|
|
char* WiFiClass::firmwareVersion()
|
|
|
|
{
|
|
|
|
return WiFiDrv::getFwVersion();
|
|
|
|
}
|
|
|
|
|
2012-06-06 11:28:21 +02:00
|
|
|
int WiFiClass::begin(char* ssid)
|
|
|
|
{
|
|
|
|
uint8_t status = WL_IDLE_STATUS;
|
|
|
|
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
|
|
|
|
|
|
|
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
delay(WL_DELAY_START_CONNECTION);
|
|
|
|
status = WiFiDrv::getConnectionStatus();
|
|
|
|
}
|
|
|
|
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
status = WL_CONNECT_FAILED;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
|
|
|
|
{
|
|
|
|
uint8_t status = WL_IDLE_STATUS;
|
|
|
|
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
|
|
|
|
|
|
|
// set encryption key
|
|
|
|
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
delay(WL_DELAY_START_CONNECTION);
|
|
|
|
status = WiFiDrv::getConnectionStatus();
|
2013-02-08 19:22:23 +01:00
|
|
|
}while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
2012-06-06 11:28:21 +02:00
|
|
|
}else{
|
|
|
|
status = WL_CONNECT_FAILED;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClass::begin(char* ssid, const char *passphrase)
|
|
|
|
{
|
|
|
|
uint8_t status = WL_IDLE_STATUS;
|
|
|
|
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
|
|
|
|
|
|
|
// set passphrase
|
|
|
|
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
delay(WL_DELAY_START_CONNECTION);
|
|
|
|
status = WiFiDrv::getConnectionStatus();
|
|
|
|
}
|
|
|
|
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
|
|
|
}else{
|
|
|
|
status = WL_CONNECT_FAILED;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-03-21 09:24:07 +01:00
|
|
|
void WiFiClass::config(IPAddress local_ip)
|
|
|
|
{
|
|
|
|
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-03-28 12:56:57 +01:00
|
|
|
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server)
|
|
|
|
{
|
|
|
|
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
|
|
|
|
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
2013-03-21 09:24:07 +01:00
|
|
|
{
|
|
|
|
WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0);
|
2013-03-28 12:56:57 +01:00
|
|
|
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
2013-03-21 09:24:07 +01:00
|
|
|
}
|
|
|
|
|
2013-03-28 12:56:57 +01:00
|
|
|
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
2013-03-21 09:24:07 +01:00
|
|
|
{
|
|
|
|
WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet);
|
2013-03-28 12:56:57 +01:00
|
|
|
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
2013-03-21 09:24:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClass::setDNS(IPAddress dns_server1)
|
|
|
|
{
|
|
|
|
WiFiDrv::setDNS(1, (uint32_t)dns_server1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2)
|
|
|
|
{
|
|
|
|
WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2);
|
|
|
|
}
|
|
|
|
|
2012-06-06 11:28:21 +02:00
|
|
|
int WiFiClass::disconnect()
|
|
|
|
{
|
|
|
|
return WiFiDrv::disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* WiFiClass::macAddress(uint8_t* mac)
|
|
|
|
{
|
|
|
|
uint8_t* _mac = WiFiDrv::getMacAddress();
|
|
|
|
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
|
|
|
|
return mac;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress WiFiClass::localIP()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
WiFiDrv::getIpAddress(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress WiFiClass::subnetMask()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
WiFiDrv::getSubnetMask(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress WiFiClass::gatewayIP()
|
|
|
|
{
|
|
|
|
IPAddress ret;
|
|
|
|
WiFiDrv::getGatewayIP(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* WiFiClass::SSID()
|
|
|
|
{
|
|
|
|
return WiFiDrv::getCurrentSSID();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
|
|
|
|
{
|
|
|
|
uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
|
|
|
|
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
|
|
|
|
return bssid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t WiFiClass::RSSI()
|
|
|
|
{
|
|
|
|
return WiFiDrv::getCurrentRSSI();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WiFiClass::encryptionType()
|
|
|
|
{
|
|
|
|
return WiFiDrv::getCurrentEncryptionType();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int8_t WiFiClass::scanNetworks()
|
|
|
|
{
|
|
|
|
uint8_t attempts = 10;
|
|
|
|
uint8_t numOfNetworks = 0;
|
|
|
|
|
|
|
|
if (WiFiDrv::startScanNetworks() == WL_FAILURE)
|
|
|
|
return WL_FAILURE;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
delay(2000);
|
|
|
|
numOfNetworks = WiFiDrv::getScanNetworks();
|
|
|
|
}
|
|
|
|
while (( numOfNetworks == 0)&&(--attempts>0));
|
|
|
|
return numOfNetworks;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* WiFiClass::SSID(uint8_t networkItem)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WiFiClass::status()
|
|
|
|
{
|
|
|
|
return WiFiDrv::getConnectionStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
|
|
|
|
{
|
|
|
|
return WiFiDrv::getHostByName(aHostname, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFiClass WiFi;
|