FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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