Microbug / BLE_API

Fork of BLE_API by Bluetooth Low Energy

Committer:
ktownsend
Date:
Fri Nov 29 14:42:22 2013 +0000
Revision:
0:ace2e8d3ce79
First commit

Who changed what in which revision?

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