changed low freq. clock source to IRC
Dependents: BLE_ANCS_SDAPI_IRC
Fork of nRF51822 by
nRF51GattServer.cpp
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 #include "nRF51GattServer.h" 00018 #include "mbed.h" 00019 00020 #include "common/common.h " 00021 #include "btle/custom/custom_helper.h" 00022 00023 #include "nRF51Gap.h" 00024 00025 /**************************************************************************/ 00026 /*! 00027 @brief Adds a new service to the GATT table on the peripheral 00028 00029 @returns ble_error_t 00030 00031 @retval BLE_ERROR_NONE 00032 Everything executed properly 00033 00034 @section EXAMPLE 00035 00036 @code 00037 00038 @endcode 00039 */ 00040 /**************************************************************************/ 00041 ble_error_t nRF51GattServer::addService(GattService & service) 00042 { 00043 /* ToDo: Make sure we don't overflow the array, etc. */ 00044 /* ToDo: Make sure this service UUID doesn't already exist (?) */ 00045 /* ToDo: Basic validation */ 00046 00047 /* Add the service to the nRF51 */ 00048 ble_uuid_t nordicUUID; 00049 nordicUUID = custom_convert_to_nordic_uuid(service.primaryServiceID); 00050 ASSERT( ERROR_NONE == 00051 sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, 00052 &nordicUUID, 00053 &service.handle), 00054 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00055 00056 /* Add characteristics to the service */ 00057 for (uint8_t i = 0; i < service.characteristicCount; i++) { 00058 GattCharacteristic *p_char = service.characteristics[i]; 00059 00060 nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID()); 00061 00062 ASSERT ( ERROR_NONE == 00063 custom_add_in_characteristic(service.handle, 00064 &nordicUUID, 00065 p_char->getProperties(), 00066 NULL, 00067 p_char->getMinLength(), 00068 p_char->getMaxLength(), 00069 &nrfCharacteristicHandles[ 00070 characteristicCount]), 00071 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00072 00073 /* Update the characteristic handle */ 00074 p_char->setHandle(characteristicCount); 00075 p_characteristics[characteristicCount++] = p_char; 00076 } 00077 00078 serviceCount++; 00079 00080 return BLE_ERROR_NONE; 00081 } 00082 00083 /**************************************************************************/ 00084 /*! 00085 @brief Reads the value of a characteristic, based on the service 00086 and characteristic index fields 00087 00088 @param[in] charHandle 00089 The handle of the GattCharacteristic to read from 00090 @param[in] buffer 00091 Buffer to hold the the characteristic's value 00092 (raw byte array in LSB format) 00093 @param[in] len 00094 The number of bytes read into the buffer 00095 00096 @returns ble_error_t 00097 00098 @retval BLE_ERROR_NONE 00099 Everything executed properly 00100 00101 @section EXAMPLE 00102 00103 @code 00104 00105 @endcode 00106 */ 00107 /**************************************************************************/ 00108 ble_error_t nRF51GattServer::readValue(uint16_t charHandle, 00109 uint8_t buffer[], 00110 uint16_t len) 00111 { 00112 ASSERT( ERROR_NONE == 00113 sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle]. 00114 value_handle, 0, 00115 &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); 00116 00117 return BLE_ERROR_NONE; 00118 } 00119 00120 /**************************************************************************/ 00121 /*! 00122 @brief Updates the value of a characteristic, based on the service 00123 and characteristic index fields 00124 00125 @param[in] charHandle 00126 The handle of the GattCharacteristic to write to 00127 @param[in] buffer 00128 Data to use when updating the characteristic's value 00129 (raw byte array in LSB format) 00130 @param[in] len 00131 The number of bytes in buffer 00132 00133 @returns ble_error_t 00134 00135 @retval BLE_ERROR_NONE 00136 Everything executed properly 00137 00138 @section EXAMPLE 00139 00140 @code 00141 00142 @endcode 00143 */ 00144 /**************************************************************************/ 00145 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, 00146 uint8_t buffer[], 00147 uint16_t len, 00148 bool localOnly) 00149 { 00150 uint16_t gapConnectionHandle = 00151 nRF51Gap::getInstance().getConnectionHandle(); 00152 00153 if (localOnly) { 00154 /* Only update locally regardless of notify/indicate */ 00155 ASSERT_INT( ERROR_NONE, 00156 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle]. 00157 value_handle, 00158 0, 00159 &len, 00160 buffer), 00161 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00162 } 00163 00164 if ((p_characteristics[charHandle]->getProperties() & 00165 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | 00166 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && 00167 (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { 00168 /* HVX update for the characteristic value */ 00169 ble_gatts_hvx_params_t hvx_params; 00170 00171 hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; 00172 hvx_params.type = 00173 (p_characteristics[charHandle]->getProperties() & 00174 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? 00175 BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; 00176 hvx_params.offset = 0; 00177 hvx_params.p_data = buffer; 00178 hvx_params.p_len = &len; 00179 00180 error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, 00181 &hvx_params); 00182 00183 /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and 00184 *ERROR_NO_TX_BUFFERS the ATT table has been updated. */ 00185 if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) && 00186 (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && 00187 (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { 00188 ASSERT_INT( ERROR_NONE, 00189 sd_ble_gatts_value_set(nrfCharacteristicHandles[ 00190 charHandle]. 00191 value_handle, 00192 0, 00193 &len, 00194 buffer), 00195 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00196 } 00197 } else { 00198 ASSERT_INT( ERROR_NONE, 00199 sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle]. 00200 value_handle, 00201 0, 00202 &len, 00203 buffer), 00204 BLE_ERROR_PARAM_OUT_OF_RANGE ); 00205 } 00206 00207 return BLE_ERROR_NONE; 00208 } 00209 00210 /**************************************************************************/ 00211 /*! 00212 @brief Callback handler for events getting pushed up from the SD 00213 */ 00214 /**************************************************************************/ 00215 void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) 00216 { 00217 uint16_t handle_value; 00218 GattServerEvents::gattEvent_t event; 00219 00220 switch (p_ble_evt->header.evt_id) { 00221 case BLE_GATTS_EVT_WRITE: 00222 /* There are 2 use case here: Values being updated & CCCD 00223 *(indicate/notify) enabled */ 00224 00225 /* 1.) Handle CCCD changes */ 00226 handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; 00227 for (uint8_t i = 0; i<characteristicCount; i++) { 00228 if ((p_characteristics[i]->getProperties() & 00229 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | 00230 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && 00231 (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { 00232 uint16_t cccd_value = 00233 (p_ble_evt->evt.gatts_evt.params.write.data[1] << 00234 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* 00235 * Little Endian but M0 may 00236 * be mis-aligned */ 00237 00238 if (((p_characteristics[i]->getProperties() & 00239 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && 00240 (cccd_value & BLE_GATT_HVX_INDICATION)) || 00241 ((p_characteristics[i]->getProperties() & 00242 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && 00243 (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { 00244 event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; 00245 } else { 00246 event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; 00247 } 00248 00249 handleEvent(event, i); 00250 return; 00251 } 00252 } 00253 00254 /* 2.) Changes to the characteristic value will be handled with other 00255 *events below */ 00256 event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; 00257 break; 00258 00259 case BLE_GATTS_EVT_HVC: 00260 /* Indication confirmation received */ 00261 event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; 00262 handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; 00263 break; 00264 00265 default: 00266 return; 00267 } 00268 00269 /* Find index (charHandle) in the pool */ 00270 for (uint8_t i = 0; i<characteristicCount; i++) { 00271 if (nrfCharacteristicHandles[i].value_handle == handle_value) { 00272 handleEvent(event, i); 00273 break; 00274 } 00275 } 00276 }
Generated on Tue Jul 12 2022 16:36:22 by 1.7.2