Skip to content

Commit

Permalink
SNTP support for esp-idf >= v5.1 (#219)
Browse files Browse the repository at this point in the history
* SNTP support for esp-idf >= v5.1

* clippy lints

* fix redundant use
  • Loading branch information
zRedShift authored Feb 1, 2023
1 parent fa6a9f0 commit 2f9f139
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 45 deletions.
15 changes: 4 additions & 11 deletions src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use embedded_svc::event_bus::{self, ErrorType};
use esp_idf_hal::cpu::Core;
use esp_idf_hal::delay::TickType;
use esp_idf_hal::interrupt;
use esp_idf_hal::task;

use esp_idf_sys::*;

Expand Down Expand Up @@ -506,7 +505,7 @@ where
};

if higher_prio_task_woken != 0 {
task::do_yield();
esp_idf_hal::task::do_yield();
}

if result == ESP_FAIL {
Expand Down Expand Up @@ -537,17 +536,11 @@ where
P: EspTypedEventSerializer<P>,
{
if interrupt::active() {
#[cfg(esp_idf_esp_event_post_from_isr)]
let result = P::serialize(payload, |raw_event| self.isr_post_raw(raw_event));

#[cfg(not(esp_idf_esp_event_post_from_isr))]
let result = {
panic!("Trying to post from an ISR handler. Enable `CONFIG_ESP_EVENT_POST_FROM_ISR` in `sdkconfig.defaults`");
panic!("Trying to post from an ISR handler. Enable `CONFIG_ESP_EVENT_POST_FROM_ISR` in `sdkconfig.defaults`");

Err(EspError::from_infallible::<ESP_FAIL>())
};

result
#[cfg(esp_idf_esp_event_post_from_isr)]
P::serialize(payload, |raw_event| self.isr_post_raw(raw_event))
} else {
P::serialize(payload, |raw_event| self.post_raw(raw_event, wait))
}
Expand Down
21 changes: 10 additions & 11 deletions src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl From<&Configuration> for Newtype<httpd_config_t> {
fn from(conf: &Configuration) -> Self {
Self(httpd_config_t {
task_priority: 5,
stack_size: conf.stack_size as _,
stack_size: conf.stack_size,
core_id: i32::MAX,
server_port: conf.http_port,
ctrl_port: 32768,
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<'a> Read for EspHttpRequest<'a> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
if !buf.is_empty() {
let fd = unsafe { httpd_req_to_sockfd(self.0) };
let len = unsafe { esp_idf_sys::read(fd, buf.as_ptr() as *mut _, buf.len() as _) };
let len = unsafe { esp_idf_sys::read(fd, buf.as_ptr() as *mut _, buf.len()) };

Ok(len as _)
} else {
Expand All @@ -525,7 +525,7 @@ impl<'a> Write for EspHttpRequest<'a> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
if !buf.is_empty() {
let fd = unsafe { httpd_req_to_sockfd(self.0) };
let len = unsafe { esp_idf_sys::write(fd, buf.as_ptr() as *const _, buf.len() as _) };
let len = unsafe { esp_idf_sys::write(fd, buf.as_ptr() as *const _, buf.len()) };

Ok(len as _)
} else {
Expand Down Expand Up @@ -986,7 +986,7 @@ pub mod ws {
if frame_data_buf.len() >= len {
raw_frame.payload = frame_data_buf.as_mut_ptr() as *mut _;
esp!(unsafe {
httpd_ws_recv_frame(*raw_req, &mut raw_frame as *mut _, len as _)
httpd_ws_recv_frame(*raw_req, &mut raw_frame as *mut _, len)
})?;
}

Expand All @@ -1010,22 +1010,21 @@ pub mod ws {
final_: frame_type.is_final(),
fragmented: frame_type.is_fragmented(),
payload: frame_data.as_ptr() as *const _ as *mut _,
len: frame_data.len() as _,
len: frame_data.len(),
}
}

#[allow(non_upper_case_globals)]
fn create_frame_type(raw_frame: &httpd_ws_frame_t) -> (FrameType, usize) {
match raw_frame.type_ {
httpd_ws_type_t_HTTPD_WS_TYPE_TEXT => (
FrameType::Text(raw_frame.fragmented),
raw_frame.len as usize + 1,
),
httpd_ws_type_t_HTTPD_WS_TYPE_TEXT => {
(FrameType::Text(raw_frame.fragmented), raw_frame.len + 1)
}
httpd_ws_type_t_HTTPD_WS_TYPE_BINARY => {
(FrameType::Binary(raw_frame.fragmented), raw_frame.len as _)
(FrameType::Binary(raw_frame.fragmented), raw_frame.len)
}
httpd_ws_type_t_HTTPD_WS_TYPE_CONTINUE => {
(FrameType::Continue(raw_frame.final_), raw_frame.len as _)
(FrameType::Continue(raw_frame.final_), raw_frame.len)
}
httpd_ws_type_t_HTTPD_WS_TYPE_PING => (FrameType::Ping, 0),
httpd_ws_type_t_HTTPD_WS_TYPE_PONG => (FrameType::Pong, 0),
Expand Down
84 changes: 61 additions & 23 deletions src/sntp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,71 @@ use core::time::Duration;

use ::log::*;

use esp_idf_sys::*;

use crate::private::cstr::CString;
use crate::private::mutex;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(not(any(esp_idf_version_major = "4", esp_idf_version_minor = "0")))]
mod esp_sntp {
use super::OperatingMode;
pub use esp_idf_sys::*;

impl From<esp_sntp_operatingmode_t> for OperatingMode {
#[allow(non_upper_case_globals)]
fn from(from: esp_sntp_operatingmode_t) -> Self {
match from {
esp_sntp_operatingmode_t_ESP_SNTP_OPMODE_POLL => OperatingMode::Poll,
esp_sntp_operatingmode_t_ESP_SNTP_OPMODE_LISTENONLY => OperatingMode::ListenOnly,
_ => unreachable!(),
}
}
}

impl From<OperatingMode> for esp_sntp_operatingmode_t {
#[allow(non_upper_case_globals)]
fn from(from: OperatingMode) -> Self {
match from {
OperatingMode::Poll => esp_sntp_operatingmode_t_ESP_SNTP_OPMODE_POLL,
OperatingMode::ListenOnly => esp_sntp_operatingmode_t_ESP_SNTP_OPMODE_LISTENONLY,
}
}
}

pub use esp_sntp_init as sntp_init;
pub use esp_sntp_setoperatingmode as sntp_setoperatingmode;
pub use esp_sntp_setservername as sntp_setservername;
pub use esp_sntp_stop as sntp_stop;
}

#[cfg(any(esp_idf_version_major = "4", esp_idf_version_minor = "0"))]
mod esp_sntp {
use super::OperatingMode;
pub use esp_idf_sys::*;

impl From<u8_t> for OperatingMode {
fn from(from: u8_t) -> Self {
match from as u32 {
SNTP_OPMODE_POLL => OperatingMode::Poll,
SNTP_OPMODE_LISTENONLY => OperatingMode::ListenOnly,
_ => unreachable!(),
}
}
}

impl From<OperatingMode> for u8_t {
fn from(from: OperatingMode) -> Self {
match from {
OperatingMode::Poll => SNTP_OPMODE_POLL as u8_t,
OperatingMode::ListenOnly => SNTP_OPMODE_LISTENONLY as u8_t,
}
}
}
}

use esp_sntp::*;

const SNTP_SERVER_NUM: usize = SNTP_MAX_SERVERS as usize;

const DEFAULT_SERVERS: [&str; 4] = [
Expand All @@ -30,25 +87,6 @@ pub enum OperatingMode {
ListenOnly,
}

impl From<u8_t> for OperatingMode {
fn from(from: u8_t) -> Self {
match from as u32 {
SNTP_OPMODE_POLL => OperatingMode::Poll,
SNTP_OPMODE_LISTENONLY => OperatingMode::ListenOnly,
_ => unreachable!(),
}
}
}

impl From<OperatingMode> for u8_t {
fn from(from: OperatingMode) -> Self {
match from {
OperatingMode::Poll => SNTP_OPMODE_POLL as u8_t,
OperatingMode::ListenOnly => SNTP_OPMODE_LISTENONLY as u8_t,
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Hash))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -170,7 +208,7 @@ impl EspSntp {
fn init(conf: &SntpConf) -> Result<Self, EspError> {
info!("Initializing");

unsafe { sntp_setoperatingmode(u8_t::from(conf.operating_mode)) };
unsafe { sntp_setoperatingmode(conf.operating_mode.into()) };
unsafe { sntp_set_sync_mode(sntp_sync_mode_t::from(conf.sync_mode)) };

let mut c_servers: [CString; SNTP_SERVER_NUM] = Default::default();
Expand Down Expand Up @@ -202,7 +240,7 @@ impl EspSntp {
SyncStatus::from(unsafe { sntp_get_sync_status() })
}

unsafe extern "C" fn sync_cb(tv: *mut esp_idf_sys::timeval) {
unsafe extern "C" fn sync_cb(tv: *mut timeval) {
debug!(
" Sync cb called: sec: {}, usec: {}",
(*tv).tv_sec,
Expand Down

0 comments on commit 2f9f139

Please sign in to comment.