1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _ESP_HIDH_PRIVATE_H_
16 #define _ESP_HIDH_PRIVATE_H_
17 
18 #include "esp_hidh.h"
19 #if CONFIG_BLUEDROID_ENABLED
20 #include "esp_gap_bt_api.h"
21 #endif /* CONFIG_BLUEDROID_ENABLED */
22 #include "freertos/FreeRTOS.h"
23 #include "freertos/task.h"
24 #include "freertos/semphr.h"
25 #include "esp_event.h"
26 #include "sys/queue.h"
27 #include "esp_timer.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * @brief HIDH device report data
35  */
36 typedef struct esp_hidh_dev_report_s {
37     struct esp_hidh_dev_report_s *next;
38     uint8_t map_index;      //the index of the report map
39     uint8_t report_id;      //the id of the report
40     uint8_t report_type;    //input, output or feature
41     uint8_t protocol_mode;  //boot or report
42     size_t value_len;       //maximum len of value by report map
43     esp_hid_usage_t usage;  //generic, keyboard or mouse
44     //BLE properties
45     uint16_t handle;        //handle to the value
46     uint16_t ccc_handle;    //handle to client config
47     uint8_t permissions;    //report permissions
48 } esp_hidh_dev_report_t;
49 
50 /**
51  * @brief HIDH device data
52  */
53 struct esp_hidh_dev_s {
54     struct esp_hidh_dev_s   *next;
55 
56     esp_hid_device_config_t config;
57     esp_hid_usage_t         usage;
58     esp_hid_transport_t     transport;      //BT, BLE or USB
59     esp_hid_trans_type_t    trans_type;     //indicate what transaction is going on, new transaction only be allowed after the previous done
60     esp_timer_handle_t      trans_timer;    //transactiion timer
61     uint8_t                 report_type;    //Get_Report tansaction report_type
62     uint8_t                 report_id;      //Get_Report tansaction report_id
63     uint8_t                 protocol_mode;  //device protocol mode
64     bool                    connected;      //we have all required data to communicate
65     bool                    opened;         //we opened the device manually, else the device connected to us
66     bool                    added;          //If lower layer has added the device
67     bool                    is_orig;        //If host initiate the connection
68     bool                    in_use;         //If false, it will be deleted from the devices list.
69     int                     status;         //status of the last command
70 
71     size_t                  reports_len;
72     esp_hidh_dev_report_t   *reports;
73 
74     void                    *tmp;
75     size_t                  tmp_len;
76 
77     xSemaphoreHandle        semaphore;
78     xSemaphoreHandle        mutex;
79 
80     esp_err_t               (*close)        (esp_hidh_dev_t *dev);
81     esp_err_t               (*report_write) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len);
82     esp_err_t               (*report_read)  (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, size_t max_length, uint8_t *value, size_t *value_len);
83     esp_err_t               (*set_report)   (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len);
84     esp_err_t               (*get_idle)     (esp_hidh_dev_t *dev);
85     esp_err_t               (*set_idle)     (esp_hidh_dev_t *dev, uint8_t idle_time);
86     esp_err_t               (*get_protocol) (esp_hidh_dev_t *dev);
87     esp_err_t               (*set_protocol) (esp_hidh_dev_t *dev, uint8_t protocol_mode);
88     void                    (*dump)         (esp_hidh_dev_t *dev, FILE *fp);
89 
90 #if CONFIG_BLUEDROID_ENABLED
91     esp_bd_addr_t bda;
92 #endif /* CONFIG_BLUEDROID_ENABLED */
93 
94     union {
95 #if CONFIG_BT_HID_HOST_ENABLED
96         struct {
97             esp_bt_cod_t cod;
98             uint8_t handle;
99             // uint8_t sub_class;
100             // uint8_t app_id;
101             // uint16_t attr_mask;
102         } bt;
103 #endif /* CONFIG_BT_HID_HOST_ENABLED */
104 #if CONFIG_GATTC_ENABLE
105         struct {
106             esp_ble_addr_type_t address_type;
107             int conn_id;
108             uint16_t appearance;
109             uint16_t battery_handle;
110             uint16_t battery_ccc_handle;
111         } ble;
112 #endif /* CONFIG_GATTC_ENABLE */
113     };
114     TAILQ_ENTRY(esp_hidh_dev_s) devices;
115 };
116 
117 typedef TAILQ_HEAD(esp_hidh_dev_head_s, esp_hidh_dev_s) esp_hidh_dev_head_t;
118 
119 esp_hidh_dev_t *esp_hidh_dev_malloc(void);
120 
121 #if CONFIG_BLUEDROID_ENABLED
122 esp_hidh_dev_t *esp_hidh_dev_get_by_bda(esp_bd_addr_t bda); //BT/BLE
123 esp_hidh_dev_t *esp_hidh_dev_get_by_handle(uint8_t handle); //Classic Bluetooth Only
124 esp_hidh_dev_t *esp_hidh_dev_get_by_conn_id(uint16_t conn_id); //BLE Only
125 #endif /* CONFIG_BLUEDROID_ENABLED */
126 
127 esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_id_type_proto(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t protocol_mode);
128 esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_id_and_type(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type);
129 esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_len_and_proto(esp_hidh_dev_t *dev, size_t len, int protocol_mode);
130 esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_id_and_proto(esp_hidh_dev_t *dev, size_t report_id, int protocol_mode);
131 esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_proto_and_data(esp_hidh_dev_t *dev, int protocol_mode,
132                                                                        size_t len, const uint8_t *data, bool *has_report_id);
133 esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_handle(esp_hidh_dev_t *dev, uint16_t handle);  //BLE Only
134 void esp_hidh_process_event_data_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
135                                          void *event_data);
136 void esp_hidh_dev_lock(esp_hidh_dev_t *dev);
137 void esp_hidh_dev_unlock(esp_hidh_dev_t *dev);
138 void esp_hidh_dev_wait(esp_hidh_dev_t *dev);
139 void esp_hidh_dev_send(esp_hidh_dev_t *dev);
140 esp_err_t esp_hidh_dev_free_inner(esp_hidh_dev_t *dev);
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #endif /* _ESP_HIDH_PRIVATE_H_ */
146