Skip to content

Commit

Permalink
clippy + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyash-b committed Nov 30, 2024
1 parent 9ea67ad commit fe81072
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 71 deletions.
18 changes: 9 additions & 9 deletions components/src/protocomm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Protocol Communications
//!
//!
//! Protocomm is a component that can be used for building services with transport independent endpoints for interaction with other applications.
//! It's architecture is roughly based on Protocomm component in ESP-IDf. You can read more about it [here].
//!
//!
//! By default it provides 2 transports(types of servers):
//! - Https: Uses an HTTP server.
//! - Gatt: Serves a GATT application over BLE.
//!
//!
//! You can read more information about the transports at [ProtocommHttpd] and [ProtocommGatt]
//!
//!
//! [here]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/provisioning/protocomm.html
mod security;
Expand All @@ -25,11 +25,11 @@ use crate::http;
/// Protcomm with HTTP transport
///
/// Uses an HTTP server for providing interaction with endpoints.
///
///
/// All the registered endpoints are available as POST endpoints as "/ep_name".
/// The caller is responsible for setting up appropriate mechanism for interacting with this HTTP server.
/// For e.g., by creating a WiFi Access Point, connecting to a known WiFi network, mDNS etc.
///
///
/// <b> HTTP server is started as soon as it is initialized so the endpoints are available as soon as they are registered.
/// The service stops when the object is dropped </b>
#[allow(private_interfaces)]
Expand All @@ -38,13 +38,13 @@ pub type ProtocommHttpd = Protocomm<TransportHttpd>;
/// Protcomm with GATT transport
///
/// Uses an GATT service for providing interaction with endpoints.
///
///
/// All the registered endpoints are available as GATT characteristics with the UUID specified while registering them.
/// The caller is responsible for suitable BLE advertisement for this service.
///
///
/// <b> The registered endpoints are made function after calling `start()`.
/// This should be done after all the required endpoints are registered.
/// There is no corresponding `stop()` method. The service stops when the object is dropped </b>
/// There is no corresponding `stop()` method. The service stops when the object is dropped </b>
#[allow(private_interfaces)]
pub type ProtocommGatt = Protocomm<TransportGatt>;

Expand Down
27 changes: 13 additions & 14 deletions components/src/wifi_prov.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! WiFi Provisioning component
//!
//!
//! Provisioning is the process of providing WiFi credentials to a device without manually hardcoding credentials in firmware.
//! WiFi Provisioning is built upon [Protocomm] using endpoints tailored for connecting nearby WiFi networks using
//! WiFi Provisioning is built upon [Protocomm] using endpoints tailored for connecting nearby WiFi networks using
//! companion phone apps.
//!
//!
//! It is roughly based on the architecture of WiFi provisioning component in ESP-IDF. More details can be found [here]
//!
//!
//! It provides implementation for 2 transport methods(the initial communication between companion apps and the device):
//! - Over BLE
//! - Over SoftAP
//!
//!
//! You can read more about each of the transports at: [WiFiProvMgrBle] and [WiFiProvMgrSoftAp]
//!
//!
//! <b> Note: WiFi provisioning won't actually work for linux since WiFi component is WIP for linux. </b>
//!
//!
//! Example
//! ```rust
//! let prov_config = WifiProvBleConfig {
Expand All @@ -32,11 +32,10 @@
//! prov_mgr.start()?;
//! ```
//! Provisioning will stop when `prov_mgr` is dropped.
//!
//!
//! [Protocomm]: crate::protocomm::Protocomm
//! [here]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/provisioning/wifi_provisioning.html

use std::sync::mpsc::{self, Receiver, Sender};

use quick_protobuf::{MessageWrite, Writer};
Expand Down Expand Up @@ -64,13 +63,13 @@ pub use softap::WiFiProvSoftApConfig;
use softap::WiFiProvTransportSoftAp;

/// BLE transport for WiFi Provisioning.
///
///
/// Uses a GATT Service for provisioning purposes.
/// The UUID for this Service is the one provided in the config.
///
///
/// Each of the endpoint is a GATT Characteristic.
/// The UUID for the Characteristic is generated by masking 12th and 13th byte in Service UUID with increasing value for each endpoint registered.
///
///
/// Rest of the details are similar to BLE transport in [Protocomm]
///
/// <b> Note: Bluetooth needs to be turned on on Linux before running the program </b>
Expand Down Expand Up @@ -401,9 +400,9 @@ mod ep_prov_scan {
let count = cmd.count as usize;
let end_index = start_index + count;

let entries = networks.clone()
let entries = networks
.clone()
.drain(start_index..end_index)
.into_iter()
.map(|x| x.into())
.collect();

Expand Down
7 changes: 5 additions & 2 deletions components/src/wifi_prov/softap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ impl WiFiProvTransportSoftAp {

impl WiFiProvTransportTrait for WiFiProvTransportSoftAp {
fn start(&mut self) -> Result<(), crate::error::Error> {
let mut wifi_ap_config = WifiApConfig::default();
wifi_ap_config.ssid = self.service_name.clone();
let mut wifi_ap_config = WifiApConfig {
ssid: self.service_name.clone(),
..Default::default()
};

let key = &self.service_key;
if key.is_some() {
wifi_ap_config.password = key.as_ref().unwrap().clone();
Expand Down
2 changes: 1 addition & 1 deletion examples/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod esp {

// Declare it here since we want wifi to be connected after connect_wifi returns
let wifi_arc_mutex = Arc::new(Mutex::new(WifiMgr::new()?));
connect_wifi(&rmaker, wifi_arc_mutex.clone())?;
connect_wifi(rmaker, wifi_arc_mutex.clone())?;

log::info!("WiFi connected successfully");

Expand Down
2 changes: 1 addition & 1 deletion examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
pub mod ws2812;

use std::sync::{Arc, Mutex};
use anyhow::Result;
use components::{
persistent_storage::NvsPartition,
wifi::{WifiClientConfig, WifiMgr},
wifi_prov::{WiFiProvMgrBle, WifiProvBleConfig},
};
use rainmaker::Rainmaker;
use std::sync::{Arc, Mutex};

pub fn initializse_logger() {
#[cfg(target_os = "linux")]
Expand Down
2 changes: 1 addition & 1 deletion examples/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn main() -> Result<()> {

// Declare it here since we want wifi to be connected after connect_wifi returns
let wifi_arc_mutex = Arc::new(Mutex::new(WifiMgr::new()?));
connect_wifi(&rmaker, wifi_arc_mutex.clone())?;
connect_wifi(rmaker, wifi_arc_mutex.clone())?;

log::info!("WiFi connected successfully");

Expand Down
2 changes: 1 addition & 1 deletion rainmaker/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub const USER_MAPPING_TOPIC_SUFFIX: &str = "user/mapping";
pub const NODE_CONFIG_TOPIC_SUFFIX: &str = "config";
pub const NODE_PARAMS_LOCAL_INIT_TOPIC_SUFFIX: &str = "params/local/init";
pub const NODE_PARAMS_REMOTE_TOPIC_SUFFIX: &str = "params/remote";
pub const NODE_PARAMS_LOCAL_TOPIC_SUFFIX: &str = "params/local";
pub const NODE_PARAMS_LOCAL_TOPIC_SUFFIX: &str = "params/local";
4 changes: 3 additions & 1 deletion rainmaker/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! A module for devices. Device is a physical entity which associated with a node and can be controlled from ESP-RainMaker Dashboard. One or more devices can be created and registered with node using device module.
//! Device module.
//!
//! One or more devices can be created and registered with node using device module.
//!
//! Example for creating an instance of device:
//! ```rust
Expand Down
34 changes: 19 additions & 15 deletions rainmaker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![feature(trait_alias)]

//! # Rust Implementation of ESP Rainmaker.
//!
//!
//! A cross-platform implementation of ESP Rainmaker for ESP32 products and Linux using Rust.
//!
//!
//! Full fledged C based ESP RainMaker SDK can be found [here](https://github.com/espressif/esp-rainmaker).
pub mod device;
pub mod error;
pub mod node;
pub(crate) mod proto;
pub mod param;
pub(crate) mod proto;
pub(crate) mod utils;

mod constants;
Expand Down Expand Up @@ -59,11 +59,11 @@ static mut RAINMAKER: OnceLock<Rainmaker> = OnceLock::new();

impl Rainmaker {
/// Initializes the RainMaker Agent.
///
///
/// Throws an error if agent is already initialized else returns the mutable reference of Rainmaker.
///
///
/// This function panics if node claiming is not performed.
///
///
/// For claiming process, ensure following steps are performed:
/// - Install [`esp-rainmaker-cli`](https://rainmaker.espressif.com/docs/cli-setup/) package.
/// - - For ESP:
Expand Down Expand Up @@ -99,8 +99,8 @@ impl Rainmaker {
}

/// Starts the RainMaker core task which includes connect to RainMaker cloud over MQTT if hasn't been already.
///
/// Reports node configuration and initial values of parameters, subscribe to respective topics and wait for commands.
///
/// Reports node configuration and initial values of parameters, subscribe to respective topics and wait for commands.
/// # Ensure agent(node) is initialized and WiFi is connected before using this function.
pub fn start(&mut self) -> Result<(), RMakerError> {
// initialize mqtt if not done already
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Rainmaker {
}

/// Registers node to agent.
///
///
/// This should be called before the `start()` function.
/// # Example
/// ```rust
Expand All @@ -147,13 +147,13 @@ impl Rainmaker {
/// rmaker.register_node();
/// rmaker.start();
/// ```
///
///
pub fn register_node(&mut self, node: Node) {
self.node = Some(node.into());
}

/// Registers the endpoint used for claiming process with `WiFiProvMgr`. This is used for associating a RainMaker node with the user account performing the provisioning.
///
///
/// This should be called before `WiFiProvMgr::start()`
pub fn reg_user_mapping_ep<T: WiFiProvTransportTrait>(&self, prov_mgr: &mut WifiProvMgr<T>) {
let node_id = self.get_node_id();
Expand Down Expand Up @@ -272,10 +272,10 @@ fn cloud_user_assoc_callback(_ep: &str, data: &[u8], node_id: &str) -> Vec<u8> {
out_vec
}

/// Reports parameters values of devices to the RainMaker cloud over MQTT.
///
/// Reports parameters values of devices to the RainMaker cloud over MQTT.
///
/// Appropriate Device Name and a map of parameters(name: value) must be provided.
///
///
/// Example (Can be used in a device callback function)
/// ```
/// fn device_cb(params: HashMaps<String, Value>)
Expand All @@ -290,6 +290,10 @@ pub fn report_params(device_name: &str, params: HashMap<String, Value>) {
device_name: params
});

let local_params_topic = format!("node/{}/{}", NODEID.as_str(), NODE_PARAMS_LOCAL_TOPIC_SUFFIX);
let local_params_topic = format!(
"node/{}/{}",
NODEID.as_str(),
NODE_PARAMS_LOCAL_TOPIC_SUFFIX
);
rmaker_mqtt::publish(&local_params_topic, updated_params.to_string().into_bytes()).unwrap();
}
51 changes: 25 additions & 26 deletions rainmaker/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Node module of rainmaker-rs.
//!
//!
//! Methods related node are implemented for struct [Node].
/*
Expand Down Expand Up @@ -57,15 +57,14 @@ pub struct Node {
}

impl Node {

/// An instance of node can be created using `new` method of the module. Node ID should be passed as an argument for the same. Node ID can be obtained from the method [`get_node_id`].
/// ```rust
/// let rmaker = Rainmaker::init()?;
/// let node_id = rmaker.get_node_id();
/// let mut node = Node::new(node_id);
/// ```
///
/// [`get_node_id`]: crate::Rainmaker::get_node_id
/// An instance of node can be created using `new` method of the module. Node ID should be passed as an argument for the same. Node ID can be obtained from the method [`get_node_id`].
/// ```rust
/// let rmaker = Rainmaker::init()?;
/// let node_id = rmaker.get_node_id();
/// let mut node = Node::new(node_id);
/// ```
///
/// [`get_node_id`]: crate::Rainmaker::get_node_id
pub fn new(node_id: String) -> Self {
Self {
node_id,
Expand All @@ -75,32 +74,32 @@ impl Node {
}
}

/// Node information [Info] (Name, FW Version) is set using this function.
/// ```rust
/// node.set_info(Info{
/// name: "Example Node".to_string(),
/// fw_version: "v1.0".to_string()
/// });
/// ```
/// Node information [Info] (Name, FW Version) is set using this function.
/// ```rust
/// node.set_info(Info{
/// name: "Example Node".to_string(),
/// fw_version: "v1.0".to_string()
/// });
/// ```
pub fn set_info(&mut self, info: Info) {
self.info = Some(info);
}

/// Used to define attributes of node.
/// Used to define attributes of node.
pub fn set_attribute(&mut self, name: String, value: String) {
self.attributes
.insert(name, value)
.expect("Failed to set atttribute");
}

/// Multiple devices can be associated with the node by using this method. Instance of device should be passed as an argument.
///
/// Ensure that instance of [device] is created properly and callback is set appropriately in order to report updated parameter values.
/// ```rust
/// node.add_device(device);
/// ```
///
/// [device]: crate::device
/// Multiple devices can be associated with the node by using this method. Instance of device should be passed as an argument.
///
/// Ensure that instance of [device] is created properly and callback is set appropriately in order to report updated parameter values.
/// ```rust
/// node.add_device(device);
/// ```
///
/// [device]: crate::device
pub fn add_device(&mut self, device: Device) {
self.devices.push(device);
}
Expand Down

0 comments on commit fe81072

Please sign in to comment.