knx
ETS configurable knx-stack
bau_systemB_device.cpp
Go to the documentation of this file.
1 #include "bau_systemB_device.h"
2 #include "bits.h"
3 #include <string.h>
4 #include <stdio.h>
5 
7  BauSystemB(platform),
8  _addrTable(_memory),
9  _assocTable(_memory), _groupObjTable(_memory),
10 #ifdef USE_DATASECURE
11  _appLayer(_deviceObj, _secIfObj, *this),
12 #else
13  _appLayer(*this),
14 #endif
15  _transLayer(_appLayer), _netLayer(_deviceObj, _transLayer)
16 {
19 #ifdef USE_DATASECURE
21 #endif
24 
26  _memory.addSaveRestore(&_groupObjTable); // changed order for better memory management
29 #ifdef USE_DATASECURE
31 #endif
32 }
33 
35 {
36  return _appLayer;
37 }
38 
40 {
41  return _groupObjTable;
42 }
43 
45 {
46  _transLayer.loop();
49 #ifdef USE_DATASECURE
50  _appLayer.loop();
51 #endif
52  _memory.loop();
53 }
54 
56 {
57  if (!configured())
58  return;
59 
60  static uint16_t startIdx = 1;
61 
63  uint16_t objCount = table.entryCount();
64 
65  for (uint16_t asap = startIdx; asap <= objCount; asap++)
66  {
67  GroupObject& go = table.get(asap);
68 
69  ComFlag flag = go.commFlag();
70 
71  if (flag != ReadRequest && flag != WriteRequest)
72  continue;
73 
74  if (flag == WriteRequest)
75  {
76 #ifdef SMALL_GROUPOBJECT
78 #else
79  GroupObjectUpdatedHandler handler = go.callback();
80 
81  if (handler)
82  handler(go);
83 
84 #endif
85  }
86 
87  if (!go.communicationEnable())
88  {
89  go.commFlag(Ok);
90  continue;
91  }
92 
93  SecurityControl goSecurity;
94  goSecurity.toolAccess = false; // Secured group communication never uses the toolkey. ETS knows all keys, also the group keys.
95 
96 #ifdef USE_DATASECURE
97  // Get security flags from Security Interface Object for this group object
99 #else
100  goSecurity.dataSecurity = DataSecurity::None;
101 #endif
102 
103  if (flag == WriteRequest && go.transmitEnable())
104  {
105  uint8_t* data = go.valueRef();
107  go.sizeInTelegram());
108  }
109  else if (flag == ReadRequest)
110  {
112  }
113 
115 
116  startIdx = asap + 1;
117  return;
118  }
119 
120  startIdx = 1;
121 }
122 
123 void BauSystemBDevice::updateGroupObject(GroupObject& go, uint8_t* data, uint8_t length)
124 {
125  uint8_t* goData = go.valueRef();
126 
127  if (length != go.valueSize())
128  {
129  go.commFlag(Error);
130  return;
131  }
132 
133  memcpy(goData, data, length);
134 
135  if (go.commFlag() != WriteRequest)
136  {
137  go.commFlag(Updated);
138 #ifdef SMALL_GROUPOBJECT
140 #else
141  GroupObjectUpdatedHandler handler = go.callback();
142 
143  if (handler)
144  handler(go);
145 
146 #endif
147  }
148  else
149  {
150  go.commFlag(Updated);
151  }
152 }
153 
155 {
156  // _configured is set to true initially, if the device was configured with ETS it will be set to true after restart
157 
158  if (!_configured)
159  return false;
160 
165 
166 #ifdef USE_DATASECURE
168 #endif
169 
170  return _configured;
171 }
172 
173 void BauSystemBDevice::doMasterReset(EraseCode eraseCode, uint8_t channel)
174 {
175  BauSystemB::doMasterReset(eraseCode, channel);
176 
177  _addrTable.masterReset(eraseCode, channel);
178  _assocTable.masterReset(eraseCode, channel);
179  _groupObjTable.masterReset(eraseCode, channel);
180 #ifdef USE_DATASECURE
181  _secIfObj.masterReset(eraseCode, channel);
182 #endif
183 }
184 
185 void BauSystemBDevice::groupValueWriteLocalConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl& secCtrl, uint8_t* data, uint8_t dataLength, bool status)
186 {
187  GroupObject& go = _groupObjTable.get(asap);
188 
189  if (status)
190  go.commFlag(Ok);
191  else
192  go.commFlag(Error);
193 }
194 
195 void BauSystemBDevice::groupValueReadLocalConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl& secCtrl, bool status)
196 {
197  GroupObject& go = _groupObjTable.get(asap);
198 
199  if (status)
200  go.commFlag(Ok);
201  else
202  go.commFlag(Error);
203 }
204 
205 void BauSystemBDevice::groupValueReadIndication(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl& secCtrl)
206 {
207 #ifdef USE_DATASECURE
208  DataSecurity requiredGoSecurity;
209 
210  // Get security flags from Security Interface Object for this group object
211  requiredGoSecurity = _secIfObj.getGroupObjectSecurity(asap);
212 
213  if (secCtrl.dataSecurity != requiredGoSecurity)
214  {
215  println("GroupValueRead: access denied due to wrong security flags");
216  return;
217  }
218 
219 #endif
220 
221  GroupObject& go = _groupObjTable.get(asap);
222 
223  if (!go.communicationEnable() || !go.readEnable())
224  return;
225 
226  uint8_t* data = go.valueRef();
227  _appLayer.groupValueReadResponse(AckRequested, asap, priority, hopType, secCtrl, data, go.sizeInTelegram());
228 }
229 
230 void BauSystemBDevice::groupValueReadAppLayerConfirm(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl& secCtrl, uint8_t* data,
231  uint8_t dataLength)
232 {
233  GroupObject& go = _groupObjTable.get(asap);
234 
235  if (!go.communicationEnable() || !go.responseUpdateEnable())
236  return;
237 
238  updateGroupObject(go, data, dataLength);
239 }
240 
241 void BauSystemBDevice::groupValueWriteIndication(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl& secCtrl, uint8_t* data, uint8_t dataLength)
242 {
243 #ifdef USE_DATASECURE
244  DataSecurity requiredGoSecurity;
245 
246  // Get security flags from Security Interface Object for this group object
247  requiredGoSecurity = _secIfObj.getGroupObjectSecurity(asap);
248 
249  if (secCtrl.dataSecurity != requiredGoSecurity)
250  {
251  println("GroupValueWrite: access denied due to wrong security flags");
252  return;
253  }
254 
255 #endif
256  GroupObject& go = _groupObjTable.get(asap);
257 
258  if (!go.communicationEnable() || !go.writeEnable())
259  return;
260 
261  updateGroupObject(go, data, dataLength);
262 }
void println(const char *s)
This is an implementation of the application layer as specified in .
void associationTableObject(AssociationTableObject &assocTable)
void groupValueReadResponse(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t *data, uint8_t dataLength)
void groupValueReadRequest(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl)
void groupValueWriteRequest(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t *data, uint8_t dataLength)
void transportLayer(TransportLayer &layer)
Assigns the TransportLayer to which encoded APDU are submitted to.
void loop() override
void groupValueWriteIndication(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t *data, uint8_t dataLength) override
ApplicationLayer & applicationLayer() override
AssociationTableObject _assocTable
GroupObjectTableObject & groupObjectTable()
void groupValueWriteLocalConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t *data, uint8_t dataLength, bool status) override
SecureApplicationLayer _appLayer
void doMasterReset(EraseCode eraseCode, uint8_t channel) override
BauSystemBDevice(Platform &platform)
TransportLayer _transLayer
NetworkLayerDevice _netLayer
void updateGroupObject(GroupObject &go, uint8_t *data, uint8_t length)
void groupValueReadAppLayerConfirm(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t *data, uint8_t dataLength) override
void groupValueReadLocalConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl, bool status) override
AddressTableObject _addrTable
GroupObjectTableObject _groupObjTable
void groupValueReadIndication(uint16_t asap, Priority priority, HopCountType hopType, const SecurityControl &secCtrl) override
SecurityInterfaceObject _secIfObj
bool configured() override
void nextRestartState()
virtual void doMasterReset(EraseCode eraseCode, uint8_t channel)
Memory _memory
Definition: bau_systemB.h:123
ApplicationProgramObject _appProgram
Definition: bau_systemB.h:125
DeviceObject _deviceObj
Definition: bau_systemB.h:124
This class represents a single group object.
Definition: group_object.h:42
bool writeEnable()
Check if the write flag (W) was set.
bool responseUpdateEnable()
Check if the update flag (U) was set.
size_t sizeInTelegram()
returns the size of the group object in Byte as it is in a telegram.
static void processClassCallback(GroupObject &ko)
ComFlag commFlag()
Return the current state of the group object.
bool transmitEnable()
Check if the transmit flag (T) was set.
uint8_t * valueRef()
returns the pointer to the value of the group object.
void callback(GroupObjectUpdatedHandler handler)
register a callback for this group object.
Priority priority()
Get the priority of the group object.
bool communicationEnable()
Check if the communication flag (C) was set.
size_t valueSize()
returns the size of the group object in Byte.
bool readEnable()
Check if the read flag (R) was set.
GroupObject & get(uint16_t asap)
virtual void masterReset(EraseCode eraseCode, uint8_t channel)
void loop()
Definition: memory.cpp:554
void addSaveRestore(SaveRestore *obj)
Definition: memory.cpp:209
void groupAddressTable(AddressTableObject &addrTable)
void masterReset(EraseCode eraseCode, uint8_t channel) override
DataSecurity getGroupObjectSecurity(uint16_t index)
LoadState loadState()
This method returns the LoadState of the interface object.
void networkLayer(NetworkLayer &layer)
void groupAddressTable(AddressTableObject &addrTable)
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
@ Updated
Group object was updated.
Definition: group_object.h:12
@ Error
there was an error on processing a request
Definition: group_object.h:17
@ Transmitting
Group Object is processed a the moment (read or write)
Definition: group_object.h:15
@ WriteRequest
Write was requested but was not processed.
Definition: group_object.h:14
std::function< void(GroupObject &)> GroupObjectUpdatedHandler
Definition: group_object.h:33
HopCountType
Definition: knx_types.h:124
@ NetworkLayerParameter
use NetworkLayer::hopCount as NPDU::hopCount
Definition: knx_types.h:126
Priority
Definition: knx_types.h:10
EraseCode
Definition: knx_types.h:242
AckType
Definition: knx_types.h:18
@ AckRequested
We want a DataLinkLayer acknowledgement.
Definition: knx_types.h:20
DataSecurity
Definition: knx_types.h:223
@ None
Definition: knx_types.h:224
@ LS_LOADED
Definition: property.h:200
DataSecurity dataSecurity
Definition: knx_types.h:232