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

Add NimBLEAttValue class. #286

Merged
merged 1 commit into from
Jan 17, 2022
Merged

Add NimBLEAttValue class. #286

merged 1 commit into from
Jan 17, 2022

Conversation

h2zero
Copy link
Owner

@h2zero h2zero commented Sep 15, 2021

This is a specialized container class to hold BLE attribute values.

  • Removes the use of std::string previously used to store the values.
  • Allows for setting/getting/notifying values from std::string, std::vector<uint8_t>, Arduino String, const char*, and uint8_t buffers.
  • Has operators retrieve the value as std::string, Arduino String, std::vector<uint8_t>, uint8_t* or char pointers.
  • Includes iterators and subscript/random access operator.
  • Introduces a max length parameter to the creation of server characteristics/descriptors to limit the size of the memory footprint.
  • Nearly Seamless integration with existing code.
  • Adds a config option to enable/disable timestamp storage when the value is updated.
  • Adds a config option to specify the initial size of the value container if not specified in the constructor.

Examples

Writing POD types to a remote characteristic:

  • pRemoteChar->writeValue(10);
  • pRemoteChar->writeValue("TEST");
  • pRemoteChar->writeValue(23.45);
  • pRemoteChar->writeValue('X');

Write container types to a remote characteristic:

  • pRemoteChar->writeValue(String("test string");
  • pRemoteChar->writeValue(std::string("test std::string");
  • pRemoteChar->writeValue(std::vector<uint8_t>({1,2,3,4,5,6}));
  • pRemoteChar->writeValue(NimBLEAttValue({1,2,3,4,5,6}));

Reading POD types from a remote characteristic:

  • int i_val = pRemoteChar->readValue<int>();
  • `double d_val = pRemoteChar->readValue();
  • `const uint8_t* u8p = pRemoteChar->readValue();

Reading container types from a remote characteristic:

  • std::string ss_str(pRemoteChar->readValue());
  • String ar_str = pRemoteChar->readValue();
  • std::vector<uint8_t> vec = pRemoteChar->readValue();
  • NimBLEAttValue att_val = pRemoteChar->readValue();

Reading a single byte from the value:

uint8_t val = pRemoteChar->readValue()[2];

Printing the hex bytes of a value:

for (auto it :  pRemoteChar->readValue()) {
    printf("value: %02x\n", it);
 }

Note: The server attributes work the same way but with setValue()/getVaue()

Directly using the NimBLEAttValue class:

// create an attribute value with a maximum capacity of 8 bytes, initialized to "TEST" (an extra byte will be allocated for the NULL) 
NimBLEAttValue my_att_val("TEST", 8); 

my_att_val += "1234"; // Value now contains "TEST1234"
printf("my_att_val = %s\n", my_att_val.c_str()); // use the c_str() method, print's "TEST1234"

// Concatenate a String and a NimBLEAttValue (same use for std::string)
String test_str = "my att value is: ";
test_str += my_att_val;
Serial.println(test_str);

src/NimBLEAttValue.h Outdated Show resolved Hide resolved
src/NimBLEAttValue.h Outdated Show resolved Hide resolved
@h2zero
Copy link
Owner Author

h2zero commented Jan 14, 2022

Finally got around to cleaning this up, if anyone would like to test it out please provide feedback. I'd be especially interested in some criticism from any C++ template wizards out there.

@h2zero h2zero force-pushed the attribute-value branch 5 times, most recently from 04626ec to 6b25c84 Compare January 16, 2022 04:43
This is a specialized container class to hold BLE attribute values.

- Removes the use of std::string previously used to store the values.
- Allows for setting/getting/notifying values from std::string, std::vector<uint8_t>, Arduino String, const char*, and uint8_t buffers.
- Has operators retrieve the value as std::string, Arduino String, std::vector<uint8_t>, uint8_t* or char pointers.
- Includes iterators and subscript/random access operator.
- Introduces a max length parameter to the creation of server characteristics/descriptors to limit the size of the memory footprint.
- Nearly Seamless integration with existing code.
- Adds a config option to enable/disable timestamp storage when the value is updated.
- Adds a config option to specify the initial size of the value container if not specified in the constructor.
@h2zero
Copy link
Owner Author

h2zero commented Jan 17, 2022

Merging this as I have seen no issues so far in long term testing.

@h2zero h2zero merged commit 7020e28 into master Jan 17, 2022
@h2zero h2zero deleted the attribute-value branch January 17, 2022 03:28
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

Successfully merging this pull request may close these issues.

2 participants