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

Add a minimal SRP client API #15

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build_openthread/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ build()
-DOT_SLAAC=ON \
-DOT_SETTINGS_RAM=ON \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DOT_SRP_CLIENT=ON \
-DOT_ECDSA=ON \
-DOT_COMPILE_WARNING_AS_ERROR=ON "$@" "${OT_SRCDIR}"

if [[ -n ${OT_CMAKE_NINJA_TARGET[*]} ]]; then
Expand Down
7 changes: 7 additions & 0 deletions esp-openthread-sys/include/include.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
#include "openthread/platform/entropy.h"
#include "openthread/platform/settings.h"
#include "openthread/platform/logging.h"

#include "openthread/srp_client_buffers.h"
#include "openthread/srp_client.h"

#ifndef OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE
#define OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE 1
#endif
355 changes: 355 additions & 0 deletions esp-openthread-sys/src/bindings.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions esp-openthread/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ no-std-net = "0.6.0"
[features]
esp32c6 = [ "esp-ieee802154/esp32c6", "esp-hal/esp32c6"]
esp32h2 = [ "esp-ieee802154/esp32h2", "esp-hal/esp32h2"]
srp-client = []
99 changes: 99 additions & 0 deletions esp-openthread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
mod entropy;
mod platform;
mod radio;
#[cfg(feature = "srp-client")]
mod srp_client;
mod timer;

use core::{
Expand Down Expand Up @@ -688,6 +690,103 @@ impl<'a> OpenThread<'a> {
};
}
}

#[cfg(feature = "srp-client")]
pub fn setup_srp_client_autostart(
&mut self,
callback: Option<
unsafe extern "C" fn(aServerSockAddr: *const otSockAddr, aContext: *mut c_void),
>,
) -> Result<(), Error> {
if !callback.is_some() {
srp_client::enable_srp_autostart(self.instance);
return Ok(());
}
srp_client::enable_srp_autostart_with_callback_and_context(
self.instance,
callback,
core::ptr::null_mut(),
);

Ok(())
}

#[cfg(feature = "srp-client")]
pub fn setup_srp_client_host_addr_autoconfig(&mut self) -> Result<(), Error> {
srp_client::set_srp_client_host_addresses_auto_config(self.instance)?;
Ok(())
}

#[cfg(feature = "srp-client")]
pub fn setup_srp_client_set_hostname(&mut self, host_name: &str) -> Result<(), Error> {
srp_client::set_srp_client_host_name(self.instance, host_name.as_ptr() as _)?;
Ok(())
}

#[cfg(feature = "srp-client")]
pub fn setup_srp_client_with_addr(
&mut self,
host_name: &str,
addr: otSockAddr,
) -> Result<(), Error> {
srp_client::set_srp_client_host_name(self.instance, host_name.as_ptr() as _)?;
srp_client::srp_client_start(self.instance, addr)?;
Ok(())
}

// For now, txt entries are expected to be provided as hex strings to avoid having to pull in the hex crate
// for example a key entry of 'abc' should be provided as '03616263'
#[cfg(feature = "srp-client")]
pub fn register_service_with_srp_client(
&mut self,
instance_name: &str,
service_name: &str,
service_labels: &[&str],
txt_entry: &str,
port: u16,
priority: Option<u16>,
weight: Option<u16>,
lease: Option<u32>,
key_lease: Option<u32>,
) -> Result<(), Error> {
if !srp_client::is_srp_client_running(self.instance) {
self.setup_srp_client_autostart(None)?;
}

srp_client::add_srp_client_service(
self.instance,
instance_name.as_ptr() as _,
instance_name.len() as _,
service_name.as_ptr() as _,
service_name.len() as _,
service_labels,
txt_entry.as_ptr() as _,
txt_entry.len() as _,
port,
priority,
weight,
lease,
key_lease,
)?;

Ok(())
}

#[cfg(feature = "srp-client")]
pub fn set_srp_client_ttl(&mut self, ttl: u32) {
srp_client::set_srp_client_ttl(self.instance, ttl);
}

#[cfg(feature = "srp-client")]
pub fn get_srp_client_ttl(&mut self) -> u32 {
srp_client::get_srp_client_ttl(self.instance)
}

#[cfg(feature = "srp-client")]
pub fn stop_srp_client(&mut self) -> Result<(), Error> {
srp_client::srp_client_stop(self.instance);
Ok(())
}
}

impl<'a> Drop for OpenThread<'a> {
Expand Down
14 changes: 14 additions & 0 deletions esp-openthread/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,17 @@ pub extern "C" fn isupper() {
pub extern "C" fn strcmp() {
todo!()
}

// copy pasta from https://github.com/esp-rs/esp-hal/blob/main/esp-wifi/src/compat/misc.rs#L101
#[no_mangle]
unsafe extern "C" fn strstr(str1: *const i8, str2: *const i8) -> *const i8 {
let s1 = core::ffi::CStr::from_ptr(str1).to_str().unwrap();
let s2 = core::ffi::CStr::from_ptr(str2).to_str().unwrap();

let idx = s1.find(s2);

match idx {
Some(offset) => str1.add(offset),
None => core::ptr::null(),
}
}
Loading