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:
607:8b3210b0b55e
Parent:
603:efa5a21436c1
Child:
608:700a809b0832
Synchronized with git rev 273ed9e6
Author: Rohit Grover
add a comment to UUID constructor to explain MSB byte ordering.

Who changed what in which revision?

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