1 /******************************************************************************
2 *
3 * Copyright 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #ifndef BTIF_HH_H
20 #define BTIF_HH_H
21
22 #include <base/strings/stringprintf.h>
23 #include <hardware/bluetooth.h>
24 #include <hardware/bt_hh.h>
25 #include <pthread.h>
26 #include <stdint.h>
27
28 #include "bta/include/bta_hh_api.h"
29 #include "osi/include/alarm.h"
30 #include "osi/include/fixed_queue.h"
31 #include "types/raw_address.h"
32
33 /*******************************************************************************
34 * Constants & Macros
35 ******************************************************************************/
36
37 #define BTIF_HH_MAX_HID 8
38 #define BTIF_HH_MAX_ADDED_DEV 32
39
40 #define BTIF_HH_MAX_KEYSTATES 3
41 #define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01
42 #define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02
43 #define BTIF_HH_KEYSTATE_MASK_SCROLLLOCK 0x04
44
45 #define BTIF_HH_MAX_POLLING_ATTEMPTS 10
46 #define BTIF_HH_POLLING_SLEEP_DURATION_US 5000
47
48 #ifndef ENABLE_UHID_SET_REPORT
49 #if defined(__ANDROID__) || defined(TARGET_FLOSS)
50 #define ENABLE_UHID_SET_REPORT 1
51 #else
52 #define ENABLE_UHID_SET_REPORT 0
53 #endif
54 #endif
55
56 /*******************************************************************************
57 * Type definitions and return values
58 ******************************************************************************/
59
60 typedef enum : unsigned {
61 BTIF_HH_DISABLED = 0,
62 BTIF_HH_ENABLED,
63 BTIF_HH_DISABLING,
64 BTIF_HH_DEV_UNKNOWN,
65 BTIF_HH_DEV_CONNECTING,
66 BTIF_HH_DEV_CONNECTED,
67 BTIF_HH_DEV_DISCONNECTED
68 } BTIF_HH_STATUS;
69
70 #define CASE_RETURN_TEXT(code) \
71 case code: \
72 return #code
73
btif_hh_status_text(const BTIF_HH_STATUS & status)74 inline std::string btif_hh_status_text(const BTIF_HH_STATUS& status) {
75 switch (status) {
76 CASE_RETURN_TEXT(BTIF_HH_DISABLED);
77 CASE_RETURN_TEXT(BTIF_HH_ENABLED);
78 CASE_RETURN_TEXT(BTIF_HH_DISABLING);
79 CASE_RETURN_TEXT(BTIF_HH_DEV_UNKNOWN);
80 CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTING);
81 CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTED);
82 CASE_RETURN_TEXT(BTIF_HH_DEV_DISCONNECTED);
83 default:
84 return base::StringPrintf("UNKNOWN[%u]", status);
85 }
86 }
87 #undef CASE_RETURN_TEXT
88
89 // Shared with uhid polling thread
90 typedef struct {
91 bthh_connection_state_t dev_status;
92 uint8_t dev_handle;
93 RawAddress bd_addr;
94 tBTA_HH_ATTR_MASK attr_mask;
95 uint8_t sub_class;
96 uint8_t app_id;
97 int fd;
98 bool ready_for_data;
99 pthread_t hh_poll_thread_id;
100 uint8_t hh_keep_polling;
101 alarm_t* vup_timer;
102 fixed_queue_t* get_rpt_id_queue;
103 #if ENABLE_UHID_SET_REPORT
104 fixed_queue_t* set_rpt_id_queue;
105 #endif // ENABLE_UHID_SET_REPORT
106 bool local_vup; // Indicated locally initiated VUP
107 } btif_hh_device_t;
108
109 /* Control block to maintain properties of devices */
110 typedef struct {
111 uint8_t dev_handle;
112 RawAddress bd_addr;
113 tBTA_HH_ATTR_MASK attr_mask;
114 } btif_hh_added_device_t;
115
116 /**
117 * BTIF-HH control block to maintain added devices and currently
118 * connected hid devices
119 */
120 typedef struct {
121 BTIF_HH_STATUS status;
122 btif_hh_device_t devices[BTIF_HH_MAX_HID];
123 uint32_t device_num;
124 btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV];
125 bool service_dereg_active;
126 RawAddress pending_conn_address;
127 } btif_hh_cb_t;
128
129 /*******************************************************************************
130 * Functions
131 ******************************************************************************/
132
133 extern btif_hh_cb_t btif_hh_cb;
134
135 btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
136 void btif_hh_remove_device(RawAddress bd_addr);
137 bool btif_hh_add_added_dev(const RawAddress& bda, tBTA_HH_ATTR_MASK attr_mask);
138 bt_status_t btif_hh_virtual_unplug(const RawAddress* bd_addr);
139 void btif_hh_disconnect(RawAddress* bd_addr);
140 void btif_hh_setreport(btif_hh_device_t* p_dev, bthh_report_type_t r_type,
141 uint16_t size, uint8_t* report);
142 void btif_hh_senddata(btif_hh_device_t* p_dev, uint16_t size, uint8_t* report);
143 void btif_hh_getreport(btif_hh_device_t* p_dev, bthh_report_type_t r_type,
144 uint8_t reportId, uint16_t bufferSize);
145 void btif_hh_service_registration(bool enable);
146
147 void DumpsysHid(int fd);
148
149 #endif
150