Maggie Mei / emw3162-driver-mbed-os-5

Fork of emw3162-driver by Maggie Mei

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMW3162Interface.h Source File

EMW3162Interface.h

00001 /* EMW3162 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 #ifndef EMW3162_INTERFACE_H
00018 #define EMW3162_INTERFACE_H
00019 
00020 #include "mbed.h"
00021 //#include "NetworkSocketAPI/NetworkStack.h"
00022 //#include "NetworkSocketAPI/WiFiInterface.h"
00023 #include "EMW3162.h"
00024 
00025 
00026 #define EMW3162_SOCKET_COUNT 5
00027 
00028 /** EMW3162Interface class
00029  *  Implementation of the NetworkStack for the EMW3162
00030  */
00031 class EMW3162Interface : public NetworkStack, public WiFiInterface
00032 {
00033 public:
00034     /** EMW3162Interface lifetime
00035      * @param tx        TX pin
00036      * @param rx        RX pin
00037      * @param debug     Enable debugging
00038      */
00039     EMW3162Interface(PinName tx, PinName rx, 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     /** Set the WiFi network credentials
00064      *
00065      *  @param ssid      Name of the network to connect to
00066      *  @param pass      Security passphrase to connect to the network
00067      *  @param security  Type of encryption for connection
00068      *                   (defaults to NSAPI_SECURITY_NONE)
00069      *  @return          0 on success, or error code on failure
00070      */
00071     virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
00072 
00073     /** Set the WiFi network channel - NOT SUPPORTED
00074      *
00075      * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
00076      *
00077      *  @param channel   Channel on which the connection is to be made, or 0 for any (Default: 0)
00078      *  @return          Not supported, returns NSAPI_ERROR_UNSUPPORTED
00079      */
00080     virtual int set_channel(uint8_t channel);
00081 
00082     /** Stop the interface
00083      *  @return             0 on success, negative on failure
00084      */
00085     virtual int disconnect();
00086 
00087     /** Get the internally stored IP address
00088      *  @return             IP address of the interface or null if not yet connected
00089      */
00090     virtual const char *get_ip_address();
00091 
00092     /** Get the internally stored MAC address
00093      *  @return             MAC address of the interface
00094      */
00095     virtual const char *get_mac_address();
00096 
00097     /** Get the local gateway
00098      *
00099      *  @return         Null-terminated representation of the local gateway
00100      *                  or null if no network mask has been recieved
00101      */
00102     virtual const char *get_gateway();
00103 
00104     /** Get the local network mask
00105      *
00106      *  @return         Null-terminated representation of the local network mask
00107      *                  or null if no network mask has been recieved
00108      */
00109     virtual const char *get_netmask();
00110 
00111     /** Gets the current radio signal strength for active connection
00112      *
00113      * @return          Connection strength in dBm (negative value)
00114      */
00115     virtual int8_t get_rssi();
00116 
00117     /** Scan for available networks
00118      *
00119      * This function will block.
00120      *
00121      * @param  ap       Pointer to allocated array to store discovered AP
00122      * @param  count    Size of allocated @a res array, or 0 to only count available AP
00123      * @param  timeout  Timeout in milliseconds; 0 for no timeout (Default: 0)
00124      * @return          Number of entries in @a, or if @a count was 0 number of available networks, negative on error
00125      *                  see @a nsapi_error
00126      */
00127     virtual int scan(WiFiAccessPoint *res, unsigned count);
00128     
00129 protected:
00130     /** Open a socket
00131      *  @param handle       Handle in which to store new socket
00132      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00133      *  @return             0 on success, negative on failure
00134      */
00135     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00136 
00137     /** Close the socket
00138      *  @param handle       Socket handle
00139      *  @return             0 on success, negative on failure
00140      *  @note On failure, any memory associated with the socket must still 
00141      *        be cleaned up
00142      */
00143     virtual int socket_close(void *handle);
00144 
00145     /** Bind a server socket to a specific port
00146      *  @param handle       Socket handle
00147      *  @param address      Local address to listen for incoming connections on 
00148      *  @return             0 on success, negative on failure.
00149      */
00150     virtual int socket_bind(void *handle, const SocketAddress &address);
00151 
00152     /** Start listening for incoming connections
00153      *  @param handle       Socket handle
00154      *  @param backlog      Number of pending connections that can be queued up at any
00155      *                      one time [Default: 1]
00156      *  @return             0 on success, negative on failure
00157      */
00158     virtual int socket_listen(void *handle, int backlog);
00159 
00160     /** Connects this TCP socket to the server
00161      *  @param handle       Socket handle
00162      *  @param address      SocketAddress to connect to
00163      *  @return             0 on success, negative on failure
00164      */
00165     virtual int socket_connect(void *handle, const SocketAddress &address);
00166 
00167     /** Accept a new connection.
00168      *  @param handle       Handle in which to store new socket
00169      *  @param server       Socket handle to server to accept from
00170      *  @return             0 on success, negative on failure
00171      *  @note This call is not-blocking, if this call would block, must
00172      *        immediately return NSAPI_ERROR_WOULD_WAIT
00173      */
00174     virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
00175 
00176     /** Send data to the remote host
00177      *  @param handle       Socket handle
00178      *  @param data         The buffer to send to the host
00179      *  @param size         The length of the buffer to send
00180      *  @return             Number of written bytes on success, negative on failure
00181      *  @note This call is not-blocking, if this call would block, must
00182      *        immediately return NSAPI_ERROR_WOULD_WAIT
00183      */
00184     virtual int socket_send(void *handle, const void *data, unsigned size);
00185 
00186     /** Receive data from the remote host
00187      *  @param handle       Socket handle
00188      *  @param data         The buffer in which to store the data received from the host
00189      *  @param size         The maximum length of the buffer
00190      *  @return             Number of received bytes on success, negative on failure
00191      *  @note This call is not-blocking, if this call would block, must
00192      *        immediately return NSAPI_ERROR_WOULD_WAIT
00193      */
00194     virtual int socket_recv(void *handle, void *data, unsigned size);
00195 
00196     /** Send a packet to a remote endpoint
00197      *  @param handle       Socket handle
00198      *  @param address      The remote SocketAddress
00199      *  @param data         The packet to be sent
00200      *  @param size         The length of the packet to be sent
00201      *  @return             The number of written bytes on success, negative on failure
00202      *  @note This call is not-blocking, if this call would block, must
00203      *        immediately return NSAPI_ERROR_WOULD_WAIT
00204      */
00205     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00206 
00207     /** Receive a packet from a remote endpoint
00208      *  @param handle       Socket handle
00209      *  @param address      Destination for the remote SocketAddress or null
00210      *  @param buffer       The buffer for storing the incoming packet data
00211      *                      If a packet is too long to fit in the supplied buffer,
00212      *                      excess bytes are discarded
00213      *  @param size         The length of the buffer
00214      *  @return             The number of received bytes on success, negative on failure
00215      *  @note This call is not-blocking, if this call would block, must
00216      *        immediately return NSAPI_ERROR_WOULD_WAIT
00217      */
00218     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00219 
00220     /** Register a callback on state change of the socket
00221      *  @param handle       Socket handle
00222      *  @param callback     Function to call on state change
00223      *  @param data         Argument to pass to callback
00224      *  @note Callback may be called in an interrupt context.
00225      */
00226     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00227 
00228     /** Provide access to the NetworkStack object
00229      *
00230      *  @return The underlying NetworkStack object
00231      */
00232     virtual NetworkStack *get_stack()
00233     {
00234         return this;
00235     }
00236     
00237 private:
00238     EMW3162 _esp;
00239     bool _ids[EMW3162_SOCKET_COUNT];
00240 
00241     char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
00242     nsapi_security_t ap_sec;
00243     uint8_t ap_ch;
00244     char ap_pass[64]; /* The longest allowed passphrase */
00245 
00246     void event();
00247     struct {
00248         void (*callback)(void *);
00249         void *data;
00250     } _cbs[EMW3162_SOCKET_COUNT];
00251 };
00252 
00253 
00254 #endif