Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* mbed Microcontroller Library
switches 0:5c4d7b2438d3 2 * Copyright (c) 2006-2013 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 5 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 6 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 7 *
switches 0:5c4d7b2438d3 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 13 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 14 * limitations under the License.
switches 0:5c4d7b2438d3 15 */
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 #ifndef __UUID_H__
switches 0:5c4d7b2438d3 18 #define __UUID_H__
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 #include <stdint.h>
switches 0:5c4d7b2438d3 21 #include <string.h>
switches 0:5c4d7b2438d3 22 #include <algorithm>
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 #include "blecommon.h"
switches 0:5c4d7b2438d3 25
switches 0:5c4d7b2438d3 26 /**
switches 0:5c4d7b2438d3 27 * A trivial converter for single hexadecimal character to an unsigned integer.
switches 0:5c4d7b2438d3 28 *
switches 0:5c4d7b2438d3 29 * @param c
switches 0:5c4d7b2438d3 30 * Hexadecimal character.
switches 0:5c4d7b2438d3 31 *
switches 0:5c4d7b2438d3 32 * @return The corresponding value as unsigned integer.
switches 0:5c4d7b2438d3 33 */
switches 0:5c4d7b2438d3 34 static uint8_t char2int(char c) {
switches 0:5c4d7b2438d3 35 if ((c >= '0') && (c <= '9')) {
switches 0:5c4d7b2438d3 36 return c - '0';
switches 0:5c4d7b2438d3 37 } else if ((c >= 'a') && (c <= 'f')) {
switches 0:5c4d7b2438d3 38 return c - 'a' + 10;
switches 0:5c4d7b2438d3 39 } else if ((c >= 'A') && (c <= 'F')) {
switches 0:5c4d7b2438d3 40 return c - 'A' + 10;
switches 0:5c4d7b2438d3 41 } else {
switches 0:5c4d7b2438d3 42 return 0;
switches 0:5c4d7b2438d3 43 }
switches 0:5c4d7b2438d3 44 }
switches 0:5c4d7b2438d3 45
switches 0:5c4d7b2438d3 46 /**
switches 0:5c4d7b2438d3 47 * An instance of this class represents a Universally Unique Identifier (UUID)
switches 0:5c4d7b2438d3 48 * in the BLE API.
switches 0:5c4d7b2438d3 49 */
switches 0:5c4d7b2438d3 50 class UUID {
switches 0:5c4d7b2438d3 51 public:
switches 0:5c4d7b2438d3 52 /**
switches 0:5c4d7b2438d3 53 * Enumeration of the possible types of UUIDs in BLE with regards to length.
switches 0:5c4d7b2438d3 54 */
switches 0:5c4d7b2438d3 55 enum UUID_Type_t {
switches 0:5c4d7b2438d3 56 UUID_TYPE_SHORT = 0, /**< Short 16-bit UUID. */
switches 0:5c4d7b2438d3 57 UUID_TYPE_LONG = 1 /**< Full 128-bit UUID. */
switches 0:5c4d7b2438d3 58 };
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 /**
switches 0:5c4d7b2438d3 61 * Enumeration to specify byte ordering of the long version of the UUID.
switches 0:5c4d7b2438d3 62 */
switches 0:5c4d7b2438d3 63 typedef enum {
switches 0:5c4d7b2438d3 64 MSB, /**< Most-significant byte first (at the smallest address) */
switches 0:5c4d7b2438d3 65 LSB /**< least-significant byte first (at the smallest address) */
switches 0:5c4d7b2438d3 66 } ByteOrder_t;
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 /**
switches 0:5c4d7b2438d3 69 * Type for a 16-bit UUID.
switches 0:5c4d7b2438d3 70 */
switches 0:5c4d7b2438d3 71 typedef uint16_t ShortUUIDBytes_t;
switches 0:5c4d7b2438d3 72
switches 0:5c4d7b2438d3 73 /**
switches 0:5c4d7b2438d3 74 * Length of a long UUID in bytes.
switches 0:5c4d7b2438d3 75 */
switches 0:5c4d7b2438d3 76 static const unsigned LENGTH_OF_LONG_UUID = 16;
switches 0:5c4d7b2438d3 77 /**
switches 0:5c4d7b2438d3 78 * Type for a 128-bit UUID.
switches 0:5c4d7b2438d3 79 */
switches 0:5c4d7b2438d3 80 typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID];
switches 0:5c4d7b2438d3 81
switches 0:5c4d7b2438d3 82 /**
switches 0:5c4d7b2438d3 83 * Maximum length of a string representation of a UUID not including the
switches 0:5c4d7b2438d3 84 * null termination ('\0'): two characters per
switches 0:5c4d7b2438d3 85 * byte plus four '-' characters.
switches 0:5c4d7b2438d3 86 */
switches 0:5c4d7b2438d3 87 static const unsigned MAX_UUID_STRING_LENGTH = LENGTH_OF_LONG_UUID * 2 + 4;
switches 0:5c4d7b2438d3 88
switches 0:5c4d7b2438d3 89 public:
switches 0:5c4d7b2438d3 90
switches 0:5c4d7b2438d3 91 /**
switches 0:5c4d7b2438d3 92 * Creates a new 128-bit UUID.
switches 0:5c4d7b2438d3 93 *
switches 0:5c4d7b2438d3 94 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
switches 0:5c4d7b2438d3 95 * different service or characteristics on the BLE device.
switches 0:5c4d7b2438d3 96 *
switches 0:5c4d7b2438d3 97 * @param stringUUID
switches 0:5c4d7b2438d3 98 * The 128-bit (16-byte) UUID as a human readable const-string.
switches 0:5c4d7b2438d3 99 * Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
switches 0:5c4d7b2438d3 100 * Upper and lower case supported. Hyphens are optional, but only
switches 0:5c4d7b2438d3 101 * upto four of them. The UUID is stored internally as a 16 byte
switches 0:5c4d7b2438d3 102 * array, LSB (little endian), which is opposite from the string.
switches 0:5c4d7b2438d3 103 */
switches 0:5c4d7b2438d3 104 UUID(const char* stringUUID) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
switches 0:5c4d7b2438d3 105 bool nibble = false;
switches 0:5c4d7b2438d3 106 uint8_t byte = 0;
switches 0:5c4d7b2438d3 107 size_t baseIndex = 0;
switches 0:5c4d7b2438d3 108 uint8_t tempUUID[LENGTH_OF_LONG_UUID];
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 /*
switches 0:5c4d7b2438d3 111 * Iterate through string, abort if NULL is encountered prematurely.
switches 0:5c4d7b2438d3 112 * Ignore upto four hyphens.
switches 0:5c4d7b2438d3 113 */
switches 0:5c4d7b2438d3 114 for (size_t index = 0; (index < MAX_UUID_STRING_LENGTH) && (baseIndex < LENGTH_OF_LONG_UUID); index++) {
switches 0:5c4d7b2438d3 115 if (stringUUID[index] == '\0') {
switches 0:5c4d7b2438d3 116 /* Error abort */
switches 0:5c4d7b2438d3 117 break;
switches 0:5c4d7b2438d3 118 } else if (stringUUID[index] == '-') {
switches 0:5c4d7b2438d3 119 /* Ignore hyphen */
switches 0:5c4d7b2438d3 120 continue;
switches 0:5c4d7b2438d3 121 } else if (nibble) {
switches 0:5c4d7b2438d3 122 /* Got second nibble */
switches 0:5c4d7b2438d3 123 byte |= char2int(stringUUID[index]);
switches 0:5c4d7b2438d3 124 nibble = false;
switches 0:5c4d7b2438d3 125
switches 0:5c4d7b2438d3 126 /* Store copy */
switches 0:5c4d7b2438d3 127 tempUUID[baseIndex++] = byte;
switches 0:5c4d7b2438d3 128 } else {
switches 0:5c4d7b2438d3 129 /* Got first nibble */
switches 0:5c4d7b2438d3 130 byte = char2int(stringUUID[index]) << 4;
switches 0:5c4d7b2438d3 131 nibble = true;
switches 0:5c4d7b2438d3 132 }
switches 0:5c4d7b2438d3 133 }
switches 0:5c4d7b2438d3 134
switches 0:5c4d7b2438d3 135 /* Populate internal variables if string was successfully parsed */
switches 0:5c4d7b2438d3 136 if (baseIndex == LENGTH_OF_LONG_UUID) {
switches 0:5c4d7b2438d3 137 setupLong(tempUUID, UUID::MSB);
switches 0:5c4d7b2438d3 138 } else {
switches 0:5c4d7b2438d3 139 const uint8_t sig[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
switches 0:5c4d7b2438d3 140 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
switches 0:5c4d7b2438d3 141 setupLong(sig, UUID::MSB);
switches 0:5c4d7b2438d3 142 }
switches 0:5c4d7b2438d3 143 }
switches 0:5c4d7b2438d3 144
switches 0:5c4d7b2438d3 145 /**
switches 0:5c4d7b2438d3 146 * Creates a new 128-bit UUID.
switches 0:5c4d7b2438d3 147 *
switches 0:5c4d7b2438d3 148 * @param[in] longUUID
switches 0:5c4d7b2438d3 149 * The 128-bit (16-byte) UUID value.
switches 0:5c4d7b2438d3 150 * @param[in] order
switches 0:5c4d7b2438d3 151 * The bit order of the UUID, MSB by default.
switches 0:5c4d7b2438d3 152 *
switches 0:5c4d7b2438d3 153 * @note The UUID is a unique 128-bit (16 byte) ID used to identify
switches 0:5c4d7b2438d3 154 * different service or characteristics on the BLE device.
switches 0:5c4d7b2438d3 155 */
switches 0:5c4d7b2438d3 156 UUID(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) : type(UUID_TYPE_LONG), baseUUID(), shortUUID(0) {
switches 0:5c4d7b2438d3 157 setupLong(longUUID, order);
switches 0:5c4d7b2438d3 158 }
switches 0:5c4d7b2438d3 159
switches 0:5c4d7b2438d3 160 /**
switches 0:5c4d7b2438d3 161 * Creates a new 16-bit UUID.
switches 0:5c4d7b2438d3 162 *
switches 0:5c4d7b2438d3 163 * For efficiency, and because 16 bytes would take a large chunk of the
switches 0:5c4d7b2438d3 164 * 27-byte data payload length of the Link Layer, the BLE specification adds
switches 0:5c4d7b2438d3 165 * two additional UUID formats: 16-bit and 32-bit UUIDs. These shortened
switches 0:5c4d7b2438d3 166 * formats can be used only with UUIDs that are defined in the Bluetooth
switches 0:5c4d7b2438d3 167 * specification (listed by the Bluetooth SIG as standard
switches 0:5c4d7b2438d3 168 * Bluetooth UUIDs).
switches 0:5c4d7b2438d3 169 *
switches 0:5c4d7b2438d3 170 * To reconstruct the full 128-bit UUID from the shortened version, insert
switches 0:5c4d7b2438d3 171 * the 16-bit short value (indicated by xxxx, including leading zeros) into
switches 0:5c4d7b2438d3 172 * the Bluetooth Base UUID:
switches 0:5c4d7b2438d3 173 *
switches 0:5c4d7b2438d3 174 * 0000xxxx-0000-1000-8000-00805F9B34FB
switches 0:5c4d7b2438d3 175 *
switches 0:5c4d7b2438d3 176 * @param[in] _shortUUID
switches 0:5c4d7b2438d3 177 * The short UUID value.
switches 0:5c4d7b2438d3 178 *
switches 0:5c4d7b2438d3 179 * @note Shortening is not available for UUIDs that are not derived from the
switches 0:5c4d7b2438d3 180 * Bluetooth Base UUID. Such non-standard UUIDs are commonly called
switches 0:5c4d7b2438d3 181 * vendor-specific UUIDs. In these cases, you’ll need to use the full
switches 0:5c4d7b2438d3 182 * 128-bit UUID value at all times.
switches 0:5c4d7b2438d3 183 *
switches 0:5c4d7b2438d3 184 * @note The UUID is a unique 16-bit (2 byte) ID used to identify
switches 0:5c4d7b2438d3 185 * different service or characteristics on the BLE device.
switches 0:5c4d7b2438d3 186 *
switches 0:5c4d7b2438d3 187 * @note We do not yet support 32-bit shortened UUIDs.
switches 0:5c4d7b2438d3 188 */
switches 0:5c4d7b2438d3 189 UUID(ShortUUIDBytes_t _shortUUID) : type(UUID_TYPE_SHORT), baseUUID(), shortUUID(_shortUUID) {
switches 0:5c4d7b2438d3 190 /* Empty */
switches 0:5c4d7b2438d3 191 }
switches 0:5c4d7b2438d3 192
switches 0:5c4d7b2438d3 193 /**
switches 0:5c4d7b2438d3 194 * Copy constructor.
switches 0:5c4d7b2438d3 195 *
switches 0:5c4d7b2438d3 196 * @param[in] source
switches 0:5c4d7b2438d3 197 * The UUID to copy.
switches 0:5c4d7b2438d3 198 */
switches 0:5c4d7b2438d3 199 UUID(const UUID &source) {
switches 0:5c4d7b2438d3 200 type = source.type;
switches 0:5c4d7b2438d3 201 shortUUID = source.shortUUID;
switches 0:5c4d7b2438d3 202 memcpy(baseUUID, source.baseUUID, LENGTH_OF_LONG_UUID);
switches 0:5c4d7b2438d3 203 }
switches 0:5c4d7b2438d3 204
switches 0:5c4d7b2438d3 205 /**
switches 0:5c4d7b2438d3 206 * The empty constructor.
switches 0:5c4d7b2438d3 207 *
switches 0:5c4d7b2438d3 208 * @note The type of the resulting UUID instance is UUID_TYPE_SHORT and the
switches 0:5c4d7b2438d3 209 * value BLE_UUID_UNKNOWN.
switches 0:5c4d7b2438d3 210 */
switches 0:5c4d7b2438d3 211 UUID(void) : type(UUID_TYPE_SHORT), shortUUID(BLE_UUID_UNKNOWN) {
switches 0:5c4d7b2438d3 212 /* empty */
switches 0:5c4d7b2438d3 213 }
switches 0:5c4d7b2438d3 214
switches 0:5c4d7b2438d3 215 /**
switches 0:5c4d7b2438d3 216 * Fill in a 128-bit UUID; this is useful when the UUID is not known at the
switches 0:5c4d7b2438d3 217 * time of the object construction.
switches 0:5c4d7b2438d3 218 *
switches 0:5c4d7b2438d3 219 * @param[in] longUUID
switches 0:5c4d7b2438d3 220 * The UUID value to copy.
switches 0:5c4d7b2438d3 221 * @param[in] order
switches 0:5c4d7b2438d3 222 * The byte ordering of the UUID at @p longUUID.
switches 0:5c4d7b2438d3 223 */
switches 0:5c4d7b2438d3 224 void setupLong(const LongUUIDBytes_t longUUID, ByteOrder_t order = UUID::MSB) {
switches 0:5c4d7b2438d3 225 type = UUID_TYPE_LONG;
switches 0:5c4d7b2438d3 226 if (order == UUID::MSB) {
switches 0:5c4d7b2438d3 227 /*
switches 0:5c4d7b2438d3 228 * Switch endian. Input is big-endian, internal representation
switches 0:5c4d7b2438d3 229 * is little endian.
switches 0:5c4d7b2438d3 230 */
switches 0:5c4d7b2438d3 231 std::reverse_copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
switches 0:5c4d7b2438d3 232 } else {
switches 0:5c4d7b2438d3 233 std::copy(longUUID, longUUID + LENGTH_OF_LONG_UUID, baseUUID);
switches 0:5c4d7b2438d3 234 }
switches 0:5c4d7b2438d3 235 shortUUID = (uint16_t)((baseUUID[13] << 8) | (baseUUID[12]));
switches 0:5c4d7b2438d3 236 }
switches 0:5c4d7b2438d3 237
switches 0:5c4d7b2438d3 238 public:
switches 0:5c4d7b2438d3 239 /**
switches 0:5c4d7b2438d3 240 * Check whether this UUID is short or long.
switches 0:5c4d7b2438d3 241 *
switches 0:5c4d7b2438d3 242 * @return UUID_TYPE_SHORT if the UUID is short, UUID_TYPE_LONG otherwise.
switches 0:5c4d7b2438d3 243 */
switches 0:5c4d7b2438d3 244 UUID_Type_t shortOrLong(void) const {
switches 0:5c4d7b2438d3 245 return type;
switches 0:5c4d7b2438d3 246 }
switches 0:5c4d7b2438d3 247
switches 0:5c4d7b2438d3 248 /**
switches 0:5c4d7b2438d3 249 * Get a pointer to the UUID value based on the current UUID type.
switches 0:5c4d7b2438d3 250 *
switches 0:5c4d7b2438d3 251 * @return A pointer to the short UUID if the type is set to
switches 0:5c4d7b2438d3 252 * UUID_TYPE_SHORT. Otherwise, a pointer to the long UUID if the
switches 0:5c4d7b2438d3 253 * type is set to UUID_TYPE_LONG.
switches 0:5c4d7b2438d3 254 */
switches 0:5c4d7b2438d3 255 const uint8_t *getBaseUUID(void) const {
switches 0:5c4d7b2438d3 256 if (type == UUID_TYPE_SHORT) {
switches 0:5c4d7b2438d3 257 return (const uint8_t*)&shortUUID;
switches 0:5c4d7b2438d3 258 } else {
switches 0:5c4d7b2438d3 259 return baseUUID;
switches 0:5c4d7b2438d3 260 }
switches 0:5c4d7b2438d3 261 }
switches 0:5c4d7b2438d3 262
switches 0:5c4d7b2438d3 263 /**
switches 0:5c4d7b2438d3 264 * Get the short UUID.
switches 0:5c4d7b2438d3 265 *
switches 0:5c4d7b2438d3 266 * @return The short UUID.
switches 0:5c4d7b2438d3 267 */
switches 0:5c4d7b2438d3 268 ShortUUIDBytes_t getShortUUID(void) const {
switches 0:5c4d7b2438d3 269 return shortUUID;
switches 0:5c4d7b2438d3 270 }
switches 0:5c4d7b2438d3 271
switches 0:5c4d7b2438d3 272 /**
switches 0:5c4d7b2438d3 273 * Get the length (in bytes) of the UUID based on its type.
switches 0:5c4d7b2438d3 274 *
switches 0:5c4d7b2438d3 275 * @retval sizeof(ShortUUIDBytes_t) if the UUID type is UUID_TYPE_SHORT.
switches 0:5c4d7b2438d3 276 * @retval LENGTH_OF_LONG_UUID if the UUID type is UUID_TYPE_LONG.
switches 0:5c4d7b2438d3 277 */
switches 0:5c4d7b2438d3 278 uint8_t getLen(void) const {
switches 0:5c4d7b2438d3 279 return ((type == UUID_TYPE_SHORT) ? sizeof(ShortUUIDBytes_t) : LENGTH_OF_LONG_UUID);
switches 0:5c4d7b2438d3 280 }
switches 0:5c4d7b2438d3 281
switches 0:5c4d7b2438d3 282 /**
switches 0:5c4d7b2438d3 283 * Overload == operator to enable UUID comparisons.
switches 0:5c4d7b2438d3 284 *
switches 0:5c4d7b2438d3 285 * @param[in] other
switches 0:5c4d7b2438d3 286 * The other UUID in the comparison.
switches 0:5c4d7b2438d3 287 *
switches 0:5c4d7b2438d3 288 * @return true if this == @p other, false otherwise.
switches 0:5c4d7b2438d3 289 */
switches 0:5c4d7b2438d3 290 bool operator== (const UUID &other) const {
switches 0:5c4d7b2438d3 291 if ((this->type == UUID_TYPE_SHORT) && (other.type == UUID_TYPE_SHORT) &&
switches 0:5c4d7b2438d3 292 (this->shortUUID == other.shortUUID)) {
switches 0:5c4d7b2438d3 293 return true;
switches 0:5c4d7b2438d3 294 }
switches 0:5c4d7b2438d3 295
switches 0:5c4d7b2438d3 296 if ((this->type == UUID_TYPE_LONG) && (other.type == UUID_TYPE_LONG) &&
switches 0:5c4d7b2438d3 297 (memcmp(this->baseUUID, other.baseUUID, LENGTH_OF_LONG_UUID) == 0)) {
switches 0:5c4d7b2438d3 298 return true;
switches 0:5c4d7b2438d3 299 }
switches 0:5c4d7b2438d3 300
switches 0:5c4d7b2438d3 301 return false;
switches 0:5c4d7b2438d3 302 }
switches 0:5c4d7b2438d3 303
switches 0:5c4d7b2438d3 304 /**
switches 0:5c4d7b2438d3 305 * Overload != operator to enable UUID comparisons.
switches 0:5c4d7b2438d3 306 *
switches 0:5c4d7b2438d3 307 * @param[in] other
switches 0:5c4d7b2438d3 308 * The other UUID in the comparison.
switches 0:5c4d7b2438d3 309 *
switches 0:5c4d7b2438d3 310 * @return true if this != @p other, false otherwise.
switches 0:5c4d7b2438d3 311 */
switches 0:5c4d7b2438d3 312 bool operator!= (const UUID &other) const {
switches 0:5c4d7b2438d3 313 return !(*this == other);
switches 0:5c4d7b2438d3 314 }
switches 0:5c4d7b2438d3 315
switches 0:5c4d7b2438d3 316 private:
switches 0:5c4d7b2438d3 317 /**
switches 0:5c4d7b2438d3 318 * The UUID type. Refer to UUID_Type_t.
switches 0:5c4d7b2438d3 319 */
switches 0:5c4d7b2438d3 320 UUID_Type_t type;
switches 0:5c4d7b2438d3 321 /**
switches 0:5c4d7b2438d3 322 * The long UUID value.
switches 0:5c4d7b2438d3 323 */
switches 0:5c4d7b2438d3 324 LongUUIDBytes_t baseUUID;
switches 0:5c4d7b2438d3 325 /**
switches 0:5c4d7b2438d3 326 * The short UUID value.
switches 0:5c4d7b2438d3 327 */
switches 0:5c4d7b2438d3 328 ShortUUIDBytes_t shortUUID;
switches 0:5c4d7b2438d3 329 };
switches 0:5c4d7b2438d3 330
switches 0:5c4d7b2438d3 331 #endif // ifndef __UUID_H__