Skip to content

Commit c2c1043

Browse files
committed
WiFi library for Arduino Due
1 parent 059ceed commit c2c1043

File tree

31 files changed

+4403
-0
lines changed

31 files changed

+4403
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include "wifi_drv.h"
2+
#include "WiFi.h"
3+
4+
extern "C" {
5+
#include "utility/wl_definitions.h"
6+
#include "utility/wl_types.h"
7+
#include "debug.h"
8+
}
9+
10+
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
11+
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
12+
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
13+
14+
WiFiClass::WiFiClass()
15+
{
16+
// Driver initialization
17+
init();
18+
}
19+
20+
void WiFiClass::init()
21+
{
22+
WiFiDrv::wifiDriverInit();
23+
}
24+
25+
uint8_t WiFiClass::getSocket()
26+
{
27+
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
28+
{
29+
if (WiFiClass::_server_port[i] == 0)
30+
{
31+
return i;
32+
}
33+
}
34+
return NO_SOCKET_AVAIL;
35+
}
36+
37+
char* WiFiClass::firmwareVersion()
38+
{
39+
return WiFiDrv::getFwVersion();
40+
}
41+
42+
int WiFiClass::begin(char* ssid)
43+
{
44+
uint8_t status = WL_IDLE_STATUS;
45+
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
46+
47+
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
48+
{
49+
do
50+
{
51+
delay(WL_DELAY_START_CONNECTION);
52+
status = WiFiDrv::getConnectionStatus();
53+
}
54+
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
55+
}else
56+
{
57+
status = WL_CONNECT_FAILED;
58+
}
59+
return status;
60+
}
61+
62+
int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
63+
{
64+
uint8_t status = WL_IDLE_STATUS;
65+
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
66+
67+
// set encryption key
68+
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
69+
{
70+
do
71+
{
72+
delay(WL_DELAY_START_CONNECTION);
73+
status = WiFiDrv::getConnectionStatus();
74+
}
75+
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
76+
}else{
77+
status = WL_CONNECT_FAILED;
78+
}
79+
return status;
80+
}
81+
82+
int WiFiClass::begin(char* ssid, const char *passphrase)
83+
{
84+
uint8_t status = WL_IDLE_STATUS;
85+
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
86+
87+
// set passphrase
88+
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
89+
{
90+
do
91+
{
92+
delay(WL_DELAY_START_CONNECTION);
93+
status = WiFiDrv::getConnectionStatus();
94+
}
95+
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
96+
}else{
97+
status = WL_CONNECT_FAILED;
98+
}
99+
return status;
100+
}
101+
102+
int WiFiClass::disconnect()
103+
{
104+
return WiFiDrv::disconnect();
105+
}
106+
107+
uint8_t* WiFiClass::macAddress(uint8_t* mac)
108+
{
109+
uint8_t* _mac = WiFiDrv::getMacAddress();
110+
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
111+
return mac;
112+
}
113+
114+
IPAddress WiFiClass::localIP()
115+
{
116+
IPAddress ret;
117+
WiFiDrv::getIpAddress(ret);
118+
return ret;
119+
}
120+
121+
IPAddress WiFiClass::subnetMask()
122+
{
123+
IPAddress ret;
124+
WiFiDrv::getSubnetMask(ret);
125+
return ret;
126+
}
127+
128+
IPAddress WiFiClass::gatewayIP()
129+
{
130+
IPAddress ret;
131+
WiFiDrv::getGatewayIP(ret);
132+
return ret;
133+
}
134+
135+
char* WiFiClass::SSID()
136+
{
137+
return WiFiDrv::getCurrentSSID();
138+
}
139+
140+
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
141+
{
142+
uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
143+
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
144+
return bssid;
145+
}
146+
147+
int32_t WiFiClass::RSSI()
148+
{
149+
return WiFiDrv::getCurrentRSSI();
150+
}
151+
152+
uint8_t WiFiClass::encryptionType()
153+
{
154+
return WiFiDrv::getCurrentEncryptionType();
155+
}
156+
157+
158+
int8_t WiFiClass::scanNetworks()
159+
{
160+
uint8_t attempts = 10;
161+
uint8_t numOfNetworks = 0;
162+
163+
if (WiFiDrv::startScanNetworks() == WL_FAILURE)
164+
return WL_FAILURE;
165+
do
166+
{
167+
delay(2000);
168+
numOfNetworks = WiFiDrv::getScanNetworks();
169+
}
170+
while (( numOfNetworks == 0)&&(--attempts>0));
171+
return numOfNetworks;
172+
}
173+
174+
char* WiFiClass::SSID(uint8_t networkItem)
175+
{
176+
return WiFiDrv::getSSIDNetoworks(networkItem);
177+
}
178+
179+
int32_t WiFiClass::RSSI(uint8_t networkItem)
180+
{
181+
return WiFiDrv::getRSSINetoworks(networkItem);
182+
}
183+
184+
uint8_t WiFiClass::encryptionType(uint8_t networkItem)
185+
{
186+
return WiFiDrv::getEncTypeNetowrks(networkItem);
187+
}
188+
189+
uint8_t WiFiClass::status()
190+
{
191+
return WiFiDrv::getConnectionStatus();
192+
}
193+
194+
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
195+
{
196+
return WiFiDrv::getHostByName(aHostname, aResult);
197+
}
198+
199+
WiFiClass WiFi;
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#ifndef WiFi_h
2+
#define WiFi_h
3+
4+
#include <inttypes.h>
5+
6+
extern "C" {
7+
#include "utility/wl_definitions.h"
8+
#include "utility/wl_types.h"
9+
}
10+
11+
#include "IPAddress.h"
12+
#include "WiFiClient.h"
13+
#include "WiFiServer.h"
14+
15+
class WiFiClass
16+
{
17+
private:
18+
19+
static void init();
20+
public:
21+
static int16_t _state[MAX_SOCK_NUM];
22+
static uint16_t _server_port[MAX_SOCK_NUM];
23+
24+
WiFiClass();
25+
26+
/*
27+
* Get the first socket available
28+
*/
29+
static uint8_t getSocket();
30+
31+
/*
32+
* Get firmware version
33+
*/
34+
static char* firmwareVersion();
35+
36+
37+
/* Start Wifi connection for OPEN networks
38+
*
39+
* param ssid: Pointer to the SSID string.
40+
*/
41+
int begin(char* ssid);
42+
43+
/* Start Wifi connection with WEP encryption.
44+
* Configure a key into the device. The key type (WEP-40, WEP-104)
45+
* is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
46+
*
47+
* param ssid: Pointer to the SSID string.
48+
* param key_idx: The key index to set. Valid values are 0-3.
49+
* param key: Key input buffer.
50+
*/
51+
int begin(char* ssid, uint8_t key_idx, const char* key);
52+
53+
/* Start Wifi connection with passphrase
54+
* the most secure supported mode will be automatically selected
55+
*
56+
* param ssid: Pointer to the SSID string.
57+
* param passphrase: Passphrase. Valid characters in a passphrase
58+
* must be between ASCII 32-126 (decimal).
59+
*/
60+
int begin(char* ssid, const char *passphrase);
61+
62+
/*
63+
* Disconnect from the network
64+
*
65+
* return: one value of wl_status_t enum
66+
*/
67+
int disconnect(void);
68+
69+
/*
70+
* Get the interface MAC address.
71+
*
72+
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
73+
*/
74+
uint8_t* macAddress(uint8_t* mac);
75+
76+
/*
77+
* Get the interface IP address.
78+
*
79+
* return: Ip address value
80+
*/
81+
IPAddress localIP();
82+
83+
/*
84+
* Get the interface subnet mask address.
85+
*
86+
* return: subnet mask address value
87+
*/
88+
IPAddress subnetMask();
89+
90+
/*
91+
* Get the gateway ip address.
92+
*
93+
* return: gateway ip address value
94+
*/
95+
IPAddress gatewayIP();
96+
97+
/*
98+
* Return the current SSID associated with the network
99+
*
100+
* return: ssid string
101+
*/
102+
char* SSID();
103+
104+
/*
105+
* Return the current BSSID associated with the network.
106+
* It is the MAC address of the Access Point
107+
*
108+
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
109+
*/
110+
uint8_t* BSSID(uint8_t* bssid);
111+
112+
/*
113+
* Return the current RSSI /Received Signal Strength in dBm)
114+
* associated with the network
115+
*
116+
* return: signed value
117+
*/
118+
int32_t RSSI();
119+
120+
/*
121+
* Return the Encryption Type associated with the network
122+
*
123+
* return: one value of wl_enc_type enum
124+
*/
125+
uint8_t encryptionType();
126+
127+
/*
128+
* Start scan WiFi networks available
129+
*
130+
* return: Number of discovered networks
131+
*/
132+
int8_t scanNetworks();
133+
134+
/*
135+
* Return the SSID discovered during the network scan.
136+
*
137+
* param networkItem: specify from which network item want to get the information
138+
*
139+
* return: ssid string of the specified item on the networks scanned list
140+
*/
141+
char* SSID(uint8_t networkItem);
142+
143+
/*
144+
* Return the encryption type of the networks discovered during the scanNetworks
145+
*
146+
* param networkItem: specify from which network item want to get the information
147+
*
148+
* return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
149+
*/
150+
uint8_t encryptionType(uint8_t networkItem);
151+
152+
/*
153+
* Return the RSSI of the networks discovered during the scanNetworks
154+
*
155+
* param networkItem: specify from which network item want to get the information
156+
*
157+
* return: signed value of RSSI of the specified item on the networks scanned list
158+
*/
159+
int32_t RSSI(uint8_t networkItem);
160+
161+
/*
162+
* Return Connection status.
163+
*
164+
* return: one of the value defined in wl_status_t
165+
*/
166+
uint8_t status();
167+
168+
/*
169+
* Resolve the given hostname to an IP address.
170+
* param aHostname: Name to be resolved
171+
* param aResult: IPAddress structure to store the returned IP address
172+
* result: 1 if aIPAddrString was successfully converted to an IP address,
173+
* else error code
174+
*/
175+
int hostByName(const char* aHostname, IPAddress& aResult);
176+
177+
friend class WiFiClient;
178+
friend class WiFiServer;
179+
};
180+
181+
extern WiFiClass WiFi;
182+
183+
#endif

0 commit comments

Comments
 (0)