15
15
*/
16
16
17
17
#include " mbed.h"
18
- #include " LWIPInterface.h"
18
+ #include " EthernetInterface.h"
19
+ #include " NetworkStack.h"
19
20
20
21
#include " lwip/inet.h"
21
22
#include " lwip/netif.h"
34
35
#include " lwip/def.h"
35
36
#include " lwip/ip_addr.h"
36
37
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
+
37
163
38
164
/* TCP/IP and Network Interface Initialisation */
39
165
static struct netif netif;
@@ -90,55 +216,6 @@ static void set_mac_address(void)
90
216
#endif
91
217
}
92
218
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
-
142
219
struct lwip_socket {
143
220
nsapi_protocol_t proto;
144
221
union {
@@ -158,6 +235,11 @@ static void udp_recv_irq(
158
235
void *arg, struct udp_pcb *upcb, struct pbuf *p,
159
236
struct ip_addr *addr, uint16_t port);
160
237
238
+ const char *LWIPInterface::get_ip_address ()
239
+ {
240
+ return ip_addr[0 ] ? ip_addr : 0 ;
241
+ }
242
+
161
243
int LWIPInterface::socket_open (void **handle, nsapi_protocol_t proto)
162
244
{
163
245
struct lwip_socket *s = new struct lwip_socket ;
@@ -591,3 +673,61 @@ void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *
591
673
s->callback = callback;
592
674
s->data = data;
593
675
}
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
+ }
0 commit comments