1
1
#include " MbedUdp.h"
2
2
3
- #ifndef WIFI_UDP_BUFFER_SIZE
4
- #define WIFI_UDP_BUFFER_SIZE 508
5
- #endif
6
-
7
3
arduino::MbedUDP::MbedUDP () {
8
4
_packet_buffer = new uint8_t [WIFI_UDP_BUFFER_SIZE];
9
5
_current_packet = NULL ;
@@ -60,18 +56,35 @@ void arduino::MbedUDP::stop() {
60
56
int arduino::MbedUDP::beginPacket (IPAddress ip, uint16_t port) {
61
57
_host = SocketHelpers::socketAddressFromIpAddress (ip, port);
62
58
// If IP is null and port is 0 the initialization failed
59
+ txBuffer.clear ();
63
60
return (_host.get_ip_address () == nullptr && _host.get_port () == 0 ) ? 0 : 1 ;
64
61
}
65
62
66
63
int arduino::MbedUDP::beginPacket (const char *host, uint16_t port) {
67
64
_host = SocketAddress (host, port);
65
+ txBuffer.clear ();
68
66
getNetwork ()->gethostbyname (host, &_host);
69
67
// If IP is null and port is 0 the initialization failed
70
68
return (_host.get_ip_address () == nullptr && _host.get_port () == 0 ) ? 0 : 1 ;
71
69
}
72
70
73
71
int arduino::MbedUDP::endPacket () {
74
- return 1 ;
72
+ _socket.set_blocking (true );
73
+ _socket.set_timeout (1000 );
74
+
75
+ size_t size = txBuffer.available ();
76
+ uint8_t buffer[size];
77
+ for (int i = 0 ; i < size; i++) {
78
+ buffer[i] = txBuffer.read_char ();
79
+ }
80
+
81
+ nsapi_size_or_error_t ret = _socket.sendto (_host, buffer, size);
82
+ _socket.set_blocking (false );
83
+ _socket.set_timeout (0 );
84
+ if (ret < 0 ) {
85
+ return 0 ;
86
+ }
87
+ return size;
75
88
}
76
89
77
90
// Write a single byte into the packet
@@ -81,13 +94,12 @@ size_t arduino::MbedUDP::write(uint8_t byte) {
81
94
82
95
// Write size bytes from buffer into the packet
83
96
size_t arduino::MbedUDP::write (const uint8_t *buffer, size_t size) {
84
- _socket.set_blocking (true );
85
- _socket.set_timeout (1000 );
86
- nsapi_size_or_error_t ret = _socket.sendto (_host, buffer, size);
87
- _socket.set_blocking (false );
88
- _socket.set_timeout (0 );
89
- if (ret < 0 ) {
90
- return 0 ;
97
+ for (int i = 0 ; i<size; i++) {
98
+ if (txBuffer.availableForStore ()) {
99
+ txBuffer.store_char (buffer[i]);
100
+ } else {
101
+ return 0 ;
102
+ }
91
103
}
92
104
return size;
93
105
}
0 commit comments