High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
Rohit Grover
Date:
Wed May 28 15:25:55 2014 +0100
Revision:
50:9078969e80e4
Parent:
43:ac154ac74a30
Child:
53:a1bec483c8e3
define LENGTH_OF_LONG_UUID as a constant

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 27:4a83843f04b0 1 /* mbed Microcontroller Library
ktownsend 27:4a83843f04b0 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 27:4a83843f04b0 3 *
ktownsend 27:4a83843f04b0 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 27:4a83843f04b0 5 * you may not use this file except in compliance with the License.
ktownsend 27:4a83843f04b0 6 * You may obtain a copy of the License at
ktownsend 27:4a83843f04b0 7 *
ktownsend 27:4a83843f04b0 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 27:4a83843f04b0 9 *
ktownsend 27:4a83843f04b0 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 27:4a83843f04b0 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 27:4a83843f04b0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 27:4a83843f04b0 13 * See the License for the specific language governing permissions and
ktownsend 27:4a83843f04b0 14 * limitations under the License.
ktownsend 27:4a83843f04b0 15 */
Rohit Grover 34:da2ea8cd6216 16
ktownsend 27:4a83843f04b0 17
ktownsend 0:ace2e8d3ce79 18 #include <stdio.h>
ktownsend 0:ace2e8d3ce79 19 #include <string.h>
ktownsend 0:ace2e8d3ce79 20
ktownsend 27:4a83843f04b0 21 #include "UUID.h"
ktownsend 0:ace2e8d3ce79 22
ktownsend 0:ace2e8d3ce79 23 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 24 /*!
ktownsend 0:ace2e8d3ce79 25 @brief Creates an empty 128-bit UUID
Rohit Grover 34:da2ea8cd6216 26
ktownsend 0:ace2e8d3ce79 27 @note This UUID must be assigned a valid value via the 'update'
ktownsend 0:ace2e8d3ce79 28 function before it can be safely used!
ktownsend 0:ace2e8d3ce79 29 */
ktownsend 0:ace2e8d3ce79 30 /**************************************************************************/
Rohit Grover 50:9078969e80e4 31 UUID::UUID(void) : type(UUID_TYPE_SHORT),
Rohit Grover 50:9078969e80e4 32 base(),
Rohit Grover 50:9078969e80e4 33 value(0)
ktownsend 0:ace2e8d3ce79 34 {
Rohit Grover 50:9078969e80e4 35 /* empty */
ktownsend 0:ace2e8d3ce79 36 }
ktownsend 0:ace2e8d3ce79 37
ktownsend 0:ace2e8d3ce79 38 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 39 /*!
ktownsend 0:ace2e8d3ce79 40 @brief Creates a new 128-bit UUID
Rohit Grover 34:da2ea8cd6216 41
ktownsend 0:ace2e8d3ce79 42 @note The UUID is a unique 128-bit (16 byte) ID used to identify
ktownsend 0:ace2e8d3ce79 43 different service or characteristics on the BLE device.
Rohit Grover 34:da2ea8cd6216 44
ktownsend 0:ace2e8d3ce79 45 @note When creating a UUID, the constructor will check if all bytes
ktownsend 0:ace2e8d3ce79 46 except bytes 2/3 are equal to 0. If only bytes 2/3 have a
ktownsend 0:ace2e8d3ce79 47 value, the UUID will be treated as a short/BLE UUID, and the
ktownsend 0:ace2e8d3ce79 48 .type field will be set to UUID::UUID_TYPE_SHORT. If any
ktownsend 0:ace2e8d3ce79 49 of the bytes outside byte 2/3 have a non-zero value, the UUID
Rohit Grover 34:da2ea8cd6216 50 will be considered a 128-bit ID, and .type will be assigned
Rohit Grover 34:da2ea8cd6216 51 as UUID::UUID_TYPE_LONG.
ktownsend 0:ace2e8d3ce79 52
ktownsend 0:ace2e8d3ce79 53 @param[in] uuid_base
ktownsend 0:ace2e8d3ce79 54 The 128-bit (16-byte) UUID value. For 128-bit values,
ktownsend 0:ace2e8d3ce79 55 assign all 16 bytes. For 16-bit values, assign the
ktownsend 0:ace2e8d3ce79 56 16-bits to byte 2 and 3, and leave the rest of the bytes
ktownsend 0:ace2e8d3ce79 57 as 0.
ktownsend 0:ace2e8d3ce79 58
ktownsend 0:ace2e8d3ce79 59 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 60
ktownsend 0:ace2e8d3ce79 61 @code
ktownsend 0:ace2e8d3ce79 62
ktownsend 0:ace2e8d3ce79 63 // Create a short UUID (0x180F)
ktownsend 0:ace2e8d3ce79 64 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ktownsend 0:ace2e8d3ce79 65 UUID ble_uuid = UUID(shortID);
ktownsend 0:ace2e8d3ce79 66 // ble_uuid.type = UUID_TYPE_SHORT
ktownsend 0:ace2e8d3ce79 67 // ble_uuid.value = 0x180F
Rohit Grover 34:da2ea8cd6216 68
ktownsend 0:ace2e8d3ce79 69 // Creeate a long UUID
ktownsend 0:ace2e8d3ce79 70 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
Rohit Grover 34:da2ea8cd6216 71 0x44, 0x55, 0x66, 0x77,
Rohit Grover 34:da2ea8cd6216 72 0x88, 0x99, 0xAA, 0xBB,
ktownsend 0:ace2e8d3ce79 73 0xCC, 0xDD, 0xEE, 0xFF };
ktownsend 0:ace2e8d3ce79 74 UUID custom_uuid = UUID(longID);
ktownsend 0:ace2e8d3ce79 75 // custom_uuid.type = UUID_TYPE_LONG
ktownsend 0:ace2e8d3ce79 76 // custom_uuid.value = 0x3322
ktownsend 0:ace2e8d3ce79 77 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
Rohit Grover 34:da2ea8cd6216 78
ktownsend 0:ace2e8d3ce79 79 @endcode
ktownsend 0:ace2e8d3ce79 80 */
ktownsend 0:ace2e8d3ce79 81 /**************************************************************************/
Rohit Grover 50:9078969e80e4 82 UUID::UUID(const uint8_t uuid_base[LENGTH_OF_LONG_UUID]) :
Rohit Grover 50:9078969e80e4 83 type(UUID_TYPE_SHORT),
Rohit Grover 50:9078969e80e4 84 base(),
Rohit Grover 50:9078969e80e4 85 value(0)
ktownsend 0:ace2e8d3ce79 86 {
Rohit Grover 50:9078969e80e4 87 memcpy(base, uuid_base, LENGTH_OF_LONG_UUID);
ktownsend 0:ace2e8d3ce79 88 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:ace2e8d3ce79 89
ktownsend 0:ace2e8d3ce79 90 /* Check if this is a short of a long UUID */
Rohit Grover 34:da2ea8cd6216 91 if (uuid_base[0] + uuid_base[1] +
Rohit Grover 34:da2ea8cd6216 92 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
Rohit Grover 34:da2ea8cd6216 93 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
Rohit Grover 34:da2ea8cd6216 94 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) {
Rohit Grover 34:da2ea8cd6216 95 type = UUID_TYPE_SHORT;
Rohit Grover 43:ac154ac74a30 96 } else {
ktownsend 0:ace2e8d3ce79 97 type = UUID_TYPE_LONG;
ktownsend 0:ace2e8d3ce79 98 }
ktownsend 0:ace2e8d3ce79 99 }
ktownsend 0:ace2e8d3ce79 100
ktownsend 0:ace2e8d3ce79 101 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 102 /*!
ktownsend 0:ace2e8d3ce79 103 @brief Creates a short (16-bit) UUID
ktownsend 0:ace2e8d3ce79 104
ktownsend 0:ace2e8d3ce79 105 @param[in] ble_uuid
ktownsend 0:ace2e8d3ce79 106 The 16-bit BLE UUID value.
ktownsend 0:ace2e8d3ce79 107 */
ktownsend 0:ace2e8d3ce79 108 /**************************************************************************/
Rohit Grover 50:9078969e80e4 109 UUID::UUID(const uint16_t ble_uuid) : type(UUID_TYPE_SHORT),
Rohit Grover 50:9078969e80e4 110 base(),
Rohit Grover 50:9078969e80e4 111 value(ble_uuid)
ktownsend 0:ace2e8d3ce79 112 {
Rohit Grover 34:da2ea8cd6216 113 memcpy(base + 2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:ace2e8d3ce79 114 }
ktownsend 0:ace2e8d3ce79 115
ktownsend 0:ace2e8d3ce79 116 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 117 /*!
ktownsend 0:ace2e8d3ce79 118 @brief UUID destructor
ktownsend 0:ace2e8d3ce79 119 */
ktownsend 0:ace2e8d3ce79 120 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 121 UUID::~UUID(void)
ktownsend 0:ace2e8d3ce79 122 {
ktownsend 0:ace2e8d3ce79 123 }
ktownsend 0:ace2e8d3ce79 124
ktownsend 0:ace2e8d3ce79 125 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 126 /*!
ktownsend 0:ace2e8d3ce79 127 @brief Updates the value of the UUID
Rohit Grover 34:da2ea8cd6216 128
ktownsend 0:ace2e8d3ce79 129 @args[in] uuid_base
ktownsend 0:ace2e8d3ce79 130 The 128-bit value to use when updating the UUID. For
ktownsend 0:ace2e8d3ce79 131 16-bit IDs, insert the ID in bytes 2/3 in LSB format.
Rohit Grover 34:da2ea8cd6216 132
ktownsend 0:ace2e8d3ce79 133 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:ace2e8d3ce79 134 error code if there was a problem
ktownsend 0:ace2e8d3ce79 135 @retval BLE_ERROR_NONE
ktownsend 0:ace2e8d3ce79 136 Everything executed correctly
ktownsend 0:ace2e8d3ce79 137
ktownsend 0:ace2e8d3ce79 138 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 139
ktownsend 0:ace2e8d3ce79 140 @code
Rohit Grover 34:da2ea8cd6216 141
Rohit Grover 34:da2ea8cd6216 142 @endcode
ktownsend 0:ace2e8d3ce79 143 */
ktownsend 0:ace2e8d3ce79 144 /**************************************************************************/
Rohit Grover 50:9078969e80e4 145 ble_error_t UUID::update(uint8_t const uuid_base[LENGTH_OF_LONG_UUID])
ktownsend 0:ace2e8d3ce79 146 {
Rohit Grover 50:9078969e80e4 147 memcpy(base, uuid_base, LENGTH_OF_LONG_UUID);
ktownsend 0:ace2e8d3ce79 148 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:ace2e8d3ce79 149
ktownsend 0:ace2e8d3ce79 150 /* Check if this is a short of a long UUID */
Rohit Grover 34:da2ea8cd6216 151 if (uuid_base[0] + uuid_base[1] +
Rohit Grover 34:da2ea8cd6216 152 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
Rohit Grover 34:da2ea8cd6216 153 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
Rohit Grover 34:da2ea8cd6216 154 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) {
Rohit Grover 34:da2ea8cd6216 155 type = UUID_TYPE_SHORT;
Rohit Grover 43:ac154ac74a30 156 } else {
ktownsend 0:ace2e8d3ce79 157 type = UUID_TYPE_LONG;
ktownsend 0:ace2e8d3ce79 158 }
Rohit Grover 34:da2ea8cd6216 159
ktownsend 0:ace2e8d3ce79 160 return BLE_ERROR_NONE;
ktownsend 0:ace2e8d3ce79 161 }
ktownsend 0:ace2e8d3ce79 162
ktownsend 0:ace2e8d3ce79 163 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 164 /*!
ktownsend 0:ace2e8d3ce79 165 @brief Updates the value of the UUID
Rohit Grover 34:da2ea8cd6216 166
ktownsend 0:ace2e8d3ce79 167 @args[in] ble_uuid
ktownsend 0:ace2e8d3ce79 168 The 16-bit value to use when updating the UUID.
Rohit Grover 34:da2ea8cd6216 169
ktownsend 0:ace2e8d3ce79 170 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:ace2e8d3ce79 171 error code if there was a problem
ktownsend 0:ace2e8d3ce79 172 @retval BLE_ERROR_NONE
ktownsend 0:ace2e8d3ce79 173 Everything executed correctly
ktownsend 0:ace2e8d3ce79 174
ktownsend 0:ace2e8d3ce79 175 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 176
ktownsend 0:ace2e8d3ce79 177 @code
Rohit Grover 34:da2ea8cd6216 178
Rohit Grover 34:da2ea8cd6216 179 @endcode
ktownsend 0:ace2e8d3ce79 180 */
ktownsend 0:ace2e8d3ce79 181 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 182 ble_error_t UUID::update(uint16_t const ble_uuid)
ktownsend 0:ace2e8d3ce79 183 {
Rohit Grover 50:9078969e80e4 184 memset(base, 0, LENGTH_OF_LONG_UUID);
Rohit Grover 34:da2ea8cd6216 185 memcpy(base + 2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:ace2e8d3ce79 186 value = ble_uuid;
Rohit Grover 34:da2ea8cd6216 187 type = UUID_TYPE_SHORT;
Rohit Grover 34:da2ea8cd6216 188
ktownsend 0:ace2e8d3ce79 189 return BLE_ERROR_NONE;
ktownsend 0:ace2e8d3ce79 190 }