BLE switch interface using micro:bit with 3 tact switches or 3 Makey Makey sensors
BLE_HID/HIDServiceBase.h@1:9d0e2e5b5d25, 2019-06-06 (annotated)
- Committer:
- masakjm
- Date:
- Thu Jun 06 19:06:06 2019 +0000
- Revision:
- 1:9d0e2e5b5d25
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masakjm | 1:9d0e2e5b5d25 | 1 | /* mbed Microcontroller Library |
masakjm | 1:9d0e2e5b5d25 | 2 | * Copyright (c) 2015 ARM Limited |
masakjm | 1:9d0e2e5b5d25 | 3 | * |
masakjm | 1:9d0e2e5b5d25 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
masakjm | 1:9d0e2e5b5d25 | 5 | * you may not use this file except in compliance with the License. |
masakjm | 1:9d0e2e5b5d25 | 6 | * You may obtain a copy of the License at |
masakjm | 1:9d0e2e5b5d25 | 7 | * |
masakjm | 1:9d0e2e5b5d25 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
masakjm | 1:9d0e2e5b5d25 | 9 | * |
masakjm | 1:9d0e2e5b5d25 | 10 | * Unless required by applicable law or agreed to in writing, software |
masakjm | 1:9d0e2e5b5d25 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
masakjm | 1:9d0e2e5b5d25 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
masakjm | 1:9d0e2e5b5d25 | 13 | * See the License for the specific language governing permissions and |
masakjm | 1:9d0e2e5b5d25 | 14 | * limitations under the License. |
masakjm | 1:9d0e2e5b5d25 | 15 | */ |
masakjm | 1:9d0e2e5b5d25 | 16 | |
masakjm | 1:9d0e2e5b5d25 | 17 | #ifndef HID_SERVICE_BASE_H_ |
masakjm | 1:9d0e2e5b5d25 | 18 | #define HID_SERVICE_BASE_H_ |
masakjm | 1:9d0e2e5b5d25 | 19 | |
masakjm | 1:9d0e2e5b5d25 | 20 | #include "mbed.h" |
masakjm | 1:9d0e2e5b5d25 | 21 | |
masakjm | 1:9d0e2e5b5d25 | 22 | #include "ble/BLE.h" |
masakjm | 1:9d0e2e5b5d25 | 23 | #include "HID_types.h" |
masakjm | 1:9d0e2e5b5d25 | 24 | |
masakjm | 1:9d0e2e5b5d25 | 25 | #define BLE_UUID_DESCRIPTOR_REPORT_REFERENCE 0x2908 |
masakjm | 1:9d0e2e5b5d25 | 26 | |
masakjm | 1:9d0e2e5b5d25 | 27 | typedef const uint8_t report_map_t[]; |
masakjm | 1:9d0e2e5b5d25 | 28 | typedef const uint8_t * report_t; |
masakjm | 1:9d0e2e5b5d25 | 29 | |
masakjm | 1:9d0e2e5b5d25 | 30 | typedef struct { |
masakjm | 1:9d0e2e5b5d25 | 31 | uint16_t bcdHID; |
masakjm | 1:9d0e2e5b5d25 | 32 | uint8_t bCountryCode; |
masakjm | 1:9d0e2e5b5d25 | 33 | uint8_t flags; |
masakjm | 1:9d0e2e5b5d25 | 34 | } HID_information_t; |
masakjm | 1:9d0e2e5b5d25 | 35 | |
masakjm | 1:9d0e2e5b5d25 | 36 | enum ReportType { |
masakjm | 1:9d0e2e5b5d25 | 37 | INPUT_REPORT = 0x1, |
masakjm | 1:9d0e2e5b5d25 | 38 | OUTPUT_REPORT = 0x2, |
masakjm | 1:9d0e2e5b5d25 | 39 | FEATURE_REPORT = 0x3, |
masakjm | 1:9d0e2e5b5d25 | 40 | }; |
masakjm | 1:9d0e2e5b5d25 | 41 | |
masakjm | 1:9d0e2e5b5d25 | 42 | enum ProtocolMode { |
masakjm | 1:9d0e2e5b5d25 | 43 | BOOT_PROTOCOL = 0x0, |
masakjm | 1:9d0e2e5b5d25 | 44 | REPORT_PROTOCOL = 0x1, |
masakjm | 1:9d0e2e5b5d25 | 45 | }; |
masakjm | 1:9d0e2e5b5d25 | 46 | |
masakjm | 1:9d0e2e5b5d25 | 47 | typedef struct { |
masakjm | 1:9d0e2e5b5d25 | 48 | uint8_t ID; |
masakjm | 1:9d0e2e5b5d25 | 49 | uint8_t type; |
masakjm | 1:9d0e2e5b5d25 | 50 | } report_reference_t; |
masakjm | 1:9d0e2e5b5d25 | 51 | |
masakjm | 1:9d0e2e5b5d25 | 52 | |
masakjm | 1:9d0e2e5b5d25 | 53 | class HIDServiceBase { |
masakjm | 1:9d0e2e5b5d25 | 54 | public: |
masakjm | 1:9d0e2e5b5d25 | 55 | /** |
masakjm | 1:9d0e2e5b5d25 | 56 | * Constructor |
masakjm | 1:9d0e2e5b5d25 | 57 | * |
masakjm | 1:9d0e2e5b5d25 | 58 | * @param _ble |
masakjm | 1:9d0e2e5b5d25 | 59 | * BLE object to add this service to |
masakjm | 1:9d0e2e5b5d25 | 60 | * @param reportMap |
masakjm | 1:9d0e2e5b5d25 | 61 | * Byte array representing the input/output report formats. In USB HID jargon, it |
masakjm | 1:9d0e2e5b5d25 | 62 | * is called "HID report descriptor". |
masakjm | 1:9d0e2e5b5d25 | 63 | * @param reportMapLength |
masakjm | 1:9d0e2e5b5d25 | 64 | * Size of the reportMap array |
masakjm | 1:9d0e2e5b5d25 | 65 | * @param outputReportLength |
masakjm | 1:9d0e2e5b5d25 | 66 | * Maximum length of a sent report (up to 64 bytes) (default: 64 bytes) |
masakjm | 1:9d0e2e5b5d25 | 67 | * @param inputReportLength |
masakjm | 1:9d0e2e5b5d25 | 68 | * Maximum length of a received report (up to 64 bytes) (default: 64 bytes) |
masakjm | 1:9d0e2e5b5d25 | 69 | * @param inputReportTickerDelay |
masakjm | 1:9d0e2e5b5d25 | 70 | * Delay between input report notifications, in ms. Acceptable values depend directly on |
masakjm | 1:9d0e2e5b5d25 | 71 | * GAP's connInterval parameter, so it shouldn't be less than 12ms |
masakjm | 1:9d0e2e5b5d25 | 72 | * Preferred GAP connection interval is set after this value, in order to send |
masakjm | 1:9d0e2e5b5d25 | 73 | * notifications as quick as possible: minimum connection interval will be set to |
masakjm | 1:9d0e2e5b5d25 | 74 | * (inputReportTickerDelay / 2) |
masakjm | 1:9d0e2e5b5d25 | 75 | */ |
masakjm | 1:9d0e2e5b5d25 | 76 | HIDServiceBase(BLE &_ble, |
masakjm | 1:9d0e2e5b5d25 | 77 | report_map_t reportMap, |
masakjm | 1:9d0e2e5b5d25 | 78 | uint8_t reportMapLength, |
masakjm | 1:9d0e2e5b5d25 | 79 | report_t inputReport, |
masakjm | 1:9d0e2e5b5d25 | 80 | report_t outputReport, |
masakjm | 1:9d0e2e5b5d25 | 81 | report_t featureReport, |
masakjm | 1:9d0e2e5b5d25 | 82 | uint8_t inputReportLength = 0, |
masakjm | 1:9d0e2e5b5d25 | 83 | uint8_t outputReportLength = 0, |
masakjm | 1:9d0e2e5b5d25 | 84 | uint8_t featureReportLength = 0, |
masakjm | 1:9d0e2e5b5d25 | 85 | uint8_t inputReportTickerDelay = 50); |
masakjm | 1:9d0e2e5b5d25 | 86 | |
masakjm | 1:9d0e2e5b5d25 | 87 | /** |
masakjm | 1:9d0e2e5b5d25 | 88 | * Send Report |
masakjm | 1:9d0e2e5b5d25 | 89 | * |
masakjm | 1:9d0e2e5b5d25 | 90 | * @param report Report to send. Must be of size @ref inputReportLength |
masakjm | 1:9d0e2e5b5d25 | 91 | * @return The write status |
masakjm | 1:9d0e2e5b5d25 | 92 | * |
masakjm | 1:9d0e2e5b5d25 | 93 | * @note Don't call send() directly for multiple reports! Use reportTicker for that, in order |
masakjm | 1:9d0e2e5b5d25 | 94 | * to avoid overloading the BLE stack, and let it handle events between each report. |
masakjm | 1:9d0e2e5b5d25 | 95 | */ |
masakjm | 1:9d0e2e5b5d25 | 96 | virtual ble_error_t send(const report_t report); |
masakjm | 1:9d0e2e5b5d25 | 97 | |
masakjm | 1:9d0e2e5b5d25 | 98 | /** |
masakjm | 1:9d0e2e5b5d25 | 99 | * Read Report |
masakjm | 1:9d0e2e5b5d25 | 100 | * |
masakjm | 1:9d0e2e5b5d25 | 101 | * @param report Report to fill. Must be of size @ref outputReportLength |
masakjm | 1:9d0e2e5b5d25 | 102 | * @return The read status |
masakjm | 1:9d0e2e5b5d25 | 103 | */ |
masakjm | 1:9d0e2e5b5d25 | 104 | virtual ble_error_t read(report_t report); |
masakjm | 1:9d0e2e5b5d25 | 105 | |
masakjm | 1:9d0e2e5b5d25 | 106 | virtual void onConnection(const Gap::ConnectionCallbackParams_t *params); |
masakjm | 1:9d0e2e5b5d25 | 107 | virtual void onDisconnection(const Gap::DisconnectionCallbackParams_t *params); |
masakjm | 1:9d0e2e5b5d25 | 108 | |
masakjm | 1:9d0e2e5b5d25 | 109 | virtual bool isConnected(void) |
masakjm | 1:9d0e2e5b5d25 | 110 | { |
masakjm | 1:9d0e2e5b5d25 | 111 | return connected; |
masakjm | 1:9d0e2e5b5d25 | 112 | } |
masakjm | 1:9d0e2e5b5d25 | 113 | |
masakjm | 1:9d0e2e5b5d25 | 114 | protected: |
masakjm | 1:9d0e2e5b5d25 | 115 | /** |
masakjm | 1:9d0e2e5b5d25 | 116 | * Called by BLE API when data has been successfully sent. |
masakjm | 1:9d0e2e5b5d25 | 117 | * |
masakjm | 1:9d0e2e5b5d25 | 118 | * @param count Number of reports sent |
masakjm | 1:9d0e2e5b5d25 | 119 | * |
masakjm | 1:9d0e2e5b5d25 | 120 | * @note Subclasses can override this to avoid starting the report ticker when there is nothing |
masakjm | 1:9d0e2e5b5d25 | 121 | * to send |
masakjm | 1:9d0e2e5b5d25 | 122 | */ |
masakjm | 1:9d0e2e5b5d25 | 123 | virtual void onDataSent(unsigned count); |
masakjm | 1:9d0e2e5b5d25 | 124 | |
masakjm | 1:9d0e2e5b5d25 | 125 | /** |
masakjm | 1:9d0e2e5b5d25 | 126 | * Start the ticker that sends input reports at regular interval |
masakjm | 1:9d0e2e5b5d25 | 127 | * |
masakjm | 1:9d0e2e5b5d25 | 128 | * @note reportTickerIsActive describes the state of the ticker and can be used by HIDS |
masakjm | 1:9d0e2e5b5d25 | 129 | * implementations. |
masakjm | 1:9d0e2e5b5d25 | 130 | */ |
masakjm | 1:9d0e2e5b5d25 | 131 | virtual void startReportTicker(void); |
masakjm | 1:9d0e2e5b5d25 | 132 | |
masakjm | 1:9d0e2e5b5d25 | 133 | /** |
masakjm | 1:9d0e2e5b5d25 | 134 | * Stop the input report ticker |
masakjm | 1:9d0e2e5b5d25 | 135 | */ |
masakjm | 1:9d0e2e5b5d25 | 136 | virtual void stopReportTicker(void); |
masakjm | 1:9d0e2e5b5d25 | 137 | |
masakjm | 1:9d0e2e5b5d25 | 138 | /** |
masakjm | 1:9d0e2e5b5d25 | 139 | * Called by input report ticker at regular interval (reportTickerDelay). This must be |
masakjm | 1:9d0e2e5b5d25 | 140 | * overriden by HIDS implementations to call the @ref send() with a report, if necessary. |
masakjm | 1:9d0e2e5b5d25 | 141 | */ |
masakjm | 1:9d0e2e5b5d25 | 142 | virtual void sendCallback(void) = 0; |
masakjm | 1:9d0e2e5b5d25 | 143 | |
masakjm | 1:9d0e2e5b5d25 | 144 | /** |
masakjm | 1:9d0e2e5b5d25 | 145 | * Create the Gatt descriptor for a report characteristic |
masakjm | 1:9d0e2e5b5d25 | 146 | */ |
masakjm | 1:9d0e2e5b5d25 | 147 | GattAttribute** inputReportDescriptors(); |
masakjm | 1:9d0e2e5b5d25 | 148 | GattAttribute** outputReportDescriptors(); |
masakjm | 1:9d0e2e5b5d25 | 149 | GattAttribute** featureReportDescriptors(); |
masakjm | 1:9d0e2e5b5d25 | 150 | |
masakjm | 1:9d0e2e5b5d25 | 151 | /** |
masakjm | 1:9d0e2e5b5d25 | 152 | * Create the HID information structure |
masakjm | 1:9d0e2e5b5d25 | 153 | */ |
masakjm | 1:9d0e2e5b5d25 | 154 | HID_information_t* HIDInformation(); |
masakjm | 1:9d0e2e5b5d25 | 155 | |
masakjm | 1:9d0e2e5b5d25 | 156 | protected: |
masakjm | 1:9d0e2e5b5d25 | 157 | BLE &ble; |
masakjm | 1:9d0e2e5b5d25 | 158 | bool connected; |
masakjm | 1:9d0e2e5b5d25 | 159 | |
masakjm | 1:9d0e2e5b5d25 | 160 | int reportMapLength; |
masakjm | 1:9d0e2e5b5d25 | 161 | |
masakjm | 1:9d0e2e5b5d25 | 162 | report_t inputReport; |
masakjm | 1:9d0e2e5b5d25 | 163 | report_t outputReport; |
masakjm | 1:9d0e2e5b5d25 | 164 | report_t featureReport; |
masakjm | 1:9d0e2e5b5d25 | 165 | |
masakjm | 1:9d0e2e5b5d25 | 166 | uint8_t inputReportLength; |
masakjm | 1:9d0e2e5b5d25 | 167 | uint8_t outputReportLength; |
masakjm | 1:9d0e2e5b5d25 | 168 | uint8_t featureReportLength; |
masakjm | 1:9d0e2e5b5d25 | 169 | |
masakjm | 1:9d0e2e5b5d25 | 170 | uint8_t controlPointCommand; |
masakjm | 1:9d0e2e5b5d25 | 171 | uint8_t protocolMode; |
masakjm | 1:9d0e2e5b5d25 | 172 | |
masakjm | 1:9d0e2e5b5d25 | 173 | report_reference_t inputReportReferenceData; |
masakjm | 1:9d0e2e5b5d25 | 174 | report_reference_t outputReportReferenceData; |
masakjm | 1:9d0e2e5b5d25 | 175 | report_reference_t featureReportReferenceData; |
masakjm | 1:9d0e2e5b5d25 | 176 | |
masakjm | 1:9d0e2e5b5d25 | 177 | GattAttribute inputReportReferenceDescriptor; |
masakjm | 1:9d0e2e5b5d25 | 178 | GattAttribute outputReportReferenceDescriptor; |
masakjm | 1:9d0e2e5b5d25 | 179 | GattAttribute featureReportReferenceDescriptor; |
masakjm | 1:9d0e2e5b5d25 | 180 | |
masakjm | 1:9d0e2e5b5d25 | 181 | // Optional gatt characteristics: |
masakjm | 1:9d0e2e5b5d25 | 182 | GattCharacteristic protocolModeCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 183 | |
masakjm | 1:9d0e2e5b5d25 | 184 | // Report characteristics (each sort of optional) |
masakjm | 1:9d0e2e5b5d25 | 185 | GattCharacteristic inputReportCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 186 | GattCharacteristic outputReportCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 187 | GattCharacteristic featureReportCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 188 | |
masakjm | 1:9d0e2e5b5d25 | 189 | // Required gatt characteristics: Report Map, Information, Control Point |
masakjm | 1:9d0e2e5b5d25 | 190 | GattCharacteristic reportMapCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 191 | ReadOnlyGattCharacteristic<HID_information_t> HIDInformationCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 192 | GattCharacteristic HIDControlPointCharacteristic; |
masakjm | 1:9d0e2e5b5d25 | 193 | |
masakjm | 1:9d0e2e5b5d25 | 194 | Ticker reportTicker; |
masakjm | 1:9d0e2e5b5d25 | 195 | uint32_t reportTickerDelay; |
masakjm | 1:9d0e2e5b5d25 | 196 | bool reportTickerIsActive; |
masakjm | 1:9d0e2e5b5d25 | 197 | }; |
masakjm | 1:9d0e2e5b5d25 | 198 | |
masakjm | 1:9d0e2e5b5d25 | 199 | #endif /* !HID_SERVICE_BASE_H_ */ |
masakjm | 1:9d0e2e5b5d25 | 200 |