wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1 /*
JMF 0:24d3eb812fd4 2 * Copyright (c) 2015 ARM Limited
JMF 0:24d3eb812fd4 3 *
JMF 0:24d3eb812fd4 4 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:24d3eb812fd4 5 * you may not use this file except in compliance with the License.
JMF 0:24d3eb812fd4 6 * You may obtain a copy of the License at
JMF 0:24d3eb812fd4 7 *
JMF 0:24d3eb812fd4 8 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:24d3eb812fd4 9 *
JMF 0:24d3eb812fd4 10 * Unless required by applicable law or agreed to in writing, software
JMF 0:24d3eb812fd4 11 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:24d3eb812fd4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:24d3eb812fd4 13 * See the License for the specific language governing permissions and
JMF 0:24d3eb812fd4 14 * limitations under the License.
JMF 0:24d3eb812fd4 15 */
JMF 0:24d3eb812fd4 16
JMF 0:24d3eb812fd4 17 /**
JMF 0:24d3eb812fd4 18 ******************************************************************************
JMF 0:24d3eb812fd4 19 * @file WizFi310.h
JMF 0:24d3eb812fd4 20 * @author Gateway Team
JMF 0:24d3eb812fd4 21 * @brief Header file of the WizFi310 WiFi Device
JMF 0:24d3eb812fd4 22 ******************************************************************************
JMF 0:24d3eb812fd4 23 * @attention
JMF 0:24d3eb812fd4 24 *
JMF 0:24d3eb812fd4 25 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
JMF 0:24d3eb812fd4 26 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
JMF 0:24d3eb812fd4 27 * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY
JMF 0:24d3eb812fd4 28 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
JMF 0:24d3eb812fd4 29 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
JMF 0:24d3eb812fd4 30 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
JMF 0:24d3eb812fd4 31 *
JMF 0:24d3eb812fd4 32 * <h2><center>&copy; COPYRIGHT 2017 WIZnet Co.,Ltd.</center></h2>
JMF 0:24d3eb812fd4 33 ******************************************************************************
JMF 0:24d3eb812fd4 34 */
JMF 0:24d3eb812fd4 35
JMF 0:24d3eb812fd4 36 #ifndef WIZFI310_H_
JMF 0:24d3eb812fd4 37 #define WIZFI310_H_
JMF 0:24d3eb812fd4 38
JMF 0:24d3eb812fd4 39 #include "ATCmdParser.h"
JMF 0:24d3eb812fd4 40 #include "netsocket/WiFiAccessPoint.h"
JMF 0:24d3eb812fd4 41 #include "UARTSerial.h"
JMF 0:24d3eb812fd4 42
JMF 0:24d3eb812fd4 43 /** WizFi310Interface class.
JMF 0:24d3eb812fd4 44 This is an interface to a WizFi310Interface radio.
JMF 0:24d3eb812fd4 45 */
JMF 0:24d3eb812fd4 46 class WizFi310
JMF 0:24d3eb812fd4 47 {
JMF 0:24d3eb812fd4 48 public:
JMF 0:24d3eb812fd4 49 WizFi310(PinName tx, PinName rx, bool debug=false);
JMF 0:24d3eb812fd4 50
JMF 0:24d3eb812fd4 51 /**
JMF 0:24d3eb812fd4 52 * Check firmware version of WizFi310
JMF 0:24d3eb812fd4 53 *
JMF 0:24d3eb812fd4 54 * @return character array firmware version or 0 if firmware query command gives outdated response
JMF 0:24d3eb812fd4 55 */
JMF 0:24d3eb812fd4 56 const char* get_firmware_version(void);
JMF 0:24d3eb812fd4 57
JMF 0:24d3eb812fd4 58 /**
JMF 0:24d3eb812fd4 59 * Startup the WizFi310
JMF 0:24d3eb812fd4 60 *
JMF 0:24d3eb812fd4 61 * @param mode mode of WIFI 0-client, 1-host
JMF 0:24d3eb812fd4 62 * @return true only if WizFi310 was setup correctly
JMF 0:24d3eb812fd4 63 */
JMF 0:24d3eb812fd4 64 bool startup(int mode);
JMF 0:24d3eb812fd4 65
JMF 0:24d3eb812fd4 66 /**
JMF 0:24d3eb812fd4 67 * Reset WizFi310
JMF 0:24d3eb812fd4 68 *
JMF 0:24d3eb812fd4 69 * @return true only if WizFi310 resets successfully
JMF 0:24d3eb812fd4 70 */
JMF 0:24d3eb812fd4 71 bool reset(void);
JMF 0:24d3eb812fd4 72
JMF 0:24d3eb812fd4 73 /**
JMF 0:24d3eb812fd4 74 * Enable/Disable DHCP
JMF 0:24d3eb812fd4 75 *
JMF 0:24d3eb812fd4 76 * @param enabled DHCP enabled when true
JMF 0:24d3eb812fd4 77 * @return true only if WizFi310 enables/disables DHCP successfully
JMF 0:24d3eb812fd4 78 */
JMF 0:24d3eb812fd4 79 bool dhcp(bool enabled);
JMF 0:24d3eb812fd4 80
JMF 0:24d3eb812fd4 81 /**
JMF 0:24d3eb812fd4 82 * Connect WizFi310 to AP
JMF 0:24d3eb812fd4 83 *
JMF 0:24d3eb812fd4 84 * @param ap the name of the AP
JMF 0:24d3eb812fd4 85 * @param passPhrase the password of AP
JMF 0:24d3eb812fd4 86 * @param security type of AP
JMF 0:24d3eb812fd4 87 * @return true only if WizFi310 is connected successfully
JMF 0:24d3eb812fd4 88 */
JMF 0:24d3eb812fd4 89 bool connect(const char *ap, const char *passPhrase, const char *sec);
JMF 0:24d3eb812fd4 90
JMF 0:24d3eb812fd4 91 /**
JMF 0:24d3eb812fd4 92 * Disconnect WizFi310 from AP
JMF 0:24d3eb812fd4 93 *
JMF 0:24d3eb812fd4 94 * @return true only if WizFi310 is disconnected successfully
JMF 0:24d3eb812fd4 95 */
JMF 0:24d3eb812fd4 96 bool disconnect(void);
JMF 0:24d3eb812fd4 97
JMF 0:24d3eb812fd4 98 /**
JMF 0:24d3eb812fd4 99 * Get the IP address of WizFi310
JMF 0:24d3eb812fd4 100 *
JMF 0:24d3eb812fd4 101 * @return null-teriminated IP address or null if no IP address is assigned
JMF 0:24d3eb812fd4 102 */
JMF 0:24d3eb812fd4 103 const char *getIPAddress(void);
JMF 0:24d3eb812fd4 104
JMF 0:24d3eb812fd4 105 /**
JMF 0:24d3eb812fd4 106 * Get the MAC address of WizFi310
JMF 0:24d3eb812fd4 107 *
JMF 0:24d3eb812fd4 108 * @return null-terminated MAC address or null if no MAC address is assigned
JMF 0:24d3eb812fd4 109 */
JMF 0:24d3eb812fd4 110 const char *getMACAddress(void);
JMF 0:24d3eb812fd4 111
JMF 0:24d3eb812fd4 112 /** Get the local gateway
JMF 0:24d3eb812fd4 113 *
JMF 0:24d3eb812fd4 114 * @return Null-terminated representation of the local gateway
JMF 0:24d3eb812fd4 115 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 116 */
JMF 0:24d3eb812fd4 117 const char *getGateway();
JMF 0:24d3eb812fd4 118
JMF 0:24d3eb812fd4 119 /** Get the local network mask
JMF 0:24d3eb812fd4 120 *
JMF 0:24d3eb812fd4 121 * @return Null-terminated representation of the local network mask
JMF 0:24d3eb812fd4 122 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 123 */
JMF 0:24d3eb812fd4 124 const char *getNetmask();
JMF 0:24d3eb812fd4 125
JMF 0:24d3eb812fd4 126 /* Return RSSI for active connection
JMF 0:24d3eb812fd4 127 *
JMF 0:24d3eb812fd4 128 * @return Measured RSSI
JMF 0:24d3eb812fd4 129 */
JMF 0:24d3eb812fd4 130 int8_t getRSSI();
JMF 0:24d3eb812fd4 131
JMF 0:24d3eb812fd4 132 /**
JMF 0:24d3eb812fd4 133 * Check if WizFi310 is conenected
JMF 0:24d3eb812fd4 134 *
JMF 0:24d3eb812fd4 135 * @return true only if the chip has an IP address
JMF 0:24d3eb812fd4 136 */
JMF 0:24d3eb812fd4 137 bool isConnected(void);
JMF 0:24d3eb812fd4 138
JMF 0:24d3eb812fd4 139 /** Scan for available networks
JMF 0:24d3eb812fd4 140 *
JMF 0:24d3eb812fd4 141 * @param ap Pointer to allocated array to store discovered AP
JMF 0:24d3eb812fd4 142 * @param limit Size of allocated @a res array, or 0 to only count available AP
JMF 0:24d3eb812fd4 143 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
JMF 0:24d3eb812fd4 144 * see @a nsapi_error
JMF 0:24d3eb812fd4 145 */
JMF 0:24d3eb812fd4 146 int scan(WiFiAccessPoint *res, unsigned limit);
JMF 0:24d3eb812fd4 147
JMF 0:24d3eb812fd4 148 /**Perform a dns query
JMF 0:24d3eb812fd4 149 *
JMF 0:24d3eb812fd4 150 * @param name Hostname to resolve
JMF 0:24d3eb812fd4 151 * @param ip Buffer to store IP address
JMF 0:24d3eb812fd4 152 * @return 0 true on success, false on failure
JMF 0:24d3eb812fd4 153 */
JMF 0:24d3eb812fd4 154 bool dns_lookup(const char *name, char *ip);
JMF 0:24d3eb812fd4 155
JMF 0:24d3eb812fd4 156 /**
JMF 0:24d3eb812fd4 157 * Open a socketed connection
JMF 0:24d3eb812fd4 158 *
JMF 0:24d3eb812fd4 159 * @param type the type of socket to open "UDP" or "TCP"
JMF 0:24d3eb812fd4 160 * @param id id to give the new socket, valid 0-4
JMF 0:24d3eb812fd4 161 * @param port port to open connection with
JMF 0:24d3eb812fd4 162 * @param addr the IP address of the destination
JMF 0:24d3eb812fd4 163 * @return true only if socket opened successfully
JMF 0:24d3eb812fd4 164 */
JMF 0:24d3eb812fd4 165 bool open(const char *type, int id, const char* addr, int port);
JMF 0:24d3eb812fd4 166
JMF 0:24d3eb812fd4 167 /**
JMF 0:24d3eb812fd4 168 * Sends data to an open socket
JMF 0:24d3eb812fd4 169 *
JMF 0:24d3eb812fd4 170 * @param id id of socket to send to
JMF 0:24d3eb812fd4 171 * @param data data to be sent
JMF 0:24d3eb812fd4 172 * @param amount amount of data to be sent - max 1024
JMF 0:24d3eb812fd4 173 * @return true only if data sent successfully
JMF 0:24d3eb812fd4 174 */
JMF 0:24d3eb812fd4 175 bool send(int id, const void *data, uint32_t amount);
JMF 0:24d3eb812fd4 176
JMF 0:24d3eb812fd4 177 /**
JMF 0:24d3eb812fd4 178 * Receives data from an open socket
JMF 0:24d3eb812fd4 179 *
JMF 0:24d3eb812fd4 180 * @param id id to receive from
JMF 0:24d3eb812fd4 181 * @param data placeholder for returned information
JMF 0:24d3eb812fd4 182 * @param amount number of bytes to be received
JMF 0:24d3eb812fd4 183 * @return the number of bytes received
JMF 0:24d3eb812fd4 184 */
JMF 0:24d3eb812fd4 185 int32_t recv(int id, void *data, uint32_t amount);
JMF 0:24d3eb812fd4 186
JMF 0:24d3eb812fd4 187 /**
JMF 0:24d3eb812fd4 188 * Closes a socket
JMF 0:24d3eb812fd4 189 *
JMF 0:24d3eb812fd4 190 * @param id id of socket to close, valid only 0-4
JMF 0:24d3eb812fd4 191 * @return true only if socket is closed successfully
JMF 0:24d3eb812fd4 192 */
JMF 0:24d3eb812fd4 193 bool close(int id);
JMF 0:24d3eb812fd4 194
JMF 0:24d3eb812fd4 195 /**
JMF 0:24d3eb812fd4 196 * Allows timeout to be changed between commands
JMF 0:24d3eb812fd4 197 *
JMF 0:24d3eb812fd4 198 * @param timeout_ms timeout of the connection
JMF 0:24d3eb812fd4 199 */
JMF 0:24d3eb812fd4 200 void setTimeout(uint32_t timeout_ms);
JMF 0:24d3eb812fd4 201
JMF 0:24d3eb812fd4 202 /**
JMF 0:24d3eb812fd4 203 * Checks if data is available
JMF 0:24d3eb812fd4 204 */
JMF 0:24d3eb812fd4 205 bool readable();
JMF 0:24d3eb812fd4 206
JMF 0:24d3eb812fd4 207 /**
JMF 0:24d3eb812fd4 208 * Checks if data can be written
JMF 0:24d3eb812fd4 209 */
JMF 0:24d3eb812fd4 210 bool writeable();
JMF 0:24d3eb812fd4 211
JMF 0:24d3eb812fd4 212 /**
JMF 0:24d3eb812fd4 213 * Attach a function to call whenever network state has changed
JMF 0:24d3eb812fd4 214 *
JMF 0:24d3eb812fd4 215 * @param func A pointer to a void function, or 0 to set as none
JMF 0:24d3eb812fd4 216 */
JMF 0:24d3eb812fd4 217 void attach(mbed::Callback<void()> func);
JMF 0:24d3eb812fd4 218
JMF 0:24d3eb812fd4 219 /**
JMF 0:24d3eb812fd4 220 * Attach a function to call whenever network state has changed
JMF 0:24d3eb812fd4 221 *
JMF 0:24d3eb812fd4 222 * @param obj pointer to the object to call the member function on
JMF 0:24d3eb812fd4 223 * @param method pointer to the member function to call
JMF 0:24d3eb812fd4 224 */
JMF 0:24d3eb812fd4 225 template <typename T, typename M>
JMF 0:24d3eb812fd4 226 void attach(T *obj, M method) {
JMF 0:24d3eb812fd4 227 attach(mbed::Callback<void()>(obj, method));
JMF 0:24d3eb812fd4 228 }
JMF 0:24d3eb812fd4 229
JMF 0:24d3eb812fd4 230 private:
JMF 0:24d3eb812fd4 231 mbed::UARTSerial _serial;
JMF 0:24d3eb812fd4 232 mbed::ATCmdParser _parser;
JMF 0:24d3eb812fd4 233
JMF 0:24d3eb812fd4 234 struct packet {
JMF 0:24d3eb812fd4 235 struct packet *next;
JMF 0:24d3eb812fd4 236 int id;
JMF 0:24d3eb812fd4 237 uint32_t len;
JMF 0:24d3eb812fd4 238 // data follows
JMF 0:24d3eb812fd4 239 char *data;
JMF 0:24d3eb812fd4 240 } *_packets, **_packets_end;
JMF 0:24d3eb812fd4 241 void _packet_handler();
JMF 0:24d3eb812fd4 242 //void _socket_close_handler();
JMF 0:24d3eb812fd4 243 bool recv_ap(nsapi_wifi_ap_t *ap);
JMF 0:24d3eb812fd4 244 nsapi_security_t str2sec(const char *str_sec);
JMF 0:24d3eb812fd4 245 int hex_str_to_int(const char* hex_str);
JMF 0:24d3eb812fd4 246
JMF 0:24d3eb812fd4 247 char _ip_buffer[16];
JMF 0:24d3eb812fd4 248 char _gateway_buffer[16];
JMF 0:24d3eb812fd4 249 char _netmask_buffer[16];
JMF 0:24d3eb812fd4 250 char _mac_buffer[18];
JMF 0:24d3eb812fd4 251 char _firmware_version[8];
JMF 0:24d3eb812fd4 252
JMF 0:24d3eb812fd4 253 int _op_mode; // 0 : Station Mode, 1 : AP Mode
JMF 0:24d3eb812fd4 254 bool _dhcp;
JMF 0:24d3eb812fd4 255 uint32_t _timeout_ms;
JMF 0:24d3eb812fd4 256 };
JMF 0:24d3eb812fd4 257
JMF 0:24d3eb812fd4 258 #endif