ble nano hid over gatt
Dependencies: BLE_API mbed-dev nRF51822
Diff: HIDServiceBase.cpp
- Revision:
- 83:2e940d154f8b
- Parent:
- 82:af52d37b1946
- Child:
- 85:e526a89a0674
--- a/HIDServiceBase.cpp Sat Sep 03 23:16:07 2016 +0900 +++ b/HIDServiceBase.cpp Sun Sep 04 01:12:45 2016 +0900 @@ -21,85 +21,104 @@ static const report_reference_t inputReportReferenceData = { 0, INPUT_REPORT }; static const GattAttribute inputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&inputReportReferenceData, 2, 2, false); static const GattAttribute * inputReportDescriptors[] = { - &inputReportReferenceDescriptor, + &inputReportReferenceDescriptor, }; static const report_reference_t outputReportReferenceData = { 0, OUTPUT_REPORT }; static const GattAttribute outputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&outputReportReferenceData, 2, 2, false); static const GattAttribute * outputReportDescriptors[] = { - &outputReportReferenceDescriptor, + &outputReportReferenceDescriptor, }; - + static const report_reference_t featureReportReferenceData = { 0, FEATURE_REPORT }; static const GattAttribute featureReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE, (uint8_t *)&featureReportReferenceData, 2, 2, false); static const GattAttribute * featureReportDescriptors[] = { - &featureReportReferenceDescriptor, + &featureReportReferenceDescriptor, }; static const HID_information_t HID_information = {HID_VERSION_1_11, 0x00, 0x03}; -HIDServiceBase::HIDServiceBase(BLE &_ble, - report_map_t reportMap, - uint8_t reportMapSize, - report_t inputReport, - report_t outputReport, - report_t featureReport, - uint8_t inputReportLength, - uint8_t outputReportLength, - uint8_t featureReportLength) : - ble(_ble), - connected (false), - reportMapLength(reportMapSize), +HIDServiceBase::HIDServiceBase( + BLE &_ble, + report_map_t reportMap, + uint8_t reportMapSize, + report_t inputReport, + report_t outputReport, + report_t featureReport, + uint8_t inputReportLength, + uint8_t outputReportLength, + uint8_t featureReportLength +) : + + ble(_ble), + connected (false), + reportMapLength(reportMapSize), - inputReport(inputReport), - outputReport(outputReport), - featureReport(featureReport), + inputReport(inputReport), + outputReport(outputReport), + featureReport(featureReport), + + inputReportLength(inputReportLength), + outputReportLength(outputReportLength), + featureReportLength(featureReportLength), - inputReportLength(inputReportLength), - outputReportLength(outputReportLength), - featureReportLength(featureReportLength), + protocolMode(REPORT_PROTOCOL), - protocolMode(REPORT_PROTOCOL), - - protocolModeCharacteristic(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocolMode, 1, 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE), + protocolModeCharacteristic( + GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocolMode, 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE + ), - inputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR, - (uint8_t *)inputReport, inputReportLength, inputReportLength, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, - const_cast<GattAttribute**>(inputReportDescriptors), 1), + inputReportCharacteristic( + GattCharacteristic::UUID_REPORT_CHAR, + (uint8_t *)inputReport, inputReportLength, inputReportLength, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, + const_cast<GattAttribute**>(inputReportDescriptors), 1 + ), - outputReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR, - (uint8_t *)outputReport, outputReportLength, outputReportLength, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, - const_cast<GattAttribute**>(outputReportDescriptors), 1), + outputReportCharacteristic( + GattCharacteristic::UUID_REPORT_CHAR, + (uint8_t *)outputReport, outputReportLength, outputReportLength, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, + const_cast<GattAttribute**>(outputReportDescriptors), 1 + ), - featureReportCharacteristic(GattCharacteristic::UUID_REPORT_CHAR, - (uint8_t *)featureReport, featureReportLength, featureReportLength, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ - | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, - const_cast<GattAttribute**>(featureReportDescriptors), 1), + featureReportCharacteristic( + GattCharacteristic::UUID_REPORT_CHAR, + (uint8_t *)featureReport, featureReportLength, featureReportLength, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ + | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, + const_cast<GattAttribute**>(featureReportDescriptors), 1 + ), - /* - * We need to set reportMap content as const, in order to let the compiler put it into flash - * instead of RAM. The characteristic is read-only so it won't be written, but - * GattCharacteristic constructor takes non-const arguments only. Hence the cast. - */ - reportMapCharacteristic(GattCharacteristic::UUID_REPORT_MAP_CHAR, - const_cast<uint8_t*>(reportMap), reportMapLength, reportMapLength, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ, - NULL, 0), + /* + * We need to set reportMap content as const, in order to let the compiler put it into flash + * instead of RAM. The characteristic is read-only so it won't be written, but + * GattCharacteristic constructor takes non-const arguments only. Hence the cast. + */ + reportMapCharacteristic( + GattCharacteristic::UUID_REPORT_MAP_CHAR, + const_cast<uint8_t*>(reportMap), reportMapLength, reportMapLength, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ, + NULL, 0 + ), - HIDInformationCharacteristic(GattCharacteristic::UUID_HID_INFORMATION_CHAR, const_cast<HID_information_t*>(&HID_information)), - HIDControlPointCharacteristic(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, - &controlPointCommand, 1, 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) + HIDInformationCharacteristic( + GattCharacteristic::UUID_HID_INFORMATION_CHAR, + const_cast<HID_information_t*>(&HID_information) + ), + + HIDControlPointCharacteristic( + GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, + &controlPointCommand, 1, 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE + ) { } @@ -159,22 +178,22 @@ } ble_error_t HIDServiceBase::send(const report_t report) { - return ble.gattServer().write(inputReportCharacteristic.getValueHandle(), - report, - inputReportLength); + return ble.gattServer().write( + inputReportCharacteristic.getValueHandle(), + report, + inputReportLength + ); } ble_error_t HIDServiceBase::read(report_t report) { - // TODO. For the time being, we'll just have HID input reports... - return BLE_ERROR_NOT_IMPLEMENTED; + // TODO. For the time being, we'll just have HID input reports... + return BLE_ERROR_NOT_IMPLEMENTED; } -void HIDServiceBase::onConnection(const Gap::ConnectionCallbackParams_t *params) -{ - this->connected = true; +void HIDServiceBase::onConnection(const Gap::ConnectionCallbackParams_t *params) { + this->connected = true; } -void HIDServiceBase::onDisconnection(const Gap::DisconnectionCallbackParams_t *params) -{ - this->connected = false; +void HIDServiceBase::onDisconnection(const Gap::DisconnectionCallbackParams_t *params) { + this->connected = false; }