1 /** @file
2  *  @brief Bluetooth Media Control Profile (MCP) Controller role.
3  *
4  *  Copyright 2023 NXP
5  *
6  *  SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/printk.h>
11 
12 #include <zephyr/bluetooth/conn.h>
13 #include <zephyr/bluetooth/audio/mcc.h>
14 #include <zephyr/bluetooth/audio/media_proxy.h>
15 
16 static struct bt_conn *default_conn;
17 
18 static K_SEM_DEFINE(sem_discovery_done, 0, 1);
19 
mcc_discover_mcs_cb(struct bt_conn * conn,int err)20 static void mcc_discover_mcs_cb(struct bt_conn *conn, int err)
21 {
22 	if (err) {
23 		printk("MCP: Discovery of MCS failed (%d)\n", err);
24 	} else {
25 		printk("MCP: Discovered MCS\n");
26 	}
27 	k_sem_give(&sem_discovery_done);
28 }
29 
mcc_send_command_cb(struct bt_conn * conn,int err,const struct mpl_cmd * cmd)30 static void mcc_send_command_cb(struct bt_conn *conn, int err, const struct mpl_cmd *cmd)
31 {
32 	if (err) {
33 		printk("MCP: Command send failed (%d) - opcode: %u, param: %d\n",
34 			err, cmd->opcode, cmd->param);
35 	} else {
36 		printk("MCP: Successfully sent command (%d) - opcode: %u, param: %d\n",
37 			err, cmd->opcode, cmd->param);
38 	}
39 }
40 
41 static struct bt_mcc_cb mcc_cb = {
42 	.discover_mcs = mcc_discover_mcs_cb,
43 	.send_cmd = mcc_send_command_cb,
44 };
45 
mcp_ctlr_init(struct bt_conn * conn)46 int mcp_ctlr_init(struct bt_conn *conn)
47 {
48 	int err;
49 
50 	default_conn = bt_conn_ref(conn);
51 
52 	err = bt_mcc_init(&mcc_cb);
53 	if (err != 0) {
54 		return err;
55 	}
56 
57 	err = bt_mcc_discover_mcs(default_conn, true);
58 	if (err == 0) {
59 		k_sem_take(&sem_discovery_done, K_FOREVER);
60 	}
61 	return err;
62 }
63 
mcp_send_cmd(uint8_t mcp_opcode)64 int mcp_send_cmd(uint8_t mcp_opcode)
65 {
66 	int err;
67 	struct mpl_cmd cmd;
68 
69 	cmd.opcode = mcp_opcode;
70 	cmd.use_param = false;
71 
72 	if (default_conn == NULL) {
73 		printk("MCP: No connection\n");
74 		return -EINVAL;
75 	}
76 
77 	err = bt_mcc_send_cmd(default_conn, &cmd);
78 	if (err != 0) {
79 		printk("MCP: Command failed: %d\n", err);
80 	}
81 
82 	return err;
83 }
84