Initial release

Dependencies:   microbit

Committer:
masakjm
Date:
Mon Sep 17 02:48:51 2018 +0000
Revision:
0:28fb3e9ef81a
first release

Who changed what in which revision?

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