Hiroh Satoh / keyboard Featured

Dependencies:   BLE_API mbed-dev nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DeviceInformationService.h Source File

DeviceInformationService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__
00018 #define __BLE_DEVICE_INFORMATION_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 
00022 // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.pnp_id.xml
00023 #pragma pack(push, 1)
00024 typedef struct {
00025     uint8_t vendorID_source;
00026     uint16_t vendorID;
00027     uint16_t productID;
00028     uint16_t productVersion;
00029 } PnPID_t;
00030 #pragma pack(pop)
00031 
00032 
00033 /**
00034 * @class DeviceInformationService
00035 * @brief BLE Device Information Service
00036 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
00037 * Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml
00038 */
00039 class DeviceInformationService {
00040 public:
00041     /**
00042      * @brief Device Information Service Constructor: copies device-specific information
00043      * into the BLE stack.
00044      *
00045      * @param[in] _ble
00046      *                A reference to a BLE object for the underlying controller.
00047      * @param[in] manufacturersName
00048      *                The name of the manufacturer of the device.
00049      * @param[in] modelNumber
00050      *                The model number that is assigned by the device vendor.
00051      * @param[in] serialNumber
00052      *                The serial number for a particular instance of the device.
00053      * @param[in] hardwareRevision
00054      *                The hardware revision for the hardware within the device.
00055      * @param[in] firmwareRevision
00056      *                The device's firmware version.
00057      * @param[in] softwareRevision
00058      *                The device's software version.
00059      */
00060     DeviceInformationService(
00061         BLE        &_ble,
00062         const char *manufacturersName = NULL,
00063         const char *modelNumber       = NULL,
00064         const char *serialNumber      = NULL,
00065         const char *hardwareRevision  = NULL,
00066         const char *firmwareRevision  = NULL,
00067         const char *softwareRevision  = NULL,
00068         const PnPID_t *pnpID          = NULL
00069     ) :
00070         ble(_ble),
00071         manufacturersNameStringCharacteristic(
00072             GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR,
00073             (uint8_t *)manufacturersName,
00074             (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* Min length */
00075             (manufacturersName != NULL) ? strlen(manufacturersName) : 0, /* Max length */
00076             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00077         ),
00078         modelNumberStringCharacteristic(
00079             GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR,
00080             (uint8_t *)modelNumber,
00081             (modelNumber != NULL) ? strlen(modelNumber) : 0, /* Min length */
00082             (modelNumber != NULL) ? strlen(modelNumber) : 0, /* Max length */
00083             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00084         ),
00085         serialNumberStringCharacteristic(
00086             GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR,
00087             (uint8_t *)serialNumber,
00088             (serialNumber != NULL) ? strlen(serialNumber) : 0, /* Min length */
00089             (serialNumber != NULL) ? strlen(serialNumber) : 0, /* Max length */
00090             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00091         ),
00092         hardwareRevisionStringCharacteristic(
00093             GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR,
00094             (uint8_t *)hardwareRevision,
00095             (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* Min length */
00096             (hardwareRevision != NULL) ? strlen(hardwareRevision) : 0, /* Max length */
00097             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00098         ),
00099         firmwareRevisionStringCharacteristic(
00100             GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR,
00101             (uint8_t *)firmwareRevision,
00102             (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* Min length */
00103             (firmwareRevision != NULL) ? strlen(firmwareRevision) : 0, /* Max length */
00104             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00105         ),
00106         softwareRevisionStringCharacteristic(
00107             GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR,
00108             (uint8_t *)softwareRevision,
00109             (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* Min length */
00110             (softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* Max length */
00111             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
00112         ),
00113         pnpIDCharacteristic(
00114             GattCharacteristic::UUID_PNP_ID_CHAR,
00115             const_cast<PnPID_t*>(pnpID)
00116         )
00117     {
00118         static bool serviceAdded = false; /* We only add the information service once. */
00119         if (serviceAdded) {
00120             return;
00121         }
00122 
00123         // required for OS X bonding
00124         pnpIDCharacteristic.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00125 
00126         GattCharacteristic *charTable[] = {
00127             &manufacturersNameStringCharacteristic,
00128             &modelNumberStringCharacteristic,
00129             &serialNumberStringCharacteristic,
00130             &hardwareRevisionStringCharacteristic,
00131             &firmwareRevisionStringCharacteristic,
00132             &softwareRevisionStringCharacteristic,
00133             &pnpIDCharacteristic};
00134 
00135         GattService deviceInformationService(
00136             GattService::UUID_DEVICE_INFORMATION_SERVICE,
00137             charTable,
00138             sizeof(charTable) / sizeof(GattCharacteristic *)
00139         );
00140 
00141         ble.addService(deviceInformationService);
00142         serviceAdded = true;
00143     }
00144 
00145 protected:
00146     /**
00147      * A reference to the BLE instance object to which the services and
00148      * characteristics will be added.
00149      */
00150     BLE                &ble;
00151     /**
00152      * BLE characterising to allow BLE peers access to the manufacturer's name.
00153      */
00154     GattCharacteristic  manufacturersNameStringCharacteristic;
00155     /**
00156      * BLE characterising to allow BLE peers access to the model number.
00157      */
00158     GattCharacteristic  modelNumberStringCharacteristic;
00159     /**
00160      * BLE characterising to allow BLE peers access to the serial number.
00161      */
00162     GattCharacteristic  serialNumberStringCharacteristic;
00163     /**
00164      * BLE characterising to allow BLE peers access to the hardware revision string.
00165      */
00166     GattCharacteristic  hardwareRevisionStringCharacteristic;
00167     /**
00168      * BLE characterising to allow BLE peers access to the firmware revision string.
00169      */
00170     GattCharacteristic  firmwareRevisionStringCharacteristic;
00171     /**
00172      * BLE characterising to allow BLE peers access to the software revision string.
00173      */
00174     GattCharacteristic  softwareRevisionStringCharacteristic;
00175     ReadOnlyGattCharacteristic<PnPID_t>  pnpIDCharacteristic;
00176 };
00177 
00178 #endif /* #ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__*/