back up of work during May 2019
Dependencies: microbit
BLE_HID/HIDDeviceInformationService.h@6:f372773ad32f, 2019-06-05 (annotated)
- Committer:
- tht216
- Date:
- Wed Jun 05 15:21:14 2019 +0000
- Branch:
- class_implmentation
- Revision:
- 6:f372773ad32f
- Parent:
- 1:c840c2b6f490
TODO:; 1. multi keypresses; 2. integration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xx316 | 1:c840c2b6f490 | 1 | /* mbed Microcontroller Library |
xx316 | 1:c840c2b6f490 | 2 | * Copyright (c) 2006-2013 ARM Limited |
xx316 | 1:c840c2b6f490 | 3 | * |
xx316 | 1:c840c2b6f490 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
xx316 | 1:c840c2b6f490 | 5 | * you may not use this file except in compliance with the License. |
xx316 | 1:c840c2b6f490 | 6 | * You may obtain a copy of the License at |
xx316 | 1:c840c2b6f490 | 7 | * |
xx316 | 1:c840c2b6f490 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
xx316 | 1:c840c2b6f490 | 9 | * |
xx316 | 1:c840c2b6f490 | 10 | * Unless required by applicable law or agreed to in writing, software |
xx316 | 1:c840c2b6f490 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
xx316 | 1:c840c2b6f490 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
xx316 | 1:c840c2b6f490 | 13 | * See the License for the specific language governing permissions and |
xx316 | 1:c840c2b6f490 | 14 | * limitations under the License. |
xx316 | 1:c840c2b6f490 | 15 | */ |
xx316 | 1:c840c2b6f490 | 16 | |
xx316 | 1:c840c2b6f490 | 17 | #ifndef __BLE_HID_DEVICE_INFORMATION_SERVICE_H__ |
xx316 | 1:c840c2b6f490 | 18 | #define __BLE_HID_DEVICE_INFORMATION_SERVICE_H__ |
xx316 | 1:c840c2b6f490 | 19 | |
xx316 | 1:c840c2b6f490 | 20 | #include "ble/BLE.h" |
xx316 | 1:c840c2b6f490 | 21 | |
xx316 | 1:c840c2b6f490 | 22 | #include "HID_types.h" |
xx316 | 1:c840c2b6f490 | 23 | |
xx316 | 1:c840c2b6f490 | 24 | /** |
xx316 | 1:c840c2b6f490 | 25 | * @class HIDDeviceInformationService |
xx316 | 1:c840c2b6f490 | 26 | * @brief BLE Device Information Service <br> |
xx316 | 1:c840c2b6f490 | 27 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml <br> |
xx316 | 1:c840c2b6f490 | 28 | * Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml |
xx316 | 1:c840c2b6f490 | 29 | */ |
xx316 | 1:c840c2b6f490 | 30 | class HIDDeviceInformationService { |
xx316 | 1:c840c2b6f490 | 31 | public: |
xx316 | 1:c840c2b6f490 | 32 | /** |
xx316 | 1:c840c2b6f490 | 33 | * @brief Device Information Service Constructor. |
xx316 | 1:c840c2b6f490 | 34 | * |
xx316 | 1:c840c2b6f490 | 35 | * @param[ref] _ble |
xx316 | 1:c840c2b6f490 | 36 | * BLE object for the underlying controller. |
xx316 | 1:c840c2b6f490 | 37 | * @param[in] manufacturersName |
xx316 | 1:c840c2b6f490 | 38 | * This characteristic represents the name of the |
xx316 | 1:c840c2b6f490 | 39 | * manufacturer of the device. The name is copied into the |
xx316 | 1:c840c2b6f490 | 40 | * BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 41 | * @param[in] modelNumber |
xx316 | 1:c840c2b6f490 | 42 | * This characteristic represents the model number that is |
xx316 | 1:c840c2b6f490 | 43 | * assigned by the device vendor. The value is copied into |
xx316 | 1:c840c2b6f490 | 44 | * the BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 45 | * @param[in] serialNumber |
xx316 | 1:c840c2b6f490 | 46 | * This characteristic represents the serial number for a |
xx316 | 1:c840c2b6f490 | 47 | * particular instance of the device. The value is copied |
xx316 | 1:c840c2b6f490 | 48 | * into the BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 49 | * @param[in] hardwareRevision |
xx316 | 1:c840c2b6f490 | 50 | * This characteristic represents the hardware revision for |
xx316 | 1:c840c2b6f490 | 51 | * the hardware within the device. The value is copied |
xx316 | 1:c840c2b6f490 | 52 | * into the BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 53 | * @param[in] firmwareRevision |
xx316 | 1:c840c2b6f490 | 54 | * This characteristic represents the firmware revision for |
xx316 | 1:c840c2b6f490 | 55 | * the firmware within the device. The value is copied |
xx316 | 1:c840c2b6f490 | 56 | * into the BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 57 | * @param[in] softwareRevision |
xx316 | 1:c840c2b6f490 | 58 | * This characteristic represents the software revision for |
xx316 | 1:c840c2b6f490 | 59 | * the software within the device. The value is copied |
xx316 | 1:c840c2b6f490 | 60 | * into the BLE stack during this constructor. |
xx316 | 1:c840c2b6f490 | 61 | * @param[in] pnpID |
xx316 | 1:c840c2b6f490 | 62 | * This characteristic represents HID-specific information, |
xx316 | 1:c840c2b6f490 | 63 | * such as vendor id, product id and version. |
xx316 | 1:c840c2b6f490 | 64 | */ |
xx316 | 1:c840c2b6f490 | 65 | HIDDeviceInformationService(BLE &_ble, |
xx316 | 1:c840c2b6f490 | 66 | const char *manufacturersName = NULL, |
xx316 | 1:c840c2b6f490 | 67 | const char *modelNumber = NULL, |
xx316 | 1:c840c2b6f490 | 68 | const char *serialNumber = NULL, |
xx316 | 1:c840c2b6f490 | 69 | const char *hardwareRevision = NULL, |
xx316 | 1:c840c2b6f490 | 70 | const char *firmwareRevision = NULL, |
xx316 | 1:c840c2b6f490 | 71 | const char *softwareRevision = NULL, |
xx316 | 1:c840c2b6f490 | 72 | PnPID_t *PnPID = NULL) : |
xx316 | 1:c840c2b6f490 | 73 | ble(_ble), |
xx316 | 1:c840c2b6f490 | 74 | manufacturersNameStringCharacteristic(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 75 | (uint8_t *)manufacturersName, |
xx316 | 1:c840c2b6f490 | 76 | (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 77 | (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 78 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 79 | modelNumberStringCharacteristic(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 80 | (uint8_t *)modelNumber, |
xx316 | 1:c840c2b6f490 | 81 | (modelNumber != NULL) ? strlen(modelNumber) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 82 | (modelNumber != NULL) ? strlen(modelNumber) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 83 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 84 | serialNumberStringCharacteristic(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 85 | (uint8_t *)serialNumber, |
xx316 | 1:c840c2b6f490 | 86 | (serialNumber != NULL) ? strlen(serialNumber) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 87 | (serialNumber != NULL) ? strlen(serialNumber) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 88 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 89 | hardwareRevisionStringCharacteristic(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 90 | (uint8_t *)hardwareRevision, |
xx316 | 1:c840c2b6f490 | 91 | (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 92 | (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 93 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 94 | firmwareRevisionStringCharacteristic(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 95 | (uint8_t *)firmwareRevision, |
xx316 | 1:c840c2b6f490 | 96 | (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 97 | (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 98 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 99 | softwareRevisionStringCharacteristic(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, |
xx316 | 1:c840c2b6f490 | 100 | (uint8_t *)softwareRevision, |
xx316 | 1:c840c2b6f490 | 101 | (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* minLength */ |
xx316 | 1:c840c2b6f490 | 102 | (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* maxLength */ |
xx316 | 1:c840c2b6f490 | 103 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
xx316 | 1:c840c2b6f490 | 104 | pnpIDCharacteristic(GattCharacteristic::UUID_PNP_ID_CHAR, |
xx316 | 1:c840c2b6f490 | 105 | PnPID) |
xx316 | 1:c840c2b6f490 | 106 | { |
xx316 | 1:c840c2b6f490 | 107 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
xx316 | 1:c840c2b6f490 | 108 | if (serviceAdded) { |
xx316 | 1:c840c2b6f490 | 109 | return; |
xx316 | 1:c840c2b6f490 | 110 | } |
xx316 | 1:c840c2b6f490 | 111 | |
xx316 | 1:c840c2b6f490 | 112 | GattCharacteristic *charTable[] = {&manufacturersNameStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 113 | &modelNumberStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 114 | &serialNumberStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 115 | &hardwareRevisionStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 116 | &firmwareRevisionStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 117 | &softwareRevisionStringCharacteristic, |
xx316 | 1:c840c2b6f490 | 118 | &pnpIDCharacteristic}; |
xx316 | 1:c840c2b6f490 | 119 | GattService deviceInformationService(GattService::UUID_DEVICE_INFORMATION_SERVICE, charTable, |
xx316 | 1:c840c2b6f490 | 120 | sizeof(charTable) / sizeof(GattCharacteristic *)); |
xx316 | 1:c840c2b6f490 | 121 | |
xx316 | 1:c840c2b6f490 | 122 | /* |
xx316 | 1:c840c2b6f490 | 123 | * This is a hack to make things work on MacOSX 10.10. I don't have the details, but MacOSX |
xx316 | 1:c840c2b6f490 | 124 | * 10.10 gets confused when only characteristics from HID Service require security... |
xx316 | 1:c840c2b6f490 | 125 | */ |
xx316 | 1:c840c2b6f490 | 126 | pnpIDCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM); |
xx316 | 1:c840c2b6f490 | 127 | ble.addService(deviceInformationService); |
xx316 | 1:c840c2b6f490 | 128 | serviceAdded = true; |
xx316 | 1:c840c2b6f490 | 129 | } |
xx316 | 1:c840c2b6f490 | 130 | |
xx316 | 1:c840c2b6f490 | 131 | protected: |
xx316 | 1:c840c2b6f490 | 132 | BLE &ble; |
xx316 | 1:c840c2b6f490 | 133 | GattCharacteristic manufacturersNameStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 134 | GattCharacteristic modelNumberStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 135 | GattCharacteristic serialNumberStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 136 | GattCharacteristic hardwareRevisionStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 137 | GattCharacteristic firmwareRevisionStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 138 | GattCharacteristic softwareRevisionStringCharacteristic; |
xx316 | 1:c840c2b6f490 | 139 | ReadOnlyGattCharacteristic<PnPID_t> pnpIDCharacteristic; |
xx316 | 1:c840c2b6f490 | 140 | }; |
xx316 | 1:c840c2b6f490 | 141 | |
xx316 | 1:c840c2b6f490 | 142 | #endif /* #ifndef __BLE_HID_DEVICE_INFORMATION_SERVICE_H__*/ |
xx316 | 1:c840c2b6f490 | 143 | |
xx316 | 1:c840c2b6f490 | 144 |