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 <hardware/bluetooth.h>
23 #include <hardware/bt_hh.h>
24 #include <pthread.h>
25 #include <stdint.h>
26 #include "bta/include/bta_hh_api.h"
27 #include "btu.h"
28 #include "osi/include/fixed_queue.h"
29 
30 /*******************************************************************************
31  *  Constants & Macros
32  ******************************************************************************/
33 
34 #define BTIF_HH_MAX_HID 8
35 #define BTIF_HH_MAX_ADDED_DEV 32
36 
37 #define BTIF_HH_MAX_KEYSTATES 3
38 #define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01
39 #define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02
40 #define BTIF_HH_KEYSTATE_MASK_SCROLLLOCK 0x04
41 
42 #define BTIF_HH_MAX_POLLING_ATTEMPTS 10
43 #define BTIF_HH_POLLING_SLEEP_DURATION_US 5000
44 
45 /*******************************************************************************
46  *  Type definitions and return values
47  ******************************************************************************/
48 
49 typedef enum : unsigned {
50   BTIF_HH_DISABLED = 0,
51   BTIF_HH_ENABLED,
52   BTIF_HH_DISABLING,
53   BTIF_HH_DEV_UNKNOWN,
54   BTIF_HH_DEV_CONNECTING,
55   BTIF_HH_DEV_CONNECTED,
56   BTIF_HH_DEV_DISCONNECTED
57 } BTIF_HH_STATUS;
58 
59 #define CASE_RETURN_TEXT(code) \
60   case code:                   \
61     return #code
62 
btif_hh_status_text(const BTIF_HH_STATUS & status)63 inline std::string btif_hh_status_text(const BTIF_HH_STATUS& status) {
64   switch (status) {
65     CASE_RETURN_TEXT(BTIF_HH_DISABLED);
66     CASE_RETURN_TEXT(BTIF_HH_ENABLED);
67     CASE_RETURN_TEXT(BTIF_HH_DISABLING);
68     CASE_RETURN_TEXT(BTIF_HH_DEV_UNKNOWN);
69     CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTING);
70     CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTED);
71     CASE_RETURN_TEXT(BTIF_HH_DEV_DISCONNECTED);
72     default:
73       return std::string("UNKNOWN[%hhu]", status);
74   }
75 }
76 #undef CASE_RETURN_TEXT
77 
78 // Shared with uhid polling thread
79 typedef struct {
80   bthh_connection_state_t dev_status;
81   uint8_t dev_handle;
82   RawAddress bd_addr;
83   tBTA_HH_ATTR_MASK attr_mask;
84   uint8_t sub_class;
85   uint8_t app_id;
86   int fd;
87   bool ready_for_data;
88   pthread_t hh_poll_thread_id;
89   pid_t pid{-1};
90   uint8_t hh_keep_polling;
91   alarm_t* vup_timer;
92   fixed_queue_t* get_rpt_id_queue;
93   uint8_t get_rpt_snt;
94   bool local_vup;  // Indicated locally initiated VUP
95 } btif_hh_device_t;
96 
97 /* Control block to maintain properties of devices */
98 typedef struct {
99   uint8_t dev_handle;
100   RawAddress bd_addr;
101   tBTA_HH_ATTR_MASK attr_mask;
102 } btif_hh_added_device_t;
103 
104 /**
105  * BTIF-HH control block to maintain added devices and currently
106  * connected hid devices
107  */
108 typedef struct {
109   BTIF_HH_STATUS status;
110   btif_hh_device_t devices[BTIF_HH_MAX_HID];
111   uint32_t device_num;
112   btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV];
113   btif_hh_device_t* p_curr_dev;
114   bool service_dereg_active;
115   RawAddress pending_conn_address;
116 } btif_hh_cb_t;
117 
118 /*******************************************************************************
119  *  Functions
120  ******************************************************************************/
121 
122 extern btif_hh_cb_t btif_hh_cb;
123 
124 extern btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
125 extern void btif_hh_remove_device(RawAddress bd_addr);
126 extern bool btif_hh_add_added_dev(const RawAddress& bda,
127                                   tBTA_HH_ATTR_MASK attr_mask);
128 extern bt_status_t btif_hh_virtual_unplug(const RawAddress* bd_addr);
129 extern void btif_hh_disconnect(RawAddress* bd_addr);
130 extern void btif_hh_setreport(btif_hh_device_t* p_dev,
131                               bthh_report_type_t r_type, uint16_t size,
132                               uint8_t* report);
133 extern void btif_hh_senddata(btif_hh_device_t* p_dev, uint16_t size,
134                              uint8_t* report);
135 extern void btif_hh_getreport(btif_hh_device_t* p_dev,
136                               bthh_report_type_t r_type, uint8_t reportId,
137                               uint16_t bufferSize);
138 extern void btif_hh_service_registration(bool enable);
139 
140 #endif
141