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

Refactor BDAddr #81

Merged
merged 13 commits into from
Mar 10, 2021
Merged

Conversation

MattesWhite
Copy link
Contributor

This PR follows the discussion of #68.

At last I found some time to work on this. For now I refactored BDAddr as a show case what I intend to do. If the implementation is approved I'll start working on UUID. Of course I'll include the changed suggested in #80 as well.

Initially, I wanted to write the parsers with nom but I recognized that there is a bunch of nom v4 code in the crate so I settled for a 'hand-crafted' version. Another solution would be to use regex but I didn't wanted to pull in another dependency without asking.

@qdot
Copy link
Contributor

qdot commented Oct 5, 2020

I definitely like the direction this is going, though do note that your current version is failing on macOS and Windows.

I think you're fine to move on to UUID, though I did bring in #80 (as I'm about to do a quick point release to fix Windows UWP reads), but you can integrate that in as you need.

The nom dependency thing is killing me too, I'm really hoping to get back to this one day and possibly just move us to actual dbus usage. :(

I don't think bringing in regex would be a huge issue? But being an ex browser dev I'm also kind of oblivious to compile times and all that, and the library I use this for is... pretty violently monolithic so I have like 30+ dependencies there. :)

@MattesWhite
Copy link
Contributor Author

The BDAddr part is now finalized. All pipelines should only fail because of legacy code by now.

A question of style: Do you prefer having bigger modules (src/api/mod.rs has now more than 500 lines without tests) or smaller once, e.g. put BDAddr and UUID into submodules and re-export them? Personally, I'd prefer the latter but I'll align with your choice of style.

Once, I did a refactoring from nom v4 to nom v5 so I'm willing to take care of this. However, the issue of having no RSSI values available (#76) is way more pressing for me. In addition, there is already worked on nom v6 which probably will improve support for custom errors so waiting a little bit and solving #48 first is maybe not that bad.

For now I'll focus on refactoring UUIDs.

@qdot
Copy link
Contributor

qdot commented Oct 10, 2020

Looks like everything built on the latest commit, so that's positive.

And smaller modules, please! The big modules came in with the libraries I imported, I'm much more a fan of submodules and re-exporting.

For the nom stuff, it's really hard to say how much that matters other than the conflicting dependencies, especially if we start looking toward dbus again for bluez. The RSSI is now important for me too (My library that depends on this now advertises RSSI relay capabilities, and yet, I have no RSSI here... >.> ), so hopefully that can be looked at soon.

@MattesWhite
Copy link
Contributor Author

So first work on UUIDs is done. I decided that its most practical to use uuid::Uuid wherever possible but I kept the short-value variant which has nice benefits like use parse and format functions of uuid crate.

I can't guarantee that the Windows version still works or that I didn't introduce bugs. I tried to adapt what was possible but when I the need to reverse bytes here and there makes my head hurt. In addition, I have only a Linux machine to test, so I don't even know if it compiles. Is there a way to do (at least) a cargo check for another OS? All in all, it would be really nice if someone could verify the code on a Windows machine.

@qdot
Copy link
Contributor

qdot commented Oct 12, 2020

After the issues I had with the macOS read/write patches, I'll be testing this throughly across all platforms before landing, because it directly affects users of my libraries that depend on this. 👍

So far, according to CI it looks like the only thing catching you on windows is a &u16 to u32 miscast, so not too horrible?

@MattesWhite
Copy link
Contributor Author

Yes the fixing errors caught by the pipelines is easy but I don't wanted to fix each error separately, push a single commit, wait for CI, iterate and so on. This also costs pipeline time, I think? What really bothers me is that I often read that Windows GUIDs are little endian and the uuid::Uuid is big endian so I can't guarantee that the conversion functions do the right thing. And writing tests without IDE support is not very pleasant.

@MattesWhite MattesWhite marked this pull request as ready for review October 13, 2020 06:48
@qdot
Copy link
Contributor

qdot commented Oct 13, 2020

Well, looks like you pass CI now at least. And I do all my development on windows (and the vast number of users of this library are also on windows, and probably don't know they're using this library), so like I said, I'll at least give it a runthru before landing. :)

@johnmave126
Copy link

Actually, how about using eui48 for mac address? That crate seems pretty slim and seems to be treated as de-facto mac address crate in crate like tokio-postgres.

@qwandor
Copy link
Collaborator

qwandor commented Feb 20, 2021

@MattesWhite Hi! Any chance you could rebase the BDAddr changes against the latest dev branch? The diff is a bit hard to follow at the moment.

I've done something a bit similar for UUIDs in #123, but using the type from the uuid crate directly in the API and internally, which makes equality work properly, and then using an extension trait and a helper function to convert to and from the short form where needed.

@NZSmartie NZSmartie force-pushed the refactor-uuid-and-bdaddr branch from cde5f12 to 39000b2 Compare March 7, 2021 01:05
@NZSmartie NZSmartie changed the base branch from master to dev March 7, 2021 01:05
@NZSmartie NZSmartie changed the title Refactor BDAddr and UUID Refactor BDAddr Mar 7, 2021
@NZSmartie
Copy link
Collaborator

I've rebased it on to the current dev branch and omitted all UUID related changes. It did drop the useful UUID constants you had defined, but I'm sure we could add that in another PR, that way this PR only focuses on BDAddr

@NZSmartie NZSmartie force-pushed the refactor-uuid-and-bdaddr branch from 39000b2 to a1309d7 Compare March 7, 2021 01:09
Comment on lines +20 to +21
let b: [u8; 6] = uuid.as_bytes()[0..6].try_into().unwrap();
BDAddr::try_from(b).unwrap()
Copy link
Collaborator

@NZSmartie NZSmartie Mar 7, 2021

Choose a reason for hiding this comment

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

One of refactors that didn't quite survive the rebase was this. It may be worth reviewing as I ended up applying a dirty fix so that macOS/iOS would still build

@MattesWhite
Copy link
Contributor Author

Hi all,

sorry for the late response. Lately I don't have much time to monitor the progress of btleplug. However, I'm very exited to see the progress on async 👍 .

Feel free to edit this PR as much as you like. I'm happy that at least some pieces of my code end up in the crate. If there are any questions I'll be happy to help.

Ok(Self { address })
}
/// Writes the address without delimiters.
pub fn write_flat(&self, f: &mut impl fmt::Write) -> fmt::Result {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this actually used anywhere? I'd be inclined to leave it and to_string_flat out unless you have a usecase in mind.

Copy link
Contributor Author

@MattesWhite MattesWhite Mar 9, 2021

Choose a reason for hiding this comment

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

Actually this was my initial motivation to write this PR (see #68 (comment)). I'm working on a 'Bluetooth gateway' where the BDAddr of devices is used as the identifier in a REST API. As the default : in MAC addresses is also a valid separator in URIs I use this flat format. eui48 provides a similar format.

Don't know if this is enough to justify its existence in btleplug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least I renamed it to _no_delim instead of _flat

let mut address = [0; 6];
for i in (0..12).step_by(2) {
let part = &s[i..i + 2];
address[i / 2] = u8::from_str_radix(part, 16).expect("Checked upfront");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than checking twice, remove the is_ascii_hexdigit check above, and return the error here instead.

impl From<u64> for BDAddr {
fn from(int: u64) -> Self {
let mut cpy = [0; 6];
let slice = int.to_be_bytes(); // Reverse order to have MSB on index 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check that the top two bytes are 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this really a problem/error? Would you rather use TryFrom?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's better. Done this myself.

@qwandor
Copy link
Collaborator

qwandor commented Mar 8, 2021

I think that this is generally an improvement, once the a bunch of small things are fixed. However, we could consider instead using one of the existing crates which provides a standard MAC address type, such as eui48 or macaddr. I haven't had a chance yet to see which of the two is a better choice.

@qwandor qwandor force-pushed the refactor-uuid-and-bdaddr branch from 57fe4f6 to 65d4c8f Compare March 10, 2021 21:17
@qwandor qwandor force-pushed the refactor-uuid-and-bdaddr branch from 65d4c8f to 19626ca Compare March 10, 2021 21:44
@qwandor qwandor merged commit 19626ca into deviceplug:dev Mar 10, 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

Successfully merging this pull request may close these issues.

5 participants