1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <zephyr/shell/shell.h>
9 #include <zephyr/bluetooth/mesh.h>
10 #include <zephyr/bluetooth/mesh/shell.h>
11 
12 #include "utils.h"
13 
14 extern const struct shell *bt_mesh_shell_ctx_shell;
15 
status_print(int err,char * msg,uint16_t addr,struct bt_mesh_large_comp_data_rsp * rsp)16 static void status_print(int err, char *msg, uint16_t addr, struct bt_mesh_large_comp_data_rsp *rsp)
17 {
18 	if (err) {
19 		shell_error(bt_mesh_shell_ctx_shell,
20 			    "Failed to send %s Get message (err %d)", msg, err);
21 		return;
22 	}
23 
24 	shell_print(bt_mesh_shell_ctx_shell,
25 		    "%s [0x%04x]: page: %u offset: %u total size: %u", msg, addr, rsp->page,
26 		    rsp->offset, rsp->total_size);
27 	shell_hexdump(bt_mesh_shell_ctx_shell, rsp->data->data, rsp->data->len);
28 }
29 
cmd_large_comp_data_get(const struct shell * sh,size_t argc,char * argv[])30 static int cmd_large_comp_data_get(const struct shell *sh, size_t argc, char *argv[])
31 {
32 	NET_BUF_SIMPLE_DEFINE(comp, 64);
33 	struct bt_mesh_large_comp_data_rsp rsp = {
34 		.data = &comp,
35 	};
36 	uint8_t page;
37 	uint16_t offset;
38 	int err = 0;
39 
40 	net_buf_simple_init(&comp, 0);
41 
42 	page = shell_strtoul(argv[1], 0, &err);
43 	offset = shell_strtoul(argv[2], 0, &err);
44 
45 	if (err) {
46 		shell_warn(sh, "Unable to parse input string argument");
47 		return err;
48 	}
49 
50 	err = bt_mesh_large_comp_data_get(bt_mesh_shell_target_ctx.net_idx,
51 					  bt_mesh_shell_target_ctx.dst, page, offset, &rsp);
52 	status_print(err, "Composition Data", bt_mesh_shell_target_ctx.dst, &rsp);
53 
54 	return err;
55 }
56 
cmd_models_metadata_get(const struct shell * sh,size_t argc,char * argv[])57 static int cmd_models_metadata_get(const struct shell *sh, size_t argc, char *argv[])
58 {
59 	NET_BUF_SIMPLE_DEFINE(metadata, 64);
60 	struct bt_mesh_large_comp_data_rsp rsp = {
61 		.data = &metadata,
62 	};
63 	uint8_t page;
64 	uint16_t offset;
65 	int err = 0;
66 
67 	net_buf_simple_init(&metadata, 0);
68 
69 	page = shell_strtoul(argv[1], 0, &err);
70 	offset = shell_strtoul(argv[2], 0, &err);
71 
72 	if (err) {
73 		shell_warn(sh, "Unable to parse input string argument");
74 		return err;
75 	}
76 
77 	err = bt_mesh_models_metadata_get(bt_mesh_shell_target_ctx.net_idx,
78 					  bt_mesh_shell_target_ctx.dst, page, offset, &rsp);
79 	status_print(err, "Models Metadata", bt_mesh_shell_target_ctx.dst, &rsp);
80 
81 	return err;
82 }
83 
84 SHELL_STATIC_SUBCMD_SET_CREATE(
85 	large_comp_data_cmds,
86 	SHELL_CMD_ARG(large-comp-data-get, NULL, "<page> <offset>", cmd_large_comp_data_get, 3, 0),
87 	SHELL_CMD_ARG(models-metadata-get, NULL, "<page> <offset>", cmd_models_metadata_get, 3, 0),
88 	SHELL_SUBCMD_SET_END);
89 
90 SHELL_SUBCMD_ADD((mesh, models), lcd, &large_comp_data_cmds,
91 		 "Large Comp Data Cli commands",
92 		 bt_mesh_shell_mdl_cmds_help, 1, 1);
93