Delta
/
BLE_HIDScanner_DELTA
Example for BLE HID scanner
Fork of BLE_HIDScanner_DELTA by
HIDService.h@1:8bca989a70be, 2017-02-07 (annotated)
- Committer:
- silviaChen
- Date:
- Tue Feb 07 02:51:56 2017 +0000
- Revision:
- 1:8bca989a70be
- Child:
- 2:9f46fa6237dd
BLE HID Keyboard demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
silviaChen | 1:8bca989a70be | 1 | #ifndef __BLE_HID_SERVICE_H__ |
silviaChen | 1:8bca989a70be | 2 | #define __BLE_HID_SERVICE_H__ |
silviaChen | 1:8bca989a70be | 3 | |
silviaChen | 1:8bca989a70be | 4 | #include "BLE.h" |
silviaChen | 1:8bca989a70be | 5 | /** |
silviaChen | 1:8bca989a70be | 6 | * @class Human Interface Device Service |
silviaChen | 1:8bca989a70be | 7 | * @brief BLE Human Interface Device Service. This service displays the Glucose measurement value represented as a 16bit Float format.<br> |
silviaChen | 1:8bca989a70be | 8 | * @Author: Marco.Hsu |
silviaChen | 1:8bca989a70be | 9 | * @Email: marco.missyou@gmail.com |
silviaChen | 1:8bca989a70be | 10 | */ |
silviaChen | 1:8bca989a70be | 11 | |
silviaChen | 1:8bca989a70be | 12 | extern const uint8_t KeyboardReportMap[76]; |
silviaChen | 1:8bca989a70be | 13 | |
silviaChen | 1:8bca989a70be | 14 | class HIDService { |
silviaChen | 1:8bca989a70be | 15 | public: |
silviaChen | 1:8bca989a70be | 16 | HIDService(BLEDevice &_ble, const uint8_t* key = &KeyboardReportMap[0]): |
silviaChen | 1:8bca989a70be | 17 | ble(_ble), |
silviaChen | 1:8bca989a70be | 18 | protocol_modeValue(1), // Report Protocol Mode(1), Boot Protocol Mode(0) |
silviaChen | 1:8bca989a70be | 19 | KeyboardMap(key), |
silviaChen | 1:8bca989a70be | 20 | Protocol_Mode(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocol_modeValue, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
silviaChen | 1:8bca989a70be | 21 | ReportMap(GattCharacteristic::UUID_REPORT_MAP_CHAR, KeyboardMap.getPointer(), 76, sizeof(KeyboardMap), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
silviaChen | 1:8bca989a70be | 22 | Report(GattCharacteristic::UUID_REPORT_CHAR, reportValue.getPointer(), 8, 8, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
silviaChen | 1:8bca989a70be | 23 | HID_Information(GattCharacteristic::UUID_HID_INFORMATION_CHAR, hidInformation.getPointer(), 4, 4, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
silviaChen | 1:8bca989a70be | 24 | HID_Control_Point(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, &hidcontrolPointer, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE) |
silviaChen | 1:8bca989a70be | 25 | { |
silviaChen | 1:8bca989a70be | 26 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
silviaChen | 1:8bca989a70be | 27 | if (serviceAdded) { |
silviaChen | 1:8bca989a70be | 28 | return; |
silviaChen | 1:8bca989a70be | 29 | } |
silviaChen | 1:8bca989a70be | 30 | |
silviaChen | 1:8bca989a70be | 31 | //SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM; |
silviaChen | 1:8bca989a70be | 32 | Protocol_Mode.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
silviaChen | 1:8bca989a70be | 33 | ReportMap.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
silviaChen | 1:8bca989a70be | 34 | Report.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
silviaChen | 1:8bca989a70be | 35 | HID_Information.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
silviaChen | 1:8bca989a70be | 36 | HID_Control_Point.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
silviaChen | 1:8bca989a70be | 37 | GattCharacteristic *charTable[] = {&Protocol_Mode, &ReportMap, &Report, &HID_Information, &HID_Control_Point}; |
silviaChen | 1:8bca989a70be | 38 | GattService HIDGattService(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
silviaChen | 1:8bca989a70be | 39 | ble.addService(HIDGattService); |
silviaChen | 1:8bca989a70be | 40 | serviceAdded = true; |
silviaChen | 1:8bca989a70be | 41 | ble.onDataWritten(this, &HIDService::onDataWritten); |
silviaChen | 1:8bca989a70be | 42 | } |
silviaChen | 1:8bca989a70be | 43 | public: |
silviaChen | 1:8bca989a70be | 44 | void updateReport(uint8_t modifydata, uint8_t data) { |
silviaChen | 1:8bca989a70be | 45 | reportValue.updateReportValue(modifydata, data); |
silviaChen | 1:8bca989a70be | 46 | |
silviaChen | 1:8bca989a70be | 47 | //ble.updateCharacteristicValue(Report.getValueAttribute().getHandle(), reportValue.getPointer(), 8); |
silviaChen | 1:8bca989a70be | 48 | ble.gattServer().write(Report.getValueAttribute().getHandle(), reportValue.getPointer(), 8); |
silviaChen | 1:8bca989a70be | 49 | } |
silviaChen | 1:8bca989a70be | 50 | |
silviaChen | 1:8bca989a70be | 51 | virtual void onDataWritten(const GattWriteCallbackParams *params) { |
silviaChen | 1:8bca989a70be | 52 | if (params->handle == HID_Control_Point.getValueAttribute().getHandle()) { |
silviaChen | 1:8bca989a70be | 53 | uint16_t bytesRead = params->len; |
silviaChen | 1:8bca989a70be | 54 | if (bytesRead == 1) { |
silviaChen | 1:8bca989a70be | 55 | memcpy(&hidcontrolPointer, params->data, bytesRead); |
silviaChen | 1:8bca989a70be | 56 | } |
silviaChen | 1:8bca989a70be | 57 | } |
silviaChen | 1:8bca989a70be | 58 | if (params->handle == Report.getValueAttribute().getHandle()) { |
silviaChen | 1:8bca989a70be | 59 | uint16_t bytesRead = params->len; |
silviaChen | 1:8bca989a70be | 60 | if (bytesRead <= 4) { |
silviaChen | 1:8bca989a70be | 61 | memcpy(&reportValue, params->data, bytesRead); |
silviaChen | 1:8bca989a70be | 62 | } |
silviaChen | 1:8bca989a70be | 63 | } |
silviaChen | 1:8bca989a70be | 64 | } |
silviaChen | 1:8bca989a70be | 65 | |
silviaChen | 1:8bca989a70be | 66 | private: |
silviaChen | 1:8bca989a70be | 67 | struct ReportMapStructure{ |
silviaChen | 1:8bca989a70be | 68 | uint8_t KeyboardMap[76]; |
silviaChen | 1:8bca989a70be | 69 | ReportMapStructure(const uint8_t* data): KeyboardMap() { |
silviaChen | 1:8bca989a70be | 70 | memcpy(&KeyboardMap[0], data, 76); |
silviaChen | 1:8bca989a70be | 71 | } |
silviaChen | 1:8bca989a70be | 72 | uint8_t *getPointer(void) { |
silviaChen | 1:8bca989a70be | 73 | return KeyboardMap; |
silviaChen | 1:8bca989a70be | 74 | } |
silviaChen | 1:8bca989a70be | 75 | }; |
silviaChen | 1:8bca989a70be | 76 | |
silviaChen | 1:8bca989a70be | 77 | private: |
silviaChen | 1:8bca989a70be | 78 | struct ReportStructure { |
silviaChen | 1:8bca989a70be | 79 | // Initial setting report value |
silviaChen | 1:8bca989a70be | 80 | ReportStructure(): reportValue() { |
silviaChen | 1:8bca989a70be | 81 | uint8_t data= 0x00; |
silviaChen | 1:8bca989a70be | 82 | updateReportValue(data, data); |
silviaChen | 1:8bca989a70be | 83 | } |
silviaChen | 1:8bca989a70be | 84 | |
silviaChen | 1:8bca989a70be | 85 | void updateReportValue(uint8_t modifyKey, uint8_t data){ |
silviaChen | 1:8bca989a70be | 86 | memset(&reportValue[0], 0 ,8); |
silviaChen | 1:8bca989a70be | 87 | memcpy(&reportValue[0], &modifyKey, 1); |
silviaChen | 1:8bca989a70be | 88 | memcpy(&reportValue[2], &data, 1); |
silviaChen | 1:8bca989a70be | 89 | } |
silviaChen | 1:8bca989a70be | 90 | |
silviaChen | 1:8bca989a70be | 91 | uint8_t *getPointer(void) { |
silviaChen | 1:8bca989a70be | 92 | return reportValue; |
silviaChen | 1:8bca989a70be | 93 | } |
silviaChen | 1:8bca989a70be | 94 | |
silviaChen | 1:8bca989a70be | 95 | uint8_t reportValue[8]; |
silviaChen | 1:8bca989a70be | 96 | }; |
silviaChen | 1:8bca989a70be | 97 | |
silviaChen | 1:8bca989a70be | 98 | private: |
silviaChen | 1:8bca989a70be | 99 | struct HIDInforStructure{ |
silviaChen | 1:8bca989a70be | 100 | uint16_t bcdHID; |
silviaChen | 1:8bca989a70be | 101 | uint8_t bCountryCode; |
silviaChen | 1:8bca989a70be | 102 | uint8_t Flags; |
silviaChen | 1:8bca989a70be | 103 | |
silviaChen | 1:8bca989a70be | 104 | HIDInforStructure():bcdHID(0),bCountryCode(0),Flags(0){ |
silviaChen | 1:8bca989a70be | 105 | memcpy(&hidInformation[0], &bcdHID, 2); |
silviaChen | 1:8bca989a70be | 106 | memcpy(&hidInformation[2], &bCountryCode, 1); |
silviaChen | 1:8bca989a70be | 107 | memcpy(&hidInformation[3], &Flags, 1); |
silviaChen | 1:8bca989a70be | 108 | } |
silviaChen | 1:8bca989a70be | 109 | uint8_t *getPointer(void) { |
silviaChen | 1:8bca989a70be | 110 | return hidInformation; |
silviaChen | 1:8bca989a70be | 111 | } |
silviaChen | 1:8bca989a70be | 112 | |
silviaChen | 1:8bca989a70be | 113 | uint8_t hidInformation[4]; |
silviaChen | 1:8bca989a70be | 114 | }; |
silviaChen | 1:8bca989a70be | 115 | |
silviaChen | 1:8bca989a70be | 116 | private: |
silviaChen | 1:8bca989a70be | 117 | BLEDevice &ble; |
silviaChen | 1:8bca989a70be | 118 | uint8_t protocol_modeValue; |
silviaChen | 1:8bca989a70be | 119 | ReportStructure reportValue; |
silviaChen | 1:8bca989a70be | 120 | uint8_t hidcontrolPointer; |
silviaChen | 1:8bca989a70be | 121 | ReportMapStructure KeyboardMap; |
silviaChen | 1:8bca989a70be | 122 | HIDInforStructure hidInformation; |
silviaChen | 1:8bca989a70be | 123 | GattCharacteristic Protocol_Mode; |
silviaChen | 1:8bca989a70be | 124 | GattCharacteristic ReportMap; |
silviaChen | 1:8bca989a70be | 125 | GattCharacteristic Report; |
silviaChen | 1:8bca989a70be | 126 | // ReadOnlyGattCharacteristic Boot_Keyboard_Input_Report; |
silviaChen | 1:8bca989a70be | 127 | // ReadWriteGattCharacteristic Boot_Keyboard_Output_Report; |
silviaChen | 1:8bca989a70be | 128 | // ReadOnlyGattCharacteristic Boot_Mouse_Input_Report; |
silviaChen | 1:8bca989a70be | 129 | GattCharacteristic HID_Information; |
silviaChen | 1:8bca989a70be | 130 | GattCharacteristic HID_Control_Point; |
silviaChen | 1:8bca989a70be | 131 | }; |
silviaChen | 1:8bca989a70be | 132 | #endif /* #ifndef __BLE_GLUCOSE_SERVICE_H__*/ |