knx
ETS configurable knx-stack
group_object.cpp
Go to the documentation of this file.
1 #include "group_object.h"
2 #include "bits.h"
3 #include "string.h"
4 #include "datapoint_types.h"
6 
7 #ifdef SMALL_GROUPOBJECT
8 GroupObjectUpdatedHandler GroupObject::_updateHandlerStatic = 0;
9 #endif
10 GroupObjectTableObject* GroupObject::_table = 0;
11 
13 {
14  _data = 0;
15  _commFlag = Uninitialized;
16  _dataLength = 0;
17 #ifndef SMALL_GROUPOBJECT
18  _updateHandler = 0;
19 #endif
20 }
21 
23 {
24  _data = new uint8_t[other._dataLength];
25  _commFlag = other._commFlag;
26  _dataLength = other._dataLength;
27  _asap = other._asap;
28 #ifndef SMALL_GROUPOBJECT
29  _updateHandler = other._updateHandler;
30 #endif
31  memcpy(_data, other._data, _dataLength);
32 }
33 
35 {
36  if (_data)
37  delete[] _data;
38 }
39 
41 {
42  if (!_table)
43  return false;
44 
45  return bitRead(ntohs(_table->_tableData[_asap]), 15) > 0;
46 }
47 
49 {
50  if (!_table)
51  return false;
52 
53  return bitRead(ntohs(_table->_tableData[_asap]), 14) > 0 ;
54 }
55 
57 {
58  if (!_table)
59  return false;
60 
61  return bitRead(ntohs(_table->_tableData[_asap]), 13) > 0;
62 }
63 
65 {
66  if (!_table)
67  return false;
68 
69  return bitRead(ntohs(_table->_tableData[_asap]), 12) > 0 ;
70 }
71 
73 {
74  if (!_table)
75  return false;
76 
77  // we forbid reading of new (uninitialized) go
78  if (_commFlag == Uninitialized)
79  return false;
80 
81  return bitRead(ntohs(_table->_tableData[_asap]), 11) > 0;
82 }
83 
85 {
86  if (!_table)
87  return false;
88 
89  return bitRead(ntohs(_table->_tableData[_asap]), 10) > 0;
90 }
91 
92 
94 {
95  if (!_table)
96  return LowPriority;
97 
98  return (Priority)((ntohs(_table->_tableData[_asap]) >> 6) & (3 << 2)) ;
99 }
100 
102 {
103  return _data;
104 }
105 
107 {
108  return _asap;
109 }
110 
111 size_t GroupObject::goSize()
112 {
113  size_t size = sizeInTelegram();
114  if (size == 0)
115  return 1;
116 
117  return size;
118 }
119 
120 // see knxspec 3.5.1 p. 178
121 size_t GroupObject::asapValueSize(uint8_t code)
122 {
123  if (code < 7)
124  return 0;
125  if (code < 8)
126  return 1;
127  if (code < 11 || (code > 20 && code < 255))
128  return code - 6;
129  switch (code)
130  {
131  case 11:
132  return 6;
133  case 12:
134  return 8;
135  case 13:
136  return 10;
137  case 14:
138  return 14;
139  case 15:
140  return 5;
141  case 16:
142  return 7;
143  case 17:
144  return 9;
145  case 18:
146  return 11;
147  case 19:
148  return 12;
149  case 20:
150  return 13;
151  case 255:
152  return 252;
153  }
154  return -1;
155 }
156 
157 
159 {
160  return _commFlag;
161 }
162 
164 {
165  _commFlag = value;
166 }
167 
169 {
170  _commFlag = ReadRequest;
171 }
172 
174 {
175  _commFlag = WriteRequest;
176 }
177 
179 {
180  return _dataLength;
181 }
182 
184 {
185  uint8_t code = lowByte(ntohs(_table->_tableData[_asap]));
186  return asapValueSize(code);
187 }
188 
189 #ifdef SMALL_GROUPOBJECT
190 GroupObjectUpdatedHandler GroupObject::classCallback()
191 {
192  return _updateHandlerStatic;
193 }
194 
195 void GroupObject::classCallback(GroupObjectUpdatedHandler handler)
196 {
197  _updateHandlerStatic = handler;
198 }
199 
200 void GroupObject::processClassCallback(GroupObject& ko)
201 {
202  if (_updateHandlerStatic != 0)
203  _updateHandlerStatic(ko);
204 }
205 
206 #else
208 {
209  _updateHandler = handler;
210 }
211 
212 
214 {
215  return _updateHandler;
216 }
217 #endif
218 
219 void GroupObject::value(const KNXValue& value, const Dpt& type)
220 {
221  valueNoSend(value, type);
222  objectWritten();
223 }
224 
225 
227 {
228  KNXValue value = "";
229  KNX_Decode_Value(_data, _dataLength, type, value);
230  return value;
231 }
232 
233 bool GroupObject::tryValue(KNXValue& value, const Dpt& type)
234 {
235  return KNX_Decode_Value(_data, _dataLength, type, value);
236 }
237 
238 #ifndef SMALL_GROUPOBJECT
240 {
241  _datapointType = value;
242 }
243 
244 
246 {
247  return _datapointType;
248 }
249 
250 
252 {
253  return tryValue(value, _datapointType);
254 }
255 
256 
257 void GroupObject::value(const KNXValue& value)
258 {
259  this->value(value, _datapointType);
260 }
261 
262 
264 {
265  return value(_datapointType);
266 }
267 
268 
270 {
271  valueNoSend(value, _datapointType);
272 }
273 #endif
274 
275 void GroupObject::valueNoSend(const KNXValue& value, const Dpt& type)
276 {
277  if (_commFlag == Uninitialized)
278  _commFlag = Ok;
279 
280  KNX_Encode_Value(value, _data, _dataLength, type);
281 }
Definition: dpt.h:364
This class represents a single group object.
Definition: group_object.h:42
bool writeEnable()
Check if the write flag (W) was set.
GroupObjectUpdatedHandler callback()
returns the registered callback
uint16_t asap()
returns the Application Service Access Point of the group object.
bool responseUpdateEnable()
Check if the update flag (U) was set.
virtual ~GroupObject()
The destructor.
size_t sizeInTelegram()
returns the size of the group object in Byte as it is in a telegram.
ComFlag commFlag()
Return the current state of the group object.
KNXValue value()
return the current value of the group object.
bool transmitEnable()
Check if the transmit flag (T) was set.
GroupObject()
The constructor.
void objectWritten()
Mark a communication object as written.
uint8_t * valueRef()
returns the pointer to the value of the group object.
void requestObjectRead()
Request the read of a communication object.
Priority priority()
Get the priority of the group object.
bool valueReadOnInit()
Check if the initialisation flag (I) was set.
bool communicationEnable()
Check if the communication flag (C) was set.
void valueNoSend(const KNXValue &value, const Dpt &type)
set the current value of the group object.
bool tryValue(KNXValue &value, const Dpt &type)
set the current value of the group object.
size_t valueSize()
returns the size of the group object in Byte.
Dpt dataPointType()
returns the currently configured datapoint type.
bool readEnable()
Check if the read flag (R) was set.
int KNX_Decode_Value(uint8_t *payload, size_t payload_length, const Dpt &datatype, KNXValue &value)
Converts the KNX Payload given by the specific DPT and puts the value in the KNXValue struc.
Definition: dptconvert.cpp:13
int KNX_Encode_Value(const KNXValue &value, uint8_t *payload, size_t payload_length, const Dpt &datatype)
Converts the KNXValue struct to the KNX Payload as the specific DPT.
Definition: dptconvert.cpp:136
ComFlag
Definition: group_object.h:11
@ Ok
read or write request were send successfully
Definition: group_object.h:16
@ ReadRequest
Read was requested but was not processed.
Definition: group_object.h:13
@ Uninitialized
uninitialized Group Object, its value is not valid
Definition: group_object.h:18
@ WriteRequest
Write was requested but was not processed.
Definition: group_object.h:14
std::function< void(GroupObject &)> GroupObjectUpdatedHandler
Definition: group_object.h:33
Priority
Definition: knx_types.h:10
@ LowPriority
Normal priority of group communication.
Definition: knx_types.h:11