ClearCore Library
EthernetUdp.h
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 /*
24  \file EthernetUdp.h
25 **/
26 
27 #ifndef __ETHERNETUDP_H__
28 #define __ETHERNETUDP_H__
29 
30 #include "IpAddress.h"
31 #include "lwip/udp.h"
32 
33 namespace ClearCore {
34 
43 class EthernetUdp {
44 
45 public:
46 #ifndef HIDE_FROM_DOXYGEN
47 
50  typedef struct {
51  struct udp_pcb *pcb;
52  struct pbuf *packet;
53  uint16_t available;
54  ip_addr_t remoteIp;
55  u16_t remotePort;
56  } UdpData;
57 #endif // !HIDE_FROM_DOXYGEN
58 
62  EthernetUdp();
63 
72  bool Begin(uint16_t localPort);
73 
77  void End();
78 
87  bool Connect(IpAddress remoteIp, uint16_t remotePort);
88 
94  bool PacketSend();
95 
105  uint32_t PacketWrite(uint8_t c);
106 
116  uint32_t PacketWrite(const char *nullTermStr);
117 
128  uint32_t PacketWrite(const uint8_t *buffer, uint32_t size);
129 
139  uint16_t PacketParse();
140 
146  uint16_t BytesAvailable();
147 
158  int32_t PacketRead(unsigned char *dataPtr, uint16_t len);
159 
171  int16_t Peek();
172 
176  void PacketFlush();
177 
184 
190  uint16_t RemotePort();
191 
192 private:
193  UdpData m_udpData;
194  uint16_t m_udpLocalPort;
195 
196  struct pbuf *m_outgoingPacket;
197 
198  struct pbuf *m_incomingPacket;
199  uint16_t m_udpBytesAvailable;
200  IpAddress m_udpRemoteIpReceived;
201  uint16_t m_udpRemotePortReceived;
202  IpAddress m_udpRemoteIpDestination;
203  uint16_t m_udpRemotePortDestination;
204 
205  // Begin() was called.
206  bool m_initialized;
207  // Connect() was called and we can write to a packet.
208  bool m_packetBegun;
209  // PacketWrite() was called and we can send a packet.
210  bool m_packetReadyToSend;
211  // PacketParse() was called and we can read a packet.
212  bool m_packetParsed;
213 
214  uint16_t UdpPacketRead(pbuf *packet, uint16_t *available,
215  unsigned char *buffer, uint16_t size);
216 }; // EthernetUdp
217 
218 #ifndef HIDE_FROM_DOXYGEN
219 
220 void UdpReceive(void *arg, struct udp_pcb *pcb, struct pbuf *p,
221  const ip_addr_t *addr, u16_t port);
222 
223 #endif // !HIDE_FROM_DOXYGEN
224 } // ClearCore namespace
225 #endif // !__ETHERNETUDP_H__
bool PacketSend()
Send the UDP packet set up with Connect().
An IP Address class.
Definition: IpAddress.h:41
uint16_t RemotePort()
Returns the remote port for the current packet.
uint32_t PacketWrite(uint8_t c)
Write data into the outgoing UDP packet.
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
void PacketFlush()
Flush the current packet.
bool Begin(uint16_t localPort)
Initialize the UDP session and begin listening on the specified local port.
bool Connect(IpAddress remoteIp, uint16_t remotePort)
Setup to send a UDP packet to a specified remote.
int32_t PacketRead(unsigned char *dataPtr, uint16_t len)
Reads the current packet received from the UDP session.
uint16_t BytesAvailable()
Number of bytes available to read from the current packet.
int16_t Peek()
Attempts to get the next available character.
uint16_t PacketParse()
Check for the newest incoming UDP packet.
ClearCore UDP session class.
Definition: EthernetUdp.h:43
void End()
Disable the UDP session.
IpAddress RemoteIp()
Returns the remote IP address for the current packet.