Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
BlueNRGGattServer.cpp@7:55ac052585db, 2014-08-01 (annotated)
- Committer:
- mridup
- Date:
- Fri Aug 01 12:52:33 2014 +0000
- Revision:
- 7:55ac052585db
- Parent:
- 5:31dedfa19a12
- Child:
- 8:486bcaa4eb3d
Addition of multiple characteristics in a service
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mridup | 2:a2b623661316 | 1 | /* mbed Microcontroller Library | 
| mridup | 2:a2b623661316 | 2 | * Copyright (c) 2006-2013 ARM Limited | 
| mridup | 2:a2b623661316 | 3 | * | 
| mridup | 2:a2b623661316 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| mridup | 2:a2b623661316 | 5 | * you may not use this file except in compliance with the License. | 
| mridup | 2:a2b623661316 | 6 | * You may obtain a copy of the License at | 
| mridup | 2:a2b623661316 | 7 | * | 
| mridup | 2:a2b623661316 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| mridup | 2:a2b623661316 | 9 | * | 
| mridup | 2:a2b623661316 | 10 | * Unless required by applicable law or agreed to in writing, software | 
| mridup | 2:a2b623661316 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| mridup | 2:a2b623661316 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| mridup | 2:a2b623661316 | 13 | * See the License for the specific language governing permissions and | 
| mridup | 2:a2b623661316 | 14 | * limitations under the License. | 
| mridup | 2:a2b623661316 | 15 | */ | 
| mridup | 2:a2b623661316 | 16 | |
| mridup | 2:a2b623661316 | 17 | #include "BlueNRGGattServer.h" | 
| mridup | 2:a2b623661316 | 18 | #include "mbed.h" | 
| mridup | 2:a2b623661316 | 19 | #include "BlueNRGGap.h" | 
| mridup | 2:a2b623661316 | 20 | |
| mridup | 5:31dedfa19a12 | 21 | #define STORE_LE_16(buf, val) ( ((buf)[0] = (tHalUint8) (val) ) , \ | 
| mridup | 5:31dedfa19a12 | 22 | ((buf)[1] = (tHalUint8) (val>>8) ) ) | 
| mridup | 5:31dedfa19a12 | 23 | |
| mridup | 5:31dedfa19a12 | 24 | #define STORE_LE_32(buf, val) ( ((buf)[0] = (tHalUint8) (val) ) , \ | 
| mridup | 5:31dedfa19a12 | 25 | ((buf)[1] = (tHalUint8) (val>>8) ) , \ | 
| mridup | 5:31dedfa19a12 | 26 | ((buf)[2] = (tHalUint8) (val>>16) ) , \ | 
| mridup | 5:31dedfa19a12 | 27 | ((buf)[3] = (tHalUint8) (val>>24) ) ) | 
| mridup | 5:31dedfa19a12 | 28 | |
| mridup | 2:a2b623661316 | 29 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 30 | /*! | 
| mridup | 2:a2b623661316 | 31 | @brief Adds a new service to the GATT table on the peripheral | 
| mridup | 2:a2b623661316 | 32 | |
| mridup | 2:a2b623661316 | 33 | @returns ble_error_t | 
| mridup | 2:a2b623661316 | 34 | |
| mridup | 2:a2b623661316 | 35 | @retval BLE_ERROR_NONE | 
| mridup | 2:a2b623661316 | 36 | Everything executed properly | 
| mridup | 2:a2b623661316 | 37 | |
| mridup | 2:a2b623661316 | 38 | @section EXAMPLE | 
| mridup | 2:a2b623661316 | 39 | |
| mridup | 2:a2b623661316 | 40 | @code | 
| mridup | 2:a2b623661316 | 41 | |
| mridup | 2:a2b623661316 | 42 | @endcode | 
| mridup | 2:a2b623661316 | 43 | */ | 
| mridup | 2:a2b623661316 | 44 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 45 | ble_error_t BlueNRGGattServer::addService(GattService &service) | 
| mridup | 2:a2b623661316 | 46 | { | 
| mridup | 2:a2b623661316 | 47 | /* ToDo: Make sure we don't overflow the array, etc. */ | 
| mridup | 2:a2b623661316 | 48 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ | 
| mridup | 2:a2b623661316 | 49 | /* ToDo: Basic validation */ | 
| mridup | 5:31dedfa19a12 | 50 | |
| mridup | 5:31dedfa19a12 | 51 | tBleStatus ret; | 
| mridup | 5:31dedfa19a12 | 52 | |
| mridup | 5:31dedfa19a12 | 53 | |
| mridup | 5:31dedfa19a12 | 54 | /* Add the service to the BlueNRG */ | 
| mridup | 7:55ac052585db | 55 | uint16_t primary_uuid = 0x180D;//(service.getUUID()).getShortUUID(); | 
| mridup | 7:55ac052585db | 56 | |
| mridup | 7:55ac052585db | 57 | //TODO: Check UUID existence?? | 
| mridup | 5:31dedfa19a12 | 58 | |
| mridup | 5:31dedfa19a12 | 59 | ret = aci_gatt_add_serv(UUID_TYPE_16, (const uint8_t*)primary_uuid, PRIMARY_SERVICE, 7, | 
| mridup | 5:31dedfa19a12 | 60 | &hrmServHandle); | 
| mridup | 5:31dedfa19a12 | 61 | service.setHandle(hrmServHandle); | 
| mridup | 2:a2b623661316 | 62 | |
| mridup | 7:55ac052585db | 63 | //TODO: iterate to include all characteristics | 
| mridup | 7:55ac052585db | 64 | for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { | 
| mridup | 5:31dedfa19a12 | 65 | GattCharacteristic *p_char = service.getCharacteristic(0); | 
| mridup | 5:31dedfa19a12 | 66 | uint16_t char_uuid = (p_char->getUUID()).getShortUUID(); | 
| mridup | 7:55ac052585db | 67 | |
| mridup | 7:55ac052585db | 68 | //TODO: Check UUID existence?? | 
| mridup | 7:55ac052585db | 69 | |
| mridup | 5:31dedfa19a12 | 70 | ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_16, (const uint8_t*)char_uuid, 1, | 
| mridup | 7:55ac052585db | 71 | p_char->getProperties()/*CHAR_PROP_NOTIFY*/, ATTR_PERMISSION_NONE, 0, | 
| mridup | 7:55ac052585db | 72 | 16, 0, /*&hrmCharHandle*/ &bleCharacteristicHandles[characteristicCount]); | 
| mridup | 7:55ac052585db | 73 | |
| mridup | 7:55ac052585db | 74 | /* Update the characteristic handle */ | 
| mridup | 7:55ac052585db | 75 | uint16_t charHandle = characteristicCount; | 
| mridup | 5:31dedfa19a12 | 76 | |
| mridup | 5:31dedfa19a12 | 77 | p_characteristics[characteristicCount++] = p_char; | 
| mridup | 7:55ac052585db | 78 | p_char->setHandle(charHandle); //Set the characteristic count as the corresponding char handle | 
| mridup | 7:55ac052585db | 79 | |
| mridup | 7:55ac052585db | 80 | if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) { | 
| mridup | 7:55ac052585db | 81 | updateValue(charHandle, p_char->getValuePtr(), p_char->getInitialLength(), false /* localOnly */); | 
| mridup | 7:55ac052585db | 82 | } | 
| mridup | 7:55ac052585db | 83 | } | 
| mridup | 7:55ac052585db | 84 | |
| mridup | 5:31dedfa19a12 | 85 | serviceCount++; | 
| mridup | 7:55ac052585db | 86 | |
| mridup | 7:55ac052585db | 87 | //FIXME: There is no GattService pointer array in GattServer. | 
| mridup | 7:55ac052585db | 88 | // There should be one? (Only the user is aware of GattServices!) Report to forum. | 
| mridup | 5:31dedfa19a12 | 89 | |
| mridup | 2:a2b623661316 | 90 | return BLE_ERROR_NONE; | 
| mridup | 2:a2b623661316 | 91 | } | 
| mridup | 2:a2b623661316 | 92 | |
| mridup | 7:55ac052585db | 93 | |
| mridup | 2:a2b623661316 | 94 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 95 | /*! | 
| mridup | 2:a2b623661316 | 96 | @brief Reads the value of a characteristic, based on the service | 
| mridup | 2:a2b623661316 | 97 | and characteristic index fields | 
| mridup | 2:a2b623661316 | 98 | |
| mridup | 2:a2b623661316 | 99 | @param[in] charHandle | 
| mridup | 2:a2b623661316 | 100 | The handle of the GattCharacteristic to read from | 
| mridup | 2:a2b623661316 | 101 | @param[in] buffer | 
| mridup | 2:a2b623661316 | 102 | Buffer to hold the the characteristic's value | 
| mridup | 2:a2b623661316 | 103 | (raw byte array in LSB format) | 
| mridup | 2:a2b623661316 | 104 | @param[in] len | 
| mridup | 2:a2b623661316 | 105 | The number of bytes read into the buffer | 
| mridup | 2:a2b623661316 | 106 | |
| mridup | 2:a2b623661316 | 107 | @returns ble_error_t | 
| mridup | 2:a2b623661316 | 108 | |
| mridup | 2:a2b623661316 | 109 | @retval BLE_ERROR_NONE | 
| mridup | 2:a2b623661316 | 110 | Everything executed properly | 
| mridup | 2:a2b623661316 | 111 | |
| mridup | 2:a2b623661316 | 112 | @section EXAMPLE | 
| mridup | 2:a2b623661316 | 113 | |
| mridup | 2:a2b623661316 | 114 | @code | 
| mridup | 2:a2b623661316 | 115 | |
| mridup | 2:a2b623661316 | 116 | @endcode | 
| mridup | 2:a2b623661316 | 117 | */ | 
| mridup | 2:a2b623661316 | 118 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 119 | ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) | 
| mridup | 2:a2b623661316 | 120 | { | 
| mridup | 2:a2b623661316 | 121 | |
| mridup | 2:a2b623661316 | 122 | return BLE_ERROR_NONE; | 
| mridup | 2:a2b623661316 | 123 | } | 
| mridup | 2:a2b623661316 | 124 | |
| mridup | 2:a2b623661316 | 125 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 126 | /*! | 
| mridup | 2:a2b623661316 | 127 | @brief Updates the value of a characteristic, based on the service | 
| mridup | 2:a2b623661316 | 128 | and characteristic index fields | 
| mridup | 2:a2b623661316 | 129 | |
| mridup | 2:a2b623661316 | 130 | @param[in] charHandle | 
| mridup | 2:a2b623661316 | 131 | The handle of the GattCharacteristic to write to | 
| mridup | 2:a2b623661316 | 132 | @param[in] buffer | 
| mridup | 2:a2b623661316 | 133 | Data to use when updating the characteristic's value | 
| mridup | 2:a2b623661316 | 134 | (raw byte array in LSB format) | 
| mridup | 2:a2b623661316 | 135 | @param[in] len | 
| mridup | 2:a2b623661316 | 136 | The number of bytes in buffer | 
| mridup | 2:a2b623661316 | 137 | |
| mridup | 2:a2b623661316 | 138 | @returns ble_error_t | 
| mridup | 2:a2b623661316 | 139 | |
| mridup | 2:a2b623661316 | 140 | @retval BLE_ERROR_NONE | 
| mridup | 2:a2b623661316 | 141 | Everything executed properly | 
| mridup | 2:a2b623661316 | 142 | |
| mridup | 2:a2b623661316 | 143 | @section EXAMPLE | 
| mridup | 2:a2b623661316 | 144 | |
| mridup | 2:a2b623661316 | 145 | @code | 
| mridup | 2:a2b623661316 | 146 | |
| mridup | 2:a2b623661316 | 147 | @endcode | 
| mridup | 2:a2b623661316 | 148 | */ | 
| mridup | 2:a2b623661316 | 149 | /**************************************************************************/ | 
| mridup | 2:a2b623661316 | 150 | ble_error_t BlueNRGGattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) | 
| mridup | 2:a2b623661316 | 151 | { | 
| mridup | 5:31dedfa19a12 | 152 | tBleStatus ret; | 
| mridup | 5:31dedfa19a12 | 153 | tHalUint8 buff[6]; | 
| mridup | 2:a2b623661316 | 154 | |
| mridup | 5:31dedfa19a12 | 155 | STORE_LE_16(buff,125); | 
| mridup | 5:31dedfa19a12 | 156 | STORE_LE_16(buff+2,145); | 
| mridup | 5:31dedfa19a12 | 157 | STORE_LE_16(buff+4,543); | 
| mridup | 5:31dedfa19a12 | 158 | |
| mridup | 5:31dedfa19a12 | 159 | ret = aci_gatt_update_char_value(hrmServHandle, charHandle, 0, 6, buff); | 
| mridup | 2:a2b623661316 | 160 | |
| mridup | 2:a2b623661316 | 161 | return BLE_ERROR_NONE; | 
| mridup | 2:a2b623661316 | 162 | } | 
