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