ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Thu Sep 15 09:31:05 2016 +0900
Revision:
86:e0fab77e669d
Parent:
83:2e940d154f8b
support consumer keys

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 #ifndef HID_SERVICE_BASE_H_
cho45 54:899fc2b0a76b 18 #define HID_SERVICE_BASE_H_
cho45 54:899fc2b0a76b 19
cho45 54:899fc2b0a76b 20 #include "mbed.h"
cho45 54:899fc2b0a76b 21
cho45 54:899fc2b0a76b 22 #include "ble/BLE.h"
cho45 54:899fc2b0a76b 23 #include "USBHID_Types.h"
cho45 54:899fc2b0a76b 24
cho45 54:899fc2b0a76b 25 #define BLE_UUID_DESCRIPTOR_REPORT_REFERENCE 0x2908
cho45 54:899fc2b0a76b 26 #define BLE_UUID_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE 0x2907
cho45 54:899fc2b0a76b 27
cho45 54:899fc2b0a76b 28 typedef const uint8_t report_map_t[];
cho45 54:899fc2b0a76b 29 typedef const uint8_t * report_t;
cho45 54:899fc2b0a76b 30
cho45 54:899fc2b0a76b 31 typedef struct {
cho45 83:2e940d154f8b 32 uint16_t bcdHID;
cho45 83:2e940d154f8b 33 uint8_t bCountryCode;
cho45 83:2e940d154f8b 34 uint8_t flags;
cho45 54:899fc2b0a76b 35 } HID_information_t;
cho45 54:899fc2b0a76b 36
cho45 54:899fc2b0a76b 37 enum ReportType {
cho45 83:2e940d154f8b 38 INPUT_REPORT = 0x1,
cho45 83:2e940d154f8b 39 OUTPUT_REPORT = 0x2,
cho45 83:2e940d154f8b 40 FEATURE_REPORT = 0x3,
cho45 54:899fc2b0a76b 41 };
cho45 54:899fc2b0a76b 42
cho45 54:899fc2b0a76b 43 enum ProtocolMode {
cho45 83:2e940d154f8b 44 BOOT_PROTOCOL = 0x0,
cho45 83:2e940d154f8b 45 REPORT_PROTOCOL = 0x1,
cho45 54:899fc2b0a76b 46 };
cho45 54:899fc2b0a76b 47
cho45 54:899fc2b0a76b 48 typedef struct {
cho45 83:2e940d154f8b 49 uint8_t ID;
cho45 83:2e940d154f8b 50 uint8_t type;
cho45 54:899fc2b0a76b 51 } report_reference_t;
cho45 54:899fc2b0a76b 52
cho45 54:899fc2b0a76b 53
cho45 54:899fc2b0a76b 54 class HIDServiceBase {
cho45 54:899fc2b0a76b 55 public:
cho45 83:2e940d154f8b 56 /**
cho45 83:2e940d154f8b 57 * Constructor
cho45 83:2e940d154f8b 58 *
cho45 83:2e940d154f8b 59 * @param _ble
cho45 83:2e940d154f8b 60 * BLE object to add this service to
cho45 83:2e940d154f8b 61 * @param reportMap
cho45 83:2e940d154f8b 62 * Byte array representing the input/output report formats. In USB HID jargon, it
cho45 83:2e940d154f8b 63 * is called "HID report descriptor".
cho45 83:2e940d154f8b 64 * @param reportMapLength
cho45 83:2e940d154f8b 65 * Size of the reportMap array
cho45 83:2e940d154f8b 66 * @param outputReportLength
cho45 83:2e940d154f8b 67 * Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
cho45 83:2e940d154f8b 68 * @param inputReportLength
cho45 83:2e940d154f8b 69 * Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
cho45 83:2e940d154f8b 70 */
cho45 83:2e940d154f8b 71 HIDServiceBase(
cho45 83:2e940d154f8b 72 BLE &_ble,
cho45 83:2e940d154f8b 73 report_map_t reportMap,
cho45 83:2e940d154f8b 74 uint8_t reportMapLength,
cho45 83:2e940d154f8b 75 report_t inputReport,
cho45 83:2e940d154f8b 76 report_t outputReport,
cho45 83:2e940d154f8b 77 report_t featureReport,
cho45 83:2e940d154f8b 78 uint8_t inputReportLength = 0,
cho45 83:2e940d154f8b 79 uint8_t outputReportLength = 0,
cho45 83:2e940d154f8b 80 uint8_t featureReportLength = 0
cho45 83:2e940d154f8b 81 );
cho45 54:899fc2b0a76b 82
cho45 83:2e940d154f8b 83 /**
cho45 83:2e940d154f8b 84 * Send Report
cho45 83:2e940d154f8b 85 *
cho45 83:2e940d154f8b 86 * @param report Report to send. Must be of size @ref inputReportLength
cho45 83:2e940d154f8b 87 * @return The write status
cho45 83:2e940d154f8b 88 *
cho45 83:2e940d154f8b 89 * @note Don't call send() directly for multiple reports! Use reportTicker for that, in order
cho45 83:2e940d154f8b 90 * to avoid overloading the BLE stack, and let it handle events between each report.
cho45 83:2e940d154f8b 91 */
cho45 83:2e940d154f8b 92 virtual ble_error_t send(const report_t report);
cho45 54:899fc2b0a76b 93
cho45 83:2e940d154f8b 94 /**
cho45 83:2e940d154f8b 95 * Read Report
cho45 83:2e940d154f8b 96 *
cho45 83:2e940d154f8b 97 * @param report Report to fill. Must be of size @ref outputReportLength
cho45 83:2e940d154f8b 98 * @return The read status
cho45 83:2e940d154f8b 99 */
cho45 83:2e940d154f8b 100 virtual ble_error_t read(report_t report);
cho45 54:899fc2b0a76b 101
cho45 83:2e940d154f8b 102 virtual void onConnection(const Gap::ConnectionCallbackParams_t *params);
cho45 83:2e940d154f8b 103 virtual void onDisconnection(const Gap::DisconnectionCallbackParams_t *params);
cho45 54:899fc2b0a76b 104
cho45 83:2e940d154f8b 105 virtual bool isConnected(void) {
cho45 83:2e940d154f8b 106 return connected;
cho45 83:2e940d154f8b 107 }
cho45 54:899fc2b0a76b 108
cho45 79:0095bfb18c57 109 virtual void init(void);
cho45 83:2e940d154f8b 110
cho45 54:899fc2b0a76b 111 protected:
cho45 83:2e940d154f8b 112 /**
cho45 83:2e940d154f8b 113 * Called by BLE API when data has been successfully sent.
cho45 83:2e940d154f8b 114 *
cho45 83:2e940d154f8b 115 * @param count Number of reports sent
cho45 83:2e940d154f8b 116 *
cho45 83:2e940d154f8b 117 * @note Subclasses can override this to avoid starting the report ticker when there is nothing
cho45 83:2e940d154f8b 118 * to send
cho45 83:2e940d154f8b 119 */
cho45 83:2e940d154f8b 120 virtual void onDataSent(unsigned count);
cho45 54:899fc2b0a76b 121
cho45 79:0095bfb18c57 122 /**
cho45 83:2e940d154f8b 123 */
cho45 83:2e940d154f8b 124 virtual void onDataWritten(const GattWriteCallbackParams *params);
cho45 79:0095bfb18c57 125
cho45 79:0095bfb18c57 126 virtual void addExtraCharacteristics(GattCharacteristic** characteristics, uint8_t& charIndex);
cho45 79:0095bfb18c57 127
cho45 54:899fc2b0a76b 128 protected:
cho45 83:2e940d154f8b 129 BLE &ble;
cho45 83:2e940d154f8b 130 bool connected;
cho45 54:899fc2b0a76b 131
cho45 83:2e940d154f8b 132 int reportMapLength;
cho45 54:899fc2b0a76b 133
cho45 83:2e940d154f8b 134 report_t inputReport;
cho45 83:2e940d154f8b 135 report_t outputReport;
cho45 83:2e940d154f8b 136 report_t featureReport;
cho45 54:899fc2b0a76b 137
cho45 83:2e940d154f8b 138 uint8_t inputReportLength;
cho45 83:2e940d154f8b 139 uint8_t outputReportLength;
cho45 83:2e940d154f8b 140 uint8_t featureReportLength;
cho45 54:899fc2b0a76b 141
cho45 83:2e940d154f8b 142 uint8_t controlPointCommand;
cho45 83:2e940d154f8b 143 uint8_t protocolMode;
cho45 54:899fc2b0a76b 144
cho45 83:2e940d154f8b 145 // Optional gatt characteristics:
cho45 83:2e940d154f8b 146 GattCharacteristic protocolModeCharacteristic;
cho45 54:899fc2b0a76b 147
cho45 83:2e940d154f8b 148 // Report characteristics (each sort of optional)
cho45 83:2e940d154f8b 149 GattCharacteristic inputReportCharacteristic;
cho45 83:2e940d154f8b 150 GattCharacteristic outputReportCharacteristic;
cho45 83:2e940d154f8b 151 GattCharacteristic featureReportCharacteristic;
cho45 54:899fc2b0a76b 152
cho45 83:2e940d154f8b 153 // Required gatt characteristics: Report Map, Information, Control Point
cho45 83:2e940d154f8b 154 GattCharacteristic reportMapCharacteristic;
cho45 83:2e940d154f8b 155 ReadOnlyGattCharacteristic<HID_information_t> HIDInformationCharacteristic;
cho45 83:2e940d154f8b 156 GattCharacteristic HIDControlPointCharacteristic;
cho45 54:899fc2b0a76b 157 };
cho45 54:899fc2b0a76b 158
cho45 75:351d7ffe81d1 159 #endif /* !HID_SERVICE_BASE_H_ */