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

For Aggregate Format Descriptor - Descriptor handles are not accessible (solution provided) #20

Open
MichaelRoop opened this issue Feb 12, 2021 · 1 comment

Comments

@MichaelRoop
Copy link

MichaelRoop commented Feb 12, 2021

The handles are required when you want to create and Aggregate Format Descriptor. It requires the handles of the previously created Format Descriptors as it's data packet. It can contain one or many Format descriptors that the define the payload of the owning Characteristic.

On the App side, the Format Descriptors are used in order of inclusion in the Aggregate Format Descriptor to parse sections of the data packet received from that Characteristic

I am in the process of exposing the handle() function to test viability of the change. Since it is a const function it should not impact the value

@MichaelRoop
Copy link
Author

MichaelRoop commented Feb 14, 2021

I have cloned your repo and am doing work on my own branch to track changes, Let me know if I can push the branch to your repo

After proof of concept I did some refactoring so changes would better fit BLEArduino architecture. Here are the only changes remaining for the Aggregate support

BLELocalDescriptor.h

  • uint16_t handle() const; moved to public
  • New function declaration: writeValue(const uint8_t value[], int length);
  • uint8_t* _value: removed const

BLELocalDescriptor.cpp

  • Implemented writeValue function
int BLELocalDescriptor::writeValue(const uint8_t value[], int length) {
  memcpy(_value, value, min(_valueSize, length));
  return 1;
}

BLEDescriptor.h

  • New uint16_t handle() function declaration
  • New function declaration: writeValue(const uint8_t value[], int length);

BLEDescriptor.cpp

  • Implement handle function
  • Implement writeValue function
uint16_t BLEDescriptor::handle() const
{
    if (_local) {
        return _local->handle();
    }
    if (_remote) {
        return _remote->handle();
    }
    return NULL;
}

int BLEDescriptor::writeValue(const uint8_t value[], int length) 
{
    if (_local) 
    {
        return _local->writeValue(value, length);
    }
    if (_remote) 
    {
        return _remote->writeValue(value, length);
    }
}

Here is a sample of what usage would look like

// Characteristic which transmits the composite data
// 1 byte var, 2 byte var, and 4 byte var
uint8_t dataBlock[]{ 0x0,0x0,0x0,0x0,0x0,0x0,0x00 };
BLECharacteristic multiDataCharacteristic ("F001", BLERead | BLENotify, sizeof(dataBlock), true);

// Format descriptor for the uint8_t
uint8_t formatUint8[7] = {
	0x4,		// BLE spec46 = uint8t. Defines size of data in characteristic read 
	0,	// Exponent display 0
	0x0, 0x0 ,	// Measurement unit-less
	0x0,		// Namespace byte. 0 undefined
	0x0, 0x0	// Description uint
};
// BLE spec 0x2904 is the Format Descriptor
BLEDescriptor descriptorFormat_Uint8("2904", formatUint8, sizeof(formatUint8));

//Descriptror format for uint 16, 2 place exponent display
uint8_t formatUint16[] = {	0x6, (byte)-2, 0x0, 0x0 , 0x0, 0x0, 0x0 };
BLEDescriptor descriptorFormat_Uint16("2904", formatUint16, sizeof(formatUint16));

//Descriptror format for uint 32, 3 place exponent display
uint8_t formatUint32[] = { 0x8, (byte)-3, 0x0, 0x0 , 0x0, 0x0, 0x0 };
BLEDescriptor descriptorFormat_Uint32("2904", formatUint32, sizeof(formatUint32));

// Aggregate Format Descriptor 0x2905. Data block for 3 uint16_t Format Descriptor handles
uint8_t aggFormatHandles[] { 0x0, 0x0, 0x0, 0x0,0x0, 0x0 };
BLEDescriptor descriptorFormat_Aggregate("2905", aggFormatHandles, sizeof(aggFormatHandles));

void setup() {
	Serial.begin(9600);
	while (!Serial) {}

	if (!BLE.begin()) {
		Serial.println("FAIL");
		while (1);
	}

	BLE.setLocalName("MR Agg BLE"); // visible in search
	BLE.setDeviceName("MR Agg BLE Device");
	BLE.setAppearance(GENERIC_ACCESS_APPEARANCE_ID);
	BLE.setEventHandler(BLEConnected, bleOnConnectHandler);
	BLE.setEventHandler(BLEDisconnected, bleOnDisconnectHandler);

	// Add descriptors to Characteristic and Characteristic to Service
	multiDataCharacteristic.addDescriptor(descriptorFormat_Aggregate);
	multiDataCharacteristic.addDescriptor(descriptorFormat_Uint8);
	multiDataCharacteristic.addDescriptor(descriptorFormat_Uint16);
	multiDataCharacteristic.addDescriptor(descriptorFormat_Uint32);
	ioService.addCharacteristic(multiDataCharacteristic);
	BLE.addService(ioService);

	// Set Aggregate value (Array of Format handles) here 
	// since they are all 0 until set with BLE.addService(ioService);
	CopyHandleToAggregateValue(0, descriptorFormat_Uint8.handle());
	CopyHandleToAggregateValue(1, descriptorFormat_Uint16.handle());
	CopyHandleToAggregateValue(2, descriptorFormat_Uint32.handle());
       
        // write the new value with Format Descriptor handles to the Aggregate Format Descriptor
	descriptorFormat_Aggregate.writeValue(aggFormatHandles, sizeof(aggFormatHandles));

	BLE.setAdvertisedService(ioService);
	BLE.advertise();
	Serial.println("BLE started..");
}

void CopyHandleToAggregateValue(int pos, uint16_t handle) {
	uint16_t* ptr = (uint16_t*)aggFormatHandles;
	memcpy(&ptr[pos], &handle, sizeof(uint16_t));
}

@MichaelRoop MichaelRoop changed the title Descriptor handles are not accessible Descriptor handles are not accessible (solution provided) Feb 14, 2021
@MichaelRoop MichaelRoop changed the title Descriptor handles are not accessible (solution provided) For Aggregate Format Descriptor - Descriptor handles are not accessible (solution provided) Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant