|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <assert.h> |
| 21 | + |
| 22 | +#include "sysinit/sysinit.h" |
| 23 | +#include "syscfg/syscfg.h" |
| 24 | +#include "host/ble_hs.h" |
| 25 | +#include "services/hid/ble_svc_hid.h" |
| 26 | + |
| 27 | + |
| 28 | +uint16_t ble_svc_hid_val_handle; |
| 29 | + |
| 30 | +/* Access function */ |
| 31 | +static int |
| 32 | +ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle, |
| 33 | + struct ble_gatt_access_ctxt *ctxt, void *arg); |
| 34 | + |
| 35 | +/* Service and characteristics definition */ |
| 36 | +/* TODO: Confirm details of characteristic `Report` */ |
| 37 | +static const struct ble_gatt_svc_def ble_svc_hid_defs[] = { |
| 38 | + { |
| 39 | + /*** Service: Tx Power Service. */ |
| 40 | + .type = BLE_GATT_SVC_TYPE_PRIMARY, |
| 41 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_UUID16), |
| 42 | + .characteristics = (struct ble_gatt_chr_def[]) { { |
| 43 | + /*** Characteristic: Protocol Mode. */ |
| 44 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE), |
| 45 | + .access_cb = ble_svc_hid_access, |
| 46 | + .val_handle = &ble_svc_hid_val_handle, |
| 47 | + .flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE_NO_RSP, |
| 48 | + }, { |
| 49 | + /*** Characteristic: Report. */ |
| 50 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT), |
| 51 | + .access_cb = ble_svc_hid_access, |
| 52 | + .val_handle = &ble_svc_hid_val_handle, |
| 53 | + .flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY, |
| 54 | + }, { |
| 55 | + /*** Characteristic: Report Map. */ |
| 56 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_REPORT_MAP), |
| 57 | + .access_cb = ble_svc_hid_access, |
| 58 | + .val_handle = &ble_svc_hid_val_handle, |
| 59 | + .flags = BLE_GATT_CHR_F_READ, |
| 60 | + }, { |
| 61 | + /*** Characteristic: Boot Keyboard Input Report. */ |
| 62 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT), |
| 63 | + .access_cb = ble_svc_hid_access, |
| 64 | + .val_handle = &ble_svc_hid_val_handle, |
| 65 | + .flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY, |
| 66 | + }, { |
| 67 | + /*** Characteristic: Boot Keyboard Output Report. */ |
| 68 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT), |
| 69 | + .access_cb = ble_svc_hid_access, |
| 70 | + .val_handle = &ble_svc_hid_val_handle, |
| 71 | + .flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_WRITE|BLE_GATT_CHR_F_WRITE_NO_RSP, |
| 72 | + }, { |
| 73 | + /*** Characteristic: Boot Mouse Input Report. */ |
| 74 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT), |
| 75 | + .access_cb = ble_svc_hid_access, |
| 76 | + .val_handle = &ble_svc_hid_val_handle, |
| 77 | + .flags = BLE_GATT_CHR_F_READ|BLE_GATT_CHR_F_NOTIFY, |
| 78 | + }, { |
| 79 | + /*** Characteristic: Hid Information. */ |
| 80 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_INFORMATION), |
| 81 | + .access_cb = ble_svc_hid_access, |
| 82 | + .val_handle = &ble_svc_hid_val_handle, |
| 83 | + .flags = BLE_GATT_CHR_F_READ, |
| 84 | + }, { |
| 85 | + /*** Characteristic: Hid Control Point. */ |
| 86 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT), |
| 87 | + .access_cb = ble_svc_hid_access, |
| 88 | + .val_handle = &ble_svc_hid_val_handle, |
| 89 | + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, |
| 90 | + }, { |
| 91 | + 0, /* No more characteristics in this service. */ |
| 92 | + } }, |
| 93 | + }, |
| 94 | + |
| 95 | + { |
| 96 | + 0, /* No more services. */ |
| 97 | + }, |
| 98 | +}; |
| 99 | + |
| 100 | +/* { |
| 101 | + / ** Alert Notification Control Point |
| 102 | + * |
| 103 | + * This characteristic allows the peer device to |
| 104 | + * enable/disable the alert notification of new alert |
| 105 | + * and unread event more selectively than can be done |
| 106 | + * by setting or clearing the notification bit in the |
| 107 | + * Client Characteristic Configuration for each alert |
| 108 | + * characteristic. |
| 109 | + * / |
| 110 | + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT), |
| 111 | + .access_cb = ble_svc_ans_access, |
| 112 | + .flags = BLE_GATT_CHR_F_WRITE, |
| 113 | + } |
| 114 | +*/ |
| 115 | + |
| 116 | +/** |
| 117 | + * HID access function |
| 118 | + */ |
| 119 | +static int |
| 120 | +ble_svc_hid_access(uint16_t conn_handle, uint16_t attr_handle, |
| 121 | + struct ble_gatt_access_ctxt *ctxt, |
| 122 | + void *arg) |
| 123 | +{ |
| 124 | + uint16_t uuid16; |
| 125 | + int rc=0; |
| 126 | + |
| 127 | + /* ANS Control point command and catagory variables */ |
| 128 | + //uint8_t cmd_id; |
| 129 | + //int i; |
| 130 | + |
| 131 | + uuid16 = ble_uuid_u16(ctxt->chr->uuid); |
| 132 | + assert(uuid16 != 0); |
| 133 | + |
| 134 | + switch (uuid16) { |
| 135 | + case BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE: |
| 136 | + if (ctxt->op == BLE_GATT_CHR_F_READ) { |
| 137 | + } else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) { |
| 138 | + } |
| 139 | + return rc; |
| 140 | + |
| 141 | + case BLE_SVC_HID_CHR_UUID16_REPORT: |
| 142 | + if (ctxt->op == BLE_GATT_CHR_F_READ) { |
| 143 | + } else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) { |
| 144 | + } |
| 145 | + return rc; |
| 146 | + |
| 147 | + case BLE_SVC_HID_CHR_UUID16_REPORT_MAP: |
| 148 | + assert(ctxt->op == BLE_GATT_CHR_F_READ); |
| 149 | + return rc; |
| 150 | + |
| 151 | + case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_INPUT_REPORT: |
| 152 | + if (ctxt->op == BLE_GATT_CHR_F_READ) { |
| 153 | + } else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) { |
| 154 | + } |
| 155 | + return rc; |
| 156 | + |
| 157 | + case BLE_SVC_HID_CHR_UUID16_BOOT_KEYBOARD_OUTPUT_REPORT: |
| 158 | + if (ctxt->op == BLE_GATT_CHR_F_READ) { |
| 159 | + } else if (ctxt->op == BLE_GATT_CHR_F_WRITE) { |
| 160 | + } else if (ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP) { |
| 161 | + } |
| 162 | + return rc; |
| 163 | + |
| 164 | + case BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INPUT_REPORT: |
| 165 | + if (ctxt->op == BLE_GATT_CHR_F_READ) { |
| 166 | + } else if (ctxt->op == BLE_GATT_CHR_F_NOTIFY) { |
| 167 | + } |
| 168 | + return rc; |
| 169 | + |
| 170 | + case BLE_SVC_HID_CHR_UUID16_HID_INFORMATION: |
| 171 | + assert(ctxt->op == BLE_GATT_CHR_F_READ); |
| 172 | + return rc; |
| 173 | + |
| 174 | + case BLE_SVC_HID_CHR_UUID16_HID_CONTROL_POINT: |
| 175 | + assert(ctxt->op == BLE_GATT_CHR_F_WRITE_NO_RSP); |
| 176 | + return rc; |
| 177 | + |
| 178 | + default: |
| 179 | + assert(0); |
| 180 | + return BLE_ATT_ERR_UNLIKELY; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * Initialize the HID service |
| 186 | + */ |
| 187 | +void |
| 188 | +ble_svc_hid_init(void) |
| 189 | +{ |
| 190 | + int rc; |
| 191 | + |
| 192 | + /* Ensure this function only gets called by sysinit. */ |
| 193 | + SYSINIT_ASSERT_ACTIVE(); |
| 194 | + |
| 195 | + rc = ble_gatts_count_cfg(ble_svc_hid_defs); |
| 196 | + SYSINIT_PANIC_ASSERT(rc == 0); |
| 197 | + |
| 198 | + rc = ble_gatts_add_svcs(ble_svc_hid_defs); |
| 199 | + SYSINIT_PANIC_ASSERT(rc == 0); |
| 200 | +} |
0 commit comments