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:
Fri May 30 09:58:35 2014 +0100
Revision:
60:de30b62ab8c1
Parent:
53:a1bec483c8e3
Child:
61:119faa0dfeee
UUID constructors should zero out relative part of baseUUID

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 a new 128-bit UUID
Rohit Grover 34:da2ea8cd6216 26
ktownsend 0:ace2e8d3ce79 27 @note The UUID is a unique 128-bit (16 byte) ID used to identify
ktownsend 0:ace2e8d3ce79 28 different service or characteristics on the BLE device.
Rohit Grover 34:da2ea8cd6216 29
ktownsend 0:ace2e8d3ce79 30 @note When creating a UUID, the constructor will check if all bytes
ktownsend 0:ace2e8d3ce79 31 except bytes 2/3 are equal to 0. If only bytes 2/3 have a
ktownsend 0:ace2e8d3ce79 32 value, the UUID will be treated as a short/BLE UUID, and the
ktownsend 0:ace2e8d3ce79 33 .type field will be set to UUID::UUID_TYPE_SHORT. If any
ktownsend 0:ace2e8d3ce79 34 of the bytes outside byte 2/3 have a non-zero value, the UUID
Rohit Grover 34:da2ea8cd6216 35 will be considered a 128-bit ID, and .type will be assigned
Rohit Grover 34:da2ea8cd6216 36 as UUID::UUID_TYPE_LONG.
ktownsend 0:ace2e8d3ce79 37
ktownsend 0:ace2e8d3ce79 38 @param[in] uuid_base
ktownsend 0:ace2e8d3ce79 39 The 128-bit (16-byte) UUID value. For 128-bit values,
ktownsend 0:ace2e8d3ce79 40 assign all 16 bytes. For 16-bit values, assign the
ktownsend 0:ace2e8d3ce79 41 16-bits to byte 2 and 3, and leave the rest of the bytes
ktownsend 0:ace2e8d3ce79 42 as 0.
ktownsend 0:ace2e8d3ce79 43
ktownsend 0:ace2e8d3ce79 44 @section EXAMPLE
ktownsend 0:ace2e8d3ce79 45
ktownsend 0:ace2e8d3ce79 46 @code
ktownsend 0:ace2e8d3ce79 47
ktownsend 0:ace2e8d3ce79 48 // Create a short UUID (0x180F)
ktownsend 0:ace2e8d3ce79 49 uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ktownsend 0:ace2e8d3ce79 50 UUID ble_uuid = UUID(shortID);
ktownsend 0:ace2e8d3ce79 51 // ble_uuid.type = UUID_TYPE_SHORT
ktownsend 0:ace2e8d3ce79 52 // ble_uuid.value = 0x180F
Rohit Grover 34:da2ea8cd6216 53
ktownsend 0:ace2e8d3ce79 54 // Creeate a long UUID
ktownsend 0:ace2e8d3ce79 55 uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33,
Rohit Grover 34:da2ea8cd6216 56 0x44, 0x55, 0x66, 0x77,
Rohit Grover 34:da2ea8cd6216 57 0x88, 0x99, 0xAA, 0xBB,
ktownsend 0:ace2e8d3ce79 58 0xCC, 0xDD, 0xEE, 0xFF };
ktownsend 0:ace2e8d3ce79 59 UUID custom_uuid = UUID(longID);
ktownsend 0:ace2e8d3ce79 60 // custom_uuid.type = UUID_TYPE_LONG
ktownsend 0:ace2e8d3ce79 61 // custom_uuid.value = 0x3322
ktownsend 0:ace2e8d3ce79 62 // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
Rohit Grover 34:da2ea8cd6216 63
ktownsend 0:ace2e8d3ce79 64 @endcode
ktownsend 0:ace2e8d3ce79 65 */
ktownsend 0:ace2e8d3ce79 66 /**************************************************************************/
Rohit Grover 60:de30b62ab8c1 67 UUID::UUID(const uint8_t longUUID[LENGTH_OF_LONG_UUID]) :
Rohit Grover 50:9078969e80e4 68 type(UUID_TYPE_SHORT),
Rohit Grover 60:de30b62ab8c1 69 baseUUID(),
Rohit Grover 50:9078969e80e4 70 value(0)
ktownsend 0:ace2e8d3ce79 71 {
Rohit Grover 60:de30b62ab8c1 72 memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
Rohit Grover 60:de30b62ab8c1 73 value = (uint16_t)((longUUID[3] << 8) | (longUUID[2]));/* NEEDS REVIEW */
ktownsend 0:ace2e8d3ce79 74
ktownsend 0:ace2e8d3ce79 75 /* Check if this is a short of a long UUID */
Rohit Grover 60:de30b62ab8c1 76 unsigned index;
Rohit Grover 60:de30b62ab8c1 77 for (index = 0; index < LENGTH_OF_LONG_UUID; index++) {
Rohit Grover 60:de30b62ab8c1 78 if ((index == 2) || (index == 3)) {
Rohit Grover 60:de30b62ab8c1 79 continue; /* we should not consider bytes 2 and 3 because that's
Rohit Grover 60:de30b62ab8c1 80 * where the 16-bit relative UUID is placed. */
Rohit Grover 60:de30b62ab8c1 81 }
Rohit Grover 60:de30b62ab8c1 82
Rohit Grover 60:de30b62ab8c1 83 if (baseUUID[index] != 0) {
Rohit Grover 60:de30b62ab8c1 84 type = UUID_TYPE_LONG;
Rohit Grover 60:de30b62ab8c1 85
Rohit Grover 60:de30b62ab8c1 86 /* NEEDS REVIEW */
Rohit Grover 60:de30b62ab8c1 87 /* zero out the 16-bit part in the base; this will help equate long
Rohit Grover 60:de30b62ab8c1 88 * UUIDs when they differ only in this 16-bit relative part.*/
Rohit Grover 60:de30b62ab8c1 89 baseUUID[2] = 0;
Rohit Grover 60:de30b62ab8c1 90 baseUUID[3] = 0;
Rohit Grover 60:de30b62ab8c1 91
Rohit Grover 60:de30b62ab8c1 92 return;
Rohit Grover 60:de30b62ab8c1 93 }
ktownsend 0:ace2e8d3ce79 94 }
ktownsend 0:ace2e8d3ce79 95 }
ktownsend 0:ace2e8d3ce79 96
ktownsend 0:ace2e8d3ce79 97 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 98 /*!
ktownsend 0:ace2e8d3ce79 99 @brief Creates a short (16-bit) UUID
ktownsend 0:ace2e8d3ce79 100
ktownsend 0:ace2e8d3ce79 101 @param[in] ble_uuid
ktownsend 0:ace2e8d3ce79 102 The 16-bit BLE UUID value.
ktownsend 0:ace2e8d3ce79 103 */
ktownsend 0:ace2e8d3ce79 104 /**************************************************************************/
Rohit Grover 60:de30b62ab8c1 105 UUID::UUID(const uint16_t shortUUID) : type(UUID_TYPE_SHORT),
Rohit Grover 60:de30b62ab8c1 106 baseUUID(),
Rohit Grover 60:de30b62ab8c1 107 value(shortUUID)
ktownsend 0:ace2e8d3ce79 108 {
Rohit Grover 60:de30b62ab8c1 109 /* NEEDS REVIEW */
Rohit Grover 60:de30b62ab8c1 110 baseUUID[2] = (shortUUID >> 8);
Rohit Grover 60:de30b62ab8c1 111 baseUUID[3] = (shortUUID & 0xff);
ktownsend 0:ace2e8d3ce79 112 }
ktownsend 0:ace2e8d3ce79 113
ktownsend 0:ace2e8d3ce79 114 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 115 /*!
ktownsend 0:ace2e8d3ce79 116 @brief UUID destructor
ktownsend 0:ace2e8d3ce79 117 */
ktownsend 0:ace2e8d3ce79 118 /**************************************************************************/
ktownsend 0:ace2e8d3ce79 119 UUID::~UUID(void)
ktownsend 0:ace2e8d3ce79 120 {
ktownsend 0:ace2e8d3ce79 121 }