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.
Fork of nRF51822 by
nRF51GattServer.cpp@56:a1071b629aa3, 2014-09-02 (annotated)
- Committer:
- Rohit Grover
- Date:
- Tue Sep 02 15:50:05 2014 +0100
- Revision:
- 56:a1071b629aa3
- Parent:
- 52:120bd37b9d0d
- Child:
- 57:2aa01a5008ac
Release 0.1.0
=============
We've achieved significant gains in power consumption: the BLE_Beacon demo now
runs at around 35uA of average current broadcasting once a second at 0dB; when
not using the radio, this demo consumes around 7uA.
Features
~~~~~~~~
- Replace initialization of high-frequency external crystal clock-source with
the use of low-frequency clock. This brings in significant gains in power
consumption.
- Re-implement the micro-second timer on nRF51 using the app_timer module
(which internally uses RTC). This limits the precision of the us_Timer to
30uS; but brings in significant gains in power consumption.
- Reduce the number of available app_timers and the event depths for app-timer
events; this will reduce memory consumption for zero-initialized data by
around 1K.
- Remove the call to conn_params_init() at startup. This is not mandatory; and
was causing an unnecessary re-negotiation of connection parameters a few
seconds into every connection.
- Reduce default transmission power level to 0dbB (was 4dbB before).
- Reduce min connection interval to 50ms and max to 500ms (previous values
were much larger).
- Replace a few instances of use of wait() with nrf_delay_us().
- onConnection() callback now receives connection-parameters applicable to the
new connection.
- onDataSent() callback now receives a count parameter containing the number of
times notifications were sent out since the last callback.
- A 'reason' parameter has been added to Gap::disconnect() to indicate the
reason for disconnection; and also to the onDisconnection callback to
receive a reason from the remote host.
- disable the app_gpiote module by default.
Bugfixes
~~~~~~~~
- onDataWritten() callback now passes an additional parameter
(GattServer::WriteEventCallback_t) encapsulating the update. This avoids
having to re-fetch the updated characteristic's value attribute. It also
fixes a bug where multiple updates to the characteristic's value-attribute
could get clobbered if they occurred in quick succession before the
callbacks could be processed.
Compatibility
~~~~~~~~~~~~~
Compatible with revision 0.1.0 of the BLE_API.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rohit Grover |
22:c6ee8136847e | 1 | /* mbed Microcontroller Library |
Rohit Grover |
22:c6ee8136847e | 2 | * Copyright (c) 2006-2013 ARM Limited |
Rohit Grover |
22:c6ee8136847e | 3 | * |
Rohit Grover |
22:c6ee8136847e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rohit Grover |
22:c6ee8136847e | 5 | * you may not use this file except in compliance with the License. |
Rohit Grover |
22:c6ee8136847e | 6 | * You may obtain a copy of the License at |
Rohit Grover |
22:c6ee8136847e | 7 | * |
Rohit Grover |
22:c6ee8136847e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rohit Grover |
22:c6ee8136847e | 9 | * |
Rohit Grover |
22:c6ee8136847e | 10 | * Unless required by applicable law or agreed to in writing, software |
Rohit Grover |
22:c6ee8136847e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rohit Grover |
22:c6ee8136847e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rohit Grover |
22:c6ee8136847e | 13 | * See the License for the specific language governing permissions and |
Rohit Grover |
22:c6ee8136847e | 14 | * limitations under the License. |
Rohit Grover |
22:c6ee8136847e | 15 | */ |
Rohit Grover |
22:c6ee8136847e | 16 | |
Rohit Grover |
22:c6ee8136847e | 17 | #include "nRF51GattServer.h" |
Rohit Grover |
22:c6ee8136847e | 18 | #include "mbed.h" |
Rohit Grover |
22:c6ee8136847e | 19 | |
Rohit Grover |
22:c6ee8136847e | 20 | #include "common/common.h" |
Rohit Grover |
22:c6ee8136847e | 21 | #include "btle/custom/custom_helper.h" |
Rohit Grover |
22:c6ee8136847e | 22 | |
Rohit Grover |
22:c6ee8136847e | 23 | #include "nRF51Gap.h" |
Rohit Grover |
22:c6ee8136847e | 24 | |
Rohit Grover |
22:c6ee8136847e | 25 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 26 | /*! |
Rohit Grover |
22:c6ee8136847e | 27 | @brief Adds a new service to the GATT table on the peripheral |
Rohit Grover |
22:c6ee8136847e | 28 | |
Rohit Grover |
22:c6ee8136847e | 29 | @returns ble_error_t |
Rohit Grover |
22:c6ee8136847e | 30 | |
Rohit Grover |
22:c6ee8136847e | 31 | @retval BLE_ERROR_NONE |
Rohit Grover |
22:c6ee8136847e | 32 | Everything executed properly |
Rohit Grover |
22:c6ee8136847e | 33 | |
Rohit Grover |
22:c6ee8136847e | 34 | @section EXAMPLE |
Rohit Grover |
22:c6ee8136847e | 35 | |
Rohit Grover |
22:c6ee8136847e | 36 | @code |
Rohit Grover |
22:c6ee8136847e | 37 | |
Rohit Grover |
22:c6ee8136847e | 38 | @endcode |
Rohit Grover |
22:c6ee8136847e | 39 | */ |
Rohit Grover |
22:c6ee8136847e | 40 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 41 | ble_error_t nRF51GattServer::addService(GattService &service) |
Rohit Grover |
22:c6ee8136847e | 42 | { |
Rohit Grover |
22:c6ee8136847e | 43 | /* ToDo: Make sure we don't overflow the array, etc. */ |
Rohit Grover |
22:c6ee8136847e | 44 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
Rohit Grover |
22:c6ee8136847e | 45 | /* ToDo: Basic validation */ |
Rohit Grover |
22:c6ee8136847e | 46 | |
Rohit Grover |
22:c6ee8136847e | 47 | /* Add the service to the nRF51 */ |
Rohit Grover |
22:c6ee8136847e | 48 | ble_uuid_t nordicUUID; |
Rohit Grover |
22:c6ee8136847e | 49 | nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); |
Rohit Grover |
29:cee837a465a1 | 50 | |
Rohit Grover |
29:cee837a465a1 | 51 | uint16_t serviceHandle; |
Rohit Grover |
22:c6ee8136847e | 52 | ASSERT( ERROR_NONE == |
Rohit Grover |
22:c6ee8136847e | 53 | sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, |
Rohit Grover |
22:c6ee8136847e | 54 | &nordicUUID, |
Rohit Grover |
29:cee837a465a1 | 55 | &serviceHandle), |
Rohit Grover |
22:c6ee8136847e | 56 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
29:cee837a465a1 | 57 | service.setHandle(serviceHandle); |
Rohit Grover |
22:c6ee8136847e | 58 | |
Rohit Grover |
22:c6ee8136847e | 59 | /* Add characteristics to the service */ |
Rohit Grover |
22:c6ee8136847e | 60 | for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { |
Rohit Grover |
22:c6ee8136847e | 61 | GattCharacteristic *p_char = service.getCharacteristic(i); |
Rohit Grover |
22:c6ee8136847e | 62 | |
Rohit Grover |
56:a1071b629aa3 | 63 | nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID()); |
Rohit Grover |
22:c6ee8136847e | 64 | |
Rohit Grover |
22:c6ee8136847e | 65 | ASSERT ( ERROR_NONE == |
Rohit Grover |
56:a1071b629aa3 | 66 | custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID, |
Rohit Grover |
22:c6ee8136847e | 67 | &nordicUUID, |
Rohit Grover |
22:c6ee8136847e | 68 | p_char->getProperties(), |
Rohit Grover |
56:a1071b629aa3 | 69 | p_char->getValueAttribute().getValuePtr(), |
Rohit Grover |
56:a1071b629aa3 | 70 | p_char->getValueAttribute().getInitialLength(), |
Rohit Grover |
56:a1071b629aa3 | 71 | p_char->getValueAttribute().getMaxLength(), |
Rohit Grover |
22:c6ee8136847e | 72 | &nrfCharacteristicHandles[characteristicCount]), |
Rohit Grover |
22:c6ee8136847e | 73 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
22:c6ee8136847e | 74 | |
Rohit Grover |
22:c6ee8136847e | 75 | /* Update the characteristic handle */ |
Rohit Grover |
28:fdc1a88a80c8 | 76 | uint16_t charHandle = characteristicCount; |
Rohit Grover |
22:c6ee8136847e | 77 | p_characteristics[characteristicCount++] = p_char; |
Rohit Grover |
28:fdc1a88a80c8 | 78 | |
Rohit Grover |
56:a1071b629aa3 | 79 | p_char->getValueAttribute().setHandle(charHandle); |
Rohit Grover |
56:a1071b629aa3 | 80 | |
Rohit Grover |
56:a1071b629aa3 | 81 | /* Add optional descriptors if any */ |
Rohit Grover |
56:a1071b629aa3 | 82 | /* ToDo: Make sure we don't overflow the array */ |
Rohit Grover |
56:a1071b629aa3 | 83 | for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { |
Rohit Grover |
56:a1071b629aa3 | 84 | GattAttribute *p_desc = p_char->getDescriptor(j); |
Rohit Grover |
56:a1071b629aa3 | 85 | |
Rohit Grover |
56:a1071b629aa3 | 86 | nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID()); |
Rohit Grover |
56:a1071b629aa3 | 87 | |
Rohit Grover |
56:a1071b629aa3 | 88 | ASSERT ( ERROR_NONE == |
Rohit Grover |
56:a1071b629aa3 | 89 | custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID, |
Rohit Grover |
56:a1071b629aa3 | 90 | &nordicUUID, |
Rohit Grover |
56:a1071b629aa3 | 91 | p_desc->getValuePtr(), |
Rohit Grover |
56:a1071b629aa3 | 92 | p_desc->getInitialLength(), |
Rohit Grover |
56:a1071b629aa3 | 93 | p_desc->getMaxLength(), |
Rohit Grover |
56:a1071b629aa3 | 94 | &nrfDescriptorHandles[descriptorCount]), |
Rohit Grover |
56:a1071b629aa3 | 95 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
56:a1071b629aa3 | 96 | |
Rohit Grover |
56:a1071b629aa3 | 97 | uint16_t descHandle = descriptorCount; |
Rohit Grover |
56:a1071b629aa3 | 98 | p_descriptors[descriptorCount++] = p_desc; |
Rohit Grover |
56:a1071b629aa3 | 99 | p_desc->setHandle(descHandle); |
Rohit Grover |
30:85305292b44f | 100 | } |
Rohit Grover |
22:c6ee8136847e | 101 | } |
Rohit Grover |
22:c6ee8136847e | 102 | |
Rohit Grover |
22:c6ee8136847e | 103 | serviceCount++; |
Rohit Grover |
22:c6ee8136847e | 104 | |
Rohit Grover |
22:c6ee8136847e | 105 | return BLE_ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 106 | } |
Rohit Grover |
22:c6ee8136847e | 107 | |
Rohit Grover |
22:c6ee8136847e | 108 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 109 | /*! |
Rohit Grover |
22:c6ee8136847e | 110 | @brief Reads the value of a characteristic, based on the service |
Rohit Grover |
22:c6ee8136847e | 111 | and characteristic index fields |
Rohit Grover |
22:c6ee8136847e | 112 | |
Rohit Grover |
22:c6ee8136847e | 113 | @param[in] charHandle |
Rohit Grover |
22:c6ee8136847e | 114 | The handle of the GattCharacteristic to read from |
Rohit Grover |
22:c6ee8136847e | 115 | @param[in] buffer |
Rohit Grover |
22:c6ee8136847e | 116 | Buffer to hold the the characteristic's value |
Rohit Grover |
22:c6ee8136847e | 117 | (raw byte array in LSB format) |
Rohit Grover |
22:c6ee8136847e | 118 | @param[in] len |
Rohit Grover |
22:c6ee8136847e | 119 | The number of bytes read into the buffer |
Rohit Grover |
22:c6ee8136847e | 120 | |
Rohit Grover |
22:c6ee8136847e | 121 | @returns ble_error_t |
Rohit Grover |
22:c6ee8136847e | 122 | |
Rohit Grover |
22:c6ee8136847e | 123 | @retval BLE_ERROR_NONE |
Rohit Grover |
22:c6ee8136847e | 124 | Everything executed properly |
Rohit Grover |
22:c6ee8136847e | 125 | |
Rohit Grover |
22:c6ee8136847e | 126 | @section EXAMPLE |
Rohit Grover |
22:c6ee8136847e | 127 | |
Rohit Grover |
22:c6ee8136847e | 128 | @code |
Rohit Grover |
22:c6ee8136847e | 129 | |
Rohit Grover |
22:c6ee8136847e | 130 | @endcode |
Rohit Grover |
22:c6ee8136847e | 131 | */ |
Rohit Grover |
22:c6ee8136847e | 132 | /**************************************************************************/ |
Rohit Grover |
31:c3ce6ee5d300 | 133 | ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) |
Rohit Grover |
22:c6ee8136847e | 134 | { |
Rohit Grover |
22:c6ee8136847e | 135 | ASSERT( ERROR_NONE == |
Rohit Grover |
31:c3ce6ee5d300 | 136 | sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, lengthP, buffer), |
Rohit Grover |
22:c6ee8136847e | 137 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
Rohit Grover |
22:c6ee8136847e | 138 | |
Rohit Grover |
22:c6ee8136847e | 139 | return BLE_ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 140 | } |
Rohit Grover |
22:c6ee8136847e | 141 | |
Rohit Grover |
22:c6ee8136847e | 142 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 143 | /*! |
Rohit Grover |
22:c6ee8136847e | 144 | @brief Updates the value of a characteristic, based on the service |
Rohit Grover |
22:c6ee8136847e | 145 | and characteristic index fields |
Rohit Grover |
22:c6ee8136847e | 146 | |
Rohit Grover |
22:c6ee8136847e | 147 | @param[in] charHandle |
Rohit Grover |
22:c6ee8136847e | 148 | The handle of the GattCharacteristic to write to |
Rohit Grover |
22:c6ee8136847e | 149 | @param[in] buffer |
Rohit Grover |
22:c6ee8136847e | 150 | Data to use when updating the characteristic's value |
Rohit Grover |
22:c6ee8136847e | 151 | (raw byte array in LSB format) |
Rohit Grover |
22:c6ee8136847e | 152 | @param[in] len |
Rohit Grover |
22:c6ee8136847e | 153 | The number of bytes in buffer |
Rohit Grover |
22:c6ee8136847e | 154 | |
Rohit Grover |
22:c6ee8136847e | 155 | @returns ble_error_t |
Rohit Grover |
22:c6ee8136847e | 156 | |
Rohit Grover |
22:c6ee8136847e | 157 | @retval BLE_ERROR_NONE |
Rohit Grover |
22:c6ee8136847e | 158 | Everything executed properly |
Rohit Grover |
22:c6ee8136847e | 159 | |
Rohit Grover |
22:c6ee8136847e | 160 | @section EXAMPLE |
Rohit Grover |
22:c6ee8136847e | 161 | |
Rohit Grover |
22:c6ee8136847e | 162 | @code |
Rohit Grover |
22:c6ee8136847e | 163 | |
Rohit Grover |
22:c6ee8136847e | 164 | @endcode |
Rohit Grover |
22:c6ee8136847e | 165 | */ |
Rohit Grover |
22:c6ee8136847e | 166 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 167 | ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) |
Rohit Grover |
22:c6ee8136847e | 168 | { |
Rohit Grover |
22:c6ee8136847e | 169 | uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle(); |
Rohit Grover |
22:c6ee8136847e | 170 | |
Rohit Grover |
22:c6ee8136847e | 171 | if (localOnly) { |
Rohit Grover |
22:c6ee8136847e | 172 | /* Only update locally regardless of notify/indicate */ |
Rohit Grover |
22:c6ee8136847e | 173 | ASSERT_INT( ERROR_NONE, |
Rohit Grover |
22:c6ee8136847e | 174 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
Rohit Grover |
22:c6ee8136847e | 175 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
38:dc4d0edf9bb9 | 176 | return BLE_ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 177 | } |
Rohit Grover |
22:c6ee8136847e | 178 | |
Rohit Grover |
22:c6ee8136847e | 179 | if ((p_characteristics[charHandle]->getProperties() & |
Rohit Grover |
22:c6ee8136847e | 180 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | |
Rohit Grover |
22:c6ee8136847e | 181 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
Rohit Grover |
22:c6ee8136847e | 182 | (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { |
Rohit Grover |
22:c6ee8136847e | 183 | /* HVX update for the characteristic value */ |
Rohit Grover |
22:c6ee8136847e | 184 | ble_gatts_hvx_params_t hvx_params; |
Rohit Grover |
22:c6ee8136847e | 185 | |
Rohit Grover |
22:c6ee8136847e | 186 | hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; |
Rohit Grover |
22:c6ee8136847e | 187 | hvx_params.type = |
Rohit Grover |
22:c6ee8136847e | 188 | (p_characteristics[charHandle]->getProperties() & |
Rohit Grover |
22:c6ee8136847e | 189 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? |
Rohit Grover |
22:c6ee8136847e | 190 | BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
Rohit Grover |
22:c6ee8136847e | 191 | hvx_params.offset = 0; |
Rohit Grover |
22:c6ee8136847e | 192 | hvx_params.p_data = buffer; |
Rohit Grover |
22:c6ee8136847e | 193 | hvx_params.p_len = &len; |
Rohit Grover |
22:c6ee8136847e | 194 | |
Rohit Grover |
22:c6ee8136847e | 195 | error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); |
Rohit Grover |
22:c6ee8136847e | 196 | |
Rohit Grover |
22:c6ee8136847e | 197 | /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and |
Rohit Grover |
22:c6ee8136847e | 198 | *ERROR_NO_TX_BUFFERS the ATT table has been updated. */ |
Rohit Grover |
22:c6ee8136847e | 199 | if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) && |
Rohit Grover |
22:c6ee8136847e | 200 | (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && |
Rohit Grover |
22:c6ee8136847e | 201 | (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { |
Rohit Grover |
22:c6ee8136847e | 202 | ASSERT_INT( ERROR_NONE, |
Rohit Grover |
22:c6ee8136847e | 203 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
Rohit Grover |
22:c6ee8136847e | 204 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
22:c6ee8136847e | 205 | } |
Rohit Grover |
22:c6ee8136847e | 206 | } else { |
Rohit Grover |
22:c6ee8136847e | 207 | ASSERT_INT( ERROR_NONE, |
Rohit Grover |
22:c6ee8136847e | 208 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
Rohit Grover |
22:c6ee8136847e | 209 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
Rohit Grover |
22:c6ee8136847e | 210 | } |
Rohit Grover |
22:c6ee8136847e | 211 | |
Rohit Grover |
22:c6ee8136847e | 212 | return BLE_ERROR_NONE; |
Rohit Grover |
22:c6ee8136847e | 213 | } |
Rohit Grover |
22:c6ee8136847e | 214 | |
Rohit Grover |
22:c6ee8136847e | 215 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 216 | /*! |
Rohit Grover |
22:c6ee8136847e | 217 | @brief Callback handler for events getting pushed up from the SD |
Rohit Grover |
22:c6ee8136847e | 218 | */ |
Rohit Grover |
22:c6ee8136847e | 219 | /**************************************************************************/ |
Rohit Grover |
22:c6ee8136847e | 220 | void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) |
Rohit Grover |
22:c6ee8136847e | 221 | { |
Rohit Grover |
56:a1071b629aa3 | 222 | uint16_t handle_value; |
Rohit Grover |
56:a1071b629aa3 | 223 | GattServerEvents::gattEvent_t eventType; |
Rohit Grover |
56:a1071b629aa3 | 224 | const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt; |
Rohit Grover |
22:c6ee8136847e | 225 | |
Rohit Grover |
22:c6ee8136847e | 226 | switch (p_ble_evt->header.evt_id) { |
Rohit Grover |
22:c6ee8136847e | 227 | case BLE_GATTS_EVT_WRITE: |
Rohit Grover |
22:c6ee8136847e | 228 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
Rohit Grover |
22:c6ee8136847e | 229 | |
Rohit Grover |
22:c6ee8136847e | 230 | /* 1.) Handle CCCD changes */ |
Rohit Grover |
56:a1071b629aa3 | 231 | handle_value = gattsEventP->params.write.handle; |
Rohit Grover |
22:c6ee8136847e | 232 | for (uint8_t i = 0; i<characteristicCount; i++) { |
Rohit Grover |
35:7174913c9d67 | 233 | if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
Rohit Grover |
35:7174913c9d67 | 234 | (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { |
Rohit Grover |
22:c6ee8136847e | 235 | uint16_t cccd_value = |
Rohit Grover |
56:a1071b629aa3 | 236 | (gattsEventP->params.write.data[1] << 8) | |
Rohit Grover |
56:a1071b629aa3 | 237 | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ |
Rohit Grover |
22:c6ee8136847e | 238 | |
Rohit Grover |
35:7174913c9d67 | 239 | if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) || |
Rohit Grover |
35:7174913c9d67 | 240 | ((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { |
Rohit Grover |
56:a1071b629aa3 | 241 | eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
Rohit Grover |
22:c6ee8136847e | 242 | } else { |
Rohit Grover |
56:a1071b629aa3 | 243 | eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
Rohit Grover |
22:c6ee8136847e | 244 | } |
Rohit Grover |
22:c6ee8136847e | 245 | |
Rohit Grover |
56:a1071b629aa3 | 246 | handleEvent(eventType, i); |
Rohit Grover |
22:c6ee8136847e | 247 | return; |
Rohit Grover |
22:c6ee8136847e | 248 | } |
Rohit Grover |
22:c6ee8136847e | 249 | } |
Rohit Grover |
22:c6ee8136847e | 250 | |
Rohit Grover |
34:48d24b1d2fe6 | 251 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
Rohit Grover |
56:a1071b629aa3 | 252 | eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
Rohit Grover |
22:c6ee8136847e | 253 | break; |
Rohit Grover |
22:c6ee8136847e | 254 | |
Rohit Grover |
22:c6ee8136847e | 255 | case BLE_GATTS_EVT_HVC: |
Rohit Grover |
22:c6ee8136847e | 256 | /* Indication confirmation received */ |
Rohit Grover |
56:a1071b629aa3 | 257 | eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
Rohit Grover |
56:a1071b629aa3 | 258 | handle_value = gattsEventP->params.hvc.handle; |
Rohit Grover |
22:c6ee8136847e | 259 | break; |
Rohit Grover |
22:c6ee8136847e | 260 | |
Rohit Grover |
56:a1071b629aa3 | 261 | case BLE_EVT_TX_COMPLETE: { |
Rohit Grover |
56:a1071b629aa3 | 262 | handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count); |
Rohit Grover |
56:a1071b629aa3 | 263 | return; |
Rohit Grover |
56:a1071b629aa3 | 264 | } |
Rohit Grover |
56:a1071b629aa3 | 265 | |
Rohit Grover |
39:d52a28e1fa80 | 266 | case BLE_GATTS_EVT_SYS_ATTR_MISSING: |
Rohit Grover |
56:a1071b629aa3 | 267 | sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0); |
Rohit Grover |
39:d52a28e1fa80 | 268 | return; |
Rohit Grover |
39:d52a28e1fa80 | 269 | |
Rohit Grover |
22:c6ee8136847e | 270 | default: |
Rohit Grover |
22:c6ee8136847e | 271 | return; |
Rohit Grover |
22:c6ee8136847e | 272 | } |
Rohit Grover |
22:c6ee8136847e | 273 | |
Rohit Grover |
22:c6ee8136847e | 274 | /* Find index (charHandle) in the pool */ |
Rohit Grover |
22:c6ee8136847e | 275 | for (uint8_t i = 0; i<characteristicCount; i++) { |
Rohit Grover |
22:c6ee8136847e | 276 | if (nrfCharacteristicHandles[i].value_handle == handle_value) { |
Rohit Grover |
56:a1071b629aa3 | 277 | switch (eventType) { |
Rohit Grover |
56:a1071b629aa3 | 278 | case GattServerEvents::GATT_EVENT_DATA_WRITTEN: { |
Rohit Grover |
56:a1071b629aa3 | 279 | GattCharacteristicWriteCBParams cbParams = { |
Rohit Grover |
56:a1071b629aa3 | 280 | .op = static_cast<GattCharacteristicWriteCBParams::Type>(gattsEventP->params.write.op), |
Rohit Grover |
56:a1071b629aa3 | 281 | .offset = gattsEventP->params.write.offset, |
Rohit Grover |
56:a1071b629aa3 | 282 | .len = gattsEventP->params.write.len, |
Rohit Grover |
56:a1071b629aa3 | 283 | .data = gattsEventP->params.write.data |
Rohit Grover |
56:a1071b629aa3 | 284 | }; |
Rohit Grover |
56:a1071b629aa3 | 285 | handleDataWrittenEvent(i, &cbParams); |
Rohit Grover |
56:a1071b629aa3 | 286 | break; |
Rohit Grover |
56:a1071b629aa3 | 287 | } |
Rohit Grover |
56:a1071b629aa3 | 288 | default: |
Rohit Grover |
56:a1071b629aa3 | 289 | handleEvent(eventType, i); |
Rohit Grover |
56:a1071b629aa3 | 290 | break; |
Rohit Grover |
56:a1071b629aa3 | 291 | } |
Rohit Grover |
22:c6ee8136847e | 292 | } |
Rohit Grover |
22:c6ee8136847e | 293 | } |
Rohit Grover |
22:c6ee8136847e | 294 | } |