leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
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 "mbed_trace.h"
00040 #include "NetworkStack.h"
00041 #include "WiFiInterface.h"
00042 #include "WizFi310.h"
00043 
00044 /** WizFi310Interface class
00045  *  Implementation of the NetworkStack for the WizFi310
00046  */
00047 class WizFi310Interface : public NetworkStack, public WiFiInterface {
00048 public:
00049     /* WizFi310Interface constructor
00050      */
00051 
00052     WizFi310Interface(PinName tx = MBED_CONF_WIZFI310_TX,
00053                       PinName rx = MBED_CONF_WIZFI310_RX,
00054                       PinName rts = MBED_CONF_WIZFI310_RTS,
00055                       PinName cts = MBED_CONF_WIZFI310_CTS,
00056                       PinName rst = MBED_CONF_WIZFI310_RST);
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     virtual nsapi_connection_status_t get_connection_status() const;
00104 
00105     virtual const char *get_ip_address();
00106 
00107     /** Gets the current radio signal strength for active connection
00108      *
00109      * @return          Connection strength in dBm (negative value)
00110      */
00111     virtual int8_t get_rssi();
00112     virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);
00113     virtual void attach(Callback<void(nsapi_event_t, intptr_t)> status_cb);
00114 
00115 protected:
00116     /** Open a socket
00117      *  @param handle       Handle in which to store new socket
00118      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00119      *  @return             0 on success, negative on failure
00120      */
00121     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00122 
00123     /** Close the socket
00124      *  @param handle       Socket handle
00125      *  @return             0 on success, negative on failure
00126      *  @note On failure, any memory associated with the socket must still
00127      *        be cleaned up
00128      */
00129     virtual int socket_close(void *handle);
00130 
00131     /** Bind a server socket to a specific port
00132      *  @param handle       Socket handle
00133      *  @param address      Local address to listen for incoming connections on
00134      *  @return             0 on success, negative on failure.
00135      */
00136     virtual int socket_bind(void *handle, const SocketAddress &address);
00137 
00138     /** Start listening for incoming connections
00139      *  @param handle       Socket handle
00140      *  @param backlog      Number of pending connections that can be queued up at any
00141      *                      one time [Default: 1]
00142      *  @return             0 on success, negative on failure
00143      */
00144     virtual int socket_listen(void *handle, int backlog);
00145 
00146     /** Connects this TCP socket to the server
00147      *  @param handle       Socket handle
00148      *  @param address      SocketAddress to connect to
00149      *  @return             0 on success, negative on failure
00150      */
00151     virtual int socket_connect(void *handle, const SocketAddress &address);
00152 
00153     /** Accept a new connection.
00154      *  @param handle       Handle in which to store new socket
00155      *  @param server       Socket handle to server to accept from
00156      *  @return             0 on success, negative on failure
00157      *  @note This call is not-blocking, if this call would block, must
00158      *        immediately return NSAPI_ERROR_WOULD_WAIT
00159      */
00160     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00161 
00162     /** Send data to the remote host
00163      *  @param handle       Socket handle
00164      *  @param data         The buffer to send to the host
00165      *  @param size         The length of the buffer to send
00166      *  @return             Number of written bytes on success, negative on failure
00167      *  @note This call is not-blocking, if this call would block, must
00168      *        immediately return NSAPI_ERROR_WOULD_WAIT
00169      */
00170     virtual int socket_send(void *handle, const void *data, unsigned size);
00171 
00172     /** Receive data from the remote host
00173      *  @param handle       Socket handle
00174      *  @param data         The buffer in which to store the data received from the host
00175      *  @param size         The maximum length of the buffer
00176      *  @return             Number of received bytes on success, negative on failure
00177      *  @note This call is not-blocking, if this call would block, must
00178      *        immediately return NSAPI_ERROR_WOULD_WAIT
00179      */
00180     virtual int socket_recv(void *handle, void *data, unsigned size);
00181 
00182     /** Send a packet to a remote endpoint
00183      *  @param handle       Socket handle
00184      *  @param address      The remote SocketAddress
00185      *  @param data         The packet to be sent
00186      *  @param size         The length of the packet to be sent
00187      *  @return             The number of written bytes on success, negative on failure
00188      *  @note This call is not-blocking, if this call would block, must
00189      *        immediately return NSAPI_ERROR_WOULD_WAIT
00190      */
00191     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00192 
00193     /** Receive a packet from a remote endpoint
00194      *  @param handle       Socket handle
00195      *  @param address      Destination for the remote SocketAddress or null
00196      *  @param buffer       The buffer for storing the incoming packet data
00197      *                      If a packet is too long to fit in the supplied buffer,
00198      *                      excess bytes are discarded
00199      *  @param size         The length of the buffer
00200      *  @return             The number of received bytes on success, negative on failure
00201      *  @note This call is not-blocking, if this call would block, must
00202      *        immediately return NSAPI_ERROR_WOULD_WAIT
00203      */
00204     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00205 
00206     /** Register a callback on state change of the socket
00207      *  @param handle       Socket handle
00208      *  @param callback     Function to call on state change
00209      *  @param data         Argument to pass to callback
00210      *  @note Callback may be called in an interrupt context.
00211      */
00212     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00213 
00214     /** Provide access to the NetworkStack object
00215     *
00216     *  @return The underlying NetworkStack object
00217     */
00218     virtual NetworkStack *get_stack()
00219     {
00220         return this;
00221     }
00222 
00223 private:
00224     struct wizfi310_socket {
00225         WizFi310 &wifi;
00226         Mutex op_mtx;
00227         Mutex state_mtx;
00228         nsapi_protocol_t proto;
00229         SocketAddress addr;
00230         Packet *first;
00231         Packet *last;
00232 
00233         int id;
00234         volatile bool connected;
00235 
00236         Semaphore semphr;
00237         Callback<void (void *)> cbk;
00238         void *data;
00239 
00240         wizfi310_socket(WizFi310 &wiz, nsapi_protocol_t proto):
00241             wifi(wiz),
00242             op_mtx("wizfi310_socket_op"),
00243             state_mtx("wizfi310_socket_state"),
00244             proto(proto), first(NULL), last(NULL),
00245             id(-1), connected(false),
00246             semphr(0, 1), cbk(NULL), data(NULL) {}
00247 
00248 
00249         void close();
00250 
00251         ~wizfi310_socket();
00252     };
00253     struct scan_ctx_t {
00254         uint32_t count;
00255         uint32_t idx;
00256         WiFiAccessPoint *res;
00257     } volatile m_scan_ctx;
00258 
00259     WizFi310 m_wizfi310;
00260 
00261     char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
00262     nsapi_security_t ap_sec;
00263     uint8_t ap_ch;
00264     char ap_pass[64]; /* The longest allowed passphrase */
00265 
00266     Mutex m_mutex;
00267     Semaphore m_semphr;
00268     Callback<void(nsapi_event_t, intptr_t)>  m_on_status_change;
00269     uint32_t m_socket_count;
00270 
00271 
00272     void evt_rdy(bool dhcp, const char *ip, const char *gw, const char *mac);
00273     void do_send_data(wizfi310_socket *s);
00274     void scan_ap(nsapi_wifi_ap_t *ap);
00275     void link_status_change(nsapi_connection_status_t event);
00276     static void socket_event(void *ctx, WizFi310::socket_event_t type, WizFi310::socket_event_data_t &data);
00277 };
00278 
00279 #endif
00280