1 /******************************************************************************
2  *
3  *  Copyright (C) 2016 The Android Open Source Project
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *  Copyright (C) 2019 Blake Felt
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at:
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  ******************************************************************************/
20 #ifndef BTC_HH_H
21 #define BTC_HH_H
22 
23 #include <stdint.h>
24 #include "bta/bta_hh_api.h"
25 #include "btc/btc_task.h"
26 #include "osi/alarm.h"
27 #include "esp_hidh_api.h"
28 
29 #define BTC_HH_MAX_HID 8
30 #define BTC_HH_MAX_ADDED_DEV 32
31 
32 #define BTC_HH_MAX_KEYSTATES 3
33 #define BTC_HH_KEYSTATE_MASK_NUMLOCK 0x01
34 #define BTC_HH_KEYSTATE_MASK_CAPSLOCK 0x02
35 #define BTC_HH_KEYSTATE_MASK_SCROLLLOCK 0x04
36 
37 #define BTC_HH_MAX_POLLING_ATTEMPTS 10
38 #define BTC_HH_POLLING_SLEEP_DURATION_US 5000
39 
40 /*******************************************************************************
41  *  Type definitions and return values
42  ******************************************************************************/
43 typedef enum {
44     BTC_HH_INIT_EVT = 0,
45     BTC_HH_CONNECT_EVT,
46     BTC_HH_DISCONNECT_EVT,
47     BTC_HH_UNPLUG_EVT,
48     BTC_HH_SET_INFO_EVT,
49     BTC_HH_GET_PROTO_EVT,
50     BTC_HH_SET_PROTO_EVT,
51     BTC_HH_GET_IDLE_EVT,
52     BTC_HH_SET_IDLE_EVT,
53     BTC_HH_GET_REPORT_EVT,
54     BTC_HH_SET_REPORT_EVT,
55     BTC_HH_SEND_DATA_EVT,
56     BTC_HH_DEINIT_EVT,
57 } BTC_HH_EVT;
58 
59 typedef enum {
60     BTC_HH_DISABLED = 0,
61     BTC_HH_ENABLED,
62     BTC_HH_DISABLING,
63     BTC_HH_DEV_UNKNOWN,
64     BTC_HH_DEV_CONNECTING,
65     BTC_HH_DEV_CONNECTED,
66     BTC_HH_DEV_DISCONNECTED
67 } BTC_HH_STATUS;
68 
69 typedef struct {
70     esp_hidh_connection_state_t dev_status;
71     uint8_t dev_handle;
72     BD_ADDR bd_addr;
73     uint16_t attr_mask;
74     uint8_t sub_class;
75     uint8_t app_id;
76     bool ready_for_data;
77     osi_alarm_t *vup_timer;
78     bool local_vup; // Indicated locally initiated VUP
79 } btc_hh_device_t;
80 
81 /* Control block to maintain properties of devices */
82 typedef struct {
83     uint8_t dev_handle;
84     BD_ADDR bd_addr;
85     uint16_t attr_mask;
86 } btc_hh_added_device_t;
87 
88 /**
89  * BTC-HH control block to maintain added devices and currently
90  * connected hid devices
91  */
92 typedef struct {
93     BTC_HH_STATUS status;
94     btc_hh_device_t devices[BTC_HH_MAX_HID];
95     uint32_t device_num;
96     BTC_HH_EVT add_event;
97     btc_hh_added_device_t added_devices[BTC_HH_MAX_ADDED_DEV];
98     btc_hh_device_t *p_curr_dev;
99     bool service_dereg_active;
100     BD_ADDR pending_conn_address;
101 } btc_hh_cb_t;
102 
103 /* btc_spp_args_t */
104 typedef union {
105     // BTC_HH_CONNECT_EVT
106     struct connect_arg {
107         BD_ADDR bd_addr;
108     } connect;
109 
110     // BTC_HH_DISCONNECT_EVT
111     struct disconnect_arg {
112         BD_ADDR bd_addr;
113     } disconnect;
114 
115     // BTC_HH_UNPLUG_EVT
116     struct unplug_arg {
117         BD_ADDR bd_addr;
118     } unplug;
119 
120     // BTC_HH_SET_INFO_EVT
121     struct set_info_arg {
122         BD_ADDR bd_addr;
123         esp_hidh_hid_info_t *hid_info;
124     } set_info;
125 
126     // BTC_HH_GET_PROTO_EVT
127     struct get_protocol_arg {
128         BD_ADDR bd_addr;
129     } get_protocol;
130 
131     // BTC_HH_SET_PROTO_EVT
132     struct set_protocol_arg {
133         BD_ADDR bd_addr;
134         esp_hidh_protocol_mode_t protocol_mode;
135     } set_protocol;
136 
137     // BTC_HH_GET_IDLE_EVT
138     struct get_idle_arg {
139         BD_ADDR bd_addr;
140     } get_idle;
141 
142     // BTC_HH_SET_IDLE_EVT
143     struct set_idle_arg {
144         BD_ADDR bd_addr;
145         uint16_t idle_time;
146     } set_idle;
147 
148     // BTC_HH_GET_REPORT_EVT
149     struct get_report_arg {
150         BD_ADDR bd_addr;
151         esp_hidh_report_type_t report_type;
152         uint8_t report_id;
153         int buffer_size;
154     } get_report;
155 
156     // BTC_HH_SET_REPORT_EVT
157     struct set_report_arg {
158         BD_ADDR bd_addr;
159         esp_hidh_report_type_t report_type;
160         size_t len;
161         uint8_t *report;
162     } set_report;
163 
164     // BTC_HH_SEND_DATA_EVT
165     struct send_data_arg {
166         BD_ADDR bd_addr;
167         size_t len;
168         uint8_t *data;
169     } send_data;
170 } btc_hidh_args_t;
171 /*******************************************************************************
172  *  Variables
173  ******************************************************************************/
174 extern btc_hh_cb_t btc_hh_cb;
175 /*******************************************************************************
176  *  Functions
177  ******************************************************************************/
178 
179 void btc_hh_call_handler(btc_msg_t *msg);
180 
181 void btc_hh_cb_handler(btc_msg_t *msg);
182 
183 void btc_hh_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
184 
185 void btc_hh_cb_arg_deep_free(btc_msg_t *msg);
186 
187 bool btc_hh_add_added_dev(BD_ADDR bd_addr, uint16_t attr_mask);
188 
189 #endif /* BTC_HH_H */
190