ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
HIDServiceBase.cpp@85:e526a89a0674, 2016-09-15 (annotated)
- Committer:
- cho45
- Date:
- Thu Sep 15 08:48:57 2016 +0900
- Revision:
- 85:e526a89a0674
- Parent:
- 83:2e940d154f8b
- Child:
- 86:e0fab77e669d
Add ScanParametersService
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cho45 | 54:899fc2b0a76b | 1 | /* mbed Microcontroller Library |
cho45 | 54:899fc2b0a76b | 2 | * Copyright (c) 2015 ARM Limited |
cho45 | 54:899fc2b0a76b | 3 | * |
cho45 | 54:899fc2b0a76b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
cho45 | 54:899fc2b0a76b | 5 | * you may not use this file except in compliance with the License. |
cho45 | 54:899fc2b0a76b | 6 | * You may obtain a copy of the License at |
cho45 | 54:899fc2b0a76b | 7 | * |
cho45 | 54:899fc2b0a76b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
cho45 | 54:899fc2b0a76b | 9 | * |
cho45 | 54:899fc2b0a76b | 10 | * Unless required by applicable law or agreed to in writing, software |
cho45 | 54:899fc2b0a76b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
cho45 | 54:899fc2b0a76b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
cho45 | 54:899fc2b0a76b | 13 | * See the License for the specific language governing permissions and |
cho45 | 54:899fc2b0a76b | 14 | * limitations under the License. |
cho45 | 54:899fc2b0a76b | 15 | */ |
cho45 | 54:899fc2b0a76b | 16 | |
cho45 | 54:899fc2b0a76b | 17 | #include "mbed.h" |
cho45 |
79:0095bfb18c57 | 18 | #include "config.h" |
cho45 | 54:899fc2b0a76b | 19 | #include "HIDServiceBase.h" |
cho45 | 54:899fc2b0a76b | 20 | |
cho45 |
75:351d7ffe81d1 | 21 | static const report_reference_t inputReportReferenceData = { 0, INPUT_REPORT }; |
cho45 |
75:351d7ffe81d1 | 22 | static const GattAttribute inputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&inputReportReferenceData, 2, 2, false); |
cho45 |
75:351d7ffe81d1 | 23 | static const GattAttribute * inputReportDescriptors[] = { |
cho45 |
83:2e940d154f8b | 24 | &inputReportReferenceDescriptor, |
cho45 |
75:351d7ffe81d1 | 25 | }; |
cho45 |
75:351d7ffe81d1 | 26 | |
cho45 |
75:351d7ffe81d1 | 27 | static const report_reference_t outputReportReferenceData = { 0, OUTPUT_REPORT }; |
cho45 |
75:351d7ffe81d1 | 28 | static const GattAttribute outputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&outputReportReferenceData, 2, 2, false); |
cho45 |
75:351d7ffe81d1 | 29 | static const GattAttribute * outputReportDescriptors[] = { |
cho45 |
83:2e940d154f8b | 30 | &outputReportReferenceDescriptor, |
cho45 |
75:351d7ffe81d1 | 31 | }; |
cho45 |
75:351d7ffe81d1 | 32 | |
cho45 |
83:2e940d154f8b | 33 | |
cho45 |
75:351d7ffe81d1 | 34 | static const report_reference_t featureReportReferenceData = { 0, FEATURE_REPORT }; |
cho45 |
75:351d7ffe81d1 | 35 | static const GattAttribute featureReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&featureReportReferenceData, 2, 2, false); |
cho45 |
75:351d7ffe81d1 | 36 | static const GattAttribute * featureReportDescriptors[] = { |
cho45 |
83:2e940d154f8b | 37 | &featureReportReferenceDescriptor, |
cho45 |
75:351d7ffe81d1 | 38 | }; |
cho45 |
75:351d7ffe81d1 | 39 | |
cho45 |
85:e526a89a0674 | 40 | // static const uint16_t reportMapExternalReportReferenceData = GattCharacteristic::UUID_BATTERY_LEVEL_CHAR; |
cho45 |
85:e526a89a0674 | 41 | static const uint8_t reportMapExternalReportReferenceData[] = { 0x2A, 0x19 }; |
cho45 |
85:e526a89a0674 | 42 | static const GattAttribute reportMapExternalReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE, (uint8_t *)&reportMapExternalReportReferenceData, 2, 2, false); |
cho45 |
85:e526a89a0674 | 43 | static const GattAttribute * reportMapDescriptors[] = { |
cho45 |
85:e526a89a0674 | 44 | &reportMapExternalReportReferenceDescriptor, |
cho45 |
85:e526a89a0674 | 45 | }; |
cho45 |
85:e526a89a0674 | 46 | |
cho45 |
85:e526a89a0674 | 47 | #define HID_REMOTE_WAKE_Pos 0 |
cho45 |
85:e526a89a0674 | 48 | #define HID_NORMALLY_CONNECTABLE_Pos 1 |
cho45 |
85:e526a89a0674 | 49 | static const HID_information_t HID_information = { |
cho45 |
85:e526a89a0674 | 50 | HID_VERSION_1_11, |
cho45 |
85:e526a89a0674 | 51 | 0x00, |
cho45 |
85:e526a89a0674 | 52 | (1<<HID_REMOTE_WAKE_Pos) | |
cho45 |
85:e526a89a0674 | 53 | (0<<HID_NORMALLY_CONNECTABLE_Pos) |
cho45 |
85:e526a89a0674 | 54 | }; |
cho45 |
75:351d7ffe81d1 | 55 | |
cho45 |
83:2e940d154f8b | 56 | HIDServiceBase::HIDServiceBase( |
cho45 |
83:2e940d154f8b | 57 | BLE &_ble, |
cho45 |
83:2e940d154f8b | 58 | report_map_t reportMap, |
cho45 |
83:2e940d154f8b | 59 | uint8_t reportMapSize, |
cho45 |
83:2e940d154f8b | 60 | report_t inputReport, |
cho45 |
83:2e940d154f8b | 61 | report_t outputReport, |
cho45 |
83:2e940d154f8b | 62 | report_t featureReport, |
cho45 |
83:2e940d154f8b | 63 | uint8_t inputReportLength, |
cho45 |
83:2e940d154f8b | 64 | uint8_t outputReportLength, |
cho45 |
83:2e940d154f8b | 65 | uint8_t featureReportLength |
cho45 |
83:2e940d154f8b | 66 | ) : |
cho45 |
83:2e940d154f8b | 67 | |
cho45 |
83:2e940d154f8b | 68 | ble(_ble), |
cho45 |
83:2e940d154f8b | 69 | connected (false), |
cho45 |
83:2e940d154f8b | 70 | reportMapLength(reportMapSize), |
cho45 | 54:899fc2b0a76b | 71 | |
cho45 |
83:2e940d154f8b | 72 | inputReport(inputReport), |
cho45 |
83:2e940d154f8b | 73 | outputReport(outputReport), |
cho45 |
83:2e940d154f8b | 74 | featureReport(featureReport), |
cho45 |
83:2e940d154f8b | 75 | |
cho45 |
83:2e940d154f8b | 76 | inputReportLength(inputReportLength), |
cho45 |
83:2e940d154f8b | 77 | outputReportLength(outputReportLength), |
cho45 |
83:2e940d154f8b | 78 | featureReportLength(featureReportLength), |
cho45 | 54:899fc2b0a76b | 79 | |
cho45 |
83:2e940d154f8b | 80 | protocolMode(REPORT_PROTOCOL), |
cho45 | 54:899fc2b0a76b | 81 | |
cho45 |
83:2e940d154f8b | 82 | protocolModeCharacteristic( |
cho45 |
83:2e940d154f8b | 83 | GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocolMode, 1, 1, |
cho45 |
83:2e940d154f8b | 84 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
cho45 |
83:2e940d154f8b | 85 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
cho45 |
83:2e940d154f8b | 86 | ), |
cho45 | 54:899fc2b0a76b | 87 | |
cho45 |
83:2e940d154f8b | 88 | inputReportCharacteristic( |
cho45 |
83:2e940d154f8b | 89 | GattCharacteristic::UUID_REPORT_CHAR, |
cho45 |
83:2e940d154f8b | 90 | (uint8_t *)inputReport, inputReportLength, inputReportLength, |
cho45 |
83:2e940d154f8b | 91 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
cho45 |
83:2e940d154f8b | 92 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
cho45 |
83:2e940d154f8b | 93 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, |
cho45 |
83:2e940d154f8b | 94 | const_cast<GattAttribute**>(inputReportDescriptors), 1 |
cho45 |
83:2e940d154f8b | 95 | ), |
cho45 | 54:899fc2b0a76b | 96 | |
cho45 |
83:2e940d154f8b | 97 | outputReportCharacteristic( |
cho45 |
83:2e940d154f8b | 98 | GattCharacteristic::UUID_REPORT_CHAR, |
cho45 |
83:2e940d154f8b | 99 | (uint8_t *)outputReport, outputReportLength, outputReportLength, |
cho45 |
83:2e940d154f8b | 100 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
cho45 |
83:2e940d154f8b | 101 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
cho45 |
83:2e940d154f8b | 102 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, |
cho45 |
83:2e940d154f8b | 103 | const_cast<GattAttribute**>(outputReportDescriptors), 1 |
cho45 |
83:2e940d154f8b | 104 | ), |
cho45 | 54:899fc2b0a76b | 105 | |
cho45 |
83:2e940d154f8b | 106 | featureReportCharacteristic( |
cho45 |
83:2e940d154f8b | 107 | GattCharacteristic::UUID_REPORT_CHAR, |
cho45 |
83:2e940d154f8b | 108 | (uint8_t *)featureReport, featureReportLength, featureReportLength, |
cho45 |
83:2e940d154f8b | 109 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
cho45 |
83:2e940d154f8b | 110 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, |
cho45 |
83:2e940d154f8b | 111 | const_cast<GattAttribute**>(featureReportDescriptors), 1 |
cho45 |
83:2e940d154f8b | 112 | ), |
cho45 | 54:899fc2b0a76b | 113 | |
cho45 |
83:2e940d154f8b | 114 | /* |
cho45 |
83:2e940d154f8b | 115 | * We need to set reportMap content as const, in order to let the compiler put it into flash |
cho45 |
83:2e940d154f8b | 116 | * instead of RAM. The characteristic is read-only so it won't be written, but |
cho45 |
83:2e940d154f8b | 117 | * GattCharacteristic constructor takes non-const arguments only. Hence the cast. |
cho45 |
83:2e940d154f8b | 118 | */ |
cho45 |
83:2e940d154f8b | 119 | reportMapCharacteristic( |
cho45 |
83:2e940d154f8b | 120 | GattCharacteristic::UUID_REPORT_MAP_CHAR, |
cho45 |
83:2e940d154f8b | 121 | const_cast<uint8_t*>(reportMap), reportMapLength, reportMapLength, |
cho45 |
83:2e940d154f8b | 122 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ, |
cho45 |
85:e526a89a0674 | 123 | const_cast<GattAttribute**>(reportMapDescriptors), 1 |
cho45 |
83:2e940d154f8b | 124 | ), |
cho45 | 54:899fc2b0a76b | 125 | |
cho45 |
83:2e940d154f8b | 126 | HIDInformationCharacteristic( |
cho45 |
83:2e940d154f8b | 127 | GattCharacteristic::UUID_HID_INFORMATION_CHAR, |
cho45 |
83:2e940d154f8b | 128 | const_cast<HID_information_t*>(&HID_information) |
cho45 |
83:2e940d154f8b | 129 | ), |
cho45 |
83:2e940d154f8b | 130 | |
cho45 |
83:2e940d154f8b | 131 | HIDControlPointCharacteristic( |
cho45 |
83:2e940d154f8b | 132 | GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, |
cho45 |
83:2e940d154f8b | 133 | &controlPointCommand, 1, 1, |
cho45 |
83:2e940d154f8b | 134 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE |
cho45 |
83:2e940d154f8b | 135 | ) |
cho45 | 54:899fc2b0a76b | 136 | { |
cho45 |
79:0095bfb18c57 | 137 | } |
cho45 | 54:899fc2b0a76b | 138 | |
cho45 |
79:0095bfb18c57 | 139 | void HIDServiceBase::init(void) { |
cho45 |
79:0095bfb18c57 | 140 | static GattCharacteristic *characteristics[] = { |
cho45 |
79:0095bfb18c57 | 141 | &reportMapCharacteristic, |
cho45 |
79:0095bfb18c57 | 142 | &protocolModeCharacteristic, |
cho45 |
79:0095bfb18c57 | 143 | &HIDControlPointCharacteristic, |
cho45 |
85:e526a89a0674 | 144 | &HIDInformationCharacteristic, |
cho45 |
79:0095bfb18c57 | 145 | NULL, |
cho45 |
79:0095bfb18c57 | 146 | NULL, |
cho45 |
79:0095bfb18c57 | 147 | NULL, |
cho45 |
79:0095bfb18c57 | 148 | NULL, |
cho45 |
79:0095bfb18c57 | 149 | NULL |
cho45 |
79:0095bfb18c57 | 150 | }; |
cho45 | 54:899fc2b0a76b | 151 | |
cho45 |
79:0095bfb18c57 | 152 | uint8_t charIndex = 4; |
cho45 |
79:0095bfb18c57 | 153 | /* |
cho45 |
79:0095bfb18c57 | 154 | * Report characteristics are optional, and depend on the reportMap descriptor |
cho45 |
79:0095bfb18c57 | 155 | * Note: at least one should be present, but we don't check that at the moment. |
cho45 |
79:0095bfb18c57 | 156 | */ |
cho45 |
79:0095bfb18c57 | 157 | if (inputReportLength) |
cho45 |
79:0095bfb18c57 | 158 | characteristics[charIndex++] = &inputReportCharacteristic; |
cho45 |
79:0095bfb18c57 | 159 | if (outputReportLength) |
cho45 |
79:0095bfb18c57 | 160 | characteristics[charIndex++] = &outputReportCharacteristic; |
cho45 |
79:0095bfb18c57 | 161 | if (featureReportLength) |
cho45 |
79:0095bfb18c57 | 162 | characteristics[charIndex++] = &featureReportCharacteristic; |
cho45 |
79:0095bfb18c57 | 163 | |
cho45 |
79:0095bfb18c57 | 164 | addExtraCharacteristics(characteristics, charIndex); |
cho45 | 54:899fc2b0a76b | 165 | |
cho45 |
79:0095bfb18c57 | 166 | DEBUG_PRINTF_BLE("new GattService %d\r\n", charIndex); |
cho45 |
79:0095bfb18c57 | 167 | GattService service(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, characteristics, charIndex); |
cho45 | 54:899fc2b0a76b | 168 | |
cho45 |
79:0095bfb18c57 | 169 | ble.gattServer().addService(service); |
cho45 | 54:899fc2b0a76b | 170 | |
cho45 |
79:0095bfb18c57 | 171 | ble.gap().onConnection(this, &HIDServiceBase::onConnection); |
cho45 |
79:0095bfb18c57 | 172 | ble.gap().onDisconnection(this, &HIDServiceBase::onDisconnection); |
cho45 | 54:899fc2b0a76b | 173 | |
cho45 |
79:0095bfb18c57 | 174 | ble.gattServer().onDataSent(this, &HIDServiceBase::onDataSent); |
cho45 |
79:0095bfb18c57 | 175 | ble.gattServer().onDataWritten(this, &HIDServiceBase::onDataWritten); |
cho45 | 54:899fc2b0a76b | 176 | |
cho45 |
79:0095bfb18c57 | 177 | SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM; |
cho45 |
79:0095bfb18c57 | 178 | protocolModeCharacteristic.requireSecurity(securityMode); |
cho45 |
79:0095bfb18c57 | 179 | reportMapCharacteristic.requireSecurity(securityMode); |
cho45 |
79:0095bfb18c57 | 180 | inputReportCharacteristic.requireSecurity(securityMode); |
cho45 |
79:0095bfb18c57 | 181 | outputReportCharacteristic.requireSecurity(securityMode); |
cho45 |
79:0095bfb18c57 | 182 | featureReportCharacteristic.requireSecurity(securityMode); |
cho45 | 54:899fc2b0a76b | 183 | } |
cho45 | 54:899fc2b0a76b | 184 | |
cho45 | 54:899fc2b0a76b | 185 | void HIDServiceBase::onDataSent(unsigned count) { |
cho45 | 54:899fc2b0a76b | 186 | } |
cho45 | 54:899fc2b0a76b | 187 | |
cho45 |
79:0095bfb18c57 | 188 | void HIDServiceBase::onDataWritten(const GattWriteCallbackParams *params) { |
cho45 |
79:0095bfb18c57 | 189 | } |
cho45 |
79:0095bfb18c57 | 190 | |
cho45 |
79:0095bfb18c57 | 191 | void HIDServiceBase::addExtraCharacteristics(GattCharacteristic** characteristics, uint8_t& charIndex) { |
cho45 |
79:0095bfb18c57 | 192 | } |
cho45 | 54:899fc2b0a76b | 193 | |
cho45 | 54:899fc2b0a76b | 194 | ble_error_t HIDServiceBase::send(const report_t report) { |
cho45 |
83:2e940d154f8b | 195 | return ble.gattServer().write( |
cho45 |
83:2e940d154f8b | 196 | inputReportCharacteristic.getValueHandle(), |
cho45 |
83:2e940d154f8b | 197 | report, |
cho45 |
83:2e940d154f8b | 198 | inputReportLength |
cho45 |
83:2e940d154f8b | 199 | ); |
cho45 | 54:899fc2b0a76b | 200 | } |
cho45 | 54:899fc2b0a76b | 201 | |
cho45 | 54:899fc2b0a76b | 202 | ble_error_t HIDServiceBase::read(report_t report) { |
cho45 |
83:2e940d154f8b | 203 | // TODO. For the time being, we'll just have HID input reports... |
cho45 |
83:2e940d154f8b | 204 | return BLE_ERROR_NOT_IMPLEMENTED; |
cho45 | 54:899fc2b0a76b | 205 | } |
cho45 | 54:899fc2b0a76b | 206 | |
cho45 |
83:2e940d154f8b | 207 | void HIDServiceBase::onConnection(const Gap::ConnectionCallbackParams_t *params) { |
cho45 |
83:2e940d154f8b | 208 | this->connected = true; |
cho45 | 54:899fc2b0a76b | 209 | } |
cho45 | 54:899fc2b0a76b | 210 | |
cho45 |
83:2e940d154f8b | 211 | void HIDServiceBase::onDisconnection(const Gap::DisconnectionCallbackParams_t *params) { |
cho45 |
83:2e940d154f8b | 212 | this->connected = false; |
cho45 |
75:351d7ffe81d1 | 213 | } |