1 /*
2  * Copyright (c) 2020 Linumiz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "hawkbit_device.h"
7 #include <string.h>
8 #include <zephyr/mgmt/hawkbit/hawkbit.h>
9 
10 static bool hawkbit_get_device_identity_default(char *id, int id_max_len);
11 
12 static hawkbit_get_device_identity_cb_handler_t hawkbit_get_device_identity_cb_handler =
13 	hawkbit_get_device_identity_default;
14 
hawkbit_get_device_identity(char * id,int id_max_len)15 bool hawkbit_get_device_identity(char *id, int id_max_len)
16 {
17 	return hawkbit_get_device_identity_cb_handler(id, id_max_len);
18 }
19 
hawkbit_get_device_identity_default(char * id,int id_max_len)20 static bool hawkbit_get_device_identity_default(char *id, int id_max_len)
21 {
22 #ifdef CONFIG_HAWKBIT_HWINFO_DEVICE_ID
23 	uint8_t hwinfo_id[DEVICE_ID_BIN_MAX_SIZE];
24 	ssize_t id_length, length;
25 
26 	memset(id, 0, id_max_len);
27 	length = snprintk(id, id_max_len, "%s-", CONFIG_BOARD);
28 	if (length <= 0) {
29 		return false;
30 	}
31 
32 	id_length = hwinfo_get_device_id(hwinfo_id, DEVICE_ID_BIN_MAX_SIZE);
33 	if (id_length <= 0) {
34 		return false;
35 	}
36 
37 	length = bin2hex(hwinfo_id, (size_t)id_length, &id[length], id_max_len - length);
38 
39 	return length > 0;
40 #else /* CONFIG_HAWKBIT_HWINFO_DEVICE_ID */
41 	ARG_UNUSED(id);
42 	ARG_UNUSED(id_max_len);
43 
44 	return false;
45 #endif /* CONFIG_HAWKBIT_HWINFO_DEVICE_ID */
46 }
47 
48 #ifdef CONFIG_HAWKBIT_CUSTOM_DEVICE_ID
hawkbit_set_device_identity_cb(hawkbit_get_device_identity_cb_handler_t cb)49 int hawkbit_set_device_identity_cb(hawkbit_get_device_identity_cb_handler_t cb)
50 {
51 	if (cb == NULL) {
52 		return -EINVAL;
53 	}
54 
55 	hawkbit_get_device_identity_cb_handler = cb;
56 
57 	return 0;
58 }
59 #endif /* CONFIG_HAWKBIT_CUSTOM_DEVICE_ID */
60