1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/mesh.h>
8 #include <zephyr/shell/shell.h>
9 #include <ctype.h>
10 #include <string.h>
11 
12 #include "mesh/net.h"
13 #include "mesh/access.h"
14 #include "utils.h"
15 
bt_mesh_shell_mdl_first_get(uint16_t id,const struct bt_mesh_model ** mod)16 bool bt_mesh_shell_mdl_first_get(uint16_t id, const struct bt_mesh_model **mod)
17 {
18 	const struct bt_mesh_comp *comp = bt_mesh_comp_get();
19 
20 	for (int i = 0; i < comp->elem_count; i++) {
21 		*mod = bt_mesh_model_find(&comp->elem[i], id);
22 		if (*mod) {
23 			return true;
24 		}
25 	}
26 
27 	return false;
28 }
29 
bt_mesh_shell_mdl_instance_set(const struct shell * sh,const struct bt_mesh_model ** mod,uint16_t mod_id,uint8_t elem_idx)30 int bt_mesh_shell_mdl_instance_set(const struct shell *sh, const struct bt_mesh_model **mod,
31 				 uint16_t mod_id, uint8_t elem_idx)
32 {
33 	const struct bt_mesh_model *mod_temp;
34 	const struct bt_mesh_comp *comp = bt_mesh_comp_get();
35 
36 	if (elem_idx >= comp->elem_count) {
37 		shell_error(sh, "Invalid element index");
38 		return -EINVAL;
39 	}
40 
41 	mod_temp = bt_mesh_model_find(&comp->elem[elem_idx], mod_id);
42 
43 	if (mod_temp) {
44 		*mod = mod_temp;
45 	} else {
46 		shell_error(sh, "Unable to find model instance for element index %d", elem_idx);
47 		return -ENODEV;
48 	}
49 
50 	return 0;
51 }
52 
bt_mesh_shell_mdl_print_all(const struct shell * sh,uint16_t mod_id)53 int bt_mesh_shell_mdl_print_all(const struct shell *sh, uint16_t mod_id)
54 {
55 	const struct bt_mesh_comp *comp = bt_mesh_comp_get();
56 	const struct bt_mesh_model *mod;
57 
58 	for (int i = 0; i < comp->elem_count; i++) {
59 		mod = bt_mesh_model_find(&comp->elem[i], mod_id);
60 		if (mod) {
61 			shell_print(sh,
62 				    "Client model instance found at addr 0x%.4X. Element index: %d",
63 				    comp->elem[i].rt->addr, mod->rt->elem_idx);
64 		}
65 	}
66 
67 	return 0;
68 }
69 
bt_mesh_shell_mdl_cmds_help(const struct shell * sh,size_t argc,char ** argv)70 int bt_mesh_shell_mdl_cmds_help(const struct shell *sh, size_t argc, char **argv)
71 {
72 	shell_print(
73 		sh,
74 		"\nFor a detailed description of the commands and arguments in this shell module,\n"
75 		"please refer to the Zephyr Project documentation online.\n");
76 
77 	if (argc == 1) {
78 		shell_help(sh);
79 		return 0;
80 	}
81 
82 	shell_error(sh, "%s unknown command: %s", argv[0], argv[1]);
83 	return -EINVAL;
84 }
85