prova

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:46 2016 +0100
Revision:
1131:692ddf04fc42
Parent:
1056:ce2fb3d09929
Child:
1134:d540a48f650d
Synchronized with git rev 13bf70b6
Author: Rohit Grover
Release 2.1.5
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic. Also contains some improvements to documentation.

Who changed what in which revision?

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