This is a sample application program for GR-PEACH_WlanBP3595STA library. GR-PEACH_WlanBP3595STA library only works with GR-PEACH. This sample works as TCP socket sever, and this program sends a message when a connection is accepted.

Dependencies:   EthernetInterface GR-PEACH_WlanBP3595STA mbed-rtos mbed

Fork of GR-PEACH_WlanBP3595STA_sample by Rohm

Committer:
tousaki
Date:
Fri Dec 25 07:51:26 2015 +0000
Revision:
0:64fab22ec392
Child:
1:423b42d70d3f
Created 1st version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tousaki 0:64fab22ec392 1 /* This is a sample application program for WlanBP3595 library. */
tousaki 0:64fab22ec392 2 /* This program works as TCP socket sever, and this program sends a message */
tousaki 0:64fab22ec392 3 /* when a connection is accepted. The setting of the WLAN is WPA2+AES. */
tousaki 0:64fab22ec392 4
tousaki 0:64fab22ec392 5 /*
tousaki 0:64fab22ec392 6 This program works with the following libraries.
tousaki 0:64fab22ec392 7 mbed-rtos : revision 84
tousaki 0:64fab22ec392 8 EthernetInterface_GR-PEACH_WlanBP3595STA : revision 50
tousaki 0:64fab22ec392 9 */
tousaki 0:64fab22ec392 10
tousaki 0:64fab22ec392 11 #include "mbed.h"
tousaki 0:64fab22ec392 12 #include "rtos.h"
tousaki 0:64fab22ec392 13 #include "EthernetInterface.h"
tousaki 0:64fab22ec392 14 #include "WlanBP3595.h"
tousaki 0:64fab22ec392 15
tousaki 0:64fab22ec392 16 /* Please change the following macro definition to your setting. */
tousaki 0:64fab22ec392 17 #define WLAN_SSID ("SSIDofYourAP") // SSID
tousaki 0:64fab22ec392 18 #define WLAN_PSK ("PSKofYourAP") // PSK(Pre-Shared Key)
tousaki 0:64fab22ec392 19 #define SERVER_IP ("192.168.1.200") // Server IP address
tousaki 0:64fab22ec392 20 #define SERVER_PORT (50000) // TCP server socket port number
tousaki 0:64fab22ec392 21 #define SUBNET_MASK ("255.255.255.0") // Subnet mask
tousaki 0:64fab22ec392 22 #define DEFAULT_GATEWAY ("192.168.1.1") // Default gateway
tousaki 0:64fab22ec392 23 #define CONNECT_TIMEOUT (3*60*1000) // Connect-Timeout in ms
tousaki 0:64fab22ec392 24 #define SEND_MESSAGE ("Hello, world!\r\n") // Send-message
tousaki 0:64fab22ec392 25
tousaki 0:64fab22ec392 26 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData);
tousaki 0:64fab22ec392 27 static int _wlan_connect(void);
tousaki 0:64fab22ec392 28
tousaki 0:64fab22ec392 29 DigitalOut red_led(LED1); // On: error
tousaki 0:64fab22ec392 30 DigitalOut green_led(LED2); // On: WLAN has been connected
tousaki 0:64fab22ec392 31
tousaki 0:64fab22ec392 32 /** Main function
tousaki 0:64fab22ec392 33 *
tousaki 0:64fab22ec392 34 */
tousaki 0:64fab22ec392 35 int main() {
tousaki 0:64fab22ec392 36 EthernetInterface eth;
tousaki 0:64fab22ec392 37 TCPSocketServer server;
tousaki 0:64fab22ec392 38 TCPSocketConnection connection;
tousaki 0:64fab22ec392 39 uint32_t status;
tousaki 0:64fab22ec392 40 int ret;
tousaki 0:64fab22ec392 41
tousaki 0:64fab22ec392 42 /* Initialize WlanBP3595 */
tousaki 0:64fab22ec392 43 ret = WlanBP3595_Init(_wlan_inf_callback);
tousaki 0:64fab22ec392 44 if (ret != 0) {
tousaki 0:64fab22ec392 45 /* error */
tousaki 0:64fab22ec392 46 red_led = 1;
tousaki 0:64fab22ec392 47 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 48 }
tousaki 0:64fab22ec392 49
tousaki 0:64fab22ec392 50 /* Wait until WLAN_BP3595_START */
tousaki 0:64fab22ec392 51 while (1) {
tousaki 0:64fab22ec392 52 Thread::wait(200);
tousaki 0:64fab22ec392 53 status = WlanBP3595_GetWlanSts();
tousaki 0:64fab22ec392 54 if (status == WLAN_BP3595_START) {
tousaki 0:64fab22ec392 55 break;
tousaki 0:64fab22ec392 56 }
tousaki 0:64fab22ec392 57 }
tousaki 0:64fab22ec392 58
tousaki 0:64fab22ec392 59 /* Initialize EthernetInterface */
tousaki 0:64fab22ec392 60 ret = eth.init(SERVER_IP, SUBNET_MASK, DEFAULT_GATEWAY);
tousaki 0:64fab22ec392 61 if (ret != 0) {
tousaki 0:64fab22ec392 62 /* error */
tousaki 0:64fab22ec392 63 red_led = 1;
tousaki 0:64fab22ec392 64 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 65 }
tousaki 0:64fab22ec392 66
tousaki 0:64fab22ec392 67 /* Connect(WLAN) */
tousaki 0:64fab22ec392 68 ret = _wlan_connect();
tousaki 0:64fab22ec392 69 if (ret != 0) {
tousaki 0:64fab22ec392 70 /* error */
tousaki 0:64fab22ec392 71 red_led = 1;
tousaki 0:64fab22ec392 72 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 73 }
tousaki 0:64fab22ec392 74
tousaki 0:64fab22ec392 75 /* Connect(EthernetInterface) */
tousaki 0:64fab22ec392 76 ret = eth.connect(CONNECT_TIMEOUT);
tousaki 0:64fab22ec392 77 if (ret != 0) {
tousaki 0:64fab22ec392 78 /* error */
tousaki 0:64fab22ec392 79 red_led = 1;
tousaki 0:64fab22ec392 80 while (1) { Thread::wait(1000); }
tousaki 0:64fab22ec392 81 }
tousaki 0:64fab22ec392 82
tousaki 0:64fab22ec392 83 /* Bind and listen */
tousaki 0:64fab22ec392 84 server.bind(SERVER_PORT);
tousaki 0:64fab22ec392 85 server.listen();
tousaki 0:64fab22ec392 86
tousaki 0:64fab22ec392 87 /* Loop */
tousaki 0:64fab22ec392 88 while (1) {
tousaki 0:64fab22ec392 89 /* Accept */
tousaki 0:64fab22ec392 90 server.accept(connection);
tousaki 0:64fab22ec392 91 printf("Connection from: %s\n", connection.get_address());
tousaki 0:64fab22ec392 92
tousaki 0:64fab22ec392 93 /* Send a message */
tousaki 0:64fab22ec392 94 connection.send_all((char *)SEND_MESSAGE, sizeof(SEND_MESSAGE)-1);
tousaki 0:64fab22ec392 95
tousaki 0:64fab22ec392 96 /* Close */
tousaki 0:64fab22ec392 97 connection.close();
tousaki 0:64fab22ec392 98 }
tousaki 0:64fab22ec392 99 }
tousaki 0:64fab22ec392 100
tousaki 0:64fab22ec392 101 /** WLAN Information callback function
tousaki 0:64fab22ec392 102 *
tousaki 0:64fab22ec392 103 */
tousaki 0:64fab22ec392 104 static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData)
tousaki 0:64fab22ec392 105 {
tousaki 0:64fab22ec392 106 if (ucType == 'I')
tousaki 0:64fab22ec392 107 {
tousaki 0:64fab22ec392 108 if (usWid == 0x0005) // WID_STATUS
tousaki 0:64fab22ec392 109 {
tousaki 0:64fab22ec392 110 if (pucData[0] == 0x01) // CONNECTED
tousaki 0:64fab22ec392 111 {
tousaki 0:64fab22ec392 112 /* Notify the EthernetInterface driver that WLAN has been connected */
tousaki 0:64fab22ec392 113 WlanBP3595_Connected();
tousaki 0:64fab22ec392 114 green_led = 1;
tousaki 0:64fab22ec392 115 }
tousaki 0:64fab22ec392 116 else
tousaki 0:64fab22ec392 117 {
tousaki 0:64fab22ec392 118 /* Notify the EthernetInterface driver that WLAN has been disconnected */
tousaki 0:64fab22ec392 119 WlanBP3595_Disconnected();
tousaki 0:64fab22ec392 120 green_led = 0;
tousaki 0:64fab22ec392 121 }
tousaki 0:64fab22ec392 122 }
tousaki 0:64fab22ec392 123 }
tousaki 0:64fab22ec392 124 }
tousaki 0:64fab22ec392 125
tousaki 0:64fab22ec392 126 /** WLAN connecting function
tousaki 0:64fab22ec392 127 *
tousaki 0:64fab22ec392 128 */
tousaki 0:64fab22ec392 129 static int _wlan_connect(void)
tousaki 0:64fab22ec392 130 {
tousaki 0:64fab22ec392 131 grp_u8 ucWidData8; // 8bit wid data
tousaki 0:64fab22ec392 132 grp_wld_byte_array tBAWidData; // byte array wid data
tousaki 0:64fab22ec392 133 int ret;
tousaki 0:64fab22ec392 134
tousaki 0:64fab22ec392 135 /* Set BSS type */
tousaki 0:64fab22ec392 136 ucWidData8 = 0x00; // BSS-STA(Infrastructure)
tousaki 0:64fab22ec392 137 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8);
tousaki 0:64fab22ec392 138 if (ret != 0) {
tousaki 0:64fab22ec392 139 /* error */
tousaki 0:64fab22ec392 140 return -1;
tousaki 0:64fab22ec392 141 }
tousaki 0:64fab22ec392 142
tousaki 0:64fab22ec392 143 /* Set SSID */
tousaki 0:64fab22ec392 144 tBAWidData.pucData = (grp_u8 *)WLAN_SSID;
tousaki 0:64fab22ec392 145 tBAWidData.ulSize = strlen((char *)tBAWidData.pucData);
tousaki 0:64fab22ec392 146 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
tousaki 0:64fab22ec392 147 if (ret != 0) {
tousaki 0:64fab22ec392 148 /* error */
tousaki 0:64fab22ec392 149 return -1;
tousaki 0:64fab22ec392 150 }
tousaki 0:64fab22ec392 151
tousaki 0:64fab22ec392 152 /* Set 11i mode */
tousaki 0:64fab22ec392 153 ucWidData8 = 0x01 | // Encryption is enable
tousaki 0:64fab22ec392 154 0x10 | // WPA2 is enable
tousaki 0:64fab22ec392 155 0x20; // CCMP(AES) is enable
tousaki 0:64fab22ec392 156 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
tousaki 0:64fab22ec392 157 if (ret != 0) {
tousaki 0:64fab22ec392 158 /* error */
tousaki 0:64fab22ec392 159 return -1;
tousaki 0:64fab22ec392 160 }
tousaki 0:64fab22ec392 161
tousaki 0:64fab22ec392 162 /* Set PSK */
tousaki 0:64fab22ec392 163 tBAWidData.pucData = (grp_u8 *)WLAN_PSK;
tousaki 0:64fab22ec392 164 tBAWidData.ulSize = strlen((char *)tBAWidData.pucData);
tousaki 0:64fab22ec392 165 ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
tousaki 0:64fab22ec392 166 if (ret != 0) {
tousaki 0:64fab22ec392 167 /* error */
tousaki 0:64fab22ec392 168 return -1;
tousaki 0:64fab22ec392 169 }
tousaki 0:64fab22ec392 170
tousaki 0:64fab22ec392 171 return 0;
tousaki 0:64fab22ec392 172 }