hid_dev.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "hid_dev.h"
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include "esp_log.h"
  19. static hid_report_map_t *hid_dev_rpt_tbl;
  20. static uint8_t hid_dev_rpt_tbl_Len;
  21. static hid_report_map_t *hid_dev_rpt_by_id(uint8_t id, uint8_t type)
  22. {
  23. hid_report_map_t *rpt = hid_dev_rpt_tbl;
  24. for (uint8_t i = hid_dev_rpt_tbl_Len; i > 0; i--, rpt++) {
  25. if (rpt->id == id && rpt->type == type && rpt->mode == hidProtocolMode) {
  26. return rpt;
  27. }
  28. }
  29. return NULL;
  30. }
  31. void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report)
  32. {
  33. hid_dev_rpt_tbl = p_report;
  34. hid_dev_rpt_tbl_Len = num_reports;
  35. return;
  36. }
  37. void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id,
  38. uint8_t id, uint8_t type, uint8_t length, uint8_t *data)
  39. {
  40. hid_report_map_t *p_rpt;
  41. // get att handle for report
  42. if ((p_rpt = hid_dev_rpt_by_id(id, type)) != NULL) {
  43. // if notifications are enabled
  44. ESP_LOGD(HID_LE_PRF_TAG, "%s(), send the report, handle = %d", __func__, p_rpt->handle);
  45. esp_ble_gatts_send_indicate(gatts_if, conn_id, p_rpt->handle, length, data, false);
  46. }
  47. return;
  48. }
  49. void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd)
  50. {
  51. if (!buffer) {
  52. ESP_LOGE(HID_LE_PRF_TAG, "%s(), the buffer is NULL, hid build report failed.", __func__);
  53. return;
  54. }
  55. switch (cmd) {
  56. case HID_CONSUMER_CHANNEL_UP:
  57. HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_UP);
  58. break;
  59. case HID_CONSUMER_CHANNEL_DOWN:
  60. HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_DOWN);
  61. break;
  62. case HID_CONSUMER_VOLUME_UP:
  63. HID_CC_RPT_SET_VOLUME_UP(buffer);
  64. break;
  65. case HID_CONSUMER_VOLUME_DOWN:
  66. HID_CC_RPT_SET_VOLUME_DOWN(buffer);
  67. break;
  68. case HID_CONSUMER_MUTE:
  69. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_MUTE);
  70. break;
  71. case HID_CONSUMER_POWER:
  72. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_POWER);
  73. break;
  74. case HID_CONSUMER_RECALL_LAST:
  75. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_LAST);
  76. break;
  77. case HID_CONSUMER_ASSIGN_SEL:
  78. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_ASSIGN_SEL);
  79. break;
  80. case HID_CONSUMER_PLAY:
  81. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PLAY);
  82. break;
  83. case HID_CONSUMER_PAUSE:
  84. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PAUSE);
  85. break;
  86. case HID_CONSUMER_RECORD:
  87. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_RECORD);
  88. break;
  89. case HID_CONSUMER_FAST_FORWARD:
  90. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_FAST_FWD);
  91. break;
  92. case HID_CONSUMER_REWIND:
  93. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_REWIND);
  94. break;
  95. case HID_CONSUMER_SCAN_NEXT_TRK:
  96. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_NEXT_TRK);
  97. break;
  98. case HID_CONSUMER_SCAN_PREV_TRK:
  99. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_PREV_TRK);
  100. break;
  101. case HID_CONSUMER_STOP:
  102. HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_STOP);
  103. break;
  104. default:
  105. break;
  106. }
  107. return;
  108. }