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 #include "../sar_cfg_internal.h"
14 
cmd_tx_get(const struct shell * sh,size_t argc,char * argv[])15 static int cmd_tx_get(const struct shell *sh, size_t argc, char *argv[])
16 {
17 	struct bt_mesh_sar_tx rsp;
18 	int err;
19 
20 	err = bt_mesh_sar_cfg_cli_transmitter_get(bt_mesh_shell_target_ctx.net_idx,
21 						  bt_mesh_shell_target_ctx.dst, &rsp);
22 	if (err) {
23 		shell_error(sh,
24 			    "Failed to send SAR Transmitter Get (err %d)", err);
25 		return 0;
26 	}
27 
28 	shell_print(sh, "Transmitter Get: %u %u %u %u %u %u %u",
29 		    rsp.seg_int_step, rsp.unicast_retrans_count,
30 		    rsp.unicast_retrans_without_prog_count,
31 		    rsp.unicast_retrans_int_step, rsp.unicast_retrans_int_inc,
32 		    rsp.multicast_retrans_count, rsp.multicast_retrans_int);
33 
34 	return 0;
35 }
36 
cmd_tx_set(const struct shell * sh,size_t argc,char * argv[])37 static int cmd_tx_set(const struct shell *sh, size_t argc, char *argv[])
38 {
39 	struct bt_mesh_sar_tx set, rsp;
40 	int err = 0;
41 
42 	set.seg_int_step = shell_strtoul(argv[1], 0, &err);
43 	set.unicast_retrans_count = shell_strtoul(argv[2], 0, &err);
44 	set.unicast_retrans_without_prog_count = shell_strtoul(argv[3], 0, &err);
45 	set.unicast_retrans_int_step = shell_strtoul(argv[4], 0, &err);
46 	set.unicast_retrans_int_inc = shell_strtoul(argv[5], 0, &err);
47 	set.multicast_retrans_count = shell_strtoul(argv[6], 0, &err);
48 	set.multicast_retrans_int = shell_strtoul(argv[7], 0, &err);
49 
50 	if (err) {
51 		shell_warn(sh, "Unable to parse input string argument");
52 		return err;
53 	}
54 
55 	err = bt_mesh_sar_cfg_cli_transmitter_set(bt_mesh_shell_target_ctx.net_idx,
56 						  bt_mesh_shell_target_ctx.dst, &set, &rsp);
57 	if (err) {
58 		shell_error(sh, "Failed to send SAR Transmitter Set (err %d)", err);
59 		return 0;
60 	}
61 
62 	shell_print(sh, "Transmitter Set: %u %u %u %u %u %u %u",
63 		    rsp.seg_int_step, rsp.unicast_retrans_count,
64 		    rsp.unicast_retrans_without_prog_count,
65 		    rsp.unicast_retrans_int_step, rsp.unicast_retrans_int_inc,
66 		    rsp.multicast_retrans_count, rsp.multicast_retrans_int);
67 
68 	return 0;
69 }
70 
cmd_rx_get(const struct shell * sh,size_t argc,char * argv[])71 static int cmd_rx_get(const struct shell *sh, size_t argc, char *argv[])
72 {
73 	struct bt_mesh_sar_rx rsp;
74 	int err;
75 
76 	err = bt_mesh_sar_cfg_cli_receiver_get(bt_mesh_shell_target_ctx.net_idx,
77 					       bt_mesh_shell_target_ctx.dst, &rsp);
78 	if (err) {
79 		shell_error(sh, "Failed to send SAR Receiver Get (err %d)", err);
80 		return 0;
81 	}
82 
83 	shell_print(sh, "Receiver Get: %u %u %u %u %u", rsp.seg_thresh,
84 		    rsp.ack_delay_inc, rsp.ack_retrans_count,
85 		    rsp.discard_timeout, rsp.rx_seg_int_step);
86 
87 	return 0;
88 }
89 
cmd_rx_set(const struct shell * sh,size_t argc,char * argv[])90 static int cmd_rx_set(const struct shell *sh, size_t argc, char *argv[])
91 {
92 	struct bt_mesh_sar_rx set, rsp;
93 	int err = 0;
94 
95 	set.seg_thresh = shell_strtoul(argv[1], 0, &err);
96 	set.ack_delay_inc = shell_strtoul(argv[2], 0, &err);
97 	set.ack_retrans_count = shell_strtoul(argv[3], 0, &err);
98 	set.discard_timeout = shell_strtoul(argv[4], 0, &err);
99 	set.rx_seg_int_step = shell_strtoul(argv[5], 0, &err);
100 
101 	if (err) {
102 		shell_warn(sh, "Unable to parse input string argument");
103 		return err;
104 	}
105 
106 	err = bt_mesh_sar_cfg_cli_receiver_set(bt_mesh_shell_target_ctx.net_idx,
107 					       bt_mesh_shell_target_ctx.dst, &set, &rsp);
108 	if (err) {
109 		shell_error(sh, "Failed to send SAR Receiver Set (err %d)", err);
110 		return 0;
111 	}
112 
113 	shell_print(sh, "Receiver Set: %u %u %u %u %u", rsp.seg_thresh,
114 		    rsp.ack_delay_inc, rsp.ack_retrans_count,
115 		    rsp.discard_timeout, rsp.rx_seg_int_step);
116 
117 	return 0;
118 }
119 
120 SHELL_STATIC_SUBCMD_SET_CREATE(
121 	sar_cfg_cli_cmds, SHELL_CMD_ARG(tx-get, NULL, NULL, cmd_tx_get, 1, 0),
122 	SHELL_CMD_ARG(tx-set, NULL,
123 		      "<SegIntStep> <UniRetransCnt> <UniRetransWithoutProgCnt> "
124 		      "<UniRetransIntStep> <UniRetransIntInc> <MultiRetransCnt> "
125 		      "<MultiRetransInt>",
126 		      cmd_tx_set, 8, 0),
127 	SHELL_CMD_ARG(rx-get, NULL, NULL, cmd_rx_get, 1, 0),
128 	SHELL_CMD_ARG(rx-set, NULL,
129 		      "<SegThresh> <AckDelayInc> <DiscardTimeout> "
130 		      "<RxSegIntStep> <AckRetransCount>",
131 		      cmd_rx_set, 6, 0),
132 	SHELL_SUBCMD_SET_END);
133 
134 SHELL_SUBCMD_ADD((mesh, models), sar, &sar_cfg_cli_cmds, "Sar Cfg Cli commands",
135 		 bt_mesh_shell_mdl_cmds_help, 1, 1);
136