1 /**
2  * @file
3  * @brief Shell APIs for Bluetooth IAS
4  *
5  * Copyright (c) 2022 Codecoup
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <stdint.h>
11 #include <zephyr/types.h>
12 #include <zephyr/shell/shell.h>
13 #include <stdlib.h>
14 #include <zephyr/bluetooth/gatt.h>
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/services/ias.h>
17 
18 #include "bt.h"
19 
20 extern const struct shell *ctx_shell;
21 
ias_discover_cb(struct bt_conn * conn,int err)22 static void ias_discover_cb(struct bt_conn *conn, int err)
23 {
24 	if (err != 0) {
25 		shell_error(ctx_shell, "Failed to discover IAS err: %d\n", err);
26 	} else {
27 		shell_print(ctx_shell, "IAS discover success\n");
28 	}
29 }
30 
31 static struct bt_ias_client_cb ias_client_callbacks = {
32 	.discover = ias_discover_cb,
33 };
34 
cmd_ias_client_init(const struct shell * sh,size_t argc,char ** argv)35 static int cmd_ias_client_init(const struct shell *sh, size_t argc, char **argv)
36 {
37 	int err;
38 
39 	if (!ctx_shell) {
40 		ctx_shell = sh;
41 	}
42 
43 	err = bt_ias_client_cb_register(&ias_client_callbacks);
44 	if (err != 0) {
45 		shell_print(sh, "IAS client init failed");
46 	} else {
47 		shell_print(sh, "IAS client initialized");
48 	}
49 
50 	return err;
51 }
52 
cmd_ias_client_discover(const struct shell * sh,size_t argc,char ** argv)53 static int cmd_ias_client_discover(const struct shell *sh, size_t argc, char **argv)
54 {
55 	int err;
56 
57 	err = bt_ias_discover(default_conn);
58 	if (err != 0) {
59 		shell_print(sh, "IAS discover failed");
60 	}
61 
62 	return err;
63 }
64 
cmd_ias_client_set_alert(const struct shell * sh,size_t argc,char ** argv)65 static int cmd_ias_client_set_alert(const struct shell *sh, size_t argc, char **argv)
66 {
67 	int err = 0;
68 
69 	if (strcmp(argv[1], "stop") == 0) {
70 		err = bt_ias_client_alert_write(default_conn,
71 						BT_IAS_ALERT_LVL_NO_ALERT);
72 	} else if (strcmp(argv[1], "mild") == 0) {
73 		err = bt_ias_client_alert_write(default_conn,
74 						BT_IAS_ALERT_LVL_MILD_ALERT);
75 	} else if (strcmp(argv[1], "high") == 0) {
76 		err = bt_ias_client_alert_write(default_conn,
77 						BT_IAS_ALERT_LVL_HIGH_ALERT);
78 	} else {
79 		shell_error(sh, "Invalid alert level %s", argv[1]);
80 		return -EINVAL;
81 	}
82 
83 	if (err != 0) {
84 		shell_error(sh, "Failed to send %s alert %d", argv[1], err);
85 	} else {
86 		shell_print(sh, "Sent alert %s", argv[1]);
87 	}
88 
89 	return err;
90 }
91 
cmd_ias_client(const struct shell * sh,size_t argc,char ** argv)92 static int cmd_ias_client(const struct shell *sh, size_t argc, char **argv)
93 {
94 	if (argc > 1) {
95 		shell_error(sh, "%s unknown parameter: %s",
96 			    argv[0], argv[1]);
97 	} else {
98 		shell_error(sh, "%s Missing subcommand", argv[0]);
99 	}
100 
101 	return -ENOEXEC;
102 }
103 
104 SHELL_STATIC_SUBCMD_SET_CREATE(ias_cli_cmds,
105 	SHELL_CMD_ARG(init, NULL,
106 		      "Initialize the client and register callbacks",
107 		      cmd_ias_client_init, 1, 0),
108 	SHELL_CMD_ARG(discover, NULL,
109 		      "Discover IAS",
110 		      cmd_ias_client_discover, 1, 0),
111 	SHELL_CMD_ARG(set_alert, NULL,
112 		      "Send alert <stop/mild/high>",
113 		      cmd_ias_client_set_alert, 2, 0),
114 		      SHELL_SUBCMD_SET_END
115 );
116 
117 SHELL_CMD_ARG_REGISTER(ias_client, &ias_cli_cmds, "Bluetooth IAS client shell commands",
118 		       cmd_ias_client, 1, 1);
119