|
| 1 | +#include <stdio.h> |
| 2 | +#include "wifi_drv.h" |
| 3 | +#include "WiFi.h" |
| 4 | + |
| 5 | +extern "C" { |
| 6 | + #include "utility/wl_definitions.h" |
| 7 | + #include "utility/wl_types.h" |
| 8 | + #include "debug.h" |
| 9 | +} |
| 10 | + |
| 11 | +// XXX: don't make assumptions about the value of MAX_SOCK_NUM. |
| 12 | +int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; |
| 13 | +uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; |
| 14 | + |
| 15 | +WiFiClass::WiFiClass() |
| 16 | +{ |
| 17 | + // Driver initialization |
| 18 | + init(); |
| 19 | +} |
| 20 | + |
| 21 | +void WiFiClass::init() |
| 22 | +{ |
| 23 | + WiFiDrv::wifiDriverInit(); |
| 24 | +} |
| 25 | + |
| 26 | +uint8_t WiFiClass::getSocket() |
| 27 | +{ |
| 28 | + for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) |
| 29 | + { |
| 30 | + if (WiFiClass::_server_port[i] == 0) |
| 31 | + { |
| 32 | + return i; |
| 33 | + } |
| 34 | + } |
| 35 | + return NO_SOCKET_AVAIL; |
| 36 | +} |
| 37 | + |
| 38 | +char* WiFiClass::firmwareVersion() |
| 39 | +{ |
| 40 | + return WiFiDrv::getFwVersion(); |
| 41 | +} |
| 42 | + |
| 43 | +int WiFiClass::begin(char* ssid) |
| 44 | +{ |
| 45 | + uint8_t status = WL_IDLE_STATUS; |
| 46 | + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; |
| 47 | + |
| 48 | + if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) |
| 49 | + { |
| 50 | + do |
| 51 | + { |
| 52 | + delay(WL_DELAY_START_CONNECTION); |
| 53 | + status = WiFiDrv::getConnectionStatus(); |
| 54 | + } |
| 55 | + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); |
| 56 | + }else |
| 57 | + { |
| 58 | + status = WL_CONNECT_FAILED; |
| 59 | + } |
| 60 | + return status; |
| 61 | +} |
| 62 | + |
| 63 | +int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) |
| 64 | +{ |
| 65 | + uint8_t status = WL_IDLE_STATUS; |
| 66 | + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; |
| 67 | + |
| 68 | + // set encryption key |
| 69 | + if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) |
| 70 | + { |
| 71 | + do |
| 72 | + { |
| 73 | + delay(WL_DELAY_START_CONNECTION); |
| 74 | + status = WiFiDrv::getConnectionStatus(); |
| 75 | + } |
| 76 | + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); |
| 77 | + }else{ |
| 78 | + status = WL_CONNECT_FAILED; |
| 79 | + } |
| 80 | + return status; |
| 81 | +} |
| 82 | + |
| 83 | +int WiFiClass::begin(char* ssid, const char *passphrase) |
| 84 | +{ |
| 85 | + uint8_t status = WL_IDLE_STATUS; |
| 86 | + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; |
| 87 | + |
| 88 | + // set passphrase |
| 89 | + if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) |
| 90 | + { |
| 91 | + do |
| 92 | + { |
| 93 | + delay(WL_DELAY_START_CONNECTION); |
| 94 | + status = WiFiDrv::getConnectionStatus(); |
| 95 | + } |
| 96 | + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); |
| 97 | + }else{ |
| 98 | + status = WL_CONNECT_FAILED; |
| 99 | + } |
| 100 | + return status; |
| 101 | +} |
| 102 | + |
| 103 | +int WiFiClass::disconnect() |
| 104 | +{ |
| 105 | + return WiFiDrv::disconnect(); |
| 106 | +} |
| 107 | + |
| 108 | +uint8_t* WiFiClass::macAddress(uint8_t* mac) |
| 109 | +{ |
| 110 | + uint8_t* _mac = WiFiDrv::getMacAddress(); |
| 111 | + memcpy(mac, _mac, WL_MAC_ADDR_LENGTH); |
| 112 | + return mac; |
| 113 | +} |
| 114 | + |
| 115 | +IPAddress WiFiClass::localIP() |
| 116 | +{ |
| 117 | + IPAddress ret; |
| 118 | + WiFiDrv::getIpAddress(ret); |
| 119 | + return ret; |
| 120 | +} |
| 121 | + |
| 122 | +IPAddress WiFiClass::subnetMask() |
| 123 | +{ |
| 124 | + IPAddress ret; |
| 125 | + WiFiDrv::getSubnetMask(ret); |
| 126 | + return ret; |
| 127 | +} |
| 128 | + |
| 129 | +IPAddress WiFiClass::gatewayIP() |
| 130 | +{ |
| 131 | + IPAddress ret; |
| 132 | + WiFiDrv::getGatewayIP(ret); |
| 133 | + return ret; |
| 134 | +} |
| 135 | + |
| 136 | +char* WiFiClass::SSID() |
| 137 | +{ |
| 138 | + return WiFiDrv::getCurrentSSID(); |
| 139 | +} |
| 140 | + |
| 141 | +uint8_t* WiFiClass::BSSID(uint8_t* bssid) |
| 142 | +{ |
| 143 | + uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); |
| 144 | + memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); |
| 145 | + return bssid; |
| 146 | +} |
| 147 | + |
| 148 | +int32_t WiFiClass::RSSI() |
| 149 | +{ |
| 150 | + return WiFiDrv::getCurrentRSSI(); |
| 151 | +} |
| 152 | + |
| 153 | +uint8_t WiFiClass::encryptionType() |
| 154 | +{ |
| 155 | + return WiFiDrv::getCurrentEncryptionType(); |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +int8_t WiFiClass::scanNetworks() |
| 160 | +{ |
| 161 | + uint8_t attempts = 10; |
| 162 | + uint8_t numOfNetworks = 0; |
| 163 | + |
| 164 | + if (WiFiDrv::startScanNetworks() == WL_FAILURE) |
| 165 | + return WL_FAILURE; |
| 166 | + do |
| 167 | + { |
| 168 | + delay(2000); |
| 169 | + numOfNetworks = WiFiDrv::getScanNetworks(); |
| 170 | + } |
| 171 | + while (( numOfNetworks == 0)&&(--attempts>0)); |
| 172 | + return numOfNetworks; |
| 173 | +} |
| 174 | + |
| 175 | +char* WiFiClass::SSID(uint8_t networkItem) |
| 176 | +{ |
| 177 | + return WiFiDrv::getSSIDNetoworks(networkItem); |
| 178 | +} |
| 179 | + |
| 180 | +int32_t WiFiClass::RSSI(uint8_t networkItem) |
| 181 | +{ |
| 182 | + return WiFiDrv::getRSSINetoworks(networkItem); |
| 183 | +} |
| 184 | + |
| 185 | +uint8_t WiFiClass::encryptionType(uint8_t networkItem) |
| 186 | +{ |
| 187 | + return WiFiDrv::getEncTypeNetowrks(networkItem); |
| 188 | +} |
| 189 | + |
| 190 | +uint8_t WiFiClass::status() |
| 191 | +{ |
| 192 | + return WiFiDrv::getConnectionStatus(); |
| 193 | +} |
| 194 | + |
| 195 | +int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) |
| 196 | +{ |
| 197 | + return WiFiDrv::getHostByName(aHostname, aResult); |
| 198 | +} |
| 199 | + |
| 200 | +WiFiClass WiFi; |
0 commit comments