High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
rgrover1
Date:
Mon Mar 02 11:50:47 2015 +0000
Revision:
291:0b66dd15728e
Parent:
260:ea7f9f14cc15
Child:
292:b5ee2ada4f33
Synchronized with git rev 28a8852f
Author: Jeremy Brodt
Updated UUID class to get length and pointer. Added UUID comparison.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 291:0b66dd15728e 1 /* mbed Microcontroller Library
rgrover1 291:0b66dd15728e 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 291:0b66dd15728e 3 *
rgrover1 291:0b66dd15728e 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 291:0b66dd15728e 5 * you may not use this file except in compliance with the License.
rgrover1 291:0b66dd15728e 6 * You may obtain a copy of the License at
rgrover1 291:0b66dd15728e 7 *
rgrover1 291:0b66dd15728e 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 291:0b66dd15728e 9 *
rgrover1 291:0b66dd15728e 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 291:0b66dd15728e 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 291:0b66dd15728e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 291:0b66dd15728e 13 * See the License for the specific language governing permissions and
rgrover1 291:0b66dd15728e 14 * limitations under the License.
rgrover1 291:0b66dd15728e 15 */
rgrover1 291:0b66dd15728e 16
rgrover1 291:0b66dd15728e 17 #include <string.h>
rgrover1 291:0b66dd15728e 18
rgrover1 291:0b66dd15728e 19 #include "UUID.h"
rgrover1 291:0b66dd15728e 20
rgrover1 291:0b66dd15728e 21 UUID::UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
rgrover1 291:0b66dd15728e 22 /* empty */
rgrover1 291:0b66dd15728e 23 }
rgrover1 291:0b66dd15728e 24
rgrover1 291:0b66dd15728e 25 /**************************************************************************/
rgrover1 291:0b66dd15728e 26 /*!
rgrover1 291:0b66dd15728e 27 @brief Creates a new 128-bit UUID
rgrover1 291:0b66dd15728e 28
rgrover1 291:0b66dd15728e 29 @note The UUID is a unique 128-bit (16 byte) ID used to identify
rgrover1 291:0b66dd15728e 30 different service or characteristics on the BLE device.
rgrover1 291:0b66dd15728e 31
rgrover1 291:0b66dd15728e 32 @note When creating a UUID, the constructor will check if all bytes
rgrover1 291:0b66dd15728e 33 except bytes 2/3 are equal to 0. If only bytes 2/3 have a
rgrover1 291:0b66dd15728e 34 value, the UUID will be treated as a short/BLE UUID, and the
rgrover1 291:0b66dd15728e 35 .type field will be set to UUID::UUID_TYPE_SHORT. If any
rgrover1 291:0b66dd15728e 36 of the bytes outside byte 2/3 have a non-zero value, the UUID
rgrover1 291:0b66dd15728e 37 will be considered a 128-bit ID, and .type will be assigned
rgrover1 291:0b66dd15728e 38 as UUID::UUID_TYPE_LONG.
rgrover1 291:0b66dd15728e 39
rgrover1 291:0b66dd15728e 40 @param[in] uuid_base
rgrover1 291:0b66dd15728e 41 The 128-bit (16-byte) UUID value. For 128-bit values,
rgrover1 291:0b66dd15728e 42 assign all 16 bytes. For 16-bit values, assign the
rgrover1 291:0b66dd15728e 43 16-bits to byte 2 and 3, and leave the rest of the bytes
rgrover1 291:0b66dd15728e 44 as 0.
rgrover1 291:0b66dd15728e 45
rgrover1 291:0b66dd15728e 46 @section EXAMPLE
rgrover1 291:0b66dd15728e 47
rgrover1 291:0b66dd15728e 48 @code
rgrover1 291:0b66dd15728e 49
rgrover1 291:0b66dd15728e 50 // Create a short UUID (0x180F)
rgrover1 291:0b66dd15728e 51 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
rgrover1 291:0b66dd15728e 52 UUID ble_uuid = UUID(shortID);
rgrover1 291:0b66dd15728e 53 // ble_uuid.type = UUID_TYPE_SHORT
rgrover1 291:0b66dd15728e 54 // ble_uuid.value = 0x180F
rgrover1 291:0b66dd15728e 55
rgrover1 291:0b66dd15728e 56 // Creeate a long UUID
rgrover1 291:0b66dd15728e 57 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
rgrover1 291:0b66dd15728e 58 0x44, 0x55, 0x66, 0x77,
rgrover1 291:0b66dd15728e 59 0x88, 0x99, 0xAA, 0xBB,
rgrover1 291:0b66dd15728e 60 0xCC, 0xDD, 0xEE, 0xFF };
rgrover1 291:0b66dd15728e 61 UUID custom_uuid = UUID(longID);
rgrover1 291:0b66dd15728e 62 // custom_uuid.type = UUID_TYPE_LONG
rgrover1 291:0b66dd15728e 63 // custom_uuid.value = 0x3322
rgrover1 291:0b66dd15728e 64 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
rgrover1 291:0b66dd15728e 65
rgrover1 291:0b66dd15728e 66 @endcode
rgrover1 291:0b66dd15728e 67 */
rgrover1 291:0b66dd15728e 68 /**************************************************************************/
rgrover1 291:0b66dd15728e 69 UUID::UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(0)
rgrover1 291:0b66dd15728e 70 {
rgrover1 291:0b66dd15728e 71 memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
rgrover1 291:0b66dd15728e 72 shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
rgrover1 291:0b66dd15728e 73
rgrover1 291:0b66dd15728e 74 /* Check if this is a short of a long UUID */
rgrover1 291:0b66dd15728e 75 unsigned index;
rgrover1 291:0b66dd15728e 76 for (index = 0; index < LENGTH_OF_LONG_UUID; index++) {
rgrover1 291:0b66dd15728e 77 if ((index == 2) || (index == 3)) {
rgrover1 291:0b66dd15728e 78 continue; /* we should not consider bytes 2 and 3 because that's
rgrover1 291:0b66dd15728e 79 * where the 16-bit relative UUID is placed. */
rgrover1 291:0b66dd15728e 80 }
rgrover1 291:0b66dd15728e 81
rgrover1 291:0b66dd15728e 82 if (baseUUID[index] != 0) {
rgrover1 291:0b66dd15728e 83 type = UUID_TYPE_LONG;
rgrover1 291:0b66dd15728e 84
rgrover1 291:0b66dd15728e 85 /* zero out the 16-bit part in the base; this will help equate long
rgrover1 291:0b66dd15728e 86 * UUIDs when they differ only in this 16-bit relative part.*/
rgrover1 291:0b66dd15728e 87 baseUUID[2] = 0;
rgrover1 291:0b66dd15728e 88 baseUUID[3] = 0;
rgrover1 291:0b66dd15728e 89
rgrover1 291:0b66dd15728e 90 return;
rgrover1 291:0b66dd15728e 91 }
rgrover1 291:0b66dd15728e 92 }
rgrover1 291:0b66dd15728e 93 }
rgrover1 291:0b66dd15728e 94
rgrover1 291:0b66dd15728e 95 bool UUID::operator==(const UUID &other) const
rgrover1 291:0b66dd15728e 96 {
rgrover1 291:0b66dd15728e 97 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
rgrover1 291:0b66dd15728e 98 (this->shortUUID == other.shortUUID)) {
rgrover1 291:0b66dd15728e 99 return true;
rgrover1 291:0b66dd15728e 100 }
rgrover1 291:0b66dd15728e 101
rgrover1 291:0b66dd15728e 102 if ((this->type == LENGTH_OF_LONG_UUID) && (other.type == LENGTH_OF_LONG_UUID) &&
rgrover1 291:0b66dd15728e 103 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
rgrover1 291:0b66dd15728e 104 return true;
rgrover1 291:0b66dd15728e 105 }
rgrover1 291:0b66dd15728e 106
rgrover1 291:0b66dd15728e 107 return false;
rgrover1 291:0b66dd15728e 108 }