hog.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /** @file
  2. * @brief HoG Service sample
  3. */
  4. /*
  5. * Copyright (c) 2016 Intel Corporation
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #include <errno.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include "base/types.h"
  13. #include <bluetooth/bluetooth.h>
  14. #include <bluetooth/conn.h>
  15. #include <bluetooth/gatt.h>
  16. #include <bluetooth/hci.h>
  17. #include <bluetooth/uuid.h>
  18. #include <logging/bt_log_impl.h>
  19. enum
  20. {
  21. HIDS_REMOTE_WAKE = BIT(0),
  22. HIDS_NORMALLY_CONNECTABLE = BIT(1),
  23. };
  24. struct hids_info
  25. {
  26. uint16_t version; /* version number of base USB HID Specification */
  27. uint8_t code; /* country HID Device hardware is localized for. */
  28. uint8_t flags;
  29. } __packed;
  30. struct hids_report
  31. {
  32. uint8_t id; /* report id */
  33. uint8_t type; /* report type */
  34. } __packed;
  35. static struct hids_info info = {
  36. .version = 0x0000,
  37. .code = 0x00,
  38. .flags = HIDS_NORMALLY_CONNECTABLE,
  39. };
  40. enum
  41. {
  42. HIDS_INPUT = 0x01,
  43. HIDS_OUTPUT = 0x02,
  44. HIDS_FEATURE = 0x03,
  45. };
  46. static struct hids_report input = {
  47. .id = 0x01,
  48. .type = HIDS_INPUT,
  49. };
  50. static uint8_t simulate_input;
  51. static uint8_t ctrl_point;
  52. static uint8_t report_map[] = {
  53. 0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
  54. 0x09, 0x02, /* Usage (Mouse) */
  55. 0xA1, 0x01, /* Collection (Application) */
  56. 0x09, 0x01, /* Usage (Pointer) */
  57. 0xA1, 0x00, /* Collection (Physical) */
  58. 0x05, 0x09, /* Usage Page (Button) */
  59. 0x19, 0x01, /* Usage Minimum (0x01) */
  60. 0x29, 0x03, /* Usage Maximum (0x03) */
  61. 0x15, 0x00, /* Logical Minimum (0) */
  62. 0x25, 0x01, /* Logical Maximum (1) */
  63. 0x95, 0x03, /* Report Count (3) */
  64. 0x75, 0x01, /* Report Size (1) */
  65. 0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,...) */
  66. 0x95, 0x01, /* Report Count (1) */
  67. 0x75, 0x05, /* Report Size (5) */
  68. 0x81, 0x03, /* Input (Const,Var,Abs,No Wrap,Linear,...) */
  69. 0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
  70. 0x09, 0x30, /* Usage (X) */
  71. 0x09, 0x31, /* Usage (Y) */
  72. 0x15, 0x81, /* Logical Minimum (129) */
  73. 0x25, 0x7F, /* Logical Maximum (127) */
  74. 0x75, 0x08, /* Report Size (8) */
  75. 0x95, 0x02, /* Report Count (2) */
  76. 0x81, 0x06, /* Input (Data,Var,Rel,No Wrap,Linear,...) */
  77. 0xC0, /* End Collection */
  78. 0xC0, /* End Collection */
  79. };
  80. static ssize_t read_info(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
  81. uint16_t len, uint16_t offset)
  82. {
  83. return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
  84. sizeof(struct hids_info));
  85. }
  86. static ssize_t read_report_map(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
  87. uint16_t len, uint16_t offset)
  88. {
  89. return bt_gatt_attr_read(conn, attr, buf, len, offset, report_map, sizeof(report_map));
  90. }
  91. static ssize_t read_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
  92. uint16_t len, uint16_t offset)
  93. {
  94. return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
  95. sizeof(struct hids_report));
  96. }
  97. static void input_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
  98. {
  99. simulate_input = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
  100. }
  101. static ssize_t read_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
  102. uint16_t len, uint16_t offset)
  103. {
  104. return bt_gatt_attr_read(conn, attr, buf, len, offset, NULL, 0);
  105. }
  106. static ssize_t write_ctrl_point(struct bt_conn *conn, const struct bt_gatt_attr *attr,
  107. const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
  108. {
  109. uint8_t *value = attr->user_data;
  110. if (offset + len > sizeof(ctrl_point))
  111. {
  112. return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
  113. }
  114. memcpy(value + offset, buf, len);
  115. return len;
  116. }
  117. /* HID Service Declaration */
  118. BT_GATT_SERVICE_DEFINE(
  119. hog_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS),
  120. BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_info,
  121. NULL, &info),
  122. BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP, BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
  123. read_report_map, NULL, NULL),
  124. BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
  125. BT_GATT_PERM_READ_AUTHEN, read_input_report, NULL, NULL),
  126. BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN),
  127. BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ, read_report, NULL, &input),
  128. BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
  129. BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point), );
  130. void hog_init(void)
  131. {
  132. }