Jim Flynn / Mbed OS wifi_Example

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WizFi310.h Source File

WizFi310.h

Go to the documentation of this file.
00001 /* 
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    WizFi310.h
00020   * @author  Gateway Team
00021   * @brief   Header file of 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_H_
00037 #define WIZFI310_H_
00038 
00039 #include "ATCmdParser.h"
00040 #include "netsocket/WiFiAccessPoint.h"
00041 #include "UARTSerial.h"
00042 
00043 /** WizFi310Interface class.
00044     This is an interface to a WizFi310Interface radio.
00045 */
00046 class WizFi310
00047 {
00048 public:
00049     WizFi310(PinName tx, PinName rx, bool debug=false);
00050 
00051     /**
00052     * Check firmware version of WizFi310
00053     *
00054     * @return character array firmware version or 0 if firmware query command gives outdated response
00055     */
00056     const char* get_firmware_version(void);
00057     
00058     /**
00059     * Startup the WizFi310
00060     *
00061     * @param mode mode of WIFI 0-client, 1-host
00062     * @return true only if WizFi310 was setup correctly
00063     */
00064     bool startup(int mode);
00065 
00066     /**
00067     * Reset WizFi310
00068     *
00069     * @return true only if WizFi310 resets successfully
00070     */
00071     bool reset(void);
00072 
00073     /**
00074     * Enable/Disable DHCP
00075     *
00076     * @param enabled DHCP enabled when true
00077     * @return true only if WizFi310 enables/disables DHCP successfully
00078     */
00079     bool dhcp(bool enabled);
00080 
00081     /**
00082     * Connect WizFi310 to AP
00083     *
00084     * @param ap the name of the AP
00085     * @param passPhrase the password of AP
00086     * @param security type of AP
00087     * @return true only if WizFi310 is connected successfully
00088     */
00089     bool connect(const char *ap, const char *passPhrase, const char *sec);
00090 
00091     /**
00092     * Disconnect WizFi310 from AP
00093     *
00094     * @return true only if WizFi310 is disconnected successfully
00095     */
00096     bool disconnect(void);
00097 
00098     /**
00099     * Get the IP address of WizFi310
00100     *
00101     * @return null-teriminated IP address or null if no IP address is assigned
00102     */
00103     const char *getIPAddress(void);
00104 
00105     /**
00106     * Get the MAC address of WizFi310
00107     *
00108     * @return null-terminated MAC address or null if no MAC address is assigned
00109     */
00110     const char *getMACAddress(void);
00111 
00112      /** Get the local gateway
00113      *
00114      *  @return         Null-terminated representation of the local gateway
00115      *                  or null if no network mask has been recieved
00116      */
00117     const char *getGateway();
00118 
00119     /** Get the local network mask
00120      *
00121      *  @return         Null-terminated representation of the local network mask 
00122      *                  or null if no network mask has been recieved
00123      */
00124     const char *getNetmask();
00125 
00126     /* Return RSSI for active connection
00127      *
00128      * @return      Measured RSSI
00129      */
00130     int8_t getRSSI();
00131 
00132     /**
00133     * Check if WizFi310 is conenected
00134     *
00135     * @return true only if the chip has an IP address
00136     */
00137     bool isConnected(void);
00138 
00139     /** Scan for available networks
00140      *
00141      * @param  ap    Pointer to allocated array to store discovered AP
00142      * @param  limit Size of allocated @a res array, or 0 to only count available AP
00143      * @return       Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
00144      *               see @a nsapi_error
00145      */
00146     int scan(WiFiAccessPoint *res, unsigned limit);
00147     
00148     /**Perform a dns query
00149     *
00150     * @param name Hostname to resolve
00151     * @param ip   Buffer to store IP address
00152     * @return 0 true on success, false on failure
00153     */
00154     bool dns_lookup(const char *name, char *ip);
00155 
00156     /**
00157     * Open a socketed connection
00158     *
00159     * @param type the type of socket to open "UDP" or "TCP"
00160     * @param id id to give the new socket, valid 0-4
00161     * @param port port to open connection with
00162     * @param addr the IP address of the destination
00163     * @return true only if socket opened successfully
00164     */
00165     bool open(const char *type, int id, const char* addr, int port);
00166 
00167     /**
00168     * Sends data to an open socket
00169     *
00170     * @param id id of socket to send to
00171     * @param data data to be sent
00172     * @param amount amount of data to be sent - max 1024
00173     * @return true only if data sent successfully
00174     */
00175     bool send(int id, const void *data, uint32_t amount);
00176 
00177     /**
00178     * Receives data from an open socket
00179     *
00180     * @param id id to receive from
00181     * @param data placeholder for returned information
00182     * @param amount number of bytes to be received
00183     * @return the number of bytes received
00184     */
00185     int32_t recv(int id, void *data, uint32_t amount);
00186 
00187     /**
00188     * Closes a socket
00189     *
00190     * @param id id of socket to close, valid only 0-4
00191     * @return true only if socket is closed successfully
00192     */
00193     bool close(int id);
00194 
00195     /**
00196     * Allows timeout to be changed between commands
00197     *
00198     * @param timeout_ms timeout of the connection
00199     */
00200     void setTimeout(uint32_t timeout_ms);
00201 
00202     /**
00203     * Checks if data is available
00204     */
00205     bool readable();
00206 
00207     /**
00208     * Checks if data can be written
00209     */
00210     bool writeable();
00211 
00212     /**
00213     * Attach a function to call whenever network state has changed
00214     *
00215     * @param func A pointer to a void function, or 0 to set as none
00216     */
00217     void attach(mbed::Callback<void()> func);
00218 
00219     /**
00220     * Attach a function to call whenever network state has changed
00221     *
00222     * @param obj pointer to the object to call the member function on
00223     * @param method pointer to the member function to call
00224     */
00225     template <typename T, typename M>
00226     void attach(T *obj, M method) {
00227         attach(mbed::Callback<void()>(obj, method));
00228     }
00229 
00230 private:
00231     mbed::UARTSerial _serial;
00232     mbed::ATCmdParser _parser;
00233 
00234     struct packet {
00235         struct packet *next;
00236         int id;
00237         uint32_t len;
00238         // data follows
00239         char *data;
00240     } *_packets, **_packets_end;
00241     void _packet_handler();
00242     //void _socket_close_handler();
00243     bool recv_ap(nsapi_wifi_ap_t *ap);
00244     nsapi_security_t str2sec(const char *str_sec);
00245     int hex_str_to_int(const char* hex_str);
00246 
00247     char _ip_buffer[16];
00248     char _gateway_buffer[16];
00249     char _netmask_buffer[16];
00250     char _mac_buffer[18];
00251     char _firmware_version[8];
00252 
00253     int  _op_mode;      // 0 : Station Mode, 1 : AP Mode
00254     bool _dhcp;
00255     uint32_t _timeout_ms;
00256 };
00257 
00258 #endif