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 /* ISM43362Interface Example
JMF 0:24d3eb812fd4 2 * Copyright (c) STMicroelectronics 2017
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 #ifndef ISM43362_H
JMF 0:24d3eb812fd4 18 #define ISM43362_H
JMF 0:24d3eb812fd4 19 #include "ATParser.h"
JMF 0:24d3eb812fd4 20
JMF 0:24d3eb812fd4 21 #define ES_WIFI_MAX_SSID_NAME_SIZE 32
JMF 0:24d3eb812fd4 22 #define ES_WIFI_MAX_PSWD_NAME_SIZE 32
JMF 0:24d3eb812fd4 23 #define ES_WIFI_PRODUCT_ID_SIZE 32
JMF 0:24d3eb812fd4 24 #define ES_WIFI_PRODUCT_NAME_SIZE 32
JMF 0:24d3eb812fd4 25 #define ES_WIFI_FW_REV_SIZE 16
JMF 0:24d3eb812fd4 26 #define ES_WIFI_API_REV_SIZE 16
JMF 0:24d3eb812fd4 27 #define ES_WIFI_STACK_REV_SIZE 16
JMF 0:24d3eb812fd4 28 #define ES_WIFI_RTOS_REV_SIZE 16
JMF 0:24d3eb812fd4 29
JMF 0:24d3eb812fd4 30 // The input range for AT Command 'R1' is 0 to 1200 bytes
JMF 0:24d3eb812fd4 31 // 'R1' Set Read Transport Packet Size (bytes)
JMF 0:24d3eb812fd4 32 #define ES_WIFI_MAX_RX_PACKET_SIZE 1200
JMF 0:24d3eb812fd4 33 // Module maxume DATA payload for Tx packet is 1460
JMF 0:24d3eb812fd4 34 #define ES_WIFI_MAX_TX_PACKET_SIZE 1460
JMF 0:24d3eb812fd4 35 typedef enum ism_security {
JMF 0:24d3eb812fd4 36 ISM_SECURITY_NONE = 0x0, /*!< open access point */
JMF 0:24d3eb812fd4 37 ISM_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */
JMF 0:24d3eb812fd4 38 ISM_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
JMF 0:24d3eb812fd4 39 ISM_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
JMF 0:24d3eb812fd4 40 ISM_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
JMF 0:24d3eb812fd4 41 ISM_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */
JMF 0:24d3eb812fd4 42 } ism_security_t;
JMF 0:24d3eb812fd4 43
JMF 0:24d3eb812fd4 44 extern "C" int32_t ParseNumber(char *ptr, uint8_t *cnt);
JMF 0:24d3eb812fd4 45
JMF 0:24d3eb812fd4 46 /** ISM43362Interface class.
JMF 0:24d3eb812fd4 47 This is an interface to a ISM43362 radio.
JMF 0:24d3eb812fd4 48 */
JMF 0:24d3eb812fd4 49 class ISM43362 {
JMF 0:24d3eb812fd4 50 public:
JMF 0:24d3eb812fd4 51 ISM43362(PinName mosi, PinName miso, PinName clk, PinName nss, PinName resetpin, PinName datareadypin, PinName wakeup, bool debug = false);
JMF 0:24d3eb812fd4 52
JMF 0:24d3eb812fd4 53 /**
JMF 0:24d3eb812fd4 54 * Check firmware version of ISM43362
JMF 0:24d3eb812fd4 55 *
JMF 0:24d3eb812fd4 56 * @return fw version or null if no version is read
JMF 0:24d3eb812fd4 57 */
JMF 0:24d3eb812fd4 58 uint32_t get_firmware_version(void);
JMF 0:24d3eb812fd4 59
JMF 0:24d3eb812fd4 60 /**
JMF 0:24d3eb812fd4 61 * Reset ISM43362
JMF 0:24d3eb812fd4 62 *
JMF 0:24d3eb812fd4 63 * @return true only if ISM43362 resets successfully
JMF 0:24d3eb812fd4 64 */
JMF 0:24d3eb812fd4 65 bool reset(void);
JMF 0:24d3eb812fd4 66
JMF 0:24d3eb812fd4 67 /**
JMF 0:24d3eb812fd4 68 * Enable/Disable DHCP
JMF 0:24d3eb812fd4 69 *
JMF 0:24d3eb812fd4 70 * @param enabled DHCP enabled when true
JMF 0:24d3eb812fd4 71 * @return true only if ISM43362 enables/disables DHCP successfully
JMF 0:24d3eb812fd4 72 */
JMF 0:24d3eb812fd4 73 bool dhcp(bool enabled);
JMF 0:24d3eb812fd4 74
JMF 0:24d3eb812fd4 75 /**
JMF 0:24d3eb812fd4 76 * Connect ISM43362 to AP
JMF 0:24d3eb812fd4 77 *
JMF 0:24d3eb812fd4 78 * @param ap the name of the AP
JMF 0:24d3eb812fd4 79 * @param passPhrase the password of AP
JMF 0:24d3eb812fd4 80 * @param ap_sec the security level of network AP
JMF 0:24d3eb812fd4 81 * @return nsapi_error enum
JMF 0:24d3eb812fd4 82 */
JMF 0:24d3eb812fd4 83 int connect(const char *ap, const char *passPhrase, ism_security_t ap_sec);
JMF 0:24d3eb812fd4 84
JMF 0:24d3eb812fd4 85 /**
JMF 0:24d3eb812fd4 86 * Disconnect ISM43362 from AP
JMF 0:24d3eb812fd4 87 *
JMF 0:24d3eb812fd4 88 * @return true only if ISM43362 is disconnected successfully
JMF 0:24d3eb812fd4 89 */
JMF 0:24d3eb812fd4 90 bool disconnect(void);
JMF 0:24d3eb812fd4 91
JMF 0:24d3eb812fd4 92 /**
JMF 0:24d3eb812fd4 93 * Get the IP address of ISM43362
JMF 0:24d3eb812fd4 94 *
JMF 0:24d3eb812fd4 95 * @return null-teriminated IP address or null if no IP address is assigned
JMF 0:24d3eb812fd4 96 */
JMF 0:24d3eb812fd4 97 const char *getIPAddress(void);
JMF 0:24d3eb812fd4 98
JMF 0:24d3eb812fd4 99 /**
JMF 0:24d3eb812fd4 100 * Get the MAC address of ISM43362
JMF 0:24d3eb812fd4 101 *
JMF 0:24d3eb812fd4 102 * @return null-terminated MAC address or null if no MAC address is assigned
JMF 0:24d3eb812fd4 103 */
JMF 0:24d3eb812fd4 104 const char *getMACAddress(void);
JMF 0:24d3eb812fd4 105
JMF 0:24d3eb812fd4 106 /** Get the local gateway
JMF 0:24d3eb812fd4 107 *
JMF 0:24d3eb812fd4 108 * @return Null-terminated representation of the local gateway
JMF 0:24d3eb812fd4 109 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 110 */
JMF 0:24d3eb812fd4 111 const char *getGateway();
JMF 0:24d3eb812fd4 112
JMF 0:24d3eb812fd4 113 /** Get the local network mask
JMF 0:24d3eb812fd4 114 *
JMF 0:24d3eb812fd4 115 * @return Null-terminated representation of the local network mask
JMF 0:24d3eb812fd4 116 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 117 */
JMF 0:24d3eb812fd4 118 const char *getNetmask();
JMF 0:24d3eb812fd4 119
JMF 0:24d3eb812fd4 120 /* Return RSSI for active connection
JMF 0:24d3eb812fd4 121 *
JMF 0:24d3eb812fd4 122 * @return Measured RSSI
JMF 0:24d3eb812fd4 123 */
JMF 0:24d3eb812fd4 124 int8_t getRSSI();
JMF 0:24d3eb812fd4 125
JMF 0:24d3eb812fd4 126 /**
JMF 0:24d3eb812fd4 127 * Check if ISM43362 is conenected
JMF 0:24d3eb812fd4 128 *
JMF 0:24d3eb812fd4 129 * @return true only if the chip has an IP address
JMF 0:24d3eb812fd4 130 */
JMF 0:24d3eb812fd4 131 bool isConnected(void);
JMF 0:24d3eb812fd4 132
JMF 0:24d3eb812fd4 133 /** Scan for available networks
JMF 0:24d3eb812fd4 134 *
JMF 0:24d3eb812fd4 135 * @param ap Pointer to allocated array to store discovered AP
JMF 0:24d3eb812fd4 136 * @param limit Size of allocated @a res array, or 0 to only count available AP
JMF 0:24d3eb812fd4 137 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
JMF 0:24d3eb812fd4 138 * see @a nsapi_error
JMF 0:24d3eb812fd4 139 */
JMF 0:24d3eb812fd4 140 int scan(WiFiAccessPoint *res, unsigned limit);
JMF 0:24d3eb812fd4 141
JMF 0:24d3eb812fd4 142 /**Perform a dns query
JMF 0:24d3eb812fd4 143 *
JMF 0:24d3eb812fd4 144 * @param name Hostname to resolve
JMF 0:24d3eb812fd4 145 * @param ip Buffer to store IP address
JMF 0:24d3eb812fd4 146 * @return 0 true on success, false on failure
JMF 0:24d3eb812fd4 147 */
JMF 0:24d3eb812fd4 148 bool dns_lookup(const char *name, char *ip);
JMF 0:24d3eb812fd4 149
JMF 0:24d3eb812fd4 150 /**
JMF 0:24d3eb812fd4 151 * Open a socketed connection
JMF 0:24d3eb812fd4 152 *
JMF 0:24d3eb812fd4 153 * @param type the type of socket to open "UDP" or "TCP"
JMF 0:24d3eb812fd4 154 * @param id id to give the new socket, valid 0-4
JMF 0:24d3eb812fd4 155 * @param port port to open connection with
JMF 0:24d3eb812fd4 156 * @param addr the IP address of the destination
JMF 0:24d3eb812fd4 157 * @return true only if socket opened successfully
JMF 0:24d3eb812fd4 158 */
JMF 0:24d3eb812fd4 159 bool open(const char *type, int id, const char *addr, int port);
JMF 0:24d3eb812fd4 160
JMF 0:24d3eb812fd4 161 /**
JMF 0:24d3eb812fd4 162 * Sends data to an open socket
JMF 0:24d3eb812fd4 163 *
JMF 0:24d3eb812fd4 164 * @param id id of socket to send to
JMF 0:24d3eb812fd4 165 * @param data data to be sent
JMF 0:24d3eb812fd4 166 * @param amount amount of data to be sent - max 1024
JMF 0:24d3eb812fd4 167 * @return true only if data sent successfully
JMF 0:24d3eb812fd4 168 */
JMF 0:24d3eb812fd4 169 bool send(int id, const void *data, uint32_t amount);
JMF 0:24d3eb812fd4 170
JMF 0:24d3eb812fd4 171 /**
JMF 0:24d3eb812fd4 172 * Receives data from an open socket
JMF 0:24d3eb812fd4 173 *
JMF 0:24d3eb812fd4 174 * @param id id to receive from
JMF 0:24d3eb812fd4 175 * @param data placeholder for returned information
JMF 0:24d3eb812fd4 176 * @param amount number of bytes to be received
JMF 0:24d3eb812fd4 177 * @return the number of bytes received
JMF 0:24d3eb812fd4 178 */
JMF 0:24d3eb812fd4 179 int32_t recv(int id, void *data, uint32_t amount);
JMF 0:24d3eb812fd4 180
JMF 0:24d3eb812fd4 181 /**
JMF 0:24d3eb812fd4 182 * Closes a socket
JMF 0:24d3eb812fd4 183 *
JMF 0:24d3eb812fd4 184 * @param id id of socket to close, valid only 0-4
JMF 0:24d3eb812fd4 185 * @return true only if socket is closed successfully
JMF 0:24d3eb812fd4 186 */
JMF 0:24d3eb812fd4 187 bool close(int id);
JMF 0:24d3eb812fd4 188
JMF 0:24d3eb812fd4 189 /**
JMF 0:24d3eb812fd4 190 * Checks if data is available
JMF 0:24d3eb812fd4 191 */
JMF 0:24d3eb812fd4 192 bool readable();
JMF 0:24d3eb812fd4 193
JMF 0:24d3eb812fd4 194 /**
JMF 0:24d3eb812fd4 195 * Checks if data can be written
JMF 0:24d3eb812fd4 196 */
JMF 0:24d3eb812fd4 197 bool writeable();
JMF 0:24d3eb812fd4 198
JMF 0:24d3eb812fd4 199 /**
JMF 0:24d3eb812fd4 200 * Attach a function to call whenever network state has changed
JMF 0:24d3eb812fd4 201 *
JMF 0:24d3eb812fd4 202 * @param func A pointer to a void function, or 0 to set as none
JMF 0:24d3eb812fd4 203 */
JMF 0:24d3eb812fd4 204 void attach(Callback<void()> func);
JMF 0:24d3eb812fd4 205
JMF 0:24d3eb812fd4 206 /**
JMF 0:24d3eb812fd4 207 * Check is datas are available to read for a socket
JMF 0:24d3eb812fd4 208 * @param id socket id
JMF 0:24d3eb812fd4 209 * @param data placeholder for returned information
JMF 0:24d3eb812fd4 210 * @param amount size to read for the check
JMF 0:24d3eb812fd4 211 * @return amount of read value, or -1 for errors
JMF 0:24d3eb812fd4 212 */
JMF 0:24d3eb812fd4 213 int check_recv_status(int id, void *data);
JMF 0:24d3eb812fd4 214
JMF 0:24d3eb812fd4 215 /**
JMF 0:24d3eb812fd4 216 * Attach a function to call whenever network state has changed
JMF 0:24d3eb812fd4 217 *
JMF 0:24d3eb812fd4 218 * @param obj pointer to the object to call the member function on
JMF 0:24d3eb812fd4 219 * @param method pointer to the member function to call
JMF 0:24d3eb812fd4 220 */
JMF 0:24d3eb812fd4 221 template <typename T, typename M>
JMF 0:24d3eb812fd4 222 void attach(T *obj, M method)
JMF 0:24d3eb812fd4 223 {
JMF 0:24d3eb812fd4 224 attach(Callback<void()>(obj, method));
JMF 0:24d3eb812fd4 225 }
JMF 0:24d3eb812fd4 226
JMF 0:24d3eb812fd4 227 private:
JMF 0:24d3eb812fd4 228 BufferedSpi _bufferspi;
JMF 0:24d3eb812fd4 229 ATParser _parser;
JMF 0:24d3eb812fd4 230 DigitalOut _resetpin;
JMF 0:24d3eb812fd4 231 volatile int _active_id;
JMF 0:24d3eb812fd4 232 void print_rx_buff(void);
JMF 0:24d3eb812fd4 233 bool check_response(void);
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 } *_packets, * *_packets_end;
JMF 0:24d3eb812fd4 240 void _packet_handler();
JMF 0:24d3eb812fd4 241 bool _ism_debug;
JMF 0:24d3eb812fd4 242
JMF 0:24d3eb812fd4 243 char _ip_buffer[16];
JMF 0:24d3eb812fd4 244 char _gateway_buffer[16];
JMF 0:24d3eb812fd4 245 char _netmask_buffer[16];
JMF 0:24d3eb812fd4 246 char _mac_buffer[18];
JMF 0:24d3eb812fd4 247 uint32_t _FwVersionId;
JMF 0:24d3eb812fd4 248 };
JMF 0:24d3eb812fd4 249
JMF 0:24d3eb812fd4 250 #endif