-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHIDService.h
153 lines (116 loc) · 4.55 KB
/
HIDService.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
#ifndef HID_SERVICE_H
#define HID_SERVICE_H
#include "MicroBitConfig.h"
#if CONFIG_ENABLED(DEVICE_BLE)
#include "MicroBitBLEManager.h"
#include "MicroBitBLEService.h"
#include "EventModel.h"
#include "debug.h"
#include "HIDReporter.h"
#include"peer_manager.h"
/*
| Reporter | Map Size | Report Size |
|:--------:|:--------:|:-----------:|
| Keyboard | 47 | 8 |
| Mouse | 54 | 4 |
| Abs Mouse| 54 | 5 |
| Media | 37 | 1 |
| Gamepad | 64 | 7 |
| | | |
|Total/Max | 249 | 8 |
*/
const int numReportsMax = 4;
const int reportMapMaxSize = 200; // 172 is enough for any 3
const int reportMaxSize = 8; // Max size
const int minTimeBetweenNotifies = 6; // Minimum amount of time between sending notifies
const int maxTimeBetweenNotifies = 200; // Maximum amount of time between sending notifies
const int defaultTimeBetweenNotifies = 10; // Default amount of time between sending notifies
/**
* Class definition for a MicroBit BLE HID Service.
*/
class HIDService : public MicroBitBLEService
{
public:
static HIDService *getInstance();
static void setEventsPerSecond(uint32_t events);
private:
static HIDService *service; // Singleton
friend class HIDReporter;
int addHIDReporter(HIDReporter& reporter);
void setName();
/**
* Constructor.
* Create a representation of the Bluetooth SIG HID Service
* @param _ble The instance of a BLE device that we're running on.
*/
HIDService();
/**
* Invoked when BLE connects.
*/
void onConnect(const microbit_ble_evt_t *p_ble_evt);
/**
* Invoked when BLE disconnects.
*/
void onDisconnect(const microbit_ble_evt_t *p_ble_evt);
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
void onDataWritten(const microbit_ble_evt_write_t *params);
/**
* Callback. Invoked when any of our attributes are read via BLE.
*/
void onDataRead(microbit_onDataRead_t *params);
// BLE Events...Let's monitor 'em all.
bool onBleEvent(const microbit_ble_evt_t *p_ble_evt);
// Override notification process to enforce minimum time between events.
bool notifyChrValue( int idx, const uint8_t *data, uint16_t length);
// Peer Manager Events (re-enable CCCDs)
void pm_events( const pm_evt_t* p_event);
// Static instance variables were created to facilitate multiple HID Services
// (Now a singleton is used and they could be converted to instance variables)
// Peer Manager Events (re-enable CCCDs)
static void static_pm_events( const pm_evt_t* p_event);
// Index for each characteristic in arrays of handles and UUIDs
typedef enum mbbs_cIdx
{
mbbs_cIdxProtocolMode,
mbbs_cIdxHIDInfo,
mbbs_cIdxReportMap,
mbbs_cIdxReport1,
mbbs_cIdxReport2,
mbbs_cIdxReport3,
mbbs_cIdxReport4, // NOTE: Adding Reports requires updating chars array in HIDService.cpp
// and const numReports to be changed
mbbs_cIdxCOUNT
} mbbs_cIdx;
// Service UUID
static const uint16_t hidService;
// UUIDs for our service and characteristics
static const uint16_t charUUID[mbbs_cIdxCOUNT];
static const int EVT_STATUS; // Reporters send it via MicroBitEvent()
// Data for each characteristic when they are held by Soft Device.
MicroBitBLEChar chars[mbbs_cIdxCOUNT];
int characteristicCount() { return mbbs_cIdxCOUNT; };
MicroBitBLEChar *characteristicPtr(int idx) { return &chars[ idx]; };
// HID Info characteristic
// Can't be const (may be modified by stack; should be persistent)
static uint16_t HIDInfo[2];
// Can't be const (may be modified by stack; should be persistent)
uint8_t protocolMode; // 0=>Boot Protocol; 1=>Report; Always 1
uint32_t timeBetweenNotifies; // Actual time between notifications
// Actual service data
uint8_t reportMap[reportMapMaxSize];
unsigned reportMapUsed;
uint8_t reports[numReportsMax][reportMaxSize];
HIDReporter *reporters[numReportsMax];
unsigned numReporters;
char gapName[14];
void addReporter(HIDReporter *reporter);
uint8_t *getReportBuffer(int index) { return reports[index]; }
void addReportDescriptor(uint16_t value_handle, uint8_t reportID, uint8_t reportTypeD);
void advertiseHID();
// Debugging: Print the attribute / info.
void debugAttribute(int index);
};
#endif
#endif