AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
HRM1017/nRF51GattServer.cpp@7:505a9a98b776, 2015-03-14 (annotated)
- Committer:
- lipoyang
- Date:
- Sat Mar 14 11:51:38 2015 +0000
- Revision:
- 7:505a9a98b776
- Parent:
- 4:ebda47d22091
- adjust servo center position
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 1:48f6e08a3ac2 | 1 | /* mbed Microcontroller Library |
jksoft | 1:48f6e08a3ac2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jksoft | 1:48f6e08a3ac2 | 3 | * |
jksoft | 1:48f6e08a3ac2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jksoft | 1:48f6e08a3ac2 | 5 | * you may not use this file except in compliance with the License. |
jksoft | 1:48f6e08a3ac2 | 6 | * You may obtain a copy of the License at |
jksoft | 1:48f6e08a3ac2 | 7 | * |
jksoft | 1:48f6e08a3ac2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jksoft | 1:48f6e08a3ac2 | 9 | * |
jksoft | 1:48f6e08a3ac2 | 10 | * Unless required by applicable law or agreed to in writing, software |
jksoft | 1:48f6e08a3ac2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jksoft | 1:48f6e08a3ac2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jksoft | 1:48f6e08a3ac2 | 13 | * See the License for the specific language governing permissions and |
jksoft | 1:48f6e08a3ac2 | 14 | * limitations under the License. |
jksoft | 1:48f6e08a3ac2 | 15 | */ |
jksoft | 1:48f6e08a3ac2 | 16 | |
jksoft | 1:48f6e08a3ac2 | 17 | #include "nRF51GattServer.h" |
jksoft | 1:48f6e08a3ac2 | 18 | #include "mbed.h" |
jksoft | 1:48f6e08a3ac2 | 19 | |
jksoft | 1:48f6e08a3ac2 | 20 | #include "common/common.h" |
jksoft | 1:48f6e08a3ac2 | 21 | #include "btle/custom/custom_helper.h" |
jksoft | 1:48f6e08a3ac2 | 22 | |
jksoft | 1:48f6e08a3ac2 | 23 | #include "nRF51Gap.h" |
jksoft | 1:48f6e08a3ac2 | 24 | |
jksoft | 1:48f6e08a3ac2 | 25 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 26 | /*! |
jksoft | 1:48f6e08a3ac2 | 27 | @brief Adds a new service to the GATT table on the peripheral |
jksoft | 1:48f6e08a3ac2 | 28 | |
jksoft | 1:48f6e08a3ac2 | 29 | @returns ble_error_t |
jksoft | 1:48f6e08a3ac2 | 30 | |
jksoft | 1:48f6e08a3ac2 | 31 | @retval BLE_ERROR_NONE |
jksoft | 1:48f6e08a3ac2 | 32 | Everything executed properly |
jksoft | 1:48f6e08a3ac2 | 33 | |
jksoft | 1:48f6e08a3ac2 | 34 | @section EXAMPLE |
jksoft | 1:48f6e08a3ac2 | 35 | |
jksoft | 1:48f6e08a3ac2 | 36 | @code |
jksoft | 1:48f6e08a3ac2 | 37 | |
jksoft | 1:48f6e08a3ac2 | 38 | @endcode |
jksoft | 1:48f6e08a3ac2 | 39 | */ |
jksoft | 1:48f6e08a3ac2 | 40 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 41 | ble_error_t nRF51GattServer::addService(GattService &service) |
jksoft | 1:48f6e08a3ac2 | 42 | { |
jksoft | 1:48f6e08a3ac2 | 43 | /* ToDo: Make sure we don't overflow the array, etc. */ |
jksoft | 1:48f6e08a3ac2 | 44 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
jksoft | 1:48f6e08a3ac2 | 45 | /* ToDo: Basic validation */ |
jksoft | 1:48f6e08a3ac2 | 46 | |
jksoft | 1:48f6e08a3ac2 | 47 | /* Add the service to the nRF51 */ |
jksoft | 1:48f6e08a3ac2 | 48 | ble_uuid_t nordicUUID; |
jksoft | 1:48f6e08a3ac2 | 49 | nordicUUID = custom_convert_to_nordic_uuid(service.getUUID()); |
jksoft | 1:48f6e08a3ac2 | 50 | |
jksoft | 1:48f6e08a3ac2 | 51 | uint16_t serviceHandle; |
jksoft | 1:48f6e08a3ac2 | 52 | ASSERT( ERROR_NONE == |
jksoft | 1:48f6e08a3ac2 | 53 | sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, |
jksoft | 1:48f6e08a3ac2 | 54 | &nordicUUID, |
jksoft | 1:48f6e08a3ac2 | 55 | &serviceHandle), |
jksoft | 1:48f6e08a3ac2 | 56 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 1:48f6e08a3ac2 | 57 | service.setHandle(serviceHandle); |
jksoft | 1:48f6e08a3ac2 | 58 | |
jksoft | 1:48f6e08a3ac2 | 59 | /* Add characteristics to the service */ |
jksoft | 1:48f6e08a3ac2 | 60 | for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { |
jksoft | 1:48f6e08a3ac2 | 61 | GattCharacteristic *p_char = service.getCharacteristic(i); |
jksoft | 1:48f6e08a3ac2 | 62 | |
jksoft | 1:48f6e08a3ac2 | 63 | nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID()); |
jksoft | 1:48f6e08a3ac2 | 64 | |
jksoft | 1:48f6e08a3ac2 | 65 | ASSERT ( ERROR_NONE == |
jksoft | 1:48f6e08a3ac2 | 66 | custom_add_in_characteristic(service.getHandle(), |
jksoft | 1:48f6e08a3ac2 | 67 | &nordicUUID, |
jksoft | 1:48f6e08a3ac2 | 68 | p_char->getProperties(), |
jksoft | 1:48f6e08a3ac2 | 69 | NULL, |
jksoft | 1:48f6e08a3ac2 | 70 | p_char->getInitialLength(), |
jksoft | 1:48f6e08a3ac2 | 71 | p_char->getMaxLength(), |
jksoft | 1:48f6e08a3ac2 | 72 | &nrfCharacteristicHandles[characteristicCount]), |
jksoft | 1:48f6e08a3ac2 | 73 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 1:48f6e08a3ac2 | 74 | |
jksoft | 1:48f6e08a3ac2 | 75 | /* Update the characteristic handle */ |
jksoft | 1:48f6e08a3ac2 | 76 | uint16_t charHandle = characteristicCount; |
jksoft | 1:48f6e08a3ac2 | 77 | p_characteristics[characteristicCount++] = p_char; |
jksoft | 1:48f6e08a3ac2 | 78 | |
jksoft | 1:48f6e08a3ac2 | 79 | p_char->setHandle(charHandle); |
jksoft | 1:48f6e08a3ac2 | 80 | if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) { |
jksoft | 1:48f6e08a3ac2 | 81 | updateValue(charHandle, p_char->getValuePtr(), p_char->getInitialLength(), false /* localOnly */); |
jksoft | 1:48f6e08a3ac2 | 82 | } |
jksoft | 1:48f6e08a3ac2 | 83 | } |
jksoft | 1:48f6e08a3ac2 | 84 | |
jksoft | 1:48f6e08a3ac2 | 85 | serviceCount++; |
jksoft | 1:48f6e08a3ac2 | 86 | |
jksoft | 1:48f6e08a3ac2 | 87 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 88 | } |
jksoft | 1:48f6e08a3ac2 | 89 | |
jksoft | 1:48f6e08a3ac2 | 90 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 91 | /*! |
jksoft | 1:48f6e08a3ac2 | 92 | @brief Reads the value of a characteristic, based on the service |
jksoft | 1:48f6e08a3ac2 | 93 | and characteristic index fields |
jksoft | 1:48f6e08a3ac2 | 94 | |
jksoft | 1:48f6e08a3ac2 | 95 | @param[in] charHandle |
jksoft | 1:48f6e08a3ac2 | 96 | The handle of the GattCharacteristic to read from |
jksoft | 1:48f6e08a3ac2 | 97 | @param[in] buffer |
jksoft | 1:48f6e08a3ac2 | 98 | Buffer to hold the the characteristic's value |
jksoft | 1:48f6e08a3ac2 | 99 | (raw byte array in LSB format) |
jksoft | 1:48f6e08a3ac2 | 100 | @param[in] len |
jksoft | 1:48f6e08a3ac2 | 101 | The number of bytes read into the buffer |
jksoft | 1:48f6e08a3ac2 | 102 | |
jksoft | 1:48f6e08a3ac2 | 103 | @returns ble_error_t |
jksoft | 1:48f6e08a3ac2 | 104 | |
jksoft | 1:48f6e08a3ac2 | 105 | @retval BLE_ERROR_NONE |
jksoft | 1:48f6e08a3ac2 | 106 | Everything executed properly |
jksoft | 1:48f6e08a3ac2 | 107 | |
jksoft | 1:48f6e08a3ac2 | 108 | @section EXAMPLE |
jksoft | 1:48f6e08a3ac2 | 109 | |
jksoft | 1:48f6e08a3ac2 | 110 | @code |
jksoft | 1:48f6e08a3ac2 | 111 | |
jksoft | 1:48f6e08a3ac2 | 112 | @endcode |
jksoft | 1:48f6e08a3ac2 | 113 | */ |
jksoft | 1:48f6e08a3ac2 | 114 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 115 | ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) |
jksoft | 1:48f6e08a3ac2 | 116 | { |
jksoft | 1:48f6e08a3ac2 | 117 | ASSERT( ERROR_NONE == |
jksoft | 1:48f6e08a3ac2 | 118 | sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, lengthP, buffer), |
jksoft | 1:48f6e08a3ac2 | 119 | BLE_ERROR_PARAM_OUT_OF_RANGE); |
jksoft | 1:48f6e08a3ac2 | 120 | |
jksoft | 1:48f6e08a3ac2 | 121 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 122 | } |
jksoft | 1:48f6e08a3ac2 | 123 | |
jksoft | 1:48f6e08a3ac2 | 124 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 125 | /*! |
jksoft | 1:48f6e08a3ac2 | 126 | @brief Updates the value of a characteristic, based on the service |
jksoft | 1:48f6e08a3ac2 | 127 | and characteristic index fields |
jksoft | 1:48f6e08a3ac2 | 128 | |
jksoft | 1:48f6e08a3ac2 | 129 | @param[in] charHandle |
jksoft | 1:48f6e08a3ac2 | 130 | The handle of the GattCharacteristic to write to |
jksoft | 1:48f6e08a3ac2 | 131 | @param[in] buffer |
jksoft | 1:48f6e08a3ac2 | 132 | Data to use when updating the characteristic's value |
jksoft | 1:48f6e08a3ac2 | 133 | (raw byte array in LSB format) |
jksoft | 1:48f6e08a3ac2 | 134 | @param[in] len |
jksoft | 1:48f6e08a3ac2 | 135 | The number of bytes in buffer |
jksoft | 1:48f6e08a3ac2 | 136 | |
jksoft | 1:48f6e08a3ac2 | 137 | @returns ble_error_t |
jksoft | 1:48f6e08a3ac2 | 138 | |
jksoft | 1:48f6e08a3ac2 | 139 | @retval BLE_ERROR_NONE |
jksoft | 1:48f6e08a3ac2 | 140 | Everything executed properly |
jksoft | 1:48f6e08a3ac2 | 141 | |
jksoft | 1:48f6e08a3ac2 | 142 | @section EXAMPLE |
jksoft | 1:48f6e08a3ac2 | 143 | |
jksoft | 1:48f6e08a3ac2 | 144 | @code |
jksoft | 1:48f6e08a3ac2 | 145 | |
jksoft | 1:48f6e08a3ac2 | 146 | @endcode |
jksoft | 1:48f6e08a3ac2 | 147 | */ |
jksoft | 1:48f6e08a3ac2 | 148 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 149 | ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) |
jksoft | 1:48f6e08a3ac2 | 150 | { |
jksoft | 1:48f6e08a3ac2 | 151 | uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle(); |
jksoft | 1:48f6e08a3ac2 | 152 | |
jksoft | 1:48f6e08a3ac2 | 153 | if (localOnly) { |
jksoft | 1:48f6e08a3ac2 | 154 | /* Only update locally regardless of notify/indicate */ |
jksoft | 1:48f6e08a3ac2 | 155 | ASSERT_INT( ERROR_NONE, |
jksoft | 1:48f6e08a3ac2 | 156 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
jksoft | 1:48f6e08a3ac2 | 157 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 1:48f6e08a3ac2 | 158 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 159 | } |
jksoft | 1:48f6e08a3ac2 | 160 | |
jksoft | 1:48f6e08a3ac2 | 161 | if ((p_characteristics[charHandle]->getProperties() & |
jksoft | 1:48f6e08a3ac2 | 162 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | |
jksoft | 1:48f6e08a3ac2 | 163 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
jksoft | 1:48f6e08a3ac2 | 164 | (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { |
jksoft | 1:48f6e08a3ac2 | 165 | /* HVX update for the characteristic value */ |
jksoft | 1:48f6e08a3ac2 | 166 | ble_gatts_hvx_params_t hvx_params; |
jksoft | 1:48f6e08a3ac2 | 167 | |
jksoft | 1:48f6e08a3ac2 | 168 | hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; |
jksoft | 1:48f6e08a3ac2 | 169 | hvx_params.type = |
jksoft | 1:48f6e08a3ac2 | 170 | (p_characteristics[charHandle]->getProperties() & |
jksoft | 1:48f6e08a3ac2 | 171 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? |
jksoft | 1:48f6e08a3ac2 | 172 | BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; |
jksoft | 1:48f6e08a3ac2 | 173 | hvx_params.offset = 0; |
jksoft | 1:48f6e08a3ac2 | 174 | hvx_params.p_data = buffer; |
jksoft | 1:48f6e08a3ac2 | 175 | hvx_params.p_len = &len; |
jksoft | 1:48f6e08a3ac2 | 176 | |
jksoft | 1:48f6e08a3ac2 | 177 | error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); |
jksoft | 1:48f6e08a3ac2 | 178 | |
jksoft | 1:48f6e08a3ac2 | 179 | /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and |
jksoft | 1:48f6e08a3ac2 | 180 | *ERROR_NO_TX_BUFFERS the ATT table has been updated. */ |
jksoft | 1:48f6e08a3ac2 | 181 | if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) && |
jksoft | 1:48f6e08a3ac2 | 182 | (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && |
jksoft | 1:48f6e08a3ac2 | 183 | (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { |
jksoft | 1:48f6e08a3ac2 | 184 | ASSERT_INT( ERROR_NONE, |
jksoft | 1:48f6e08a3ac2 | 185 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
jksoft | 1:48f6e08a3ac2 | 186 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 1:48f6e08a3ac2 | 187 | } |
jksoft | 1:48f6e08a3ac2 | 188 | } else { |
jksoft | 1:48f6e08a3ac2 | 189 | ASSERT_INT( ERROR_NONE, |
jksoft | 1:48f6e08a3ac2 | 190 | sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), |
jksoft | 1:48f6e08a3ac2 | 191 | BLE_ERROR_PARAM_OUT_OF_RANGE ); |
jksoft | 1:48f6e08a3ac2 | 192 | } |
jksoft | 1:48f6e08a3ac2 | 193 | |
jksoft | 1:48f6e08a3ac2 | 194 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 195 | } |
jksoft | 1:48f6e08a3ac2 | 196 | |
jksoft | 1:48f6e08a3ac2 | 197 | ble_error_t nRF51GattServer::setDeviceName(const uint8_t *deviceName) |
jksoft | 1:48f6e08a3ac2 | 198 | { |
jksoft | 1:48f6e08a3ac2 | 199 | ble_gap_conn_sec_mode_t sec_mode; |
jksoft | 1:48f6e08a3ac2 | 200 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed |
jksoft | 1:48f6e08a3ac2 | 201 | |
jksoft | 1:48f6e08a3ac2 | 202 | if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) { |
jksoft | 1:48f6e08a3ac2 | 203 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 204 | } else { |
jksoft | 1:48f6e08a3ac2 | 205 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
jksoft | 1:48f6e08a3ac2 | 206 | } |
jksoft | 1:48f6e08a3ac2 | 207 | } |
jksoft | 1:48f6e08a3ac2 | 208 | |
jksoft | 1:48f6e08a3ac2 | 209 | ble_error_t nRF51GattServer::getDeviceName(uint8_t *deviceName, unsigned *lengthP) |
jksoft | 1:48f6e08a3ac2 | 210 | { |
jksoft | 1:48f6e08a3ac2 | 211 | if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { |
jksoft | 1:48f6e08a3ac2 | 212 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 213 | } else { |
jksoft | 1:48f6e08a3ac2 | 214 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
jksoft | 1:48f6e08a3ac2 | 215 | } |
jksoft | 1:48f6e08a3ac2 | 216 | } |
jksoft | 1:48f6e08a3ac2 | 217 | |
jksoft | 1:48f6e08a3ac2 | 218 | ble_error_t nRF51GattServer::setAppearance(uint16_t appearance) |
jksoft | 1:48f6e08a3ac2 | 219 | { |
jksoft | 1:48f6e08a3ac2 | 220 | if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { |
jksoft | 1:48f6e08a3ac2 | 221 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 222 | } else { |
jksoft | 1:48f6e08a3ac2 | 223 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
jksoft | 1:48f6e08a3ac2 | 224 | } |
jksoft | 1:48f6e08a3ac2 | 225 | } |
jksoft | 1:48f6e08a3ac2 | 226 | |
jksoft | 1:48f6e08a3ac2 | 227 | ble_error_t nRF51GattServer::getAppearance(uint16_t *appearanceP) |
jksoft | 1:48f6e08a3ac2 | 228 | { |
jksoft | 1:48f6e08a3ac2 | 229 | if (sd_ble_gap_appearance_get(appearanceP)) { |
jksoft | 1:48f6e08a3ac2 | 230 | return BLE_ERROR_NONE; |
jksoft | 1:48f6e08a3ac2 | 231 | } else { |
jksoft | 1:48f6e08a3ac2 | 232 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
jksoft | 1:48f6e08a3ac2 | 233 | } |
jksoft | 1:48f6e08a3ac2 | 234 | } |
jksoft | 1:48f6e08a3ac2 | 235 | |
jksoft | 1:48f6e08a3ac2 | 236 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 237 | /*! |
jksoft | 1:48f6e08a3ac2 | 238 | @brief Callback handler for events getting pushed up from the SD |
jksoft | 1:48f6e08a3ac2 | 239 | */ |
jksoft | 1:48f6e08a3ac2 | 240 | /**************************************************************************/ |
jksoft | 1:48f6e08a3ac2 | 241 | void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 242 | { |
jksoft | 1:48f6e08a3ac2 | 243 | uint16_t handle_value; |
jksoft | 1:48f6e08a3ac2 | 244 | GattServerEvents::gattEvent_t event; |
jksoft | 1:48f6e08a3ac2 | 245 | |
jksoft | 1:48f6e08a3ac2 | 246 | switch (p_ble_evt->header.evt_id) { |
jksoft | 1:48f6e08a3ac2 | 247 | case BLE_GATTS_EVT_WRITE: |
jksoft | 1:48f6e08a3ac2 | 248 | /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ |
jksoft | 1:48f6e08a3ac2 | 249 | |
jksoft | 1:48f6e08a3ac2 | 250 | /* 1.) Handle CCCD changes */ |
jksoft | 1:48f6e08a3ac2 | 251 | handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; |
jksoft | 1:48f6e08a3ac2 | 252 | for (uint8_t i = 0; i<characteristicCount; i++) { |
jksoft | 1:48f6e08a3ac2 | 253 | if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && |
jksoft | 1:48f6e08a3ac2 | 254 | (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { |
jksoft | 1:48f6e08a3ac2 | 255 | uint16_t cccd_value = |
jksoft | 1:48f6e08a3ac2 | 256 | (p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) | |
jksoft | 1:48f6e08a3ac2 | 257 | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ |
jksoft | 1:48f6e08a3ac2 | 258 | |
jksoft | 1:48f6e08a3ac2 | 259 | if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) || |
jksoft | 1:48f6e08a3ac2 | 260 | ((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { |
jksoft | 1:48f6e08a3ac2 | 261 | event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; |
jksoft | 1:48f6e08a3ac2 | 262 | } else { |
jksoft | 1:48f6e08a3ac2 | 263 | event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; |
jksoft | 1:48f6e08a3ac2 | 264 | } |
jksoft | 1:48f6e08a3ac2 | 265 | |
jksoft | 1:48f6e08a3ac2 | 266 | handleEvent(event, i); |
jksoft | 1:48f6e08a3ac2 | 267 | return; |
jksoft | 1:48f6e08a3ac2 | 268 | } |
jksoft | 1:48f6e08a3ac2 | 269 | } |
jksoft | 1:48f6e08a3ac2 | 270 | |
jksoft | 1:48f6e08a3ac2 | 271 | /* 2.) Changes to the characteristic value will be handled with other events below */ |
jksoft | 1:48f6e08a3ac2 | 272 | event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; |
jksoft | 1:48f6e08a3ac2 | 273 | break; |
jksoft | 1:48f6e08a3ac2 | 274 | |
jksoft | 1:48f6e08a3ac2 | 275 | case BLE_EVT_TX_COMPLETE: |
jksoft | 1:48f6e08a3ac2 | 276 | handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT); |
jksoft | 1:48f6e08a3ac2 | 277 | return; |
jksoft | 1:48f6e08a3ac2 | 278 | |
jksoft | 1:48f6e08a3ac2 | 279 | case BLE_GATTS_EVT_HVC: |
jksoft | 1:48f6e08a3ac2 | 280 | /* Indication confirmation received */ |
jksoft | 1:48f6e08a3ac2 | 281 | event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; |
jksoft | 1:48f6e08a3ac2 | 282 | handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; |
jksoft | 1:48f6e08a3ac2 | 283 | break; |
jksoft | 1:48f6e08a3ac2 | 284 | |
jksoft | 1:48f6e08a3ac2 | 285 | case BLE_GATTS_EVT_SYS_ATTR_MISSING: |
jksoft | 1:48f6e08a3ac2 | 286 | sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0); |
jksoft | 1:48f6e08a3ac2 | 287 | return; |
jksoft | 1:48f6e08a3ac2 | 288 | |
jksoft | 1:48f6e08a3ac2 | 289 | default: |
jksoft | 1:48f6e08a3ac2 | 290 | return; |
jksoft | 1:48f6e08a3ac2 | 291 | } |
jksoft | 1:48f6e08a3ac2 | 292 | |
jksoft | 1:48f6e08a3ac2 | 293 | /* Find index (charHandle) in the pool */ |
jksoft | 1:48f6e08a3ac2 | 294 | for (uint8_t i = 0; i<characteristicCount; i++) { |
jksoft | 1:48f6e08a3ac2 | 295 | if (nrfCharacteristicHandles[i].value_handle == handle_value) { |
jksoft | 1:48f6e08a3ac2 | 296 | handleEvent(event, i); |
jksoft | 1:48f6e08a3ac2 | 297 | break; |
jksoft | 1:48f6e08a3ac2 | 298 | } |
jksoft | 1:48f6e08a3ac2 | 299 | } |
jksoft | 1:48f6e08a3ac2 | 300 | } |