Farnell-Element14 Bologna IOT Team / wifi-ism43362

Dependents:   DISCO_L475VG_IOT01-Sensors-BSP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ISM43362Interface.h Source File

ISM43362Interface.h

00001 /* ISM43362 implementation of NetworkInterfaceAPI
00002  * Copyright (c) STMicroelectronics 2017
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ISM43362_INTERFACE_H
00018 #define ISM43362_INTERFACE_H
00019 
00020 #include "mbed.h"
00021 #include "ISM43362.h"
00022 
00023 
00024 #define ISM43362_SOCKET_COUNT 4
00025 
00026 /** ISM43362Interface class
00027  *  Implementation of the NetworkStack for the ISM43362
00028  */
00029 class ISM43362Interface : public NetworkStack, public WiFiInterface
00030 {
00031 public:
00032     /** ISM43362Interface lifetime
00033      * @param mosi       MOSI pin
00034      * @param miso       MISO pin
00035      * @param clk        CLOCK pin
00036      * @param nss        NSS pin
00037      * @param debug     Enable debugging
00038      */
00039     ISM43362Interface(PinName mosi, PinName miso, PinName clk, PinName nss, PinName reset, PinName dataready, PinName wakeup, bool debug = false);
00040 
00041     /** Start the interface
00042      *
00043      *  Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
00044      *  If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
00045      *
00046      *  @return         0 on success, negative error code on failure
00047      */
00048     virtual int connect();
00049 
00050     /** Start the interface
00051      *
00052      *  Attempts to connect to a WiFi network.
00053      *
00054      *  @param ssid      Name of the network to connect to
00055      *  @param pass      Security passphrase to connect to the network
00056      *  @param security  Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
00057      *  @param channel   This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
00058      *  @return          0 on success, or error code on failure
00059      */
00060     virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
00061                                   uint8_t channel = 0);
00062     
00063     /** Translates a hostname to an IP address with specific version
00064      *
00065      *  The hostname may be either a domain name or an IP address. If the
00066      *  hostname is an IP address, no network transactions will be performed.
00067      *
00068      *
00069      *  @param host     Hostname to resolve
00070      *  @param address  Destination for the host SocketAddress
00071      *  @param version  IP version of address to resolve, NSAPI_UNSPEC indicates
00072      *                  version is chosen by the stack (defaults to NSAPI_UNSPEC)
00073      *  @return         0 on success, negative error code on failure
00074      */
00075     virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
00076     
00077     /** Set the WiFi network credentials
00078      *
00079      *  @param ssid      Name of the network to connect to
00080      *  @param pass      Security passphrase to connect to the network
00081      *  @param security  Type of encryption for connection
00082      *                   (defaults to NSAPI_SECURITY_NONE)
00083      *  @return          0 on success, or error code on failure
00084      */
00085     virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
00086 
00087     /** Set the WiFi network channel - NOT SUPPORTED
00088      *
00089      * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
00090      *
00091      *  @param channel   Channel on which the connection is to be made, or 0 for any (Default: 0)
00092      *  @return          Not supported, returns NSAPI_ERROR_UNSUPPORTED
00093      */
00094     virtual int set_channel(uint8_t channel);
00095 
00096     /** Stop the interface
00097      *  @return             0 on success, negative on failure
00098      */
00099     virtual int disconnect();
00100 
00101     /** Get the internally stored IP address
00102      *  @return             IP address of the interface or null if not yet connected
00103      */
00104     virtual const char *get_ip_address();
00105 
00106     /** Get the internally stored MAC address
00107      *  @return             MAC address of the interface
00108      */
00109     virtual const char *get_mac_address();
00110 
00111      /** Get the local gateway
00112      *
00113      *  @return         Null-terminated representation of the local gateway
00114      *                  or null if no network mask has been recieved
00115      */
00116     virtual const char *get_gateway();
00117 
00118     /** Get the local network mask
00119      *
00120      *  @return         Null-terminated representation of the local network mask
00121      *                  or null if no network mask has been recieved
00122      */
00123     virtual const char *get_netmask();
00124 
00125     /** Gets the current radio signal strength for active connection
00126      *
00127      * @return          Connection strength in dBm (negative value)
00128      */
00129     virtual int8_t get_rssi();
00130 
00131     /** Scan for available networks
00132      *
00133      * This function will block.
00134      *
00135      * @param  ap       Pointer to allocated array to store discovered AP
00136      * @param  count    Size of allocated @a res array, or 0 to only count available AP
00137      * @param  timeout  Timeout in milliseconds; 0 for no timeout (Default: 0)
00138      * @return          Number of entries in @a, or if @a count was 0 number of available networks, negative on error
00139      *                  see @a nsapi_error
00140      */
00141     virtual int scan(WiFiAccessPoint *res, unsigned count);
00142 
00143     /** Translates a hostname to an IP address with specific version
00144      *
00145      *  The hostname may be either a domain name or an IP address. If the
00146      *  hostname is an IP address, no network transactions will be performed.
00147      *
00148      *  If no stack-specific DNS resolution is provided, the hostname
00149      *  will be resolve using a UDP socket on the stack.
00150      *
00151      *  @param address  Destination for the host SocketAddress
00152      *  @param host     Hostname to resolve
00153      *  @param version  IP version of address to resolve, NSAPI_UNSPEC indicates
00154      *                  version is chosen by the stack (defaults to NSAPI_UNSPEC)
00155      *  @return         0 on success, negative error code on failure
00156      */
00157     using NetworkInterface::gethostbyname;
00158 
00159     /** Add a domain name server to list of servers to query
00160      *
00161      *  @param addr     Destination for the host address
00162      *  @return         0 on success, negative error code on failure
00163      */
00164     using NetworkInterface::add_dns_server;
00165 
00166 protected:
00167     /** Open a socket
00168      *  @param handle       Handle in which to store new socket
00169      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00170      *  @return             0 on success, negative on failure
00171      */
00172     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00173 
00174     /** Close the socket
00175      *  @param handle       Socket handle
00176      *  @return             0 on success, negative on failure
00177      *  @note On failure, any memory associated with the socket must still
00178      *        be cleaned up
00179      */
00180     virtual int socket_close(void *handle);
00181 
00182     /** Bind a server socket to a specific port
00183      *  @param handle       Socket handle
00184      *  @param address      Local address to listen for incoming connections on
00185      *  @return             0 on success, negative on failure.
00186      */
00187     virtual int socket_bind(void *handle, const SocketAddress &address);
00188 
00189     /** Start listening for incoming connections
00190      *  @param handle       Socket handle
00191      *  @param backlog      Number of pending connections that can be queued up at any
00192      *                      one time [Default: 1]
00193      *  @return             0 on success, negative on failure
00194      */
00195     virtual int socket_listen(void *handle, int backlog);
00196 
00197     /** Connects this TCP socket to the server
00198      *  @param handle       Socket handle
00199      *  @param address      SocketAddress to connect to
00200      *  @return             0 on success, negative on failure
00201      */
00202     virtual int socket_connect(void *handle, const SocketAddress &address);
00203 
00204     /** Accept a new connection.
00205      *  @param handle       Handle in which to store new socket
00206      *  @param server       Socket handle to server to accept from
00207      *  @return             0 on success, negative on failure
00208      *  @note This call is not-blocking, if this call would block, must
00209      *        immediately return NSAPI_ERROR_WOULD_WAIT
00210      */
00211     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00212 
00213     /** Send data to the remote host
00214      *  @param handle       Socket handle
00215      *  @param data         The buffer to send to the host
00216      *  @param size         The length of the buffer to send
00217      *  @return             Number of written bytes on success, negative on failure
00218      *  @note This call is not-blocking, if this call would block, must
00219      *        immediately return NSAPI_ERROR_WOULD_WAIT
00220      */
00221     virtual int socket_send(void *handle, const void *data, unsigned size);
00222 
00223     /** Receive data from the remote host
00224      *  @param handle       Socket handle
00225      *  @param data         The buffer in which to store the data received from the host
00226      *  @param size         The maximum length of the buffer
00227      *  @return             Number of received bytes on success, negative on failure
00228      *  @note This call is not-blocking, if this call would block, must
00229      *        immediately return NSAPI_ERROR_WOULD_WAIT
00230      */
00231     virtual int socket_recv(void *handle, void *data, unsigned size);
00232 
00233     /** Send a packet to a remote endpoint
00234      *  @param handle       Socket handle
00235      *  @param address      The remote SocketAddress
00236      *  @param data         The packet to be sent
00237      *  @param size         The length of the packet to be sent
00238      *  @return             The number of written bytes on success, negative on failure
00239      *  @note This call is not-blocking, if this call would block, must
00240      *        immediately return NSAPI_ERROR_WOULD_WAIT
00241      */
00242     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00243 
00244     /** Receive a packet from a remote endpoint
00245      *  @param handle       Socket handle
00246      *  @param address      Destination for the remote SocketAddress or null
00247      *  @param buffer       The buffer for storing the incoming packet data
00248      *                      If a packet is too long to fit in the supplied buffer,
00249      *                      excess bytes are discarded
00250      *  @param size         The length of the buffer
00251      *  @return             The number of received bytes on success, negative on failure
00252      *  @note This call is not-blocking, if this call would block, must
00253      *        immediately return NSAPI_ERROR_WOULD_WAIT
00254      */
00255     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00256 
00257     /** Register a callback on state change of the socket
00258      *  @param handle       Socket handle
00259      *  @param callback     Function to call on state change
00260      *  @param data         Argument to pass to callback
00261      *  @note Callback may be called in an interrupt context.
00262      */
00263     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00264 
00265     /** Provide access to the NetworkStack object
00266      *
00267      *  @return The underlying NetworkStack object
00268      */
00269     virtual NetworkStack *get_stack()
00270     {
00271         return this;
00272     }
00273 
00274 private:
00275     ISM43362 _ism;
00276     bool _ids[ISM43362_SOCKET_COUNT];
00277     uint32_t _socket_obj[ISM43362_SOCKET_COUNT]; // store addresses of socket handles
00278     Mutex _mutex;
00279     Thread thread_read_socket;
00280     char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
00281     nsapi_security_t ap_sec;
00282     uint8_t ap_ch;
00283     char ap_pass[64]; /* The longest allowed passphrase */
00284 
00285     void event();
00286     struct {
00287         void (*callback)(void *);
00288         void *data;
00289     } _cbs[ISM43362_SOCKET_COUNT];
00290 
00291     /** Function called by the socket read thread to check if data is available on the wifi module
00292      *
00293      */
00294     virtual void socket_check_read();
00295     int socket_send_nolock(void *handle, const void *data, unsigned size);
00296     int socket_connect_nolock(void *handle, const SocketAddress &addr);
00297 
00298 };
00299 
00300 #endif