@@ -26,8 +26,6 @@ static void lwIp_udp_raw_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, co
2626
2727Arduino_10BASE_T1S_UDP::Arduino_10BASE_T1S_UDP ()
2828: _udp_pcb{nullptr }
29- , _remote_ip{0 ,0 ,0 ,0 }
30- , _remote_port{0 }
3129, _send_to_ip{0 ,0 ,0 ,0 }
3230, _send_to_port{0 }
3331{
@@ -131,77 +129,99 @@ size_t Arduino_10BASE_T1S_UDP::write(uint8_t data)
131129
132130size_t Arduino_10BASE_T1S_UDP::write (const uint8_t * buffer, size_t size)
133131{
134- _tx_data.reserve (_tx_data.size () + size);
135132 std::copy (buffer, buffer + size, std::back_inserter (_tx_data));
136133 return size;
137134}
138135
139136int Arduino_10BASE_T1S_UDP::parsePacket ()
140137{
141- return available ();
138+ if (_rx_pkt_list.size ())
139+ return _rx_pkt_list.front ()->totalSize ();
140+ else
141+ return 0 ;
142142}
143143
144144int Arduino_10BASE_T1S_UDP::available ()
145145{
146- return _rx_data.size ();
146+ if (_rx_pkt_list.size ())
147+ return _rx_pkt_list.front ()->available ();
148+ else
149+ return 0 ;
147150}
148151
149152int Arduino_10BASE_T1S_UDP::read ()
150153{
151- uint8_t const data = _rx_data.front ();
152- _rx_data.pop_front ();
153- return data;
154+ if (_rx_pkt_list.size ())
155+ return _rx_pkt_list.front ()->read ();
156+ else
157+ return -1 ;
154158}
155159
156160int Arduino_10BASE_T1S_UDP::read (unsigned char * buffer, size_t len)
157161{
158- size_t bytes_read = 0 ;
159- for (; bytes_read < len && !_rx_data.empty (); bytes_read++)
160- {
161- buffer[bytes_read] = _rx_data.front ();
162- _rx_data.pop_front ();
163- }
164- return bytes_read;
162+ if (_rx_pkt_list.size ())
163+ return _rx_pkt_list.front ()->read (buffer, len);
164+ else
165+ return -1 ;
165166}
166167
167168int Arduino_10BASE_T1S_UDP::read (char * buffer, size_t len)
168169{
169- return read ((unsigned char *)buffer, len);
170+ if (_rx_pkt_list.size ())
171+ return _rx_pkt_list.front ()->read (buffer, len);
172+ else
173+ return -1 ;
170174}
171175
172176int Arduino_10BASE_T1S_UDP::peek ()
173177{
174- return _rx_data.front ();
178+ if (_rx_pkt_list.size ())
179+ return _rx_pkt_list.front ()->peek ();
180+ else
181+ return -1 ;
175182}
176183
177184void Arduino_10BASE_T1S_UDP::flush ()
178185{
179- /* Nothing to be done. */
186+ /* Drop packet from receive buffer. */
187+ if (_rx_pkt_list.size ())
188+ _rx_pkt_list.pop_front ();
180189}
181190
182191IPAddress Arduino_10BASE_T1S_UDP::remoteIP ()
183192{
184- return _remote_ip;
193+ if (_rx_pkt_list.size ())
194+ return _rx_pkt_list.front ()->remoteIP ();
195+ else
196+ return IPAddress ();
185197}
186198
187199uint16_t Arduino_10BASE_T1S_UDP::remotePort ()
188200{
189- return _remote_port;
201+ if (_rx_pkt_list.size ())
202+ return _rx_pkt_list.front ()->remotePort ();
203+ else
204+ return 0 ;
190205}
191206
192207void Arduino_10BASE_T1S_UDP::onUdpRawRecv (struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port)
193208{
194209 /* Obtain remote port and remote IP. */
195- _remote_ip = IPAddress (ip4_addr1 (addr),
196- ip4_addr2 (addr),
197- ip4_addr3 (addr),
198- ip4_addr4 (addr));
199- _remote_port = port;
200-
201- /* Copy data into buffer. */
202- std::copy ((uint8_t *)p->payload ,
203- (uint8_t *)p->payload + p->len ,
204- std::back_inserter (_rx_data));
210+ auto const remote_ip = IPAddress (
211+ ip4_addr1 (addr),
212+ ip4_addr2 (addr),
213+ ip4_addr3 (addr),
214+ ip4_addr4 (addr));
215+ auto const remote_port = port;
216+
217+ /* Create UDP object. */
218+ auto const rx_pkt = std::make_shared<UdpRxPacket>(
219+ remote_ip,
220+ remote_port,
221+ (uint8_t const *)p->payload ,
222+ p->len );
223+
224+ _rx_pkt_list.push_back (rx_pkt);
205225
206226 /* Free pbuf */
207227 pbuf_free (p);
0 commit comments