ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Sat Sep 03 23:16:07 2016 +0900
Revision:
82:af52d37b1946
Parent:
79:0095bfb18c57
Child:
83:2e940d154f8b
?? Ticker ???????????

Who changed what in which revision?

UserRevisionLine numberNew 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 75:351d7ffe81d1 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 75:351d7ffe81d1 30 &outputReportReferenceDescriptor,
cho45 75:351d7ffe81d1 31 };
cho45 75:351d7ffe81d1 32
cho45 75:351d7ffe81d1 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 75:351d7ffe81d1 37 &featureReportReferenceDescriptor,
cho45 75:351d7ffe81d1 38 };
cho45 75:351d7ffe81d1 39
cho45 75:351d7ffe81d1 40 static const HID_information_t HID_information = {HID_VERSION_1_11, 0x00, 0x03};
cho45 75:351d7ffe81d1 41
cho45 54:899fc2b0a76b 42 HIDServiceBase::HIDServiceBase(BLE &_ble,
cho45 54:899fc2b0a76b 43 report_map_t reportMap,
cho45 54:899fc2b0a76b 44 uint8_t reportMapSize,
cho45 54:899fc2b0a76b 45 report_t inputReport,
cho45 54:899fc2b0a76b 46 report_t outputReport,
cho45 54:899fc2b0a76b 47 report_t featureReport,
cho45 54:899fc2b0a76b 48 uint8_t inputReportLength,
cho45 54:899fc2b0a76b 49 uint8_t outputReportLength,
cho45 82:af52d37b1946 50 uint8_t featureReportLength) :
cho45 54:899fc2b0a76b 51 ble(_ble),
cho45 54:899fc2b0a76b 52 connected (false),
cho45 54:899fc2b0a76b 53 reportMapLength(reportMapSize),
cho45 54:899fc2b0a76b 54
cho45 54:899fc2b0a76b 55 inputReport(inputReport),
cho45 54:899fc2b0a76b 56 outputReport(outputReport),
cho45 54:899fc2b0a76b 57 featureReport(featureReport),
cho45 54:899fc2b0a76b 58
cho45 54:899fc2b0a76b 59 inputReportLength(inputReportLength),
cho45 54:899fc2b0a76b 60 outputReportLength(outputReportLength),
cho45 54:899fc2b0a76b 61 featureReportLength(featureReportLength),
cho45 54:899fc2b0a76b 62
cho45 54:899fc2b0a76b 63 protocolMode(REPORT_PROTOCOL),
cho45 54:899fc2b0a76b 64
cho45 54:899fc2b0a76b 65 protocolModeCharacteristic(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocolMode, 1, 1,
cho45 54:899fc2b0a76b 66 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
cho45 54:899fc2b0a76b 67 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
cho45 54:899fc2b0a76b 68
cho45 54:899fc2b0a76b 69 inputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
cho45 54:899fc2b0a76b 70 (uint8_t *)inputReport, inputReportLength, inputReportLength,
cho45 54:899fc2b0a76b 71 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
cho45 54:899fc2b0a76b 72 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
cho45 54:899fc2b0a76b 73 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
cho45 75:351d7ffe81d1 74 const_cast<GattAttribute**>(inputReportDescriptors), 1),
cho45 54:899fc2b0a76b 75
cho45 54:899fc2b0a76b 76 outputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
cho45 54:899fc2b0a76b 77 (uint8_t *)outputReport, outputReportLength, outputReportLength,
cho45 54:899fc2b0a76b 78 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
cho45 54:899fc2b0a76b 79 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE
cho45 54:899fc2b0a76b 80 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
cho45 75:351d7ffe81d1 81 const_cast<GattAttribute**>(outputReportDescriptors), 1),
cho45 54:899fc2b0a76b 82
cho45 54:899fc2b0a76b 83 featureReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR,
cho45 54:899fc2b0a76b 84 (uint8_t *)featureReport, featureReportLength, featureReportLength,
cho45 54:899fc2b0a76b 85 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
cho45 54:899fc2b0a76b 86 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE,
cho45 75:351d7ffe81d1 87 const_cast<GattAttribute**>(featureReportDescriptors), 1),
cho45 54:899fc2b0a76b 88
cho45 54:899fc2b0a76b 89 /*
cho45 54:899fc2b0a76b 90 * We need to set reportMap content as const, in order to let the compiler put it into flash
cho45 54:899fc2b0a76b 91 * instead of RAM. The characteristic is read-only so it won't be written, but
cho45 54:899fc2b0a76b 92 * GattCharacteristic constructor takes non-const arguments only. Hence the cast.
cho45 54:899fc2b0a76b 93 */
cho45 54:899fc2b0a76b 94 reportMapCharacteristic(GattCharacteristic::UUID_REPORT_MAP_CHAR,
cho45 54:899fc2b0a76b 95 const_cast<uint8_t*>(reportMap), reportMapLength, reportMapLength,
cho45 54:899fc2b0a76b 96 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ,
cho45 59:2d6c0bff2151 97 NULL, 0),
cho45 54:899fc2b0a76b 98
cho45 75:351d7ffe81d1 99 HIDInformationCharacteristic(GattCharacteristic::UUID_HID_INFORMATION_CHAR, const_cast<HID_information_t*>(&HID_information)),
cho45 54:899fc2b0a76b 100 HIDControlPointCharacteristic(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR,
cho45 54:899fc2b0a76b 101 &controlPointCommand, 1, 1,
cho45 82:af52d37b1946 102 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE)
cho45 54:899fc2b0a76b 103 {
cho45 79:0095bfb18c57 104 }
cho45 54:899fc2b0a76b 105
cho45 79:0095bfb18c57 106 void HIDServiceBase::init(void) {
cho45 79:0095bfb18c57 107 static GattCharacteristic *characteristics[] = {
cho45 79:0095bfb18c57 108 &HIDInformationCharacteristic,
cho45 79:0095bfb18c57 109 &reportMapCharacteristic,
cho45 79:0095bfb18c57 110 &protocolModeCharacteristic,
cho45 79:0095bfb18c57 111 &HIDControlPointCharacteristic,
cho45 79:0095bfb18c57 112 NULL,
cho45 79:0095bfb18c57 113 NULL,
cho45 79:0095bfb18c57 114 NULL,
cho45 79:0095bfb18c57 115 NULL,
cho45 79:0095bfb18c57 116 NULL
cho45 79:0095bfb18c57 117 };
cho45 54:899fc2b0a76b 118
cho45 79:0095bfb18c57 119 uint8_t charIndex = 4;
cho45 79:0095bfb18c57 120 /*
cho45 79:0095bfb18c57 121 * Report characteristics are optional, and depend on the reportMap descriptor
cho45 79:0095bfb18c57 122 * Note: at least one should be present, but we don't check that at the moment.
cho45 79:0095bfb18c57 123 */
cho45 79:0095bfb18c57 124 if (inputReportLength)
cho45 79:0095bfb18c57 125 characteristics[charIndex++] = &inputReportCharacteristic;
cho45 79:0095bfb18c57 126 if (outputReportLength)
cho45 79:0095bfb18c57 127 characteristics[charIndex++] = &outputReportCharacteristic;
cho45 79:0095bfb18c57 128 if (featureReportLength)
cho45 79:0095bfb18c57 129 characteristics[charIndex++] = &featureReportCharacteristic;
cho45 79:0095bfb18c57 130
cho45 79:0095bfb18c57 131 addExtraCharacteristics(characteristics, charIndex);
cho45 54:899fc2b0a76b 132
cho45 79:0095bfb18c57 133 DEBUG_PRINTF_BLE("new GattService %d\r\n", charIndex);
cho45 79:0095bfb18c57 134 GattService service(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, characteristics, charIndex);
cho45 54:899fc2b0a76b 135
cho45 79:0095bfb18c57 136 ble.gattServer().addService(service);
cho45 54:899fc2b0a76b 137
cho45 79:0095bfb18c57 138 ble.gap().onConnection(this, &HIDServiceBase::onConnection);
cho45 79:0095bfb18c57 139 ble.gap().onDisconnection(this, &HIDServiceBase::onDisconnection);
cho45 54:899fc2b0a76b 140
cho45 79:0095bfb18c57 141 ble.gattServer().onDataSent(this, &HIDServiceBase::onDataSent);
cho45 79:0095bfb18c57 142 ble.gattServer().onDataWritten(this, &HIDServiceBase::onDataWritten);
cho45 54:899fc2b0a76b 143
cho45 79:0095bfb18c57 144 SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
cho45 79:0095bfb18c57 145 protocolModeCharacteristic.requireSecurity(securityMode);
cho45 79:0095bfb18c57 146 reportMapCharacteristic.requireSecurity(securityMode);
cho45 79:0095bfb18c57 147 inputReportCharacteristic.requireSecurity(securityMode);
cho45 79:0095bfb18c57 148 outputReportCharacteristic.requireSecurity(securityMode);
cho45 79:0095bfb18c57 149 featureReportCharacteristic.requireSecurity(securityMode);
cho45 54:899fc2b0a76b 150 }
cho45 54:899fc2b0a76b 151
cho45 54:899fc2b0a76b 152 void HIDServiceBase::onDataSent(unsigned count) {
cho45 54:899fc2b0a76b 153 }
cho45 54:899fc2b0a76b 154
cho45 79:0095bfb18c57 155 void HIDServiceBase::onDataWritten(const GattWriteCallbackParams *params) {
cho45 79:0095bfb18c57 156 }
cho45 79:0095bfb18c57 157
cho45 79:0095bfb18c57 158 void HIDServiceBase::addExtraCharacteristics(GattCharacteristic** characteristics, uint8_t& charIndex) {
cho45 79:0095bfb18c57 159 }
cho45 54:899fc2b0a76b 160
cho45 54:899fc2b0a76b 161 ble_error_t HIDServiceBase::send(const report_t report) {
cho45 54:899fc2b0a76b 162 return ble.gattServer().write(inputReportCharacteristic.getValueHandle(),
cho45 54:899fc2b0a76b 163 report,
cho45 54:899fc2b0a76b 164 inputReportLength);
cho45 54:899fc2b0a76b 165 }
cho45 54:899fc2b0a76b 166
cho45 54:899fc2b0a76b 167 ble_error_t HIDServiceBase::read(report_t report) {
cho45 54:899fc2b0a76b 168 // TODO. For the time being, we'll just have HID input reports...
cho45 54:899fc2b0a76b 169 return BLE_ERROR_NOT_IMPLEMENTED;
cho45 54:899fc2b0a76b 170 }
cho45 54:899fc2b0a76b 171
cho45 54:899fc2b0a76b 172 void HIDServiceBase::onConnection(const Gap::ConnectionCallbackParams_t *params)
cho45 54:899fc2b0a76b 173 {
cho45 54:899fc2b0a76b 174 this->connected = true;
cho45 54:899fc2b0a76b 175 }
cho45 54:899fc2b0a76b 176
cho45 54:899fc2b0a76b 177 void HIDServiceBase::onDisconnection(const Gap::DisconnectionCallbackParams_t *params)
cho45 54:899fc2b0a76b 178 {
cho45 54:899fc2b0a76b 179 this->connected = false;
cho45 75:351d7ffe81d1 180 }