-
Notifications
You must be signed in to change notification settings - Fork 15
/
esp_hidd_prf_api.c
165 lines (131 loc) · 5.17 KB
/
esp_hidd_prf_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_hidd_prf_api.h"
#include "hidd_le_prf_int.h"
#include "hid_dev.h"
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
// HID keyboard input report length
#define HID_KEYBOARD_IN_RPT_LEN 8
// HID LED output report length
#define HID_LED_OUT_RPT_LEN 1
// HID mouse input report length
#define HID_MOUSE_IN_RPT_LEN 5
// HID gamepad input report length
#define HID_GAMEPAD_IN_RPT_LEN 6
// HID consumer control input report length
#define HID_CC_IN_RPT_LEN 2
esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks)
{
esp_err_t hidd_status;
if(callbacks != NULL) {
hidd_le_env.hidd_cb = callbacks;
} else {
return ESP_FAIL;
}
if((hidd_status = hidd_register_cb()) != ESP_OK) {
return hidd_status;
}
esp_ble_gatts_app_register(BATTRAY_APP_ID);
if((hidd_status = esp_ble_gatts_app_register(HIDD_APP_ID)) != ESP_OK) {
return hidd_status;
}
return hidd_status;
}
esp_err_t esp_hidd_profile_init(void)
{
if (hidd_le_env.enabled) {
ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
return ESP_FAIL;
}
// Reset the hid device target environment
memset(&hidd_le_env, 0, sizeof(hidd_le_env_t));
hidd_le_env.enabled = true;
return ESP_OK;
}
esp_err_t esp_hidd_profile_deinit(void)
{
uint16_t hidd_svc_hdl = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC];
if (!hidd_le_env.enabled) {
ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized");
return ESP_OK;
}
if(hidd_svc_hdl != 0) {
esp_ble_gatts_stop_service(hidd_svc_hdl);
esp_ble_gatts_delete_service(hidd_svc_hdl);
} else {
return ESP_FAIL;
}
/* register the HID device profile to the BTA_GATTS module*/
esp_ble_gatts_app_unregister(hidd_le_env.gatt_if);
return ESP_OK;
}
uint16_t esp_hidd_get_version(void)
{
return HIDD_VERSION;
}
void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed)
{
uint8_t buffer[HID_CC_IN_RPT_LEN] = {0, 0};
if (key_pressed) {
ESP_LOGD(HID_LE_PRF_TAG, "hid_consumer_build_report");
hid_consumer_build_report(buffer, key_cmd);
}
ESP_LOGD(HID_LE_PRF_TAG, "buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]);
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT, HID_CC_IN_RPT_LEN, buffer);
return;
}
void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key)
{
if (num_key > HID_KEYBOARD_IN_RPT_LEN - 2) {
ESP_LOGE(HID_LE_PRF_TAG, "%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN);
return;
}
uint8_t buffer[HID_KEYBOARD_IN_RPT_LEN] = {0};
buffer[0] = special_key_mask;
for (int i = 0; i < num_key; i++) {
buffer[i+2] = keyboard_cmd[i];
}
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]);
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT, HID_KEYBOARD_IN_RPT_LEN, buffer);
return;
}
void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y)
{
uint8_t buffer[HID_MOUSE_IN_RPT_LEN];
buffer[0] = mouse_button; // Buttons
buffer[1] = mickeys_x; // X
buffer[2] = mickeys_y; // Y
buffer[3] = 0; // Wheel
buffer[4] = 0; // AC Pan
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_MOUSE_IN_RPT_LEN, buffer);
return;
}
void esp_hidd_send_joystick_value(uint16_t conn_id, uint16_t joystick_buttons, uint8_t joystick_x, uint8_t joystick_y, uint8_t joystick_z, uint8_t joystick_rx)
{
uint8_t buffer[HID_GAMEPAD_IN_RPT_LEN];
ESP_LOGD(HID_LE_PRF_TAG, "the buttons value = %d js1 = %d, %d js2 = %d, %d", joystick_buttons, joystick_x, joystick_y, joystick_z, joystick_rx);
buffer[0]=joystick_buttons & 0xff;
buffer[1] = ( joystick_buttons >> 8);
buffer[2] = ( joystick_x ^ 0x80 ); // X
buffer[3] = (( joystick_y ^ 0x80 ) * -1) - 1; // Y
buffer[4] = ( joystick_z ^ 0x80 ); // X
buffer[5] = (( joystick_rx ^ 0x80 ) * -1) - 1; // Y
hid_dev_send_report(hidd_le_env.gatt_if, conn_id,
HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_GAMEPAD_IN_RPT_LEN, buffer);
return;
}