BLE temperature profile using digital DS1820 or analog LM35 sensors
BLE_API_Native/hw/nRF51822n/nRF51GattServer.cpp@0:637031152314, 2015-03-07 (annotated)
- Committer:
- gkroussos
- Date:
- Sat Mar 07 16:23:41 2015 +0000
- Revision:
- 0:637031152314
Working version 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gkroussos | 0:637031152314 | 1 | /* mbed Microcontroller Library |
gkroussos | 0:637031152314 | 2 | * Copyright (c) 2006-2013 ARM Limited |
gkroussos | 0:637031152314 | 3 | * |
gkroussos | 0:637031152314 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
gkroussos | 0:637031152314 | 5 | * you may not use this file except in compliance with the License. |
gkroussos | 0:637031152314 | 6 | * You may obtain a copy of the License at |
gkroussos | 0:637031152314 | 7 | * |
gkroussos | 0:637031152314 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
gkroussos | 0:637031152314 | 9 | * |
gkroussos | 0:637031152314 | 10 | * Unless required by applicable law or agreed to in writing, software |
gkroussos | 0:637031152314 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
gkroussos | 0:637031152314 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
gkroussos | 0:637031152314 | 13 | * See the License for the specific language governing permissions and |
gkroussos | 0:637031152314 | 14 | * limitations under the License. |
gkroussos | 0:637031152314 | 15 | */ |
gkroussos | 0:637031152314 | 16 | |
gkroussos | 0:637031152314 | 17 | #include "nRF51GattServer.h" |
gkroussos | 0:637031152314 | 18 | #include "mbed.h" |
gkroussos | 0:637031152314 | 19 | |
gkroussos | 0:637031152314 | 20 | #include "common/common.h" |
gkroussos | 0:637031152314 | 21 | #include "btle/custom/custom_helper.h" |
gkroussos | 0:637031152314 | 22 | |
gkroussos | 0:637031152314 | 23 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 24 | /*! |
gkroussos | 0:637031152314 | 25 | @brief Adds a new service to the GATT table on the peripheral |
gkroussos | 0:637031152314 | 26 | |
gkroussos | 0:637031152314 | 27 | @returns ble_error_t |
gkroussos | 0:637031152314 | 28 | |
gkroussos | 0:637031152314 | 29 | @retval BLE_ERROR_NONE |
gkroussos | 0:637031152314 | 30 | Everything executed properly |
gkroussos | 0:637031152314 | 31 | |
gkroussos | 0:637031152314 | 32 | @section EXAMPLE |
gkroussos | 0:637031152314 | 33 | |
gkroussos | 0:637031152314 | 34 | @code |
gkroussos | 0:637031152314 | 35 | |
gkroussos | 0:637031152314 | 36 | @endcode |
gkroussos | 0:637031152314 | 37 | */ |
gkroussos | 0:637031152314 | 38 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 39 | ble_error_t nRF51GattServer::addService(GattService & service) |
gkroussos | 0:637031152314 | 40 | { |
gkroussos | 0:637031152314 | 41 | /* ToDo: Make sure we don't overflow the array, etc. */ |
gkroussos | 0:637031152314 | 42 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
gkroussos | 0:637031152314 | 43 | /* ToDo: Basic validation */ |
gkroussos | 0:637031152314 | 44 | |
gkroussos | 0:637031152314 | 45 | /* Add the service to the nRF51 */ |
gkroussos | 0:637031152314 | 46 | ble_uuid_t uuid; |
gkroussos | 0:637031152314 | 47 | |
gkroussos | 0:637031152314 | 48 | if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT) |
gkroussos | 0:637031152314 | 49 | { |
gkroussos | 0:637031152314 | 50 | /* 16-bit BLE UUID */ |
gkroussos | 0:637031152314 | 51 | uuid.type = BLE_UUID_TYPE_BLE; |
gkroussos | 0:637031152314 | 52 | } |
gkroussos | 0:637031152314 | 53 | else |
gkroussos | 0:637031152314 | 54 | { |
gkroussos | 0:637031152314 | 55 | /* 128-bit Custom UUID */ |
gkroussos | 0:637031152314 | 56 | uuid.type = custom_add_uuid_base( service.primaryServiceID.base ); |
gkroussos | 0:637031152314 | 57 | } |
gkroussos | 0:637031152314 | 58 | |
gkroussos | 0:637031152314 | 59 | uuid.uuid = service.primaryServiceID.value; |
gkroussos | 0:637031152314 | 60 | |
gkroussos | 0:637031152314 | 61 | ASSERT( ERROR_NONE == sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &service.handle), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
gkroussos | 0:637031152314 | 62 | |
gkroussos | 0:637031152314 | 63 | /* Add characteristics to the service */ |
gkroussos | 0:637031152314 | 64 | for (uint8_t i = 0; i < service.characteristicCount; i++) |
gkroussos | 0:637031152314 | 65 | { |
gkroussos | 0:637031152314 | 66 | GattCharacteristic * p_char = service.characteristics[i]; |
gkroussos | 0:637031152314 | 67 | |
gkroussos | 0:637031152314 | 68 | uuid.uuid = p_char->uuid; |
gkroussos | 0:637031152314 | 69 | ASSERT ( ERROR_NONE == custom_add_in_characteristic(service.handle, &uuid, p_char->properties, |
gkroussos | 0:637031152314 | 70 | NULL, p_char->lenMin, p_char->lenMax, &nrfCharacteristicHandles[characteristicCount]), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
gkroussos | 0:637031152314 | 71 | |
gkroussos | 0:637031152314 | 72 | /* Update the characteristic handle */ |
gkroussos | 0:637031152314 | 73 | p_char->handle = characteristicCount; |
gkroussos | 0:637031152314 | 74 | p_characteristics[characteristicCount++] = p_char; |
gkroussos | 0:637031152314 | 75 | } |
gkroussos | 0:637031152314 | 76 | |
gkroussos | 0:637031152314 | 77 | serviceCount++; |
gkroussos | 0:637031152314 | 78 | |
gkroussos | 0:637031152314 | 79 | return BLE_ERROR_NONE; |
gkroussos | 0:637031152314 | 80 | } |
gkroussos | 0:637031152314 | 81 | |
gkroussos | 0:637031152314 | 82 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 83 | /*! |
gkroussos | 0:637031152314 | 84 | @brief Reads the value of a characteristic, based on the service |
gkroussos | 0:637031152314 | 85 | and characteristic index fields |
gkroussos | 0:637031152314 | 86 | |
gkroussos | 0:637031152314 | 87 | @param[in] charHandle |
gkroussos | 0:637031152314 | 88 | The handle of the GattCharacteristic to read from |
gkroussos | 0:637031152314 | 89 | @param[in] buffer |
gkroussos | 0:637031152314 | 90 | Buffer to hold the the characteristic's value |
gkroussos | 0:637031152314 | 91 | (raw byte array in LSB format) |
gkroussos | 0:637031152314 | 92 | @param[in] len |
gkroussos | 0:637031152314 | 93 | The number of bytes read into the buffer |
gkroussos | 0:637031152314 | 94 | |
gkroussos | 0:637031152314 | 95 | @returns ble_error_t |
gkroussos | 0:637031152314 | 96 | |
gkroussos | 0:637031152314 | 97 | @retval BLE_ERROR_NONE |
gkroussos | 0:637031152314 | 98 | Everything executed properly |
gkroussos | 0:637031152314 | 99 | |
gkroussos | 0:637031152314 | 100 | @section EXAMPLE |
gkroussos | 0:637031152314 | 101 | |
gkroussos | 0:637031152314 | 102 | @code |
gkroussos | 0:637031152314 | 103 | |
gkroussos | 0:637031152314 | 104 | @endcode |
gkroussos | 0:637031152314 | 105 | */ |
gkroussos | 0:637031152314 | 106 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 107 | ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t len) |
gkroussos | 0:637031152314 | 108 | { |
gkroussos | 0:637031152314 | 109 | ASSERT( ERROR_NONE == sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); |
gkroussos | 0:637031152314 | 110 | |
gkroussos | 0:637031152314 | 111 | return BLE_ERROR_NONE; |
gkroussos | 0:637031152314 | 112 | } |
gkroussos | 0:637031152314 | 113 | |
gkroussos | 0:637031152314 | 114 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 115 | /*! |
gkroussos | 0:637031152314 | 116 | @brief Updates the value of a characteristic, based on the service |
gkroussos | 0:637031152314 | 117 | and characteristic index fields |
gkroussos | 0:637031152314 | 118 | |
gkroussos | 0:637031152314 | 119 | @param[in] charHandle |
gkroussos | 0:637031152314 | 120 | The handle of the GattCharacteristic to write to |
gkroussos | 0:637031152314 | 121 | @param[in] buffer |
gkroussos | 0:637031152314 | 122 | Data to use when updating the characteristic's value |
gkroussos | 0:637031152314 | 123 | (raw byte array in LSB format) |
gkroussos | 0:637031152314 | 124 | @param[in] len |
gkroussos | 0:637031152314 | 125 | The number of bytes in buffer |
gkroussos | 0:637031152314 | 126 | |
gkroussos | 0:637031152314 | 127 | @returns ble_error_t |
gkroussos | 0:637031152314 | 128 | |
gkroussos | 0:637031152314 | 129 | @retval BLE_ERROR_NONE |
gkroussos | 0:637031152314 | 130 | Everything executed properly |
gkroussos | 0:637031152314 | 131 | |
gkroussos | 0:637031152314 | 132 | @section EXAMPLE |
gkroussos | 0:637031152314 | 133 | |
gkroussos | 0:637031152314 | 134 | @code |
gkroussos | 0:637031152314 | 135 | |
gkroussos | 0:637031152314 | 136 | @endcode |
gkroussos | 0:637031152314 | 137 | */ |
gkroussos | 0:637031152314 | 138 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 139 | ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len) |
gkroussos | 0:637031152314 | 140 | { |
gkroussos | 0:637031152314 | 141 | if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
gkroussos | 0:637031152314 | 142 | (m_connectionHandle != BLE_CONN_HANDLE_INVALID) ) |
gkroussos | 0:637031152314 | 143 | { |
gkroussos | 0:637031152314 | 144 | /* HVX update for the characteristic value */ |
gkroussos | 0:637031152314 | 145 | ble_gatts_hvx_params_t hvx_params; |
gkroussos | 0:637031152314 | 146 | |
gkroussos | 0:637031152314 | 147 | hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; |
gkroussos | 0:637031152314 | 148 | hvx_params.type = (p_characteristics[charHandle]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
gkroussos | 0:637031152314 | 149 | hvx_params.offset = 0; |
gkroussos | 0:637031152314 | 150 | hvx_params.p_data = buffer; |
gkroussos | 0:637031152314 | 151 | hvx_params.p_len = &len; |
gkroussos | 0:637031152314 | 152 | |
gkroussos | 0:637031152314 | 153 | error_t error = (error_t) sd_ble_gatts_hvx(m_connectionHandle, &hvx_params); |
gkroussos | 0:637031152314 | 154 | |
gkroussos | 0:637031152314 | 155 | /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */ |
gkroussos | 0:637031152314 | 156 | if ( (error != ERROR_NONE ) && (error != ERROR_INVALID_STATE) && |
gkroussos | 0:637031152314 | 157 | (error != ERROR_BLE_NO_TX_BUFFERS ) && (error != ERROR_BUSY ) && |
gkroussos | 0:637031152314 | 158 | (error != ERROR_BLEGATTS_SYS_ATTR_MISSING ) ) |
gkroussos | 0:637031152314 | 159 | { |
gkroussos | 0:637031152314 | 160 | ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
gkroussos | 0:637031152314 | 161 | } |
gkroussos | 0:637031152314 | 162 | } else |
gkroussos | 0:637031152314 | 163 | { |
gkroussos | 0:637031152314 | 164 | ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); |
gkroussos | 0:637031152314 | 165 | } |
gkroussos | 0:637031152314 | 166 | |
gkroussos | 0:637031152314 | 167 | return BLE_ERROR_NONE; |
gkroussos | 0:637031152314 | 168 | } |
gkroussos | 0:637031152314 | 169 | |
gkroussos | 0:637031152314 | 170 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 171 | /*! |
gkroussos | 0:637031152314 | 172 | @brief Callback handler for events getting pushed up from the SD |
gkroussos | 0:637031152314 | 173 | */ |
gkroussos | 0:637031152314 | 174 | /**************************************************************************/ |
gkroussos | 0:637031152314 | 175 | void nRF51GattServer::hwCallback(ble_evt_t * p_ble_evt) |
gkroussos | 0:637031152314 | 176 | { |
gkroussos | 0:637031152314 | 177 | uint16_t handle_value; |
gkroussos | 0:637031152314 | 178 | GattServerEvents::gattEvent_t event; |
gkroussos | 0:637031152314 | 179 | |
gkroussos | 0:637031152314 | 180 | switch (p_ble_evt->header.evt_id) |
gkroussos | 0:637031152314 | 181 | { |
gkroussos | 0:637031152314 | 182 | case BLE_GATTS_EVT_WRITE: |
gkroussos | 0:637031152314 | 183 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
gkroussos | 0:637031152314 | 184 | |
gkroussos | 0:637031152314 | 185 | /* 1.) Handle CCCD changes */ |
gkroussos | 0:637031152314 | 186 | handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; |
gkroussos | 0:637031152314 | 187 | for(uint8_t i=0; i<characteristicCount; i++) |
gkroussos | 0:637031152314 | 188 | { |
gkroussos | 0:637031152314 | 189 | if ( (p_characteristics[i]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
gkroussos | 0:637031152314 | 190 | (nrfCharacteristicHandles[i].cccd_handle == handle_value) ) |
gkroussos | 0:637031152314 | 191 | { |
gkroussos | 0:637031152314 | 192 | uint16_t cccd_value = (p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ |
gkroussos | 0:637031152314 | 193 | |
gkroussos | 0:637031152314 | 194 | if ( ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION )) || |
gkroussos | 0:637031152314 | 195 | ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY ) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) |
gkroussos | 0:637031152314 | 196 | { |
gkroussos | 0:637031152314 | 197 | event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
gkroussos | 0:637031152314 | 198 | } else |
gkroussos | 0:637031152314 | 199 | { |
gkroussos | 0:637031152314 | 200 | event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
gkroussos | 0:637031152314 | 201 | } |
gkroussos | 0:637031152314 | 202 | |
gkroussos | 0:637031152314 | 203 | handleEvent(event, i); |
gkroussos | 0:637031152314 | 204 | return; |
gkroussos | 0:637031152314 | 205 | } |
gkroussos | 0:637031152314 | 206 | } |
gkroussos | 0:637031152314 | 207 | |
gkroussos | 0:637031152314 | 208 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
gkroussos | 0:637031152314 | 209 | event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
gkroussos | 0:637031152314 | 210 | break; |
gkroussos | 0:637031152314 | 211 | |
gkroussos | 0:637031152314 | 212 | case BLE_GATTS_EVT_HVC: |
gkroussos | 0:637031152314 | 213 | /* Indication confirmation received */ |
gkroussos | 0:637031152314 | 214 | event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
gkroussos | 0:637031152314 | 215 | handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; |
gkroussos | 0:637031152314 | 216 | break; |
gkroussos | 0:637031152314 | 217 | |
gkroussos | 0:637031152314 | 218 | default: |
gkroussos | 0:637031152314 | 219 | return; |
gkroussos | 0:637031152314 | 220 | } |
gkroussos | 0:637031152314 | 221 | |
gkroussos | 0:637031152314 | 222 | /* Find index (charHandle) in the pool */ |
gkroussos | 0:637031152314 | 223 | for(uint8_t i=0; i<characteristicCount; i++) |
gkroussos | 0:637031152314 | 224 | { |
gkroussos | 0:637031152314 | 225 | if (nrfCharacteristicHandles[i].value_handle == handle_value) |
gkroussos | 0:637031152314 | 226 | { |
gkroussos | 0:637031152314 | 227 | handleEvent(event, i); |
gkroussos | 0:637031152314 | 228 | break; |
gkroussos | 0:637031152314 | 229 | } |
gkroussos | 0:637031152314 | 230 | } |
gkroussos | 0:637031152314 | 231 | } |