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