This is GR_PEACH_WlanBP3595 class library. The base library is EthernetInterface.

Dependents:   GR-PEACH_WlanBP3595AP GR-PEACH_WlanBP3595STA

Committer:
tousaki
Date:
Tue May 24 10:17:15 2016 +0000
Revision:
0:41941ba775eb
Created 1st version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tousaki 0:41941ba775eb 1 /* GR_PEACH_WlanBP3595.cpp */
tousaki 0:41941ba775eb 2 /* Copyright (C) 2016 Grape Systems, Inc. */
tousaki 0:41941ba775eb 3 /* The base file is EthernetInterface.cpp. */
tousaki 0:41941ba775eb 4
tousaki 0:41941ba775eb 5 /* EthernetInterface.cpp */
tousaki 0:41941ba775eb 6 /* Copyright (C) 2012 mbed.org, MIT License
tousaki 0:41941ba775eb 7 *
tousaki 0:41941ba775eb 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
tousaki 0:41941ba775eb 9 * and associated documentation files (the "Software"), to deal in the Software without restriction,
tousaki 0:41941ba775eb 10 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
tousaki 0:41941ba775eb 11 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
tousaki 0:41941ba775eb 12 * furnished to do so, subject to the following conditions:
tousaki 0:41941ba775eb 13 *
tousaki 0:41941ba775eb 14 * The above copyright notice and this permission notice shall be included in all copies or
tousaki 0:41941ba775eb 15 * substantial portions of the Software.
tousaki 0:41941ba775eb 16 *
tousaki 0:41941ba775eb 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
tousaki 0:41941ba775eb 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tousaki 0:41941ba775eb 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
tousaki 0:41941ba775eb 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tousaki 0:41941ba775eb 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tousaki 0:41941ba775eb 22 */
tousaki 0:41941ba775eb 23 #include "GR_PEACH_WlanBP3595.h"
tousaki 0:41941ba775eb 24 #include "GR_PEACH_WlanBP3595_BssType.h"
tousaki 0:41941ba775eb 25 #include "WlanBP3595.h"
tousaki 0:41941ba775eb 26
tousaki 0:41941ba775eb 27 #include "lwip/inet.h"
tousaki 0:41941ba775eb 28 #include "lwip/netif.h"
tousaki 0:41941ba775eb 29 #include "netif/etharp.h"
tousaki 0:41941ba775eb 30 #include "lwip/dhcp.h"
tousaki 0:41941ba775eb 31 #include "wifi_arch.h"
tousaki 0:41941ba775eb 32 #include "lwip/tcpip.h"
tousaki 0:41941ba775eb 33
tousaki 0:41941ba775eb 34 #include "mbed.h"
tousaki 0:41941ba775eb 35
tousaki 0:41941ba775eb 36 /* TCP/IP and Network Interface Initialisation */
tousaki 0:41941ba775eb 37 static struct netif netif;
tousaki 0:41941ba775eb 38
tousaki 0:41941ba775eb 39 static char mac_addr[19];
tousaki 0:41941ba775eb 40 static char ip_addr[17] = "\0";
tousaki 0:41941ba775eb 41 static char gateway[17] = "\0";
tousaki 0:41941ba775eb 42 static char networkmask[17] = "\0";
tousaki 0:41941ba775eb 43 static bool use_dhcp = false;
tousaki 0:41941ba775eb 44
tousaki 0:41941ba775eb 45 static Semaphore tcpip_inited(0);
tousaki 0:41941ba775eb 46 static Semaphore netif_linked(0);
tousaki 0:41941ba775eb 47 static Semaphore netif_up(0);
tousaki 0:41941ba775eb 48 static void (*_wlan_callback_func)(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) = NULL;
tousaki 0:41941ba775eb 49
tousaki 0:41941ba775eb 50 static void tcpip_init_done(void *arg) {
tousaki 0:41941ba775eb 51 tcpip_inited.release();
tousaki 0:41941ba775eb 52 }
tousaki 0:41941ba775eb 53
tousaki 0:41941ba775eb 54 static void netif_link_callback(struct netif *netif) {
tousaki 0:41941ba775eb 55 if (netif_is_link_up(netif)) {
tousaki 0:41941ba775eb 56 netif_linked.release();
tousaki 0:41941ba775eb 57 }
tousaki 0:41941ba775eb 58 }
tousaki 0:41941ba775eb 59
tousaki 0:41941ba775eb 60 static void netif_status_callback(struct netif *netif) {
tousaki 0:41941ba775eb 61 if (netif_is_up(netif)) {
tousaki 0:41941ba775eb 62 strcpy(ip_addr, inet_ntoa(netif->ip_addr));
tousaki 0:41941ba775eb 63 strcpy(gateway, inet_ntoa(netif->gw));
tousaki 0:41941ba775eb 64 strcpy(networkmask, inet_ntoa(netif->netmask));
tousaki 0:41941ba775eb 65 netif_up.release();
tousaki 0:41941ba775eb 66 }
tousaki 0:41941ba775eb 67 }
tousaki 0:41941ba775eb 68
tousaki 0:41941ba775eb 69 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
tousaki 0:41941ba775eb 70 tcpip_init(tcpip_init_done, NULL);
tousaki 0:41941ba775eb 71 tcpip_inited.wait();
tousaki 0:41941ba775eb 72
tousaki 0:41941ba775eb 73 memset((void*) &netif, 0, sizeof(netif));
tousaki 0:41941ba775eb 74 netif_add(&netif, ipaddr, netmask, gw, NULL, wifi_arch_enetif_init, tcpip_input);
tousaki 0:41941ba775eb 75 netif_set_default(&netif);
tousaki 0:41941ba775eb 76
tousaki 0:41941ba775eb 77 netif_set_link_callback (&netif, netif_link_callback);
tousaki 0:41941ba775eb 78 netif_set_status_callback(&netif, netif_status_callback);
tousaki 0:41941ba775eb 79 }
tousaki 0:41941ba775eb 80
tousaki 0:41941ba775eb 81 static void set_mac_address(void) {
tousaki 0:41941ba775eb 82 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1],
tousaki 0:41941ba775eb 83 netif.hwaddr[2], netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]);
tousaki 0:41941ba775eb 84 }
tousaki 0:41941ba775eb 85
tousaki 0:41941ba775eb 86 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) {
tousaki 0:41941ba775eb 87 if ((ucType == 'I') && (usWid == 0x0005)) {
tousaki 0:41941ba775eb 88 if (pucData[0] == 0x01) { // CONNECTED
tousaki 0:41941ba775eb 89 /* Notify the EthernetInterface driver that WLAN was connected */
tousaki 0:41941ba775eb 90 WlanBP3595_Connected();
tousaki 0:41941ba775eb 91 } else {
tousaki 0:41941ba775eb 92 /* Notify the EthernetInterface driver that WLAN was disconnected */
tousaki 0:41941ba775eb 93 WlanBP3595_Disconnected();
tousaki 0:41941ba775eb 94 }
tousaki 0:41941ba775eb 95 }
tousaki 0:41941ba775eb 96 if (_wlan_callback_func != NULL) {
tousaki 0:41941ba775eb 97 _wlan_callback_func(ucType, usWid, usSize, pucData);
tousaki 0:41941ba775eb 98 }
tousaki 0:41941ba775eb 99 }
tousaki 0:41941ba775eb 100
tousaki 0:41941ba775eb 101 static int _wlan_init() {
tousaki 0:41941ba775eb 102 uint32_t status;
tousaki 0:41941ba775eb 103
tousaki 0:41941ba775eb 104 /* Initialize WlanBP3595 */
tousaki 0:41941ba775eb 105 if (WlanBP3595_Init(&_wlan_inf_callback) != 0) {
tousaki 0:41941ba775eb 106 return -1;
tousaki 0:41941ba775eb 107 }
tousaki 0:41941ba775eb 108
tousaki 0:41941ba775eb 109 /* Wait until WLAN_BP3595_START timeout 60s */
tousaki 0:41941ba775eb 110 while (1) {
tousaki 0:41941ba775eb 111 Thread::wait(200);
tousaki 0:41941ba775eb 112 status = WlanBP3595_GetWlanSts();
tousaki 0:41941ba775eb 113 if (status == WLAN_BP3595_START) {
tousaki 0:41941ba775eb 114 break;
tousaki 0:41941ba775eb 115 }
tousaki 0:41941ba775eb 116 }
tousaki 0:41941ba775eb 117
tousaki 0:41941ba775eb 118 return 0;
tousaki 0:41941ba775eb 119 }
tousaki 0:41941ba775eb 120
tousaki 0:41941ba775eb 121 static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security)
tousaki 0:41941ba775eb 122 {
tousaki 0:41941ba775eb 123 int ret;
tousaki 0:41941ba775eb 124 grp_u8 ucWidData8; // 8bit wid data
tousaki 0:41941ba775eb 125 grp_wld_byte_array tBAWidData; // byte array wid data
tousaki 0:41941ba775eb 126
tousaki 0:41941ba775eb 127 // Set BSS type
tousaki 0:41941ba775eb 128 ucWidData8 = BSS_TYPE;
tousaki 0:41941ba775eb 129 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8);
tousaki 0:41941ba775eb 130 if (ret != 0) {
tousaki 0:41941ba775eb 131 return -1;
tousaki 0:41941ba775eb 132 }
tousaki 0:41941ba775eb 133
tousaki 0:41941ba775eb 134 // Set SSID
tousaki 0:41941ba775eb 135 tBAWidData.pucData = (grp_u8 *)ssid;
tousaki 0:41941ba775eb 136 tBAWidData.ulSize = strlen((char *)tBAWidData.pucData);
tousaki 0:41941ba775eb 137 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
tousaki 0:41941ba775eb 138 if (ret != 0) {
tousaki 0:41941ba775eb 139 return -1;
tousaki 0:41941ba775eb 140 }
tousaki 0:41941ba775eb 141
tousaki 0:41941ba775eb 142 if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) {
tousaki 0:41941ba775eb 143 // Set PSK
tousaki 0:41941ba775eb 144 tBAWidData.pucData = (grp_u8 *)pass;
tousaki 0:41941ba775eb 145 tBAWidData.ulSize = strlen((char *)tBAWidData.pucData);
tousaki 0:41941ba775eb 146 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
tousaki 0:41941ba775eb 147 if (ret != 0) {
tousaki 0:41941ba775eb 148 return -1;
tousaki 0:41941ba775eb 149 }
tousaki 0:41941ba775eb 150 }
tousaki 0:41941ba775eb 151
tousaki 0:41941ba775eb 152 // Set 11i mode
tousaki 0:41941ba775eb 153 switch (security) {
tousaki 0:41941ba775eb 154 case NSAPI_SECURITY_WEP:
tousaki 0:41941ba775eb 155 ret = strlen(pass);
tousaki 0:41941ba775eb 156 if (ret == 5) {
tousaki 0:41941ba775eb 157 ucWidData8 = 0x03; // WEP64
tousaki 0:41941ba775eb 158 } else if (ret == 13) {
tousaki 0:41941ba775eb 159 ucWidData8 = 0x07; // WEP128
tousaki 0:41941ba775eb 160 } else {
tousaki 0:41941ba775eb 161 return -1;
tousaki 0:41941ba775eb 162 }
tousaki 0:41941ba775eb 163 break;
tousaki 0:41941ba775eb 164 case NSAPI_SECURITY_WPA:
tousaki 0:41941ba775eb 165 case NSAPI_SECURITY_WPA2:
tousaki 0:41941ba775eb 166 ucWidData8 = 0x79; // WPA/WPA2 Mixed
tousaki 0:41941ba775eb 167 break;
tousaki 0:41941ba775eb 168 case NSAPI_SECURITY_NONE:
tousaki 0:41941ba775eb 169 default:
tousaki 0:41941ba775eb 170 ucWidData8 = 0x00;
tousaki 0:41941ba775eb 171 break;
tousaki 0:41941ba775eb 172 }
tousaki 0:41941ba775eb 173 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
tousaki 0:41941ba775eb 174 if (ret != 0) {
tousaki 0:41941ba775eb 175 return -1;
tousaki 0:41941ba775eb 176 }
tousaki 0:41941ba775eb 177
tousaki 0:41941ba775eb 178 if (security == NSAPI_SECURITY_WEP) {
tousaki 0:41941ba775eb 179 // Set WEP KEY
tousaki 0:41941ba775eb 180 tBAWidData.pucData = (grp_u8 *)pass;
tousaki 0:41941ba775eb 181 tBAWidData.ulSize = strlen((char *)tBAWidData.pucData);
tousaki 0:41941ba775eb 182 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData);
tousaki 0:41941ba775eb 183 if (ret != 0) {
tousaki 0:41941ba775eb 184 return -1;
tousaki 0:41941ba775eb 185 }
tousaki 0:41941ba775eb 186 }
tousaki 0:41941ba775eb 187
tousaki 0:41941ba775eb 188 return 0;
tousaki 0:41941ba775eb 189 }
tousaki 0:41941ba775eb 190
tousaki 0:41941ba775eb 191 int GR_PEACH_WlanBP3595::init() {
tousaki 0:41941ba775eb 192 _wlan_init();
tousaki 0:41941ba775eb 193 use_dhcp = true;
tousaki 0:41941ba775eb 194 init_netif(NULL, NULL, NULL);
tousaki 0:41941ba775eb 195 set_mac_address();
tousaki 0:41941ba775eb 196 return 0;
tousaki 0:41941ba775eb 197 }
tousaki 0:41941ba775eb 198
tousaki 0:41941ba775eb 199 int GR_PEACH_WlanBP3595::init(const char* ip, const char* mask, const char* gateway) {
tousaki 0:41941ba775eb 200 _wlan_init();
tousaki 0:41941ba775eb 201 use_dhcp = false;
tousaki 0:41941ba775eb 202
tousaki 0:41941ba775eb 203 strcpy(ip_addr, ip);
tousaki 0:41941ba775eb 204
tousaki 0:41941ba775eb 205 ip_addr_t ip_n, mask_n, gateway_n;
tousaki 0:41941ba775eb 206 inet_aton(ip, &ip_n);
tousaki 0:41941ba775eb 207 inet_aton(mask, &mask_n);
tousaki 0:41941ba775eb 208 inet_aton(gateway, &gateway_n);
tousaki 0:41941ba775eb 209 init_netif(&ip_n, &mask_n, &gateway_n);
tousaki 0:41941ba775eb 210 set_mac_address();
tousaki 0:41941ba775eb 211
tousaki 0:41941ba775eb 212 return 0;
tousaki 0:41941ba775eb 213 }
tousaki 0:41941ba775eb 214
tousaki 0:41941ba775eb 215 int GR_PEACH_WlanBP3595::connect(const char *ssid,
tousaki 0:41941ba775eb 216 const char *pass,
tousaki 0:41941ba775eb 217 nsapi_security_t security,
tousaki 0:41941ba775eb 218 unsigned int timeout_ms)
tousaki 0:41941ba775eb 219 {
tousaki 0:41941ba775eb 220 _wlan_setting(ssid, pass, security);
tousaki 0:41941ba775eb 221 wifi_arch_enable_interrupts();
tousaki 0:41941ba775eb 222
tousaki 0:41941ba775eb 223 int inited;
tousaki 0:41941ba775eb 224 if (use_dhcp) {
tousaki 0:41941ba775eb 225 dhcp_start(&netif);
tousaki 0:41941ba775eb 226
tousaki 0:41941ba775eb 227 // Wait for an IP Address
tousaki 0:41941ba775eb 228 // -1: error, 0: timeout
tousaki 0:41941ba775eb 229 inited = netif_up.wait(timeout_ms);
tousaki 0:41941ba775eb 230 } else {
tousaki 0:41941ba775eb 231 netif_set_up(&netif);
tousaki 0:41941ba775eb 232
tousaki 0:41941ba775eb 233 // Wait for the link up
tousaki 0:41941ba775eb 234 inited = netif_linked.wait(timeout_ms);
tousaki 0:41941ba775eb 235 }
tousaki 0:41941ba775eb 236
tousaki 0:41941ba775eb 237 return (inited > 0) ? (0) : (-1);
tousaki 0:41941ba775eb 238 }
tousaki 0:41941ba775eb 239
tousaki 0:41941ba775eb 240 int GR_PEACH_WlanBP3595::disconnect() {
tousaki 0:41941ba775eb 241 if (use_dhcp) {
tousaki 0:41941ba775eb 242 dhcp_release(&netif);
tousaki 0:41941ba775eb 243 dhcp_stop(&netif);
tousaki 0:41941ba775eb 244 } else {
tousaki 0:41941ba775eb 245 netif_set_down(&netif);
tousaki 0:41941ba775eb 246 }
tousaki 0:41941ba775eb 247
tousaki 0:41941ba775eb 248 wifi_arch_disable_interrupts();
tousaki 0:41941ba775eb 249
tousaki 0:41941ba775eb 250 return 0;
tousaki 0:41941ba775eb 251 }
tousaki 0:41941ba775eb 252
tousaki 0:41941ba775eb 253 char* GR_PEACH_WlanBP3595::getMACAddress() {
tousaki 0:41941ba775eb 254 return mac_addr;
tousaki 0:41941ba775eb 255 }
tousaki 0:41941ba775eb 256
tousaki 0:41941ba775eb 257 char* GR_PEACH_WlanBP3595::getIPAddress() {
tousaki 0:41941ba775eb 258 return ip_addr;
tousaki 0:41941ba775eb 259 }
tousaki 0:41941ba775eb 260
tousaki 0:41941ba775eb 261 char* GR_PEACH_WlanBP3595::getGateway() {
tousaki 0:41941ba775eb 262 return gateway;
tousaki 0:41941ba775eb 263 }
tousaki 0:41941ba775eb 264
tousaki 0:41941ba775eb 265 char* GR_PEACH_WlanBP3595::getNetworkMask() {
tousaki 0:41941ba775eb 266 return networkmask;
tousaki 0:41941ba775eb 267 }
tousaki 0:41941ba775eb 268
tousaki 0:41941ba775eb 269 int GR_PEACH_WlanBP3595::WlanIoctl(uint32_t ulFunc, void *pvParam) {
tousaki 0:41941ba775eb 270 return WlanBP3595_Ioctl(ulFunc, pvParam);
tousaki 0:41941ba775eb 271 }
tousaki 0:41941ba775eb 272
tousaki 0:41941ba775eb 273 void GR_PEACH_WlanBP3595::setWlanCbFunction(void(*fptr)(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData)) {
tousaki 0:41941ba775eb 274 _wlan_callback_func = fptr;
tousaki 0:41941ba775eb 275 }