Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

ESP8266.h

00001 /* ESP8266Interface Example
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 ESP8266_H
00018 #define ESP8266_H
00019 
00020 #include "ATParser.h"
00021 
00022 /** ESP8266Interface class.
00023     This is an interface to a ESP8266 radio.
00024  */
00025 class ESP8266
00026 {
00027 public:
00028     ESP8266(PinName tx, PinName rx, bool debug=false);
00029 
00030     /**
00031     * Startup the ESP8266
00032     *
00033     * @param mode mode of WIFI 1-client, 2-host, 3-both
00034     * @return true only if ESP8266 was setup correctly
00035     */
00036     bool startup(int mode);
00037 
00038     /**
00039     * Reset ESP8266
00040     *
00041     * @return true only if ESP8266 resets successfully
00042     */
00043     bool reset(void);
00044 
00045     /**
00046     * Enable/Disable DHCP
00047     *
00048     * @param enabled DHCP enabled when true
00049     * @param mode mode of DHCP 0-softAP, 1-station, 2-both
00050     * @return true only if ESP8266 enables/disables DHCP successfully
00051     */
00052     bool dhcp(bool enabled, int mode);
00053 
00054     /**
00055     * Connect ESP8266 to AP
00056     *
00057     * @param ap the name of the AP
00058     * @param passPhrase the password of AP
00059     * @return true only if ESP8266 is connected successfully
00060     */
00061     bool connect(const char *ap, const char *passPhrase);
00062 
00063     /**
00064     * Disconnect ESP8266 from AP
00065     *
00066     * @return true only if ESP8266 is disconnected successfully
00067     */
00068     bool disconnect(void);
00069 
00070     /**
00071     * Get the IP address of ESP8266
00072     *
00073     * @return null-teriminated IP address or null if no IP address is assigned
00074     */
00075     const char *getIPAddress(void);
00076 
00077     /**
00078     * Get the MAC address of ESP8266
00079     *
00080     * @return null-terminated MAC address or null if no MAC address is assigned
00081     */
00082     const char *getMACAddress(void);
00083 
00084      /** Get the local gateway
00085      *
00086      *  @return         Null-terminated representation of the local gateway
00087      *                  or null if no network mask has been recieved
00088      */
00089     const char *getGateway();
00090 
00091     /** Get the local network mask
00092      *
00093      *  @return         Null-terminated representation of the local network mask 
00094      *                  or null if no network mask has been recieved
00095      */
00096     const char *getNetmask();
00097 
00098     /* Return RSSI for active connection
00099      *
00100      * @return      Measured RSSI
00101      */
00102     int8_t getRSSI();
00103 
00104     /**
00105     * Check if ESP8266 is conenected
00106     *
00107     * @return true only if the chip has an IP address
00108     */
00109     bool isConnected(void);
00110 
00111     /** Scan for available networks
00112      *
00113      * @param  ap    Pointer to allocated array to store discovered AP
00114      * @param  limit Size of allocated @a res array, or 0 to only count available AP
00115      * @return       Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
00116      *               see @a nsapi_error
00117      */
00118     int scan(WiFiAccessPoint *res, unsigned limit);
00119 
00120     /**
00121     * Open a socketed connection
00122     *
00123     * @param type the type of socket to open "UDP" or "TCP"
00124     * @param id id to give the new socket, valid 0-4
00125     * @param port port to open connection with
00126     * @param addr the IP address of the destination
00127     * @return true only if socket opened successfully
00128     */
00129     bool open(const char *type, int id, const char* addr, int port);
00130 
00131     /**
00132     * Sends data to an open socket
00133     *
00134     * @param id id of socket to send to
00135     * @param data data to be sent
00136     * @param amount amount of data to be sent - max 1024
00137     * @return true only if data sent successfully
00138     */
00139     bool send(int id, const void *data, uint32_t amount);
00140 
00141     /**
00142     * Receives data from an open socket
00143     *
00144     * @param id id to receive from
00145     * @param data placeholder for returned information
00146     * @param amount number of bytes to be received
00147     * @return the number of bytes received
00148     */
00149     int32_t recv(int id, void *data, uint32_t amount);
00150 
00151     /**
00152     * Closes a socket
00153     *
00154     * @param id id of socket to close, valid only 0-4
00155     * @return true only if socket is closed successfully
00156     */
00157     bool close(int id);
00158 
00159     /**
00160     * Allows timeout to be changed between commands
00161     *
00162     * @param timeout_ms timeout of the connection
00163     */
00164     void setTimeout(uint32_t timeout_ms);
00165 
00166     /**
00167     * Checks if data is available
00168     */
00169     bool readable();
00170 
00171     /**
00172     * Checks if data can be written
00173     */
00174     bool writeable();
00175 
00176     /**
00177     * Attach a function to call whenever network state has changed
00178     *
00179     * @param func A pointer to a void function, or 0 to set as none
00180     */
00181     void attach(Callback<void()> func);
00182 
00183     /**
00184     * Attach a function to call whenever network state has changed
00185     *
00186     * @param obj pointer to the object to call the member function on
00187     * @param method pointer to the member function to call
00188     */
00189     template <typename T, typename M>
00190     void attach(T *obj, M method) {
00191         attach(Callback<void()>(obj, method));
00192     }
00193 
00194 private:
00195     BufferedSerial _serial;
00196     ATParser _parser;
00197 
00198     struct packet {
00199         struct packet *next;
00200         int id;
00201         uint32_t len;
00202         // data follows
00203     } *_packets, **_packets_end;
00204     void _packet_handler();
00205     bool recv_ap(nsapi_wifi_ap_t *ap);
00206 
00207     char _ip_buffer[16];
00208     char _gateway_buffer[16];
00209     char _netmask_buffer[16];
00210     char _mac_buffer[18];
00211 };
00212 
00213 #endif