Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:615f90842ce8 1 /* ESP8266Interface Example
MACRUM 0:615f90842ce8 2 * Copyright (c) 2015 ARM Limited
MACRUM 0:615f90842ce8 3 *
MACRUM 0:615f90842ce8 4 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:615f90842ce8 5 * you may not use this file except in compliance with the License.
MACRUM 0:615f90842ce8 6 * You may obtain a copy of the License at
MACRUM 0:615f90842ce8 7 *
MACRUM 0:615f90842ce8 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:615f90842ce8 9 *
MACRUM 0:615f90842ce8 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:615f90842ce8 11 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:615f90842ce8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:615f90842ce8 13 * See the License for the specific language governing permissions and
MACRUM 0:615f90842ce8 14 * limitations under the License.
MACRUM 0:615f90842ce8 15 */
MACRUM 0:615f90842ce8 16
MACRUM 0:615f90842ce8 17 #ifndef ESP8266_H
MACRUM 0:615f90842ce8 18 #define ESP8266_H
MACRUM 0:615f90842ce8 19
MACRUM 0:615f90842ce8 20 #include "ATParser.h"
MACRUM 0:615f90842ce8 21
MACRUM 0:615f90842ce8 22 /** ESP8266Interface class.
MACRUM 0:615f90842ce8 23 This is an interface to a ESP8266 radio.
MACRUM 0:615f90842ce8 24 */
MACRUM 0:615f90842ce8 25 class ESP8266
MACRUM 0:615f90842ce8 26 {
MACRUM 0:615f90842ce8 27 public:
MACRUM 0:615f90842ce8 28 ESP8266(PinName tx, PinName rx, bool debug=false);
MACRUM 0:615f90842ce8 29
MACRUM 0:615f90842ce8 30 /**
MACRUM 0:615f90842ce8 31 * Check firmware version of ESP8266
MACRUM 0:615f90842ce8 32 *
MACRUM 0:615f90842ce8 33 * @return integer firmware version or -1 if firmware query command gives outdated response
MACRUM 0:615f90842ce8 34 */
MACRUM 0:615f90842ce8 35 int get_firmware_version(void);
MACRUM 0:615f90842ce8 36
MACRUM 0:615f90842ce8 37 /**
MACRUM 0:615f90842ce8 38 * Startup the ESP8266
MACRUM 0:615f90842ce8 39 *
MACRUM 0:615f90842ce8 40 * @param mode mode of WIFI 1-client, 2-host, 3-both
MACRUM 0:615f90842ce8 41 * @return true only if ESP8266 was setup correctly
MACRUM 0:615f90842ce8 42 */
MACRUM 0:615f90842ce8 43 bool startup(int mode);
MACRUM 0:615f90842ce8 44
MACRUM 0:615f90842ce8 45 /**
MACRUM 0:615f90842ce8 46 * Reset ESP8266
MACRUM 0:615f90842ce8 47 *
MACRUM 0:615f90842ce8 48 * @return true only if ESP8266 resets successfully
MACRUM 0:615f90842ce8 49 */
MACRUM 0:615f90842ce8 50 bool reset(void);
MACRUM 0:615f90842ce8 51
MACRUM 0:615f90842ce8 52 /**
MACRUM 0:615f90842ce8 53 * Enable/Disable DHCP
MACRUM 0:615f90842ce8 54 *
MACRUM 0:615f90842ce8 55 * @param enabled DHCP enabled when true
MACRUM 0:615f90842ce8 56 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
MACRUM 0:615f90842ce8 57 * @return true only if ESP8266 enables/disables DHCP successfully
MACRUM 0:615f90842ce8 58 */
MACRUM 0:615f90842ce8 59 bool dhcp(bool enabled, int mode);
MACRUM 0:615f90842ce8 60
MACRUM 0:615f90842ce8 61 /**
MACRUM 0:615f90842ce8 62 * Connect ESP8266 to AP
MACRUM 0:615f90842ce8 63 *
MACRUM 0:615f90842ce8 64 * @param ap the name of the AP
MACRUM 0:615f90842ce8 65 * @param passPhrase the password of AP
MACRUM 0:615f90842ce8 66 * @return true only if ESP8266 is connected successfully
MACRUM 0:615f90842ce8 67 */
MACRUM 0:615f90842ce8 68 bool connect(const char *ap, const char *passPhrase);
MACRUM 0:615f90842ce8 69
MACRUM 0:615f90842ce8 70 /**
MACRUM 0:615f90842ce8 71 * Disconnect ESP8266 from AP
MACRUM 0:615f90842ce8 72 *
MACRUM 0:615f90842ce8 73 * @return true only if ESP8266 is disconnected successfully
MACRUM 0:615f90842ce8 74 */
MACRUM 0:615f90842ce8 75 bool disconnect(void);
MACRUM 0:615f90842ce8 76
MACRUM 0:615f90842ce8 77 /**
MACRUM 0:615f90842ce8 78 * Get the IP address of ESP8266
MACRUM 0:615f90842ce8 79 *
MACRUM 0:615f90842ce8 80 * @return null-teriminated IP address or null if no IP address is assigned
MACRUM 0:615f90842ce8 81 */
MACRUM 0:615f90842ce8 82 const char *getIPAddress(void);
MACRUM 0:615f90842ce8 83
MACRUM 0:615f90842ce8 84 /**
MACRUM 0:615f90842ce8 85 * Get the MAC address of ESP8266
MACRUM 0:615f90842ce8 86 *
MACRUM 0:615f90842ce8 87 * @return null-terminated MAC address or null if no MAC address is assigned
MACRUM 0:615f90842ce8 88 */
MACRUM 0:615f90842ce8 89 const char *getMACAddress(void);
MACRUM 0:615f90842ce8 90
MACRUM 0:615f90842ce8 91 /** Get the local gateway
MACRUM 0:615f90842ce8 92 *
MACRUM 0:615f90842ce8 93 * @return Null-terminated representation of the local gateway
MACRUM 0:615f90842ce8 94 * or null if no network mask has been recieved
MACRUM 0:615f90842ce8 95 */
MACRUM 0:615f90842ce8 96 const char *getGateway();
MACRUM 0:615f90842ce8 97
MACRUM 0:615f90842ce8 98 /** Get the local network mask
MACRUM 0:615f90842ce8 99 *
MACRUM 0:615f90842ce8 100 * @return Null-terminated representation of the local network mask
MACRUM 0:615f90842ce8 101 * or null if no network mask has been recieved
MACRUM 0:615f90842ce8 102 */
MACRUM 0:615f90842ce8 103 const char *getNetmask();
MACRUM 0:615f90842ce8 104
MACRUM 0:615f90842ce8 105 /* Return RSSI for active connection
MACRUM 0:615f90842ce8 106 *
MACRUM 0:615f90842ce8 107 * @return Measured RSSI
MACRUM 0:615f90842ce8 108 */
MACRUM 0:615f90842ce8 109 int8_t getRSSI();
MACRUM 0:615f90842ce8 110
MACRUM 0:615f90842ce8 111 /**
MACRUM 0:615f90842ce8 112 * Check if ESP8266 is conenected
MACRUM 0:615f90842ce8 113 *
MACRUM 0:615f90842ce8 114 * @return true only if the chip has an IP address
MACRUM 0:615f90842ce8 115 */
MACRUM 0:615f90842ce8 116 bool isConnected(void);
MACRUM 0:615f90842ce8 117
MACRUM 0:615f90842ce8 118 /** Scan for available networks
MACRUM 0:615f90842ce8 119 *
MACRUM 0:615f90842ce8 120 * @param ap Pointer to allocated array to store discovered AP
MACRUM 0:615f90842ce8 121 * @param limit Size of allocated @a res array, or 0 to only count available AP
MACRUM 0:615f90842ce8 122 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
MACRUM 0:615f90842ce8 123 * see @a nsapi_error
MACRUM 0:615f90842ce8 124 */
MACRUM 0:615f90842ce8 125 int scan(WiFiAccessPoint *res, unsigned limit);
MACRUM 0:615f90842ce8 126
MACRUM 0:615f90842ce8 127 /**Perform a dns query
MACRUM 0:615f90842ce8 128 *
MACRUM 0:615f90842ce8 129 * @param name Hostname to resolve
MACRUM 0:615f90842ce8 130 * @param ip Buffer to store IP address
MACRUM 0:615f90842ce8 131 * @return 0 true on success, false on failure
MACRUM 0:615f90842ce8 132 */
MACRUM 0:615f90842ce8 133 bool dns_lookup(const char *name, char *ip);
MACRUM 0:615f90842ce8 134
MACRUM 0:615f90842ce8 135 /**
MACRUM 0:615f90842ce8 136 * Open a socketed connection
MACRUM 0:615f90842ce8 137 *
MACRUM 0:615f90842ce8 138 * @param type the type of socket to open "UDP" or "TCP"
MACRUM 0:615f90842ce8 139 * @param id id to give the new socket, valid 0-4
MACRUM 0:615f90842ce8 140 * @param port port to open connection with
MACRUM 0:615f90842ce8 141 * @param addr the IP address of the destination
MACRUM 0:615f90842ce8 142 * @return true only if socket opened successfully
MACRUM 0:615f90842ce8 143 */
MACRUM 0:615f90842ce8 144 bool open(const char *type, int id, const char* addr, int port);
MACRUM 0:615f90842ce8 145
MACRUM 0:615f90842ce8 146 /**
MACRUM 0:615f90842ce8 147 * Sends data to an open socket
MACRUM 0:615f90842ce8 148 *
MACRUM 0:615f90842ce8 149 * @param id id of socket to send to
MACRUM 0:615f90842ce8 150 * @param data data to be sent
MACRUM 0:615f90842ce8 151 * @param amount amount of data to be sent - max 1024
MACRUM 0:615f90842ce8 152 * @return true only if data sent successfully
MACRUM 0:615f90842ce8 153 */
MACRUM 0:615f90842ce8 154 bool send(int id, const void *data, uint32_t amount);
MACRUM 0:615f90842ce8 155
MACRUM 0:615f90842ce8 156 /**
MACRUM 0:615f90842ce8 157 * Receives data from an open socket
MACRUM 0:615f90842ce8 158 *
MACRUM 0:615f90842ce8 159 * @param id id to receive from
MACRUM 0:615f90842ce8 160 * @param data placeholder for returned information
MACRUM 0:615f90842ce8 161 * @param amount number of bytes to be received
MACRUM 0:615f90842ce8 162 * @return the number of bytes received
MACRUM 0:615f90842ce8 163 */
MACRUM 0:615f90842ce8 164 int32_t recv(int id, void *data, uint32_t amount);
MACRUM 0:615f90842ce8 165
MACRUM 0:615f90842ce8 166 /**
MACRUM 0:615f90842ce8 167 * Closes a socket
MACRUM 0:615f90842ce8 168 *
MACRUM 0:615f90842ce8 169 * @param id id of socket to close, valid only 0-4
MACRUM 0:615f90842ce8 170 * @return true only if socket is closed successfully
MACRUM 0:615f90842ce8 171 */
MACRUM 0:615f90842ce8 172 bool close(int id);
MACRUM 0:615f90842ce8 173
MACRUM 0:615f90842ce8 174 /**
MACRUM 0:615f90842ce8 175 * Allows timeout to be changed between commands
MACRUM 0:615f90842ce8 176 *
MACRUM 0:615f90842ce8 177 * @param timeout_ms timeout of the connection
MACRUM 0:615f90842ce8 178 */
MACRUM 0:615f90842ce8 179 void setTimeout(uint32_t timeout_ms);
MACRUM 0:615f90842ce8 180
MACRUM 0:615f90842ce8 181 /**
MACRUM 0:615f90842ce8 182 * Checks if data is available
MACRUM 0:615f90842ce8 183 */
MACRUM 0:615f90842ce8 184 bool readable();
MACRUM 0:615f90842ce8 185
MACRUM 0:615f90842ce8 186 /**
MACRUM 0:615f90842ce8 187 * Checks if data can be written
MACRUM 0:615f90842ce8 188 */
MACRUM 0:615f90842ce8 189 bool writeable();
MACRUM 0:615f90842ce8 190
MACRUM 0:615f90842ce8 191 /**
MACRUM 0:615f90842ce8 192 * Attach a function to call whenever network state has changed
MACRUM 0:615f90842ce8 193 *
MACRUM 0:615f90842ce8 194 * @param func A pointer to a void function, or 0 to set as none
MACRUM 0:615f90842ce8 195 */
MACRUM 0:615f90842ce8 196 void attach(Callback<void()> func);
MACRUM 0:615f90842ce8 197
MACRUM 0:615f90842ce8 198 /**
MACRUM 0:615f90842ce8 199 * Attach a function to call whenever network state has changed
MACRUM 0:615f90842ce8 200 *
MACRUM 0:615f90842ce8 201 * @param obj pointer to the object to call the member function on
MACRUM 0:615f90842ce8 202 * @param method pointer to the member function to call
MACRUM 0:615f90842ce8 203 */
MACRUM 0:615f90842ce8 204 template <typename T, typename M>
MACRUM 0:615f90842ce8 205 void attach(T *obj, M method) {
MACRUM 0:615f90842ce8 206 attach(Callback<void()>(obj, method));
MACRUM 0:615f90842ce8 207 }
MACRUM 0:615f90842ce8 208
MACRUM 0:615f90842ce8 209 private:
MACRUM 0:615f90842ce8 210 BufferedSerial _serial;
MACRUM 0:615f90842ce8 211 ATParser _parser;
MACRUM 0:615f90842ce8 212
MACRUM 0:615f90842ce8 213 struct packet {
MACRUM 0:615f90842ce8 214 struct packet *next;
MACRUM 0:615f90842ce8 215 int id;
MACRUM 0:615f90842ce8 216 uint32_t len;
MACRUM 0:615f90842ce8 217 // data follows
MACRUM 0:615f90842ce8 218 } *_packets, **_packets_end;
MACRUM 0:615f90842ce8 219 void _packet_handler();
MACRUM 0:615f90842ce8 220 bool recv_ap(nsapi_wifi_ap_t *ap);
MACRUM 0:615f90842ce8 221
MACRUM 0:615f90842ce8 222 char _ip_buffer[16];
MACRUM 0:615f90842ce8 223 char _gateway_buffer[16];
MACRUM 0:615f90842ce8 224 char _netmask_buffer[16];
MACRUM 0:615f90842ce8 225 char _mac_buffer[18];
MACRUM 0:615f90842ce8 226 };
MACRUM 0:615f90842ce8 227
MACRUM 0:615f90842ce8 228 #endif