Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / qpg6100Config.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *          Utilities for accessing persisted device configuration on
21  *          platforms based on the Qorvo QPG6100 SDK.
22  */
23 /* this file behaves like a config.h, comes first */
24 #include <platform/internal/CHIPDeviceLayerInternal.h>
25
26 #include <platform/qpg6100/qpg6100Config.h>
27
28 #include <core/CHIPEncoding.h>
29 #include <lib/core/CHIPSafeCasts.h>
30 #include <platform/internal/testing/ConfigUnitTest.h>
31 #include <support/CodeUtils.h>
32 #include <support/logging/CHIPLogging.h>
33
34 #include "FreeRTOS.h"
35 #include "qvCHIP.h"
36
37 namespace chip {
38 namespace DeviceLayer {
39 namespace Internal {
40
41 CHIP_ERROR QPG6100Config::Init()
42 {
43     qvCHIP_Nvm_Init();
44
45     return CHIP_NO_ERROR;
46 }
47
48 uint16_t QPG6100Config::GetSettingsMaxValueLength(Key key)
49 {
50     uint16_t keyLen;
51
52     return (qvCHIP_Nvm_GetMaxKeyLen(key, &keyLen) == QV_STATUS_NO_ERROR) ? keyLen : 0;
53 }
54
55 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, bool & val)
56 {
57     uint16_t length;
58
59     length = sizeof(bool);
60
61     return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
62 }
63
64 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, uint32_t & val)
65 {
66     uint16_t length;
67
68     length = sizeof(uint32_t);
69
70     return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
71 }
72
73 CHIP_ERROR QPG6100Config::ReadConfigValue(Key key, uint64_t & val)
74 {
75     uint16_t length;
76
77     length = sizeof(uint64_t);
78
79     return MapNVMError(qvCHIP_Nvm_Restore(key, reinterpret_cast<uint8_t *>(&val), &length));
80 }
81
82 CHIP_ERROR QPG6100Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen)
83 {
84     qvStatus_t res;
85     uint16_t length;
86
87     if ((buf == NULL) || (bufSize > UINT16_MAX))
88     {
89         return CHIP_ERROR_INVALID_ARGUMENT;
90     }
91
92     length = static_cast<uint16_t>(bufSize);
93     res    = qvCHIP_Nvm_Restore(key, Uint8::from_char(buf), &length);
94     if (length > bufSize)
95     {
96         return CHIP_ERROR_BUFFER_TOO_SMALL;
97     }
98     if (res == QV_STATUS_NO_ERROR)
99     {
100         outLen      = length;
101         buf[outLen] = 0;
102     }
103
104     return MapNVMError(res);
105 }
106
107 CHIP_ERROR QPG6100Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen)
108 {
109     uint8_t buffer[255];
110     uint16_t length;
111     qvStatus_t res;
112
113     if ((buf == NULL) || (bufSize > UINT16_MAX))
114     {
115         return CHIP_ERROR_INVALID_ARGUMENT;
116     }
117
118     length = static_cast<uint16_t>(bufSize);
119
120     res = qvCHIP_Nvm_Restore(key, buffer, &length);
121     if (res == QV_STATUS_NO_ERROR)
122     {
123         outLen = length;
124         if (outLen > bufSize)
125         {
126             return CHIP_ERROR_BUFFER_TOO_SMALL;
127         }
128
129         memcpy(buf, buffer, outLen);
130         buf[outLen] = 0;
131     }
132
133     return MapNVMError(res);
134 }
135
136 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, bool val)
137 {
138     uint16_t length;
139
140     length = sizeof(bool);
141
142     return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
143 }
144
145 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, uint32_t val)
146 {
147     uint16_t length;
148
149     length = sizeof(uint32_t);
150
151     return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
152 }
153
154 CHIP_ERROR QPG6100Config::WriteConfigValue(Key key, uint64_t val)
155 {
156     uint16_t length;
157
158     length = sizeof(uint64_t);
159
160     return MapNVMError(qvCHIP_Nvm_Backup(key, reinterpret_cast<uint8_t *>(&val), length));
161 }
162
163 CHIP_ERROR QPG6100Config::WriteConfigValueStr(Key key, const char * str)
164 {
165     return WriteConfigValueStr(key, str, (str != NULL) ? strlen(str) : 0);
166 }
167
168 CHIP_ERROR QPG6100Config::WriteConfigValueStr(Key key, const char * str, size_t strLen)
169 {
170     if (strLen > UINT16_MAX)
171     {
172         return CHIP_ERROR_INVALID_ARGUMENT;
173     }
174     if (str == NULL)
175     {
176         qvCHIP_Nvm_ClearValue(key);
177     }
178     else
179     {
180         return MapNVMError(qvCHIP_Nvm_Backup(key, Uint8::from_const_char(str), static_cast<uint16_t>(strLen)));
181     }
182
183     return CHIP_NO_ERROR;
184 }
185
186 CHIP_ERROR QPG6100Config::WriteConfigValueBin(Key key, const uint8_t * data, size_t dataLen)
187 {
188     if (dataLen > UINT16_MAX)
189     {
190         return CHIP_ERROR_BUFFER_TOO_SMALL;
191     }
192     if (data == NULL)
193     {
194         qvCHIP_Nvm_ClearValue(key);
195     }
196     else
197     {
198         return MapNVMError(qvCHIP_Nvm_Backup(key, data, static_cast<uint16_t>(dataLen)));
199     }
200
201     return CHIP_NO_ERROR;
202 }
203
204 CHIP_ERROR QPG6100Config::ClearConfigValue(Key key)
205 {
206     qvCHIP_Nvm_ClearValue(key);
207     return CHIP_NO_ERROR;
208 }
209
210 bool QPG6100Config::ConfigValueExists(Key key)
211 {
212     return qvCHIP_Nvm_ValueExists(key, NULL);
213 }
214
215 CHIP_ERROR QPG6100Config::FactoryResetConfig(void)
216 {
217     for (Key key = kMinConfigKey_ChipConfig; key <= kMaxConfigKey_ChipConfig; key++)
218     {
219         ClearConfigValue(key);
220     }
221
222     return CHIP_NO_ERROR;
223 }
224
225 CHIP_ERROR QPG6100Config::ForEachRecord(Key firstKey, Key lastKey, bool addNewRecord, ForEachRecordFunct funct)
226 {
227     CHIP_ERROR err = CHIP_NO_ERROR;
228     uint16_t length;
229
230     for (Key nvmKey = firstKey; nvmKey <= lastKey; ++nvmKey)
231     {
232         if (qvCHIP_Nvm_ValueExists(nvmKey, &length))
233         {
234             if (!addNewRecord)
235             {
236                 // Invoke the caller's function
237                 // (for retrieve,store,delete,enumerate GroupKey operations).
238                 err = funct(nvmKey, length);
239             }
240         }
241         else
242         {
243             if (addNewRecord)
244             {
245                 // Invoke caller's function
246                 // (for add GroupKey operation).
247                 err = funct(nvmKey, length);
248             }
249         }
250
251         SuccessOrExit(err);
252     }
253
254 exit:
255     return err;
256 }
257
258 CHIP_ERROR QPG6100Config::MapNVMError(qvStatus_t aStatus)
259 {
260     switch (aStatus)
261     {
262     case QV_STATUS_NO_ERROR:
263         return CHIP_NO_ERROR;
264     case QV_STATUS_BUFFER_TOO_SMALL:
265         return CHIP_ERROR_BUFFER_TOO_SMALL;
266     case QV_STATUS_INVALID_ARGUMENT:
267         return CHIP_ERROR_INVALID_ARGUMENT;
268     case QV_STATUS_KEY_LEN_TOO_SMALL:
269         return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
270     case QV_STATUS_INVALID_DATA:
271         return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
272     default:
273         break;
274     }
275     return CHIP_ERROR_INTERNAL;
276 }
277
278 void QPG6100Config::RunConfigUnitTest()
279 {
280     // Run common unit test
281     ::chip::DeviceLayer::Internal::RunConfigUnitTest<QPG6100Config>();
282 }
283
284 } // namespace Internal
285 } // namespace DeviceLayer
286 } // namespace chip