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 /* ISM43362 implementation of NetworkInterfaceAPI
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_INTERFACE_H
JimCarver 0:6b753f761943 18 #define ISM43362_INTERFACE_H
JimCarver 0:6b753f761943 19
JimCarver 0:6b753f761943 20 #include "mbed.h"
JimCarver 0:6b753f761943 21 #include "ISM43362.h"
JimCarver 0:6b753f761943 22
JimCarver 0:6b753f761943 23
JimCarver 0:6b753f761943 24 #define ISM43362_SOCKET_COUNT 4
JimCarver 0:6b753f761943 25
JimCarver 0:6b753f761943 26 /** ISM43362Interface class
JimCarver 0:6b753f761943 27 * Implementation of the NetworkStack for the ISM43362
JimCarver 0:6b753f761943 28 */
JimCarver 0:6b753f761943 29 class ISM43362Interface : public NetworkStack, public WiFiInterface
JimCarver 0:6b753f761943 30 {
JimCarver 0:6b753f761943 31 public:
JimCarver 0:6b753f761943 32 /** ISM43362Interface lifetime
JimCarver 0:6b753f761943 33 * @param debug Enable debugging
JimCarver 0:6b753f761943 34 */
JimCarver 0:6b753f761943 35 ISM43362Interface(bool debug = MBED_CONF_ISM43362_WIFI_DEBUG);
JimCarver 0:6b753f761943 36
JimCarver 0:6b753f761943 37 /** Start the interface
JimCarver 0:6b753f761943 38 *
JimCarver 0:6b753f761943 39 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
JimCarver 0:6b753f761943 40 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
JimCarver 0:6b753f761943 41 *
JimCarver 0:6b753f761943 42 * @return 0 on success, negative error code on failure
JimCarver 0:6b753f761943 43 */
JimCarver 0:6b753f761943 44 virtual int connect();
JimCarver 0:6b753f761943 45
JimCarver 0:6b753f761943 46 /** Start the interface
JimCarver 0:6b753f761943 47 *
JimCarver 0:6b753f761943 48 * Attempts to connect to a WiFi network.
JimCarver 0:6b753f761943 49 *
JimCarver 0:6b753f761943 50 * @param ssid Name of the network to connect to
JimCarver 0:6b753f761943 51 * @param pass Security passphrase to connect to the network
JimCarver 0:6b753f761943 52 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
JimCarver 0:6b753f761943 53 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
JimCarver 0:6b753f761943 54 * @return 0 on success, or error code on failure
JimCarver 0:6b753f761943 55 */
JimCarver 0:6b753f761943 56 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
JimCarver 0:6b753f761943 57 uint8_t channel = 0);
JimCarver 0:6b753f761943 58
JimCarver 0:6b753f761943 59 /** Translates a hostname to an IP address with specific version
JimCarver 0:6b753f761943 60 *
JimCarver 0:6b753f761943 61 * The hostname may be either a domain name or an IP address. If the
JimCarver 0:6b753f761943 62 * hostname is an IP address, no network transactions will be performed.
JimCarver 0:6b753f761943 63 *
JimCarver 0:6b753f761943 64 *
JimCarver 0:6b753f761943 65 * @param host Hostname to resolve
JimCarver 0:6b753f761943 66 * @param address Destination for the host SocketAddress
JimCarver 0:6b753f761943 67 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
JimCarver 0:6b753f761943 68 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
JimCarver 0:6b753f761943 69 * @return 0 on success, negative error code on failure
JimCarver 0:6b753f761943 70 */
JimCarver 0:6b753f761943 71 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
JimCarver 0:6b753f761943 72
JimCarver 0:6b753f761943 73 /** Set the WiFi network credentials
JimCarver 0:6b753f761943 74 *
JimCarver 0:6b753f761943 75 * @param ssid Name of the network to connect to
JimCarver 0:6b753f761943 76 * @param pass Security passphrase to connect to the network
JimCarver 0:6b753f761943 77 * @param security Type of encryption for connection
JimCarver 0:6b753f761943 78 * (defaults to NSAPI_SECURITY_NONE)
JimCarver 0:6b753f761943 79 * @return 0 on success, or error code on failure
JimCarver 0:6b753f761943 80 */
JimCarver 0:6b753f761943 81 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
JimCarver 0:6b753f761943 82
JimCarver 0:6b753f761943 83 /** Set the WiFi network channel - NOT SUPPORTED
JimCarver 0:6b753f761943 84 *
JimCarver 0:6b753f761943 85 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
JimCarver 0:6b753f761943 86 *
JimCarver 0:6b753f761943 87 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
JimCarver 0:6b753f761943 88 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
JimCarver 0:6b753f761943 89 */
JimCarver 0:6b753f761943 90 virtual int set_channel(uint8_t channel);
JimCarver 0:6b753f761943 91
JimCarver 0:6b753f761943 92 /** Stop the interface
JimCarver 0:6b753f761943 93 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 94 */
JimCarver 0:6b753f761943 95 virtual int disconnect();
JimCarver 0:6b753f761943 96
JimCarver 0:6b753f761943 97 /** Get the internally stored IP address
JimCarver 0:6b753f761943 98 * @return IP address of the interface or null if not yet connected
JimCarver 0:6b753f761943 99 */
JimCarver 0:6b753f761943 100 virtual const char *get_ip_address();
JimCarver 0:6b753f761943 101
JimCarver 0:6b753f761943 102 /** Get the internally stored MAC address
JimCarver 0:6b753f761943 103 * @return MAC address of the interface
JimCarver 0:6b753f761943 104 */
JimCarver 0:6b753f761943 105 virtual const char *get_mac_address();
JimCarver 0:6b753f761943 106
JimCarver 0:6b753f761943 107 /** Get the local gateway
JimCarver 0:6b753f761943 108 *
JimCarver 0:6b753f761943 109 * @return Null-terminated representation of the local gateway
JimCarver 0:6b753f761943 110 * or null if no network mask has been recieved
JimCarver 0:6b753f761943 111 */
JimCarver 0:6b753f761943 112 virtual const char *get_gateway();
JimCarver 0:6b753f761943 113
JimCarver 0:6b753f761943 114 /** Get the local network mask
JimCarver 0:6b753f761943 115 *
JimCarver 0:6b753f761943 116 * @return Null-terminated representation of the local network mask
JimCarver 0:6b753f761943 117 * or null if no network mask has been recieved
JimCarver 0:6b753f761943 118 */
JimCarver 0:6b753f761943 119 virtual const char *get_netmask();
JimCarver 0:6b753f761943 120
JimCarver 0:6b753f761943 121 /** Gets the current radio signal strength for active connection
JimCarver 0:6b753f761943 122 *
JimCarver 0:6b753f761943 123 * @return Connection strength in dBm (negative value)
JimCarver 0:6b753f761943 124 */
JimCarver 0:6b753f761943 125 virtual int8_t get_rssi();
JimCarver 0:6b753f761943 126
JimCarver 0:6b753f761943 127 /** Scan for available networks
JimCarver 0:6b753f761943 128 *
JimCarver 0:6b753f761943 129 * This function will block.
JimCarver 0:6b753f761943 130 *
JimCarver 0:6b753f761943 131 * @param ap Pointer to allocated array to store discovered AP
JimCarver 0:6b753f761943 132 * @param count Size of allocated @a res array, or 0 to only count available AP
JimCarver 0:6b753f761943 133 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
JimCarver 0:6b753f761943 134 * see @a nsapi_error
JimCarver 0:6b753f761943 135 */
JimCarver 0:6b753f761943 136 virtual int scan(WiFiAccessPoint *res, unsigned count);
JimCarver 0:6b753f761943 137
JimCarver 0:6b753f761943 138 /** Translates a hostname to an IP address with specific version
JimCarver 0:6b753f761943 139 *
JimCarver 0:6b753f761943 140 * The hostname may be either a domain name or an IP address. If the
JimCarver 0:6b753f761943 141 * hostname is an IP address, no network transactions will be performed.
JimCarver 0:6b753f761943 142 *
JimCarver 0:6b753f761943 143 * If no stack-specific DNS resolution is provided, the hostname
JimCarver 0:6b753f761943 144 * will be resolve using a UDP socket on the stack.
JimCarver 0:6b753f761943 145 *
JimCarver 0:6b753f761943 146 * @param address Destination for the host SocketAddress
JimCarver 0:6b753f761943 147 * @param host Hostname to resolve
JimCarver 0:6b753f761943 148 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
JimCarver 0:6b753f761943 149 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
JimCarver 0:6b753f761943 150 * @return 0 on success, negative error code on failure
JimCarver 0:6b753f761943 151 */
JimCarver 0:6b753f761943 152 using NetworkInterface::gethostbyname;
JimCarver 0:6b753f761943 153
JimCarver 0:6b753f761943 154 /** Add a domain name server to list of servers to query
JimCarver 0:6b753f761943 155 *
JimCarver 0:6b753f761943 156 * @param addr Destination for the host address
JimCarver 0:6b753f761943 157 * @return 0 on success, negative error code on failure
JimCarver 0:6b753f761943 158 */
JimCarver 0:6b753f761943 159 using NetworkInterface::add_dns_server;
JimCarver 0:6b753f761943 160
JimCarver 0:6b753f761943 161 protected:
JimCarver 0:6b753f761943 162 /** Open a socket
JimCarver 0:6b753f761943 163 * @param handle Handle in which to store new socket
JimCarver 0:6b753f761943 164 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
JimCarver 0:6b753f761943 165 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 166 */
JimCarver 0:6b753f761943 167 virtual int socket_open(void **handle, nsapi_protocol_t proto);
JimCarver 0:6b753f761943 168
JimCarver 0:6b753f761943 169 /** Close the socket
JimCarver 0:6b753f761943 170 * @param handle Socket handle
JimCarver 0:6b753f761943 171 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 172 * @note On failure, any memory associated with the socket must still
JimCarver 0:6b753f761943 173 * be cleaned up
JimCarver 0:6b753f761943 174 */
JimCarver 0:6b753f761943 175 virtual int socket_close(void *handle);
JimCarver 0:6b753f761943 176
JimCarver 0:6b753f761943 177 /** Bind a server socket to a specific port
JimCarver 0:6b753f761943 178 * @param handle Socket handle
JimCarver 0:6b753f761943 179 * @param address Local address to listen for incoming connections on
JimCarver 0:6b753f761943 180 * @return 0 on success, negative on failure.
JimCarver 0:6b753f761943 181 */
JimCarver 0:6b753f761943 182 virtual int socket_bind(void *handle, const SocketAddress &address);
JimCarver 0:6b753f761943 183
JimCarver 0:6b753f761943 184 /** Start listening for incoming connections
JimCarver 0:6b753f761943 185 * @param handle Socket handle
JimCarver 0:6b753f761943 186 * @param backlog Number of pending connections that can be queued up at any
JimCarver 0:6b753f761943 187 * one time [Default: 1]
JimCarver 0:6b753f761943 188 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 189 */
JimCarver 0:6b753f761943 190 virtual int socket_listen(void *handle, int backlog);
JimCarver 0:6b753f761943 191
JimCarver 0:6b753f761943 192 /** Connects this TCP socket to the server
JimCarver 0:6b753f761943 193 * @param handle Socket handle
JimCarver 0:6b753f761943 194 * @param address SocketAddress to connect to
JimCarver 0:6b753f761943 195 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 196 */
JimCarver 0:6b753f761943 197 virtual int socket_connect(void *handle, const SocketAddress &address);
JimCarver 0:6b753f761943 198
JimCarver 0:6b753f761943 199 /** Accept a new connection.
JimCarver 0:6b753f761943 200 * @param handle Handle in which to store new socket
JimCarver 0:6b753f761943 201 * @param server Socket handle to server to accept from
JimCarver 0:6b753f761943 202 * @return 0 on success, negative on failure
JimCarver 0:6b753f761943 203 * @note This call is not-blocking, if this call would block, must
JimCarver 0:6b753f761943 204 * immediately return NSAPI_ERROR_WOULD_WAIT
JimCarver 0:6b753f761943 205 */
JimCarver 0:6b753f761943 206 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
JimCarver 0:6b753f761943 207
JimCarver 0:6b753f761943 208 /** Send data to the remote host
JimCarver 0:6b753f761943 209 * @param handle Socket handle
JimCarver 0:6b753f761943 210 * @param data The buffer to send to the host
JimCarver 0:6b753f761943 211 * @param size The length of the buffer to send
JimCarver 0:6b753f761943 212 * @return Number of written bytes on success, negative on failure
JimCarver 0:6b753f761943 213 * @note This call is not-blocking, if this call would block, must
JimCarver 0:6b753f761943 214 * immediately return NSAPI_ERROR_WOULD_WAIT
JimCarver 0:6b753f761943 215 */
JimCarver 0:6b753f761943 216 virtual int socket_send(void *handle, const void *data, unsigned size);
JimCarver 0:6b753f761943 217
JimCarver 0:6b753f761943 218 /** Receive data from the remote host
JimCarver 0:6b753f761943 219 * @param handle Socket handle
JimCarver 0:6b753f761943 220 * @param data The buffer in which to store the data received from the host
JimCarver 0:6b753f761943 221 * @param size The maximum length of the buffer
JimCarver 0:6b753f761943 222 * @return Number of received bytes on success, negative on failure
JimCarver 0:6b753f761943 223 * @note This call is not-blocking, if this call would block, must
JimCarver 0:6b753f761943 224 * immediately return NSAPI_ERROR_WOULD_WAIT
JimCarver 0:6b753f761943 225 */
JimCarver 0:6b753f761943 226 virtual int socket_recv(void *handle, void *data, unsigned size);
JimCarver 0:6b753f761943 227
JimCarver 0:6b753f761943 228 /** Send a packet to a remote endpoint
JimCarver 0:6b753f761943 229 * @param handle Socket handle
JimCarver 0:6b753f761943 230 * @param address The remote SocketAddress
JimCarver 0:6b753f761943 231 * @param data The packet to be sent
JimCarver 0:6b753f761943 232 * @param size The length of the packet to be sent
JimCarver 0:6b753f761943 233 * @return The number of written bytes on success, negative on failure
JimCarver 0:6b753f761943 234 * @note This call is not-blocking, if this call would block, must
JimCarver 0:6b753f761943 235 * immediately return NSAPI_ERROR_WOULD_WAIT
JimCarver 0:6b753f761943 236 */
JimCarver 0:6b753f761943 237 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
JimCarver 0:6b753f761943 238
JimCarver 0:6b753f761943 239 /** Receive a packet from a remote endpoint
JimCarver 0:6b753f761943 240 * @param handle Socket handle
JimCarver 0:6b753f761943 241 * @param address Destination for the remote SocketAddress or null
JimCarver 0:6b753f761943 242 * @param buffer The buffer for storing the incoming packet data
JimCarver 0:6b753f761943 243 * If a packet is too long to fit in the supplied buffer,
JimCarver 0:6b753f761943 244 * excess bytes are discarded
JimCarver 0:6b753f761943 245 * @param size The length of the buffer
JimCarver 0:6b753f761943 246 * @return The number of received bytes on success, negative on failure
JimCarver 0:6b753f761943 247 * @note This call is not-blocking, if this call would block, must
JimCarver 0:6b753f761943 248 * immediately return NSAPI_ERROR_WOULD_WAIT
JimCarver 0:6b753f761943 249 */
JimCarver 0:6b753f761943 250 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
JimCarver 0:6b753f761943 251
JimCarver 0:6b753f761943 252 /** Register a callback on state change of the socket
JimCarver 0:6b753f761943 253 * @param handle Socket handle
JimCarver 0:6b753f761943 254 * @param callback Function to call on state change
JimCarver 0:6b753f761943 255 * @param data Argument to pass to callback
JimCarver 0:6b753f761943 256 * @note Callback may be called in an interrupt context.
JimCarver 0:6b753f761943 257 */
JimCarver 0:6b753f761943 258 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
JimCarver 0:6b753f761943 259
JimCarver 0:6b753f761943 260 /** Provide access to the NetworkStack object
JimCarver 0:6b753f761943 261 *
JimCarver 0:6b753f761943 262 * @return The underlying NetworkStack object
JimCarver 0:6b753f761943 263 */
JimCarver 0:6b753f761943 264 virtual NetworkStack *get_stack()
JimCarver 0:6b753f761943 265 {
JimCarver 0:6b753f761943 266 return this;
JimCarver 0:6b753f761943 267 }
JimCarver 0:6b753f761943 268
JimCarver 0:6b753f761943 269 private:
JimCarver 0:6b753f761943 270 ISM43362 _ism;
JimCarver 0:6b753f761943 271 bool _ids[ISM43362_SOCKET_COUNT];
JimCarver 0:6b753f761943 272 uint32_t _socket_obj[ISM43362_SOCKET_COUNT]; // store addresses of socket handles
JimCarver 0:6b753f761943 273 Mutex _mutex;
JimCarver 0:6b753f761943 274 Thread thread_read_socket;
JimCarver 0:6b753f761943 275 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
JimCarver 0:6b753f761943 276 ism_security_t ap_sec;
JimCarver 0:6b753f761943 277 uint8_t ap_ch;
JimCarver 0:6b753f761943 278 char ap_pass[64]; /* The longest allowed passphrase */
JimCarver 0:6b753f761943 279
JimCarver 0:6b753f761943 280 bool _ism_debug;
JimCarver 0:6b753f761943 281 uint32_t _FwVersion;
JimCarver 0:6b753f761943 282
JimCarver 0:6b753f761943 283 void event();
JimCarver 0:6b753f761943 284 struct {
JimCarver 0:6b753f761943 285 void (*callback)(void *);
JimCarver 0:6b753f761943 286 void *data;
JimCarver 0:6b753f761943 287 } _cbs[ISM43362_SOCKET_COUNT];
JimCarver 0:6b753f761943 288
JimCarver 0:6b753f761943 289 /** Function called by the socket read thread to check if data is available on the wifi module
JimCarver 0:6b753f761943 290 *
JimCarver 0:6b753f761943 291 */
JimCarver 0:6b753f761943 292 virtual void socket_check_read();
JimCarver 0:6b753f761943 293 int socket_send_nolock(void *handle, const void *data, unsigned size);
JimCarver 0:6b753f761943 294 int socket_connect_nolock(void *handle, const SocketAddress &addr);
JimCarver 0:6b753f761943 295
JimCarver 0:6b753f761943 296 };
JimCarver 0:6b753f761943 297
JimCarver 0:6b753f761943 298 #endif