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