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 /*******************************************************************************
49 * Type definitions and return values
50 ******************************************************************************/
51
52 typedef enum : unsigned {
53 BTIF_HH_DISABLED = 0,
54 BTIF_HH_ENABLED,
55 BTIF_HH_DISABLING,
56 BTIF_HH_DEV_UNKNOWN,
57 BTIF_HH_DEV_CONNECTING,
58 BTIF_HH_DEV_CONNECTED,
59 BTIF_HH_DEV_DISCONNECTED
60 } BTIF_HH_STATUS;
61
62 #define CASE_RETURN_TEXT(code) \
63 case code: \
64 return #code
65
btif_hh_status_text(const BTIF_HH_STATUS & status)66 inline std::string btif_hh_status_text(const BTIF_HH_STATUS& status) {
67 switch (status) {
68 CASE_RETURN_TEXT(BTIF_HH_DISABLED);
69 CASE_RETURN_TEXT(BTIF_HH_ENABLED);
70 CASE_RETURN_TEXT(BTIF_HH_DISABLING);
71 CASE_RETURN_TEXT(BTIF_HH_DEV_UNKNOWN);
72 CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTING);
73 CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTED);
74 CASE_RETURN_TEXT(BTIF_HH_DEV_DISCONNECTED);
75 default:
76 return base::StringPrintf("UNKNOWN[%u]", status);
77 }
78 }
79 #undef CASE_RETURN_TEXT
80
81 // Shared with uhid polling thread
82 typedef struct {
83 bthh_connection_state_t dev_status;
84 uint8_t dev_handle;
85 RawAddress bd_addr;
86 bool le_hid;
87 tBTA_HH_ATTR_MASK attr_mask;
88 uint8_t sub_class;
89 uint8_t app_id;
90 int fd;
91 bool ready_for_data;
92 pthread_t hh_poll_thread_id;
93 pid_t pid{-1};
94 uint8_t hh_keep_polling;
95 alarm_t* vup_timer;
96 fixed_queue_t* get_rpt_id_queue;
97 #ifdef OS_ANDROID
98 fixed_queue_t* set_rpt_id_queue;
99 #endif // OS_ANDROID
100 uint8_t get_rpt_snt;
101 bool local_vup; // Indicated locally initiated VUP
102 } btif_hh_device_t;
103
104 /* Control block to maintain properties of devices */
105 typedef struct {
106 uint8_t dev_handle;
107 RawAddress bd_addr;
108 tBTA_HH_ATTR_MASK attr_mask;
109 } btif_hh_added_device_t;
110
111 /**
112 * BTIF-HH control block to maintain added devices and currently
113 * connected hid devices
114 */
115 typedef struct {
116 BTIF_HH_STATUS status;
117 btif_hh_device_t devices[BTIF_HH_MAX_HID];
118 uint32_t device_num;
119 btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV];
120 bool service_dereg_active;
121 RawAddress pending_conn_address;
122 } btif_hh_cb_t;
123
124 /*******************************************************************************
125 * Functions
126 ******************************************************************************/
127
128 extern btif_hh_cb_t btif_hh_cb;
129
130 extern btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
131 extern void btif_hh_remove_device(RawAddress bd_addr);
132 extern bool btif_hh_add_added_dev(const RawAddress& bda,
133 tBTA_HH_ATTR_MASK attr_mask);
134 extern bt_status_t btif_hh_virtual_unplug(const RawAddress* bd_addr);
135 extern void btif_hh_disconnect(RawAddress* bd_addr);
136 extern void btif_hh_setreport(btif_hh_device_t* p_dev,
137 bthh_report_type_t r_type, uint16_t size,
138 uint8_t* report);
139 extern void btif_hh_senddata(btif_hh_device_t* p_dev, uint16_t size,
140 uint8_t* report);
141 extern void btif_hh_getreport(btif_hh_device_t* p_dev,
142 bthh_report_type_t r_type, uint8_t reportId,
143 uint16_t bufferSize);
144 extern void btif_hh_service_registration(bool enable);
145
146 #endif
147