Toyomasa Watarai
/
Mbed-example-WS-W27
Mbed Cloud example program for workshop in W27 2018.
easy-connect/wifi-ism43362/ISM43362/ISM43362.h@0:119624335925, 2018-06-30 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Jun 30 01:40:30 2018 +0000
- Revision:
- 0:119624335925
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:119624335925 | 1 | /* ISM43362Interface Example |
MACRUM | 0:119624335925 | 2 | * Copyright (c) STMicroelectronics 2017 |
MACRUM | 0:119624335925 | 3 | * |
MACRUM | 0:119624335925 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 0:119624335925 | 5 | * you may not use this file except in compliance with the License. |
MACRUM | 0:119624335925 | 6 | * You may obtain a copy of the License at |
MACRUM | 0:119624335925 | 7 | * |
MACRUM | 0:119624335925 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:119624335925 | 9 | * |
MACRUM | 0:119624335925 | 10 | * Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:119624335925 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 0:119624335925 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:119624335925 | 13 | * See the License for the specific language governing permissions and |
MACRUM | 0:119624335925 | 14 | * limitations under the License. |
MACRUM | 0:119624335925 | 15 | */ |
MACRUM | 0:119624335925 | 16 | |
MACRUM | 0:119624335925 | 17 | #ifndef ISM43362_H |
MACRUM | 0:119624335925 | 18 | #define ISM43362_H |
MACRUM | 0:119624335925 | 19 | #include "ATParser.h" |
MACRUM | 0:119624335925 | 20 | |
MACRUM | 0:119624335925 | 21 | #define ES_WIFI_MAX_SSID_NAME_SIZE 32 |
MACRUM | 0:119624335925 | 22 | #define ES_WIFI_MAX_PSWD_NAME_SIZE 32 |
MACRUM | 0:119624335925 | 23 | #define ES_WIFI_PRODUCT_ID_SIZE 32 |
MACRUM | 0:119624335925 | 24 | #define ES_WIFI_PRODUCT_NAME_SIZE 32 |
MACRUM | 0:119624335925 | 25 | #define ES_WIFI_FW_REV_SIZE 16 |
MACRUM | 0:119624335925 | 26 | #define ES_WIFI_API_REV_SIZE 16 |
MACRUM | 0:119624335925 | 27 | #define ES_WIFI_STACK_REV_SIZE 16 |
MACRUM | 0:119624335925 | 28 | #define ES_WIFI_RTOS_REV_SIZE 16 |
MACRUM | 0:119624335925 | 29 | |
MACRUM | 0:119624335925 | 30 | // The input range for AT Command 'R1' is 0 to 1200 bytes |
MACRUM | 0:119624335925 | 31 | // 'R1' Set Read Transport Packet Size (bytes) |
MACRUM | 0:119624335925 | 32 | #define ES_WIFI_MAX_RX_PACKET_SIZE 1200 |
MACRUM | 0:119624335925 | 33 | // Module maxume DATA payload for Tx packet is 1460 |
MACRUM | 0:119624335925 | 34 | #define ES_WIFI_MAX_TX_PACKET_SIZE 1460 |
MACRUM | 0:119624335925 | 35 | typedef enum ism_security { |
MACRUM | 0:119624335925 | 36 | ISM_SECURITY_NONE = 0x0, /*!< open access point */ |
MACRUM | 0:119624335925 | 37 | ISM_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */ |
MACRUM | 0:119624335925 | 38 | ISM_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */ |
MACRUM | 0:119624335925 | 39 | ISM_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */ |
MACRUM | 0:119624335925 | 40 | ISM_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */ |
MACRUM | 0:119624335925 | 41 | ISM_SECURITY_UNKNOWN = 0xFF, /*!< unknown/unsupported security in scan results */ |
MACRUM | 0:119624335925 | 42 | } ism_security_t; |
MACRUM | 0:119624335925 | 43 | |
MACRUM | 0:119624335925 | 44 | /** ISM43362Interface class. |
MACRUM | 0:119624335925 | 45 | This is an interface to a ISM43362 radio. |
MACRUM | 0:119624335925 | 46 | */ |
MACRUM | 0:119624335925 | 47 | class ISM43362 |
MACRUM | 0:119624335925 | 48 | { |
MACRUM | 0:119624335925 | 49 | public: |
MACRUM | 0:119624335925 | 50 | ISM43362(PinName mosi, PinName miso, PinName clk, PinName nss, PinName resetpin, PinName datareadypin, PinName wakeup, bool debug=false); |
MACRUM | 0:119624335925 | 51 | |
MACRUM | 0:119624335925 | 52 | /** |
MACRUM | 0:119624335925 | 53 | * Check firmware version of ISM43362 |
MACRUM | 0:119624335925 | 54 | * |
MACRUM | 0:119624335925 | 55 | * @return null-terminated fw version or null if no version is read |
MACRUM | 0:119624335925 | 56 | */ |
MACRUM | 0:119624335925 | 57 | const char *get_firmware_version(void); |
MACRUM | 0:119624335925 | 58 | |
MACRUM | 0:119624335925 | 59 | /** |
MACRUM | 0:119624335925 | 60 | * Reset ISM43362 |
MACRUM | 0:119624335925 | 61 | * |
MACRUM | 0:119624335925 | 62 | * @return true only if ISM43362 resets successfully |
MACRUM | 0:119624335925 | 63 | */ |
MACRUM | 0:119624335925 | 64 | bool reset(void); |
MACRUM | 0:119624335925 | 65 | |
MACRUM | 0:119624335925 | 66 | /** |
MACRUM | 0:119624335925 | 67 | * Enable/Disable DHCP |
MACRUM | 0:119624335925 | 68 | * |
MACRUM | 0:119624335925 | 69 | * @param enabled DHCP enabled when true |
MACRUM | 0:119624335925 | 70 | * @return true only if ISM43362 enables/disables DHCP successfully |
MACRUM | 0:119624335925 | 71 | */ |
MACRUM | 0:119624335925 | 72 | bool dhcp(bool enabled); |
MACRUM | 0:119624335925 | 73 | |
MACRUM | 0:119624335925 | 74 | /** |
MACRUM | 0:119624335925 | 75 | * Connect ISM43362 to AP |
MACRUM | 0:119624335925 | 76 | * |
MACRUM | 0:119624335925 | 77 | * @param ap the name of the AP |
MACRUM | 0:119624335925 | 78 | * @param passPhrase the password of AP |
MACRUM | 0:119624335925 | 79 | * @param ap_sec the security level of network AP |
MACRUM | 0:119624335925 | 80 | * @return nsapi_error enum |
MACRUM | 0:119624335925 | 81 | */ |
MACRUM | 0:119624335925 | 82 | int connect(const char *ap, const char *passPhrase, ism_security_t ap_sec); |
MACRUM | 0:119624335925 | 83 | |
MACRUM | 0:119624335925 | 84 | /** |
MACRUM | 0:119624335925 | 85 | * Disconnect ISM43362 from AP |
MACRUM | 0:119624335925 | 86 | * |
MACRUM | 0:119624335925 | 87 | * @return true only if ISM43362 is disconnected successfully |
MACRUM | 0:119624335925 | 88 | */ |
MACRUM | 0:119624335925 | 89 | bool disconnect(void); |
MACRUM | 0:119624335925 | 90 | |
MACRUM | 0:119624335925 | 91 | /** |
MACRUM | 0:119624335925 | 92 | * Get the IP address of ISM43362 |
MACRUM | 0:119624335925 | 93 | * |
MACRUM | 0:119624335925 | 94 | * @return null-teriminated IP address or null if no IP address is assigned |
MACRUM | 0:119624335925 | 95 | */ |
MACRUM | 0:119624335925 | 96 | const char *getIPAddress(void); |
MACRUM | 0:119624335925 | 97 | |
MACRUM | 0:119624335925 | 98 | /** |
MACRUM | 0:119624335925 | 99 | * Get the MAC address of ISM43362 |
MACRUM | 0:119624335925 | 100 | * |
MACRUM | 0:119624335925 | 101 | * @return null-terminated MAC address or null if no MAC address is assigned |
MACRUM | 0:119624335925 | 102 | */ |
MACRUM | 0:119624335925 | 103 | const char *getMACAddress(void); |
MACRUM | 0:119624335925 | 104 | |
MACRUM | 0:119624335925 | 105 | /** Get the local gateway |
MACRUM | 0:119624335925 | 106 | * |
MACRUM | 0:119624335925 | 107 | * @return Null-terminated representation of the local gateway |
MACRUM | 0:119624335925 | 108 | * or null if no network mask has been recieved |
MACRUM | 0:119624335925 | 109 | */ |
MACRUM | 0:119624335925 | 110 | const char *getGateway(); |
MACRUM | 0:119624335925 | 111 | |
MACRUM | 0:119624335925 | 112 | /** Get the local network mask |
MACRUM | 0:119624335925 | 113 | * |
MACRUM | 0:119624335925 | 114 | * @return Null-terminated representation of the local network mask |
MACRUM | 0:119624335925 | 115 | * or null if no network mask has been recieved |
MACRUM | 0:119624335925 | 116 | */ |
MACRUM | 0:119624335925 | 117 | const char *getNetmask(); |
MACRUM | 0:119624335925 | 118 | |
MACRUM | 0:119624335925 | 119 | /* Return RSSI for active connection |
MACRUM | 0:119624335925 | 120 | * |
MACRUM | 0:119624335925 | 121 | * @return Measured RSSI |
MACRUM | 0:119624335925 | 122 | */ |
MACRUM | 0:119624335925 | 123 | int8_t getRSSI(); |
MACRUM | 0:119624335925 | 124 | |
MACRUM | 0:119624335925 | 125 | /** |
MACRUM | 0:119624335925 | 126 | * Check if ISM43362 is conenected |
MACRUM | 0:119624335925 | 127 | * |
MACRUM | 0:119624335925 | 128 | * @return true only if the chip has an IP address |
MACRUM | 0:119624335925 | 129 | */ |
MACRUM | 0:119624335925 | 130 | bool isConnected(void); |
MACRUM | 0:119624335925 | 131 | |
MACRUM | 0:119624335925 | 132 | /** Scan for available networks |
MACRUM | 0:119624335925 | 133 | * |
MACRUM | 0:119624335925 | 134 | * @param ap Pointer to allocated array to store discovered AP |
MACRUM | 0:119624335925 | 135 | * @param limit Size of allocated @a res array, or 0 to only count available AP |
MACRUM | 0:119624335925 | 136 | * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error |
MACRUM | 0:119624335925 | 137 | * see @a nsapi_error |
MACRUM | 0:119624335925 | 138 | */ |
MACRUM | 0:119624335925 | 139 | int scan(WiFiAccessPoint *res, unsigned limit); |
MACRUM | 0:119624335925 | 140 | |
MACRUM | 0:119624335925 | 141 | /**Perform a dns query |
MACRUM | 0:119624335925 | 142 | * |
MACRUM | 0:119624335925 | 143 | * @param name Hostname to resolve |
MACRUM | 0:119624335925 | 144 | * @param ip Buffer to store IP address |
MACRUM | 0:119624335925 | 145 | * @return 0 true on success, false on failure |
MACRUM | 0:119624335925 | 146 | */ |
MACRUM | 0:119624335925 | 147 | bool dns_lookup(const char *name, char *ip); |
MACRUM | 0:119624335925 | 148 | |
MACRUM | 0:119624335925 | 149 | /** |
MACRUM | 0:119624335925 | 150 | * Open a socketed connection |
MACRUM | 0:119624335925 | 151 | * |
MACRUM | 0:119624335925 | 152 | * @param type the type of socket to open "UDP" or "TCP" |
MACRUM | 0:119624335925 | 153 | * @param id id to give the new socket, valid 0-4 |
MACRUM | 0:119624335925 | 154 | * @param port port to open connection with |
MACRUM | 0:119624335925 | 155 | * @param addr the IP address of the destination |
MACRUM | 0:119624335925 | 156 | * @return true only if socket opened successfully |
MACRUM | 0:119624335925 | 157 | */ |
MACRUM | 0:119624335925 | 158 | bool open(const char *type, int id, const char* addr, int port); |
MACRUM | 0:119624335925 | 159 | |
MACRUM | 0:119624335925 | 160 | /** |
MACRUM | 0:119624335925 | 161 | * Sends data to an open socket |
MACRUM | 0:119624335925 | 162 | * |
MACRUM | 0:119624335925 | 163 | * @param id id of socket to send to |
MACRUM | 0:119624335925 | 164 | * @param data data to be sent |
MACRUM | 0:119624335925 | 165 | * @param amount amount of data to be sent - max 1024 |
MACRUM | 0:119624335925 | 166 | * @return true only if data sent successfully |
MACRUM | 0:119624335925 | 167 | */ |
MACRUM | 0:119624335925 | 168 | bool send(int id, const void *data, uint32_t amount); |
MACRUM | 0:119624335925 | 169 | |
MACRUM | 0:119624335925 | 170 | /** |
MACRUM | 0:119624335925 | 171 | * Receives data from an open socket |
MACRUM | 0:119624335925 | 172 | * |
MACRUM | 0:119624335925 | 173 | * @param id id to receive from |
MACRUM | 0:119624335925 | 174 | * @param data placeholder for returned information |
MACRUM | 0:119624335925 | 175 | * @param amount number of bytes to be received |
MACRUM | 0:119624335925 | 176 | * @return the number of bytes received |
MACRUM | 0:119624335925 | 177 | */ |
MACRUM | 0:119624335925 | 178 | int32_t recv(int id, void *data, uint32_t amount); |
MACRUM | 0:119624335925 | 179 | |
MACRUM | 0:119624335925 | 180 | /** |
MACRUM | 0:119624335925 | 181 | * Closes a socket |
MACRUM | 0:119624335925 | 182 | * |
MACRUM | 0:119624335925 | 183 | * @param id id of socket to close, valid only 0-4 |
MACRUM | 0:119624335925 | 184 | * @return true only if socket is closed successfully |
MACRUM | 0:119624335925 | 185 | */ |
MACRUM | 0:119624335925 | 186 | bool close(int id); |
MACRUM | 0:119624335925 | 187 | |
MACRUM | 0:119624335925 | 188 | /** |
MACRUM | 0:119624335925 | 189 | * Allows timeout to be changed between commands |
MACRUM | 0:119624335925 | 190 | * |
MACRUM | 0:119624335925 | 191 | * @param timeout_ms timeout of the connection |
MACRUM | 0:119624335925 | 192 | */ |
MACRUM | 0:119624335925 | 193 | void setTimeout(uint32_t timeout_ms); |
MACRUM | 0:119624335925 | 194 | |
MACRUM | 0:119624335925 | 195 | /** |
MACRUM | 0:119624335925 | 196 | * Checks if data is available |
MACRUM | 0:119624335925 | 197 | */ |
MACRUM | 0:119624335925 | 198 | bool readable(); |
MACRUM | 0:119624335925 | 199 | |
MACRUM | 0:119624335925 | 200 | /** |
MACRUM | 0:119624335925 | 201 | * Checks if data can be written |
MACRUM | 0:119624335925 | 202 | */ |
MACRUM | 0:119624335925 | 203 | bool writeable(); |
MACRUM | 0:119624335925 | 204 | |
MACRUM | 0:119624335925 | 205 | /** |
MACRUM | 0:119624335925 | 206 | * Attach a function to call whenever network state has changed |
MACRUM | 0:119624335925 | 207 | * |
MACRUM | 0:119624335925 | 208 | * @param func A pointer to a void function, or 0 to set as none |
MACRUM | 0:119624335925 | 209 | */ |
MACRUM | 0:119624335925 | 210 | void attach(Callback<void()> func); |
MACRUM | 0:119624335925 | 211 | |
MACRUM | 0:119624335925 | 212 | /** |
MACRUM | 0:119624335925 | 213 | * Check is datas are available to read for a socket |
MACRUM | 0:119624335925 | 214 | * @param id socket id |
MACRUM | 0:119624335925 | 215 | * @param data placeholder for returned information |
MACRUM | 0:119624335925 | 216 | * @param amount size to read for the check |
MACRUM | 0:119624335925 | 217 | * @return amount of read value, or -1 for errors |
MACRUM | 0:119624335925 | 218 | */ |
MACRUM | 0:119624335925 | 219 | int check_recv_status(int id, void *data); |
MACRUM | 0:119624335925 | 220 | |
MACRUM | 0:119624335925 | 221 | /** |
MACRUM | 0:119624335925 | 222 | * Attach a function to call whenever network state has changed |
MACRUM | 0:119624335925 | 223 | * |
MACRUM | 0:119624335925 | 224 | * @param obj pointer to the object to call the member function on |
MACRUM | 0:119624335925 | 225 | * @param method pointer to the member function to call |
MACRUM | 0:119624335925 | 226 | */ |
MACRUM | 0:119624335925 | 227 | template <typename T, typename M> |
MACRUM | 0:119624335925 | 228 | void attach(T *obj, M method) { |
MACRUM | 0:119624335925 | 229 | attach(Callback<void()>(obj, method)); |
MACRUM | 0:119624335925 | 230 | } |
MACRUM | 0:119624335925 | 231 | |
MACRUM | 0:119624335925 | 232 | private: |
MACRUM | 0:119624335925 | 233 | BufferedSpi _bufferspi; |
MACRUM | 0:119624335925 | 234 | ATParser _parser; |
MACRUM | 0:119624335925 | 235 | DigitalOut _resetpin; |
MACRUM | 0:119624335925 | 236 | volatile int _timeout; |
MACRUM | 0:119624335925 | 237 | volatile int _active_id; |
MACRUM | 0:119624335925 | 238 | void print_rx_buff(void); |
MACRUM | 0:119624335925 | 239 | bool check_response(void); |
MACRUM | 0:119624335925 | 240 | struct packet { |
MACRUM | 0:119624335925 | 241 | struct packet *next; |
MACRUM | 0:119624335925 | 242 | int id; |
MACRUM | 0:119624335925 | 243 | uint32_t len; |
MACRUM | 0:119624335925 | 244 | // data follows |
MACRUM | 0:119624335925 | 245 | } *_packets, **_packets_end; |
MACRUM | 0:119624335925 | 246 | void _packet_handler(); |
MACRUM | 0:119624335925 | 247 | bool _ism_debug; |
MACRUM | 0:119624335925 | 248 | |
MACRUM | 0:119624335925 | 249 | char _ip_buffer[16]; |
MACRUM | 0:119624335925 | 250 | char _gateway_buffer[16]; |
MACRUM | 0:119624335925 | 251 | char _netmask_buffer[16]; |
MACRUM | 0:119624335925 | 252 | char _mac_buffer[18]; |
MACRUM | 0:119624335925 | 253 | char _fw_version[16]; |
MACRUM | 0:119624335925 | 254 | }; |
MACRUM | 0:119624335925 | 255 | |
MACRUM | 0:119624335925 | 256 | #endif |