High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Tue Dec 10 07:32:12 2013 +0000
Revision:
2:ffc5216bd2cc
Child:
11:200931be5617
UART Tests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 2:ffc5216bd2cc 1 #include <stdio.h>
ktownsend 2:ffc5216bd2cc 2 #include <string.h>
ktownsend 2:ffc5216bd2cc 3
ktownsend 2:ffc5216bd2cc 4 #include "GattService.h"
ktownsend 2:ffc5216bd2cc 5
ktownsend 2:ffc5216bd2cc 6 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 7 /*!
ktownsend 2:ffc5216bd2cc 8 @brief Creates a new GattService using the specified 128-bit UUID
ktownsend 2:ffc5216bd2cc 9
ktownsend 2:ffc5216bd2cc 10 @note The UUID value must be unique on the device
ktownsend 2:ffc5216bd2cc 11
ktownsend 2:ffc5216bd2cc 12 @param[in] uuid
ktownsend 2:ffc5216bd2cc 13 The 16 byte (128-bit) UUID to use for this characteristic
ktownsend 2:ffc5216bd2cc 14
ktownsend 2:ffc5216bd2cc 15 @section EXAMPLE
ktownsend 2:ffc5216bd2cc 16
ktownsend 2:ffc5216bd2cc 17 @code
ktownsend 2:ffc5216bd2cc 18
ktownsend 2:ffc5216bd2cc 19 @endcode
ktownsend 2:ffc5216bd2cc 20 */
ktownsend 2:ffc5216bd2cc 21 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 22 GattService::GattService(uint8_t base_uuid[16])
ktownsend 2:ffc5216bd2cc 23 {
ktownsend 2:ffc5216bd2cc 24 primaryServiceID.update(base_uuid);
ktownsend 2:ffc5216bd2cc 25 characteristicCount = 0;
ktownsend 2:ffc5216bd2cc 26 memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
ktownsend 2:ffc5216bd2cc 27 index = 0;
ktownsend 2:ffc5216bd2cc 28 }
ktownsend 2:ffc5216bd2cc 29
ktownsend 2:ffc5216bd2cc 30 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 31 /*!
ktownsend 2:ffc5216bd2cc 32 @brief Creates a new GattService using the specified 16-bit BLE UUID
ktownsend 2:ffc5216bd2cc 33
ktownsend 2:ffc5216bd2cc 34 @param[in] ble_uuid
ktownsend 2:ffc5216bd2cc 35 The standardised 16-bit (2 byte) BLE UUID to use for this
ktownsend 2:ffc5216bd2cc 36 characteristic
ktownsend 2:ffc5216bd2cc 37
ktownsend 2:ffc5216bd2cc 38 @section EXAMPLE
ktownsend 2:ffc5216bd2cc 39
ktownsend 2:ffc5216bd2cc 40 @code
ktownsend 2:ffc5216bd2cc 41
ktownsend 2:ffc5216bd2cc 42 @endcode
ktownsend 2:ffc5216bd2cc 43 */
ktownsend 2:ffc5216bd2cc 44 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 45 GattService::GattService(uint16_t ble_uuid)
ktownsend 2:ffc5216bd2cc 46 {
ktownsend 2:ffc5216bd2cc 47 primaryServiceID.update( ble_uuid );
ktownsend 2:ffc5216bd2cc 48 characteristicCount = 0;
ktownsend 2:ffc5216bd2cc 49 memset(&characteristics, 0, sizeof(serialisedChar_t) * BLE_SERVICE_MAX_CHARACTERISTICS);
ktownsend 2:ffc5216bd2cc 50 index = 0;
ktownsend 2:ffc5216bd2cc 51 }
ktownsend 2:ffc5216bd2cc 52
ktownsend 2:ffc5216bd2cc 53 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 54 /*!
ktownsend 2:ffc5216bd2cc 55 @brief Destructor
ktownsend 2:ffc5216bd2cc 56 */
ktownsend 2:ffc5216bd2cc 57 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 58 GattService::~GattService(void)
ktownsend 2:ffc5216bd2cc 59 {
ktownsend 2:ffc5216bd2cc 60 }
ktownsend 2:ffc5216bd2cc 61
ktownsend 2:ffc5216bd2cc 62 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 63 /*!
ktownsend 2:ffc5216bd2cc 64 @brief Adds a GattCharacterisic to the service, serialising the
ktownsend 2:ffc5216bd2cc 65 essential data for the characteristic.
ktownsend 2:ffc5216bd2cc 66
ktownsend 2:ffc5216bd2cc 67 @note The GattService does not store a reference to the source
ktownsend 2:ffc5216bd2cc 68 GattCharacteristic, only a serialised version of the key
ktownsend 2:ffc5216bd2cc 69 properties required to create the characteristic on the
ktownsend 2:ffc5216bd2cc 70 target radio board.
ktownsend 2:ffc5216bd2cc 71
ktownsend 2:ffc5216bd2cc 72 @note This function will update the .index field in the
ktownsend 2:ffc5216bd2cc 73 GattCharacteristic to indicate where this characteristic was
ktownsend 2:ffc5216bd2cc 74 stored in the GattService's characteristic array.
ktownsend 2:ffc5216bd2cc 75
ktownsend 2:ffc5216bd2cc 76 @param[in] characteristic
ktownsend 2:ffc5216bd2cc 77 The GattCharacteristic object describing the characteristic
ktownsend 2:ffc5216bd2cc 78 to add to this service
ktownsend 2:ffc5216bd2cc 79
ktownsend 2:ffc5216bd2cc 80 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 2:ffc5216bd2cc 81 error code if there was a problem
ktownsend 2:ffc5216bd2cc 82 @retval BLE_ERROR_NONE
ktownsend 2:ffc5216bd2cc 83 Everything executed correctly
ktownsend 2:ffc5216bd2cc 84
ktownsend 2:ffc5216bd2cc 85 @section EXAMPLE
ktownsend 2:ffc5216bd2cc 86
ktownsend 2:ffc5216bd2cc 87 @code
ktownsend 2:ffc5216bd2cc 88
ktownsend 2:ffc5216bd2cc 89 @endcode
ktownsend 2:ffc5216bd2cc 90 */
ktownsend 2:ffc5216bd2cc 91 /**************************************************************************/
ktownsend 2:ffc5216bd2cc 92 ble_error_t GattService::addCharacteristic(GattCharacteristic & characteristic)
ktownsend 2:ffc5216bd2cc 93 {
ktownsend 2:ffc5216bd2cc 94 /* ToDo: Make sure we don't overflow the array, etc. */
ktownsend 2:ffc5216bd2cc 95 /* ToDo: Make sure this characteristic UUID doesn't already exist */
ktownsend 2:ffc5216bd2cc 96 /* ToDo: Basic validation */
ktownsend 2:ffc5216bd2cc 97
ktownsend 2:ffc5216bd2cc 98 serialisedChar_t c;
ktownsend 2:ffc5216bd2cc 99
ktownsend 2:ffc5216bd2cc 100 /* Serialise the source GattCharacteristic */
ktownsend 2:ffc5216bd2cc 101 memcpy(&c.id, &characteristic.uuid, 2);
ktownsend 2:ffc5216bd2cc 102 memcpy(&c.lenMin, &characteristic.lenMin, 2);
ktownsend 2:ffc5216bd2cc 103 memcpy(&c.lenMax, &characteristic.lenMax, 2);
ktownsend 2:ffc5216bd2cc 104 memcpy(&c.properties, &characteristic.properties, 2);
ktownsend 2:ffc5216bd2cc 105 memset(&c.reserved, 0, 1);
ktownsend 2:ffc5216bd2cc 106
ktownsend 2:ffc5216bd2cc 107 /* Insert the serialised object into the buffer */
ktownsend 2:ffc5216bd2cc 108 memcpy(&characteristics[characteristicCount], &c, sizeof(serialisedChar_t));
ktownsend 2:ffc5216bd2cc 109
ktownsend 2:ffc5216bd2cc 110 /* Update the index value */
ktownsend 2:ffc5216bd2cc 111 characteristic.index = characteristicCount;
ktownsend 2:ffc5216bd2cc 112
ktownsend 2:ffc5216bd2cc 113 characteristicCount++;
ktownsend 2:ffc5216bd2cc 114
ktownsend 2:ffc5216bd2cc 115 return BLE_ERROR_NONE;
ktownsend 2:ffc5216bd2cc 116 }