Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BLEUUID toString #3289

Merged
merged 2 commits into from Oct 1, 2019

Conversation

@tanakamasayuki
Copy link
Contributor

commented Sep 27, 2019

uuid16 is Missing first 4 characters.
uuid is Missing last 2 characters.

UUID string format:
AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
123456789012345678901234567890123456 < 36 characters + null

Test code

#include "BLEDevice.h"

void setup() {
  Serial.begin(115200);
  delay(1000);

  BLEUUID uuid("00001812-0000-1000-8000-00805F9B34FB");
  Serial.printf("uuid     : %s\n", uuid.toString().c_str());

  BLEUUID uuid16("1812");
  Serial.printf("uuid16   : %s\n", uuid16.toString().c_str());

  BLEUUID uuid16_2(uuid16.toString().c_str());
  Serial.printf("uuid16_2 : %s\n", uuid16_2.toString().c_str());

  BLEUUID uuid32("00001812");
  Serial.printf("uuid32   : %s\n", uuid32.toString().c_str());
}

void loop() {
}

1.0.3 Run

uuid     : 00001812-0000-1000-8000-00805f9b34
uuid16   : 1812-0000-1000-8000-00805f9b34fb
uuid16_2 : <NULL>
uuid32   : 00001812-0000-1000-8000-00805f9b34fb

uuid -> Missing last 2 characters
uuid16 -> Missing first 4 characters
uuid32 -> Good

This patch

uuid     : 00001812-0000-1000-8000-00805f9b34fb
uuid16   : 00001812-0000-1000-8000-00805f9b34fb
uuid16_2 : 00001812-0000-1000-8000-00805f9b34fb
uuid32   : 00001812-0000-1000-8000-00805f9b34fb
uuid16 is Missing first 4 characters.
uuid is Missing last 2 characters.
@me-no-dev

This comment has been minimized.

Copy link
Member

commented Sep 29, 2019

what is the purpose of those 4 leading zeroes? I have never seen them printed or discussed before.

@tanakamasayuki

This comment has been minimized.

Copy link
Contributor Author

commented Sep 30, 2019

The UUID format is specified.

https://en.wikipedia.org/wiki/Universally_unique_identifier

123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).

https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/

BASE_UUID | 00000000-0000-1000-8000-00805F9B34FB

/libraries/BLE/src/BLEUUID.cpp

/**
 * @brief Create a UUID from a string.
 *
 * Create a UUID from a string.  There will be two possible stories here.  Either the string represents
 * a binary data field or the string represents a hex encoding of a UUID.
 * For the hex encoding, here is an example:
 *
 * ```
 * "beb5483e-36e1-4688-b7f5-ea07361b26a8"
 *  0 1 2 3  4 5  6 7  8 9  0 1 2 3 4 5
 *  12345678-90ab-cdef-1234-567890abcdef
 * ```
 *
 * This has a length of 36 characters.  We need to parse this into 16 bytes.
 *
 * @param [in] value The string to build a UUID from.
 */
BLEUUID::BLEUUID(std::string value) {
	m_valueSet = true;
	if (value.length() == 4) {
		m_uuid.len         = ESP_UUID_LEN_16;
		m_uuid.uuid.uuid16 = 0;
		for(int i=0;i<value.length();){
			uint8_t MSB = value.c_str()[i];
			uint8_t LSB = value.c_str()[i+1];
			
			if(MSB > '9') MSB -= 7;
			if(LSB > '9') LSB -= 7;
			m_uuid.uuid.uuid16 += (((MSB&0x0F) <<4) | (LSB & 0x0F))<<(2-i)*4;
			i+=2;	
		}
	}
	else if (value.length() == 8) {
		m_uuid.len         = ESP_UUID_LEN_32;
		m_uuid.uuid.uuid32 = 0;
		for(int i=0;i<value.length();){
			uint8_t MSB = value.c_str()[i];
			uint8_t LSB = value.c_str()[i+1];
			
			if(MSB > '9') MSB -= 7; 
			if(LSB > '9') LSB -= 7;
			m_uuid.uuid.uuid32 += (((MSB&0x0F) <<4) | (LSB & 0x0F))<<(6-i)*4;
			i+=2;
		}		
	}
	else if (value.length() == 16) {  // how we can have 16 byte length string reprezenting 128 bit uuid??? needs to be investigated (lack of time)
		m_uuid.len = ESP_UUID_LEN_128;
		memrcpy(m_uuid.uuid.uuid128, (uint8_t*)value.data(), 16);
	}
	else if (value.length() == 36) {
		// If the length of the string is 36 bytes then we will assume it is a long hex string in
		// UUID format.
		m_uuid.len = ESP_UUID_LEN_128;
		int n = 0;
		for(int i=0;i<value.length();){
			if(value.c_str()[i] == '-')
				i++;
			uint8_t MSB = value.c_str()[i];
			uint8_t LSB = value.c_str()[i+1];
			
			if(MSB > '9') MSB -= 7; 
			if(LSB > '9') LSB -= 7;
			m_uuid.uuid.uuid128[15-n++] = ((MSB&0x0F) <<4) | (LSB & 0x0F);
			i+=2;	
		}
	}
	else {
		log_e("ERROR: UUID value not 2, 4, 16 or 36 bytes");
		m_valueSet = false;
	}
} //BLEUUID(std::string)
 * This has a length of 36 characters.  We need to parse this into 16 bytes.
	else if (value.length() == 36) {

If it is 32 characters, an input error occurs.

@me-no-dev me-no-dev merged commit 71e3d51 into espressif:master Oct 1, 2019
21 checks passed
21 checks passed
Arduino 0 on ubuntu-latest
Details
Arduino 1 on ubuntu-latest
Details
Arduino 2 on ubuntu-latest
Details
Arduino 3 on ubuntu-latest
Details
Arduino 4 on ubuntu-latest
Details
Arduino 5 on ubuntu-latest
Details
Arduino 6 on ubuntu-latest
Details
Arduino 7 on ubuntu-latest
Details
Arduino 8 on ubuntu-latest
Details
Arduino 9 on ubuntu-latest
Details
Arduino 10 on ubuntu-latest
Details
Arduino 11 on ubuntu-latest
Details
Arduino 12 on ubuntu-latest
Details
Arduino 13 on ubuntu-latest
Details
Arduino 14 on ubuntu-latest
Details
Arduino on windows-latest
Details
Arduino on macOS-latest
Details
PlatformIO on ubuntu-latest
Details
PlatformIO on windows-latest
Details
PlatformIO on macOS-latest
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.