@@ -36,6 +36,88 @@ Nrf::PersistentStorageNode CreateIndexNode(uint8_t bridgedDeviceIndex, Nrf::Pers
3636namespace Nrf
3737{
3838
39+ template <> bool BridgeStorageManager::LoadBridgedDevice (BridgedDevice &device, uint8_t index)
40+ {
41+ Nrf::PersistentStorageNode id = CreateIndexNode (index, &mBridgedDevice );
42+ size_t readSize = 0 ;
43+ uint8_t buffer[sizeof (BridgedDevice) + kMaxUserDataSize ];
44+ uint16_t counter = 0 ;
45+ const uint8_t mandatoryItemsSize =
46+ sizeof (device.mEndpointId ) + sizeof (device.mDeviceType ) + sizeof (device.mUniqueIDLength );
47+
48+ if (!Nrf::GetPersistentStorage ().NonSecureLoad (&id, buffer, sizeof (BridgedDevice) + kMaxUserDataSize ,
49+ readSize)) {
50+ return false ;
51+ }
52+
53+ /* Validate that read size is big enough to include mandatory data. */
54+ if (readSize < mandatoryItemsSize) {
55+ return false ;
56+ }
57+
58+ /* Deserialize data and copy it from buffer into structure's fields. */
59+ memcpy (&device.mEndpointId , buffer, sizeof (device.mEndpointId ));
60+ counter += sizeof (device.mEndpointId );
61+ memcpy (&device.mDeviceType , buffer + counter, sizeof (device.mDeviceType ));
62+ counter += sizeof (device.mDeviceType );
63+ memcpy (&device.mUniqueIDLength , buffer + counter, sizeof (device.mUniqueIDLength ));
64+ counter += sizeof (device.mUniqueIDLength );
65+
66+ /* Validate that read size is big enough to include all expected data. */
67+ if (readSize < counter + device.mUniqueIDLength ) {
68+ return false ;
69+ }
70+
71+ memcpy (device.mUniqueID , buffer + counter, device.mUniqueIDLength );
72+ counter += device.mUniqueIDLength ;
73+
74+ /* Validate that read size is big enough to include all expected data. */
75+ if (readSize < counter + sizeof (device.mNodeLabelLength )) {
76+ return false ;
77+ }
78+
79+ memcpy (&device.mNodeLabelLength , buffer + counter, sizeof (device.mNodeLabelLength ));
80+ counter += sizeof (device.mNodeLabelLength );
81+
82+ /* Validate that read size is big enough to include all expected data. */
83+ if (readSize < counter + device.mNodeLabelLength ) {
84+ return false ;
85+ }
86+
87+ memcpy (device.mNodeLabel , buffer + counter, device.mNodeLabelLength );
88+ counter += device.mNodeLabelLength ;
89+
90+ /* Check if user prepared a buffer for reading user data. It can be nullptr if not needed. */
91+ if (!device.mUserData ) {
92+ device.mUserDataSize = 0 ;
93+ return true ;
94+ }
95+
96+ uint8_t inUserDataSize = device.mUserDataSize ;
97+ /* Validate if user buffer size is big enough to fit the stored user data size. */
98+ if (readSize < counter + sizeof (device.mUserDataSize )) {
99+ return false ;
100+ }
101+
102+ memcpy (&device.mUserDataSize , buffer + counter, sizeof (device.mUserDataSize ));
103+ counter += sizeof (device.mUserDataSize );
104+
105+ /* Validate that user data size value read from the storage is not bigger than the one expected by the user. */
106+ if (inUserDataSize < device.mUserDataSize ) {
107+ return false ;
108+ }
109+
110+ /* Validate that read size is big enough to include all expected data. */
111+ if (readSize < counter + device.mUserDataSize ) {
112+ return false ;
113+ }
114+
115+ memcpy (device.mUserData , buffer + counter, device.mUserDataSize );
116+ counter += device.mUserDataSize ;
117+
118+ return true ;
119+ }
120+
39121bool BridgeStorageManager::Init ()
40122{
41123 bool result = Nrf::GetPersistentStorage ().NonSecureInit ();
@@ -189,87 +271,6 @@ bool BridgeStorageManager::RemoveBridgedDeviceType(uint8_t bridgedDeviceIndex)
189271 return Nrf::GetPersistentStorage ().NonSecureRemove (&id);
190272}
191273
192- bool BridgeStorageManager::LoadBridgedDevice (BridgedDevice &device, uint8_t index)
193- {
194- Nrf::PersistentStorageNode id = CreateIndexNode (index, &mBridgedDevice );
195- size_t readSize = 0 ;
196- uint8_t buffer[sizeof (BridgedDevice) + kMaxUserDataSize ];
197- uint16_t counter = 0 ;
198- const uint8_t mandatoryItemsSize =
199- sizeof (device.mEndpointId ) + sizeof (device.mDeviceType ) + sizeof (device.mUniqueIDLength );
200-
201- if (!Nrf::GetPersistentStorage ().NonSecureLoad (&id, buffer, sizeof (BridgedDevice) + kMaxUserDataSize , readSize)) {
202- return false ;
203- }
204-
205- /* Validate that read size is big enough to include mandatory data. */
206- if (readSize < mandatoryItemsSize) {
207- return false ;
208- }
209-
210- /* Deserialize data and copy it from buffer into structure's fields. */
211- memcpy (&device.mEndpointId , buffer, sizeof (device.mEndpointId ));
212- counter += sizeof (device.mEndpointId );
213- memcpy (&device.mDeviceType , buffer + counter, sizeof (device.mDeviceType ));
214- counter += sizeof (device.mDeviceType );
215- memcpy (&device.mUniqueIDLength , buffer + counter, sizeof (device.mUniqueIDLength ));
216- counter += sizeof (device.mUniqueIDLength );
217-
218- /* Validate that read size is big enough to include all expected data. */
219- if (readSize < counter + device.mUniqueIDLength ) {
220- return false ;
221- }
222-
223- memcpy (device.mUniqueID , buffer + counter, device.mUniqueIDLength );
224- counter += device.mUniqueIDLength ;
225-
226- /* Validate that read size is big enough to include all expected data. */
227- if (readSize < counter + sizeof (device.mNodeLabelLength )) {
228- return false ;
229- }
230-
231- memcpy (&device.mNodeLabelLength , buffer + counter, sizeof (device.mNodeLabelLength ));
232- counter += sizeof (device.mNodeLabelLength );
233-
234- /* Validate that read size is big enough to include all expected data. */
235- if (readSize < counter + device.mNodeLabelLength ) {
236- return false ;
237- }
238-
239- memcpy (device.mNodeLabel , buffer + counter, device.mNodeLabelLength );
240- counter += device.mNodeLabelLength ;
241-
242- /* Check if user prepared a buffer for reading user data. It can be nullptr if not needed. */
243- if (!device.mUserData ) {
244- device.mUserDataSize = 0 ;
245- return true ;
246- }
247-
248- uint8_t inUserDataSize = device.mUserDataSize ;
249- /* Validate if user buffer size is big enough to fit the stored user data size. */
250- if (readSize < counter + sizeof (device.mUserDataSize )) {
251- return false ;
252- }
253-
254- memcpy (&device.mUserDataSize , buffer + counter, sizeof (device.mUserDataSize ));
255- counter += sizeof (device.mUserDataSize );
256-
257- /* Validate that user data size value read from the storage is not bigger than the one expected by the user. */
258- if (inUserDataSize < device.mUserDataSize ) {
259- return false ;
260- }
261-
262- /* Validate that read size is big enough to include all expected data. */
263- if (readSize < counter + device.mUserDataSize ) {
264- return false ;
265- }
266-
267- memcpy (device.mUserData , buffer + counter, device.mUserDataSize );
268- counter += device.mUserDataSize ;
269-
270- return true ;
271- }
272-
273274bool BridgeStorageManager::StoreBridgedDevice (BridgedDevice &device, uint8_t index)
274275{
275276 uint8_t buffer[sizeof (BridgedDevice) + kMaxUserDataSize ];
0 commit comments