Thanks to visit codestin.com
Credit goes to github.com

Skip to content

NimBLEScan results are incomplete! #45

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

Closed

Conversation

Jeroen88
Copy link

@Jeroen88 Jeroen88 commented May 15, 2020

Add NimBLEAdvertisedDevice copy constructor and use it in NimBLEScanResults::getDevice(), because otherwise the std::string class member variables of the advertisedDevice is not returned correctly by NimBLEScan::getResults() and NimBLEScan::start(). Only the data from the BLEAdvertisedDeviceCallbacks (so on a per device basis) is correct.
I think (but I am not a 100% sure) the root cause is because std::map<std::string, NimBLEAdvertisedDevice*> m_advertisedDevicesMap in NimBLEScanResults is pointer based, and thus the NimBLEAdvertisedDevice* pointers are copied and not constructed.

The uint8_t *payload pointer may still be dangling!

…dvertisedDevice is not returned correctly by NimBLEScan::getResults() and NimBLEScan::start(). Only the data from the BLEAdvertisedDeviceCallbacks (so per device) is correct
@Jeroen88 Jeroen88 mentioned this pull request May 15, 2020
@h2zero
Copy link
Owner

h2zero commented May 17, 2020

Good catch, can't say I've used that method much. Any reason not to just return a pointer/reference there instead of copying?

@Jeroen88
Copy link
Author

Well I did not design the API! ;)

I added NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address) in PR #46, and this works correctly without the copy constructor. However, I think it is not correct to return non-const pointers internal structures that should not be modified by the program. But non-const pointers are returned everywhere in this library! If we make them const, as suggested in PR #42, the user program should update all pointer variables to be const also, so this is not without impact.

If we return a reference we should come up with a solution to a getDevice() out-of-bounds. Now a nullptr is returned, then some kind of empty device should be returned.

Speaking of returning references, I think it would be cool to return references everywhere instead of returning pointers. But then we should introduce also 'empty objects' for (intermediate) non-existent services or characteristics. Also some kind of 'check breadcrumb trail' should be introduced in each leaf of the service.characteristic.descriptor tree, for the user to check, if the retrieval of a value fails, until what level the path is valid. This heavily changes the API!

@Jeroen88
Copy link
Author

I did some additional tests and I think the copy constructor is not necessary. Closing for now.

@Jeroen88 Jeroen88 closed this May 17, 2020
@h2zero
Copy link
Owner

h2zero commented May 17, 2020

Yes ideally a const reference should be returned wherever internal structures are returned but that's a pretty big ask. I don't mind things as they are so much since it would be the API user that messes up the data if they change things, that;s on them 😃. The only other alternative is to return copies of the data, which I don't like, but is necessary sometimes.

@Jeroen88 Jeroen88 deleted the bugfix/NimBLEAdvertisedDevice branch May 23, 2020 21:03
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