Jim Flynn / Mbed OS wifi_Example

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310Interface.h Source File

WizFi310Interface.h

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