Based on the example USB HID keyboard https://developer.mbed.org/users/jbru/code/BLE_HID_KeyboardStreamDemo/

Dependencies:   BLE_API mbed nRF51822

Fork of HID-kb by Microbug

Committer:
JonnyA
Date:
Wed Nov 14 09:46:45 2018 +0000
Revision:
3:7d7822143a2d
Parent:
0:cb1939018833
Add simple debounce wait on interrupts for buttons

Who changed what in which revision?

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