1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 // Copyright 2019      Blake Felt
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <string.h>
17 #include "esp_err.h"
18 #include "esp_bt_main.h"
19 #include "btc/btc_manage.h"
20 #include "btc_hd.h"
21 #include "esp_hidd_api.h"
22 
23 #if (defined BTC_HD_INCLUDED && BTC_HD_INCLUDED == TRUE)
24 
esp_bt_hid_device_register_callback(esp_hd_cb_t * callback)25 esp_err_t esp_bt_hid_device_register_callback(esp_hd_cb_t *callback)
26 {
27     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
28 
29     if (callback == NULL) {
30         return ESP_FAIL;
31     }
32 
33     btc_profile_cb_set(BTC_PID_HD, callback);
34     return ESP_OK;
35 }
36 
esp_bt_hid_device_init(void)37 esp_err_t esp_bt_hid_device_init(void)
38 {
39     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
40 
41     btc_msg_t msg;
42     msg.sig = BTC_SIG_API_CALL;
43     msg.pid = BTC_PID_HD;
44     msg.act = BTC_HD_INIT_EVT;
45 
46     /* Switch to BTC context */
47     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
48     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
49 }
50 
esp_bt_hid_device_deinit(void)51 esp_err_t esp_bt_hid_device_deinit(void)
52 {
53     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
54 
55     btc_msg_t msg;
56     msg.sig = BTC_SIG_API_CALL;
57     msg.pid = BTC_PID_HD;
58     msg.act = BTC_HD_DEINIT_EVT;
59 
60     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
61     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
62 }
63 
esp_bt_hid_device_register_app(esp_hidd_app_param_t * app_param,esp_hidd_qos_param_t * in_qos,esp_hidd_qos_param_t * out_qos)64 esp_err_t esp_bt_hid_device_register_app(esp_hidd_app_param_t* app_param, esp_hidd_qos_param_t* in_qos, esp_hidd_qos_param_t* out_qos)
65 {
66     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
67 
68     btc_msg_t msg;
69     btc_hidd_args_t args;
70     memset(&args, 0, sizeof(btc_hidd_args_t));
71     args.register_app.app_param = app_param;
72     args.register_app.in_qos = in_qos;
73     args.register_app.out_qos = out_qos;
74 
75     msg.sig = BTC_SIG_API_CALL;
76     msg.pid = BTC_PID_HD;
77     msg.act = BTC_HD_REGISTER_APP_EVT;
78 
79     bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_hidd_args_t), NULL);
80     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
81 }
82 
esp_bt_hid_device_unregister_app(void)83 esp_err_t esp_bt_hid_device_unregister_app(void)
84 {
85     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
86 
87     btc_msg_t msg;
88     msg.sig = BTC_SIG_API_CALL;
89     msg.pid = BTC_PID_HD;
90     msg.act = BTC_HD_UNREGISTER_APP_EVT;
91 
92     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
93     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
94 }
95 
esp_bt_hid_device_connect(esp_bd_addr_t bd_addr)96 esp_err_t esp_bt_hid_device_connect(esp_bd_addr_t bd_addr)
97 {
98     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
99 
100     btc_msg_t msg;
101     btc_hidd_args_t args;
102     memset(&args, 0, sizeof(btc_hidd_args_t));
103     memcpy(args.connect.bd_addr, bd_addr, sizeof(esp_bd_addr_t));
104 
105     msg.sig = BTC_SIG_API_CALL;
106     msg.pid = BTC_PID_HD;
107     msg.act = BTC_HD_CONNECT_EVT;
108 
109     bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_hidd_args_t), NULL);
110     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
111 }
112 
esp_bt_hid_device_disconnect(void)113 esp_err_t esp_bt_hid_device_disconnect(void)
114 {
115     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
116 
117     btc_msg_t msg;
118     msg.sig = BTC_SIG_API_CALL;
119     msg.pid = BTC_PID_HD;
120     msg.act = BTC_HD_DISCONNECT_EVT;
121 
122     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
123     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
124 }
125 
esp_bt_hid_device_send_report(esp_hidd_report_type_t type,uint8_t id,uint16_t len,uint8_t * data)126 esp_err_t esp_bt_hid_device_send_report(esp_hidd_report_type_t type, uint8_t id, uint16_t len, uint8_t* data)
127 {
128     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
129 
130     btc_msg_t msg;
131     msg.sig = BTC_SIG_API_CALL;
132     msg.pid = BTC_PID_HD;
133     msg.act = BTC_HD_SEND_REPORT_EVT;
134 
135     btc_hidd_args_t args;
136     memset(&args, 0, sizeof(btc_hidd_args_t));
137     args.send_report.type = type;
138     args.send_report.id = id;
139     args.send_report.len = len;
140     args.send_report.data = data;
141 
142     bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_hidd_args_t), btc_hd_arg_deep_copy);
143     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
144 }
145 
esp_bt_hid_device_report_error(esp_hidd_handshake_error_t error)146 esp_err_t esp_bt_hid_device_report_error(esp_hidd_handshake_error_t error)
147 {
148     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
149 
150     btc_msg_t msg;
151     msg.sig = BTC_SIG_API_CALL;
152     msg.pid = BTC_PID_HD;
153     msg.act = BTC_HD_REPORT_ERROR_EVT;
154 
155     btc_hidd_args_t args;
156     memset(&args, 0, sizeof(btc_hidd_args_t));
157     args.error = error;
158 
159     bt_status_t stat = btc_transfer_context(&msg, &args, sizeof(btc_hidd_args_t), NULL);
160     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
161 }
162 
esp_bt_hid_device_virtual_cable_unplug(void)163 esp_err_t esp_bt_hid_device_virtual_cable_unplug(void)
164 {
165     ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);
166 
167     btc_msg_t msg;
168     msg.sig = BTC_SIG_API_CALL;
169     msg.pid = BTC_PID_HD;
170     msg.act = BTC_HD_UNPLUG_EVT;
171 
172     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
173     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
174 }
175 
176 #endif /* defined BTC_HD_INCLUDED && BTC_HD_INCLUDED == TRUE */
177