Skip to content

Commit b7cdc20

Browse files
committed
Merge pull request #88 from geky/nsapi-changes
Update Network Socket API
2 parents cd4172a + b15e2c2 commit b7cdc20

34 files changed

+3247
-1106
lines changed

net/ESP8266Interface/ESP8266Interface.cpp

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct esp8266_socket {
8484
bool connected;
8585
};
8686

87-
void *ESP8266Interface::socket_create(nsapi_protocol_t proto)
87+
int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
8888
{
8989
// Look for an unused socket
9090
int id = -1;
@@ -98,38 +98,37 @@ void *ESP8266Interface::socket_create(nsapi_protocol_t proto)
9898
}
9999

100100
if (id == -1) {
101-
return 0;
101+
return NSAPI_ERROR_NO_SOCKET;
102102
}
103103

104104
struct esp8266_socket *socket = new struct esp8266_socket;
105105
if (!socket) {
106-
return 0;
106+
return NSAPI_ERROR_NO_SOCKET;
107107
}
108108

109109
socket->id = id;
110110
socket->proto = proto;
111111
socket->connected = false;
112-
return socket;
112+
*handle = socket;
113+
return 0;
113114
}
114115

115-
void ESP8266Interface::socket_destroy(void *handle)
116+
int ESP8266Interface::socket_close(void *handle)
116117
{
117118
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
119+
int err = 0;
120+
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
121+
122+
if (!_esp.close(socket->id)) {
123+
err = NSAPI_ERROR_DEVICE_ERROR;
124+
}
125+
118126
_ids[socket->id] = false;
119127
delete socket;
128+
return err;
120129
}
121130

122-
int ESP8266Interface::socket_set_option(void *handle, int optname, const void *optval, unsigned optlen)
123-
{
124-
return NSAPI_ERROR_UNSUPPORTED;
125-
}
126-
127-
int ESP8266Interface::socket_get_option(void *handle, int optname, void *optval, unsigned *optlen)
128-
{
129-
return NSAPI_ERROR_UNSUPPORTED;
130-
}
131-
132-
int ESP8266Interface::socket_bind(void *handle, int port)
131+
int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
133132
{
134133
return NSAPI_ERROR_UNSUPPORTED;
135134
}
@@ -153,12 +152,7 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
153152
return 0;
154153
}
155154

156-
bool ESP8266Interface::socket_is_connected(void *handle)
157-
{
158-
return true;
159-
}
160-
161-
int ESP8266Interface::socket_accept(void *handle, void **connection)
155+
int ESP8266Interface::socket_accept(void **handle, void *server)
162156
{
163157
return NSAPI_ERROR_UNSUPPORTED;
164158
}
@@ -207,26 +201,6 @@ int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *d
207201
return socket_recv(socket, data, size);
208202
}
209203

210-
int ESP8266Interface::socket_close(void *handle, bool shutdown)
211-
{
212-
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
213-
_esp.setTimeout(ESP8266_MISC_TIMEOUT);
214-
215-
if (!_esp.close(socket->id)) {
216-
return NSAPI_ERROR_DEVICE_ERROR;
217-
}
218-
219-
return 0;
220-
}
221-
222-
void ESP8266Interface::socket_attach_accept(void *handle, void (*callback)(void *), void *id)
223-
{
224-
}
225-
226-
void ESP8266Interface::socket_attach_send(void *handle, void (*callback)(void *), void *id)
227-
{
228-
}
229-
230-
void ESP8266Interface::socket_attach_recv(void *handle, void (*callback)(void *), void *id)
204+
void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
231205
{
232206
}

net/ESP8266Interface/ESP8266Interface.h

Lines changed: 94 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -23,189 +23,150 @@
2323
#define ESP8266_SOCKET_COUNT 5
2424

2525
/** ESP8266Interface class
26-
* Implementation of the NetworkInterface for the ESP8266
26+
* Implementation of the NetworkStack for the ESP8266
2727
*/
28-
class ESP8266Interface : public WiFiInterface
28+
class ESP8266Interface : public NetworkStack, public WiFiInterface
2929
{
3030
public:
3131
/** 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+
*/
3636
ESP8266Interface(PinName tx, PinName rx, bool debug = false);
3737

3838
/** 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+
*/
4448
virtual int connect(
4549
const char *ssid,
4650
const char *pass,
4751
nsapi_security_t security = NSAPI_SECURITY_NONE);
4852

4953
/** Stop the interface
50-
* @return 0 on success, negative on failure
54+
* @return 0 on success, negative on failure
5155
*/
5256
virtual int disconnect();
5357

5458
/** 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+
*/
5761
virtual const char *get_ip_address();
5862

5963
/** Get the internally stored MAC address
60-
/return MAC address of the interface
61-
*/
64+
* @return MAC address of the interface
65+
*/
6266
virtual const char *get_mac_address();
6367

6468
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);
9383

9484
/** 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);
10090

10191
/** 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+
*/
10797
virtual int socket_listen(void *handle, int backlog);
10898

10999
/** 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+
*/
114104
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);
121105

122106
/** 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);
130114

131115
/** 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+
*/
139123
virtual int socket_send(void *handle, const void *data, unsigned size);
140124

141125
/** 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+
*/
149133
virtual int socket_recv(void *handle, void *data, unsigned size);
150134

151135
/** 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+
*/
160144
virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
161145

162146
/** 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+
*/
173157
virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
174158

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);
204166

205167
private:
206168
ESP8266 _esp;
207169
bool _ids[ESP8266_SOCKET_COUNT];
208170
};
209171

210172
#endif
211-

0 commit comments

Comments
 (0)