Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.
Fork of BLE_API by
uuid.cpp@0:ace2e8d3ce79, 2013-11-29 (annotated)
- Committer:
- ktownsend
- Date:
- Fri Nov 29 14:42:22 2013 +0000
- Revision:
- 0:ace2e8d3ce79
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:ace2e8d3ce79 | 1 | #include <stdio.h> |
ktownsend | 0:ace2e8d3ce79 | 2 | #include <string.h> |
ktownsend | 0:ace2e8d3ce79 | 3 | |
ktownsend | 0:ace2e8d3ce79 | 4 | #include "uuid.h" |
ktownsend | 0:ace2e8d3ce79 | 5 | |
ktownsend | 0:ace2e8d3ce79 | 6 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 7 | /*! |
ktownsend | 0:ace2e8d3ce79 | 8 | @brief Creates an empty 128-bit UUID |
ktownsend | 0:ace2e8d3ce79 | 9 | |
ktownsend | 0:ace2e8d3ce79 | 10 | @note This UUID must be assigned a valid value via the 'update' |
ktownsend | 0:ace2e8d3ce79 | 11 | function before it can be safely used! |
ktownsend | 0:ace2e8d3ce79 | 12 | */ |
ktownsend | 0:ace2e8d3ce79 | 13 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 14 | UUID::UUID(void) |
ktownsend | 0:ace2e8d3ce79 | 15 | { |
ktownsend | 0:ace2e8d3ce79 | 16 | memset(base, 0, 16); |
ktownsend | 0:ace2e8d3ce79 | 17 | value = 0; |
ktownsend | 0:ace2e8d3ce79 | 18 | type = UUID_TYPE_SHORT; |
ktownsend | 0:ace2e8d3ce79 | 19 | } |
ktownsend | 0:ace2e8d3ce79 | 20 | |
ktownsend | 0:ace2e8d3ce79 | 21 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 22 | /*! |
ktownsend | 0:ace2e8d3ce79 | 23 | @brief Creates a new 128-bit UUID |
ktownsend | 0:ace2e8d3ce79 | 24 | |
ktownsend | 0:ace2e8d3ce79 | 25 | @note The UUID is a unique 128-bit (16 byte) ID used to identify |
ktownsend | 0:ace2e8d3ce79 | 26 | different service or characteristics on the BLE device. |
ktownsend | 0:ace2e8d3ce79 | 27 | |
ktownsend | 0:ace2e8d3ce79 | 28 | @note When creating a UUID, the constructor will check if all bytes |
ktownsend | 0:ace2e8d3ce79 | 29 | except bytes 2/3 are equal to 0. If only bytes 2/3 have a |
ktownsend | 0:ace2e8d3ce79 | 30 | value, the UUID will be treated as a short/BLE UUID, and the |
ktownsend | 0:ace2e8d3ce79 | 31 | .type field will be set to UUID::UUID_TYPE_SHORT. If any |
ktownsend | 0:ace2e8d3ce79 | 32 | of the bytes outside byte 2/3 have a non-zero value, the UUID |
ktownsend | 0:ace2e8d3ce79 | 33 | will be considered a 128-bit ID, and .type will be assigned |
ktownsend | 0:ace2e8d3ce79 | 34 | as UUID::UUID_TYPE_LONG. |
ktownsend | 0:ace2e8d3ce79 | 35 | |
ktownsend | 0:ace2e8d3ce79 | 36 | @param[in] uuid_base |
ktownsend | 0:ace2e8d3ce79 | 37 | The 128-bit (16-byte) UUID value. For 128-bit values, |
ktownsend | 0:ace2e8d3ce79 | 38 | assign all 16 bytes. For 16-bit values, assign the |
ktownsend | 0:ace2e8d3ce79 | 39 | 16-bits to byte 2 and 3, and leave the rest of the bytes |
ktownsend | 0:ace2e8d3ce79 | 40 | as 0. |
ktownsend | 0:ace2e8d3ce79 | 41 | |
ktownsend | 0:ace2e8d3ce79 | 42 | @section EXAMPLE |
ktownsend | 0:ace2e8d3ce79 | 43 | |
ktownsend | 0:ace2e8d3ce79 | 44 | @code |
ktownsend | 0:ace2e8d3ce79 | 45 | |
ktownsend | 0:ace2e8d3ce79 | 46 | // Create a short UUID (0x180F) |
ktownsend | 0:ace2e8d3ce79 | 47 | uint8_t shortID[16] = { 0, 0, 0x0F, 0x18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
ktownsend | 0:ace2e8d3ce79 | 48 | UUID ble_uuid = UUID(shortID); |
ktownsend | 0:ace2e8d3ce79 | 49 | // ble_uuid.type = UUID_TYPE_SHORT |
ktownsend | 0:ace2e8d3ce79 | 50 | // ble_uuid.value = 0x180F |
ktownsend | 0:ace2e8d3ce79 | 51 | |
ktownsend | 0:ace2e8d3ce79 | 52 | // Creeate a long UUID |
ktownsend | 0:ace2e8d3ce79 | 53 | uint8_t longID[16] = { 0x00, 0x11, 0x22, 0x33, |
ktownsend | 0:ace2e8d3ce79 | 54 | 0x44, 0x55, 0x66, 0x77, |
ktownsend | 0:ace2e8d3ce79 | 55 | 0x88, 0x99, 0xAA, 0xBB, |
ktownsend | 0:ace2e8d3ce79 | 56 | 0xCC, 0xDD, 0xEE, 0xFF }; |
ktownsend | 0:ace2e8d3ce79 | 57 | UUID custom_uuid = UUID(longID); |
ktownsend | 0:ace2e8d3ce79 | 58 | // custom_uuid.type = UUID_TYPE_LONG |
ktownsend | 0:ace2e8d3ce79 | 59 | // custom_uuid.value = 0x3322 |
ktownsend | 0:ace2e8d3ce79 | 60 | // custom_uuid.base = 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF |
ktownsend | 0:ace2e8d3ce79 | 61 | |
ktownsend | 0:ace2e8d3ce79 | 62 | @endcode |
ktownsend | 0:ace2e8d3ce79 | 63 | */ |
ktownsend | 0:ace2e8d3ce79 | 64 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 65 | UUID::UUID(uint8_t const uuid_base[16]) |
ktownsend | 0:ace2e8d3ce79 | 66 | { |
ktownsend | 0:ace2e8d3ce79 | 67 | memcpy(base, uuid_base, 16); |
ktownsend | 0:ace2e8d3ce79 | 68 | value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); |
ktownsend | 0:ace2e8d3ce79 | 69 | |
ktownsend | 0:ace2e8d3ce79 | 70 | /* Check if this is a short of a long UUID */ |
ktownsend | 0:ace2e8d3ce79 | 71 | if (uuid_base[0] + uuid_base[1] + |
ktownsend | 0:ace2e8d3ce79 | 72 | uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + |
ktownsend | 0:ace2e8d3ce79 | 73 | uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + |
ktownsend | 0:ace2e8d3ce79 | 74 | uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) |
ktownsend | 0:ace2e8d3ce79 | 75 | { |
ktownsend | 0:ace2e8d3ce79 | 76 | type = UUID_TYPE_SHORT; |
ktownsend | 0:ace2e8d3ce79 | 77 | } |
ktownsend | 0:ace2e8d3ce79 | 78 | else |
ktownsend | 0:ace2e8d3ce79 | 79 | { |
ktownsend | 0:ace2e8d3ce79 | 80 | type = UUID_TYPE_LONG; |
ktownsend | 0:ace2e8d3ce79 | 81 | } |
ktownsend | 0:ace2e8d3ce79 | 82 | } |
ktownsend | 0:ace2e8d3ce79 | 83 | |
ktownsend | 0:ace2e8d3ce79 | 84 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 85 | /*! |
ktownsend | 0:ace2e8d3ce79 | 86 | @brief Creates a short (16-bit) UUID |
ktownsend | 0:ace2e8d3ce79 | 87 | |
ktownsend | 0:ace2e8d3ce79 | 88 | @param[in] ble_uuid |
ktownsend | 0:ace2e8d3ce79 | 89 | The 16-bit BLE UUID value. |
ktownsend | 0:ace2e8d3ce79 | 90 | */ |
ktownsend | 0:ace2e8d3ce79 | 91 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 92 | UUID::UUID(uint16_t const ble_uuid) |
ktownsend | 0:ace2e8d3ce79 | 93 | { |
ktownsend | 0:ace2e8d3ce79 | 94 | memset(base, 0, 16); |
ktownsend | 0:ace2e8d3ce79 | 95 | memcpy(base+2, (uint8_t *)&ble_uuid, 2); |
ktownsend | 0:ace2e8d3ce79 | 96 | value = ble_uuid; |
ktownsend | 0:ace2e8d3ce79 | 97 | type = UUID_TYPE_SHORT; |
ktownsend | 0:ace2e8d3ce79 | 98 | } |
ktownsend | 0:ace2e8d3ce79 | 99 | |
ktownsend | 0:ace2e8d3ce79 | 100 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 101 | /*! |
ktownsend | 0:ace2e8d3ce79 | 102 | @brief UUID destructor |
ktownsend | 0:ace2e8d3ce79 | 103 | */ |
ktownsend | 0:ace2e8d3ce79 | 104 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 105 | UUID::~UUID(void) |
ktownsend | 0:ace2e8d3ce79 | 106 | { |
ktownsend | 0:ace2e8d3ce79 | 107 | } |
ktownsend | 0:ace2e8d3ce79 | 108 | |
ktownsend | 0:ace2e8d3ce79 | 109 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 110 | /*! |
ktownsend | 0:ace2e8d3ce79 | 111 | @brief Updates the value of the UUID |
ktownsend | 0:ace2e8d3ce79 | 112 | |
ktownsend | 0:ace2e8d3ce79 | 113 | @args[in] uuid_base |
ktownsend | 0:ace2e8d3ce79 | 114 | The 128-bit value to use when updating the UUID. For |
ktownsend | 0:ace2e8d3ce79 | 115 | 16-bit IDs, insert the ID in bytes 2/3 in LSB format. |
ktownsend | 0:ace2e8d3ce79 | 116 | |
ktownsend | 0:ace2e8d3ce79 | 117 | @returns BLE_ERROR_NONE (0) if everything executed correctly, or an |
ktownsend | 0:ace2e8d3ce79 | 118 | error code if there was a problem |
ktownsend | 0:ace2e8d3ce79 | 119 | @retval BLE_ERROR_NONE |
ktownsend | 0:ace2e8d3ce79 | 120 | Everything executed correctly |
ktownsend | 0:ace2e8d3ce79 | 121 | |
ktownsend | 0:ace2e8d3ce79 | 122 | @section EXAMPLE |
ktownsend | 0:ace2e8d3ce79 | 123 | |
ktownsend | 0:ace2e8d3ce79 | 124 | @code |
ktownsend | 0:ace2e8d3ce79 | 125 | |
ktownsend | 0:ace2e8d3ce79 | 126 | @endcode |
ktownsend | 0:ace2e8d3ce79 | 127 | */ |
ktownsend | 0:ace2e8d3ce79 | 128 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 129 | ble_error_t UUID::update(uint8_t const uuid_base[16]) |
ktownsend | 0:ace2e8d3ce79 | 130 | { |
ktownsend | 0:ace2e8d3ce79 | 131 | memcpy(base, uuid_base, 16); |
ktownsend | 0:ace2e8d3ce79 | 132 | value = (uint16_t)((uuid_base[3] << 8) | (uuid_base[2])); |
ktownsend | 0:ace2e8d3ce79 | 133 | |
ktownsend | 0:ace2e8d3ce79 | 134 | /* Check if this is a short of a long UUID */ |
ktownsend | 0:ace2e8d3ce79 | 135 | if (uuid_base[0] + uuid_base[1] + |
ktownsend | 0:ace2e8d3ce79 | 136 | uuid_base[4] + uuid_base[5] + uuid_base[6] + uuid_base[7] + |
ktownsend | 0:ace2e8d3ce79 | 137 | uuid_base[8] + uuid_base[9] + uuid_base[10] + uuid_base[11] + |
ktownsend | 0:ace2e8d3ce79 | 138 | uuid_base[12] + uuid_base[13] + uuid_base[14] + uuid_base[15] == 0) |
ktownsend | 0:ace2e8d3ce79 | 139 | { |
ktownsend | 0:ace2e8d3ce79 | 140 | type = UUID_TYPE_SHORT; |
ktownsend | 0:ace2e8d3ce79 | 141 | } |
ktownsend | 0:ace2e8d3ce79 | 142 | else |
ktownsend | 0:ace2e8d3ce79 | 143 | { |
ktownsend | 0:ace2e8d3ce79 | 144 | type = UUID_TYPE_LONG; |
ktownsend | 0:ace2e8d3ce79 | 145 | } |
ktownsend | 0:ace2e8d3ce79 | 146 | |
ktownsend | 0:ace2e8d3ce79 | 147 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 148 | } |
ktownsend | 0:ace2e8d3ce79 | 149 | |
ktownsend | 0:ace2e8d3ce79 | 150 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 151 | /*! |
ktownsend | 0:ace2e8d3ce79 | 152 | @brief Updates the value of the UUID |
ktownsend | 0:ace2e8d3ce79 | 153 | |
ktownsend | 0:ace2e8d3ce79 | 154 | @args[in] ble_uuid |
ktownsend | 0:ace2e8d3ce79 | 155 | The 16-bit value to use when updating the UUID. |
ktownsend | 0:ace2e8d3ce79 | 156 | |
ktownsend | 0:ace2e8d3ce79 | 157 | @returns BLE_ERROR_NONE (0) if everything executed correctly, or an |
ktownsend | 0:ace2e8d3ce79 | 158 | error code if there was a problem |
ktownsend | 0:ace2e8d3ce79 | 159 | @retval BLE_ERROR_NONE |
ktownsend | 0:ace2e8d3ce79 | 160 | Everything executed correctly |
ktownsend | 0:ace2e8d3ce79 | 161 | |
ktownsend | 0:ace2e8d3ce79 | 162 | @section EXAMPLE |
ktownsend | 0:ace2e8d3ce79 | 163 | |
ktownsend | 0:ace2e8d3ce79 | 164 | @code |
ktownsend | 0:ace2e8d3ce79 | 165 | |
ktownsend | 0:ace2e8d3ce79 | 166 | @endcode |
ktownsend | 0:ace2e8d3ce79 | 167 | */ |
ktownsend | 0:ace2e8d3ce79 | 168 | /**************************************************************************/ |
ktownsend | 0:ace2e8d3ce79 | 169 | ble_error_t UUID::update(uint16_t const ble_uuid) |
ktownsend | 0:ace2e8d3ce79 | 170 | { |
ktownsend | 0:ace2e8d3ce79 | 171 | memset(base, 0, 16); |
ktownsend | 0:ace2e8d3ce79 | 172 | memcpy(base+2, (uint8_t *)&ble_uuid, 2); |
ktownsend | 0:ace2e8d3ce79 | 173 | value = ble_uuid; |
ktownsend | 0:ace2e8d3ce79 | 174 | type = UUID_TYPE_SHORT; |
ktownsend | 0:ace2e8d3ce79 | 175 | |
ktownsend | 0:ace2e8d3ce79 | 176 | return BLE_ERROR_NONE; |
ktownsend | 0:ace2e8d3ce79 | 177 | } |