add printf standby

Fork of esp8266-drive by Her Smidt

Committer:
Zorgino
Date:
Fri Jun 23 10:33:44 2017 +0000
Revision:
3:8d9d75e2fb5f
Parent:
0:6946b0b9e323
wef

Who changed what in which revision?

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