|
23 | 23 | #define ESP8266_SOCKET_COUNT 5
|
24 | 24 |
|
25 | 25 | /** ESP8266Interface class
|
26 |
| - * Implementation of the NetworkInterface for the ESP8266 |
| 26 | + * Implementation of the NetworkStack for the ESP8266 |
27 | 27 | */
|
28 |
| -class ESP8266Interface : public WiFiInterface |
| 28 | +class ESP8266Interface : public NetworkStack, public WiFiInterface |
29 | 29 | {
|
30 | 30 | public:
|
31 | 31 | /** ESP8266Interface lifetime
|
32 |
| - /param tx TX pin |
33 |
| - /param rx RX pin |
34 |
| - /param debug Enable debugging |
35 |
| - */ |
| 32 | + * @param tx TX pin |
| 33 | + * @param rx RX pin |
| 34 | + * @param debug Enable debugging |
| 35 | + */ |
36 | 36 | ESP8266Interface(PinName tx, PinName rx, bool debug = false);
|
37 | 37 |
|
38 | 38 | /** Start the interface
|
39 |
| - /param ssid Name of the network to connect to |
40 |
| - /param pass Security passphrase to connect to the network |
41 |
| - /param security Type of encryption for connection |
42 |
| - /return 0 on success, negative on failure |
43 |
| - */ |
| 39 | + * |
| 40 | + * Attempts to connect to a WiFi network. If passphrase is invalid, |
| 41 | + * NSAPI_ERROR_AUTH_ERROR is returned. |
| 42 | + * |
| 43 | + * @param ssid Name of the network to connect to |
| 44 | + * @param pass Security passphrase to connect to the network |
| 45 | + * @param security Type of encryption for connection |
| 46 | + * @return 0 on success, negative error code on failure |
| 47 | + */ |
44 | 48 | virtual int connect(
|
45 | 49 | const char *ssid,
|
46 | 50 | const char *pass,
|
47 | 51 | nsapi_security_t security = NSAPI_SECURITY_NONE);
|
48 | 52 |
|
49 | 53 | /** Stop the interface
|
50 |
| - * @return 0 on success, negative on failure |
| 54 | + * @return 0 on success, negative on failure |
51 | 55 | */
|
52 | 56 | virtual int disconnect();
|
53 | 57 |
|
54 | 58 | /** Get the internally stored IP address
|
55 |
| - /return IP address of the interface or null if not yet connected |
56 |
| - */ |
| 59 | + * @return IP address of the interface or null if not yet connected |
| 60 | + */ |
57 | 61 | virtual const char *get_ip_address();
|
58 | 62 |
|
59 | 63 | /** Get the internally stored MAC address
|
60 |
| - /return MAC address of the interface |
61 |
| - */ |
| 64 | + * @return MAC address of the interface |
| 65 | + */ |
62 | 66 | virtual const char *get_mac_address();
|
63 | 67 |
|
64 | 68 | protected:
|
65 |
| - /** Create a socket |
66 |
| - /param proto The type of socket to open, TCP or UDP |
67 |
| - /return The alocated socket or null on failure |
68 |
| - */ |
69 |
| - virtual void *socket_create(nsapi_protocol_t proto); |
70 |
| - |
71 |
| - /** Destroy a socket |
72 |
| - /param socket Previously allocated socket |
73 |
| - */ |
74 |
| - virtual void socket_destroy(void *handle); |
75 |
| - |
76 |
| - /** Set socket options |
77 |
| - \param handle Socket handle |
78 |
| - \param optname Option ID |
79 |
| - \param optval Option value |
80 |
| - \param optlen Length of the option value |
81 |
| - \return 0 on success, negative on failure |
82 |
| - */ |
83 |
| - virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen); |
84 |
| - |
85 |
| - /** Get socket options |
86 |
| - \param handle Socket handle |
87 |
| - \param optname Option ID |
88 |
| - \param optval Buffer pointer where to write the option value |
89 |
| - \param optlen Length of the option value |
90 |
| - \return 0 on success, negative on failure |
91 |
| - */ |
92 |
| - virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen); |
| 69 | + /** Open a socket |
| 70 | + * @param handle Handle in which to store new socket |
| 71 | + * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
| 72 | + * @return 0 on success, negative on failure |
| 73 | + */ |
| 74 | + virtual int socket_open(void **handle, nsapi_protocol_t proto); |
| 75 | + |
| 76 | + /** Close the socket |
| 77 | + * @param handle Socket handle |
| 78 | + * @return 0 on success, negative on failure |
| 79 | + * @note On failure, any memory associated with the socket must still |
| 80 | + * be cleaned up |
| 81 | + */ |
| 82 | + virtual int socket_close(void *handle); |
93 | 83 |
|
94 | 84 | /** Bind a server socket to a specific port
|
95 |
| - \param handle Socket handle |
96 |
| - \param port The port to listen for incoming connections on |
97 |
| - \return 0 on success, negative on failure. |
98 |
| - */ |
99 |
| - virtual int socket_bind(void *handle, int port); |
| 85 | + * @param handle Socket handle |
| 86 | + * @param address Local address to listen for incoming connections on |
| 87 | + * @return 0 on success, negative on failure. |
| 88 | + */ |
| 89 | + virtual int socket_bind(void *handle, const SocketAddress &address); |
100 | 90 |
|
101 | 91 | /** Start listening for incoming connections
|
102 |
| - \param handle Socket handle |
103 |
| - \param backlog Number of pending connections that can be queued up at any |
104 |
| - one time [Default: 1] |
105 |
| - \return 0 on success, negative on failure |
106 |
| - */ |
| 92 | + * @param handle Socket handle |
| 93 | + * @param backlog Number of pending connections that can be queued up at any |
| 94 | + * one time [Default: 1] |
| 95 | + * @return 0 on success, negative on failure |
| 96 | + */ |
107 | 97 | virtual int socket_listen(void *handle, int backlog);
|
108 | 98 |
|
109 | 99 | /** Connects this TCP socket to the server
|
110 |
| - \param handle Socket handle |
111 |
| - \param address SocketAddress to connect to |
112 |
| - \return 0 on success, negative on failure |
113 |
| - */ |
| 100 | + * @param handle Socket handle |
| 101 | + * @param address SocketAddress to connect to |
| 102 | + * @return 0 on success, negative on failure |
| 103 | + */ |
114 | 104 | virtual int socket_connect(void *handle, const SocketAddress &address);
|
115 |
| - |
116 |
| - /** Check if the socket is connected |
117 |
| - \param handle Socket handle |
118 |
| - \return true if connected, false otherwise |
119 |
| - */ |
120 |
| - virtual bool socket_is_connected(void *handle); |
121 | 105 |
|
122 | 106 | /** Accept a new connection.
|
123 |
| - \param handle Socket handle |
124 |
| - \param socket A TCPSocket instance that will handle the incoming connection. |
125 |
| - \return 0 on success, negative on failure. |
126 |
| - \note This call is not-blocking, if this call would block, must |
127 |
| - immediately return NSAPI_ERROR_WOULD_WAIT |
128 |
| - */ |
129 |
| - virtual int socket_accept(void *handle, void **connection); |
| 107 | + * @param handle Handle in which to store new socket |
| 108 | + * @param server Socket handle to server to accept from |
| 109 | + * @return 0 on success, negative on failure |
| 110 | + * @note This call is not-blocking, if this call would block, must |
| 111 | + * immediately return NSAPI_ERROR_WOULD_WAIT |
| 112 | + */ |
| 113 | + virtual int socket_accept(void **handle, void *server); |
130 | 114 |
|
131 | 115 | /** Send data to the remote host
|
132 |
| - \param handle Socket handle |
133 |
| - \param data The buffer to send to the host |
134 |
| - \param size The length of the buffer to send |
135 |
| - \return Number of written bytes on success, negative on failure |
136 |
| - \note This call is not-blocking, if this call would block, must |
137 |
| - immediately return NSAPI_ERROR_WOULD_WAIT |
138 |
| - */ |
| 116 | + * @param handle Socket handle |
| 117 | + * @param data The buffer to send to the host |
| 118 | + * @param size The length of the buffer to send |
| 119 | + * @return Number of written bytes on success, negative on failure |
| 120 | + * @note This call is not-blocking, if this call would block, must |
| 121 | + * immediately return NSAPI_ERROR_WOULD_WAIT |
| 122 | + */ |
139 | 123 | virtual int socket_send(void *handle, const void *data, unsigned size);
|
140 | 124 |
|
141 | 125 | /** Receive data from the remote host
|
142 |
| - \param handle Socket handle |
143 |
| - \param data The buffer in which to store the data received from the host |
144 |
| - \param size The maximum length of the buffer |
145 |
| - \return Number of received bytes on success, negative on failure |
146 |
| - \note This call is not-blocking, if this call would block, must |
147 |
| - immediately return NSAPI_ERROR_WOULD_WAIT |
148 |
| - */ |
| 126 | + * @param handle Socket handle |
| 127 | + * @param data The buffer in which to store the data received from the host |
| 128 | + * @param size The maximum length of the buffer |
| 129 | + * @return Number of received bytes on success, negative on failure |
| 130 | + * @note This call is not-blocking, if this call would block, must |
| 131 | + * immediately return NSAPI_ERROR_WOULD_WAIT |
| 132 | + */ |
149 | 133 | virtual int socket_recv(void *handle, void *data, unsigned size);
|
150 | 134 |
|
151 | 135 | /** Send a packet to a remote endpoint
|
152 |
| - \param handle Socket handle |
153 |
| - \param address The remote SocketAddress |
154 |
| - \param data The packet to be sent |
155 |
| - \param size The length of the packet to be sent |
156 |
| - \return the number of written bytes on success, negative on failure |
157 |
| - \note This call is not-blocking, if this call would block, must |
158 |
| - immediately return NSAPI_ERROR_WOULD_WAIT |
159 |
| - */ |
| 136 | + * @param handle Socket handle |
| 137 | + * @param address The remote SocketAddress |
| 138 | + * @param data The packet to be sent |
| 139 | + * @param size The length of the packet to be sent |
| 140 | + * @return The number of written bytes on success, negative on failure |
| 141 | + * @note This call is not-blocking, if this call would block, must |
| 142 | + * immediately return NSAPI_ERROR_WOULD_WAIT |
| 143 | + */ |
160 | 144 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
|
161 | 145 |
|
162 | 146 | /** Receive a packet from a remote endpoint
|
163 |
| - \param handle Socket handle |
164 |
| - \param address Destination for the remote SocketAddress or null |
165 |
| - \param buffer The buffer for storing the incoming packet data |
166 |
| - If a packet is too long to fit in the supplied buffer, |
167 |
| - excess bytes are discarded |
168 |
| - \param size The length of the buffer |
169 |
| - \return the number of received bytes on success, negative on failure |
170 |
| - \note This call is not-blocking, if this call would block, must |
171 |
| - immediately return NSAPI_ERROR_WOULD_WAIT |
172 |
| - */ |
| 147 | + * @param handle Socket handle |
| 148 | + * @param address Destination for the remote SocketAddress or null |
| 149 | + * @param buffer The buffer for storing the incoming packet data |
| 150 | + * If a packet is too long to fit in the supplied buffer, |
| 151 | + * excess bytes are discarded |
| 152 | + * @param size The length of the buffer |
| 153 | + * @return The number of received bytes on success, negative on failure |
| 154 | + * @note This call is not-blocking, if this call would block, must |
| 155 | + * immediately return NSAPI_ERROR_WOULD_WAIT |
| 156 | + */ |
173 | 157 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
|
174 | 158 |
|
175 |
| - /** Close the socket |
176 |
| - \param handle Socket handle |
177 |
| - \param shutdown free the left-over data in message queues |
178 |
| - */ |
179 |
| - virtual int socket_close(void *handle, bool shutdown); |
180 |
| - |
181 |
| - /** Register a callback on when a new connection is ready |
182 |
| - \param handle Socket handle |
183 |
| - \param callback Function to call when accept will succeed, may be called in |
184 |
| - interrupt context. |
185 |
| - \param id Argument to pass to callback |
186 |
| - */ |
187 |
| - virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id); |
188 |
| - |
189 |
| - /** Register a callback on when send is ready |
190 |
| - \param handle Socket handle |
191 |
| - \param callback Function to call when accept will succeed, may be called in |
192 |
| - interrupt context. |
193 |
| - \param id Argument to pass to callback |
194 |
| - */ |
195 |
| - virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id); |
196 |
| - |
197 |
| - /** Register a callback on when recv is ready |
198 |
| - \param handle Socket handle |
199 |
| - \param callback Function to call when accept will succeed, may be called in |
200 |
| - interrupt context. |
201 |
| - \param id Argument to pass to callback |
202 |
| - */ |
203 |
| - virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id); |
| 159 | + /** Register a callback on state change of the socket |
| 160 | + * @param handle Socket handle |
| 161 | + * @param callback Function to call on state change |
| 162 | + * @param data Argument to pass to callback |
| 163 | + * @note Callback may be called in an interrupt context. |
| 164 | + */ |
| 165 | + virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
204 | 166 |
|
205 | 167 | private:
|
206 | 168 | ESP8266 _esp;
|
207 | 169 | bool _ids[ESP8266_SOCKET_COUNT];
|
208 | 170 | };
|
209 | 171 |
|
210 | 172 | #endif
|
211 |
| - |
0 commit comments