1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-12 23:08:52 +01:00
Arduino/WiFi/WiFi.cpp

195 lines
3.8 KiB
C++
Raw Normal View History

2011-02-28 13:49:08 +01:00
#include "wifi_drv.h"
#include "WiFi.h"
2011-02-28 13:49:08 +01:00
extern "C" {
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
2012-03-07 00:05:54 +01:00
#include "debug.h"
}
2011-02-28 13:49:08 +01:00
// 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 };
2011-02-28 13:49:08 +01:00
WiFiClass::WiFiClass()
{
// Driver initialization
init();
2011-02-28 13:49:08 +01:00
}
void WiFiClass::init()
{
WiFiDrv::wifiDriverInit();
2011-02-28 13:49:08 +01:00
}
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;
}
int WiFiClass::begin(char* ssid)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
uint8_t status = WL_IDLE_STATUS;
2011-04-21 07:47:54 +02:00
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}
2011-04-21 07:47:54 +02:00
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else
{
2011-04-05 23:24:17 +02:00
status = WL_CONNECT_FAILED;
2011-02-28 13:49:08 +01:00
}
2011-04-05 23:24:17 +02:00
return status;
2011-02-28 13:49:08 +01:00
}
int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
uint8_t status = WL_IDLE_STATUS;
2011-04-21 07:47:54 +02:00
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
// set encryption key
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
{
2011-04-05 23:24:17 +02:00
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}
2011-04-21 07:47:54 +02:00
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{
2011-04-05 23:24:17 +02:00
status = WL_CONNECT_FAILED;
}
2011-04-05 23:24:17 +02:00
return status;
2011-02-28 13:49:08 +01:00
}
int WiFiClass::begin(char* ssid, const char *passphrase)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
uint8_t status = WL_IDLE_STATUS;
2011-04-21 07:47:54 +02:00
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
2011-02-28 13:49:08 +01:00
// set passphrase
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}
2011-04-21 07:47:54 +02:00
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{
2011-04-05 23:24:17 +02:00
status = WL_CONNECT_FAILED;
2011-02-28 13:49:08 +01:00
}
2011-04-05 23:24:17 +02:00
return status;
2011-02-28 13:49:08 +01:00
}
int WiFiClass::disconnect()
2011-02-28 13:49:08 +01:00
{
return WiFiDrv::disconnect();
2011-02-28 13:49:08 +01:00
}
uint8_t* WiFiClass::macAddress(uint8_t* mac)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
uint8_t* _mac = WiFiDrv::getMacAddress();
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
return mac;
2011-02-28 13:49:08 +01:00
}
2011-06-06 01:27:45 +02:00
IPAddress WiFiClass::localIP()
2011-02-28 13:49:08 +01:00
{
IPAddress ret;
WiFiDrv::getIpAddress(ret);
return ret;
2011-02-28 13:49:08 +01:00
}
IPAddress WiFiClass::subnetMask()
{
IPAddress ret;
WiFiDrv::getSubnetMask(ret);
return ret;
}
2011-02-28 13:49:08 +01:00
IPAddress WiFiClass::gatewayIP()
2011-02-28 13:49:08 +01:00
{
IPAddress ret;
WiFiDrv::getGatewayIP(ret);
return ret;
2011-02-28 13:49:08 +01:00
}
char* WiFiClass::SSID()
2011-02-28 13:49:08 +01:00
{
return WiFiDrv::getCurrentSSID();
2011-02-28 13:49:08 +01:00
}
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
2011-02-28 13:49:08 +01:00
{
2011-04-05 23:24:17 +02:00
uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
return bssid;
2011-02-28 13:49:08 +01:00
}
int32_t WiFiClass::RSSI()
2011-02-28 13:49:08 +01:00
{
return WiFiDrv::getCurrentRSSI();
2011-02-28 13:49:08 +01:00
}
uint8_t WiFiClass::encryptionType()
2011-02-28 13:49:08 +01:00
{
return WiFiDrv::getCurrentEncryptionType();
2011-02-28 13:49:08 +01:00
}
int8_t WiFiClass::scanNetworks()
2011-02-28 13:49:08 +01:00
{
2012-05-24 08:57:46 +02:00
uint8_t attempts = 10;
uint8_t numOfNetworks = 0;
if (WiFiDrv::startScanNetworks() == WL_FAILURE)
return WL_FAILURE;
2012-05-24 08:57:46 +02:00
do
{
delay(2000);
numOfNetworks = WiFiDrv::getScanNetworks();
}
while (( numOfNetworks == 0)&&(--attempts>0));
return numOfNetworks;
2011-02-28 13:49:08 +01:00
}
char* WiFiClass::SSID(uint8_t networkItem)
2011-02-28 13:49:08 +01:00
{
return WiFiDrv::getSSIDNetoworks(networkItem);
2011-02-28 13:49:08 +01:00
}
int32_t WiFiClass::RSSI(uint8_t networkItem)
{
return WiFiDrv::getRSSINetoworks(networkItem);
}
uint8_t WiFiClass::encryptionType(uint8_t networkItem)
{
return WiFiDrv::getEncTypeNetowrks(networkItem);
}
2011-02-28 13:49:08 +01:00
2011-04-05 23:24:17 +02:00
uint8_t WiFiClass::status()
{
return WiFiDrv::getConnectionStatus();
}
2012-03-15 01:18:43 +01:00
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
{
return WiFiDrv::getHostByName(aHostname, aResult);
}
2011-02-28 13:49:08 +01:00
WiFiClass WiFi;