versione corretta

Dependents:   DISCO_L475VG_IOT01-Sensors-BSP

Committer:
group-Farnell24-IOT-Team
Date:
Tue Aug 21 08:34:28 2018 +0000
Revision:
0:766454e296c3
Initial commit

Who changed what in which revision?

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