1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * SPDX-FileContributor: Blake Felt
7  */
8 
9 #include "btc/btc_manage.h"
10 #include "btc_hh.h"
11 #include "esp_bt_main.h"
12 #include "esp_err.h"
13 #include "esp_hidh_api.h"
14 #include <string.h>
15 
16 #if (defined BTC_HH_INCLUDED && BTC_HH_INCLUDED == TRUE)
17 
esp_bt_hid_host_register_callback(esp_hh_cb_t callback)18 esp_err_t esp_bt_hid_host_register_callback(esp_hh_cb_t callback)
19 {
20     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
21 
22     if (callback == NULL) {
23         return ESP_FAIL;
24     }
25 
26     btc_profile_cb_set(BTC_PID_HH, callback);
27     return ESP_OK;
28 }
29 
esp_bt_hid_host_init(void)30 esp_err_t esp_bt_hid_host_init(void)
31 {
32     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
33 
34     btc_msg_t msg;
35 
36     msg.sig = BTC_SIG_API_CALL;
37     msg.pid = BTC_PID_HH;
38     msg.act = BTC_HH_INIT_EVT;
39 
40     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
41     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
42 }
43 
esp_bt_hid_host_deinit(void)44 esp_err_t esp_bt_hid_host_deinit(void)
45 {
46     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
47 
48     btc_msg_t msg;
49 
50     msg.sig = BTC_SIG_API_CALL;
51     msg.pid = BTC_PID_HH;
52     msg.act = BTC_HH_DEINIT_EVT;
53 
54     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
55     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
56 }
57 
esp_bt_hid_host_connect(esp_bd_addr_t bd_addr)58 esp_err_t esp_bt_hid_host_connect(esp_bd_addr_t bd_addr)
59 {
60     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
61     btc_msg_t msg;
62     btc_hidh_args_t arg;
63 
64     msg.sig = BTC_SIG_API_CALL;
65     msg.pid = BTC_PID_HH;
66     msg.act = BTC_HH_CONNECT_EVT;
67 
68     memcpy(arg.connect.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
69 
70     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
71     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
72 }
73 
esp_bt_hid_host_disconnect(esp_bd_addr_t bd_addr)74 esp_err_t esp_bt_hid_host_disconnect(esp_bd_addr_t bd_addr)
75 {
76     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
77     btc_msg_t msg;
78     btc_hidh_args_t arg;
79 
80     msg.sig = BTC_SIG_API_CALL;
81     msg.pid = BTC_PID_HH;
82     msg.act = BTC_HH_DISCONNECT_EVT;
83 
84     memcpy(arg.disconnect.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
85 
86     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
87     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
88 }
89 
esp_bt_hid_host_virtual_cable_unplug(esp_bd_addr_t bd_addr)90 esp_err_t esp_bt_hid_host_virtual_cable_unplug(esp_bd_addr_t bd_addr)
91 {
92     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
93     btc_msg_t msg;
94     btc_hidh_args_t arg;
95 
96     msg.sig = BTC_SIG_API_CALL;
97     msg.pid = BTC_PID_HH;
98     msg.act = BTC_HH_UNPLUG_EVT;
99 
100     memcpy(arg.unplug.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
101 
102     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
103     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
104 }
105 
esp_bt_hid_host_set_info(esp_bd_addr_t bd_addr,esp_hidh_hid_info_t * hid_info)106 esp_err_t esp_bt_hid_host_set_info(esp_bd_addr_t bd_addr, esp_hidh_hid_info_t *hid_info)
107 {
108     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
109     btc_msg_t msg;
110     btc_hidh_args_t arg;
111 
112     msg.sig = BTC_SIG_API_CALL;
113     msg.pid = BTC_PID_HH;
114     msg.act = BTC_HH_SET_INFO_EVT;
115 
116     memcpy(arg.set_info.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
117     arg.set_info.hid_info = hid_info;
118 
119     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t),
120                                                 btc_hh_arg_deep_copy, btc_hh_cb_arg_deep_free);
121     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
122 }
123 
esp_bt_hid_host_get_protocol(esp_bd_addr_t bd_addr)124 esp_err_t esp_bt_hid_host_get_protocol(esp_bd_addr_t bd_addr)
125 {
126     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
127     btc_msg_t msg;
128     btc_hidh_args_t arg;
129 
130     msg.sig = BTC_SIG_API_CALL;
131     msg.pid = BTC_PID_HH;
132     msg.act = BTC_HH_GET_PROTO_EVT;
133 
134     memcpy(arg.get_protocol.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
135 
136     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
137     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
138 }
139 
esp_bt_hid_host_set_protocol(esp_bd_addr_t bd_addr,esp_hidh_protocol_mode_t protocol_mode)140 esp_err_t esp_bt_hid_host_set_protocol(esp_bd_addr_t bd_addr, esp_hidh_protocol_mode_t protocol_mode)
141 {
142     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
143     btc_msg_t msg;
144     btc_hidh_args_t arg;
145 
146     msg.sig = BTC_SIG_API_CALL;
147     msg.pid = BTC_PID_HH;
148     msg.act = BTC_HH_SET_PROTO_EVT;
149 
150     memcpy(arg.set_protocol.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
151     arg.set_protocol.protocol_mode = protocol_mode;
152 
153     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
154     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
155 }
156 
esp_bt_hid_host_get_idle(esp_bd_addr_t bd_addr)157 esp_err_t esp_bt_hid_host_get_idle(esp_bd_addr_t bd_addr)
158 {
159     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
160     btc_msg_t msg;
161     btc_hidh_args_t arg;
162 
163     msg.sig = BTC_SIG_API_CALL;
164     msg.pid = BTC_PID_HH;
165     msg.act = BTC_HH_GET_IDLE_EVT;
166 
167     memcpy(arg.get_idle.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
168 
169     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
170     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
171 }
172 
esp_bt_hid_host_set_idle(esp_bd_addr_t bd_addr,uint16_t idle_time)173 esp_err_t esp_bt_hid_host_set_idle(esp_bd_addr_t bd_addr, uint16_t idle_time)
174 {
175     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
176     btc_msg_t msg;
177     btc_hidh_args_t arg;
178 
179     msg.sig = BTC_SIG_API_CALL;
180     msg.pid = BTC_PID_HH;
181     msg.act = BTC_HH_SET_IDLE_EVT;
182 
183     memcpy(arg.set_idle.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
184     arg.set_idle.idle_time = idle_time;
185 
186     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
187     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
188 }
189 
esp_bt_hid_host_get_report(esp_bd_addr_t bd_addr,esp_hidh_report_type_t report_type,uint8_t report_id,int buffer_size)190 esp_err_t esp_bt_hid_host_get_report(esp_bd_addr_t bd_addr, esp_hidh_report_type_t report_type, uint8_t report_id,
191                                      int buffer_size)
192 {
193     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
194     btc_msg_t msg;
195     btc_hidh_args_t arg;
196 
197     msg.sig = BTC_SIG_API_CALL;
198     msg.pid = BTC_PID_HH;
199     msg.act = BTC_HH_GET_REPORT_EVT;
200 
201     memcpy(arg.get_report.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
202     arg.get_report.report_type = report_type;
203     arg.get_report.report_id = report_id;
204     arg.get_report.buffer_size = buffer_size;
205 
206     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t), NULL, NULL);
207     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
208 }
209 
esp_bt_hid_host_set_report(esp_bd_addr_t bd_addr,esp_hidh_report_type_t report_type,uint8_t * report,size_t len)210 esp_err_t esp_bt_hid_host_set_report(esp_bd_addr_t bd_addr, esp_hidh_report_type_t report_type, uint8_t *report,
211                                      size_t len)
212 {
213     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
214     btc_msg_t msg;
215     btc_hidh_args_t arg;
216 
217     msg.sig = BTC_SIG_API_CALL;
218     msg.pid = BTC_PID_HH;
219     msg.act = BTC_HH_SET_REPORT_EVT;
220 
221     memcpy(arg.set_report.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
222     arg.set_report.report_type = report_type;
223     arg.set_report.len = len;
224     arg.set_report.report = report;
225 
226     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t),
227                                                 btc_hh_arg_deep_copy, btc_hh_cb_arg_deep_free);
228     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
229 }
230 
esp_bt_hid_host_send_data(esp_bd_addr_t bd_addr,uint8_t * data,size_t len)231 esp_err_t esp_bt_hid_host_send_data(esp_bd_addr_t bd_addr, uint8_t *data, size_t len)
232 {
233     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
234     btc_msg_t msg;
235     btc_hidh_args_t arg;
236 
237     msg.sig = BTC_SIG_API_CALL;
238     msg.pid = BTC_PID_HH;
239     msg.act = BTC_HH_SEND_DATA_EVT;
240 
241     memcpy(arg.send_data.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
242     arg.send_data.len = len;
243     arg.send_data.data = data;
244 
245     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hidh_args_t),
246                                                 btc_hh_arg_deep_copy, btc_hh_cb_arg_deep_free);
247     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
248 }
249 
250 #endif /* defined BTC_HH_INCLUDED && BTC_HH_INCLUDED == TRUE */
251