-
Notifications
You must be signed in to change notification settings - Fork 7k
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
bluetooth: conn: move bt_conn_lookup_index
to public header
#85546
bluetooth: conn: move bt_conn_lookup_index
to public header
#85546
Conversation
a1364a2
to
1ca358f
Compare
include/zephyr/bluetooth/conn.h
Outdated
@@ -718,6 +718,20 @@ void bt_conn_foreach(enum bt_conn_type type, | |||
*/ | |||
struct bt_conn *bt_conn_lookup_addr_le(uint8_t id, const bt_addr_le_t *peer); | |||
|
|||
/** | |||
* @brief Look up an existing connection by index. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this API have some concerns with regards to thread safety?
Let's say that the user at some point stores index 1
. And then later calls bt_conn_lookup_index(1)
. I assume it cannot know that the returned connection is the same connection as used last time.
Nevertheless, I'm sure there are good use cases for this API. But we should make this clear to the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this API have some concerns with regards to thread safety?
I can't see why the concerns would be any different to bt_conn_index
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just wondering: An index cannot be used as input to any of the Bluetooth APIs, so the user cannot do anything "wrong/unexpected". When the user gets a connection reference, this is no longer true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a fair point from @rugeGerritsen
Our APIs use a conn pointer instead of an index for a reason, and there may be unknown side effects from the higher layers relying on the index instead of the point.
That being said, I don't think this is actually any different than our APIs for
int bt_hci_get_conn_handle(const struct bt_conn *conn, uint16_t *conn_handle);
struct bt_conn *bt_hci_conn_lookup_handle(uint16_t handle);
So any issues/challenges that may or not be with this suggestion function, already exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should document the following clearly: There are two distinct use cases for bt_conn_lookup_index
, as follows:
- The first use case is for looping over all connections. In this case, the user must not care about finding a specific connection, and there are no guarantees for that.
- The second use case is to round-trip a reference trough
bt_conn_index
and back again. In this case, the index is a reified reference, similar to abt_conn
pointer. For the purpose of tracking references, we saybt_conn_index
moved the original reference into the index it returned. If the caller does not have a valid reference,bt_conn_lookup_index
is not guaranteed to return the same connection object (or return one at all).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhedberg That's reasonable. But
bt_conn_lookup_index
plays no role in this use case. Is this an argument to close this PR?As long as the "protocol data" contains information that the connection "is alive" the reverse lookup can still make sense and be useful.
I think we agree then. So long as the caller of bt_conn_lookup_index
can prove the object it got that index from is still alive, then that's proof bt_conn_lookup_index
returns that object. Tracking references is one type of proof, but I agree it's not the only one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To steer this thread back on track: Does anyone have a good suggestion for API documentation for the average user to make it easy to use the new API correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should/could we align this with bt_conn_foreach
?
bt_conn_foreach
uses bt_conn_ref
to determine if the conn
object has a reference, i.e. it only iterates on those that have a non-0 reference counter. Should bt_conn_lookup_index
return NULL
if the reference counter is 0
?
Do we want the possible return values of bt_conn_lookup_index
and bt_conn_foreach
to differ?
If not, then I guess we could/should align the documentation of those 2 functions, as they effectively provide the same feature.
That also reminded me that bt_conn_lookup_index
should take the type into account as well.
A ISO bt_conn
object and an ACL bt_conn
object may have the same index, so with the current suggested API we may have
uint8_t index = bt_conn_index(iso_conn); /* returns 0 */
struct bt_conn *conn = bt_conn_lookup_index(index); /* returns ACL at index 0 */
This hasn't been a problem when used internally, as that has always been used in an ACL context, but that cannot be guaranteed in a API context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
bt_conn_lookup_index
returnNULL
if the reference counter is0
?
Doesn't it already do that? Internally it calls bt_conn_ref()
which returns NULL if the refcount is 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
bt_conn_lookup_index
returnNULL
if the reference counter is0
?Doesn't it already do that? Internally it calls
bt_conn_ref()
which returns NULL if the refcount is0
.
Ah, you are right. Then we can align with the documentation of bt_conn_foreach
as they effectively do the same. When we add the type paramter to bt_conn_lookup_index
, we can even just call bt_conn_lookup_index
from bt_conn_foreach
to ensure that their behavior is similar.
1ca358f
to
55d4334
Compare
b83a0b4
to
bb2b79e
Compare
bb2b79e
to
3e236e9
Compare
Move the `bt_conn_lookup_index` declaration to a public header. This function is essentially the inverse of `bt_conn_index`, which is already public. Signed-off-by: Jordan Yates <[email protected]>
3e236e9
to
23e7427
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see the suggest missing documentation from #85546 (comment) being added.
A conn object returned by this function my not be connected nor contain valid values, which should be made clear to the user
Who knew this internal function was so problematic :) |
It may indeed not be connected, but it should contain valid values since the API only returns objects with a pre-existing refcount > 0 |
My perspective:
If any additional documentation is required, that should also be added to |
That's all fair enough but is a pretty big explosion of scope from just moving a function from an internal to a public header. |
That's fair :) Feel free to create a GH issue requesting this to become public, and then we can be aware of the use case and request and then use that to (hopefully) implement this in the future |
My interpretation of the blockers here was that the existing documentation (and implementation?) of the (already public) |
You can indeed use |
Move the
bt_conn_lookup_index
declaration to a public header. This function is essentially the inverse ofbt_conn_index
, which is already public.