26
26
* bjoern@cs.stanford.edu 12/30/2008
27
27
*/
28
28
29
- #include " utility/socket.h"
30
29
#include " Ethernet.h"
31
30
#include " Udp.h"
32
31
#include " Dns.h"
33
32
34
33
/* Constructor */
35
- EthernetUDP::EthernetUDP () : _sock(MAX_SOCK_NUM) {}
34
+ EthernetUDP::EthernetUDP () {}
36
35
37
36
/* Start EthernetUDP socket, listening at local port PORT */
38
37
uint8_t EthernetUDP::begin (uint16_t port) {
39
38
// Can create a single udp connection per socket
40
- if (_udp_pcb != NULL ) {
39
+ if (_udp. pcb != NULL ) {
41
40
return 0 ;
42
41
}
43
42
44
- _udp_pcb = udp_new ();
43
+ _udp. pcb = udp_new ();
45
44
46
- if (_udp_pcb == NULL ) {
45
+ if (_udp. pcb == NULL ) {
47
46
return 0 ;
48
47
}
49
48
50
49
IPAddress ip = Ethernet.localIP ();
51
50
ip_addr_t ipaddr;
52
51
53
- if (ERR_OK != udp_bind (_udp_pcb, u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port)) {
52
+ if (ERR_OK != udp_bind (_udp.pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port)) {
53
+ stop ();
54
54
return 0 ;
55
55
}
56
56
57
- udp_recv (_udp_pcb , &udp_receive_callback, &_arg );
57
+ udp_recv (_udp. pcb , &udp_receive_callback, &_udp );
58
58
59
59
_port = port;
60
60
_remaining = 0 ;
@@ -73,10 +73,10 @@ int EthernetUDP::available() {
73
73
/* Release any resources being used by this EthernetUDP instance */
74
74
void EthernetUDP::stop ()
75
75
{
76
- if (_udp_pcb != NULL ) {
77
- udp_disconnect (_udp_pcb );
78
- udp_remove (_udp_pcb );
79
- _udp_pcb = NULL ;
76
+ if (_udp. pcb != NULL ) {
77
+ udp_disconnect (_udp. pcb );
78
+ udp_remove (_udp. pcb );
79
+ _udp. pcb = NULL ;
80
80
}
81
81
82
82
stm32_eth_scheduler ();
@@ -100,34 +100,34 @@ int EthernetUDP::beginPacket(const char *host, uint16_t port)
100
100
101
101
int EthernetUDP::beginPacket (IPAddress ip, uint16_t port)
102
102
{
103
- if (_udp_pcb == NULL ) {
103
+ if (_udp. pcb == NULL ) {
104
104
return 0 ;
105
105
}
106
106
107
107
ip_addr_t ipaddr;
108
108
109
- if (ERR_OK != udp_connect ( _udp_pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port)) {
109
+ if (ERR_OK != udp_connect ( _udp. pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port)) {
110
110
return 0 ;
111
111
}
112
112
113
- udp_recv (_udp_pcb , &udp_receive_callback, &_arg );
113
+ udp_recv (_udp. pcb , &udp_receive_callback, &_udp );
114
114
stm32_eth_scheduler ();
115
115
116
116
return 1 ;
117
117
}
118
118
119
119
int EthernetUDP::endPacket ()
120
120
{
121
- if ((_udp_pcb == NULL ) || (_data == NULL )) {
121
+ if ((_udp. pcb == NULL ) || (_data == NULL )) {
122
122
return 0 ;
123
123
}
124
124
125
125
// A remote IP & port must be connected to udp pcb. Call ::beginPacket before.
126
- if ((udp_flags (_udp_pcb ) & UDP_FLAGS_CONNECTED) != UDP_FLAGS_CONNECTED) {
126
+ if ((udp_flags (_udp. pcb ) & UDP_FLAGS_CONNECTED) != UDP_FLAGS_CONNECTED) {
127
127
return 0 ;
128
128
}
129
129
130
- if (ERR_OK != udp_send (_udp_pcb , _data)) {
130
+ if (ERR_OK != udp_send (_udp. pcb , _data)) {
131
131
_data = stm32_free_data (_data);
132
132
return 0 ;
133
133
}
@@ -165,11 +165,11 @@ int EthernetUDP::parsePacket()
165
165
166
166
stm32_eth_scheduler ();
167
167
168
- if (_arg .available > 0 )
168
+ if (_udp .available > 0 )
169
169
{
170
- _remoteIP = IPAddress (ip_addr_to_u32 (&(_arg .ip )));
171
- _remotePort = _arg .port ;
172
- _remaining = _arg .available ;
170
+ _remoteIP = IPAddress (ip_addr_to_u32 (&(_udp .ip )));
171
+ _remotePort = _udp .port ;
172
+ _remaining = _udp .available ;
173
173
174
174
return _remaining;
175
175
}
@@ -181,7 +181,7 @@ int EthernetUDP::read()
181
181
{
182
182
uint8_t byte;
183
183
184
- if (_arg .p == NULL ) {
184
+ if (_udp .p == NULL ) {
185
185
return -1 ;
186
186
}
187
187
@@ -191,8 +191,8 @@ int EthernetUDP::read()
191
191
_remaining--;
192
192
193
193
if (_remaining == 0 ) {
194
- _arg .available = 0 ;
195
- _arg .p = stm32_free_data (_arg .p );
194
+ _udp .available = 0 ;
195
+ _udp .p = stm32_free_data (_udp .p );
196
196
}
197
197
198
198
return byte;
@@ -204,7 +204,7 @@ int EthernetUDP::read()
204
204
205
205
int EthernetUDP::read (unsigned char * buffer, size_t len)
206
206
{
207
- if (_arg .p == NULL ) {
207
+ if (_udp .p == NULL ) {
208
208
return -1 ;
209
209
}
210
210
@@ -215,22 +215,22 @@ int EthernetUDP::read(unsigned char* buffer, size_t len)
215
215
if (_remaining <= len)
216
216
{
217
217
// data should fit in the buffer
218
- got = (int )stm32_get_data (_arg .p , (uint8_t *)buffer, _remaining, _remaining);
218
+ got = (int )stm32_get_data (_udp .p , (uint8_t *)buffer, _remaining, _remaining);
219
219
}
220
220
else
221
221
{
222
222
// too much data for the buffer,
223
223
// grab as much as will fit
224
- got = (int )stm32_get_data (_arg .p , (uint8_t *)buffer, len, _remaining);
224
+ got = (int )stm32_get_data (_udp .p , (uint8_t *)buffer, len, _remaining);
225
225
}
226
226
227
227
if (got > 0 )
228
228
{
229
229
_remaining -= got;
230
230
231
231
if (_remaining == 0 ) {
232
- _arg .available = 0 ;
233
- _arg .p = stm32_free_data (_arg .p );
232
+ _udp .available = 0 ;
233
+ _udp .p = stm32_free_data (_udp .p );
234
234
}
235
235
236
236
return got;
@@ -251,8 +251,7 @@ int EthernetUDP::peek()
251
251
// may get the UDP header
252
252
if (!_remaining)
253
253
return -1 ;
254
- // ::peek(_sock, &b);
255
- stm32_get_data (_arg.p , &b, 1 , _remaining);
254
+ stm32_get_data (_udp.p , &b, 1 , _remaining);
256
255
return b;
257
256
}
258
257
0 commit comments