Skip to content

Commit dc0be00

Browse files
committed
check if device can be claimed with error message
Signed-off-by: Maximilian Deubel <[email protected]>
1 parent 3be2a3b commit dc0be00

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Run the following command to use this package as a dependency:
4646
# Enable modem-based JSON Web Token (JWT) generation required for nRF Cloud authentication
4747
CONFIG_MODEM_JWT=y
4848
49-
# Configure the nRF Cloud library to use the device's internal UUID
49+
# Configure the nRF Cloud library to use the device's internal UUID
5050
CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID=y
5151
# Or IMEI as the device ID
5252
CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=y
@@ -120,4 +120,4 @@ Running the tests depends on a [development installation](#development-installat
120120
121121
Check coverage
122122
123-
poetry run pytest --cov=. tests
123+
poetry run pytest --cov=src tests

src/nrfcloud_utils/claim_and_provision_device.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,19 @@ def main(in_args):
206206
if args.provisioning_tags == "nrf-cloud-onboarding":
207207
nrf_cloud_diap.ensure_nrfcloud_provisioning_rule(args.api_key, args.sectag)
208208
logger.info(f'with provisioning tags: {args.provisioning_tags}')
209+
210+
can_claim, can_claim_reason = nrf_cloud_diap.can_device_be_claimed(args.api_key, attest_tok)
211+
if not can_claim:
212+
error_exit(f'Cannot claim device: {can_claim_reason}')
213+
209214
api_res = nrf_cloud_diap.claim_device(args.api_key, attest_tok, args.provisioning_tags)
210215
nrf_cloud_diap.print_api_result("Claim device response", api_res)
211216
if api_res.status_code != 201:
212-
error_exit('ClaimDeviceOwnership API call failed')
217+
error_exit('ClaimDeviceOwnership API call failed: ' + api_res.text)
213218
elif args.provisioning_tags is not None:
219+
if cred_if.shell:
220+
# issue reboot to speed up provisioning
221+
cred_if.write_raw('kernel reboot warm')
214222
logger.info('Done. It is assumed the provisioning tags complete the process over the air.')
215223
return
216224

src/nrfcloud_utils/nrf_cloud_diap.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ def unclaim_device(api_key, dev_uuid):
7070

7171
return requests.delete(req, headers=get_auth_header(api_key))
7272

73+
def can_device_be_claimed(api_key, claim_token):
74+
global api_url
75+
req = f'{api_url}{CLAIMED_DEV}?dryrun=true'
76+
77+
response = requests.post(req, json={CLAIM_TOK : claim_token}, headers=get_auth_header(api_key))
78+
79+
reason = None
80+
81+
if response.text:
82+
response_json = response.json()
83+
reason = response_json.get('message', None)
84+
85+
return (response.status_code == 202, reason)
86+
7387
def get_create_prov_cmd_req(dev_uuid):
7488
global api_url
7589
return f'{api_url}{CLAIMED_DEV}/{dev_uuid}/{PROV}'

tests/test_claim_and_provision_device.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestClaimAndProvisionDevice:
4545
@patch("nrfcredstore.comms.serial.Serial", return_value=FakeSerial())
4646
def test_provisioning_tags(self, ser, select_device, diap):
4747
diap.claim_device = Mock(return_value=TEST_RESPONSE)
48+
diap.can_device_be_claimed = Mock(return_value=(True, ""))
4849
args = f"--port /not/a/real/device --cmd-type at --api-key NOTAKEY --provisioning-tags nrf-cloud-onboarding".split()
4950
# call DUT
5051
claim_and_provision_device.main(args)

0 commit comments

Comments
 (0)