diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 7c2864c..60c91e8 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -10,6 +10,8 @@ pub const MSG_ID_LOGIN: u32 = 1; pub const MSG_ID_LOGOUT: u32 = 2; /// Video and Audio Streams messages have this ID pub const MSG_ID_VIDEO: u32 = 3; +/// Reboot messages have this ID +pub const MSG_ID_REBOOT: u32 = 23; /// Version messages have this ID pub const MSG_ID_VERSION: u32 = 80; /// Ping messages have this ID diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index a349537..ede23b4 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -15,6 +15,7 @@ mod login; mod logout; mod media_packet; mod ping; +mod reboot; mod stream; mod time; mod version; diff --git a/crates/core/src/bc_protocol/reboot.rs b/crates/core/src/bc_protocol/reboot.rs new file mode 100644 index 0000000..b9303ad --- /dev/null +++ b/crates/core/src/bc_protocol/reboot.rs @@ -0,0 +1,40 @@ +use super::{BcCamera, Error, Result, RX_TIMEOUT}; +use crate::bc::model::*; + +impl BcCamera { + /// Reboot the camera + pub fn reboot(&self) -> Result<()> { + let connection = self.connection.as_ref().expect("Must be connected to ping"); + let sub = connection.subscribe(MSG_ID_REBOOT)?; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_REBOOT, + channel_id: self.channel_id, + msg_num: self.new_message_num(), + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + ..Default::default() + }), + }; + + sub.send(msg)?; + + let msg = sub.rx.recv_timeout(RX_TIMEOUT)?; + + if let BcMeta { + response_code: 200, .. + } = msg.meta + { + Ok(()) + } else { + Err(Error::UnintelligibleReply { + reply: msg, + why: "The camera did not accept the reboot request", + }) + } + } +} diff --git a/dissector/messages.md b/dissector/messages.md index bddf73d..c200b2e 100644 --- a/dissector/messages.md +++ b/dissector/messages.md @@ -346,6 +346,25 @@ a PR to improve it. |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| | 0a bc de f0 | 00 00 00 12 | 00 00 00 00 | 1e 00 00 00 | c8 00 | 00 00 | 00 00 00 00 | +- 23: `Reboot` + + - Client + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| + | 0a bc de f0 | 00 00 00 17 | 00 00 00 00 | 00 00 00 00 | 00 00 | 64 14 | 00 00 00 00 | + + + - Camera + + - Header + + | Magic | Message ID | Message Length | Encryption Offset | Status Code | Message Class | Binary Offset | + |--------------|--------------|----------------|-------------------|-------------------|---------------|---------------| + | 0a bc de f0 | 00 00 00 17 | 00 00 00 00 | 00 00 00 00 | c8 00 | 64 14 | 00 00 00 00 | + - 25: `` (write) - Client diff --git a/src/cmdline.rs b/src/cmdline.rs index d21b213..b584623 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -12,4 +12,5 @@ pub struct Opt { pub enum Command { Rtsp(super::rtsp::Opt), StatusLight(super::statusled::Opt), + Reboot(super::reboot::Opt), } diff --git a/src/errors.rs b/src/errors.rs index 2220c4e..10b62cf 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -7,4 +7,6 @@ pub enum Error { Rtsp(#[error(source)] super::rtsp::Error), #[error(display = "Status LED Error")] StatusLight(#[error(source)] super::statusled::Error), + #[error(display = "Reboot Error")] + Reboot(#[error(source)] super::reboot::Error), } diff --git a/src/main.rs b/src/main.rs index 8b9ebdc..78b9b5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use structopt::StructOpt; mod cmdline; mod errors; +mod reboot; mod rtsp; mod statusled; @@ -37,6 +38,9 @@ fn main() -> Result<(), Error> { Command::StatusLight(opts) => { statusled::main(opts)?; } + Command::Reboot(opts) => { + reboot::main(opts)?; + } } Ok(()) diff --git a/src/reboot/cmdline.rs b/src/reboot/cmdline.rs new file mode 100644 index 0000000..8c94ee1 --- /dev/null +++ b/src/reboot/cmdline.rs @@ -0,0 +1,12 @@ +use std::path::PathBuf; +use structopt::StructOpt; + +/// The reboot command will reboot the camera +#[derive(StructOpt, Debug)] +pub struct Opt { + /// The path to the config file + #[structopt(short, long, parse(from_os_str))] + pub config: PathBuf, + /// The name of the camera to change the lights of. Must be a name in the config + pub camera: String, +} diff --git a/src/reboot/config.rs b/src/reboot/config.rs new file mode 100644 index 0000000..de60ef2 --- /dev/null +++ b/src/reboot/config.rs @@ -0,0 +1,29 @@ +use serde::Deserialize; +use std::clone::Clone; +use validator::Validate; +use validator_derive::Validate; + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct Config { + #[validate] + pub(crate) cameras: Vec, +} + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct CameraConfig { + pub(crate) name: String, + + #[serde(rename = "address")] + pub(crate) camera_addr: String, + + pub(crate) username: String, + pub(crate) password: Option, + + #[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))] + #[serde(default = "default_channel_id")] + pub(crate) channel_id: u8, +} + +fn default_channel_id() -> u8 { + 0 +} diff --git a/src/reboot/errors.rs b/src/reboot/errors.rs new file mode 100644 index 0000000..f973118 --- /dev/null +++ b/src/reboot/errors.rs @@ -0,0 +1,20 @@ +use err_derive::Error; + +/// The main error for the reboot subcommand +#[derive(Debug, Error)] +#[allow(clippy::large_enum_variant)] +pub enum Error { + /// Raised when the config file fails to deserlize + #[error(display = "Configuration parsing error")] + Config(#[error(source)] toml::de::Error), + /// Raised when `neolink_core` raises an error + #[error(display = "Communication error")] + Protocol(#[error(source)] neolink_core::Error), + /// Raised when there is an IO error such as unable to find + /// config file + #[error(display = "I/O error")] + Io(#[error(source)] std::io::Error), + /// Raised when the config file fails validataion + #[error(display = "Validation error")] + Validation(#[error(source)] validator::ValidationErrors), +} diff --git a/src/reboot/mod.rs b/src/reboot/mod.rs new file mode 100644 index 0000000..560c2b5 --- /dev/null +++ b/src/reboot/mod.rs @@ -0,0 +1,64 @@ +/// +/// # Neolink Reboot +/// +/// This module handles the reboot subcommand +/// +/// The subcommand attepts to reboot the camera. +/// +/// # Usage +/// +/// ```bash +/// neolink reboot --config=config.toml CameraName +/// ``` +/// +use log::*; +use neolink_core::bc_protocol::BcCamera; +use std::fs; +use validator::Validate; + +mod cmdline; +mod config; +mod errors; + +pub(crate) use cmdline::Opt; +use config::Config; +pub(crate) use errors::Error; + +/// Entry point for the reboot subcommand +/// +/// Opt is the command line options +pub fn main(opt: Opt) -> Result<(), Error> { + let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + + config.validate()?; + + let mut cam_found = false; + for camera_config in &config.cameras { + if opt.camera == camera_config.name { + cam_found = true; + info!( + "{}: Connecting to camera at {}", + camera_config.name, camera_config.camera_addr + ); + + let mut camera = + BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?; + + info!("{}: Logging in", camera_config.name); + camera.login(&camera_config.username, camera_config.password.as_deref())?; + + info!("{}: Connected and logged in", camera_config.name); + + camera.reboot()?; + } + } + + if !cam_found { + error!( + "No camera with the name {} was found in the config", + opt.camera + ); + } + + Ok(()) +} diff --git a/src/statusled/errors.rs b/src/statusled/errors.rs index bd2f2a5..91b4650 100644 --- a/src/statusled/errors.rs +++ b/src/statusled/errors.rs @@ -1,6 +1,6 @@ use err_derive::Error; -/// The main error for the rtsp subcommand +/// The main error for the status-light subcommand #[derive(Debug, Error)] #[allow(clippy::large_enum_variant)] pub enum Error {