This is GR_PEACH_WlanBP3595 class library. The base library is EthernetInterface.
Dependents: GR-PEACH_WlanBP3595AP GR-PEACH_WlanBP3595STA
Revision 0:41941ba775eb, committed 2016-05-24
- Comitter:
- tousaki
- Date:
- Tue May 24 10:17:15 2016 +0000
- Commit message:
- Created 1st version.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GR_PEACH_WlanBP3595.cpp Tue May 24 10:17:15 2016 +0000 @@ -0,0 +1,275 @@ +/* GR_PEACH_WlanBP3595.cpp */ +/* Copyright (C) 2016 Grape Systems, Inc. */ +/* The base file is EthernetInterface.cpp. */ + +/* EthernetInterface.cpp */ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "GR_PEACH_WlanBP3595.h" +#include "GR_PEACH_WlanBP3595_BssType.h" +#include "WlanBP3595.h" + +#include "lwip/inet.h" +#include "lwip/netif.h" +#include "netif/etharp.h" +#include "lwip/dhcp.h" +#include "wifi_arch.h" +#include "lwip/tcpip.h" + +#include "mbed.h" + +/* TCP/IP and Network Interface Initialisation */ +static struct netif netif; + +static char mac_addr[19]; +static char ip_addr[17] = "\0"; +static char gateway[17] = "\0"; +static char networkmask[17] = "\0"; +static bool use_dhcp = false; + +static Semaphore tcpip_inited(0); +static Semaphore netif_linked(0); +static Semaphore netif_up(0); +static void (*_wlan_callback_func)(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) = NULL; + +static void tcpip_init_done(void *arg) { + tcpip_inited.release(); +} + +static void netif_link_callback(struct netif *netif) { + if (netif_is_link_up(netif)) { + netif_linked.release(); + } +} + +static void netif_status_callback(struct netif *netif) { + if (netif_is_up(netif)) { + strcpy(ip_addr, inet_ntoa(netif->ip_addr)); + strcpy(gateway, inet_ntoa(netif->gw)); + strcpy(networkmask, inet_ntoa(netif->netmask)); + netif_up.release(); + } +} + +static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { + tcpip_init(tcpip_init_done, NULL); + tcpip_inited.wait(); + + memset((void*) &netif, 0, sizeof(netif)); + netif_add(&netif, ipaddr, netmask, gw, NULL, wifi_arch_enetif_init, tcpip_input); + netif_set_default(&netif); + + netif_set_link_callback (&netif, netif_link_callback); + netif_set_status_callback(&netif, netif_status_callback); +} + +static void set_mac_address(void) { + snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", netif.hwaddr[0], netif.hwaddr[1], + netif.hwaddr[2], netif.hwaddr[3], netif.hwaddr[4], netif.hwaddr[5]); +} + +static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) { + if ((ucType == 'I') && (usWid == 0x0005)) { + if (pucData[0] == 0x01) { // CONNECTED + /* Notify the EthernetInterface driver that WLAN was connected */ + WlanBP3595_Connected(); + } else { + /* Notify the EthernetInterface driver that WLAN was disconnected */ + WlanBP3595_Disconnected(); + } + } + if (_wlan_callback_func != NULL) { + _wlan_callback_func(ucType, usWid, usSize, pucData); + } +} + +static int _wlan_init() { + uint32_t status; + + /* Initialize WlanBP3595 */ + if (WlanBP3595_Init(&_wlan_inf_callback) != 0) { + return -1; + } + + /* Wait until WLAN_BP3595_START timeout 60s */ + while (1) { + Thread::wait(200); + status = WlanBP3595_GetWlanSts(); + if (status == WLAN_BP3595_START) { + break; + } + } + + return 0; +} + +static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security) +{ + int ret; + grp_u8 ucWidData8; // 8bit wid data + grp_wld_byte_array tBAWidData; // byte array wid data + + // Set BSS type + ucWidData8 = BSS_TYPE; + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8); + if (ret != 0) { + return -1; + } + + // Set SSID + tBAWidData.pucData = (grp_u8 *)ssid; + tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData); + if (ret != 0) { + return -1; + } + + if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) { + // Set PSK + tBAWidData.pucData = (grp_u8 *)pass; + tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData); + if (ret != 0) { + return -1; + } + } + + // Set 11i mode + switch (security) { + case NSAPI_SECURITY_WEP: + ret = strlen(pass); + if (ret == 5) { + ucWidData8 = 0x03; // WEP64 + } else if (ret == 13) { + ucWidData8 = 0x07; // WEP128 + } else { + return -1; + } + break; + case NSAPI_SECURITY_WPA: + case NSAPI_SECURITY_WPA2: + ucWidData8 = 0x79; // WPA/WPA2 Mixed + break; + case NSAPI_SECURITY_NONE: + default: + ucWidData8 = 0x00; + break; + } + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8); + if (ret != 0) { + return -1; + } + + if (security == NSAPI_SECURITY_WEP) { + // Set WEP KEY + tBAWidData.pucData = (grp_u8 *)pass; + tBAWidData.ulSize = strlen((char *)tBAWidData.pucData); + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData); + if (ret != 0) { + return -1; + } + } + + return 0; +} + +int GR_PEACH_WlanBP3595::init() { + _wlan_init(); + use_dhcp = true; + init_netif(NULL, NULL, NULL); + set_mac_address(); + return 0; +} + +int GR_PEACH_WlanBP3595::init(const char* ip, const char* mask, const char* gateway) { + _wlan_init(); + use_dhcp = false; + + strcpy(ip_addr, ip); + + ip_addr_t ip_n, mask_n, gateway_n; + inet_aton(ip, &ip_n); + inet_aton(mask, &mask_n); + inet_aton(gateway, &gateway_n); + init_netif(&ip_n, &mask_n, &gateway_n); + set_mac_address(); + + return 0; +} + +int GR_PEACH_WlanBP3595::connect(const char *ssid, + const char *pass, + nsapi_security_t security, + unsigned int timeout_ms) +{ + _wlan_setting(ssid, pass, security); + wifi_arch_enable_interrupts(); + + int inited; + if (use_dhcp) { + dhcp_start(&netif); + + // Wait for an IP Address + // -1: error, 0: timeout + inited = netif_up.wait(timeout_ms); + } else { + netif_set_up(&netif); + + // Wait for the link up + inited = netif_linked.wait(timeout_ms); + } + + return (inited > 0) ? (0) : (-1); +} + +int GR_PEACH_WlanBP3595::disconnect() { + if (use_dhcp) { + dhcp_release(&netif); + dhcp_stop(&netif); + } else { + netif_set_down(&netif); + } + + wifi_arch_disable_interrupts(); + + return 0; +} + +char* GR_PEACH_WlanBP3595::getMACAddress() { + return mac_addr; +} + +char* GR_PEACH_WlanBP3595::getIPAddress() { + return ip_addr; +} + +char* GR_PEACH_WlanBP3595::getGateway() { + return gateway; +} + +char* GR_PEACH_WlanBP3595::getNetworkMask() { + return networkmask; +} + +int GR_PEACH_WlanBP3595::WlanIoctl(uint32_t ulFunc, void *pvParam) { + return WlanBP3595_Ioctl(ulFunc, pvParam); +} + +void GR_PEACH_WlanBP3595::setWlanCbFunction(void(*fptr)(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData)) { + _wlan_callback_func = fptr; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GR_PEACH_WlanBP3595.h Tue May 24 10:17:15 2016 +0000 @@ -0,0 +1,129 @@ +/* GR_PEACH_WlanBP3595.h */ +/* Copyright (C) 2016 Grape Systems, Inc. */ +/* The base file is EthernetInterface.h. */ + +/* EthernetInterface.h */ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef GR_PEACH_WLANBP3595_H_ +#define GR_PEACH_WLANBP3595_H_ + +#if !defined(TARGET_RZ_A1H) +#error The GR_PEACH_WlanBP3595 is not supported on this target +#endif + +#include "rtos.h" +#include "lwip/netif.h" + +/** Enum of WiFi encryption types + * + * The security type specifies a particular security to use when + * connected to a WiFi network + * + * @enum nsapi_protocol_t + */ +enum nsapi_security_t { + NSAPI_SECURITY_NONE = 0, /*!< open access point */ + NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */ + NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */ + NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */ +}; + + /** Interface using Wlan to connect to an IP-based network + * + */ +class GR_PEACH_WlanBP3595 { +public: + /** Initialize the interface with DHCP. + * Initialize the interface and configure it to use DHCP (no connection at this point). + * @return 0 on success, a negative number on failure + */ + static int init(); //With DHCP + + /** Initialize the interface with a static IP address. + * Initialize the interface and configure it with the following static configuration (no connection at this point). + * @param ip the IP address to use + * @param mask the IP address mask + * @param gateway the gateway to use + * @return 0 on success, a negative number on failure + */ + static int init(const char* ip, const char* mask, const char* gateway); + + /** Connect + * Bring the interface up, start DHCP if needed. + * @param ssid Name of the network to connect to + * @param pass Security passphrase to connect to the network + * @param security Type of encryption to connect with + * @param timeout_ms timeout in ms (default: (15)s). + * @return 0 on success, a negative number on failure + */ + static int connect( + const char *ssid, + const char *pass, + nsapi_security_t security = NSAPI_SECURITY_WPA2, + unsigned int timeout_ms=1800000); + + /** Disconnect + * Bring the interface down + * @return 0 on success, a negative number on failure + */ + static int disconnect(); + + /** Get the MAC address of your Wlan interface + * @return a pointer to a string containing the MAC address + */ + static char* getMACAddress(); + + /** Get the IP address of your Wlan interface + * @return a pointer to a string containing the IP address + */ + static char* getIPAddress(); + + /** Get the Gateway address of your Wlan interface + * @return a pointer to a string containing the Gateway address + */ + static char* getGateway(); + + /** Get the Network mask of your Wlan interface + * @return a pointer to a string containing the Network mask + */ + static char* getNetworkMask(); + + /** Execute WID function + * Please see "WID" of document "Software development specs of BP3595" for the details. "https://developer.mbed.org/teams/Rohm/wiki/Datasheet" + * @param ulFunc A function number + * @param pvParam The data buffer (the parameter) + * @return 0 on success, "The others" on failure + */ + static int WlanIoctl(uint32_t ulFunc, void *pvParam); + + /** Set WLAN Information callback function + * @param fptr WLAN Information callback function + */ + static void setWlanCbFunction(void(*fptr)(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData)); + +}; + +#include "TCPSocketConnection.h" +#include "TCPSocketServer.h" + +#include "Endpoint.h" +#include "UDPSocket.h" + +#endif /* GR_PEACH_WLANBP3595_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip-wifi/arch/rza1_bp3595_emac.c Tue May 24 10:17:15 2016 +0000 @@ -0,0 +1,155 @@ +#include "lwip/opt.h" +#include "lwip/def.h" +#include "lwip/mem.h" +#include "lwip/pbuf.h" +#include "lwip/sys.h" +#include "lwip/stats.h" +#include "lwip/snmp.h" +#include "lwip/tcpip.h" +#include "netif/etharp.h" +#include "netif/ppp_oe.h" +#include "mbed_interface.h" + +#include <string.h> +#include "rza1_bp3595_emac.h" +#include "WlanBP3595.h" + +/* Static variable */ +static struct netif * volatile target_netif = NULL; +static volatile int init_sts = 0; /* 0: not initialized, 1:initialized */ +static int connect_sts = 0; /* 0: disconnected, 1:connected */ + +/* Static function */ +static err_t rza1_bp3595_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr); +static err_t rza1_bp3595_low_level_output(struct netif *netif, struct pbuf *p); + +/* This function is called from the receiving thread of WlanBP3595 library. */ +void rza1_bp3595_input(void *buff, u16_t recv_size) { + struct eth_hdr *ethhdr; + struct pbuf *p; + + if (recv_size != 0) { + p = pbuf_alloc(PBUF_RAW, recv_size, PBUF_RAM); + if (p != NULL) { + /* Copy data */ + memcpy(p->payload, buff, recv_size); + /* Check Ethernet frame type */ + ethhdr = p->payload; + switch (htons(ethhdr->type)) { + case ETHTYPE_IP: + case ETHTYPE_ARP: +#if PPPOE_SUPPORT + case ETHTYPE_PPPOEDISC: + case ETHTYPE_PPPOE: +#endif /* PPPOE_SUPPORT */ + /* full packet send to tcpip_thread to process */ + if (target_netif->input(p, target_netif) != ERR_OK) { + /* Free buffer */ + pbuf_free(p); + } + break; + default: + /* Free buffer */ + pbuf_free(p); + break; + } + } + } +} + +void rza1_bp3595_connected(void) { + /* 0: not initialized, 1:initialized */ + if (init_sts == 1) { + /* 0: disconnected, 1:connected */ + if (connect_sts == 0) { + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, (void*)target_netif, 1); + connect_sts = 1; + } + } +} + +void rza1_bp3595_disconnected(void) { + /* 0: not initialized, 1:initialized */ + if (init_sts == 1) { + /* 0: disconnected, 1:connected */ + if (connect_sts == 1) { + tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, (void*)target_netif, 1); + connect_sts = 0; + } + } +} + +static err_t rza1_bp3595_etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr) { + if (netif->flags & NETIF_FLAG_LINK_UP) { + return etharp_output(netif, q, ipaddr); + } + + return ERR_CONN; +} + +static err_t rza1_bp3595_low_level_output(struct netif *netif, struct pbuf *p) { + err_t err = ERR_MEM; + int ret; + + ret = WlanBP3595_Output(p); + if (ret == 0) { + err = ERR_OK; + } + + return err; +} + +err_t wifi_arch_enetif_init(struct netif *netif) { + grp_wld_byte_array tBAWidData; /* byte array wid data */ + int ret; + + /* Set MAC hardware address */ + tBAWidData.pucData = (grp_u8 *)netif->hwaddr; + tBAWidData.ulSize = 6; + + ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_GET_MAC_ADDRESS, &tBAWidData); + if (ret != 0) { + /* error(return a default MAC hardware address) */ + netif->hwaddr[0] = 0x00; + netif->hwaddr[1] = 0x02; + netif->hwaddr[2] = 0xF7; + netif->hwaddr[3] = 0xF0; + netif->hwaddr[4] = 0x00; + netif->hwaddr[5] = 0x00; + } + + /* Set MAC hardware address length */ + netif->hwaddr_len = ETHARP_HWADDR_LEN; + + /* Set maximum transfer unit */ + netif->mtu = 1500; + + /* Set device capabilities */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP; + +#if LWIP_NETIF_HOSTNAME + /* Initialize interface hostname */ + netif->hostname = "lwiprza1"; +#endif /* LWIP_NETIF_HOSTNAME */ + + netif->name[0] = 'e'; + netif->name[1] = 'n'; + + netif->output = rza1_bp3595_etharp_output; + netif->linkoutput = rza1_bp3595_low_level_output; + + target_netif = netif; + + init_sts = 1; /* 0: not initialized, 1:initialized */ + + return ERR_OK; +} + +void wifi_arch_enable_interrupts(void) { + WlanBP3595_RecvEnable(); +} + +void wifi_arch_disable_interrupts(void) { + WlanBP3595_RecvDisable(); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip-wifi/arch/rza1_bp3595_emac.h Tue May 24 10:17:15 2016 +0000 @@ -0,0 +1,27 @@ +#ifndef _RZA1_BP3595_EMAC_H_ +#define _RZA1_BP3595_EMAC_H_ + +#include "lwip/opt.h" +#include "lwip/def.h" +#include "lwip/mem.h" +#include "lwip/pbuf.h" +#include "lwip/sys.h" +#include "lwip/stats.h" +#include "lwip/snmp.h" +#include "netif/etharp.h" +#include "netif/ppp_oe.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Function */ +void rza1_bp3595_input(void *buff, u16_t recv_size); +void rza1_bp3595_connected(void); +void rza1_bp3595_disconnected(void); + +#ifdef __cplusplus +} +#endif + +#endif /* _RZA1_BP3595_EMAC_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wifi_arch.h Tue May 24 10:17:15 2016 +0000 @@ -0,0 +1,44 @@ +/* wifi_arch.h */ +/* Copyright (C) 2016 Grape Systems, Inc. */ +/* The base file is eth_arch.h. */ + +/* eth_arch.h */ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// Architecture specific WiFi interface +// Must be implemented by each target + +#ifndef WIFIARCH_H_ +#define WIFIARCH_H_ + +#include "lwip/netif.h" + +#ifdef __cplusplus +extern "C" { +#endif +void wifi_arch_enable_interrupts(void); +void wifi_arch_disable_interrupts(void); +err_t wifi_arch_enetif_init(struct netif *netif); + +#ifdef __cplusplus +} +#endif + +#endif // #ifndef WIFIARCH_H_ +