Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Committer:
JimCarver
Date:
Thu Oct 25 14:00:12 2018 +0000
Revision:
4:e518dde96e59
Parent:
0:6b753f761943
Simulated dispenser

Who changed what in which revision?

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