embkernel
 All Classes Functions Variables Typedefs Groups Pages
NetUdpSocket.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef NET_UDP_SOCKET_HPP_
7 #define NET_UDP_SOCKET_HPP_
8 
9 #include "NetDefs.hpp"
10 #include "RtosSemaphore.hpp"
11 #include "LibLinkedList.hpp"
12 #include "RtosMsgBox.hpp"
13 #include <stdlib.h>
14 
15 class NetUdpSocket: public LIB_LINKED_LIST_ITERABLE<NetUdpSocket> {
16  friend class NetUdp;
17 
18 public:
19  typedef struct {
20  NetDefs::IP_ADDR ip;
21  NetDefs::UDP_PORT port;
22  uint16_t dataLen;
23  }__attribute__((packed)) PACKET_HEADER;
24 
25  typedef struct {
26  PACKET_HEADER hdr;
27  uint8_t data[NetDefs::UDP_MAX_DATA_LEN]; //
28  }__attribute__((packed)) RX_PACKET;
29 
30  typedef struct {
31  PACKET_HEADER hdr;
32  uint8_t* data; //
33  }__attribute__((packed)) TX_PACKET;
34 
35 private:
36  NetDefs::UDP_PORT mLocalPort;
37  static uint16_t sAutomaticPort;
38  RtosMsgBox<RX_PACKET*, 4> mRxMsgBox;
39 public:
40  NetUdpSocket();
41  ~NetUdpSocket();
42 
43  void open(NetDefs::UDP_PORT localPort = 0);
44  void close();
45 
46  TX_PACKET* allocTxPacket(size_t maxDataLen);
47  void discardTxPacket(TX_PACKET* pkt);
48  bool sendPacket(TX_PACKET& pkt, size_t dataLen);
49 
50  RX_PACKET* recvPacket(Rtos::TICK timeout);
51  void discardRxPacket(RX_PACKET* pkt);
52 };
53 
54 #endif /* NET_UDP_SOCKET_HPP_ */