Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Sat Sep 03 20:36:40 2016 +0900
Revision:
79:0095bfb18c57
Parent:
75:351d7ffe81d1
Child:
82:af52d37b1946
implement boot keyboard interface

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 54:899fc2b0a76b 32 uint16_t bcdHID;
cho45 54:899fc2b0a76b 33 uint8_t bCountryCode;
cho45 54:899fc2b0a76b 34 uint8_t flags;
cho45 54:899fc2b0a76b 35 } HID_information_t;
cho45 54:899fc2b0a76b 36
cho45 54:899fc2b0a76b 37 enum ReportType {
cho45 54:899fc2b0a76b 38 INPUT_REPORT = 0x1,
cho45 54:899fc2b0a76b 39 OUTPUT_REPORT = 0x2,
cho45 54:899fc2b0a76b 40 FEATURE_REPORT = 0x3,
cho45 54:899fc2b0a76b 41 };
cho45 54:899fc2b0a76b 42
cho45 54:899fc2b0a76b 43 enum ProtocolMode {
cho45 54:899fc2b0a76b 44 BOOT_PROTOCOL = 0x0,
cho45 54:899fc2b0a76b 45 REPORT_PROTOCOL = 0x1,
cho45 54:899fc2b0a76b 46 };
cho45 54:899fc2b0a76b 47
cho45 54:899fc2b0a76b 48 typedef struct {
cho45 54:899fc2b0a76b 49 uint8_t ID;
cho45 54:899fc2b0a76b 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 54:899fc2b0a76b 56 /**
cho45 54:899fc2b0a76b 57 * Constructor
cho45 54:899fc2b0a76b 58 *
cho45 54:899fc2b0a76b 59 * @param _ble
cho45 54:899fc2b0a76b 60 * BLE object to add this service to
cho45 54:899fc2b0a76b 61 * @param reportMap
cho45 54:899fc2b0a76b 62 * Byte array representing the input/output report formats. In USB HID jargon, it
cho45 54:899fc2b0a76b 63 * is called "HID report descriptor".
cho45 54:899fc2b0a76b 64 * @param reportMapLength
cho45 54:899fc2b0a76b 65 * Size of the reportMap array
cho45 54:899fc2b0a76b 66 * @param outputReportLength
cho45 54:899fc2b0a76b 67 * Maximum length of a sent report (up to 64 bytes) (default: 64 bytes)
cho45 54:899fc2b0a76b 68 * @param inputReportLength
cho45 54:899fc2b0a76b 69 * Maximum length of a received report (up to 64 bytes) (default: 64 bytes)
cho45 54:899fc2b0a76b 70 * @param inputReportTickerDelay
cho45 54:899fc2b0a76b 71 * Delay between input report notifications, in ms. Acceptable values depend directly on
cho45 54:899fc2b0a76b 72 * GAP's connInterval parameter, so it shouldn't be less than 12ms
cho45 54:899fc2b0a76b 73 * Preferred GAP connection interval is set after this value, in order to send
cho45 54:899fc2b0a76b 74 * notifications as quick as possible: minimum connection interval will be set to
cho45 54:899fc2b0a76b 75 * (inputReportTickerDelay / 2)
cho45 54:899fc2b0a76b 76 */
cho45 54:899fc2b0a76b 77 HIDServiceBase(BLE &_ble,
cho45 54:899fc2b0a76b 78 report_map_t reportMap,
cho45 54:899fc2b0a76b 79 uint8_t reportMapLength,
cho45 54:899fc2b0a76b 80 report_t inputReport,
cho45 54:899fc2b0a76b 81 report_t outputReport,
cho45 54:899fc2b0a76b 82 report_t featureReport,
cho45 54:899fc2b0a76b 83 uint8_t inputReportLength = 0,
cho45 54:899fc2b0a76b 84 uint8_t outputReportLength = 0,
cho45 54:899fc2b0a76b 85 uint8_t featureReportLength = 0,
cho45 54:899fc2b0a76b 86 uint8_t inputReportTickerDelay = 50);
cho45 54:899fc2b0a76b 87
cho45 54:899fc2b0a76b 88 /**
cho45 54:899fc2b0a76b 89 * Send Report
cho45 54:899fc2b0a76b 90 *
cho45 54:899fc2b0a76b 91 * @param report Report to send. Must be of size @ref inputReportLength
cho45 54:899fc2b0a76b 92 * @return The write status
cho45 54:899fc2b0a76b 93 *
cho45 54:899fc2b0a76b 94 * @note Don't call send() directly for multiple reports! Use reportTicker for that, in order
cho45 54:899fc2b0a76b 95 * to avoid overloading the BLE stack, and let it handle events between each report.
cho45 54:899fc2b0a76b 96 */
cho45 54:899fc2b0a76b 97 virtual ble_error_t send(const report_t report);
cho45 54:899fc2b0a76b 98
cho45 54:899fc2b0a76b 99 /**
cho45 54:899fc2b0a76b 100 * Read Report
cho45 54:899fc2b0a76b 101 *
cho45 54:899fc2b0a76b 102 * @param report Report to fill. Must be of size @ref outputReportLength
cho45 54:899fc2b0a76b 103 * @return The read status
cho45 54:899fc2b0a76b 104 */
cho45 54:899fc2b0a76b 105 virtual ble_error_t read(report_t report);
cho45 54:899fc2b0a76b 106
cho45 54:899fc2b0a76b 107 virtual void onConnection(const Gap::ConnectionCallbackParams_t *params);
cho45 54:899fc2b0a76b 108 virtual void onDisconnection(const Gap::DisconnectionCallbackParams_t *params);
cho45 54:899fc2b0a76b 109
cho45 54:899fc2b0a76b 110 virtual bool isConnected(void)
cho45 54:899fc2b0a76b 111 {
cho45 54:899fc2b0a76b 112 return connected;
cho45 54:899fc2b0a76b 113 }
cho45 54:899fc2b0a76b 114
cho45 79:0095bfb18c57 115 virtual void init(void);
cho45 54:899fc2b0a76b 116 protected:
cho45 79:0095bfb18c57 117
cho45 54:899fc2b0a76b 118 /**
cho45 54:899fc2b0a76b 119 * Called by BLE API when data has been successfully sent.
cho45 54:899fc2b0a76b 120 *
cho45 54:899fc2b0a76b 121 * @param count Number of reports sent
cho45 54:899fc2b0a76b 122 *
cho45 54:899fc2b0a76b 123 * @note Subclasses can override this to avoid starting the report ticker when there is nothing
cho45 54:899fc2b0a76b 124 * to send
cho45 54:899fc2b0a76b 125 */
cho45 54:899fc2b0a76b 126 virtual void onDataSent(unsigned count);
cho45 54:899fc2b0a76b 127
cho45 79:0095bfb18c57 128 /**
cho45 79:0095bfb18c57 129 */
cho45 79:0095bfb18c57 130 virtual void onDataWritten(const GattWriteCallbackParams *params);
cho45 79:0095bfb18c57 131
cho45 54:899fc2b0a76b 132 /**
cho45 54:899fc2b0a76b 133 * Start the ticker that sends input reports at regular interval
cho45 54:899fc2b0a76b 134 *
cho45 54:899fc2b0a76b 135 * @note reportTickerIsActive describes the state of the ticker and can be used by HIDS
cho45 54:899fc2b0a76b 136 * implementations.
cho45 54:899fc2b0a76b 137 */
cho45 54:899fc2b0a76b 138 virtual void startReportTicker(void);
cho45 54:899fc2b0a76b 139
cho45 54:899fc2b0a76b 140 /**
cho45 54:899fc2b0a76b 141 * Stop the input report ticker
cho45 54:899fc2b0a76b 142 */
cho45 54:899fc2b0a76b 143 virtual void stopReportTicker(void);
cho45 54:899fc2b0a76b 144
cho45 54:899fc2b0a76b 145 /**
cho45 54:899fc2b0a76b 146 * Called by input report ticker at regular interval (reportTickerDelay). This must be
cho45 54:899fc2b0a76b 147 * overriden by HIDS implementations to call the @ref send() with a report, if necessary.
cho45 54:899fc2b0a76b 148 */
cho45 54:899fc2b0a76b 149 virtual void sendCallback(void) = 0;
cho45 54:899fc2b0a76b 150
cho45 79:0095bfb18c57 151 virtual void addExtraCharacteristics(GattCharacteristic** characteristics, uint8_t& charIndex);
cho45 79:0095bfb18c57 152
cho45 54:899fc2b0a76b 153 protected:
cho45 54:899fc2b0a76b 154 BLE &ble;
cho45 54:899fc2b0a76b 155 bool connected;
cho45 54:899fc2b0a76b 156
cho45 54:899fc2b0a76b 157 int reportMapLength;
cho45 54:899fc2b0a76b 158
cho45 54:899fc2b0a76b 159 report_t inputReport;
cho45 54:899fc2b0a76b 160 report_t outputReport;
cho45 54:899fc2b0a76b 161 report_t featureReport;
cho45 54:899fc2b0a76b 162
cho45 54:899fc2b0a76b 163 uint8_t inputReportLength;
cho45 54:899fc2b0a76b 164 uint8_t outputReportLength;
cho45 54:899fc2b0a76b 165 uint8_t featureReportLength;
cho45 54:899fc2b0a76b 166
cho45 54:899fc2b0a76b 167 uint8_t controlPointCommand;
cho45 54:899fc2b0a76b 168 uint8_t protocolMode;
cho45 54:899fc2b0a76b 169
cho45 54:899fc2b0a76b 170 // Optional gatt characteristics:
cho45 54:899fc2b0a76b 171 GattCharacteristic protocolModeCharacteristic;
cho45 54:899fc2b0a76b 172
cho45 54:899fc2b0a76b 173 // Report characteristics (each sort of optional)
cho45 54:899fc2b0a76b 174 GattCharacteristic inputReportCharacteristic;
cho45 54:899fc2b0a76b 175 GattCharacteristic outputReportCharacteristic;
cho45 54:899fc2b0a76b 176 GattCharacteristic featureReportCharacteristic;
cho45 54:899fc2b0a76b 177
cho45 54:899fc2b0a76b 178 // Required gatt characteristics: Report Map, Information, Control Point
cho45 54:899fc2b0a76b 179 GattCharacteristic reportMapCharacteristic;
cho45 54:899fc2b0a76b 180 ReadOnlyGattCharacteristic<HID_information_t> HIDInformationCharacteristic;
cho45 54:899fc2b0a76b 181 GattCharacteristic HIDControlPointCharacteristic;
cho45 54:899fc2b0a76b 182
cho45 54:899fc2b0a76b 183 Ticker reportTicker;
cho45 54:899fc2b0a76b 184 uint32_t reportTickerDelay;
cho45 54:899fc2b0a76b 185 bool reportTickerIsActive;
cho45 54:899fc2b0a76b 186 };
cho45 54:899fc2b0a76b 187
cho45 75:351d7ffe81d1 188 #endif /* !HID_SERVICE_BASE_H_ */