-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstraction for synchronous esp-tls (#288)
* Start wrapping esp-tls * Improve EspTls * Allow splitting into EspTlsRead and EspTlsWrite * Don't use std * Use str and do alloc-free conversion * Pass Config by reference * Import from sys reexport * Implement crt_bundle_attach, add docs * Fix build, move stuff to private, use str * Make clippy happy * Add AsRawFd * Add example for tls * Make EspTls work with v5 * Remove unnecessary cast * Use core for no-std support * Guard EspTls with appropriate cfg * Change cstr imports to core
- Loading branch information
Showing
3 changed files
with
546 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//! Example of using blocking TLS/TCP. | ||
//! | ||
//! Add your own ssid and password | ||
|
||
use std::ffi::CStr; | ||
use std::io::{BufRead, BufReader}; | ||
|
||
use embedded_svc::io::adapters::ToStd; | ||
use embedded_svc::io::Write; | ||
use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; | ||
use esp_idf_hal::prelude::Peripherals; | ||
use esp_idf_svc::log::EspLogger; | ||
use esp_idf_svc::tls::{self, EspTls, X509}; | ||
use esp_idf_svc::wifi::{BlockingWifi, EspWifi}; | ||
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition}; | ||
use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported | ||
use log::info; | ||
|
||
const SSID: &str = env!("WIFI_SSID"); | ||
const PASSWORD: &str = env!("WIFI_PASS"); | ||
|
||
// cannot use include_str because we need a \0 at the end | ||
const CA_CERT: &str = "-----BEGIN CERTIFICATE----- | ||
MIIEvjCCA6agAwIBAgIQBtjZBNVYQ0b2ii+nVCJ+xDANBgkqhkiG9w0BAQsFADBh | ||
MQswCQYDVQQGEwJ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XJ0R2xvYmFsUm9vdENBLmNydDBCBgNV | ||
HR8EOzA5MDegNaAzhjFodHRwOi8vY3JsMy5kaWdpY2VydC5jb20vRGlnaUNlcnRH | ||
bG9iYWxSb290Q0EuY3JsMD0GA1UdIAQ2MDQwCwYJYIZIAYb9bAIBMAcGBWeBDAEB | ||
MAgGBmeBDAECATAIBgZngQwBAgIwCAYGZ4EMAQIDMA0GCSqGSIb3DQEBCwUAA4IB | ||
AQCAMs5eC91uWg0Kr+HWhMvAjvqFcO3aXbMM9yt1QP6FCvrzMXi3cEsaiVi6gL3z | ||
ax3pfs8LulicWdSQ0/1s/dCYbbdxglvPbQtaCdB73sRD2Cqk3p5BJl+7j5nL3a7h | ||
qG+fh/50tx8bIKuxT8b1Z11dmzzp/2n3YWzW2fP9NsarA4h20ksudYbj/NhVfSbC | ||
EXffPgK2fPOre3qGNm+499iTcc+G33Mw+nur7SpZyEKEOxEXGlLzyQ4UfaJbcme6 | ||
ce1XR2bFuAJKZTRei9AqPCCcUZlM51Ke92sRKw2Sfh3oius2FkOH6ipjv3U/697E | ||
A7sKPPcw7+uvTPyLNhBzPvOk | ||
-----END CERTIFICATE-----\0"; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
EspLogger::initialize_default(); | ||
|
||
let peripherals = Peripherals::take().unwrap(); | ||
let sys_loop = EspSystemEventLoop::take()?; | ||
let nvs = EspDefaultNvsPartition::take()?; | ||
|
||
let mut wifi = BlockingWifi::wrap( | ||
EspWifi::new(peripherals.modem, sys_loop.clone(), Some(nvs))?, | ||
sys_loop, | ||
)?; | ||
|
||
connect_wifi(&mut wifi)?; | ||
|
||
let ip_info = wifi.wifi().sta_netif().get_ip_info()?; | ||
|
||
info!("Wifi DHCP info: {:?}", ip_info); | ||
|
||
let mut tls = EspTls::new( | ||
"example.com", | ||
1234, | ||
&tls::Config { | ||
common_name: Some("example.com"), | ||
ca_cert: Some(X509::pem( | ||
CStr::from_bytes_with_nul(CA_CERT.as_bytes()).unwrap(), | ||
)), | ||
..Default::default() | ||
}, | ||
)?; | ||
let (reader, writer) = tls.split(); | ||
let mut reader = BufReader::with_capacity(512, ToStd::new(reader)); | ||
let mut line = String::new(); | ||
// receive line by line from server and echo them back | ||
loop { | ||
reader.read_line(&mut line)?; | ||
writer.write_all(line.as_bytes())?; | ||
line.clear(); | ||
} | ||
} | ||
|
||
fn connect_wifi(wifi: &mut BlockingWifi<EspWifi<'static>>) -> anyhow::Result<()> { | ||
let wifi_configuration: Configuration = Configuration::Client(ClientConfiguration { | ||
ssid: SSID.into(), | ||
bssid: None, | ||
auth_method: AuthMethod::WPA2Personal, | ||
password: PASSWORD.into(), | ||
channel: None, | ||
}); | ||
|
||
wifi.set_configuration(&wifi_configuration)?; | ||
|
||
wifi.start()?; | ||
info!("Wifi started"); | ||
|
||
wifi.connect()?; | ||
info!("Wifi connected"); | ||
|
||
wifi.wait_netif_up()?; | ||
info!("Wifi netif up"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.