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

[RSDK-7497] Add WiFi provisioning #206

Merged
merged 13 commits into from
May 20, 2024

Conversation

npmenard
Copy link
Member

Add support for the full configuration flow using Viam APP including setting up the WiFi. For now credentials are stored into memory only but they will be moved to NVS before the PR is merged.
I will wait for the internet connectivity PR to be merged first

@npmenard npmenard requested a review from a team as a code owner May 14, 2024 11:33
net::{Ipv4Addr, TcpListener, UdpSocket},
};

async fn dns_server() {
Copy link
Member Author

Choose a reason for hiding this comment

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

not really happy to pull an entire crate just to do DNS so I will sync with clint & ale to check if we can do some mDNS approach

ssid: ssid.to_owned(),
pwd: password.to_owned(),
})
.map_err(|_| EspError::from_non_zero(NonZeroI32::new(1).unwrap()))?;
Copy link
Member Author

Choose a reason for hiding this comment

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

this a placeholder for until I figure out the best approach to propagate errors

{
let mut conf = self.config.borrow_mut();
let (sta, _) = conf.as_mixed_conf_mut();
log::info!("attempting to connect to {} {}", ssid, password);
Copy link
Member Author

Choose a reason for hiding this comment

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

will remove before merge

@@ -142,7 +145,7 @@ mod esp32 {
storage,
info,
repr,
ip,
std::net::Ipv4Addr::new(10, 1, 12, 187),
Copy link
Member

Choose a reason for hiding this comment

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

What is this IP address?

Copy link
Member Author

Choose a reason for hiding this comment

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

the bridge IP for qemu emulation

last_connection_attempt: Option<NetworkInfo>,
provisioning_info: Option<ProvisioningInfo>,
reason: ProvisioningReason,
last_error: Option<String>,
wifi_manager: Option<Wifi>,
Copy link
Member

Choose a reason for hiding this comment

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

Is this saying that wifi_manager is either None or Some(())?
Not sure what the purpose of the unit as the generic here.

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 needs to be resolvable to an object when its None otherwise rust can't properly do codegen

let rr = dns_message_parser::rr::RR::A(dns_message_parser::rr::A {
domain_name: q.domain_name.clone(),
ttl: 3600,
ipv4_addr: Ipv4Addr::new(192, 168, 71, 1),
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the default AP Ip for now I am going to make it configurable

Comment on lines +57 to +60
fn has_str(&self, key: &str) -> Result<bool, NVSStorageError> {
let nvs = self.nvs.borrow();
Ok(nvs.str_len(key)?.is_some())
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this make has_key obsolete? I don't see it used anywhere else and the errors from has_str would be a superset of errors from has_key


#[cfg(not(feature = "provisioning"))]
{
let sys_loop_stack = EspSystemEventLoop::take().unwrap();
#[allow(clippy::redundant_clone)]
Copy link
Member

Choose a reason for hiding this comment

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

necessary? don't see a clone anywhere.

Copy link
Member

@mattjperez mattjperez left a comment

Choose a reason for hiding this comment

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

Looks awesome!

just some questions and nits

use super::wifi_provisioning::Esp32WifiProvisioningBuilder;

async fn dns_server(ap_ip: Ipv4Addr) {
let socket = async_io::Async::<UdpSocket>::bind(([0, 0, 0, 0], 53)).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect with error message

let socket = async_io::Async::<UdpSocket>::bind(([0, 0, 0, 0], 53)).unwrap();
loop {
let mut buf = [0_u8; 512];
let len = socket.recv_from(&mut buf).await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect instead of unwrap

Comment on lines +56 to +57
let buf = msg.encode().unwrap();
socket.send_to(&buf, len.1).await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect over unwrap

ServerError: From<<S as RobotCredentialStorage>::Error>,
<S as WifiCredentialStorage>::Error: Sync + Send + 'static,
{
let _ = Timer::after(std::time::Duration::from_millis(150)).await;
Copy link
Member

Choose a reason for hiding this comment

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

what's the purpose of this delay? comment would be nice

let wifi = Esp32WifiProvisioningBuilder::default()
.build(storage.clone())
.await
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect over unwrap

let mut client = builder.build().await?;
let certs = client.get_certificates().await?;

let serv_key = CString::new(certs.tls_private_key).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect over unwrap

let serv_key: *const u8 = serv_key.into_raw() as *const u8;

let tls_certs = CString::new(certs.tls_certificate)
.unwrap()
Copy link
Member

Choose a reason for hiding this comment

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

nit: expect over unwrap

Copy link
Member

@acmorrow acmorrow left a comment

Choose a reason for hiding this comment

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

Per discussion, LGTM.

@npmenard npmenard merged commit 6982c9a into viamrobotics:main May 20, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants