Skip to content

RFC: ble: utils: Add some utils when advertising only Hubble data#41

Draft
ceolin wants to merge 4 commits intoHubbleNetwork:mainfrom
ceolin:ble/utils
Draft

RFC: ble: utils: Add some utils when advertising only Hubble data#41
ceolin wants to merge 4 commits intoHubbleNetwork:mainfrom
ceolin:ble/utils

Conversation

@ceolin
Copy link
Copy Markdown
Collaborator

@ceolin ceolin commented Nov 12, 2025

Add some helpers to facilitate when advertising data only using Hubble service.

  • The same utils used across different ports (Zephyr, TI, esp-idf).

Add a helper macro that provides a Hubble BLE payload ready to
advertise.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
Simplify the BLE logic using the recently added macro,
HUBBLE_BLE_ADV_*.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
Simplify the BLE logic using the recently added macro,
HUBBLE_BLE_ADV_*.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>
Simplify the BLE logic using the recently added macro,
HUBBLE_BLE_ADV_*.

Signed-off-by: Flavio Ceolin <flavio@hubblenetwork.com>

#include <zephyr/bluetooth/bluetooth.h>

#define HUBBLE_BLE_UTILS_BUFFER_SIZE 25
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if I'd be better if there are headers common to both zephyr and other platforms. It might be confusing when on zephyr the utils buffer is 25 and others are 31. Maybe something like this:

#define BLE_ADV_LEN 31
#define HUBBLE_BLE_ADV_HEADER_SIZE 6

#ifdef __ZEPHYR__
#define HUBBLE_BLE_UTILS_BUFFER_SIZE (BLE_ADV_LEN - HUBBLE_BLE_ADV_HEADER_SIZE)
#endif

something like that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, that is the thing this symbol does not need to be exported and should be irrelevant for the user, Zephyr will never need 31 bytes but others yes since we return the raw buffer. I could make it a config option changing the default according with the system but it is one more symbol to deal with. Alternatively we can make explicit it is internal ...

({ \
int _name##_err; \
memset(&_name[6], 0, HUBBLE_BLE_UTILS_BUFFER_SIZE - 6); \
*(_out_len) = HUBBLE_BLE_UTILS_BUFFER_SIZE - 6; \
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to my comment on common constants. Instead of the magic number 6, we could have done like:
*(_out_len) = HUBBLE_BLE_UTILS_BUFFER_SIZE - HUBBLE_BLE_ADV_HEADER_SIZE;

or maybe factor this (HUBBLE_BLE_UTILS_BUFFER_SIZE - HUBBLE_BLE_ADV_HEADER_SIZE) out as constant as well since it's common.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So ... the problem is that you start to leak symbols that are irrelevant for the application and this is a public header. We need to minimize exported symbols that are not usable by the application. I rather have a comment explaining it than a symbol that is not even used in all ports ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I forgot about the part that this is a utility header. Yeah I guess an explanation comment would suffice then.

_name##_err = hubble_ble_advertise_get(_data, _len, &_name[6], \
_out_len); \
if (_name##_err == 0) { \
_name[4] = *(_out_len) + 1; \
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also let 4 be #define BLE_SERVICE_DATA_POSTION 4 as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer from above ... is this usable by the application ? So why export it in a public header ? Is it used anywhere else ?

if (status) {
return;
}
HUBBLE_BLE_ADV_DATA_SET(advData, NULL, 0, &len);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't check return status?

NULL, 0, &advData[HUBBLE_BLE_ADV_HEADER_SIZE], &len) != 0) {
return (FAILURE);
}
HUBBLE_BLE_ADV_DATA_SET(advData, NULL, 0, &len);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, no return status check?

_name[1].data_len = *(_out_len); \
_name[1].type = BT_DATA_SVC_DATA16; \
_name[1].data = _name##_adv_buf; \
*(_out_len) = 2; \
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good to me except this line. What happened if the bt_entries is more than 2 (like the current zephyr ble_beacon sample)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that will not the use case for this macro. I think I have commented but the idea is that this macro is used only when the only advertise service is ours. The reason is because it is not only one more entry but you need to add the service list as well ...

The idea here was to simplify for the easy (maybe common) case where the only service is Hubble. Anyone that needs something more complex should use regular API.

Does it make sense ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that makes sense. This probably needs to be updated/mentioned in the docs and README as well

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