knx
ETS configurable knx-stack
device_object.cpp
Go to the documentation of this file.
1 #include <cstring>
2 #include "device_object.h"
3 #include "bits.h"
4 #include "data_property.h"
5 #include "callback_property.h"
6 #include "config.h"
7 
8 #define LEN_KNX_SERIAL 6
9 
11 {
12  // Default to KNXA (0xFA)
13  // Note: ETS does not accept a SN 00FA00000000 in data secure mode.
14  // ETS says that 00FA00000000 looks "suspicious" in the log file.
15  uint8_t serialNumber[] = {0x00, 0xFA, 0x01, 0x02, 0x03, 0x04};
16  uint8_t hardwareType[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
17 
18  Property* properties[] =
19  {
21  new DataProperty(PID_SERIAL_NUMBER, false, PDT_GENERIC_06, 1, ReadLv3 | WriteLv0, serialNumber),
23  [](DeviceObject * io, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t
24  {
25  if (start == 0)
26  {
27  uint16_t currentNoOfElements = 1;
28  pushWord(currentNoOfElements, data);
29  return 1;
30  }
31 
33  return 1;
34  }),
35  new DataProperty(PID_DEVICE_CONTROL, true, PDT_BITSET8, 1, ReadLv3 | WriteLv3, (uint8_t)0),
37  new DataProperty(PID_VERSION, false, PDT_VERSION, 1, ReadLv3 | WriteLv0, (uint16_t)3),
38  new DataProperty(PID_ROUTING_COUNT, true, PDT_UNSIGNED_CHAR, 1, ReadLv3 | WriteLv3, (uint8_t)(6 << 4)),
40  [](DeviceObject * io, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t
41  {
42  if (start == 0)
43  {
44  uint16_t currentNoOfElements = 1;
45  pushWord(currentNoOfElements, data);
46  return 1;
47  }
48 
49  *data = io->_prgMode;
50  return 1;
51  },
52  [](DeviceObject * io, uint16_t start, uint8_t count, const uint8_t* data) -> uint8_t
53  {
54  if (start == 0)
55  return 1;
56 
57  io->_prgMode = *data;
58  return 1;
59  }),
60  new DataProperty(PID_MAX_APDU_LENGTH, false, PDT_UNSIGNED_INT, 1, ReadLv3 | WriteLv0, (uint16_t)254),
62  [](DeviceObject * io, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t
63  {
64  if (start == 0)
65  {
66  uint16_t currentNoOfElements = 1;
67  pushWord(currentNoOfElements, data);
68  return 1;
69  }
70 
71  *data = ((io->_ownAddress >> 8) & 0xff);
72 
73  return 1;
74  }),
76  [](DeviceObject * io, uint16_t start, uint8_t count, uint8_t* data) -> uint8_t
77  {
78  if (start == 0)
79  {
80  uint16_t currentNoOfElements = 1;
81  pushWord(currentNoOfElements, data);
82  return 1;
83  }
84 
85  *data = (io->_ownAddress & 0xff);
86  return 1;
87  }),
91 #ifdef USE_RF
93 #endif
94  };
95  initializeProperties(sizeof(properties), properties);
96 }
97 
98 uint8_t* DeviceObject::save(uint8_t* buffer)
99 {
100  buffer = pushWord(_ownAddress, buffer);
101  return InterfaceObject::save(buffer);
102 }
103 
104 const uint8_t* DeviceObject::restore(const uint8_t* buffer)
105 {
106  buffer = popWord(_ownAddress, buffer);
107  return InterfaceObject::restore(buffer);
108 }
109 
111 {
112  return 2 + InterfaceObject::saveSize();
113 }
114 
116 {
117  return _ownAddress;
118 }
119 
121 {
122  _ownAddress = value;
123 }
124 
125 #define USER_STOPPED 0x1
126 #define OWN_ADDR_DUPL 0x2
127 #define VERIFY_MODE 0x4
128 #define SAFE_STATE 0x8
129 
130 
132 {
134  uint8_t data;
135  prop->read(data);
136 
137  if (value)
138  data |= OWN_ADDR_DUPL;
139  else
140  data &= ~OWN_ADDR_DUPL;
141 
142  prop->write(data);
143 }
144 
146 {
148  uint8_t data;
149  prop->read(data);
150  return (data & VERIFY_MODE) > 0;
151 }
152 
153 void DeviceObject::verifyMode(bool value)
154 {
156  uint8_t data;
157  prop->read(data);
158 
159  if (value)
160  data |= VERIFY_MODE;
161  else
162  data &= ~VERIFY_MODE;
163 
164  prop->write(data);
165 }
166 
168 {
169  return _prgMode == 1;
170 }
171 
172 void DeviceObject::progMode(bool value)
173 {
174  if (value)
175  _prgMode = 1;
176  else
177  _prgMode = 0;
178 }
179 
181 {
182  uint16_t manufacturerId;
184  return manufacturerId;
185 }
186 
187 void DeviceObject::manufacturerId(uint16_t value)
188 {
189  uint8_t data[LEN_KNX_SERIAL];
190  memcpy(data, propertyData(PID_SERIAL_NUMBER), LEN_KNX_SERIAL);
191  pushWord(value, data);
193 }
194 
196 {
197  uint32_t bauNumber;
199  return bauNumber;
200 }
201 
202 void DeviceObject::bauNumber(uint32_t value)
203 {
204  uint8_t data[LEN_KNX_SERIAL];
205  memcpy(data, propertyData(PID_SERIAL_NUMBER), LEN_KNX_SERIAL);
206  pushInt(value, data + 2);
208 }
209 
211 {
213  return prop->data();
214 }
215 
216 void DeviceObject::orderNumber(const uint8_t* value)
217 {
219  prop->write(value);
220 }
221 
223 {
225  return prop->data();
226 }
227 
228 void DeviceObject::hardwareType(const uint8_t* value)
229 {
231  prop->write(value);
232 }
233 
235 {
236  Property* prop = property(PID_VERSION);
237  uint16_t value;
238  prop->read(value);
239  return value;
240 }
241 
242 void DeviceObject::version(uint16_t value)
243 {
244  Property* prop = property(PID_VERSION);
245  prop->write(value);
246 }
247 
249 {
251  uint16_t value;
252  prop->read(value);
253  return value;
254 }
255 
256 void DeviceObject::maskVersion(uint16_t value)
257 {
259  prop->write(value);
260 }
261 
263 {
265  uint16_t value;
266  prop->read(value);
267  return value;
268 }
269 
270 void DeviceObject::maxApduLength(uint16_t value)
271 {
273  prop->write(value);
274 }
275 
277 {
279  return prop->data();
280 }
281 
282 void DeviceObject::rfDomainAddress(uint8_t* value)
283 {
285  prop->write(value);
286 }
287 
289 {
291  uint8_t value;
292  prop->read(value);
293  return (value >> 4) & 0x07;
294 }
uint8_t * pushInt(uint32_t i, uint8_t *data)
Definition: bits.cpp:72
uint8_t * pushByteArray(const uint8_t *src, uint32_t size, uint8_t *data)
Definition: bits.cpp:82
uint8_t * pushWord(uint16_t w, uint8_t *data)
Definition: bits.cpp:64
const uint8_t * popInt(uint32_t &i, const uint8_t *data)
Definition: bits.cpp:41
const uint8_t * popWord(uint16_t &w, const uint8_t *data)
Definition: bits.cpp:34
const uint8_t * data()
const uint8_t * hardwareType()
uint16_t maskVersion()
const uint8_t * rfDomainAddress()
uint16_t saveSize() override
uint16_t manufacturerId()
const uint8_t * restore(const uint8_t *buffer) override
This method is called when the object should restore its state from the buffer.
const uint8_t * orderNumber()
uint16_t maxApduLength()
uint16_t version()
void individualAddressDuplication(bool value)
uint8_t defaultHopCount()
uint8_t * save(uint8_t *buffer) override
This method is called when the object should save its state to the buffer.
uint32_t bauNumber()
uint16_t individualAddress()
uint8_t * save(uint8_t *buffer) override
This method is called when the object should save its state to the buffer.
const uint8_t * propertyData(PropertyID id)
const uint8_t * restore(const uint8_t *buffer) override
This method is called when the object should restore its state from the buffer.
virtual void initializeProperties(size_t propertiesSize, Property **properties)
Intializes the Property-array the the supplied values.
uint16_t saveSize() override
T propertyValue(PropertyID id)
Property * property(PropertyID id)
Gets property with PropertyID id if it exists and nullptr otherwise.
virtual uint8_t read(uint16_t start, uint8_t count, uint8_t *data) const =0
virtual uint8_t write(uint16_t start, uint8_t count, const uint8_t *data)=0
@ OT_DEVICE
Device object.
@ PID_VERSION
Definition: property.h:86
@ PID_DEVICE_DESCRIPTOR
Definition: property.h:101
@ PID_DEVICE_ADDR
Definition: property.h:97
@ PID_DEVICE_CONTROL
Definition: property.h:81
@ PID_MAX_APDU_LENGTH
Definition: property.h:95
@ PID_SUBNET_ADDR
Definition: property.h:96
@ PID_OBJECT_TYPE
Interface Object Type independent Properties.
Definition: property.h:72
@ PID_HARDWARE_TYPE
Definition: property.h:99
@ PID_SERIAL_NUMBER
Definition: property.h:78
@ PID_ORDER_INFO
Definition: property.h:82
@ PID_ROUTING_COUNT
Properties in the Device Object.
Definition: property.h:93
@ PID_IO_LIST
Definition: property.h:98
@ PID_PROG_MODE
Definition: property.h:94
@ PID_RF_DOMAIN_ADDRESS_CEMI_SERVER
Definition: property.h:100
@ PID_MANUFACTURER_ID
Definition: property.h:79
@ PDT_GENERIC_06
length: 6
Definition: property.h:40
@ PDT_GENERIC_02
length: 2
Definition: property.h:36
@ PDT_BITSET8
length: 3
Definition: property.h:59
@ PDT_GENERIC_10
length: 10
Definition: property.h:44
@ PDT_VERSION
length: 3
Definition: property.h:56
@ PDT_UNSIGNED_INT
length: 2
Definition: property.h:22
@ PDT_UNSIGNED_CHAR
length: 1
Definition: property.h:20
@ WriteLv0
Definition: property.h:247
@ WriteLv3
Definition: property.h:250
@ ReadLv3
Definition: property.h:246