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 "hid_dev.h"
16 #include <stdint.h>
17 #include <string.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include "esp_log.h"
21
22 static hid_report_map_t *hid_dev_rpt_tbl;
23 static uint8_t hid_dev_rpt_tbl_Len;
24
hid_dev_rpt_by_id(uint8_t id,uint8_t type)25 static hid_report_map_t *hid_dev_rpt_by_id(uint8_t id, uint8_t type)
26 {
27 hid_report_map_t *rpt = hid_dev_rpt_tbl;
28
29 for (uint8_t i = hid_dev_rpt_tbl_Len; i > 0; i--, rpt++) {
30 if (rpt->id == id && rpt->type == type && rpt->mode == hidProtocolMode) {
31 return rpt;
32 }
33 }
34
35 return NULL;
36 }
37
hid_dev_register_reports(uint8_t num_reports,hid_report_map_t * p_report)38 void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report)
39 {
40 hid_dev_rpt_tbl = p_report;
41 hid_dev_rpt_tbl_Len = num_reports;
42 return;
43 }
44
hid_dev_send_report(esp_gatt_if_t gatts_if,uint16_t conn_id,uint8_t id,uint8_t type,uint8_t length,uint8_t * data)45 void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id,
46 uint8_t id, uint8_t type, uint8_t length, uint8_t *data)
47 {
48 hid_report_map_t *p_rpt;
49
50 // get att handle for report
51 if ((p_rpt = hid_dev_rpt_by_id(id, type)) != NULL) {
52 // if notifications are enabled
53 ESP_LOGD(HID_LE_PRF_TAG, "%s(), send the report, handle = %d", __func__, p_rpt->handle);
54 esp_ble_gatts_send_indicate(gatts_if, conn_id, p_rpt->handle, length, data, false);
55 }
56
57 return;
58 }
59
hid_consumer_build_report(uint8_t * buffer,consumer_cmd_t cmd)60 void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd)
61 {
62 if (!buffer) {
63 ESP_LOGE(HID_LE_PRF_TAG, "%s(), the buffer is NULL, hid build report failed.", __func__);
64 return;
65 }
66
67 switch (cmd) {
68 case HID_CONSUMER_CHANNEL_UP:
69 HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_UP);
70 break;
71
72 case HID_CONSUMER_CHANNEL_DOWN:
73 HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_DOWN);
74 break;
75
76 case HID_CONSUMER_VOLUME_UP:
77 HID_CC_RPT_SET_VOLUME_UP(buffer);
78 break;
79
80 case HID_CONSUMER_VOLUME_DOWN:
81 HID_CC_RPT_SET_VOLUME_DOWN(buffer);
82 break;
83
84 case HID_CONSUMER_MUTE:
85 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_MUTE);
86 break;
87
88 case HID_CONSUMER_POWER:
89 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_POWER);
90 break;
91
92 case HID_CONSUMER_RECALL_LAST:
93 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_LAST);
94 break;
95
96 case HID_CONSUMER_ASSIGN_SEL:
97 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_ASSIGN_SEL);
98 break;
99
100 case HID_CONSUMER_PLAY:
101 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PLAY);
102 break;
103
104 case HID_CONSUMER_PAUSE:
105 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PAUSE);
106 break;
107
108 case HID_CONSUMER_RECORD:
109 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_RECORD);
110 break;
111
112 case HID_CONSUMER_FAST_FORWARD:
113 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_FAST_FWD);
114 break;
115
116 case HID_CONSUMER_REWIND:
117 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_REWIND);
118 break;
119
120 case HID_CONSUMER_SCAN_NEXT_TRK:
121 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_NEXT_TRK);
122 break;
123
124 case HID_CONSUMER_SCAN_PREV_TRK:
125 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_PREV_TRK);
126 break;
127
128 case HID_CONSUMER_STOP:
129 HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_STOP);
130 break;
131
132 default:
133 break;
134 }
135
136 return;
137 }
138