Skip to content

Commit ec309b7

Browse files
authored
Merge pull request ARMmbed#151 from c1728p9/nsapi_updates
Separate interface from stack in NSAPI
2 parents a28c66a + 4fa6ebc commit ec309b7

File tree

23 files changed

+562
-301
lines changed

23 files changed

+562
-301
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/mbed-client-classic/#e8cbe5defdf97ed2b700878b8d5aead154810a9a
1+
https://github.com/ARMmbed/mbed-client-classic/#c8ccada6b9ffc0597e9695f746f9963cd3f25a11

features/FEATURE_IPV4/LWIPInterface/LWIPInterface.cpp renamed to features/FEATURE_IPV4/LWIPInterface/EthernetInterface.cpp

Lines changed: 190 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616

1717
#include "mbed.h"
18-
#include "LWIPInterface.h"
18+
#include "EthernetInterface.h"
19+
#include "NetworkStack.h"
1920

2021
#include "lwip/inet.h"
2122
#include "lwip/netif.h"
@@ -34,6 +35,131 @@
3435
#include "lwip/def.h"
3536
#include "lwip/ip_addr.h"
3637

38+
class LWIPInterface : public NetworkStack
39+
{
40+
/** Get the local IP address
41+
*
42+
* @return Null-terminated representation of the local IP address
43+
* or null if not yet connected
44+
*/
45+
virtual const char *get_ip_address();
46+
47+
/** Open a socket
48+
* @param handle Handle in which to store new socket
49+
* @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
50+
* @return 0 on success, negative on failure
51+
*/
52+
virtual int socket_open(void **handle, nsapi_protocol_t proto);
53+
54+
/** Close the socket
55+
* @param handle Socket handle
56+
* @return 0 on success, negative on failure
57+
* @note On failure, any memory associated with the socket must still
58+
* be cleaned up
59+
*/
60+
virtual int socket_close(void *handle);
61+
62+
/** Bind a server socket to a specific port
63+
* @param handle Socket handle
64+
* @param address Local address to listen for incoming connections on
65+
* @return 0 on success, negative on failure.
66+
*/
67+
virtual int socket_bind(void *handle, const SocketAddress &address);
68+
69+
/** Start listening for incoming connections
70+
* @param handle Socket handle
71+
* @param backlog Number of pending connections that can be queued up at any
72+
* one time [Default: 1]
73+
* @return 0 on success, negative on failure
74+
*/
75+
virtual int socket_listen(void *handle, int backlog);
76+
77+
/** Connects this TCP socket to the server
78+
* @param handle Socket handle
79+
* @param address SocketAddress to connect to
80+
* @return 0 on success, negative on failure
81+
*/
82+
virtual int socket_connect(void *handle, const SocketAddress &address);
83+
84+
/** Accept a new connection.
85+
* @param handle Handle in which to store new socket
86+
* @param server Socket handle to server to accept from
87+
* @return 0 on success, negative on failure
88+
* @note This call is not-blocking, if this call would block, must
89+
* immediately return NSAPI_ERROR_WOULD_WAIT
90+
*/
91+
virtual int socket_accept(void **handle, void *server);
92+
93+
/** Send data to the remote host
94+
* @param handle Socket handle
95+
* @param data The buffer to send to the host
96+
* @param size The length of the buffer to send
97+
* @return Number of written bytes on success, negative on failure
98+
* @note This call is not-blocking, if this call would block, must
99+
* immediately return NSAPI_ERROR_WOULD_WAIT
100+
*/
101+
virtual int socket_send(void *handle, const void *data, unsigned size);
102+
103+
/** Receive data from the remote host
104+
* @param handle Socket handle
105+
* @param data The buffer in which to store the data received from the host
106+
* @param size The maximum length of the buffer
107+
* @return Number of received bytes on success, negative on failure
108+
* @note This call is not-blocking, if this call would block, must
109+
* immediately return NSAPI_ERROR_WOULD_WAIT
110+
*/
111+
virtual int socket_recv(void *handle, void *data, unsigned size);
112+
113+
/** Send a packet to a remote endpoint
114+
* @param handle Socket handle
115+
* @param address The remote SocketAddress
116+
* @param data The packet to be sent
117+
* @param size The length of the packet to be sent
118+
* @return the number of written bytes on success, negative on failure
119+
* @note This call is not-blocking, if this call would block, must
120+
* immediately return NSAPI_ERROR_WOULD_WAIT
121+
*/
122+
virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
123+
124+
/** Receive a packet from a remote endpoint
125+
* @param handle Socket handle
126+
* @param address Destination for the remote SocketAddress or null
127+
* @param buffer The buffer for storing the incoming packet data
128+
* If a packet is too long to fit in the supplied buffer,
129+
* excess bytes are discarded
130+
* @param size The length of the buffer
131+
* @return the number of received bytes on success, negative on failure
132+
* @note This call is not-blocking, if this call would block, must
133+
* immediately return NSAPI_ERROR_WOULD_WAIT
134+
*/
135+
virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
136+
137+
/* Set stack-specific socket options
138+
*
139+
* The setsockopt allow an application to pass stack-specific hints
140+
* to the underlying stack. For unsupported options,
141+
* NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
142+
*
143+
* @param handle Socket handle
144+
* @param level Stack-specific protocol level
145+
* @param optname Stack-specific option identifier
146+
* @param optval Option value
147+
* @param optlen Length of the option value
148+
* @return 0 on success, negative error code on failure
149+
*/
150+
virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
151+
152+
/** Register a callback on state change of the socket
153+
* @param handle Socket handle
154+
* @param callback Function to call on state change
155+
* @param data Argument to pass to callback
156+
* @note Callback may be called in an interrupt context.
157+
*/
158+
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
159+
160+
161+
};
162+
37163

38164
/* TCP/IP and Network Interface Initialisation */
39165
static struct netif netif;
@@ -90,55 +216,6 @@ static void set_mac_address(void)
90216
#endif
91217
}
92218

93-
94-
/* Interface implementation */
95-
int LWIPInterface::connect()
96-
{
97-
// Check if we've already connected
98-
if (get_ip_address()) {
99-
return 0;
100-
}
101-
102-
// Set up network
103-
set_mac_address();
104-
init_netif(0, 0, 0);
105-
106-
// Connect to network
107-
eth_arch_enable_interrupts();
108-
109-
dhcp_start(&netif);
110-
111-
// Wait for an IP Address
112-
// -1: error, 0: timeout
113-
if (netif_up.wait(15000) <= 0) {
114-
return NSAPI_ERROR_DHCP_FAILURE;
115-
}
116-
117-
return 0;
118-
}
119-
120-
int LWIPInterface::disconnect()
121-
{
122-
dhcp_release(&netif);
123-
dhcp_stop(&netif);
124-
125-
eth_arch_disable_interrupts();
126-
ip_addr[0] = '\0';
127-
mac_addr[0] = '\0';
128-
129-
return 0;
130-
}
131-
132-
const char *LWIPInterface::get_ip_address()
133-
{
134-
return ip_addr[0] ? ip_addr : 0;
135-
}
136-
137-
const char *LWIPInterface::get_mac_address()
138-
{
139-
return mac_addr[0] ? mac_addr : 0;
140-
}
141-
142219
struct lwip_socket {
143220
nsapi_protocol_t proto;
144221
union {
@@ -158,6 +235,11 @@ static void udp_recv_irq(
158235
void *arg, struct udp_pcb *upcb, struct pbuf *p,
159236
struct ip_addr *addr, uint16_t port);
160237

238+
const char *LWIPInterface::get_ip_address()
239+
{
240+
return ip_addr[0] ? ip_addr : 0;
241+
}
242+
161243
int LWIPInterface::socket_open(void **handle, nsapi_protocol_t proto)
162244
{
163245
struct lwip_socket *s = new struct lwip_socket;
@@ -591,3 +673,61 @@ void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *
591673
s->callback = callback;
592674
s->data = data;
593675
}
676+
677+
EthernetInterface::EthernetInterface()
678+
{
679+
_stack = (NetworkStack*)new LWIPInterface();
680+
}
681+
682+
/* Interface implementation */
683+
int EthernetInterface::connect()
684+
{
685+
// Check if we've already connected
686+
if (get_ip_address()) {
687+
return 0;
688+
}
689+
690+
// Set up network
691+
set_mac_address();
692+
init_netif(0, 0, 0);
693+
694+
// Connect to network
695+
eth_arch_enable_interrupts();
696+
697+
dhcp_start(&netif);
698+
699+
// Wait for an IP Address
700+
// -1: error, 0: timeout
701+
if (netif_up.wait(15000) <= 0) {
702+
return NSAPI_ERROR_DHCP_FAILURE;
703+
}
704+
705+
return 0;
706+
}
707+
708+
int EthernetInterface::disconnect()
709+
{
710+
dhcp_release(&netif);
711+
dhcp_stop(&netif);
712+
713+
eth_arch_disable_interrupts();
714+
ip_addr[0] = '\0';
715+
mac_addr[0] = '\0';
716+
717+
return 0;
718+
}
719+
720+
const char *EthernetInterface::get_ip_address()
721+
{
722+
return ip_addr[0] ? ip_addr : 0;
723+
}
724+
725+
const char *EthernetInterface::get_mac_address()
726+
{
727+
return mac_addr[0] ? mac_addr : 0;
728+
}
729+
730+
NetworkStack * EthernetInterface::get_stack()
731+
{
732+
return _stack;
733+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* LWIP implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ETHERNET_INTERFACE_H
18+
#define ETHERNET_INTERFACE_H
19+
20+
#include "EthInterface.h"
21+
#include "rtos.h"
22+
#include "lwip/netif.h"
23+
24+
// Forward declaration
25+
class NetworkStack;
26+
27+
/** EthernetInterface class
28+
* Implementation of the NetworkStack for LWIP
29+
*/
30+
class EthernetInterface : public EthInterface
31+
{
32+
public:
33+
EthernetInterface();
34+
35+
/** Start the interface
36+
* @return 0 on success, negative on failure
37+
*/
38+
virtual int connect();
39+
40+
/** Stop the interface
41+
* @return 0 on success, negative on failure
42+
*/
43+
virtual int disconnect();
44+
45+
/** Get the internally stored IP address
46+
* @return IP address of the interface or null if not yet connected
47+
*/
48+
virtual const char *get_ip_address();
49+
50+
/** Get the internally stored MAC address
51+
* @return MAC address of the interface
52+
*/
53+
virtual const char *get_mac_address();
54+
55+
protected:
56+
NetworkStack* _stack;
57+
58+
/* Return the underlying network stack for this interface
59+
*
60+
* @return The stack running the interface
61+
*/
62+
virtual NetworkStack * get_stack(void);
63+
};
64+
65+
#endif

0 commit comments

Comments
 (0)