Dependencies:   microbit

Committer:
xx316
Date:
Wed Jun 12 15:21:01 2019 +0000
Revision:
1:032b96b05e51
for demo on Thursday;

Who changed what in which revision?

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