This is Wi-Fi interface of WizFi310 for mbed os 5

Dependents:   mbed-os-example-wifi

Committer:
kaizen
Date:
Tue Dec 19 16:59:33 2017 +0900
Revision:
1:8e468bb83e1e
Parent:
0:d0e3f515f99d
This is WizFi310 Library for mbed os5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kaizen 0:d0e3f515f99d 1 /* WiFi Example
kaizen 0:d0e3f515f99d 2 * Copyright (c) 2016 ARM Limited
kaizen 0:d0e3f515f99d 3 *
kaizen 0:d0e3f515f99d 4 * Licensed under the Apache License, Version 2.0 (the "License");
kaizen 0:d0e3f515f99d 5 * you may not use this file except in compliance with the License.
kaizen 0:d0e3f515f99d 6 * You may obtain a copy of the License at
kaizen 0:d0e3f515f99d 7 *
kaizen 0:d0e3f515f99d 8 * http://www.apache.org/licenses/LICENSE-2.0
kaizen 0:d0e3f515f99d 9 *
kaizen 0:d0e3f515f99d 10 * Unless required by applicable law or agreed to in writing, software
kaizen 0:d0e3f515f99d 11 * distributed under the License is distributed on an "AS IS" BASIS,
kaizen 0:d0e3f515f99d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kaizen 0:d0e3f515f99d 13 * See the License for the specific language governing permissions and
kaizen 0:d0e3f515f99d 14 * limitations under the License.
kaizen 0:d0e3f515f99d 15 */
kaizen 0:d0e3f515f99d 16
kaizen 0:d0e3f515f99d 17 #include "mbed.h"
kaizen 0:d0e3f515f99d 18 #include "TCPSocket.h"
kaizen 0:d0e3f515f99d 19
kaizen 0:d0e3f515f99d 20 #define WIFI_ESP8266 1
kaizen 0:d0e3f515f99d 21 #define WIFI_IDW0XX1 2
kaizen 0:d0e3f515f99d 22 #define WIFI_WIZFI310 3
kaizen 0:d0e3f515f99d 23
kaizen 0:d0e3f515f99d 24 #if TARGET_UBLOX_EVK_ODIN_W2
kaizen 0:d0e3f515f99d 25 #include "OdinWiFiInterface.h"
kaizen 0:d0e3f515f99d 26 OdinWiFiInterface wifi;
kaizen 0:d0e3f515f99d 27
kaizen 0:d0e3f515f99d 28 #elif TARGET_REALTEK_RTL8195AM
kaizen 0:d0e3f515f99d 29 #include "RTWInterface.h"
kaizen 0:d0e3f515f99d 30 RTWInterface wifi;
kaizen 0:d0e3f515f99d 31
kaizen 0:d0e3f515f99d 32 #else // External WiFi modules
kaizen 0:d0e3f515f99d 33
kaizen 0:d0e3f515f99d 34 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
kaizen 0:d0e3f515f99d 35 #include "ESP8266Interface.h"
kaizen 0:d0e3f515f99d 36 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
kaizen 0:d0e3f515f99d 37 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
kaizen 0:d0e3f515f99d 38 #include "SpwfSAInterface.h"
kaizen 0:d0e3f515f99d 39 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
kaizen 0:d0e3f515f99d 40 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_WIZFI310
kaizen 0:d0e3f515f99d 41 #include "WizFi310Interface.h"
kaizen 0:d0e3f515f99d 42 WizFi310Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
kaizen 0:d0e3f515f99d 43 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_WIZFI310
kaizen 0:d0e3f515f99d 44
kaizen 0:d0e3f515f99d 45 #endif
kaizen 0:d0e3f515f99d 46
kaizen 0:d0e3f515f99d 47 #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE
kaizen 0:d0e3f515f99d 48 #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 5
kaizen 0:d0e3f515f99d 49 #endif
kaizen 0:d0e3f515f99d 50
kaizen 0:d0e3f515f99d 51 namespace {
kaizen 0:d0e3f515f99d 52 char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
kaizen 0:d0e3f515f99d 53 char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
kaizen 0:d0e3f515f99d 54 const char ASCII_MAX = '~' - ' ';
kaizen 0:d0e3f515f99d 55 const int ECHO_LOOPS = 16;
kaizen 0:d0e3f515f99d 56 char uuid[48] = {0};
kaizen 0:d0e3f515f99d 57 }
kaizen 0:d0e3f515f99d 58
kaizen 0:d0e3f515f99d 59 void prep_buffer(char *uuid, char *tx_buffer, size_t tx_size) {
kaizen 0:d0e3f515f99d 60 size_t i = 0;
kaizen 0:d0e3f515f99d 61
kaizen 0:d0e3f515f99d 62 memcpy(tx_buffer, uuid, strlen(uuid));
kaizen 0:d0e3f515f99d 63 i += strlen(uuid);
kaizen 0:d0e3f515f99d 64
kaizen 0:d0e3f515f99d 65 tx_buffer[i++] = ' ';
kaizen 0:d0e3f515f99d 66
kaizen 0:d0e3f515f99d 67 for (; i<tx_size; ++i) {
kaizen 0:d0e3f515f99d 68 tx_buffer[i] = (rand() % 10) + '0';
kaizen 0:d0e3f515f99d 69 }
kaizen 0:d0e3f515f99d 70 }
kaizen 0:d0e3f515f99d 71
kaizen 0:d0e3f515f99d 72 void test_udp_echo(NetworkInterface *net)
kaizen 0:d0e3f515f99d 73 {
kaizen 0:d0e3f515f99d 74 printf("UDP client IP Address is %s\n", net->get_ip_address());
kaizen 0:d0e3f515f99d 75
kaizen 0:d0e3f515f99d 76 printf("target_ip %s\r\n", net->get_ip_address());
kaizen 0:d0e3f515f99d 77 char recv_key[] = "host_port";
kaizen 0:d0e3f515f99d 78 char ipbuf[60] = {0};
kaizen 0:d0e3f515f99d 79 char portbuf[16] = {0};
kaizen 0:d0e3f515f99d 80 unsigned int port = 5000;
kaizen 0:d0e3f515f99d 81
kaizen 0:d0e3f515f99d 82 UDPSocket sock;
kaizen 0:d0e3f515f99d 83 sock.open(net);
kaizen 0:d0e3f515f99d 84 sock.set_timeout(500);
kaizen 0:d0e3f515f99d 85
kaizen 0:d0e3f515f99d 86 //printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
kaizen 0:d0e3f515f99d 87 nsapi_addr_t addr = {NSAPI_IPv4, 192,168,1,46};
kaizen 0:d0e3f515f99d 88 SocketAddress udp_addr(addr, port);
kaizen 0:d0e3f515f99d 89
kaizen 0:d0e3f515f99d 90 int success = 0;
kaizen 0:d0e3f515f99d 91 for (int i=0; success < ECHO_LOOPS; i++)
kaizen 0:d0e3f515f99d 92 {
kaizen 0:d0e3f515f99d 93 prep_buffer(uuid, tx_buffer, sizeof(tx_buffer));
kaizen 0:d0e3f515f99d 94 const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
kaizen 0:d0e3f515f99d 95 if (ret >= 0) {
kaizen 0:d0e3f515f99d 96 printf("[%02d] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
kaizen 0:d0e3f515f99d 97 } else {
kaizen 0:d0e3f515f99d 98 printf("[%02d] Network error %d\n", i, ret);
kaizen 0:d0e3f515f99d 99 continue;
kaizen 0:d0e3f515f99d 100 }
kaizen 0:d0e3f515f99d 101
kaizen 0:d0e3f515f99d 102 SocketAddress temp_addr;
kaizen 0:d0e3f515f99d 103 const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
kaizen 0:d0e3f515f99d 104 if (n >= 0) {
kaizen 0:d0e3f515f99d 105 printf("[%02d] recv %d bytes - %.*s \n", i, n, n, rx_buffer);
kaizen 0:d0e3f515f99d 106 } else {
kaizen 0:d0e3f515f99d 107 printf("[%02d] Network error %d\n", i, n);
kaizen 0:d0e3f515f99d 108 continue;
kaizen 0:d0e3f515f99d 109 }
kaizen 0:d0e3f515f99d 110
kaizen 0:d0e3f515f99d 111 if ((temp_addr == udp_addr &&
kaizen 0:d0e3f515f99d 112 n == sizeof(tx_buffer) &&
kaizen 0:d0e3f515f99d 113 memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
kaizen 0:d0e3f515f99d 114 success += 1;
kaizen 0:d0e3f515f99d 115
kaizen 0:d0e3f515f99d 116 printf("[%02d] success #%d\n", i, success);
kaizen 0:d0e3f515f99d 117 continue;
kaizen 0:d0e3f515f99d 118 }
kaizen 0:d0e3f515f99d 119
kaizen 0:d0e3f515f99d 120 // failed, clean out any remaining bad packets
kaizen 0:d0e3f515f99d 121 sock.set_timeout(0);
kaizen 0:d0e3f515f99d 122 while (true) {
kaizen 0:d0e3f515f99d 123 nsapi_size_or_error_t err = sock.recvfrom(NULL, NULL, 0);
kaizen 0:d0e3f515f99d 124 if (err == NSAPI_ERROR_WOULD_BLOCK) {
kaizen 0:d0e3f515f99d 125 break;
kaizen 0:d0e3f515f99d 126 }
kaizen 0:d0e3f515f99d 127 }
kaizen 0:d0e3f515f99d 128 sock.set_timeout(500);
kaizen 0:d0e3f515f99d 129 }
kaizen 0:d0e3f515f99d 130 sock.close();
kaizen 0:d0e3f515f99d 131 net->disconnect();
kaizen 0:d0e3f515f99d 132 }
kaizen 0:d0e3f515f99d 133
kaizen 0:d0e3f515f99d 134 int main()
kaizen 0:d0e3f515f99d 135 {
kaizen 0:d0e3f515f99d 136 int count = 0;
kaizen 0:d0e3f515f99d 137
kaizen 0:d0e3f515f99d 138 printf("WiFi example\n\n");
kaizen 0:d0e3f515f99d 139 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
kaizen 0:d0e3f515f99d 140 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
kaizen 0:d0e3f515f99d 141 if (ret != 0) {
kaizen 0:d0e3f515f99d 142 printf("\nConnection error\n");
kaizen 0:d0e3f515f99d 143 return -1;
kaizen 0:d0e3f515f99d 144 }
kaizen 0:d0e3f515f99d 145
kaizen 0:d0e3f515f99d 146 printf("Success\n\n");
kaizen 0:d0e3f515f99d 147 printf("MAC: %s\n", wifi.get_mac_address());
kaizen 0:d0e3f515f99d 148 printf("IP: %s\n", wifi.get_ip_address());
kaizen 0:d0e3f515f99d 149 printf("Netmask: %s\n", wifi.get_netmask());
kaizen 0:d0e3f515f99d 150 printf("Gateway: %s\n", wifi.get_gateway());
kaizen 0:d0e3f515f99d 151 printf("RSSI: %d\n\n", wifi.get_rssi());
kaizen 0:d0e3f515f99d 152
kaizen 0:d0e3f515f99d 153 test_udp_echo(&wifi);
kaizen 0:d0e3f515f99d 154 wifi.disconnect();
kaizen 0:d0e3f515f99d 155 printf("\nDone\n");
kaizen 0:d0e3f515f99d 156 }