Simulated product dispenser

Dependencies:   HTS221

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

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