Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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