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 #include "esp_hidd.h"
16 #include "esp_hidd_private.h"
17 #include "esp_event_base.h"
18 
19 #if CONFIG_GATTS_ENABLE
20 #include "ble_hidd.h"
21 #endif /* CONFIG_GATTS_ENABLE */
22 
23 #if CONFIG_BT_HID_DEVICE_ENABLED
24 #include "bt_hidd.h"
25 #endif /* CONFIG_BT_HID_DEVICE_ENABLED */
26 
27 ESP_EVENT_DEFINE_BASE(ESP_HIDD_EVENTS);
28 
esp_hidd_dev_init(const esp_hid_device_config_t * config,esp_hid_transport_t transport,esp_event_handler_t callback,esp_hidd_dev_t ** dev_out)29 esp_err_t esp_hidd_dev_init(const esp_hid_device_config_t *config, esp_hid_transport_t transport, esp_event_handler_t callback, esp_hidd_dev_t **dev_out)
30 {
31     esp_err_t ret = ESP_OK;
32     esp_hidd_dev_t *dev = (esp_hidd_dev_t *)calloc(1, sizeof(esp_hidd_dev_t));
33     if (dev == NULL) {
34         return ESP_FAIL;
35     }
36 
37     switch (transport) {
38 #if CONFIG_GATTS_ENABLE
39     case ESP_HID_TRANSPORT_BLE:
40         ret = esp_ble_hidd_dev_init(dev, config, callback);
41         break;
42 #endif /* CONFIG_GATTS_ENABLE */
43 #if CONFIG_BT_HID_DEVICE_ENABLED
44     case ESP_HID_TRANSPORT_BT:
45         ret = esp_bt_hidd_dev_init(dev, config, callback);
46         break;
47 #endif /* CONFIG_BT_HID_DEVICE_ENABLED */
48     default:
49         ret = ESP_FAIL;
50         break;
51     }
52 
53     if (ret != ESP_OK) {
54         free(dev);
55         return ret;
56     }
57     dev->transport = transport;
58     *dev_out = dev;
59     return ret;
60 }
61 
esp_hidd_dev_deinit(esp_hidd_dev_t * dev)62 esp_err_t esp_hidd_dev_deinit(esp_hidd_dev_t *dev)
63 {
64     if (dev == NULL) {
65         return ESP_FAIL;
66     }
67     esp_err_t ret = dev->deinit(dev->dev);
68     if (ret != ESP_OK) {
69         return ret;
70     }
71     free(dev);
72     return ret;
73 }
74 
esp_hidd_dev_transport_get(esp_hidd_dev_t * dev)75 esp_hid_transport_t esp_hidd_dev_transport_get(esp_hidd_dev_t *dev)
76 {
77     if (dev == NULL) {
78         return ESP_HID_TRANSPORT_MAX;
79     }
80     return dev->transport;
81 }
82 
esp_hidd_dev_connected(esp_hidd_dev_t * dev)83 bool esp_hidd_dev_connected(esp_hidd_dev_t *dev)
84 {
85     if (dev == NULL) {
86         return false;
87     }
88     return dev->connected(dev->dev);
89 }
90 
esp_hidd_dev_battery_set(esp_hidd_dev_t * dev,uint8_t level)91 esp_err_t esp_hidd_dev_battery_set(esp_hidd_dev_t *dev, uint8_t level)
92 {
93     if (dev == NULL) {
94         return ESP_FAIL;
95     }
96     return dev->battery_set(dev->dev, level);
97 }
98 
esp_hidd_dev_input_set(esp_hidd_dev_t * dev,size_t map_index,size_t report_id,uint8_t * data,size_t length)99 esp_err_t esp_hidd_dev_input_set(esp_hidd_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length)
100 {
101     if (dev == NULL) {
102         return ESP_FAIL;
103     }
104     return dev->input_set(dev->dev, map_index, report_id, data, length);
105 }
106 
esp_hidd_dev_feature_set(esp_hidd_dev_t * dev,size_t map_index,size_t report_id,uint8_t * data,size_t length)107 esp_err_t esp_hidd_dev_feature_set(esp_hidd_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length)
108 {
109     if (dev == NULL) {
110         return ESP_FAIL;
111     }
112     return dev->feature_set(dev->dev, map_index, report_id, data, length);
113 }
114 
esp_hidd_dev_event_handler_register(esp_hidd_dev_t * dev,esp_event_handler_t callback,esp_hidd_event_t event)115 esp_err_t esp_hidd_dev_event_handler_register(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event)
116 {
117     if (dev == NULL) {
118         return ESP_FAIL;
119     }
120     return dev->event_handler_register(dev->dev, callback, event);
121 }
122 
esp_hidd_dev_event_handler_unregister(esp_hidd_dev_t * dev,esp_event_handler_t callback,esp_hidd_event_t event)123 esp_err_t esp_hidd_dev_event_handler_unregister(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event)
124 {
125     if (dev == NULL) {
126         return ESP_FAIL;
127     }
128     return dev->event_handler_unregister(dev->dev, callback, event);
129 }
130 
131 /**
132  * The deep copy data append the end of the esp_hidd_event_data_t, move the data pointer to the correct address. This is
133  * a workaround way, it's better to use flexiable array in the interface.
134  */
esp_hidd_process_event_data_handler(void * event_handler_arg,esp_event_base_t event_base,int32_t event_id,void * event_data)135 void esp_hidd_process_event_data_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id,
136                                          void *event_data)
137 {
138     esp_hidd_event_t event = (esp_hidd_event_t)event_id;
139     esp_hidd_event_data_t *param = (esp_hidd_event_data_t *)event_data;
140 
141     switch (event) {
142     case ESP_HIDD_OUTPUT_EVENT:
143         if (param->output.length && param->output.data) {
144             param->output.data = (uint8_t *)param + sizeof(esp_hidd_event_data_t);
145         }
146         break;
147     case ESP_HIDD_FEATURE_EVENT:
148         if (param->feature.length && param->feature.data) {
149             param->feature.data = (uint8_t *)param + sizeof(esp_hidd_event_data_t);
150         }
151         break;
152     default:
153         break;
154     }
155 }
156