MicroBit as BLE gamepad

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
rengro01
Date:
Mon Jan 30 08:29:06 2017 +0000
Revision:
75:df904445f561
Parent:
74:1b9850e39cd1
MicroBit as BLE gamepad

Who changed what in which revision?

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