Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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