Advantech / Mbed OS pelion-example-common
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP32.h Source File

ESP32.h

00001 /* ESP32Interface Example
00002  * Copyright (c) 2015 ARM Limited
00003  * Copyright (c) 2017 Renesas Electronics Corporation
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef ESP32_H
00019 #define ESP32_H
00020 
00021 #if DEVICE_SERIAL && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
00022 #include <vector>
00023 #include <stdint.h>
00024 #include <stdlib.h>
00025 #include "drivers/DigitalOut.h"
00026 #include "drivers/SerialBase.h"
00027 #include "drivers/UARTSerial.h"
00028 #include "features/netsocket/nsapi_types.h"
00029 #include "features/netsocket/WiFiAccessPoint.h"
00030 #include "PinNames.h"
00031 #include "platform/ATCmdParser.h"
00032 #include "platform/Callback.h"
00033 #include "platform/mbed_error.h"
00034 #include "rtos/Mutex.h"
00035 #include "rtos/ThisThread.h"
00036 
00037 #ifndef ESP32_CONNECT_TIMEOUT
00038 #define ESP32_CONNECT_TIMEOUT 15000
00039 #endif
00040 #ifndef ESP32_SEND_TIMEOUT
00041 #define ESP32_SEND_TIMEOUT    2000
00042 #endif
00043 #ifndef ESP32_RECV_TIMEOUT
00044 #define ESP32_RECV_TIMEOUT    2000
00045 #endif
00046 #ifndef ESP32_MISC_TIMEOUT
00047 #define ESP32_MISC_TIMEOUT    2000
00048 #endif
00049 
00050 /** ESP32Interface class.
00051     This is an interface to a ESP32 radio.
00052  */
00053 class ESP32
00054 {
00055 public:
00056     /**
00057     * Static method to create or retrieve the single ESP32 instance
00058     */
00059     static ESP32 * getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
00060                                 PinName rts, PinName cts, int baudrate);
00061 
00062     ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
00063           PinName rts, PinName cts, int baudrate);
00064 
00065     /**
00066     * Check firmware version of ESP8266
00067     *
00068     * @return integer firmware version or -1 if firmware query command gives outdated response
00069     */
00070     int get_firmware_version(void);
00071 
00072     /**
00073     * Sets the Wi-Fi Mode
00074     *
00075     * @param mode mode of WIFI 1-client, 2-host, 3-both
00076     * @return true only if ESP32 was setup correctly
00077     */
00078     bool set_mode(int mode);
00079 
00080     /**
00081     * Enable/Disable DHCP
00082     *
00083     * @param enabled DHCP enabled when true
00084     * @param mode mode of DHCP 0-softAP, 1-station, 2-both
00085     * @return true only if ESP32 enables/disables DHCP successfully
00086     */
00087     bool dhcp(bool enabled, int mode);
00088 
00089     /**
00090     * Connect ESP32 to AP
00091     *
00092     * @param ap the name of the AP
00093     * @param passPhrase the password of AP
00094     * @return true only if ESP32 is connected successfully
00095     */
00096     bool connect(const char *ap, const char *passPhrase);
00097 
00098     /**
00099     * Disconnect ESP32 from AP
00100     *
00101     * @return true only if ESP32 is disconnected successfully
00102     */
00103     bool disconnect(void);
00104 
00105     /**
00106     * Get the IP address of ESP32
00107     *
00108     * @return null-teriminated IP address or null if no IP address is assigned
00109     */
00110     const char *getIPAddress(void);
00111     const char *getIPAddress_ap(void);
00112 
00113     /**
00114     * Get the MAC address of ESP32
00115     *
00116     * @return null-terminated MAC address or null if no MAC address is assigned
00117     */
00118     const char *getMACAddress(void);
00119     const char *getMACAddress_ap(void);
00120 
00121      /** Get the local gateway
00122      *
00123      *  @return         Null-terminated representation of the local gateway
00124      *                  or null if no network mask has been recieved
00125      */
00126     const char *getGateway();
00127     const char *getGateway_ap();
00128 
00129     /** Get the local network mask
00130      *
00131      *  @return         Null-terminated representation of the local network mask 
00132      *                  or null if no network mask has been recieved
00133      */
00134     const char *getNetmask();
00135     const char *getNetmask_ap();
00136 
00137     /* Return RSSI for active connection
00138      *
00139      * @return      Measured RSSI
00140      */
00141     int8_t getRSSI();
00142 
00143     /**
00144     * Check if ESP32 is conenected
00145     *
00146     * @return true only if the chip has an IP address
00147     */
00148     bool isConnected(void);
00149 
00150     /** Scan for available networks
00151      *
00152      * @param  ap    Pointer to allocated array to store discovered AP
00153      * @param  limit Size of allocated @a res array, or 0 to only count available AP
00154      * @return       Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
00155      *               see @a nsapi_error
00156      */
00157     int scan(WiFiAccessPoint *res, unsigned limit);
00158 
00159     /**
00160     * Open a socketed connection
00161     *
00162     * @param type the type of socket to open "UDP" or "TCP"
00163     * @param id id to give the new socket, valid 0-4
00164     * @param port port to open connection with
00165     * @param addr the IP address of the destination
00166     * @param addr the IP address of the destination
00167     * @param opt  type=" UDP" : UDP socket's local port, zero means any
00168     *             type=" TCP" : TCP connection's keep alive time, zero means disabled
00169     * @return true only if socket opened successfully
00170     */
00171     bool open(const char *type, int id, const char* addr, int port, int opt = 0);
00172 
00173     /**
00174     * Sends data to an open socket
00175     *
00176     * @param id id of socket to send to
00177     * @param data data to be sent
00178     * @param amount amount of data to be sent - max 1024
00179     * @return true only if data sent successfully
00180     */
00181     bool send(int id, const void *data, uint32_t amount);
00182 
00183     /**
00184     * Receives data from an open socket
00185     *
00186     * @param id id to receive from
00187     * @param data placeholder for returned information
00188     * @param amount number of bytes to be received
00189     * @return the number of bytes received
00190     */
00191     int32_t recv(int id, void *data, uint32_t amount, uint32_t timeout = ESP32_RECV_TIMEOUT);
00192 
00193     /**
00194     * Closes a socket
00195     *
00196     * @param id id of socket to close, valid only 0-4
00197     * @param wait_close 
00198     * @return true only if socket is closed successfully
00199     */
00200     bool close(int id, bool wait_close = false);
00201 
00202     /**
00203     * Allows timeout to be changed between commands
00204     *
00205     * @param timeout_ms timeout of the connection
00206     */
00207     void setTimeout(uint32_t timeout_ms = ESP32_MISC_TIMEOUT);
00208 
00209     /**
00210     * Checks if data is available
00211     */
00212     bool readable();
00213 
00214     /**
00215     * Checks if data can be written
00216     */
00217     bool writeable();
00218 
00219     void socket_attach(int id, void (*callback)(void *), void *data);
00220     int get_free_id();
00221 
00222     bool config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn);
00223 
00224     bool restart();
00225     bool get_ssid(char *ap);
00226     bool cre_server(int port);
00227     bool del_server();
00228     bool accept(int * p_id);
00229 
00230     bool set_network(const char *ip_address, const char *netmask, const char *gateway);
00231     bool set_network_ap(const char *ip_address, const char *netmask, const char *gateway);
00232 
00233     /**
00234     * Attach a function to call whenever network state has changed
00235     *
00236     * @param func A pointer to a void function, or 0 to set as none
00237     */
00238     void attach_wifi_status(mbed::Callback<void(int8_t)> status_cb);
00239 
00240     /** Get the connection status
00241      *
00242      *  @return         The connection status according to ConnectionStatusType
00243      */
00244     int8_t get_wifi_status() const;
00245 
00246     static const int8_t WIFIMODE_STATION = 1;
00247     static const int8_t WIFIMODE_SOFTAP = 2;
00248     static const int8_t WIFIMODE_STATION_SOFTAP = 3;
00249     static const int8_t SOCKET_COUNT = 5;
00250 
00251     static const int8_t STATUS_DISCONNECTED = 0;
00252     static const int8_t STATUS_CONNECTED = 1;
00253     static const int8_t STATUS_GOT_IP = 2;
00254 
00255 private:
00256     mbed::DigitalOut * _p_wifi_en;
00257     mbed::DigitalOut * _p_wifi_io0;
00258     bool init_end;
00259     mbed::UARTSerial _serial;
00260     mbed::ATCmdParser _parser;
00261     struct packet {
00262         struct packet *next;
00263         int id;
00264         uint32_t len;
00265         uint32_t index;
00266         // data follows
00267     } *_packets, **_packets_end;
00268     int _wifi_mode;
00269     int _baudrate;
00270     PinName _rts;
00271     PinName _cts;
00272     int _flow_control;
00273     uint32_t last_timeout_ms;
00274 
00275     std::vector<int> _accept_id;
00276     uint32_t _id_bits;
00277     uint32_t _id_bits_close;
00278     bool _server_act;
00279     rtos::Mutex _smutex; // Protect serial port access
00280     static ESP32 * instESP32;
00281     int8_t _wifi_status;
00282     mbed::Callback<void(int8_t)> _wifi_status_cb;
00283 
00284     bool _ids[SOCKET_COUNT];
00285     struct {
00286         void (*callback)(void *);
00287         void *data;
00288         int  Notified;
00289     } _cbs[SOCKET_COUNT];
00290 
00291     bool startup();
00292     bool reset(void);
00293     void debugOn(bool debug);
00294     void socket_handler(bool connect, int id);
00295     void _connect_handler_0();
00296     void _connect_handler_1();
00297     void _connect_handler_2();
00298     void _connect_handler_3();
00299     void _connect_handler_4();
00300     void _closed_handler_0();
00301     void _closed_handler_1();
00302     void _closed_handler_2();
00303     void _closed_handler_3();
00304     void _closed_handler_4();
00305     void _connection_status_handler();
00306     void _packet_handler();
00307     void _clear_socket_packets(int id);
00308     void event();
00309     bool recv_ap(nsapi_wifi_ap_t *ap);
00310 
00311     char _ip_buffer[16];
00312     char _gateway_buffer[16];
00313     char _netmask_buffer[16];
00314     char _mac_buffer[18];
00315 
00316     char _ip_buffer_ap[16];
00317     char _gateway_buffer_ap[16];
00318     char _netmask_buffer_ap[16];
00319     char _mac_buffer_ap[18];
00320 };
00321 #endif
00322 #endif