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:
Fri Jun 19 15:52:53 2015 +0100
Revision:
603:efa5a21436c1
Parent:
575:76fe165fc0b1
Child:
607:8b3210b0b55e
Synchronized with git rev 081ceb5c
Author: Rohit Grover
Move certain UUID related declarations within the UUID namespace.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 569:a5a6a1681fcf 1 /* mbed Microcontroller Library
rgrover1 569:a5a6a1681fcf 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 569:a5a6a1681fcf 3 *
rgrover1 569:a5a6a1681fcf 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 569:a5a6a1681fcf 5 * you may not use this file except in compliance with the License.
rgrover1 569:a5a6a1681fcf 6 * You may obtain a copy of the License at
rgrover1 569:a5a6a1681fcf 7 *
rgrover1 569:a5a6a1681fcf 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 569:a5a6a1681fcf 9 *
rgrover1 569:a5a6a1681fcf 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 569:a5a6a1681fcf 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 569:a5a6a1681fcf 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 569:a5a6a1681fcf 13 * See the License for the specific language governing permissions and
rgrover1 569:a5a6a1681fcf 14 * limitations under the License.
rgrover1 569:a5a6a1681fcf 15 */
rgrover1 569:a5a6a1681fcf 16
rgrover1 569:a5a6a1681fcf 17 #ifndef __UUID_H__
rgrover1 569:a5a6a1681fcf 18 #define __UUID_H__
rgrover1 569:a5a6a1681fcf 19
rgrover1 569:a5a6a1681fcf 20 #include <string.h>
rgrover1 569:a5a6a1681fcf 21
rgrover1 569:a5a6a1681fcf 22 #include "blecommon.h"
rgrover1 569:a5a6a1681fcf 23
rgrover1 569:a5a6a1681fcf 24 class UUID {
rgrover1 569:a5a6a1681fcf 25 public:
rgrover1 569:a5a6a1681fcf 26 enum UUID_Type_t {
rgrover1 569:a5a6a1681fcf 27 UUID_TYPE_SHORT = 0, // Short BLE UUID
rgrover1 569:a5a6a1681fcf 28 UUID_TYPE_LONG = 1 // Full 128-bit UUID
rgrover1 569:a5a6a1681fcf 29 };
rgrover1 569:a5a6a1681fcf 30
rgrover1 603:efa5a21436c1 31 static const unsigned LENGTH_OF_LONG_UUID = 16;
rgrover1 603:efa5a21436c1 32 typedef uint16_t ShortUUIDBytes_t;
rgrover1 603:efa5a21436c1 33 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
rgrover1 603:efa5a21436c1 34
rgrover1 569:a5a6a1681fcf 35 public:
rgrover1 569:a5a6a1681fcf 36 /**
rgrover1 569:a5a6a1681fcf 37 * Creates a new 128-bit UUID
rgrover1 569:a5a6a1681fcf 38 *
rgrover1 569:a5a6a1681fcf 39 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
rgrover1 569:a5a6a1681fcf 40 * different service or characteristics on the BLE device.
rgrover1 569:a5a6a1681fcf 41 *
rgrover1 569:a5a6a1681fcf 42 * @param longUUID
rgrover1 569:a5a6a1681fcf 43 * The 128-bit (16-byte) UUID value.
rgrover1 569:a5a6a1681fcf 44 */
rgrover1 569:a5a6a1681fcf 45 UUID(const LongUUIDBytes_t longUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
rgrover1 569:a5a6a1681fcf 46 memcpy(baseUUID, longUUID, LENGTH_OF_LONG_UUID);
rgrover1 569:a5a6a1681fcf 47 shortUUID = (uint16_t)((longUUID[2] << 8) | (longUUID[3]));
rgrover1 569:a5a6a1681fcf 48 }
rgrover1 569:a5a6a1681fcf 49
rgrover1 569:a5a6a1681fcf 50 /**
rgrover1 569:a5a6a1681fcf 51 * Creates a new 16-bit UUID
rgrover1 569:a5a6a1681fcf 52 *
rgrover1 569:a5a6a1681fcf 53 * @note The UUID is a unique 16-bit (2 byte) ID used to identify
rgrover1 569:a5a6a1681fcf 54 * different service or characteristics on the BLE device.
rgrover1 569:a5a6a1681fcf 55 *
rgrover1 569:a5a6a1681fcf 56 * For efficiency, and because 16 bytes would take a large chunk of the
rgrover1 569:a5a6a1681fcf 57 * 27-byte data payload length of the Link Layer, the BLE specification adds
rgrover1 569:a5a6a1681fcf 58 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened
rgrover1 569:a5a6a1681fcf 59 * formats can be used only with UUIDs that are defined in the Bluetooth
rgrover1 569:a5a6a1681fcf 60 * specification (i.e., that are listed by the Bluetooth SIG as standard
rgrover1 569:a5a6a1681fcf 61 * Bluetooth UUIDs).
rgrover1 569:a5a6a1681fcf 62 *
rgrover1 569:a5a6a1681fcf 63 * To reconstruct the full 128-bit UUID from the shortened version, insert
rgrover1 569:a5a6a1681fcf 64 * the 16-bit short value (indicated by xxxx, including leading zeros) into
rgrover1 569:a5a6a1681fcf 65 * the Bluetooth Base UUID:
rgrover1 569:a5a6a1681fcf 66 *
rgrover1 569:a5a6a1681fcf 67 * 0000xxxx-0000-1000-8000-00805F9B34FB
rgrover1 569:a5a6a1681fcf 68 *
rgrover1 569:a5a6a1681fcf 69 * @note Shortening is not available for UUIDs that are not derived from the
rgrover1 569:a5a6a1681fcf 70 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called
rgrover1 569:a5a6a1681fcf 71 * vendor-specific UUIDs. In these cases, you’ll need to use the full
rgrover1 569:a5a6a1681fcf 72 * 128-bit UUID value at all times.
rgrover1 569:a5a6a1681fcf 73 *
rgrover1 569:a5a6a1681fcf 74 * @note we don't yet support 32-bit shortened UUIDs.
rgrover1 569:a5a6a1681fcf 75 */
rgrover1 569:a5a6a1681fcf 76 UUID(ShortUUIDBytes_t shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(shortUUID) {
rgrover1 569:a5a6a1681fcf 77 /* empty */
rgrover1 569:a5a6a1681fcf 78 }
rgrover1 569:a5a6a1681fcf 79
rgrover1 575:76fe165fc0b1 80 UUID(const UUID &source) {
rgrover1 575:76fe165fc0b1 81 type = source.type;
rgrover1 575:76fe165fc0b1 82 shortUUID = source.shortUUID;
rgrover1 575:76fe165fc0b1 83 memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID);
rgrover1 575:76fe165fc0b1 84 }
rgrover1 575:76fe165fc0b1 85
rgrover1 575:76fe165fc0b1 86 UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) {
rgrover1 575:76fe165fc0b1 87 /* empty */
rgrover1 575:76fe165fc0b1 88 }
rgrover1 575:76fe165fc0b1 89
rgrover1 569:a5a6a1681fcf 90 public:
rgrover1 569:a5a6a1681fcf 91 UUID_Type_t shortOrLong(void) const {return type; }
rgrover1 569:a5a6a1681fcf 92 const uint8_t *getBaseUUID(void) const {
rgrover1 569:a5a6a1681fcf 93 if (type == UUID_TYPE_SHORT) {
rgrover1 569:a5a6a1681fcf 94 return (const uint8_t*)&shortUUID;
rgrover1 569:a5a6a1681fcf 95 } else {
rgrover1 569:a5a6a1681fcf 96 return baseUUID;
rgrover1 569:a5a6a1681fcf 97 }
rgrover1 569:a5a6a1681fcf 98 }
rgrover1 569:a5a6a1681fcf 99
rgrover1 569:a5a6a1681fcf 100 ShortUUIDBytes_t getShortUUID(void) const {return shortUUID;}
rgrover1 569:a5a6a1681fcf 101 uint8_t getLen(void) const {
rgrover1 569:a5a6a1681fcf 102 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
rgrover1 569:a5a6a1681fcf 103 }
rgrover1 569:a5a6a1681fcf 104
rgrover1 569:a5a6a1681fcf 105 bool operator== (const UUID &other) const {
rgrover1 569:a5a6a1681fcf 106 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
rgrover1 569:a5a6a1681fcf 107 (this->shortUUID == other.shortUUID)) {
rgrover1 569:a5a6a1681fcf 108 return true;
rgrover1 569:a5a6a1681fcf 109 }
rgrover1 569:a5a6a1681fcf 110
rgrover1 569:a5a6a1681fcf 111 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) &&
rgrover1 569:a5a6a1681fcf 112 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
rgrover1 569:a5a6a1681fcf 113 return true;
rgrover1 569:a5a6a1681fcf 114 }
rgrover1 569:a5a6a1681fcf 115
rgrover1 569:a5a6a1681fcf 116 return false;
rgrover1 569:a5a6a1681fcf 117 }
rgrover1 569:a5a6a1681fcf 118
rgrover1 569:a5a6a1681fcf 119 private:
rgrover1 569:a5a6a1681fcf 120 UUID_Type_t type; // UUID_TYPE_SHORT or UUID_TYPE_LONG
rgrover1 569:a5a6a1681fcf 121 LongUUIDBytes_t baseUUID; /* the base of the long UUID (if
rgrover1 569:a5a6a1681fcf 122 * used). Note: bytes 12 and 13 (counting from LSB)
rgrover1 569:a5a6a1681fcf 123 * are zeroed out to allow comparison with other long
rgrover1 569:a5a6a1681fcf 124 * UUIDs which differ only in the 16-bit relative
rgrover1 569:a5a6a1681fcf 125 * part.*/
rgrover1 569:a5a6a1681fcf 126 ShortUUIDBytes_t shortUUID; // 16 bit uuid (byte 2-3 using with base)
rgrover1 569:a5a6a1681fcf 127 };
rgrover1 569:a5a6a1681fcf 128
rgrover1 569:a5a6a1681fcf 129 #endif // ifndef __UUID_H__