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

Remove EspWifiInitFor & more granular init/deinit per driver #2301

Merged
merged 8 commits into from
Nov 4, 2024

Conversation

MabezDev
Copy link
Member

@MabezDev MabezDev commented Oct 8, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

  • Rework EspWifiInit to EspWifiController - bikeshedding welcome
  • No longer require EspWifiInitFor on initialize
  • Add Drop impls for each driver, and add Drop for EspWifiController to fully deinit the stack
  • deinit no longer returns the used resources, instead the peripheral ref pattern will take care of this

I haven't updated all the examples yet, I'll do so after a bit of review.

Testing

Try this modified wifi_dhcp example: https://gist.github.com/MabezDev/78aac5133c482a56db9600697c179650

esp-wifi/src/ble/controller/mod.rs Show resolved Hide resolved
esp-wifi/src/lib.rs Show resolved Hide resolved
esp-wifi/src/lib.rs Outdated Show resolved Hide resolved
esp-wifi/src/lib.rs Outdated Show resolved Hide resolved
esp-wifi/src/timer/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/timer/riscv.rs Outdated Show resolved Hide resolved
examples/Cargo.toml Show resolved Hide resolved
@MabezDev
Copy link
Member Author

Nearly done updating the examples. esp-now is causing me a considerable headache right now as it can be initialized on its own or from an existing wifi driver (ish). This makes it a bit tricky to reason about when to deinit wifi - will need a bit longer to figure this out.

@MabezDev MabezDev force-pushed the esp-wifi-peripheral-ref branch 3 times, most recently from ef966f2 to c845caa Compare October 29, 2024 15:32
@MabezDev MabezDev marked this pull request as ready for review October 30, 2024 15:56
@MabezDev
Copy link
Member Author

CI is green(ish), this is now ready for another review round.

esp-wifi/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@bjoernQ bjoernQ left a comment

Choose a reason for hiding this comment

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

Looks good and works fine - happy to see that awkward EspWifiInitialization and EspWifiInitFor go away

@@ -361,7 +385,7 @@ impl EspWifiTimerSource for TimeBase {
/// ```
pub fn init<'d, T: EspWifiTimerSource>(
timer: impl Peripheral<P = T> + 'd,
_rng: hal::rng::Rng,
_rng: impl EspWifiRngSource,
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be impl Peripheral<P = RNG> if Trng implements Peripheral<P = RNG> without any additional traits.

Copy link
Member Author

Choose a reason for hiding this comment

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

It could be, but the problem is that we don't want to completely own the Rng or Trng here, users will still want to use it in their own applications see: #2372

Copy link
Contributor

Choose a reason for hiding this comment

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

And how does this (a trait that requires passing by value) help in not completely owning the rng?

Copy link
Member Author

Choose a reason for hiding this comment

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

Rng driver is clone, Trng might be clone soon. RNG (the raw peripheral) will take exclusive access whether its via &mut Self or Self and not allow any other part of the system to use Rng.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you misunderstand what I want. I'm not proposing requiring using the raw peripheral. I'm proposing accepting drivers that work with the peripheral. Making Rng and Trng implement Peripheral allows us to pass both of those, or a mutable reference to them (or just the RNG peripheral directly) and esp-wifi wouldn't know the difference.

I.e. I'm suggesting that instead of a custom trait we have:

impl Peripheral for Rng {
    type P = RNG;
...
}


impl Peripheral for Trng {
    type P = RNG;
...
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case we may still go this way, just using Peripheral<P = Rng>. But I'll not force the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

With Trng also impl Peripheral<P = Rng>? That could work. Just wondering if there a case where someone might need to write Peripheral<P = Trng>, because they wouldn't be able to in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Peripheral<P = Trng>

That is an interesting question, drivers absolutely requiring true randomness. I'm not sure, but we're not supporting it right now, either.

Copy link
Contributor

Choose a reason for hiding this comment

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

The "funny" thing is that RNG will give true random numbers when the RF peripherals are enabled (but we still need #2443)

@@ -287,7 +288,7 @@ impl<'d> Drop for EspWifiController<'d> {
///
/// This trait is meant to be used only for the `init` function.
/// Calling `timers()` multiple times may panic.
pub trait EspWifiTimerSource {
pub trait EspWifiTimerSource: private::Sealed {
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this a bit of an overkill?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, but is there a down side to doing it?

Copy link
Contributor

Choose a reason for hiding this comment

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

More code :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose, but I can't see a reason to keep this trait open of downstream crates to impl.

I'm happy to remove it, but I'd like a more compelling argument than removing a few lines of hidden boiler plate impls.

@MabezDev MabezDev added the skip-changelog No changelog modification needed label Oct 31, 2024
@MabezDev
Copy link
Member Author

Skip changelog for esp-hal changes, this is now ready for another round of review.

esp-wifi/src/ble/controller/mod.rs Show resolved Hide resolved
esp-wifi/src/esp_now/mod.rs Outdated Show resolved Hide resolved
@jessebraham jessebraham added this pull request to the merge queue Nov 4, 2024
Merged via the queue into esp-rs:main with commit c717f04 Nov 4, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-changelog No changelog modification needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants