Axel Hwang / Mbed OS ProjetIOT
Committer:
axelhwang
Date:
Wed Oct 28 07:42:46 2020 +0000
Revision:
0:5738799d7a56
Push

Who changed what in which revision?

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