A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:a7a43371b306 1 /* ESP8266 implementation of NetworkInterfaceAPI
ram54288 0:a7a43371b306 2 * Copyright (c) 2015 ARM Limited
ram54288 0:a7a43371b306 3 *
ram54288 0:a7a43371b306 4 * Licensed under the Apache License, Version 2.0 (the "License");
ram54288 0:a7a43371b306 5 * you may not use this file except in compliance with the License.
ram54288 0:a7a43371b306 6 * You may obtain a copy of the License at
ram54288 0:a7a43371b306 7 *
ram54288 0:a7a43371b306 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:a7a43371b306 9 *
ram54288 0:a7a43371b306 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:a7a43371b306 11 * distributed under the License is distributed on an "AS IS" BASIS,
ram54288 0:a7a43371b306 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:a7a43371b306 13 * See the License for the specific language governing permissions and
ram54288 0:a7a43371b306 14 * limitations under the License.
ram54288 0:a7a43371b306 15 */
ram54288 0:a7a43371b306 16
ram54288 0:a7a43371b306 17 #ifndef ESP8266_INTERFACE_H
ram54288 0:a7a43371b306 18 #define ESP8266_INTERFACE_H
ram54288 0:a7a43371b306 19
ram54288 0:a7a43371b306 20 #include "mbed.h"
ram54288 0:a7a43371b306 21 #include "ESP8266.h"
ram54288 0:a7a43371b306 22
ram54288 0:a7a43371b306 23
ram54288 0:a7a43371b306 24 #define ESP8266_SOCKET_COUNT 5
ram54288 0:a7a43371b306 25
ram54288 0:a7a43371b306 26 /** ESP8266Interface class
ram54288 0:a7a43371b306 27 * Implementation of the NetworkStack for the ESP8266
ram54288 0:a7a43371b306 28 */
ram54288 0:a7a43371b306 29 class ESP8266Interface : public NetworkStack, public WiFiInterface
ram54288 0:a7a43371b306 30 {
ram54288 0:a7a43371b306 31 public:
ram54288 0:a7a43371b306 32 /** ESP8266Interface lifetime
ram54288 0:a7a43371b306 33 * @param tx TX pin
ram54288 0:a7a43371b306 34 * @param rx RX pin
ram54288 0:a7a43371b306 35 * @param debug Enable debugging
ram54288 0:a7a43371b306 36 */
ram54288 0:a7a43371b306 37 ESP8266Interface(PinName tx, PinName rx, bool debug = false);
ram54288 0:a7a43371b306 38
ram54288 0:a7a43371b306 39 /** Start the interface
ram54288 0:a7a43371b306 40 *
ram54288 0:a7a43371b306 41 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
ram54288 0:a7a43371b306 42 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
ram54288 0:a7a43371b306 43 *
ram54288 0:a7a43371b306 44 * @return 0 on success, negative error code on failure
ram54288 0:a7a43371b306 45 */
ram54288 0:a7a43371b306 46 virtual int connect();
ram54288 0:a7a43371b306 47
ram54288 0:a7a43371b306 48 /** Start the interface
ram54288 0:a7a43371b306 49 *
ram54288 0:a7a43371b306 50 * Attempts to connect to a WiFi network.
ram54288 0:a7a43371b306 51 *
ram54288 0:a7a43371b306 52 * @param ssid Name of the network to connect to
ram54288 0:a7a43371b306 53 * @param pass Security passphrase to connect to the network
ram54288 0:a7a43371b306 54 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
ram54288 0:a7a43371b306 55 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
ram54288 0:a7a43371b306 56 * @return 0 on success, or error code on failure
ram54288 0:a7a43371b306 57 */
ram54288 0:a7a43371b306 58 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
ram54288 0:a7a43371b306 59 uint8_t channel = 0);
ram54288 0:a7a43371b306 60
ram54288 0:a7a43371b306 61 /** Set the WiFi network credentials
ram54288 0:a7a43371b306 62 *
ram54288 0:a7a43371b306 63 * @param ssid Name of the network to connect to
ram54288 0:a7a43371b306 64 * @param pass Security passphrase to connect to the network
ram54288 0:a7a43371b306 65 * @param security Type of encryption for connection
ram54288 0:a7a43371b306 66 * (defaults to NSAPI_SECURITY_NONE)
ram54288 0:a7a43371b306 67 * @return 0 on success, or error code on failure
ram54288 0:a7a43371b306 68 */
ram54288 0:a7a43371b306 69 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
ram54288 0:a7a43371b306 70
ram54288 0:a7a43371b306 71 /** Set the WiFi network channel - NOT SUPPORTED
ram54288 0:a7a43371b306 72 *
ram54288 0:a7a43371b306 73 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
ram54288 0:a7a43371b306 74 *
ram54288 0:a7a43371b306 75 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
ram54288 0:a7a43371b306 76 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
ram54288 0:a7a43371b306 77 */
ram54288 0:a7a43371b306 78 virtual int set_channel(uint8_t channel);
ram54288 0:a7a43371b306 79
ram54288 0:a7a43371b306 80 /** Stop the interface
ram54288 0:a7a43371b306 81 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 82 */
ram54288 0:a7a43371b306 83 virtual int disconnect();
ram54288 0:a7a43371b306 84
ram54288 0:a7a43371b306 85 /** Get the internally stored IP address
ram54288 0:a7a43371b306 86 * @return IP address of the interface or null if not yet connected
ram54288 0:a7a43371b306 87 */
ram54288 0:a7a43371b306 88 virtual const char *get_ip_address();
ram54288 0:a7a43371b306 89
ram54288 0:a7a43371b306 90 /** Get the internally stored MAC address
ram54288 0:a7a43371b306 91 * @return MAC address of the interface
ram54288 0:a7a43371b306 92 */
ram54288 0:a7a43371b306 93 virtual const char *get_mac_address();
ram54288 0:a7a43371b306 94
ram54288 0:a7a43371b306 95 /** Get the local gateway
ram54288 0:a7a43371b306 96 *
ram54288 0:a7a43371b306 97 * @return Null-terminated representation of the local gateway
ram54288 0:a7a43371b306 98 * or null if no network mask has been recieved
ram54288 0:a7a43371b306 99 */
ram54288 0:a7a43371b306 100 virtual const char *get_gateway();
ram54288 0:a7a43371b306 101
ram54288 0:a7a43371b306 102 /** Get the local network mask
ram54288 0:a7a43371b306 103 *
ram54288 0:a7a43371b306 104 * @return Null-terminated representation of the local network mask
ram54288 0:a7a43371b306 105 * or null if no network mask has been recieved
ram54288 0:a7a43371b306 106 */
ram54288 0:a7a43371b306 107 virtual const char *get_netmask();
ram54288 0:a7a43371b306 108
ram54288 0:a7a43371b306 109 /** Gets the current radio signal strength for active connection
ram54288 0:a7a43371b306 110 *
ram54288 0:a7a43371b306 111 * @return Connection strength in dBm (negative value)
ram54288 0:a7a43371b306 112 */
ram54288 0:a7a43371b306 113 virtual int8_t get_rssi();
ram54288 0:a7a43371b306 114
ram54288 0:a7a43371b306 115 /** Scan for available networks
ram54288 0:a7a43371b306 116 *
ram54288 0:a7a43371b306 117 * This function will block.
ram54288 0:a7a43371b306 118 *
ram54288 0:a7a43371b306 119 * @param ap Pointer to allocated array to store discovered AP
ram54288 0:a7a43371b306 120 * @param count Size of allocated @a res array, or 0 to only count available AP
ram54288 0:a7a43371b306 121 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
ram54288 0:a7a43371b306 122 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
ram54288 0:a7a43371b306 123 * see @a nsapi_error
ram54288 0:a7a43371b306 124 */
ram54288 0:a7a43371b306 125 virtual int scan(WiFiAccessPoint *res, unsigned count);
ram54288 0:a7a43371b306 126
ram54288 0:a7a43371b306 127 /** Translates a hostname to an IP address with specific version
ram54288 0:a7a43371b306 128 *
ram54288 0:a7a43371b306 129 * The hostname may be either a domain name or an IP address. If the
ram54288 0:a7a43371b306 130 * hostname is an IP address, no network transactions will be performed.
ram54288 0:a7a43371b306 131 *
ram54288 0:a7a43371b306 132 * If no stack-specific DNS resolution is provided, the hostname
ram54288 0:a7a43371b306 133 * will be resolve using a UDP socket on the stack.
ram54288 0:a7a43371b306 134 *
ram54288 0:a7a43371b306 135 * @param address Destination for the host SocketAddress
ram54288 0:a7a43371b306 136 * @param host Hostname to resolve
ram54288 0:a7a43371b306 137 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
ram54288 0:a7a43371b306 138 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
ram54288 0:a7a43371b306 139 * @return 0 on success, negative error code on failure
ram54288 0:a7a43371b306 140 */
ram54288 0:a7a43371b306 141 using NetworkInterface::gethostbyname;
ram54288 0:a7a43371b306 142
ram54288 0:a7a43371b306 143 /** Add a domain name server to list of servers to query
ram54288 0:a7a43371b306 144 *
ram54288 0:a7a43371b306 145 * @param addr Destination for the host address
ram54288 0:a7a43371b306 146 * @return 0 on success, negative error code on failure
ram54288 0:a7a43371b306 147 */
ram54288 0:a7a43371b306 148 using NetworkInterface::add_dns_server;
ram54288 0:a7a43371b306 149
ram54288 0:a7a43371b306 150 protected:
ram54288 0:a7a43371b306 151 /** Open a socket
ram54288 0:a7a43371b306 152 * @param handle Handle in which to store new socket
ram54288 0:a7a43371b306 153 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
ram54288 0:a7a43371b306 154 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 155 */
ram54288 0:a7a43371b306 156 virtual int socket_open(void **handle, nsapi_protocol_t proto);
ram54288 0:a7a43371b306 157
ram54288 0:a7a43371b306 158 /** Close the socket
ram54288 0:a7a43371b306 159 * @param handle Socket handle
ram54288 0:a7a43371b306 160 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 161 * @note On failure, any memory associated with the socket must still
ram54288 0:a7a43371b306 162 * be cleaned up
ram54288 0:a7a43371b306 163 */
ram54288 0:a7a43371b306 164 virtual int socket_close(void *handle);
ram54288 0:a7a43371b306 165
ram54288 0:a7a43371b306 166 /** Bind a server socket to a specific port
ram54288 0:a7a43371b306 167 * @param handle Socket handle
ram54288 0:a7a43371b306 168 * @param address Local address to listen for incoming connections on
ram54288 0:a7a43371b306 169 * @return 0 on success, negative on failure.
ram54288 0:a7a43371b306 170 */
ram54288 0:a7a43371b306 171 virtual int socket_bind(void *handle, const SocketAddress &address);
ram54288 0:a7a43371b306 172
ram54288 0:a7a43371b306 173 /** Start listening for incoming connections
ram54288 0:a7a43371b306 174 * @param handle Socket handle
ram54288 0:a7a43371b306 175 * @param backlog Number of pending connections that can be queued up at any
ram54288 0:a7a43371b306 176 * one time [Default: 1]
ram54288 0:a7a43371b306 177 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 178 */
ram54288 0:a7a43371b306 179 virtual int socket_listen(void *handle, int backlog);
ram54288 0:a7a43371b306 180
ram54288 0:a7a43371b306 181 /** Connects this TCP socket to the server
ram54288 0:a7a43371b306 182 * @param handle Socket handle
ram54288 0:a7a43371b306 183 * @param address SocketAddress to connect to
ram54288 0:a7a43371b306 184 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 185 */
ram54288 0:a7a43371b306 186 virtual int socket_connect(void *handle, const SocketAddress &address);
ram54288 0:a7a43371b306 187
ram54288 0:a7a43371b306 188 /** Accept a new connection.
ram54288 0:a7a43371b306 189 * @param handle Handle in which to store new socket
ram54288 0:a7a43371b306 190 * @param server Socket handle to server to accept from
ram54288 0:a7a43371b306 191 * @return 0 on success, negative on failure
ram54288 0:a7a43371b306 192 * @note This call is not-blocking, if this call would block, must
ram54288 0:a7a43371b306 193 * immediately return NSAPI_ERROR_WOULD_WAIT
ram54288 0:a7a43371b306 194 */
ram54288 0:a7a43371b306 195 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
ram54288 0:a7a43371b306 196
ram54288 0:a7a43371b306 197 /** Send data to the remote host
ram54288 0:a7a43371b306 198 * @param handle Socket handle
ram54288 0:a7a43371b306 199 * @param data The buffer to send to the host
ram54288 0:a7a43371b306 200 * @param size The length of the buffer to send
ram54288 0:a7a43371b306 201 * @return Number of written bytes on success, negative on failure
ram54288 0:a7a43371b306 202 * @note This call is not-blocking, if this call would block, must
ram54288 0:a7a43371b306 203 * immediately return NSAPI_ERROR_WOULD_WAIT
ram54288 0:a7a43371b306 204 */
ram54288 0:a7a43371b306 205 virtual int socket_send(void *handle, const void *data, unsigned size);
ram54288 0:a7a43371b306 206
ram54288 0:a7a43371b306 207 /** Receive data from the remote host
ram54288 0:a7a43371b306 208 * @param handle Socket handle
ram54288 0:a7a43371b306 209 * @param data The buffer in which to store the data received from the host
ram54288 0:a7a43371b306 210 * @param size The maximum length of the buffer
ram54288 0:a7a43371b306 211 * @return Number of received bytes on success, negative on failure
ram54288 0:a7a43371b306 212 * @note This call is not-blocking, if this call would block, must
ram54288 0:a7a43371b306 213 * immediately return NSAPI_ERROR_WOULD_WAIT
ram54288 0:a7a43371b306 214 */
ram54288 0:a7a43371b306 215 virtual int socket_recv(void *handle, void *data, unsigned size);
ram54288 0:a7a43371b306 216
ram54288 0:a7a43371b306 217 /** Send a packet to a remote endpoint
ram54288 0:a7a43371b306 218 * @param handle Socket handle
ram54288 0:a7a43371b306 219 * @param address The remote SocketAddress
ram54288 0:a7a43371b306 220 * @param data The packet to be sent
ram54288 0:a7a43371b306 221 * @param size The length of the packet to be sent
ram54288 0:a7a43371b306 222 * @return The number of written bytes on success, negative on failure
ram54288 0:a7a43371b306 223 * @note This call is not-blocking, if this call would block, must
ram54288 0:a7a43371b306 224 * immediately return NSAPI_ERROR_WOULD_WAIT
ram54288 0:a7a43371b306 225 */
ram54288 0:a7a43371b306 226 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
ram54288 0:a7a43371b306 227
ram54288 0:a7a43371b306 228 /** Receive a packet from a remote endpoint
ram54288 0:a7a43371b306 229 * @param handle Socket handle
ram54288 0:a7a43371b306 230 * @param address Destination for the remote SocketAddress or null
ram54288 0:a7a43371b306 231 * @param buffer The buffer for storing the incoming packet data
ram54288 0:a7a43371b306 232 * If a packet is too long to fit in the supplied buffer,
ram54288 0:a7a43371b306 233 * excess bytes are discarded
ram54288 0:a7a43371b306 234 * @param size The length of the buffer
ram54288 0:a7a43371b306 235 * @return The number of received bytes on success, negative on failure
ram54288 0:a7a43371b306 236 * @note This call is not-blocking, if this call would block, must
ram54288 0:a7a43371b306 237 * immediately return NSAPI_ERROR_WOULD_WAIT
ram54288 0:a7a43371b306 238 */
ram54288 0:a7a43371b306 239 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
ram54288 0:a7a43371b306 240
ram54288 0:a7a43371b306 241 /** Register a callback on state change of the socket
ram54288 0:a7a43371b306 242 * @param handle Socket handle
ram54288 0:a7a43371b306 243 * @param callback Function to call on state change
ram54288 0:a7a43371b306 244 * @param data Argument to pass to callback
ram54288 0:a7a43371b306 245 * @note Callback may be called in an interrupt context.
ram54288 0:a7a43371b306 246 */
ram54288 0:a7a43371b306 247 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
ram54288 0:a7a43371b306 248
ram54288 0:a7a43371b306 249 /** Provide access to the NetworkStack object
ram54288 0:a7a43371b306 250 *
ram54288 0:a7a43371b306 251 * @return The underlying NetworkStack object
ram54288 0:a7a43371b306 252 */
ram54288 0:a7a43371b306 253 virtual NetworkStack *get_stack()
ram54288 0:a7a43371b306 254 {
ram54288 0:a7a43371b306 255 return this;
ram54288 0:a7a43371b306 256 }
ram54288 0:a7a43371b306 257
ram54288 0:a7a43371b306 258 private:
ram54288 0:a7a43371b306 259 ESP8266 _esp;
ram54288 0:a7a43371b306 260 bool _ids[ESP8266_SOCKET_COUNT];
ram54288 0:a7a43371b306 261
ram54288 0:a7a43371b306 262 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
ram54288 0:a7a43371b306 263 nsapi_security_t ap_sec;
ram54288 0:a7a43371b306 264 uint8_t ap_ch;
ram54288 0:a7a43371b306 265 char ap_pass[64]; /* The longest allowed passphrase */
ram54288 0:a7a43371b306 266
ram54288 0:a7a43371b306 267 void event();
ram54288 0:a7a43371b306 268
ram54288 0:a7a43371b306 269 struct {
ram54288 0:a7a43371b306 270 void (*callback)(void *);
ram54288 0:a7a43371b306 271 void *data;
ram54288 0:a7a43371b306 272 } _cbs[ESP8266_SOCKET_COUNT];
ram54288 0:a7a43371b306 273 };
ram54288 0:a7a43371b306 274
ram54288 0:a7a43371b306 275 #endif