1 // Copyright 2017-2018 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_prf_api.h"
16 #include "hidd_le_prf_int.h"
17 #include "hid_dev.h"
18 #include <stdlib.h>
19 #include <string.h>
20 #include "esp_log.h"
21 
22 // HID keyboard input report length
23 #define HID_KEYBOARD_IN_RPT_LEN     8
24 
25 // HID LED output report length
26 #define HID_LED_OUT_RPT_LEN         1
27 
28 // HID mouse input report length
29 #define HID_MOUSE_IN_RPT_LEN        5
30 
31 // HID consumer control input report length
32 #define HID_CC_IN_RPT_LEN           2
33 
esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks)34 esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks)
35 {
36     esp_err_t hidd_status;
37 
38     if(callbacks != NULL) {
39    	    hidd_le_env.hidd_cb = callbacks;
40     } else {
41         return ESP_FAIL;
42     }
43 
44     if((hidd_status = hidd_register_cb()) != ESP_OK) {
45         return hidd_status;
46     }
47 
48     esp_ble_gatts_app_register(BATTRAY_APP_ID);
49 
50     if((hidd_status = esp_ble_gatts_app_register(HIDD_APP_ID)) != ESP_OK) {
51         return hidd_status;
52     }
53 
54     return hidd_status;
55 }
56 
esp_hidd_profile_init(void)57 esp_err_t esp_hidd_profile_init(void)
58 {
59      if (hidd_le_env.enabled) {
60         ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
61         return ESP_FAIL;
62     }
63     // Reset the hid device target environment
64     memset(&hidd_le_env, 0, sizeof(hidd_le_env_t));
65     hidd_le_env.enabled = true;
66     return ESP_OK;
67 }
68 
esp_hidd_profile_deinit(void)69 esp_err_t esp_hidd_profile_deinit(void)
70 {
71     uint16_t hidd_svc_hdl = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC];
72     if (!hidd_le_env.enabled) {
73         ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
74         return ESP_OK;
75     }
76 
77     if(hidd_svc_hdl != 0) {
78 	esp_ble_gatts_stop_service(hidd_svc_hdl);
79 	esp_ble_gatts_delete_service(hidd_svc_hdl);
80     } else {
81 	return ESP_FAIL;
82    }
83 
84     /* register the HID device profile to the BTA_GATTS module*/
85     esp_ble_gatts_app_unregister(hidd_le_env.gatt_if);
86 
87     return ESP_OK;
88 }
89 
esp_hidd_get_version(void)90 uint16_t esp_hidd_get_version(void)
91 {
92 	return HIDD_VERSION;
93 }
94 
esp_hidd_send_consumer_value(uint16_t conn_id,uint8_t key_cmd,bool key_pressed)95 void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed)
96 {
97     uint8_t buffer[HID_CC_IN_RPT_LEN] = {0, 0};
98     if (key_pressed) {
99         ESP_LOGD(HID_LE_PRF_TAG, "hid_consumer_build_report");
100         hid_consumer_build_report(buffer, key_cmd);
101     }
102     ESP_LOGD(HID_LE_PRF_TAG, "buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]);
103     hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
104                         HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT, HID_CC_IN_RPT_LEN, buffer);
105     return;
106 }
107 
esp_hidd_send_keyboard_value(uint16_t conn_id,key_mask_t special_key_mask,uint8_t * keyboard_cmd,uint8_t num_key)108 void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key)
109 {
110     if (num_key > HID_KEYBOARD_IN_RPT_LEN - 2) {
111         ESP_LOGE(HID_LE_PRF_TAG, "%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN);
112         return;
113     }
114 
115     uint8_t buffer[HID_KEYBOARD_IN_RPT_LEN] = {0};
116 
117     buffer[0] = special_key_mask;
118 
119     for (int i = 0; i < num_key; i++) {
120         buffer[i+2] = keyboard_cmd[i];
121     }
122 
123     ESP_LOGD(HID_LE_PRF_TAG, "the key vaule = %d,%d,%d, %d, %d, %d,%d, %d", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]);
124     hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
125                         HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT, HID_KEYBOARD_IN_RPT_LEN, buffer);
126     return;
127 }
128 
esp_hidd_send_mouse_value(uint16_t conn_id,uint8_t mouse_button,int8_t mickeys_x,int8_t mickeys_y)129 void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y)
130 {
131     uint8_t buffer[HID_MOUSE_IN_RPT_LEN];
132 
133     buffer[0] = mouse_button;   // Buttons
134     buffer[1] = mickeys_x;           // X
135     buffer[2] = mickeys_y;           // Y
136     buffer[3] = 0;           // Wheel
137     buffer[4] = 0;           // AC Pan
138 
139     hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
140                         HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_MOUSE_IN_RPT_LEN, buffer);
141     return;
142 }
143