knx
ETS configurable knx-stack
tpdu.cpp
Go to the documentation of this file.
1 #include "tpdu.h"
2 #include "cemi_frame.h"
3 
4 TPDU::TPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame)
5 {
6 }
7 
9 {
10  if (control())
11  {
12  if (numbered())
13  {
14  if ((_data[0] & 1) == 0)
15  return Ack;
16  else
17  return Nack;
18  }
19  else if ((_data[0] & 1) == 0)
20  return Connect;
21  else
22  return Disconnect;
23  }
24  else
25  {
26  if (_frame.addressType() == GroupAddress)
27  {
28  if (_frame.destinationAddress() == 0)
29  return DataBroadcast;
30  else
31  return DataGroup;
32  }
33  else if (numbered())
34  return DataConnected;
35  else
36  return DataInduvidual;
37  }
38 }
39 
40 void TPDU::type(TpduType type)
41 {
42  switch (type)
43  {
44  case DataBroadcast:
45  case DataGroup:
46  case DataInduvidual:
47  _data[0] &= 0x3;
48  break;
49 
50  case DataConnected:
51  _data[0] &= 0xC3;
52  _data[0] |= 0x40;
53  break;
54 
55  case Connect:
56  _data[0] = 0x80;
57  break;
58 
59  case Disconnect:
60  _data[0] = 0x81;
61  break;
62 
63  case Ack:
64  _data[0] &= ~0xC3;
65  _data[0] |= 0xC2;
66  break;
67 
68  case Nack:
69  _data[0] |= 0xC3;
70  break;
71  }
72 }
73 
74 bool TPDU::numbered() const
75 {
76  return (_data[0] & 0x40) > 0;
77 }
78 
79 void TPDU::numbered(bool value)
80 {
81  if (value)
82  _data[0] |= 0x40;
83  else
84  _data[0] &= ~0x40;
85 }
86 
87 bool TPDU::control() const
88 {
89  return (_data[0] & 0x80) > 0;
90 }
91 
92 void TPDU::control(bool value)
93 {
94  if (value)
95  _data[0] |= 0x80;
96  else
97  _data[0] &= ~0x80;
98 }
99 
100 uint8_t TPDU::sequenceNumber() const
101 {
102  return ((_data[0] >> 2) & 0xF);
103 }
104 
105 void TPDU::sequenceNumber(uint8_t value)
106 {
107  _data[0] &= ~(0xF << 2);
108  _data[0] |= ((value & 0xF) << 2);
109 }
110 
112 {
113  return _frame.apdu();
114 }
115 
117 {
118  return _frame;
119 }
120 
122 {
123  /* print.print("TPDU: ");
124  print.print(type(), HEX, 2);
125  print.print(" ");
126  for (uint8_t i = 0; i < apdu().length() + 1; ++i)
127  {
128  if (i) print.print(" ");
129  print.print(_data[i], HEX, 2);
130  }
131  print.println()*/;
132 }
This class represents an Application Protocol Data Unit.
Definition: apdu.h:12
APDU & apdu()
Definition: cemi_frame.cpp:367
uint16_t destinationAddress() const
Definition: cemi_frame.cpp:315
AddressType addressType() const
Definition: cemi_frame.cpp:281
TpduType type() const
Definition: tpdu.cpp:8
bool numbered() const
Definition: tpdu.cpp:74
bool control() const
Definition: tpdu.cpp:87
void printPDU()
Definition: tpdu.cpp:121
uint8_t sequenceNumber() const
Definition: tpdu.cpp:100
TPDU(uint8_t *data, CemiFrame &frame)
Definition: tpdu.cpp:4
APDU & apdu()
Definition: tpdu.cpp:111
CemiFrame & frame()
Definition: tpdu.cpp:116
TpduType
Definition: knx_types.h:130
@ DataConnected
Definition: knx_types.h:134
@ Ack
Definition: knx_types.h:137
@ DataBroadcast
Definition: knx_types.h:131
@ DataInduvidual
Definition: knx_types.h:133
@ Nack
Definition: knx_types.h:138
@ Disconnect
Definition: knx_types.h:136
@ Connect
Definition: knx_types.h:135
@ DataGroup
Definition: knx_types.h:132
@ GroupAddress
Definition: knx_types.h:35