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