テスト用

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Child:
1:f84abedbf4fb
Initial import of the nRF51822 code

Who changed what in which revision?

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