Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ESP8266
Fork of ESP8266Interface by
ESP8266Interface.h@55:c0808849cb89, 2016-04-06 (annotated)
- Committer:
- geky
- Date:
- Wed Apr 06 13:49:41 2016 +0000
- Revision:
- 55:c0808849cb89
- Parent:
- 53:8ded612adb96
Matched changes to NSAPI
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| geky | 49:750ed1b67483 | 1 | /* ESP8266 implementation of NetworkInterfaceAPI | 
| sarahmarshy | 18:9fc7976c7b27 | 2 | * Copyright (c) 2015 ARM Limited | 
| sarahmarshy | 18:9fc7976c7b27 | 3 | * | 
| sarahmarshy | 18:9fc7976c7b27 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| sarahmarshy | 18:9fc7976c7b27 | 5 | * you may not use this file except in compliance with the License. | 
| sarahmarshy | 18:9fc7976c7b27 | 6 | * You may obtain a copy of the License at | 
| sarahmarshy | 18:9fc7976c7b27 | 7 | * | 
| sarahmarshy | 18:9fc7976c7b27 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| sarahmarshy | 18:9fc7976c7b27 | 9 | * | 
| sarahmarshy | 18:9fc7976c7b27 | 10 | * Unless required by applicable law or agreed to in writing, software | 
| sarahmarshy | 18:9fc7976c7b27 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| sarahmarshy | 18:9fc7976c7b27 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| sarahmarshy | 18:9fc7976c7b27 | 13 | * See the License for the specific language governing permissions and | 
| sarahmarshy | 18:9fc7976c7b27 | 14 | * limitations under the License. | 
| sarahmarshy | 18:9fc7976c7b27 | 15 | */ | 
| sarahmarshy | 18:9fc7976c7b27 | 16 | |
| Christopher Haster | 34:9c26a3dcdc1f | 17 | #ifndef ESP8266_INTERFACE_H | 
| Christopher Haster | 34:9c26a3dcdc1f | 18 | #define ESP8266_INTERFACE_H | 
| sarahmarshy | 18:9fc7976c7b27 | 19 | |
| sarahmarshy | 18:9fc7976c7b27 | 20 | #include "WiFiInterface.h" | 
| sarahmarshy | 23:fd0f3197c30b | 21 | #include "ESP8266.h" | 
| Christopher Haster | 34:9c26a3dcdc1f | 22 | |
| Christopher Haster | 34:9c26a3dcdc1f | 23 | #define ESP8266_SOCKET_COUNT 5 | 
| sarahmarshy | 18:9fc7976c7b27 | 24 | |
| Christopher Haster | 34:9c26a3dcdc1f | 25 | /** ESP8266Interface class | 
| Christopher Haster | 34:9c26a3dcdc1f | 26 | * Implementation of the NetworkInterface for the ESP8266 | 
| sarahmarshy | 18:9fc7976c7b27 | 27 | */ | 
| sarahmarshy | 18:9fc7976c7b27 | 28 | class ESP8266Interface : public WiFiInterface | 
| sarahmarshy | 18:9fc7976c7b27 | 29 | { | 
| sarahmarshy | 18:9fc7976c7b27 | 30 | public: | 
| geky | 55:c0808849cb89 | 31 | /** ESP8266Interface lifetime | 
| geky | 55:c0808849cb89 | 32 | /param tx TX pin | 
| geky | 55:c0808849cb89 | 33 | /param rx RX pin | 
| geky | 55:c0808849cb89 | 34 | /param debug Enable debugging | 
| geky | 55:c0808849cb89 | 35 | */ | 
| Christopher Haster | 34:9c26a3dcdc1f | 36 | ESP8266Interface(PinName tx, PinName rx, bool debug = false); | 
| Christopher Haster | 34:9c26a3dcdc1f | 37 | |
| geky | 55:c0808849cb89 | 38 | /** Start the interface | 
| geky | 55:c0808849cb89 | 39 | /param ssid Name of the network to connect to | 
| geky | 55:c0808849cb89 | 40 | /param pass Security passphrase to connect to the network | 
| geky | 55:c0808849cb89 | 41 | /param security Type of encryption for connection | 
| geky | 55:c0808849cb89 | 42 | /return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 43 | */ | 
| geky | 55:c0808849cb89 | 44 | virtual int connect( | 
| Christopher Haster | 34:9c26a3dcdc1f | 45 | const char *ssid, | 
| Christopher Haster | 34:9c26a3dcdc1f | 46 | const char *pass, | 
| geky | 55:c0808849cb89 | 47 | nsapi_security_t security = NSAPI_SECURITY_NONE); | 
| geky | 55:c0808849cb89 | 48 | |
| geky | 55:c0808849cb89 | 49 | /** Stop the interface | 
| geky | 55:c0808849cb89 | 50 | * @return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 51 | */ | 
| geky | 55:c0808849cb89 | 52 | virtual int disconnect(); | 
| geky | 55:c0808849cb89 | 53 | |
| geky | 55:c0808849cb89 | 54 | /** Get the internally stored IP address | 
| geky | 55:c0808849cb89 | 55 | /return IP address of the interface or null if not yet connected | 
| geky | 55:c0808849cb89 | 56 | */ | 
| geky | 55:c0808849cb89 | 57 | virtual const char *get_ip_address(); | 
| geky | 55:c0808849cb89 | 58 | |
| geky | 55:c0808849cb89 | 59 | /** Get the internally stored MAC address | 
| geky | 55:c0808849cb89 | 60 | /return MAC address of the interface | 
| geky | 55:c0808849cb89 | 61 | */ | 
| geky | 55:c0808849cb89 | 62 | virtual const char *get_mac_address(); | 
| geky | 55:c0808849cb89 | 63 | |
| geky | 55:c0808849cb89 | 64 | protected: | 
| geky | 55:c0808849cb89 | 65 | /** Create a socket | 
| geky | 55:c0808849cb89 | 66 | /param proto The type of socket to open, TCP or UDP | 
| geky | 55:c0808849cb89 | 67 | /return The alocated socket or null on failure | 
| geky | 55:c0808849cb89 | 68 | */ | 
| geky | 55:c0808849cb89 | 69 | virtual void *socket_create(nsapi_protocol_t proto); | 
| geky | 55:c0808849cb89 | 70 | |
| geky | 55:c0808849cb89 | 71 | /** Destroy a socket | 
| geky | 55:c0808849cb89 | 72 | /param socket Previously allocated socket | 
| geky | 55:c0808849cb89 | 73 | */ | 
| geky | 55:c0808849cb89 | 74 | virtual void socket_destroy(void *handle); | 
| Christopher Haster | 34:9c26a3dcdc1f | 75 | |
| geky | 55:c0808849cb89 | 76 | /** Set socket options | 
| geky | 55:c0808849cb89 | 77 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 78 | \param optname Option ID | 
| geky | 55:c0808849cb89 | 79 | \param optval Option value | 
| geky | 55:c0808849cb89 | 80 | \param optlen Length of the option value | 
| geky | 55:c0808849cb89 | 81 | \return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 82 | */ | 
| geky | 55:c0808849cb89 | 83 | virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen); | 
| geky | 55:c0808849cb89 | 84 | |
| geky | 55:c0808849cb89 | 85 | /** Get socket options | 
| geky | 55:c0808849cb89 | 86 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 87 | \param optname Option ID | 
| geky | 55:c0808849cb89 | 88 | \param optval Buffer pointer where to write the option value | 
| geky | 55:c0808849cb89 | 89 | \param optlen Length of the option value | 
| geky | 55:c0808849cb89 | 90 | \return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 91 | */ | 
| geky | 55:c0808849cb89 | 92 | virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen); | 
| geky | 55:c0808849cb89 | 93 | |
| geky | 55:c0808849cb89 | 94 | /** Bind a server socket to a specific port | 
| geky | 55:c0808849cb89 | 95 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 96 | \param port The port to listen for incoming connections on | 
| geky | 55:c0808849cb89 | 97 | \return 0 on success, negative on failure. | 
| geky | 55:c0808849cb89 | 98 | */ | 
| geky | 55:c0808849cb89 | 99 | virtual int socket_bind(void *handle, int port); | 
| geky | 55:c0808849cb89 | 100 | |
| geky | 55:c0808849cb89 | 101 | /** Start listening for incoming connections | 
| geky | 55:c0808849cb89 | 102 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 103 | \param backlog Number of pending connections that can be queued up at any | 
| geky | 55:c0808849cb89 | 104 | one time [Default: 1] | 
| geky | 55:c0808849cb89 | 105 | \return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 106 | */ | 
| geky | 55:c0808849cb89 | 107 | virtual int socket_listen(void *handle, int backlog); | 
| geky | 55:c0808849cb89 | 108 | |
| geky | 55:c0808849cb89 | 109 | /** Connects this TCP socket to the server | 
| geky | 55:c0808849cb89 | 110 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 111 | \param address SocketAddress to connect to | 
| geky | 55:c0808849cb89 | 112 | \return 0 on success, negative on failure | 
| geky | 55:c0808849cb89 | 113 | */ | 
| geky | 55:c0808849cb89 | 114 | virtual int socket_connect(void *handle, const SocketAddress &address); | 
| geky | 55:c0808849cb89 | 115 | |
| geky | 55:c0808849cb89 | 116 | /** Check if the socket is connected | 
| geky | 55:c0808849cb89 | 117 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 118 | \return true if connected, false otherwise | 
| geky | 55:c0808849cb89 | 119 | */ | 
| geky | 55:c0808849cb89 | 120 | virtual bool socket_is_connected(void *handle); | 
| Christopher Haster | 34:9c26a3dcdc1f | 121 | |
| geky | 55:c0808849cb89 | 122 | /** Accept a new connection. | 
| geky | 55:c0808849cb89 | 123 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 124 | \param socket A TCPSocket instance that will handle the incoming connection. | 
| geky | 55:c0808849cb89 | 125 | \return 0 on success, negative on failure. | 
| geky | 55:c0808849cb89 | 126 | \note This call is not-blocking, if this call would block, must | 
| geky | 55:c0808849cb89 | 127 | immediately return NSAPI_ERROR_WOULD_WAIT | 
| geky | 55:c0808849cb89 | 128 | */ | 
| geky | 55:c0808849cb89 | 129 | virtual int socket_accept(void *handle, void **connection); | 
| geky | 55:c0808849cb89 | 130 | |
| geky | 55:c0808849cb89 | 131 | /** Send data to the remote host | 
| geky | 55:c0808849cb89 | 132 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 133 | \param data The buffer to send to the host | 
| geky | 55:c0808849cb89 | 134 | \param size The length of the buffer to send | 
| geky | 55:c0808849cb89 | 135 | \return Number of written bytes on success, negative on failure | 
| geky | 55:c0808849cb89 | 136 | \note This call is not-blocking, if this call would block, must | 
| geky | 55:c0808849cb89 | 137 | immediately return NSAPI_ERROR_WOULD_WAIT | 
| geky | 55:c0808849cb89 | 138 | */ | 
| geky | 55:c0808849cb89 | 139 | virtual int socket_send(void *handle, const void *data, unsigned size); | 
| geky | 55:c0808849cb89 | 140 | |
| geky | 55:c0808849cb89 | 141 | /** Receive data from the remote host | 
| geky | 55:c0808849cb89 | 142 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 143 | \param data The buffer in which to store the data received from the host | 
| geky | 55:c0808849cb89 | 144 | \param size The maximum length of the buffer | 
| geky | 55:c0808849cb89 | 145 | \return Number of received bytes on success, negative on failure | 
| geky | 55:c0808849cb89 | 146 | \note This call is not-blocking, if this call would block, must | 
| geky | 55:c0808849cb89 | 147 | immediately return NSAPI_ERROR_WOULD_WAIT | 
| geky | 55:c0808849cb89 | 148 | */ | 
| geky | 55:c0808849cb89 | 149 | virtual int socket_recv(void *handle, void *data, unsigned size); | 
| geky | 55:c0808849cb89 | 150 | |
| geky | 55:c0808849cb89 | 151 | /** Send a packet to a remote endpoint | 
| geky | 55:c0808849cb89 | 152 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 153 | \param address The remote SocketAddress | 
| geky | 55:c0808849cb89 | 154 | \param data The packet to be sent | 
| geky | 55:c0808849cb89 | 155 | \param size The length of the packet to be sent | 
| geky | 55:c0808849cb89 | 156 | \return the number of written bytes on success, negative on failure | 
| geky | 55:c0808849cb89 | 157 | \note This call is not-blocking, if this call would block, must | 
| geky | 55:c0808849cb89 | 158 | immediately return NSAPI_ERROR_WOULD_WAIT | 
| geky | 55:c0808849cb89 | 159 | */ | 
| geky | 55:c0808849cb89 | 160 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); | 
| Christopher Haster | 34:9c26a3dcdc1f | 161 | |
| geky | 55:c0808849cb89 | 162 | /** Receive a packet from a remote endpoint | 
| geky | 55:c0808849cb89 | 163 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 164 | \param address Destination for the remote SocketAddress or null | 
| geky | 55:c0808849cb89 | 165 | \param buffer The buffer for storing the incoming packet data | 
| geky | 55:c0808849cb89 | 166 | If a packet is too long to fit in the supplied buffer, | 
| geky | 55:c0808849cb89 | 167 | excess bytes are discarded | 
| geky | 55:c0808849cb89 | 168 | \param size The length of the buffer | 
| geky | 55:c0808849cb89 | 169 | \return the number of received bytes on success, negative on failure | 
| geky | 55:c0808849cb89 | 170 | \note This call is not-blocking, if this call would block, must | 
| geky | 55:c0808849cb89 | 171 | immediately return NSAPI_ERROR_WOULD_WAIT | 
| geky | 55:c0808849cb89 | 172 | */ | 
| geky | 55:c0808849cb89 | 173 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); | 
| geky | 55:c0808849cb89 | 174 | |
| geky | 55:c0808849cb89 | 175 | /** Close the socket | 
| geky | 55:c0808849cb89 | 176 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 177 | \param shutdown free the left-over data in message queues | 
| geky | 55:c0808849cb89 | 178 | */ | 
| geky | 55:c0808849cb89 | 179 | virtual int socket_close(void *handle, bool shutdown); | 
| Christopher Haster | 34:9c26a3dcdc1f | 180 | |
| geky | 55:c0808849cb89 | 181 | /** Register a callback on when a new connection is ready | 
| geky | 55:c0808849cb89 | 182 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 183 | \param callback Function to call when accept will succeed, may be called in | 
| geky | 55:c0808849cb89 | 184 | interrupt context. | 
| geky | 55:c0808849cb89 | 185 | \param id Argument to pass to callback | 
| geky | 55:c0808849cb89 | 186 | */ | 
| geky | 55:c0808849cb89 | 187 | virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id); | 
| geky | 55:c0808849cb89 | 188 | |
| geky | 55:c0808849cb89 | 189 | /** Register a callback on when send is ready | 
| geky | 55:c0808849cb89 | 190 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 191 | \param callback Function to call when accept will succeed, may be called in | 
| geky | 55:c0808849cb89 | 192 | interrupt context. | 
| geky | 55:c0808849cb89 | 193 | \param id Argument to pass to callback | 
| geky | 55:c0808849cb89 | 194 | */ | 
| geky | 55:c0808849cb89 | 195 | virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id); | 
| geky | 55:c0808849cb89 | 196 | |
| geky | 55:c0808849cb89 | 197 | /** Register a callback on when recv is ready | 
| geky | 55:c0808849cb89 | 198 | \param handle Socket handle | 
| geky | 55:c0808849cb89 | 199 | \param callback Function to call when accept will succeed, may be called in | 
| geky | 55:c0808849cb89 | 200 | interrupt context. | 
| geky | 55:c0808849cb89 | 201 | \param id Argument to pass to callback | 
| geky | 55:c0808849cb89 | 202 | */ | 
| geky | 55:c0808849cb89 | 203 | virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id); | 
| geky | 55:c0808849cb89 | 204 | |
| Christopher Haster | 34:9c26a3dcdc1f | 205 | private: | 
| Christopher Haster | 34:9c26a3dcdc1f | 206 | ESP8266 _esp; | 
| Christopher Haster | 34:9c26a3dcdc1f | 207 | bool _ids[ESP8266_SOCKET_COUNT]; | 
| sarahmarshy | 18:9fc7976c7b27 | 208 | }; | 
| sarahmarshy | 18:9fc7976c7b27 | 209 | |
| sarahmarshy | 18:9fc7976c7b27 | 210 | #endif | 
| geky | 55:c0808849cb89 | 211 | 
