Skip to content

Commit 0992b42

Browse files
alexsvenrlubos
authored andcommitted
applications: nrf5340_audio: Automatic SIRK generation
- Generate SIRK based on username when using buildprog - OCT-3666 Signed-off-by: Alexander Svensen <alexander.svensen@nordicsemi.no> (cherry picked from commit 9947f48)
1 parent 9ae241e commit 0992b42

4 files changed

Lines changed: 59 additions & 23 deletions

File tree

applications/nrf5340_audio/doc/building.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ When preparing the JSON file, update the following fields:
6868
* ``location`` - This field is valid only for headsets.
6969
It sets the location on which the headset is meant to work, especially when using the :ref:`default CIS transport mode configuration <nrf53_audio_transport_mode_configuration>`.
7070
For more information, see :ref:`nrf53_audio_app_configuration_headset_location`.
71+
* ``sirk`` - This field sets the SIRK for the device set, which is used in the unicast applications.
72+
The SIRK must be 16 characters long and cannot be the default SIRK defined in the Kconfig file (``NRF5340_TWS_DEMO``).
73+
If the default SIRK is used, the script will raise an error and stop building.
7174

7275
.. _nrf53_audio_app_building_script_running:
7376

applications/nrf5340_audio/tools/buildprog/buildprog.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
TARGET_RELEASE_FOLDER = "build_release"
5454
TARGET_DEBUG_FOLDER = "build_debug"
5555

56+
DEFAULT_SIRK = "NRF5340_TWS_DEMO"
57+
5658
MAX_USER_NAME_LEN = 248 - len('\0')
5759

5860

@@ -99,7 +101,7 @@ def __print_dev_conf(device_list):
99101

100102

101103
def __build_cmd_get(core: Core, device: AudioDevice, build: BuildType,
102-
pristine, options):
104+
pristine, options, sirk=""):
103105

104106
build_cmd = (f"west build {TARGET_AUDIO_FOLDER} "
105107
f"-b {TARGET_BOARD_NRF5340_AUDIO_DK_APP_NAME} "
@@ -143,6 +145,14 @@ def __build_cmd_get(core: Core, device: AudioDevice, build: BuildType,
143145
else:
144146
if device == AudioDevice.headset:
145147
overlay_flag = f" -DEXTRA_CONF_FILE={UNICAST_SERVER_OVERLAY}"
148+
149+
if sirk == DEFAULT_SIRK:
150+
raise ValueError("Default SIRK is not allowed. Please set a custom SIRK in the JSON file.")
151+
152+
if len(sirk) != 16:
153+
raise ValueError("SIRK must be 16 characters long")
154+
155+
device_flag += " -DCONFIG_BT_SET_IDENTITY_RESOLVING_KEY=\\\"" + sirk + "\\\""
146156
else:
147157
overlay_flag = f" -DEXTRA_CONF_FILE={UNICAST_CLIENT_OVERLAY}"
148158

@@ -156,13 +166,14 @@ def __build_cmd_get(core: Core, device: AudioDevice, build: BuildType,
156166
return build_cmd, dest_folder, device_flag, release_flag, overlay_flag
157167

158168

159-
def __build_module(build_config, options):
169+
def __build_module(build_config, options, sirk=""):
160170
build_cmd, dest_folder, device_flag, release_flag, overlay_flag = __build_cmd_get(
161171
build_config.core,
162172
build_config.device,
163173
build_config.build,
164174
build_config.pristine,
165175
options,
176+
sirk,
166177
)
167178
west_str = f"{build_cmd} -d {dest_folder} "
168179

@@ -196,10 +207,10 @@ def __find_snr():
196207
def __populate_hex_paths(dev, options):
197208
"""Poplulate hex paths where relevant"""
198209

199-
_, temp_dest_folder, _, _, _ = __build_cmd_get(Core.app, dev.nrf5340_audio_dk_dev, options.build, options.pristine, options)
210+
_, temp_dest_folder, _, _, _ = __build_cmd_get(Core.app, dev.nrf5340_audio_dk_dev, options.build, options.pristine, options, dev.sirk)
200211
dev.hex_path_app = temp_dest_folder / "nrf5340_audio/zephyr/zephyr.hex"
201212

202-
_, temp_dest_folder, _, _, _ = __build_cmd_get(Core.net, dev.nrf5340_audio_dk_dev, options.build, options.pristine, options)
213+
_, temp_dest_folder, _, _, _ = __build_cmd_get(Core.net, dev.nrf5340_audio_dk_dev, options.build, options.pristine, options, dev.sirk)
203214
dev.hex_path_net = temp_dest_folder / "ipc_radio/zephyr/zephyr.hex"
204215

205216

@@ -350,7 +361,17 @@ def __main():
350361
# Then run git update-index --skip-worktree FILENAME to avoid changes
351362
# being pushed
352363
with AUDIO_KIT_SERIAL_NUMBERS_JSON.open() as f:
353-
dev_arr = json.load(f)
364+
json_data = json.load(f)
365+
366+
# Extract SIRK and devices from the new JSON structure
367+
if "devices" in json_data:
368+
dev_arr = json_data["devices"]
369+
sirk = json_data.get("sirk", "")
370+
else:
371+
# Fallback for old format (array at root level)
372+
dev_arr = json_data
373+
sirk = DEFAULT_SIRK
374+
354375
device_list = []
355376
for dev in dev_arr:
356377
if AudioDevice[dev["nrf5340_audio_dk_dev"]] == AudioDevice.headset:
@@ -387,6 +408,7 @@ def __main():
387408
snr_connected=(dev["nrf5340_audio_dk_snr"] in boards_snr_connected),
388409
recover_on_fail=options.recover_on_fail,
389410
nrf5340_audio_dk_dev=AudioDevice[dev["nrf5340_audio_dk_dev"]],
411+
sirk=sirk,
390412
cores=cores,
391413
devices=devices,
392414
_only_reboot=options.only_reboot,
@@ -431,7 +453,7 @@ def __main():
431453
)
432454

433455
for build_cfg in build_configs:
434-
__build_module(build_cfg, options)
456+
__build_module(build_cfg, options, sirk)
435457

436458
# Build step finished
437459
# Program step start
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
[
2-
{
3-
"nrf5340_audio_dk_snr": 1000,
4-
"nrf5340_audio_dk_dev": "headset",
5-
"location": ["FRONT_LEFT"]
6-
},
7-
{
8-
"nrf5340_audio_dk_snr": 1000,
9-
"nrf5340_audio_dk_dev": "gateway",
10-
"location": ["NA"]
11-
},
12-
{
13-
"nrf5340_audio_dk_snr": 1000,
14-
"nrf5340_audio_dk_dev": "headset",
15-
"location": ["FRONT_RIGHT"]
16-
}
17-
]
1+
{
2+
"sirk": "NRF5340_TWS_DEMO",
3+
"devices": [
4+
{
5+
"nrf5340_audio_dk_snr": 1000,
6+
"nrf5340_audio_dk_dev": "headset",
7+
"location": [
8+
"FRONT_LEFT"
9+
]
10+
},
11+
{
12+
"nrf5340_audio_dk_snr": 1000,
13+
"nrf5340_audio_dk_dev": "gateway",
14+
"location": [
15+
"NA"
16+
]
17+
},
18+
{
19+
"nrf5340_audio_dk_snr": 1000,
20+
"nrf5340_audio_dk_dev": "headset",
21+
"location": [
22+
"FRONT_RIGHT"
23+
]
24+
}
25+
]
26+
}

applications/nrf5340_audio/tools/buildprog/nrf5340_audio_dk_devices.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class DeviceConf:
105105
cores: InitVar[list[Core]]
106106
devices: InitVar[list[AudioDevice]]
107107
_only_reboot: InitVar[SelectFlags]
108+
sirk: str = "" # SIRK for the device set
109+
108110
# Post init variables
109111
only_reboot: SelectFlags = field(init=False, default=SelectFlags.NOT)
110112
hex_path_app: Path = field(init=False, default=None)

0 commit comments

Comments
 (0)