a

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
hydroguy45
Date:
Fri Nov 01 18:52:45 2019 +0000
Revision:
12:ea030e3181d3
Parent:
7:ca226305d28b
Cleaned up the header

Who changed what in which revision?

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