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 
cmd_priv_beacon_get(const struct shell * sh,size_t argc,char * argv[])14 static int cmd_priv_beacon_get(const struct shell *sh, size_t argc, char *argv[])
15 {
16 	struct bt_mesh_priv_beacon val;
17 	int err;
18 
19 	err = bt_mesh_priv_beacon_cli_get(bt_mesh_shell_target_ctx.net_idx,
20 					  bt_mesh_shell_target_ctx.dst,
21 					  &val);
22 	if (err) {
23 		shell_error(sh, "Failed to send Private Beacon Get (err %d)", err);
24 		return 0;
25 	}
26 
27 	shell_print(sh, "Private Beacon state: %u, %u", val.enabled, val.rand_interval);
28 
29 	return 0;
30 }
31 
cmd_priv_beacon_set(const struct shell * sh,size_t argc,char * argv[])32 static int cmd_priv_beacon_set(const struct shell *sh, size_t argc, char *argv[])
33 {
34 	struct bt_mesh_priv_beacon val;
35 	int err = 0;
36 
37 	val.enabled = shell_strtobool(argv[1], 0, &err);
38 	if (err) {
39 		shell_warn(sh, "Unable to parse input string argument");
40 		return err;
41 	}
42 
43 	val.rand_interval = shell_strtoul(argv[2], 0, &err);
44 	if (err) {
45 		shell_warn(sh, "Unable to parse input string argument");
46 		return err;
47 	}
48 
49 	err = bt_mesh_priv_beacon_cli_set(bt_mesh_shell_target_ctx.net_idx,
50 					  bt_mesh_shell_target_ctx.dst,
51 					  &val, &val);
52 	if (err) {
53 		shell_error(sh, "Failed to send Private Beacon Set (err %d)", err);
54 		return 0;
55 	}
56 
57 	return 0;
58 }
59 
cmd_priv_gatt_proxy_get(const struct shell * sh,size_t argc,char * argv[])60 static int cmd_priv_gatt_proxy_get(const struct shell *sh, size_t argc, char *argv[])
61 {
62 	uint8_t state;
63 	int err;
64 
65 	err = bt_mesh_priv_beacon_cli_gatt_proxy_get(bt_mesh_shell_target_ctx.net_idx,
66 						     bt_mesh_shell_target_ctx.dst, &state);
67 	if (err) {
68 		shell_error(sh, "Failed to send Private GATT Proxy Get (err %d)", err);
69 		return 0;
70 	}
71 
72 	shell_print(sh, "Private GATT Proxy state: %u", state);
73 
74 	return 0;
75 }
76 
cmd_priv_gatt_proxy_set(const struct shell * sh,size_t argc,char * argv[])77 static int cmd_priv_gatt_proxy_set(const struct shell *sh, size_t argc, char *argv[])
78 {
79 	uint8_t state;
80 	int err = 0;
81 
82 	state = shell_strtobool(argv[1], 0, &err);
83 	if (err) {
84 		shell_warn(sh, "Unable to parse input string argument");
85 		return err;
86 	}
87 
88 	err = bt_mesh_priv_beacon_cli_gatt_proxy_set(bt_mesh_shell_target_ctx.net_idx,
89 						     bt_mesh_shell_target_ctx.dst, state, &state);
90 	if (err) {
91 		shell_error(sh, "Failed to send Private GATT Proxy Set (err %d)", err);
92 		return 0;
93 	}
94 
95 	return 0;
96 }
97 
cmd_priv_node_id_get(const struct shell * sh,size_t argc,char * argv[])98 static int cmd_priv_node_id_get(const struct shell *sh, size_t argc, char *argv[])
99 {
100 	struct bt_mesh_priv_node_id val;
101 	uint16_t key_net_idx;
102 	int err = 0;
103 
104 	key_net_idx = shell_strtoul(argv[1], 0, &err);
105 
106 	err = bt_mesh_priv_beacon_cli_node_id_get(bt_mesh_shell_target_ctx.net_idx,
107 						  bt_mesh_shell_target_ctx.dst, key_net_idx, &val);
108 	if (err) {
109 		shell_error(sh, "Failed to send Private Node Identity Get (err %d)", err);
110 		return 0;
111 	}
112 
113 	shell_print(sh, "Private Node Identity state: (net_idx: %u, state: %u, status: %u)",
114 		    val.net_idx, val.state, val.status);
115 
116 	return 0;
117 }
118 
cmd_priv_node_id_set(const struct shell * sh,size_t argc,char * argv[])119 static int cmd_priv_node_id_set(const struct shell *sh, size_t argc, char *argv[])
120 {
121 	struct bt_mesh_priv_node_id val;
122 	int err = 0;
123 
124 	val.net_idx = shell_strtoul(argv[1], 0, &err);
125 	val.state = shell_strtoul(argv[2], 0, &err);
126 
127 	if (err) {
128 		shell_warn(sh, "Unable to parse input string argument");
129 		return err;
130 	}
131 
132 	err = bt_mesh_priv_beacon_cli_node_id_set(bt_mesh_shell_target_ctx.net_idx,
133 						  bt_mesh_shell_target_ctx.dst, &val, &val);
134 	if (err) {
135 		shell_error(sh, "Failed to send Private Node Identity Set (err %d)", err);
136 		return 0;
137 	}
138 
139 	return 0;
140 }
141 
142 SHELL_STATIC_SUBCMD_SET_CREATE(
143 	priv_beacons_cmds,
144 	SHELL_CMD_ARG(priv-beacon-get, NULL, NULL, cmd_priv_beacon_get, 1, 0),
145 	SHELL_CMD_ARG(priv-beacon-set, NULL, "<Val(off, on)> <RandInt(10s steps)>",
146 		      cmd_priv_beacon_set, 3, 0),
147 	SHELL_CMD_ARG(priv-gatt-proxy-get, NULL, NULL, cmd_priv_gatt_proxy_get, 1, 0),
148 	SHELL_CMD_ARG(priv-gatt-proxy-set, NULL, "Val(off, on)> ", cmd_priv_gatt_proxy_set,
149 		      2, 0),
150 	SHELL_CMD_ARG(priv-node-id-get, NULL, "<NetKeyIdx>", cmd_priv_node_id_get, 2, 0),
151 	SHELL_CMD_ARG(priv-node-id-set, NULL, "<NetKeyIdx> <State>", cmd_priv_node_id_set, 3,
152 		      0),
153 	SHELL_SUBCMD_SET_END);
154 
155 SHELL_SUBCMD_ADD((mesh, models), prb, &priv_beacons_cmds, "Private Beacon Cli commands",
156 		 bt_mesh_shell_mdl_cmds_help, 1, 1);
157