OneNet_IoT_demo for ASC platform

Dependencies:   Common_lib ESP8266 EdpKit_lib cJSON_lib driver_mbed_HP20x driver_mbed_TH02 wifi_example

Fork of mbed-os-example-esp8266 by ESP8266

Committer:
sarahmarshy
Date:
Thu Jan 12 22:05:15 2017 +0000
Revision:
1:b4a718e62e0b
Parent:
0:b887535f68bf
Update esp8266-driver

Who changed what in which revision?

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