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:
Thu Jan 09 11:03:10 2014 +0000
Revision:
27:4a83843f04b0
Parent:
uuid.cpp@0:ace2e8d3ce79
Child:
34:da2ea8cd6216
Matching file names and apache headers

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 */
ktownsend 27:4a83843f04b0 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
ktownsend 0:ace2e8d3ce79 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 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 31 UUID::UUID(void)
ktownsend 0:ace2e8d3ce79 32 {
ktownsend 0:ace2e8d3ce79 33 memset(base, 0, 16);
ktownsend 0:ace2e8d3ce79 34 value = 0;
ktownsend 0:ace2e8d3ce79 35 type = UUID_TYPE_SHORT;
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
ktownsend 0:ace2e8d3ce79 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.
ktownsend 0:ace2e8d3ce79 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
ktownsend 0:ace2e8d3ce79 50 will be considered a 128-bit ID, and .type will be assigned
ktownsend 0:ace2e8d3ce79 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
ktownsend 0:ace2e8d3ce79 68
ktownsend 0:ace2e8d3ce79 69 // Creeate a long UUID
ktownsend 0:ace2e8d3ce79 70 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
ktownsend 0:ace2e8d3ce79 71 0x44, 0x55, 0x66, 0x77,
ktownsend 0:ace2e8d3ce79 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
ktownsend 0:ace2e8d3ce79 78
ktownsend 0:ace2e8d3ce79 79 @endcode
ktownsend 0:ace2e8d3ce79 80 */
ktownsend 0:ace2e8d3ce79 81 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 82 UUID::UUID(uint8_t const uuid_base[16])
ktownsend 0:ace2e8d3ce79 83 {
ktownsend 0:ace2e8d3ce79 84 memcpy(base, uuid_base, 16);
ktownsend 0:ace2e8d3ce79 85 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:ace2e8d3ce79 86
ktownsend 0:ace2e8d3ce79 87 /* Check if this is a short of a long UUID */
ktownsend 0:ace2e8d3ce79 88 if (uuid_base[0] + uuid_base[1] +
ktownsend 0:ace2e8d3ce79 89 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
ktownsend 0:ace2e8d3ce79 90 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
ktownsend 0:ace2e8d3ce79 91 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0)
ktownsend 0:ace2e8d3ce79 92 {
ktownsend 0:ace2e8d3ce79 93 type = UUID_TYPE_SHORT;
ktownsend 0:ace2e8d3ce79 94 }
ktownsend 0:ace2e8d3ce79 95 else
ktownsend 0:ace2e8d3ce79 96 {
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 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 109 UUID::UUID(uint16_t const ble_uuid)
ktownsend 0:ace2e8d3ce79 110 {
ktownsend 0:ace2e8d3ce79 111 memset(base, 0, 16);
ktownsend 0:ace2e8d3ce79 112 memcpy(base+2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:ace2e8d3ce79 113 value = ble_uuid;
ktownsend 0:ace2e8d3ce79 114 type = UUID_TYPE_SHORT;
ktownsend 0:ace2e8d3ce79 115 }
ktownsend 0:ace2e8d3ce79 116
ktownsend 0:ace2e8d3ce79 117 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 118 /*!
ktownsend 0:ace2e8d3ce79 119 @brief UUID destructor
ktownsend 0:ace2e8d3ce79 120 */
ktownsend 0:ace2e8d3ce79 121 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 122 UUID::~UUID(void)
ktownsend 0:ace2e8d3ce79 123 {
ktownsend 0:ace2e8d3ce79 124 }
ktownsend 0:ace2e8d3ce79 125
ktownsend 0:ace2e8d3ce79 126 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 127 /*!
ktownsend 0:ace2e8d3ce79 128 @brief Updates the value of the UUID
ktownsend 0:ace2e8d3ce79 129
ktownsend 0:ace2e8d3ce79 130 @args[in] uuid_base
ktownsend 0:ace2e8d3ce79 131 The 128-bit value to use when updating the UUID. For
ktownsend 0:ace2e8d3ce79 132 16-bit IDs, insert the ID in bytes 2/3 in LSB format.
ktownsend 0:ace2e8d3ce79 133
ktownsend 0:ace2e8d3ce79 134 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:ace2e8d3ce79 135 error code if there was a problem
ktownsend 0:ace2e8d3ce79 136 @retval BLE_ERROR_NONE
ktownsend 0:ace2e8d3ce79 137 Everything executed correctly
ktownsend 0:ace2e8d3ce79 138
ktownsend 0:ace2e8d3ce79 139 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 140
ktownsend 0:ace2e8d3ce79 141 @code
ktownsend 0:ace2e8d3ce79 142
ktownsend 0:ace2e8d3ce79 143 @endcode
ktownsend 0:ace2e8d3ce79 144 */
ktownsend 0:ace2e8d3ce79 145 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 146 ble_error_t UUID::update(uint8_t const uuid_base[16])
ktownsend 0:ace2e8d3ce79 147 {
ktownsend 0:ace2e8d3ce79 148 memcpy(base, uuid_base, 16);
ktownsend 0:ace2e8d3ce79 149 value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2]));
ktownsend 0:ace2e8d3ce79 150
ktownsend 0:ace2e8d3ce79 151 /* Check if this is a short of a long UUID */
ktownsend 0:ace2e8d3ce79 152 if (uuid_base[0] + uuid_base[1] +
ktownsend 0:ace2e8d3ce79 153 uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] +
ktownsend 0:ace2e8d3ce79 154 uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] +
ktownsend 0:ace2e8d3ce79 155 uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0)
ktownsend 0:ace2e8d3ce79 156 {
ktownsend 0:ace2e8d3ce79 157 type = UUID_TYPE_SHORT;
ktownsend 0:ace2e8d3ce79 158 }
ktownsend 0:ace2e8d3ce79 159 else
ktownsend 0:ace2e8d3ce79 160 {
ktownsend 0:ace2e8d3ce79 161 type = UUID_TYPE_LONG;
ktownsend 0:ace2e8d3ce79 162 }
ktownsend 0:ace2e8d3ce79 163
ktownsend 0:ace2e8d3ce79 164 return BLE_ERROR_NONE;
ktownsend 0:ace2e8d3ce79 165 }
ktownsend 0:ace2e8d3ce79 166
ktownsend 0:ace2e8d3ce79 167 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 168 /*!
ktownsend 0:ace2e8d3ce79 169 @brief Updates the value of the UUID
ktownsend 0:ace2e8d3ce79 170
ktownsend 0:ace2e8d3ce79 171 @args[in] ble_uuid
ktownsend 0:ace2e8d3ce79 172 The 16-bit value to use when updating the UUID.
ktownsend 0:ace2e8d3ce79 173
ktownsend 0:ace2e8d3ce79 174 @returns BLE_ERROR_NONE (0) if everything executed correctly, or an
ktownsend 0:ace2e8d3ce79 175 error code if there was a problem
ktownsend 0:ace2e8d3ce79 176 @retval BLE_ERROR_NONE
ktownsend 0:ace2e8d3ce79 177 Everything executed correctly
ktownsend 0:ace2e8d3ce79 178
ktownsend 0:ace2e8d3ce79 179 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 180
ktownsend 0:ace2e8d3ce79 181 @code
ktownsend 0:ace2e8d3ce79 182
ktownsend 0:ace2e8d3ce79 183 @endcode
ktownsend 0:ace2e8d3ce79 184 */
ktownsend 0:ace2e8d3ce79 185 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 186 ble_error_t UUID::update(uint16_t const ble_uuid)
ktownsend 0:ace2e8d3ce79 187 {
ktownsend 0:ace2e8d3ce79 188 memset(base, 0, 16);
ktownsend 0:ace2e8d3ce79 189 memcpy(base+2, (uint8_t *)&ble_uuid, 2);
ktownsend 0:ace2e8d3ce79 190 value = ble_uuid;
ktownsend 0:ace2e8d3ce79 191 type = UUID_TYPE_SHORT;
ktownsend 0:ace2e8d3ce79 192
ktownsend 0:ace2e8d3ce79 193 return BLE_ERROR_NONE;
ktownsend 0:ace2e8d3ce79 194 }