Skip to content

Commit 27d5995

Browse files
committed
[nrfconnect] Read PartNumber, URL and Label from factory data
Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
1 parent b21f56a commit 27d5995

6 files changed

+46
-33
lines changed

scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py

+3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def generate_json(self):
302302
self._add_entry("vendor_name", self._args.vendor_name)
303303
self._add_entry("product_name", self._args.product_name)
304304
self._add_entry("product_label", self._args.product_label)
305+
self._add_entry("product_url", self._args.product_url)
305306
self._add_entry("part_number", self._args.part_number)
306307
self._add_entry("date", self._args.date)
307308
self._add_entry("hw_ver", self._args.hw_ver)
@@ -440,6 +441,8 @@ def base64_str(s): return base64.b64decode(s)
440441
the setup code. Discriminator is used during a discovery process.")
441442

442443
# optional keys
444+
optional_arguments.add_argument("--product_url", type=str,
445+
help="[string] provide link to product-specific web page")
443446
optional_arguments.add_argument("--product_label", type=str,
444447
help="[string] provide human-readable product label")
445448
optional_arguments.add_argument("--part_number", type=str,

scripts/tools/nrfconnect/nrfconnect_factory_data.schema

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
"type": "string",
6161
"maxLength": 64
6262
},
63+
"product_url": {
64+
"description": "link to product-specific web page",
65+
"type": "string",
66+
"maxLength": 256
67+
},
6368
"part_number": {
6469
"description": "human-readable vendor assigned part number",
6570
"type": "string",

scripts/tools/nrfconnect/tests/test_generate_factory_data.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ def test_generate_factory_data_all_specified(self):
154154
'--product_id', '0xABCD',
155155
'--vendor_name', 'Nordic Semiconductor ASA',
156156
'--product_name', 'Lock Gen2',
157-
'--product_label', 'Lock',
158157
'--part_number', 'PCA10056',
158+
'--product_url', 'https://nonexistentlockfactory.com/lock',
159+
'--product_label', 'Lock',
159160
'--date', '2022-07-20',
160161
'--hw_ver', '101',
161162
'--hw_ver_str', 'v1.1',
@@ -180,8 +181,9 @@ def test_generate_factory_data_all_specified(self):
180181
self.assertEqual(factory_data.get('product_id'), 0xABCD)
181182
self.assertEqual(factory_data.get('vendor_name'), 'Nordic Semiconductor ASA')
182183
self.assertEqual(factory_data.get('product_name'), 'Lock Gen2')
183-
self.assertEqual(factory_data.get('product_label'), 'Lock')
184184
self.assertEqual(factory_data.get('part_number'), 'PCA10056')
185+
self.assertEqual(factory_data.get('product_url'), 'https://nonexistentlockfactory.com/lock')
186+
self.assertEqual(factory_data.get('product_label'), 'Lock')
185187
self.assertEqual(factory_data.get('date'), '2022-07-20')
186188
self.assertEqual(factory_data.get('hw_ver'), 101)
187189
self.assertEqual(factory_data.get('hw_ver_str'), 'v1.1')

src/platform/nrfconnect/FactoryDataParser.c

+12
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ bool ParseFactoryData(uint8_t * buffer, uint16_t bufferSize, struct FactoryData
149149
{
150150
res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->product_name);
151151
}
152+
else if (strncmp("part_number", (const char *) currentString.value, currentString.len) == 0)
153+
{
154+
res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->part_number);
155+
}
156+
else if (strncmp("product_url", (const char *) currentString.value, currentString.len) == 0)
157+
{
158+
res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->product_url);
159+
}
160+
else if (strncmp("product_label", (const char *) currentString.value, currentString.len) == 0)
161+
{
162+
res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->product_label);
163+
}
152164
else if (strncmp("enable_key", (const char *) currentString.value, currentString.len) == 0)
153165
{
154166
res = res && zcbor_bstr_decode(states, (struct zcbor_string *) &factoryData->enable_key);

src/platform/nrfconnect/FactoryDataParser.h

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ struct FactoryData
4242
uint16_t product_id;
4343
struct FactoryDataString vendor_name;
4444
struct FactoryDataString product_name;
45+
struct FactoryDataString part_number;
46+
struct FactoryDataString product_url;
47+
struct FactoryDataString product_label;
4548
uint16_t hw_ver;
4649
struct FactoryDataString hw_ver_str;
4750
struct FactoryDataString rd_uid;

src/platform/nrfconnect/FactoryDataProvider.cpp

+19-31
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ CHIP_ERROR LoadKeypairFromRaw(ByteSpan privateKey, ByteSpan publicKey, Crypto::P
3737
memcpy(serializedKeypair.Bytes() + publicKey.size(), privateKey.data(), privateKey.size());
3838
return keypair.Deserialize(serializedKeypair);
3939
}
40+
41+
CHIP_ERROR GetFactoryDataString(const FactoryDataString & str, char * buf, size_t bufSize)
42+
{
43+
ReturnErrorCodeIf(bufSize < str.len + 1, CHIP_ERROR_BUFFER_TOO_SMALL);
44+
ReturnErrorCodeIf(!str.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
45+
46+
memcpy(buf, str.data, str.len);
47+
buf[str.len] = 0;
48+
49+
return CHIP_NO_ERROR;
50+
}
51+
4052
} // namespace
4153

4254
namespace DeviceLayer {
@@ -230,13 +242,7 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::SetSetupPasscode(uint32_t setu
230242
template <class FlashFactoryData>
231243
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetVendorName(char * buf, size_t bufSize)
232244
{
233-
ReturnErrorCodeIf(bufSize < mFactoryData.vendor_name.len + 1, CHIP_ERROR_BUFFER_TOO_SMALL);
234-
ReturnErrorCodeIf(!mFactoryData.vendor_name.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
235-
236-
memcpy(buf, mFactoryData.vendor_name.data, mFactoryData.vendor_name.len);
237-
buf[mFactoryData.vendor_name.len] = 0;
238-
239-
return CHIP_NO_ERROR;
245+
return GetFactoryDataString(mFactoryData.vendor_name, buf, bufSize);
240246
}
241247

242248
template <class FlashFactoryData>
@@ -250,13 +256,7 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetVendorId(uint16_t & vendorI
250256
template <class FlashFactoryData>
251257
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetProductName(char * buf, size_t bufSize)
252258
{
253-
ReturnErrorCodeIf(bufSize < mFactoryData.product_name.len + 1, CHIP_ERROR_BUFFER_TOO_SMALL);
254-
ReturnErrorCodeIf(!mFactoryData.product_name.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
255-
256-
memcpy(buf, mFactoryData.product_name.data, mFactoryData.product_name.len);
257-
buf[mFactoryData.product_name.len] = 0;
258-
259-
return CHIP_NO_ERROR;
259+
return GetFactoryDataString(mFactoryData.product_name, buf, bufSize);
260260
}
261261

262262
template <class FlashFactoryData>
@@ -270,31 +270,25 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetProductId(uint16_t & produc
270270
template <class FlashFactoryData>
271271
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetPartNumber(char * buf, size_t bufSize)
272272
{
273-
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
273+
return GetFactoryDataString(mFactoryData.part_number, buf, bufSize);
274274
}
275275

276276
template <class FlashFactoryData>
277277
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetProductURL(char * buf, size_t bufSize)
278278
{
279-
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
279+
return GetFactoryDataString(mFactoryData.product_url, buf, bufSize);
280280
}
281281

282282
template <class FlashFactoryData>
283283
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetProductLabel(char * buf, size_t bufSize)
284284
{
285-
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
285+
return GetFactoryDataString(mFactoryData.product_label, buf, bufSize);
286286
}
287287

288288
template <class FlashFactoryData>
289289
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetSerialNumber(char * buf, size_t bufSize)
290290
{
291-
ReturnErrorCodeIf(bufSize < mFactoryData.sn.len + 1, CHIP_ERROR_BUFFER_TOO_SMALL);
292-
ReturnErrorCodeIf(!mFactoryData.sn.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
293-
294-
memcpy(buf, mFactoryData.sn.data, mFactoryData.sn.len);
295-
buf[mFactoryData.sn.len] = 0;
296-
297-
return CHIP_NO_ERROR;
291+
return GetFactoryDataString(mFactoryData.sn, buf, bufSize);
298292
}
299293

300294
template <class FlashFactoryData>
@@ -318,13 +312,7 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetHardwareVersion(uint16_t &
318312
template <class FlashFactoryData>
319313
CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetHardwareVersionString(char * buf, size_t bufSize)
320314
{
321-
ReturnErrorCodeIf(bufSize < mFactoryData.hw_ver_str.len + 1, CHIP_ERROR_BUFFER_TOO_SMALL);
322-
ReturnErrorCodeIf(!mFactoryData.hw_ver_str.data, CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
323-
324-
memcpy(buf, mFactoryData.hw_ver_str.data, mFactoryData.hw_ver_str.len);
325-
buf[mFactoryData.hw_ver_str.len] = 0;
326-
327-
return CHIP_NO_ERROR;
315+
return GetFactoryDataString(mFactoryData.hw_ver_str, buf, bufSize);
328316
}
329317

330318
template <class FlashFactoryData>

0 commit comments

Comments
 (0)