More changes to run in tokio
This commit is contained in:
1 parent
25bd29f2a7
commit
c8be843ebc
34 files changed
+1193
-2002
No files matched your search
Generated
+19
-18
@@ -429,9 +429,9 @@ checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38390104763dc37a5145a53c29c63c1290b5d316d6086ec32c293f6736051bb0"
|
||||
checksum = "13e2792b0ff0340399d58445b88fd9770e3489eff258a4cbc1523418f12abf84"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
@@ -444,9 +444,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ba265a92256105f45b719605a571ffe2d1f0fea3807304b522c1d778f79eed"
|
||||
checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
@@ -454,15 +454,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac"
|
||||
checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7acc85df6714c176ab5edf386123fafe217be88c0840ec11f199441134a074e2"
|
||||
checksum = "e8de0a35a6ab97ec8869e32a2473f4b1324459e14c29275d14b10cb1fd19b50e"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
@@ -471,15 +471,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb"
|
||||
checksum = "bfb8371b6fb2aeb2d280374607aeabfc99d95c72edfe51692e42d3d7f0d08531"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bdfb8ce053d86b91919aad980c220b1fb8401a9394410e1c289ed7e66b61835d"
|
||||
checksum = "95a73af87da33b5acf53acfebdc339fe592ecf5357ac7c0a7734ab9d8c876a70"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -488,21 +488,21 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9"
|
||||
checksum = "f310820bb3e8cfd46c80db4d7fb8353e15dfff853a127158425f31e0be6c8364"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2ffb393ac5d9a6eaa9d3fdf37ae2776656b706e200c8e16b1bdb227f5198e6ea"
|
||||
checksum = "dcf79a1bf610b10f42aea489289c5a2c478a786509693b80cd39c44ccd936366"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.25"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "197676987abd2f9cadff84926f410af1c183608d36641465df73ae8211dc65d6"
|
||||
checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
@@ -1132,6 +1132,7 @@ dependencies = [
|
||||
"crossbeam-channel",
|
||||
"env_logger",
|
||||
"err-derive",
|
||||
"futures",
|
||||
"gstreamer",
|
||||
"gstreamer-app",
|
||||
"gstreamer-rtsp",
|
||||
|
||||
@@ -44,3 +44,4 @@ rumqttc = "0.8.0"
|
||||
tokio = { version = "1.0", features = ["rt", "macros", "io-util"] }
|
||||
anyhow = "1.0.42"
|
||||
async-trait = "0.1.64"
|
||||
futures = "0.3.26"
|
||||
+40
-21
@@ -4,8 +4,10 @@
|
||||
//! BcCodex is used with a `[tokio_util::codec::Framed]` to form complete packets
|
||||
//!
|
||||
use crate::bc::model::*;
|
||||
use crate::{Error, Result};
|
||||
use crate::bc::xml::*;
|
||||
use crate::{Credentials, Error, Result};
|
||||
use bytes::BytesMut;
|
||||
use log::*;
|
||||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
pub(crate) struct BcCodex {
|
||||
@@ -13,17 +15,11 @@ pub(crate) struct BcCodex {
|
||||
}
|
||||
|
||||
impl BcCodex {
|
||||
pub(crate) fn new() -> Self {
|
||||
pub(crate) fn new(credentials: Credentials) -> Self {
|
||||
Self {
|
||||
context: BcContext::new(),
|
||||
context: BcContext::new(credentials),
|
||||
}
|
||||
}
|
||||
pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol {
|
||||
self.context.get_encrypted()
|
||||
}
|
||||
pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) {
|
||||
self.context.set_encrypted(protocol);
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder<Bc> for BcCodex {
|
||||
@@ -31,7 +27,6 @@ impl Encoder<Bc> for BcCodex {
|
||||
|
||||
fn encode(&mut self, item: Bc, dst: &mut BytesMut) -> Result<()> {
|
||||
// let context = self.context.read().unwrap();
|
||||
|
||||
let buf: Vec<u8> = Default::default();
|
||||
let buf = item.serialize(buf, self.context.get_encrypted())?;
|
||||
dst.reserve(buf.len());
|
||||
@@ -44,22 +39,46 @@ impl Decoder for BcCodex {
|
||||
type Item = Bc;
|
||||
type Error = Error;
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
let bc = match { Bc::deserialize(&self.context, src) } {
|
||||
trace!("Decoding: {:X?}", src);
|
||||
let bc = Bc::deserialize(&self.context, src);
|
||||
trace!("As: {:?}", bc);
|
||||
let bc = match bc {
|
||||
Ok(bc) => bc,
|
||||
Err(Error::NomIncomplete(_)) => return Ok(None),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
// Update context
|
||||
if bc.meta.msg_id == 1 && (bc.meta.response_code >> 8) == 0xdd {
|
||||
// Login reply has the encryption info
|
||||
// Set that the encryption type now
|
||||
let encryption_protocol_byte = (bc.meta.response_code & 0xff) as usize;
|
||||
match encryption_protocol_byte {
|
||||
0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted),
|
||||
0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt),
|
||||
0x02 => self.context.set_encrypted(EncryptionProtocol::Aes(None)),
|
||||
_ => {
|
||||
return Err(Error::UnknownEncryption(encryption_protocol_byte));
|
||||
if let Bc {
|
||||
meta:
|
||||
BcMeta {
|
||||
msg_id: 1,
|
||||
response_code,
|
||||
..
|
||||
},
|
||||
body:
|
||||
BcBody::ModernMsg(ModernMsg {
|
||||
payload:
|
||||
Some(BcPayloads::BcXml(BcXml {
|
||||
encryption: Some(Encryption { nonce, .. }),
|
||||
..
|
||||
})),
|
||||
..
|
||||
}),
|
||||
} = &bc
|
||||
{
|
||||
if response_code >> 8 == 0xdd {
|
||||
// Login reply has the encryption info
|
||||
// Set that the encryption type now
|
||||
let encryption_protocol_byte = (response_code & 0xff) as usize;
|
||||
match encryption_protocol_byte {
|
||||
0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted),
|
||||
0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt),
|
||||
0x02 => self.context.set_encrypted(EncryptionProtocol::Aes(
|
||||
self.context.credentials.make_aeskey(nonce),
|
||||
)),
|
||||
_ => {
|
||||
return Err(Error::UnknownEncryption(encryption_protocol_byte));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ fn bc_modern_msg<'a>(
|
||||
msg_id: 1,
|
||||
response_code,
|
||||
..
|
||||
} if (response_code & 0xff) == 0x02 => EncryptionProtocol::Aes(None),
|
||||
} if (response_code & 0xff) == 0x02 => EncryptionProtocol::BCEncrypt, // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key
|
||||
_ => context.get_encrypted().clone(),
|
||||
};
|
||||
let processed_payload_buf =
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use crate::Credentials;
|
||||
|
||||
pub use super::xml::{BcPayloads, BcXml, Extension};
|
||||
use std::collections::HashSet;
|
||||
|
||||
@@ -175,11 +177,12 @@ pub enum EncryptionProtocol {
|
||||
BCEncrypt,
|
||||
/// Latest cameras/firmwares use Aes with the key derived from
|
||||
/// the camera's password and the negotiated NONCE
|
||||
Aes(Option<[u8; 16]>),
|
||||
Aes([u8; 16]),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BcContext {
|
||||
pub(crate) credentials: Credentials,
|
||||
pub(crate) in_bin_mode: HashSet<u16>,
|
||||
pub(crate) encryption_protocol: EncryptionProtocol,
|
||||
}
|
||||
@@ -218,8 +221,9 @@ impl Bc {
|
||||
}
|
||||
|
||||
impl BcContext {
|
||||
pub(crate) fn new() -> BcContext {
|
||||
pub(crate) fn new(credentials: Credentials) -> BcContext {
|
||||
BcContext {
|
||||
credentials,
|
||||
in_bin_mode: HashSet::new(),
|
||||
encryption_protocol: EncryptionProtocol::Unencrypted,
|
||||
}
|
||||
@@ -228,6 +232,7 @@ impl BcContext {
|
||||
#[allow(unused)] // Used in tests
|
||||
pub(crate) fn new_with_encryption(encryption_protocol: EncryptionProtocol) -> BcContext {
|
||||
BcContext {
|
||||
credentials: Default::default(),
|
||||
in_bin_mode: HashSet::new(),
|
||||
encryption_protocol,
|
||||
}
|
||||
|
||||
@@ -191,8 +191,7 @@ fn test_legacy_login_roundtrip() {
|
||||
|
||||
#[test]
|
||||
fn test_modern_login_roundtrip() {
|
||||
let mut context = BcContext::new();
|
||||
context.set_encrypted(EncryptionProtocol::BCEncrypt);
|
||||
let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt);
|
||||
|
||||
// I don't want to make up a sample message; just load it
|
||||
let sample = include_bytes!("samples/model_sample_modern_login.bin");
|
||||
|
||||
@@ -16,17 +16,12 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
|
||||
.map(|(key, i)| *i ^ key ^ (offset as u8))
|
||||
.collect()
|
||||
}
|
||||
EncryptionProtocol::Aes(key) => {
|
||||
EncryptionProtocol::Aes(aeskey) => {
|
||||
// AES decryption
|
||||
if let Some(aeskey) = key {
|
||||
let mut decrypted = buf.to_vec();
|
||||
Cfb::<Aes128>::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
|
||||
decrypted
|
||||
} else {
|
||||
// Not yet ready to decrypt (still in login phase)
|
||||
// Use BCEncrypt
|
||||
decrypt(offset, buf, &EncryptionProtocol::BCEncrypt)
|
||||
}
|
||||
|
||||
let mut decrypted = buf.to_vec();
|
||||
Cfb::<Aes128>::new(aeskey.into(), IV.into()).decrypt(&mut decrypted);
|
||||
decrypted
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,17 +36,11 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
|
||||
// Encrypt is the same as decrypt
|
||||
decrypt(offset, buf, encryption_protocol)
|
||||
}
|
||||
EncryptionProtocol::Aes(key) => {
|
||||
EncryptionProtocol::Aes(aeskey) => {
|
||||
// AES encryption
|
||||
if let Some(aeskey) = key {
|
||||
let mut encrypted = buf.to_vec();
|
||||
Cfb::<Aes128>::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);
|
||||
encrypted
|
||||
} else {
|
||||
// Not yet ready to decrypt (still in login phase)
|
||||
// Use BCEncrypt
|
||||
encrypt(offset, buf, &EncryptionProtocol::BCEncrypt)
|
||||
}
|
||||
let mut encrypted = buf.to_vec();
|
||||
Cfb::<Aes128>::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);
|
||||
encrypted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use crate::bc;
|
||||
use crate::{bc, Credentials};
|
||||
use futures::stream::StreamExt;
|
||||
use log::*;
|
||||
use std::convert::TryInto;
|
||||
use std::net::ToSocketAddrs;
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, AtomicU16, Ordering},
|
||||
Mutex,
|
||||
};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
|
||||
|
||||
use Md5Trunc::*;
|
||||
|
||||
@@ -33,7 +30,7 @@ pub use pirstate::PirState;
|
||||
pub use ptz::Direction;
|
||||
pub use resolution::*;
|
||||
use std::sync::Arc;
|
||||
pub use stream::{Stream, StreamData};
|
||||
pub use stream::{StreamData, StreamKind};
|
||||
|
||||
pub(crate) type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
@@ -55,20 +52,7 @@ pub struct BcCamera {
|
||||
logged_in: AtomicBool,
|
||||
message_num: AtomicU16,
|
||||
// Certain commands such as logout require the username/pass in plain text.... why....???
|
||||
credentials: Mutex<Option<Credentials>>,
|
||||
}
|
||||
|
||||
// Used for caching the credentials
|
||||
#[derive(Clone)]
|
||||
struct Credentials {
|
||||
username: String,
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
impl Credentials {
|
||||
fn new(username: String, password: Option<String>) -> Self {
|
||||
Self { username, password }
|
||||
}
|
||||
credentials: Credentials,
|
||||
}
|
||||
|
||||
impl Drop for BcCamera {
|
||||
@@ -92,13 +76,27 @@ impl BcCamera {
|
||||
///
|
||||
/// returns either an error or the camera
|
||||
///
|
||||
pub async fn new_with_addr<T: ToSocketAddrs>(host: T, channel_id: u8) -> Result<Self> {
|
||||
pub async fn new_with_addr<U: ToSocketAddrs, V: Into<String>, W: Into<String>>(
|
||||
host: U,
|
||||
channel_id: u8,
|
||||
username: V,
|
||||
passwd: Option<W>,
|
||||
) -> Result<Self> {
|
||||
let username: String = username.into();
|
||||
let passwd: Option<String> = passwd.map(|t| t.into());
|
||||
let addr_iter = match host.to_socket_addrs() {
|
||||
Ok(iter) => iter,
|
||||
Err(_) => return Err(Error::AddrResolutionError),
|
||||
};
|
||||
for addr in addr_iter {
|
||||
if let Ok(cam) = Self::new(SocketAddrOrUid::SocketAddr(addr), channel_id).await {
|
||||
if let Ok(cam) = Self::new(
|
||||
SocketAddrOrUid::SocketAddr(addr),
|
||||
channel_id,
|
||||
&username,
|
||||
passwd.as_ref(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
return Ok(cam);
|
||||
}
|
||||
}
|
||||
@@ -119,8 +117,19 @@ impl BcCamera {
|
||||
///
|
||||
/// returns either an error or the camera
|
||||
///
|
||||
pub async fn new_with_uid(uid: &str, channel_id: u8) -> Result<Self> {
|
||||
Self::new(SocketAddrOrUid::Uid(uid.to_string()), channel_id).await
|
||||
pub async fn new_with_uid<U: Into<String>, V: Into<String>>(
|
||||
uid: &str,
|
||||
channel_id: u8,
|
||||
username: U,
|
||||
passwd: Option<V>,
|
||||
) -> Result<Self> {
|
||||
Self::new(
|
||||
SocketAddrOrUid::Uid(uid.to_string()),
|
||||
channel_id,
|
||||
username,
|
||||
passwd,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
///
|
||||
@@ -143,16 +152,20 @@ impl BcCamera {
|
||||
///
|
||||
/// returns either an error or the camera
|
||||
///
|
||||
pub async fn new_with_addr_or_uid<T: ToSocketAddrsOrUid>(
|
||||
host: T,
|
||||
pub async fn new_with_addr_or_uid<U: ToSocketAddrsOrUid, V: Into<String>, W: Into<String>>(
|
||||
host: U,
|
||||
channel_id: u8,
|
||||
username: V,
|
||||
passwd: Option<W>,
|
||||
) -> Result<Self> {
|
||||
let addr_iter = match host.to_socket_addrs_or_uid() {
|
||||
Ok(iter) => iter,
|
||||
Err(_) => return Err(Error::AddrResolutionError),
|
||||
};
|
||||
let username: String = username.into();
|
||||
let passwd: Option<String> = passwd.map(|t| t.into());
|
||||
for addr_or_uid in addr_iter {
|
||||
if let Ok(cam) = Self::new(addr_or_uid, channel_id).await {
|
||||
if let Ok(cam) = Self::new(addr_or_uid, channel_id, &username, passwd.as_ref()).await {
|
||||
return Ok(cam);
|
||||
}
|
||||
}
|
||||
@@ -169,15 +182,30 @@ impl BcCamera {
|
||||
///
|
||||
/// * `channel_id` - The channel ID this is usually zero unless using a NVR
|
||||
///
|
||||
/// * `username` - The username to login with
|
||||
///
|
||||
/// * `passed` - The password to login with required for AES encrypted camera
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// returns either an error or the camera
|
||||
///
|
||||
pub async fn new(addr: SocketAddrOrUid, channel_id: u8) -> Result<Self> {
|
||||
let source: Box<dyn Source> = match addr {
|
||||
pub async fn new<U: Into<String>, V: Into<String>>(
|
||||
addr: SocketAddrOrUid,
|
||||
channel_id: u8,
|
||||
username: U,
|
||||
passwd: Option<V>,
|
||||
) -> Result<Self> {
|
||||
let username: String = username.into();
|
||||
let passwd: Option<String> = passwd.map(|t| t.into());
|
||||
|
||||
let (sink, source): (BcConnSink, BcConnSource) = match addr {
|
||||
SocketAddrOrUid::SocketAddr(addr) => {
|
||||
debug!("Trying address {}", addr);
|
||||
Box::new(TcpSource::new(addr).await?)
|
||||
let (x, r) = TcpSource::new(addr, &username, passwd.as_ref())
|
||||
.await?
|
||||
.split();
|
||||
(Box::new(x), Box::new(r))
|
||||
}
|
||||
SocketAddrOrUid::Uid(uid) => {
|
||||
debug!("Trying uid {}", uid);
|
||||
@@ -255,11 +283,14 @@ impl BcCamera {
|
||||
}
|
||||
last_result
|
||||
}?;
|
||||
Box::new(UdpSource::new_from_discovery(discovery).await?)
|
||||
let (x, r) = UdpSource::new_from_discovery(discovery, &username, passwd.as_ref())
|
||||
.await?
|
||||
.split();
|
||||
(Box::new(x), Box::new(r))
|
||||
}
|
||||
};
|
||||
|
||||
let conn = BcConnection::new(source).await?;
|
||||
let conn = BcConnection::new(sink, source).await?;
|
||||
|
||||
debug!("Success");
|
||||
let me = Self {
|
||||
@@ -267,7 +298,7 @@ impl BcCamera {
|
||||
message_num: AtomicU16::new(0),
|
||||
channel_id,
|
||||
logged_in: AtomicBool::new(false),
|
||||
credentials: Mutex::new(None),
|
||||
credentials: Credentials::new(username, passwd),
|
||||
};
|
||||
Ok(me)
|
||||
}
|
||||
@@ -291,18 +322,10 @@ impl BcCamera {
|
||||
}
|
||||
|
||||
// Certains commands like logout need the username and password
|
||||
// this command will return it as a tuple of (Username, Option<Password>)
|
||||
// this command will return
|
||||
// This will only work after login
|
||||
fn get_credentials(&self) -> Option<Credentials> {
|
||||
self.credentials.lock().unwrap().clone()
|
||||
}
|
||||
// This is used to store the credentials it is called during login.
|
||||
fn set_credentials(&self, username: String, password: Option<String>) {
|
||||
*(self.credentials.lock().unwrap()) = Some(Credentials::new(username, password));
|
||||
}
|
||||
// This is used to clear the stored credentials it is called during logout.
|
||||
fn clear_credentials(&self) {
|
||||
*(self.credentials.lock().unwrap()) = None;
|
||||
fn get_credentials(&self) -> &Credentials {
|
||||
&self.credentials
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,16 +349,6 @@ fn md5_string(input: &str, trunc: Md5Trunc) -> String {
|
||||
md5
|
||||
}
|
||||
|
||||
/// This is a convience function to make an AES key from the login password and the NONCE
|
||||
/// negotiated during login
|
||||
pub fn make_aes_key(nonce: &str, passwd: &str) -> [u8; 16] {
|
||||
let key_phrase = format!("{}-{}", nonce, passwd);
|
||||
let key_phrase_hash = format!("{:X}\0", md5::compute(key_phrase))
|
||||
.to_uppercase()
|
||||
.into_bytes();
|
||||
key_phrase_hash[0..16].try_into().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_md5_string() {
|
||||
// Note that these literals are only 31 characters long - see explanation above.
|
||||
|
||||
Binary file not shown.
@@ -1,11 +1,13 @@
|
||||
use super::{BcSubscription, Source};
|
||||
use super::BcSubscription;
|
||||
use crate::{bc::model::*, Error, Result};
|
||||
use futures::sink::{Sink, SinkExt};
|
||||
use futures::stream::{Stream, StreamExt};
|
||||
use log::*;
|
||||
use std::collections::btree_map::Entry;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use tokio::sync::mpsc::{channel, Sender};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use tokio::{
|
||||
sync::RwLock,
|
||||
@@ -13,6 +15,8 @@ use tokio::{
|
||||
};
|
||||
|
||||
type Subscriber = Arc<RwLock<BTreeMap<u16, Sender<Bc>>>>;
|
||||
pub(crate) type BcConnSink = Box<dyn Sink<Bc, Error = Error> + Send + Sync + Unpin>;
|
||||
pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync + Unpin>;
|
||||
|
||||
/// A shareable connection to a camera. Handles serialization of messages. To send/receive, call
|
||||
/// .[subscribe()] with a message number. You can use the BcSubscription to send or receive only
|
||||
@@ -20,28 +24,29 @@ type Subscriber = Arc<RwLock<BTreeMap<u16, Sender<Bc>>>>;
|
||||
///
|
||||
/// There can be only one subscriber per kind of message at a time.
|
||||
pub struct BcConnection {
|
||||
sink: Arc<RwLock<Box<dyn Source>>>,
|
||||
sink: Arc<Mutex<BcConnSink>>,
|
||||
subscribers: Subscriber,
|
||||
rx_thread: Mutex<Option<JoinHandle<()>>>,
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub async fn new(source: Box<dyn Source>) -> Result<Self> {
|
||||
pub async fn new(sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
|
||||
let subscribers: Subscriber = Default::default();
|
||||
|
||||
let subs = subscribers.clone();
|
||||
|
||||
let arc_source = Arc::new(RwLock::new(source));
|
||||
let conn = arc_source.clone();
|
||||
|
||||
let rx_thread = task::spawn(async move {
|
||||
loop {
|
||||
let bc = match conn.write().await.recv().await {
|
||||
Ok(bc) => bc,
|
||||
Err(e) => {
|
||||
trace!("packet Wait");
|
||||
let packet = source.next().await;
|
||||
trace!("packet: {:?}", packet);
|
||||
let bc = match packet {
|
||||
Some(Ok(bc)) => bc,
|
||||
Some(Err(e)) => {
|
||||
error!("Deserialization error: {:?}", e);
|
||||
break;
|
||||
}
|
||||
None => continue,
|
||||
};
|
||||
|
||||
if let Err(e) = Self::poll(bc, &subs).await {
|
||||
@@ -52,20 +57,22 @@ impl BcConnection {
|
||||
});
|
||||
|
||||
Ok(BcConnection {
|
||||
sink: arc_source,
|
||||
sink: Arc::new(Mutex::new(sink)),
|
||||
subscribers,
|
||||
rx_thread: Mutex::new(Some(rx_thread)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stop_polling(&self) {
|
||||
if let Some(t) = self.rx_thread.lock().unwrap().take() {
|
||||
if let Some(t) = self.rx_thread.blocking_lock().take() {
|
||||
t.abort()
|
||||
};
|
||||
}
|
||||
|
||||
pub(super) async fn send(&self, bc: Bc) -> crate::Result<()> {
|
||||
self.sink.write().await.send(bc).await?;
|
||||
trace!("send Wait: {:?}", bc);
|
||||
self.sink.lock().await.send(bc).await?;
|
||||
trace!("send Complete");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -79,7 +86,11 @@ impl BcConnection {
|
||||
}
|
||||
|
||||
pub fn unsubscribe(&self, msg_num: u16) -> Result<()> {
|
||||
self.subscribers.blocking_write().remove(&msg_num);
|
||||
let subs = self.subscribers.clone();
|
||||
tokio::task::spawn(async move {
|
||||
subs.write().await.remove(&msg_num);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -109,19 +120,12 @@ impl BcConnection {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn get_encrypted(&self) -> EncryptionProtocol {
|
||||
self.sink.read().await.get_encrypted().clone()
|
||||
}
|
||||
pub(crate) async fn set_encrypted(&self, protocol: EncryptionProtocol) {
|
||||
self.sink.write().await.set_encrypted(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BcConnection {
|
||||
fn drop(&mut self) {
|
||||
debug!("Shutting down BcConnection...");
|
||||
if let Some(t) = self.rx_thread.get_mut().unwrap().take() {
|
||||
if let Some(t) = self.rx_thread.get_mut().take() {
|
||||
t.abort()
|
||||
};
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -10,12 +10,12 @@ use tokio::net::UdpSocket;
|
||||
mod bcconn;
|
||||
mod bcsub;
|
||||
mod discovery;
|
||||
mod source;
|
||||
// mod source;
|
||||
mod tcpsource;
|
||||
mod udpsource;
|
||||
|
||||
pub(crate) use self::{
|
||||
bcconn::BcConnection, bcsub::BcSubscription, discovery::Discovery, source::*,
|
||||
bcconn::BcConnection, bcconn::*, bcsub::BcSubscription, discovery::Discovery,
|
||||
tcpsource::TcpSource, udpsource::UdpSource,
|
||||
};
|
||||
|
||||
|
||||
@@ -8,11 +8,14 @@ use crate::{bc::model::*, Result};
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Source: Send + Sync {
|
||||
pub trait Source:
|
||||
futures::stream::Stream<Item = Result<Bc>>
|
||||
+ futures::sink::Sink<Bc, Error = crate::Error>
|
||||
+ Send
|
||||
+ Sync
|
||||
{
|
||||
async fn send(&mut self, bc: Bc) -> Result<()>;
|
||||
async fn recv(&mut self) -> Result<Bc>;
|
||||
fn get_encrypted(&self) -> &EncryptionProtocol;
|
||||
fn set_encrypted(&mut self, protocol: EncryptionProtocol);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -23,13 +26,6 @@ impl Source for TcpSource {
|
||||
async fn recv(&mut self) -> Result<Bc> {
|
||||
TcpSource::recv(self).await
|
||||
}
|
||||
|
||||
fn get_encrypted(&self) -> &EncryptionProtocol {
|
||||
TcpSource::get_encrypted(self)
|
||||
}
|
||||
fn set_encrypted(&mut self, protocol: EncryptionProtocol) {
|
||||
TcpSource::set_encrypted(self, protocol)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -40,11 +36,4 @@ impl Source for UdpSource {
|
||||
async fn recv(&mut self) -> Result<Bc> {
|
||||
UdpSource::recv(self).await
|
||||
}
|
||||
|
||||
fn get_encrypted(&self) -> &EncryptionProtocol {
|
||||
UdpSource::get_encrypted(self)
|
||||
}
|
||||
fn set_encrypted(&mut self, protocol: EncryptionProtocol) {
|
||||
UdpSource::set_encrypted(self, protocol)
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
use crate::bc::codex::BcCodex;
|
||||
use crate::bc::model::*;
|
||||
use crate::Result;
|
||||
use crate::{bc::codex::BcCodex, Credentials};
|
||||
use delegate::delegate;
|
||||
use futures::{
|
||||
sink::{Sink, SinkExt},
|
||||
stream::{Stream, StreamExt},
|
||||
};
|
||||
use futures::{sink::Sink, stream::Stream};
|
||||
use std::net::SocketAddr;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
@@ -17,30 +14,17 @@ pub(crate) struct TcpSource {
|
||||
}
|
||||
|
||||
impl TcpSource {
|
||||
pub(crate) async fn new(addr: SocketAddr) -> Result<TcpSource> {
|
||||
pub(crate) async fn new<T: Into<String>, U: Into<String>>(
|
||||
addr: SocketAddr,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<TcpSource> {
|
||||
let stream = connect_to(addr).await?;
|
||||
|
||||
Ok(Self {
|
||||
inner: Framed::new(stream, BcCodex::new()),
|
||||
inner: Framed::new(stream, BcCodex::new(Credentials::new(username, password))),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> {
|
||||
self.inner.send(bc).await
|
||||
}
|
||||
pub(crate) async fn recv(&mut self) -> Result<Bc> {
|
||||
loop {
|
||||
if let Some(result) = self.inner.next().await {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol {
|
||||
self.inner.codec().get_encrypted()
|
||||
}
|
||||
pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) {
|
||||
self.inner.codec_mut().set_encrypted(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for TcpSource {
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AbortHandle {
|
||||
aborted: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl AbortHandle {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
aborted: Arc::new(AtomicBool::new(false)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn abort(&self) {
|
||||
self.aborted.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn is_aborted(&self) -> bool {
|
||||
self.aborted.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
@@ -1,570 +0,0 @@
|
||||
use super::{aborthandle::AbortHandle, Error, Result, MTU, P2P_RELAY_HOSTNAMES, WAIT_TIME};
|
||||
use crate::bcudp::{model::*, xml::*};
|
||||
use local_ip_address::local_ip;
|
||||
use log::*;
|
||||
use rand::{thread_rng, Rng};
|
||||
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
|
||||
use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket};
|
||||
use std::time::Duration;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
pub struct UdpDiscover {
|
||||
pub client_id: i32,
|
||||
pub camera_id: i32,
|
||||
pub mtu: u32,
|
||||
pub address: SocketAddr,
|
||||
}
|
||||
|
||||
impl UdpDiscover {
|
||||
// Sends data on a UDP socket until aborted or max retries reached
|
||||
fn retrying_send_to<T: ToSocketAddrs>(
|
||||
socket: &UdpSocket,
|
||||
buf: &[u8],
|
||||
addr: T,
|
||||
) -> IoResult<(usize, AbortHandle)> {
|
||||
let addr = addr
|
||||
.to_socket_addrs()?
|
||||
.next()
|
||||
.ok_or_else(|| IoError::new(ErrorKind::NotFound, Error::ConnectionUnavaliable))?;
|
||||
Self::retrying_send_to_multi(socket, buf, &[addr])
|
||||
}
|
||||
|
||||
// Sends data to multiple destinations using a UDP socket
|
||||
// until aborted or max retries reached
|
||||
fn retrying_send_to_multi<T: ToSocketAddrs>(
|
||||
socket: &UdpSocket,
|
||||
buf: &[u8],
|
||||
addrs: &[T],
|
||||
) -> IoResult<(usize, AbortHandle)> {
|
||||
let handle = AbortHandle::new();
|
||||
|
||||
let addrs: Vec<SocketAddr> = addrs
|
||||
.iter()
|
||||
.flat_map(|a| {
|
||||
a.to_socket_addrs()
|
||||
.ok()
|
||||
.and_then(|mut a_iter| a_iter.next())
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut bytes_send = 0;
|
||||
for addr in addrs.iter() {
|
||||
bytes_send = socket.send_to(buf, addr)?;
|
||||
}
|
||||
|
||||
let thread_buffer = buf.to_vec();
|
||||
let thread_handle = handle.clone();
|
||||
let thread_socket = socket.try_clone()?;
|
||||
let thread_addr: Vec<SocketAddr> = addrs;
|
||||
|
||||
const MAX_RETRIES: usize = 10;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(WAIT_TIME);
|
||||
for _ in 0..MAX_RETRIES {
|
||||
if thread_handle.is_aborted() {
|
||||
break;
|
||||
}
|
||||
for addr in thread_addr.iter() {
|
||||
let _ = thread_socket.send_to(&thread_buffer[..], addr);
|
||||
}
|
||||
std::thread::sleep(WAIT_TIME);
|
||||
}
|
||||
});
|
||||
|
||||
Ok((bytes_send, handle))
|
||||
}
|
||||
|
||||
// Performs local discovery
|
||||
//
|
||||
// This involves broadcasting a C2dC
|
||||
// Bc Discovery packet to ports 2015 and 2018
|
||||
// and awaiting a D2cCr reply
|
||||
fn discover_from_uuid_local(socket: &UdpSocket, uid: &str, timeout: Duration) -> Result<Self> {
|
||||
let mut rng = thread_rng();
|
||||
// If tid is too large it will overflow during encrypt so we just use a random u8
|
||||
let tid: u32 = (rng.gen::<u8>()) as u32;
|
||||
let client_id: i32 = rng.gen();
|
||||
let local_addr = socket.local_addr()?;
|
||||
let port = local_addr.port();
|
||||
|
||||
let mtu = MTU;
|
||||
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_c: Some(C2dC {
|
||||
uid: uid.to_string(),
|
||||
cli: ClientList { port: port as u32 },
|
||||
cid: client_id,
|
||||
mtu,
|
||||
debug: false,
|
||||
os: "MAC".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
let mut broadcasts = vec![Ipv4Addr::BROADCAST];
|
||||
for iface in get_if_addrs::get_if_addrs()?.iter() {
|
||||
if let get_if_addrs::IfAddr::V4(ifacev4) = &iface.addr {
|
||||
if let Some(broadcast) = ifacev4.broadcast.as_ref() {
|
||||
broadcasts.push(*broadcast);
|
||||
}
|
||||
}
|
||||
}
|
||||
let ports: [u16; 2] = [2015, 2018];
|
||||
let destinations: Vec<(Ipv4Addr, u16)> = broadcasts
|
||||
.iter()
|
||||
.flat_map(|&addr| {
|
||||
ports
|
||||
.iter()
|
||||
.map(|&port| (addr, port))
|
||||
.collect::<Vec<(Ipv4Addr, u16)>>()
|
||||
})
|
||||
.collect();
|
||||
debug!("Broadcasting to: {:?}", destinations);
|
||||
|
||||
let (amount_sent, abort) = Self::retrying_send_to_multi(socket, &buf[..], &destinations)?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
let start_time = OffsetDateTime::now_utc();
|
||||
let camera_address;
|
||||
let camera_id;
|
||||
loop {
|
||||
if (OffsetDateTime::now_utc() - start_time) <= timeout {
|
||||
let mut buf = vec![0; mtu as usize];
|
||||
if let Ok((bytes_read, socket_addr)) = socket.recv_from(&mut buf) {
|
||||
match BcUdp::deserialize(&buf[0..bytes_read]) {
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_c_r: Some(D2cCr { did, cid, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id => {
|
||||
abort.abort();
|
||||
camera_address = socket_addr;
|
||||
camera_id = did;
|
||||
break;
|
||||
}
|
||||
Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt),
|
||||
Err(_) => debug!(
|
||||
"Udp Discovery got this unexpected binary: {:?}",
|
||||
&buf[0..bytes_read]
|
||||
),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
abort.abort();
|
||||
return Err(Error::Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure we didn't forget to abort the sending
|
||||
abort.abort();
|
||||
// Actual connection time!
|
||||
socket.connect(camera_address)?;
|
||||
|
||||
Ok(UdpDiscover {
|
||||
address: camera_address,
|
||||
client_id,
|
||||
camera_id,
|
||||
mtu,
|
||||
})
|
||||
}
|
||||
|
||||
// This function will contact the p2p relay servers
|
||||
//
|
||||
// It will ask each of the servers for details on a specific UID
|
||||
//
|
||||
// On success it returns the M2cQr that the p2p relay
|
||||
// server has about the UID
|
||||
fn get_register(
|
||||
uid: &str,
|
||||
socket: &UdpSocket,
|
||||
timeout: Duration,
|
||||
tid: u32,
|
||||
// client_id: u32,
|
||||
) -> Result<M2cQr> {
|
||||
// let local_addr = socket.local_addr()?;
|
||||
// let local_port = local_addr.port();
|
||||
|
||||
let mtu = MTU;
|
||||
|
||||
for p2p_relay in P2P_RELAY_HOSTNAMES.iter() {
|
||||
debug!("Trying register: {}", p2p_relay);
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2m_q: Some(C2mQ {
|
||||
uid: uid.to_string(),
|
||||
os: "MAC".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
let (amount_sent, abort) =
|
||||
Self::retrying_send_to(socket, &buf[..], format!("{}:9999", p2p_relay))?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
let start_time = OffsetDateTime::now_utc();
|
||||
loop {
|
||||
if (OffsetDateTime::now_utc() - start_time) <= timeout {
|
||||
let mut buf = vec![0; mtu as usize];
|
||||
if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) {
|
||||
match BcUdp::deserialize(&buf[0..bytes_read]) {
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
m2c_q_r: Some(m2c_q_r),
|
||||
..
|
||||
},
|
||||
})) if m2c_q_r.reg.port == 0 || m2c_q_r.reg.ip.is_empty() => {
|
||||
// This register is empty
|
||||
// Sometimes a registery will return a M2cQr but it's port and addr
|
||||
// will be 0
|
||||
// In this case we ignore this server and contact another
|
||||
abort.abort();
|
||||
break;
|
||||
}
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
m2c_q_r: Some(m2c_q_r),
|
||||
..
|
||||
},
|
||||
})) => {
|
||||
abort.abort();
|
||||
return Ok(m2c_q_r);
|
||||
}
|
||||
Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt),
|
||||
Err(_) => debug!(
|
||||
"Udp Discovery got this unexpected binary: {:?}",
|
||||
&buf[0..bytes_read]
|
||||
),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
abort.abort();
|
||||
// This p2p server didn't have the data we need try another
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
debug!("All registers tried");
|
||||
Err(Error::ConnectionUnavaliable)
|
||||
}
|
||||
|
||||
// This will start remote discovery against the reolink p2p servers
|
||||
//
|
||||
// It has the following stages
|
||||
// - Contact the p2p register: This will get the register, relay and log server ip addresses
|
||||
// - Register the client ip address to the register (This can probably be used for NAT holepunching if the camera
|
||||
// tries to connect to us using this data)
|
||||
// - The register will return the camera ip/port and SID
|
||||
// - Connect to the camera using register's provided ip/port and negotiate a cid and did
|
||||
// - Send a message to the reolink log server that we will connect locally
|
||||
// - Send a message to the camera that we could connect remotely
|
||||
fn discover_from_uuid_remote(
|
||||
socket: &UdpSocket,
|
||||
uid: &str,
|
||||
timeout: Duration,
|
||||
) -> Result<UdpDiscover> {
|
||||
let local_addr = socket.local_addr()?;
|
||||
let local_port = local_addr.port();
|
||||
|
||||
let mut rng = thread_rng();
|
||||
// If tid is too large it will overflow during encrypt so we just use a random u8
|
||||
let tid: u32 = (rng.gen::<u8>()) as u32;
|
||||
let mtu = MTU;
|
||||
let client_id: i32 = rng.gen();
|
||||
|
||||
let m2c_q_r = Self::get_register(uid, socket, timeout, tid)?;
|
||||
|
||||
debug!("Got this information from the register: {:?}", m2c_q_r);
|
||||
|
||||
let register_address = m2c_q_r.reg;
|
||||
let relay_address = m2c_q_r.relay;
|
||||
let log_address = m2c_q_r.log;
|
||||
// let device_address = m2c_q_r.t;
|
||||
|
||||
let default_local_address = local_ip().expect("There to be a local ip");
|
||||
debug!("Register address found: {:?}", register_address);
|
||||
debug!("Registering this address: {:?}", default_local_address);
|
||||
|
||||
let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 };
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2r_c: Some(C2rC {
|
||||
uid: uid.to_string(),
|
||||
cli: IpPort {
|
||||
ip: default_local_address.to_string(),
|
||||
port: local_port,
|
||||
},
|
||||
relay: relay_address,
|
||||
cid: client_id,
|
||||
family: local_family,
|
||||
debug: false,
|
||||
os: "MAC".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
let (amount_sent, abort) = Self::retrying_send_to(
|
||||
socket,
|
||||
&buf[..],
|
||||
format!("{}:{}", register_address.ip, register_address.port),
|
||||
)?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
let device_sid;
|
||||
let dev_loc;
|
||||
let start_time = OffsetDateTime::now_utc();
|
||||
loop {
|
||||
if (OffsetDateTime::now_utc() - start_time) <= timeout {
|
||||
let mut buf = vec![0; mtu as usize];
|
||||
if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) {
|
||||
match BcUdp::deserialize(&buf[0..bytes_read]) {
|
||||
// Got camera data from register
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
r2c_t: Some(R2cT { dev, cid, sid, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id && !dev.ip.is_empty() && dev.port > 0 => {
|
||||
abort.abort();
|
||||
// Make a local request to the camera with the dev info
|
||||
device_sid = sid;
|
||||
dev_loc = dev;
|
||||
break;
|
||||
}
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
r2c_t: Some(R2cT { cid, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id => {
|
||||
// Got a reply but the ip/port was empty please wait
|
||||
}
|
||||
Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt),
|
||||
Err(_) => debug!(
|
||||
"Udp Discovery got this unexpected binary: {:?}",
|
||||
&buf[0..bytes_read]
|
||||
),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
abort.abort();
|
||||
return Err(Error::Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Register revealed address as SID: {:?}", device_sid);
|
||||
debug!("Register revealed address as IP: {:?}", dev_loc);
|
||||
|
||||
// ensure abort
|
||||
abort.abort();
|
||||
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_t: Some(C2dT {
|
||||
sid: device_sid,
|
||||
cid: client_id,
|
||||
mtu,
|
||||
conn: "local".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
let (amount_sent, abort) =
|
||||
Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
let device_id;
|
||||
let start_time = OffsetDateTime::now_utc();
|
||||
loop {
|
||||
if (OffsetDateTime::now_utc() - start_time) <= timeout {
|
||||
let mut buf = vec![0; mtu as usize];
|
||||
if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) {
|
||||
match BcUdp::deserialize(&buf[0..bytes_read]) {
|
||||
// Got camera data from camera
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_t: Some(D2cT { cid, did, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id => {
|
||||
device_id = did;
|
||||
break;
|
||||
}
|
||||
// Got camera data from camera CFM
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_cfm: Some(D2cCfm { cid, did, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id => {
|
||||
device_id = did;
|
||||
break;
|
||||
}
|
||||
// Got camera data from camera disc
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_disc: Some(D2cDisc { cid, did, .. }),
|
||||
..
|
||||
},
|
||||
})) if cid == client_id => {
|
||||
device_id = did;
|
||||
break;
|
||||
}
|
||||
Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt),
|
||||
Err(_) => debug!(
|
||||
"Udp Discovery got this unexpected binary: {:?}",
|
||||
&buf[0..bytes_read]
|
||||
),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
abort.abort();
|
||||
return Err(Error::Timeout);
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Got device ID as: {:?}", device_id);
|
||||
|
||||
// Ensure aborted
|
||||
abort.abort();
|
||||
|
||||
// Announce to the log that we will connect locally
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2r_cfm: Some(C2rCfm {
|
||||
sid: device_sid,
|
||||
cid: client_id,
|
||||
did: device_id,
|
||||
conn: "local".to_string(),
|
||||
rsp: 0,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
// Just let this retry to max limit as we don't get a reply
|
||||
let (amount_sent, _) = Self::retrying_send_to(
|
||||
socket,
|
||||
&buf[..],
|
||||
format!("{}:{}", log_address.ip, log_address.port),
|
||||
)?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
// Announce a map type connection (I think this means we could connect remotely)
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_t: Some(C2dT {
|
||||
sid: device_sid,
|
||||
cid: client_id,
|
||||
mtu,
|
||||
conn: "map".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
msg.serialize(&mut buf)?;
|
||||
|
||||
// Just let this retry to max limit as we don't get a reply
|
||||
let (amount_sent, _) =
|
||||
Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?;
|
||||
assert_eq!(amount_sent, buf.len());
|
||||
|
||||
let camera_address: SocketAddr = format!("{}:{}", dev_loc.ip, dev_loc.port)
|
||||
.to_socket_addrs()?
|
||||
.next()
|
||||
.ok_or(Error::ConnectionUnavaliable)?;
|
||||
|
||||
socket.connect(camera_address)?;
|
||||
|
||||
Ok(UdpDiscover {
|
||||
address: camera_address,
|
||||
client_id,
|
||||
camera_id: device_id,
|
||||
mtu,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn discover_from_uuid(
|
||||
socket: &UdpSocket,
|
||||
uid: &str,
|
||||
timeout: Duration,
|
||||
allow_remote: bool,
|
||||
) -> Result<Self> {
|
||||
match Self::discover_from_uuid_local(socket, uid, timeout) {
|
||||
Err(Error::Timeout) if allow_remote => {
|
||||
info!("Trying remote discovery against reolink servers");
|
||||
Self::discover_from_uuid_remote(socket, uid, timeout)
|
||||
}
|
||||
Ok(result) => Ok(result),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_client_disconnect(&self, socket: &UdpSocket) {
|
||||
if let Ok(addr) = socket.peer_addr() {
|
||||
let mut rng = thread_rng();
|
||||
let tid: u32 = (rng.gen::<u8>()) as u32;
|
||||
let bcudp_msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_disc: Some(C2dDisc {
|
||||
cid: self.client_id,
|
||||
did: self.camera_id,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
let buf = bcudp_msg
|
||||
.serialize(&mut buf)
|
||||
.expect("Unable to serliaze udp disconnect");
|
||||
let _ = Self::retrying_send_to(socket, buf, addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,326 +0,0 @@
|
||||
/// The module will handle a udp connection
|
||||
///
|
||||
/// Because of the complexity it is seperated into modules
|
||||
///
|
||||
/// discover module handles the initial handshake which discovers
|
||||
/// the ip from the uid and reconnects
|
||||
///
|
||||
/// transmit handles the sending and recieving of data through the socket
|
||||
/// this includes the BcUdp wrapping and the acknoledgements
|
||||
///
|
||||
use super::{Error, Result};
|
||||
use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender};
|
||||
use lazy_static::lazy_static;
|
||||
use log::*;
|
||||
use rand::{seq::SliceRandom, thread_rng, Rng};
|
||||
use std::{
|
||||
io::{BufRead, Error as IoError, ErrorKind, Read, Result as IoResult, Write},
|
||||
net::{SocketAddr, UdpSocket},
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
mod aborthandle;
|
||||
mod discover;
|
||||
mod transmit;
|
||||
|
||||
use aborthandle::*;
|
||||
use discover::*;
|
||||
use transmit::*;
|
||||
|
||||
// How long to set the socket wait
|
||||
// This is kept short so as to not block the read and write parts
|
||||
const SOCKET_WAIT_TIME: Duration = Duration::from_millis(50);
|
||||
// How long to wait between retransmits when no reply is recieved
|
||||
const WAIT_TIME: Duration = Duration::from_millis(500);
|
||||
// The maximum data size including header
|
||||
//
|
||||
// TODO: Maybe use path mtu discovery (although reolinks seems to just use this constant)
|
||||
const MTU: u32 = 1030;
|
||||
|
||||
lazy_static! {
|
||||
static ref P2P_RELAY_HOSTNAMES: [&'static str; 10] = [
|
||||
"p2p.reolink.com",
|
||||
"p2p1.reolink.com",
|
||||
"p2p2.reolink.com",
|
||||
"p2p3.reolink.com",
|
||||
"p2p6.reolink.com",
|
||||
"p2p7.reolink.com",
|
||||
"p2p8.reolink.com",
|
||||
"p2p9.reolink.com",
|
||||
"p2p14.reolink.com",
|
||||
"p2p15.reolink.com",
|
||||
];
|
||||
}
|
||||
|
||||
pub struct UdpSource {
|
||||
outgoing: Sender<Vec<u8>>,
|
||||
incoming: Receiver<Vec<u8>>,
|
||||
aborter: AbortHandle,
|
||||
timeout: Duration,
|
||||
mtu: u32,
|
||||
|
||||
read_buffer: Buffered,
|
||||
write_buffer: Buffered,
|
||||
}
|
||||
|
||||
impl UdpSource {
|
||||
pub fn new(uid: &str, timeout: Duration) -> Result<Self> {
|
||||
let (outgoing, from_outgoing) = unbounded();
|
||||
let (to_incoming, incoming) = unbounded();
|
||||
let aborter = AbortHandle::new();
|
||||
|
||||
Self::start_polling(uid, timeout, &aborter, to_incoming, from_outgoing)?;
|
||||
|
||||
Ok(Self {
|
||||
outgoing,
|
||||
incoming,
|
||||
aborter,
|
||||
timeout,
|
||||
mtu: MTU,
|
||||
|
||||
read_buffer: Default::default(),
|
||||
write_buffer: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn start_polling(
|
||||
uid: &str,
|
||||
timeout: Duration,
|
||||
aborter: &AbortHandle,
|
||||
to_incoming: Sender<Vec<u8>>,
|
||||
from_outgoing: Receiver<Vec<u8>>,
|
||||
) -> Result<()> {
|
||||
let socket = Self::get_socket(SOCKET_WAIT_TIME)?;
|
||||
let allow_remote = true;
|
||||
let discovery_result = Arc::new(UdpDiscover::discover_from_uuid(
|
||||
&socket,
|
||||
uid,
|
||||
timeout,
|
||||
allow_remote,
|
||||
)?);
|
||||
socket.connect(discovery_result.address)?;
|
||||
let transmit = Arc::new(UdpTransmit::new());
|
||||
|
||||
let thread_aborter = aborter.clone();
|
||||
let thread_transmit = transmit.clone();
|
||||
let thread_socket = socket.try_clone()?;
|
||||
let thread_discovery_result = discovery_result.clone();
|
||||
|
||||
// Poll Read
|
||||
std::thread::spawn(move || {
|
||||
while !thread_aborter.is_aborted() {
|
||||
if let Err(err) = thread_transmit.poll_read(
|
||||
&thread_socket,
|
||||
&thread_discovery_result,
|
||||
&to_incoming,
|
||||
) {
|
||||
if !thread_aborter.is_aborted() {
|
||||
match err {
|
||||
TransmitError::Disc => {
|
||||
error!("Camera requested disconnect");
|
||||
thread_aborter.abort();
|
||||
}
|
||||
e => {
|
||||
error!("Error during Udp Transmit: {:?}", e);
|
||||
thread_aborter.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
error!("Udp read poll aborted");
|
||||
// We send client disconnect here
|
||||
thread_discovery_result.send_client_disconnect(&thread_socket);
|
||||
});
|
||||
|
||||
let thread_aborter = aborter.clone();
|
||||
let thread_socket = socket.try_clone()?;
|
||||
let thread_transmit = transmit;
|
||||
let thread_discovery_result = discovery_result;
|
||||
|
||||
// Poll Write
|
||||
std::thread::spawn(move || {
|
||||
while !thread_aborter.is_aborted() {
|
||||
if let Err(err) = thread_transmit.poll_write(
|
||||
&thread_socket,
|
||||
&thread_discovery_result,
|
||||
&from_outgoing,
|
||||
) {
|
||||
if !thread_aborter.is_aborted() {
|
||||
match err {
|
||||
TransmitError::Disc => {
|
||||
error!("Camera requested disconnect");
|
||||
thread_aborter.abort();
|
||||
}
|
||||
e => {
|
||||
error!("Error during Udp Transmit: {:?}", e);
|
||||
thread_aborter.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
error!("Udp write poll aborted");
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> IoResult<Self> {
|
||||
Ok(Self {
|
||||
outgoing: self.outgoing.clone(),
|
||||
incoming: self.incoming.clone(),
|
||||
aborter: self.aborter.clone(),
|
||||
timeout: self.timeout,
|
||||
mtu: self.mtu,
|
||||
|
||||
// New buffer so they don't pollute each other
|
||||
read_buffer: Default::default(),
|
||||
write_buffer: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn stop_polling(&self) {
|
||||
self.aborter.abort();
|
||||
}
|
||||
|
||||
fn get_socket(timeout: Duration) -> Result<UdpSocket> {
|
||||
// Select a random port to bind to
|
||||
let mut ports: Vec<u16> = (53500..54000).into_iter().collect();
|
||||
let mut rng = thread_rng();
|
||||
ports.shuffle(&mut rng);
|
||||
|
||||
let addrs: Vec<_> = ports
|
||||
.iter()
|
||||
.map(|&port| SocketAddr::from(([0, 0, 0, 0], port)))
|
||||
.collect();
|
||||
let socket = UdpSocket::bind(&addrs[..])?;
|
||||
socket.set_read_timeout(Some(timeout))?;
|
||||
socket.set_write_timeout(Some(timeout))?;
|
||||
socket.set_nonblocking(false)?;
|
||||
socket.set_broadcast(true)?;
|
||||
Ok(socket)
|
||||
}
|
||||
}
|
||||
|
||||
// Ensuring polling stops
|
||||
impl Drop for UdpSource {
|
||||
fn drop(&mut self) {
|
||||
self.stop_polling();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Buffered {
|
||||
buffer: Vec<u8>,
|
||||
consumed: usize,
|
||||
}
|
||||
|
||||
impl Read for UdpSource {
|
||||
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
|
||||
if self.aborter.is_aborted() {
|
||||
return Err(IoError::new(
|
||||
ErrorKind::ConnectionAborted,
|
||||
"Connection dropped",
|
||||
));
|
||||
}
|
||||
let buffer = self.fill_buf()?;
|
||||
let amt = std::cmp::min(buf.len(), buffer.len());
|
||||
|
||||
// First check if the amount of bytes we want to read is small:
|
||||
// `copy_from_slice` will generally expand to a call to `memcpy`, and
|
||||
// for a single byte the overhead is significant.
|
||||
if amt == 1 {
|
||||
buf[0] = buffer[0];
|
||||
} else {
|
||||
buf[..amt].copy_from_slice(&buffer[..amt]);
|
||||
}
|
||||
|
||||
self.consume(amt);
|
||||
|
||||
Ok(amt)
|
||||
}
|
||||
}
|
||||
|
||||
impl BufRead for UdpSource {
|
||||
fn fill_buf(&mut self) -> IoResult<&[u8]> {
|
||||
const CLEAR_CONSUMED_AT: usize = 1024;
|
||||
// This is a trade off between caching too much dead memory
|
||||
// and calling the drain method too often
|
||||
if self.read_buffer.consumed > CLEAR_CONSUMED_AT {
|
||||
let _ = self
|
||||
.read_buffer
|
||||
.buffer
|
||||
.drain(0..self.read_buffer.consumed)
|
||||
.collect::<Vec<u8>>();
|
||||
self.read_buffer.consumed = 0;
|
||||
}
|
||||
if self.read_buffer.buffer.len() <= self.read_buffer.consumed {
|
||||
// Get next packet of the read queue
|
||||
match self.incoming.recv_timeout(self.timeout) {
|
||||
Ok(buf) => self.read_buffer.buffer.extend(buf),
|
||||
Err(RecvTimeoutError::Timeout) => {
|
||||
return Err(IoError::new(
|
||||
ErrorKind::UnexpectedEof,
|
||||
"Connection timedout when reading from udp channel",
|
||||
));
|
||||
}
|
||||
Err(RecvTimeoutError::Disconnected) => {
|
||||
return Err(IoError::new(
|
||||
ErrorKind::UnexpectedEof,
|
||||
"Connection dropped when reading from udp channel",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(&self.read_buffer.buffer.as_slice()[self.read_buffer.consumed..])
|
||||
}
|
||||
|
||||
fn consume(&mut self, amt: usize) {
|
||||
assert!(self.read_buffer.consumed + amt <= self.read_buffer.buffer.len());
|
||||
self.read_buffer.consumed += amt;
|
||||
}
|
||||
}
|
||||
|
||||
const UDPDATA_HEADER_SIZE: usize = 20;
|
||||
impl Write for UdpSource {
|
||||
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
|
||||
if self.aborter.is_aborted() {
|
||||
return Err(IoError::new(
|
||||
std::io::ErrorKind::ConnectionAborted,
|
||||
"Connection dropped",
|
||||
));
|
||||
}
|
||||
self.write_buffer.buffer.extend(buf.to_vec());
|
||||
if self.write_buffer.buffer.len() > self.mtu as usize - UDPDATA_HEADER_SIZE {
|
||||
let _ = self.flush();
|
||||
}
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> IoResult<()> {
|
||||
if self.aborter.is_aborted() {
|
||||
return Err(IoError::new(
|
||||
std::io::ErrorKind::ConnectionAborted,
|
||||
"Connection dropped",
|
||||
));
|
||||
}
|
||||
for chunk in self
|
||||
.write_buffer
|
||||
.buffer
|
||||
.chunks(self.mtu as usize - UDPDATA_HEADER_SIZE)
|
||||
{
|
||||
if self.outgoing.send(chunk.to_vec()).is_err() {
|
||||
return Err(IoError::new(
|
||||
std::io::ErrorKind::ConnectionAborted,
|
||||
"Connection dropped",
|
||||
));
|
||||
}
|
||||
}
|
||||
self.write_buffer.buffer.clear();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,346 +0,0 @@
|
||||
/// This module handles the data from Bc->BcUdp->UdpSocket
|
||||
///
|
||||
/// - It recieves Bc as Vec<u8> (after serialize)
|
||||
/// - It wraps it in BcUdp packet
|
||||
/// - It retransmits when camera dosen't acknoledge
|
||||
/// - It sends UdpAck to acknoledge recieved packets
|
||||
///
|
||||
use super::{aborthandle::AbortHandle, discover::UdpDiscover, SOCKET_WAIT_TIME, WAIT_TIME};
|
||||
use crate::bcudp::{model::*, xml::*};
|
||||
use crossbeam_channel::{Receiver, SendError, Sender, TryRecvError};
|
||||
use err_derive::Error;
|
||||
use log::*;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Error as IoError, ErrorKind};
|
||||
use std::net::UdpSocket;
|
||||
use std::sync::{
|
||||
atomic::{AtomicU32, Ordering},
|
||||
Mutex,
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum TransmitError {
|
||||
/// Error raised when IO fails such as when the connection is lost
|
||||
#[error(display = "Io error")]
|
||||
Io(#[error(source)] IoError),
|
||||
|
||||
/// Error raised when socket recv IO fails such as when the connection is lost
|
||||
#[error(display = "Socket Recv error")]
|
||||
SocketRecv { err: std::io::Error },
|
||||
|
||||
/// Error raised when socker IO fails such as when the connection is lost
|
||||
#[error(display = "Socket Send error")]
|
||||
SocketSend { err: std::io::Error },
|
||||
|
||||
/// Error raised when camera sends a disconnect request
|
||||
#[error(display = "Camera Requested Disconnected")]
|
||||
Disc,
|
||||
|
||||
/// Error raised during deserlization
|
||||
#[error(display = "BcUdp Deserialization error")]
|
||||
BcUdpDeserialization(#[error(source)] crate::bcudp::de::Error),
|
||||
|
||||
/// Error raised during serlization
|
||||
#[error(display = "BcUdp Serialization error")]
|
||||
BcUdpSerialization(#[error(source)] crate::bcudp::ser::Error),
|
||||
|
||||
/// Error raised during send
|
||||
#[error(display = "BcUdp Send to Channel Failed")]
|
||||
BcUdpSend(#[error(source)] SendError<Vec<u8>>),
|
||||
|
||||
/// Error raised during recv
|
||||
#[error(display = "BcUdp Recv from Channel Failed")]
|
||||
BcUdpRecv(#[error(source)] TryRecvError),
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, TransmitError>;
|
||||
|
||||
pub struct UdpTransmit {
|
||||
client_recieved: ClientRecieved,
|
||||
client_sent: ClientSent,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ClientSent {
|
||||
buffer: Mutex<HashMap<u32, Vec<u8>>>,
|
||||
last_sent: Mutex<Option<u32>>,
|
||||
}
|
||||
|
||||
impl ClientSent {
|
||||
fn get_next_packet_id(&self) -> u32 {
|
||||
let mut lock = self.last_sent.lock().unwrap();
|
||||
if let Some(current) = lock.as_ref() {
|
||||
*lock = Some(current + 1);
|
||||
} else {
|
||||
*lock = Some(0);
|
||||
}
|
||||
lock.unwrap()
|
||||
}
|
||||
|
||||
fn register_payload(&self, packet_id: u32, payload: Vec<u8>) {
|
||||
self.buffer
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(packet_id)
|
||||
.or_insert(payload);
|
||||
}
|
||||
|
||||
// fn acknoledge_to(&self, packet_id: u32) {
|
||||
// self.buffer.lock().unwrap().retain(|&k, _| k > packet_id);
|
||||
// }
|
||||
//
|
||||
// fn acknoledge_these(&self, packet_ids: Vec<u32>) {
|
||||
// let locked = self.buffer.lock().unwrap();
|
||||
// for packet_id in packet_ids {
|
||||
// let _ = locked.remove(&packet_id);
|
||||
// }
|
||||
// }
|
||||
|
||||
// This function translates the `UdpAck` payload directly
|
||||
fn acknoledge_from_ack_data(&self, start: u32, payload: Vec<u8>) {
|
||||
let mut locked = self.buffer.lock().unwrap();
|
||||
locked.retain(|&k, _| k > start);
|
||||
|
||||
for (idx, &value) in payload.iter().enumerate() {
|
||||
let packet_id = (start + 1) + idx as u32;
|
||||
if value > 0 {
|
||||
locked.remove(&packet_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lock(&self) -> ClientSentLocked {
|
||||
ClientSentLocked::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct ClientSentLocked<'a> {
|
||||
locked: std::sync::MutexGuard<'a, HashMap<u32, Vec<u8>>>,
|
||||
}
|
||||
|
||||
impl<'a> ClientSentLocked<'a> {
|
||||
fn new(source: &'a ClientSent) -> Self {
|
||||
let locked = source.buffer.lock().unwrap();
|
||||
Self { locked }
|
||||
}
|
||||
|
||||
fn needs_resend(&self) -> ResendIter {
|
||||
ResendIter {
|
||||
iter: self.locked.iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ResendIter<'a> {
|
||||
iter: std::collections::hash_map::Iter<'a, u32, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<'a> std::iter::Iterator for ResendIter<'a> {
|
||||
type Item = (u32, &'a Vec<u8>);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let (k, v) = self.iter.next()?;
|
||||
Some((*k, v))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ClientRecieved {
|
||||
buffer: Mutex<HashMap<u32, Vec<u8>>>,
|
||||
consumed: Mutex<Option<u32>>,
|
||||
}
|
||||
|
||||
impl ClientRecieved {
|
||||
// Consume all contigious packets
|
||||
fn consume(&self) -> Vec<Vec<u8>> {
|
||||
let mut locked = self.buffer.lock().unwrap();
|
||||
let mut results = vec![];
|
||||
if locked.keys().max().is_some() {
|
||||
// Just check there is some data
|
||||
let mut packet_id_loc = self.consumed.lock().unwrap();
|
||||
let mut next_packet_id = packet_id_loc.map(|v| v + 1).unwrap_or(0);
|
||||
while let Some(payload) = locked.remove(&next_packet_id) {
|
||||
*packet_id_loc = Some(next_packet_id);
|
||||
results.push(payload);
|
||||
next_packet_id = packet_id_loc.unwrap() + 1;
|
||||
}
|
||||
}
|
||||
results
|
||||
}
|
||||
|
||||
// Revieve a new packet only update if entry is new
|
||||
fn receive(&self, packet_id: u32, payload: Vec<u8>) {
|
||||
let mut locked = self.buffer.lock().unwrap();
|
||||
let _ = locked.entry(packet_id).or_insert(payload);
|
||||
}
|
||||
|
||||
// This gets the needed payload to form a `UdpAck` packet
|
||||
fn get_needed_packets(&self) -> Option<(u32, Vec<u8>)> {
|
||||
let locked = self.buffer.lock().unwrap();
|
||||
let packet_id_loc = self.consumed.lock().unwrap();
|
||||
|
||||
// If this is none we have recieved no packets so we cannot
|
||||
// ack anything anyways. Just return None
|
||||
let mut start = packet_id_loc.as_ref().copied()?;
|
||||
|
||||
// Find the last contigous packet
|
||||
while locked.contains_key(&(start + 1)) {
|
||||
start += 1;
|
||||
}
|
||||
|
||||
// Find last packet in buffer
|
||||
let vec = if let Some(end) = locked.keys().max() {
|
||||
let mut vec = vec![];
|
||||
// From last contiguous packet to last recieved packet
|
||||
// create a payload of `00` (unreceived) and `01` (received)
|
||||
// that can be used to form the `UdpAck` packet
|
||||
for i in (start + 1)..(end + 1) {
|
||||
if locked.contains_key(&i) {
|
||||
vec.push(1)
|
||||
} else {
|
||||
vec.push(0)
|
||||
}
|
||||
}
|
||||
vec
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
Some((start, vec))
|
||||
}
|
||||
}
|
||||
|
||||
impl UdpTransmit {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client_recieved: Default::default(),
|
||||
client_sent: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_read(
|
||||
&self,
|
||||
socket: &UdpSocket,
|
||||
discovery_result: &UdpDiscover,
|
||||
send_to_incoming: &Sender<Vec<u8>>,
|
||||
) -> Result<()> {
|
||||
// Handle recv from `socket` and send to `send_to_incoming`
|
||||
let mut buf = vec![0; discovery_result.mtu as usize];
|
||||
match socket.recv(&mut buf) {
|
||||
Ok(amount_recv) => {
|
||||
let bcudp_msg = BcUdp::deserialize(&buf[0..amount_recv])?;
|
||||
match bcudp_msg {
|
||||
BcUdp::Discovery(UdpDiscovery {
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_disc: Some(D2cDisc { cid, did }),
|
||||
..
|
||||
},
|
||||
..
|
||||
}) if cid == discovery_result.client_id
|
||||
&& did == discovery_result.camera_id =>
|
||||
{
|
||||
// Camera sent a disconnect
|
||||
// Reply with C2D_Disc and end
|
||||
discovery_result.send_client_disconnect(socket);
|
||||
return Err(TransmitError::Disc);
|
||||
}
|
||||
BcUdp::Discovery(packet) => {
|
||||
debug!("Got unexpected discovery packet: {:?}", packet)
|
||||
}
|
||||
BcUdp::Ack(UdpAck {
|
||||
connection_id,
|
||||
packet_id,
|
||||
payload,
|
||||
}) if connection_id == discovery_result.client_id => {
|
||||
self.client_sent
|
||||
.acknoledge_from_ack_data(packet_id, payload);
|
||||
}
|
||||
BcUdp::Ack(UdpAck { .. }) => {
|
||||
debug!("Got a UdpAck for another client id");
|
||||
}
|
||||
BcUdp::Data(UdpData {
|
||||
connection_id: cid,
|
||||
packet_id,
|
||||
payload,
|
||||
}) if cid == discovery_result.client_id => {
|
||||
trace!("Recieving UdpData packet {}", packet_id);
|
||||
self.client_recieved.receive(packet_id, payload);
|
||||
|
||||
for next_payload in self.client_recieved.consume().drain(..) {
|
||||
send_to_incoming.send(next_payload)?;
|
||||
}
|
||||
}
|
||||
BcUdp::Data(UdpData { .. }) => {
|
||||
debug!("Got UdpData for another client id");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => {
|
||||
// Just a timeout
|
||||
}
|
||||
Err(e) => {
|
||||
// Should stop
|
||||
return Err(TransmitError::SocketRecv { err: e });
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::significant_drop_in_scrutinee)]
|
||||
pub fn poll_write(
|
||||
&self,
|
||||
socket: &UdpSocket,
|
||||
discovery_result: &UdpDiscover,
|
||||
get_from_outgoing: &Receiver<Vec<u8>>,
|
||||
) -> Result<()> {
|
||||
// Handle Retransmit
|
||||
for (_, payload) in self.client_sent.lock().needs_resend() {
|
||||
socket
|
||||
.send(payload)
|
||||
.map_err(|e| TransmitError::SocketSend { err: e })?;
|
||||
std::thread::sleep(SOCKET_WAIT_TIME);
|
||||
}
|
||||
|
||||
// Handle recv from `send_to_incoming` and send to `socket`
|
||||
match get_from_outgoing.try_recv() {
|
||||
Ok(payload) => {
|
||||
let packet_id = self.client_sent.get_next_packet_id();
|
||||
let bcudp_msg = BcUdp::Data(UdpData {
|
||||
connection_id: discovery_result.camera_id,
|
||||
packet_id,
|
||||
payload,
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
bcudp_msg.serialize(&mut buf)?;
|
||||
socket
|
||||
.send(&buf)
|
||||
.map_err(|e| TransmitError::SocketSend { err: e })?;
|
||||
self.client_sent.register_payload(packet_id, buf);
|
||||
}
|
||||
Err(TryRecvError::Empty) => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
|
||||
// Handle acknoledgment
|
||||
let client_wants = self.client_recieved.get_needed_packets();
|
||||
if let Some((client_wants, also_wants)) = client_wants {
|
||||
let bcack_msg = BcUdp::Ack(UdpAck {
|
||||
connection_id: discovery_result.camera_id,
|
||||
packet_id: client_wants,
|
||||
payload: also_wants,
|
||||
});
|
||||
|
||||
let mut buf = vec![];
|
||||
bcack_msg.serialize(&mut buf)?;
|
||||
socket
|
||||
.send(&buf)
|
||||
.map_err(|e| TransmitError::SocketSend { err: e })?;
|
||||
}
|
||||
|
||||
std::thread::sleep(SOCKET_WAIT_TIME);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use crate::bc::codex::BcCodex;
|
||||
use crate::bc::model::*;
|
||||
use crate::bcudp::codex::BcUdpCodex;
|
||||
use crate::bcudp::model::*;
|
||||
use crate::{Error, Result};
|
||||
use crate::{Credentials, Error, Result};
|
||||
use delegate::delegate;
|
||||
use futures::{
|
||||
sink::{Sink, SinkExt},
|
||||
@@ -36,51 +36,62 @@ pub(crate) struct UdpSource {
|
||||
|
||||
impl UdpSource {
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn new(addr: SocketAddr, client_id: i32, camera_id: i32) -> Result<Self> {
|
||||
pub(crate) async fn new<T: Into<String>, U: Into<String>>(
|
||||
addr: SocketAddr,
|
||||
client_id: i32,
|
||||
camera_id: i32,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<Self> {
|
||||
let stream = connect().await?;
|
||||
|
||||
Self::new_from_socket(stream, addr, client_id, camera_id).await
|
||||
Self::new_from_socket(stream, addr, client_id, camera_id, username, password).await
|
||||
}
|
||||
pub(crate) async fn new_from_discovery(discovery: DiscoveryResult) -> Result<Self> {
|
||||
pub(crate) async fn new_from_discovery<T: Into<String>, U: Into<String>>(
|
||||
discovery: DiscoveryResult,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<Self> {
|
||||
Self::new_from_socket(
|
||||
discovery.socket,
|
||||
discovery.addr,
|
||||
discovery.client_id,
|
||||
discovery.camera_id,
|
||||
username,
|
||||
password,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn new_from_socket(
|
||||
pub(crate) async fn new_from_socket<T: Into<String>, U: Into<String>>(
|
||||
stream: UdpSocket,
|
||||
addr: SocketAddr,
|
||||
client_id: i32,
|
||||
camera_id: i32,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<Self> {
|
||||
let bcudp_source = BcUdpSource::new_from_socket(stream, addr).await?;
|
||||
let payload_source = bcudp_source.into_payload_source(client_id, camera_id);
|
||||
let async_read = payload_source.into_async_read().compat();
|
||||
let framed = Framed::new(async_read, BcCodex::new());
|
||||
let framed = Framed::new(
|
||||
async_read,
|
||||
BcCodex::new(Credentials::new(username, password)),
|
||||
);
|
||||
|
||||
Ok(Self { inner: framed })
|
||||
}
|
||||
|
||||
pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> {
|
||||
self.inner.send(bc).await
|
||||
}
|
||||
pub(crate) async fn recv(&mut self) -> Result<Bc> {
|
||||
loop {
|
||||
if let Some(result) = self.inner.next().await {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol {
|
||||
self.inner.codec().get_encrypted()
|
||||
}
|
||||
pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) {
|
||||
self.inner.codec_mut().set_encrypted(protocol)
|
||||
}
|
||||
// pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> {
|
||||
// self.inner.send(bc).await
|
||||
// }
|
||||
// pub(crate) async fn recv(&mut self) -> Result<Bc> {
|
||||
// loop {
|
||||
// if let Some(result) = self.inner.next().await {
|
||||
// return result;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl Stream for UdpSource {
|
||||
@@ -315,7 +326,7 @@ impl Stream for UdpPayloadSource {
|
||||
BcUdp::Ack(ack @ UdpAck { connection_id, .. }),
|
||||
addr,
|
||||
)))) if connection_id == this.client_id && addr == camera_addr => {
|
||||
trace!("UDPSource.RecievedPacker: Ack");
|
||||
trace!("UDPSource.RecievedPacket: Ack");
|
||||
this.handle_ack(ack);
|
||||
|
||||
for (_, resend) in this.sent.iter() {
|
||||
|
||||
@@ -100,6 +100,24 @@ pub enum Error {
|
||||
#[error(display = "Could not parse as IP")]
|
||||
AddrParseError(#[error(source)] std::net::AddrParseError),
|
||||
|
||||
/// Raised when a relay connection is not possible
|
||||
/// usually happens if the camera has not contacted reolink yet
|
||||
#[error(display = "Cannot perform relay connection with this camera")]
|
||||
NoDmap,
|
||||
|
||||
/// Raised when a dev connection is not possible
|
||||
/// usually happens if the camera has not contacted reolink yet
|
||||
#[error(display = "Cannot perform lookup with this camera against reolink servers")]
|
||||
NoDev,
|
||||
|
||||
/// Raised when cannot determine local IP address
|
||||
#[error(display = "Local IP address is unknown: {}", _0)]
|
||||
LocalIpError(#[error(source)] std::sync::Arc<local_ip_address::Error>),
|
||||
|
||||
/// Raised when a discovery fails to be accepted by the register
|
||||
#[error(display = "Register refuses to accept us")]
|
||||
RegisterError,
|
||||
|
||||
/// Raised when the stream is not enough to complete a message
|
||||
#[error(display = "Nom Parsing incomplete: {}", _0)]
|
||||
NomIncomplete(usize),
|
||||
@@ -129,6 +147,12 @@ impl From<cookie_factory::GenError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<local_ip_address::Error> for Error {
|
||||
fn from(k: local_ip_address::Error) -> Self {
|
||||
Error::LocalIpError(std::sync::Arc::new(k))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<nom::Err<NomErrorType<'a>>> for Error {
|
||||
fn from(k: nom::Err<NomErrorType<'a>>) -> Self {
|
||||
match k {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{make_aes_key, md5_string, BcCamera, Error, Result, Truncate, ZeroLast};
|
||||
use super::{md5_string, BcCamera, Error, Result, Truncate, ZeroLast};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
@@ -6,10 +6,11 @@ impl BcCamera {
|
||||
/// Login to the camera.
|
||||
///
|
||||
/// This should be called before most other commands
|
||||
pub async fn login(&self, username: &str, password: Option<&str>) -> Result<DeviceInfo> {
|
||||
pub async fn login(&self) -> Result<DeviceInfo> {
|
||||
let device_info;
|
||||
// This { is here due to the connection and set_credentials both requiring a mutable borrow
|
||||
{
|
||||
let credentials = self.get_credentials();
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_login = connection.subscribe(msg_num).await?;
|
||||
@@ -23,8 +24,10 @@ impl BcCamera {
|
||||
// Note: I suspect there may be a buffer overflow opportunity in the firmware since in the
|
||||
// Baichuan library, these strings are capped at 32 bytes with a null terminator. This
|
||||
// could also be a mistake in the library, the effect being it only compares 31 chars, not 32.
|
||||
let md5_username = md5_string(username, ZeroLast);
|
||||
let md5_password = password
|
||||
let md5_username = md5_string(&credentials.username, ZeroLast);
|
||||
let md5_password = credentials
|
||||
.password
|
||||
.as_ref()
|
||||
.map(|p| md5_string(p, ZeroLast))
|
||||
.unwrap_or_else(|| EMPTY_LEGACY_PASSWORD.to_owned());
|
||||
|
||||
@@ -70,12 +73,12 @@ impl BcCamera {
|
||||
// In the modern login flow, the username/password are concat'd with the server's nonce
|
||||
// string, then MD5'd, then the hex of this MD5 is sent as the password. This nonce
|
||||
// prevents replay attacks if the server were to require modern flow, but not rainbow table
|
||||
// attacks (since the plain user/password MD5s have already been sent). The upshot is that
|
||||
// attacks (since the plain user/password MD5s have already been sent). The upshot is that
|
||||
// you should use a very strong random password that is not found in a rainbow table and
|
||||
// not feasibly crackable with John the Ripper.
|
||||
|
||||
let modern_password = password.unwrap_or("");
|
||||
let concat_username = format!("{}{}", username, nonce);
|
||||
let modern_password = credentials.password.clone().unwrap_or_default();
|
||||
let concat_username = format!("{}{}", credentials.username, nonce);
|
||||
let concat_password = format!("{}{}", modern_password, nonce);
|
||||
let md5_username = md5_string(&concat_username, Truncate);
|
||||
let md5_password = md5_string(&concat_password, Truncate);
|
||||
@@ -131,18 +134,7 @@ impl BcCamera {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if let EncryptionProtocol::Aes(_) = connection.get_encrypted().await {
|
||||
// We setup the data for the AES key now
|
||||
// as all subsequent communications will use it
|
||||
let passwd = password.unwrap_or("");
|
||||
let full_key = make_aes_key(nonce, passwd);
|
||||
connection
|
||||
.set_encrypted(EncryptionProtocol::Aes(Some(full_key)))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
self.set_credentials(username.to_string(), password.map(|s| s.to_string()));
|
||||
Ok(device_info)
|
||||
}
|
||||
}
|
||||
@@ -6,39 +6,37 @@ impl BcCamera {
|
||||
/// Logout from the camera
|
||||
pub async fn logout(&self) -> Result<()> {
|
||||
if self.logged_in.load(Ordering::Relaxed) {
|
||||
if let Some(credentials) = self.get_credentials() {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let sub_logout = connection.subscribe(msg_num).await?;
|
||||
let credentials = self.get_credentials();
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let sub_logout = connection.subscribe(msg_num).await?;
|
||||
|
||||
let username = credentials.username.clone();
|
||||
let password = credentials.password.as_ref().cloned().unwrap_or_default();
|
||||
let username = credentials.username.clone();
|
||||
let password = credentials.password.as_ref().cloned().unwrap_or_default();
|
||||
|
||||
let modern_logout = Bc::new_from_xml(
|
||||
BcMeta {
|
||||
msg_id: MSG_ID_LOGOUT,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
stream_type: 0,
|
||||
response_code: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
BcXml {
|
||||
login_user: Some(LoginUser {
|
||||
version: xml_ver(),
|
||||
user_name: username,
|
||||
password,
|
||||
user_ver: 1,
|
||||
}),
|
||||
login_net: Some(LoginNet::default()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let modern_logout = Bc::new_from_xml(
|
||||
BcMeta {
|
||||
msg_id: MSG_ID_LOGOUT,
|
||||
channel_id: self.channel_id,
|
||||
msg_num,
|
||||
stream_type: 0,
|
||||
response_code: 0,
|
||||
class: 0x6414,
|
||||
},
|
||||
BcXml {
|
||||
login_user: Some(LoginUser {
|
||||
version: xml_ver(),
|
||||
user_name: username,
|
||||
password,
|
||||
user_ver: 1,
|
||||
}),
|
||||
login_net: Some(LoginNet::default()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
sub_logout.send(modern_logout).await?;
|
||||
}
|
||||
sub_logout.send(modern_logout).await?;
|
||||
}
|
||||
self.clear_credentials();
|
||||
self.logged_in.store(false, Ordering::Relaxed);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::{
|
||||
bcmedia::model::*,
|
||||
};
|
||||
use futures::stream::StreamExt;
|
||||
use log::*;
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
@@ -13,7 +14,7 @@ use tokio::task::{self, JoinHandle};
|
||||
|
||||
/// The stream names supported by BC
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Stream {
|
||||
pub enum StreamKind {
|
||||
/// This is the HD stream
|
||||
Main,
|
||||
/// This is the SD stream
|
||||
@@ -74,20 +75,30 @@ impl BcCamera {
|
||||
/// To pull frames from the camera's buffer use `recv_data` on the returned object
|
||||
///
|
||||
/// The buffer_size represents number of compete messages so 1 would be one complete message
|
||||
/// which may be a single audio frame or a whole video key frame
|
||||
/// which may be a single audio frame or a whole video key frame. If 0 a default of 100 is used
|
||||
///
|
||||
pub async fn start_video(&self, stream: Stream, buffer_size: usize) -> Result<StreamData> {
|
||||
pub async fn start_video(
|
||||
&self,
|
||||
stream: StreamKind,
|
||||
mut buffer_size: usize,
|
||||
) -> Result<StreamData> {
|
||||
info!("start_video");
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
|
||||
let abort_handle = Arc::new(AtomicBool::new(false));
|
||||
let abort_handle_thread = abort_handle.clone();
|
||||
|
||||
if buffer_size == 0 {
|
||||
buffer_size = 100;
|
||||
}
|
||||
let (tx, rx) = channel(buffer_size);
|
||||
let channel_id = self.channel_id;
|
||||
|
||||
let handle = task::spawn(async move {
|
||||
info!("sub_video");
|
||||
let mut sub_video = connection.subscribe(msg_num).await?;
|
||||
info!("post sub_video");
|
||||
|
||||
// On an E1 and swann cameras:
|
||||
// - mainStream always has a value of 0
|
||||
@@ -98,9 +109,9 @@ impl BcCamera {
|
||||
// - subStream is 0
|
||||
// - externStream is 0
|
||||
let stream_code = match stream {
|
||||
Stream::Main => 0,
|
||||
Stream::Sub => 1,
|
||||
Stream::Extern => 0,
|
||||
StreamKind::Main => 0,
|
||||
StreamKind::Sub => 1,
|
||||
StreamKind::Extern => 0,
|
||||
};
|
||||
|
||||
// Theses are the numbers used with the offical client
|
||||
@@ -113,15 +124,15 @@ impl BcCamera {
|
||||
// - subStream is 256
|
||||
// - externStram is 1024
|
||||
let handle = match stream {
|
||||
Stream::Main => 0,
|
||||
Stream::Sub => 256,
|
||||
Stream::Extern => 1024,
|
||||
StreamKind::Main => 0,
|
||||
StreamKind::Sub => 256,
|
||||
StreamKind::Extern => 1024,
|
||||
};
|
||||
|
||||
let stream_name = match stream {
|
||||
Stream::Main => "mainStream",
|
||||
Stream::Sub => "subStream",
|
||||
Stream::Extern => "externStream",
|
||||
StreamKind::Main => "mainStream",
|
||||
StreamKind::Sub => "subStream",
|
||||
StreamKind::Extern => "externStream",
|
||||
}
|
||||
.to_string();
|
||||
|
||||
@@ -145,7 +156,9 @@ impl BcCamera {
|
||||
},
|
||||
);
|
||||
|
||||
info!("Sending stream start");
|
||||
sub_video.send(start_video).await?;
|
||||
info!("Sent stream start");
|
||||
|
||||
let msg = sub_video.recv().await?;
|
||||
if let BcMeta {
|
||||
@@ -207,7 +220,7 @@ impl BcCamera {
|
||||
}
|
||||
|
||||
/// Stop a camera from sending more stream data.
|
||||
pub async fn stop_video(&self, stream: Stream) -> Result<()> {
|
||||
pub async fn stop_video(&self, stream: StreamKind) -> Result<()> {
|
||||
let connection = self.get_connection();
|
||||
let msg_num = self.new_message_num();
|
||||
let mut sub_video = connection.subscribe(msg_num).await?;
|
||||
@@ -221,9 +234,9 @@ impl BcCamera {
|
||||
// - subStream is 0
|
||||
// - externStream is 0
|
||||
let stream_code = match stream {
|
||||
Stream::Main => 0,
|
||||
Stream::Sub => 1,
|
||||
Stream::Extern => 0,
|
||||
StreamKind::Main => 0,
|
||||
StreamKind::Sub => 1,
|
||||
StreamKind::Extern => 0,
|
||||
};
|
||||
|
||||
// Theses are the numbers used with the offical client
|
||||
@@ -236,9 +249,9 @@ impl BcCamera {
|
||||
// - subStream is 256
|
||||
// - externStram is 1024
|
||||
let handle = match stream {
|
||||
Stream::Main => 0,
|
||||
Stream::Sub => 256,
|
||||
Stream::Extern => 1024,
|
||||
StreamKind::Main => 0,
|
||||
StreamKind::Sub => 256,
|
||||
StreamKind::Extern => 1024,
|
||||
};
|
||||
|
||||
let stop_video = Bc::new_from_xml(
|
||||
|
||||
@@ -51,6 +51,9 @@ pub struct UdpXml {
|
||||
/// C2R_CFM xml client to register CFM
|
||||
#[yaserde(rename = "C2R_CFM")]
|
||||
pub c2r_cfm: Option<C2rCfm>,
|
||||
/// C2D_A xml client to device accept
|
||||
#[yaserde(rename = "C2D_A")]
|
||||
pub c2d_a: Option<C2dA>,
|
||||
}
|
||||
|
||||
impl UdpXml {
|
||||
@@ -319,3 +322,21 @@ pub struct C2rCfm {
|
||||
/// The camera connection ID
|
||||
pub did: i32,
|
||||
}
|
||||
|
||||
/// C2D_A xml
|
||||
///
|
||||
/// Client to device accept.
|
||||
/// Sent it reply to a D2C_T
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
|
||||
pub struct C2dA {
|
||||
/// The camera SID
|
||||
pub sid: u32,
|
||||
/// Type of connection observed values are `"local"`
|
||||
pub conn: String,
|
||||
/// The client connection ID
|
||||
pub cid: i32,
|
||||
/// The camera connection ID
|
||||
pub did: i32,
|
||||
/// Maximum size in bytes of a transmission
|
||||
pub mtu: u32,
|
||||
}
|
||||
@@ -41,3 +41,53 @@ pub use bc_protocol::Error;
|
||||
pub(crate) use bc_protocol::Result;
|
||||
|
||||
pub(crate) type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>;
|
||||
|
||||
// Used for caching the credentials
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Credentials {
|
||||
username: String,
|
||||
password: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Credentials {
|
||||
/// Default credentials for some reolink cameras
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: "admin".to_string(),
|
||||
password: Some("123456".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Credentials {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_map()
|
||||
.entry(&"username", &self.username)
|
||||
.entry(&"password", &"******")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
use std::convert::TryInto;
|
||||
impl Credentials {
|
||||
pub(crate) fn new<T: Into<String>, U: Into<String>>(username: T, password: Option<U>) -> Self {
|
||||
Self {
|
||||
username: username.into(),
|
||||
password: password.map(|t| t.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// This is a convience function to make an AES key from the login password and the NONCE
|
||||
/// negotiated during login
|
||||
pub(crate) fn make_aeskey<T: AsRef<str>>(&self, nonce: T) -> [u8; 16] {
|
||||
let key_phrase = format!(
|
||||
"{}-{}",
|
||||
nonce.as_ref(),
|
||||
self.password.clone().unwrap_or_default()
|
||||
);
|
||||
let key_phrase_hash = format!("{:X}\0", md5::compute(key_phrase))
|
||||
.to_uppercase()
|
||||
.into_bytes();
|
||||
key_phrase_hash[0..16].try_into().unwrap()
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,11 @@ impl EventCam {
|
||||
camera_config.name, camera_addr
|
||||
);
|
||||
let camera = camera_addr
|
||||
.connect_camera(camera_config.channel_id)
|
||||
.connect_camera(
|
||||
camera_config.channel_id,
|
||||
&camera_config.username,
|
||||
camera_config.password.as_ref(),
|
||||
)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
@@ -162,7 +166,7 @@ impl EventCam {
|
||||
|
||||
info!("{}: Logging in", camera_config.name);
|
||||
camera
|
||||
.login(&camera_config.username, camera_config.password.as_deref())
|
||||
.login()
|
||||
.await
|
||||
.context("Failed to login to the camera")?;
|
||||
info!("{}: Connected and logged in", camera_config.name);
|
||||
|
||||
@@ -218,6 +218,8 @@ impl GstOutputs {
|
||||
// - In the case of PausedSources::None we just pass through without encoding
|
||||
// - In the case of all other pause sources we need to recencode in order
|
||||
// to get smooth streams when the stream is paused/resumed
|
||||
info!("self.video_format: {:?}", self.video_format);
|
||||
info!("self.audio_format: {:?}", self.audio_format);
|
||||
let launch_vid_select = match self.when_paused {
|
||||
PausedSources::None => match self.video_format {
|
||||
Some(StreamFormat::H264) => "! rtph264pay name=pay0",
|
||||
|
||||
+5
-3
@@ -156,11 +156,13 @@ async fn camera_main(
|
||||
|
||||
let _ = camera.manage().await;
|
||||
|
||||
info!("Init Stream");
|
||||
camera
|
||||
.stream()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not start stream", config.name))
|
||||
.map_err(CameraFailureKind::Retry)?;
|
||||
info!("Inited Stream");
|
||||
|
||||
let backoff = Backoff::new();
|
||||
let mut motion = if config.pause.on_motion {
|
||||
@@ -182,9 +184,9 @@ async fn camera_main(
|
||||
loop {
|
||||
match camera.get_state() {
|
||||
StateInfo::Streaming => {
|
||||
let can_pause = camera.can_pause();
|
||||
let can_pause = camera.can_pause().await;
|
||||
if config.pause.on_disconnect {
|
||||
if let Some(false) = camera.client_connected() {
|
||||
if let Some(false) = camera.client_connected().await {
|
||||
if can_pause {
|
||||
info!("Pause on disconnect");
|
||||
camera.pause().await.map_err(CameraFailureKind::Retry)?;
|
||||
@@ -231,7 +233,7 @@ async fn camera_main(
|
||||
}
|
||||
StateInfo::Paused => {
|
||||
if config.pause.on_disconnect {
|
||||
if let Some(true) = camera.client_connected() {
|
||||
if let Some(true) = camera.client_connected().await {
|
||||
info!("Resume on disconnect");
|
||||
camera.stream().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@ pub(crate) struct LoggedIn {}
|
||||
#[async_trait]
|
||||
impl CameraState for LoggedIn {
|
||||
async fn setup(&mut self, shared: &Shared) -> Result<(), anyhow::Error> {
|
||||
shared
|
||||
.camera
|
||||
.login(&shared.username, shared.password.as_deref())
|
||||
.await?;
|
||||
shared.camera.login().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
+18
-14
@@ -6,7 +6,7 @@ use log::*;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use neolink_core::bc_protocol::{MotionData, Stream};
|
||||
use neolink_core::bc_protocol::{MotionData, StreamKind as Stream};
|
||||
|
||||
use crate::config::{CameraConfig, UserConfig};
|
||||
use crate::rtsp::gst::RtspServer;
|
||||
@@ -62,7 +62,11 @@ impl RtspCamera {
|
||||
|
||||
info!("{}: Connecting to camera at {}", config.name, camera_addr);
|
||||
let camera = camera_addr
|
||||
.connect_camera(config.channel_id)
|
||||
.connect_camera(
|
||||
config.channel_id,
|
||||
&config.username,
|
||||
config.password.as_ref(),
|
||||
)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
@@ -149,16 +153,16 @@ impl RtspCamera {
|
||||
State::Streaming(ref mut connected) => {
|
||||
info!("{}: Logging in", &self.shared.name);
|
||||
connected.tear_down(&self.shared).await?;
|
||||
let mut state: LoggedIn = Default::default();
|
||||
state.setup(&self.shared).await?;
|
||||
let state: LoggedIn = Default::default();
|
||||
// state.setup(&self.shared).await?;
|
||||
self.state = State::LoggedIn(state);
|
||||
info!("{}: Successfully stopped straming", &self.shared.name);
|
||||
}
|
||||
State::Paused(ref mut connected) => {
|
||||
info!("{}: Logging in", &self.shared.name);
|
||||
connected.tear_down(&self.shared).await?;
|
||||
let mut state: LoggedIn = Default::default();
|
||||
state.setup(&self.shared).await?;
|
||||
let state: LoggedIn = Default::default();
|
||||
// state.setup(&self.shared).await?;
|
||||
self.state = State::LoggedIn(state);
|
||||
info!(
|
||||
"{}: Successfully stopped paused streaming",
|
||||
@@ -182,7 +186,7 @@ impl RtspCamera {
|
||||
}
|
||||
State::Paused(ref mut paused) => {
|
||||
info!("{}: Resuming stream", &self.shared.name);
|
||||
let outputs = paused.take_outputs()?;
|
||||
let outputs = paused.take_outputs().await?;
|
||||
|
||||
paused.tear_down(&self.shared).await?;
|
||||
let mut state: Streaming = Default::default();
|
||||
@@ -193,9 +197,9 @@ impl RtspCamera {
|
||||
self.state = State::Streaming(state);
|
||||
info!("{}: Successfully resumed streaming", &self.shared.name);
|
||||
}
|
||||
State::LoggedIn(ref mut loggedin) => {
|
||||
State::LoggedIn(ref mut _loggedin) => {
|
||||
info!("{}: Starting stream", &self.shared.name);
|
||||
loggedin.tear_down(&self.shared).await?;
|
||||
// loggedin.tear_down(&self.shared).await?;
|
||||
let mut state: Streaming = Default::default();
|
||||
state.setup(&self.shared).await?;
|
||||
self.state = State::Streaming(state);
|
||||
@@ -236,10 +240,10 @@ impl RtspCamera {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn client_connected(&self) -> Option<bool> {
|
||||
pub(crate) async fn client_connected(&self) -> Option<bool> {
|
||||
match &self.state {
|
||||
State::Streaming(state) => Some(state.client_connected()),
|
||||
State::Paused(state) => Some(state.client_connected()),
|
||||
State::Streaming(state) => Some(state.client_connected().await),
|
||||
State::Paused(state) => Some(state.client_connected().await),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -260,9 +264,9 @@ impl RtspCamera {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn can_pause(&self) -> bool {
|
||||
pub(crate) async fn can_pause(&self) -> bool {
|
||||
match &self.state {
|
||||
State::Streaming(state) => state.can_pause(),
|
||||
State::Streaming(state) => state.can_pause().await,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
+24
-21
@@ -8,10 +8,13 @@ use async_trait::async_trait;
|
||||
use crossbeam::utils::Backoff;
|
||||
use log::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::sync::Arc;
|
||||
use tokio::{
|
||||
sync::Mutex,
|
||||
task::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use neolink_core::bc_protocol::Stream;
|
||||
use neolink_core::bc_protocol::StreamKind as Stream;
|
||||
|
||||
use super::{CameraState, Shared};
|
||||
|
||||
@@ -77,7 +80,7 @@ impl CameraState for Paused {
|
||||
|
||||
// Lock and setup output
|
||||
{
|
||||
let mut locked_output = output.lock().unwrap();
|
||||
let mut locked_output = output.lock().await;
|
||||
locked_output.set_input_source(InputMode::Paused)?;
|
||||
}
|
||||
|
||||
@@ -89,10 +92,10 @@ impl CameraState for Paused {
|
||||
let arc_abort_handle = abort_handle.clone();
|
||||
let output_thread = output.clone();
|
||||
|
||||
let handle = thread::spawn(move || {
|
||||
let handle = task::spawn(async move {
|
||||
let backoff = Backoff::new();
|
||||
while arc_abort_handle.is_live() {
|
||||
let mut locked_output = output_thread.lock().unwrap();
|
||||
let mut locked_output = output_thread.lock().await;
|
||||
locked_output.write_last_iframe()?;
|
||||
backoff.spin();
|
||||
}
|
||||
@@ -118,7 +121,7 @@ impl CameraState for Paused {
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
info!("{}: Stopping paused stream {:?}", &shared.name, stream);
|
||||
|
||||
match handle.join() {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)),
|
||||
Ok(Ok(_)) => {}
|
||||
@@ -133,13 +136,10 @@ impl CameraState for Paused {
|
||||
impl Drop for Paused {
|
||||
fn drop(&mut self) {
|
||||
self.abort_handle.abort();
|
||||
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
info!("During_drop: Aborting paused stream {:?}", stream);
|
||||
if let Ok(Err(e)) = handle.join() {
|
||||
warn!("During drop: {:?} did not stop cleanly: {:?}", stream, e);
|
||||
} else {
|
||||
warn!("During drop: Panicked while streaming: {:?}", stream);
|
||||
let backoff = Backoff::new();
|
||||
for (_, handle) in self.handles.drain() {
|
||||
while !handle.is_finished() {
|
||||
backoff.spin();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,10 +150,10 @@ impl Paused {
|
||||
self.handles.iter().all(|(_, h)| !h.is_finished()) && self.abort_handle.is_live()
|
||||
}
|
||||
|
||||
pub(crate) fn take_outputs(&mut self) -> Result<HashMap<Stream, GstOutputs>> {
|
||||
pub(crate) async fn take_outputs(&mut self) -> Result<HashMap<Stream, GstOutputs>> {
|
||||
self.abort_handle.abort();
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
match handle.join() {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)),
|
||||
Ok(Ok(_)) => {}
|
||||
@@ -163,7 +163,7 @@ impl Paused {
|
||||
for (stream, arc_mutex_output) in self.outputs.drain() {
|
||||
let mutex_output =
|
||||
Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?;
|
||||
let output = mutex_output.into_inner()?;
|
||||
let output = mutex_output.into_inner();
|
||||
result.insert(stream, output);
|
||||
}
|
||||
Ok(result)
|
||||
@@ -177,9 +177,12 @@ impl Paused {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn client_connected(&self) -> bool {
|
||||
self.outputs
|
||||
.iter()
|
||||
.any(|(_, output)| output.lock().unwrap().is_connected())
|
||||
pub(crate) async fn client_connected(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if output.lock().await.is_connected() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use async_trait::async_trait;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use neolink_core::bc_protocol::{BcCamera, Stream};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream};
|
||||
|
||||
use crate::config::PauseConfig;
|
||||
use crate::rtsp::gst::RtspServer;
|
||||
|
||||
@@ -7,10 +7,13 @@ use async_trait::async_trait;
|
||||
use crossbeam::utils::Backoff;
|
||||
use log::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::task::{self, JoinHandle};
|
||||
use std::sync::Arc;
|
||||
use tokio::{
|
||||
sync::Mutex,
|
||||
task::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use neolink_core::bc_protocol::Stream;
|
||||
use neolink_core::bc_protocol::StreamKind as Stream;
|
||||
|
||||
use super::{CameraState, Shared};
|
||||
|
||||
@@ -77,7 +80,7 @@ impl CameraState for Streaming {
|
||||
|
||||
// Lock and setup output
|
||||
{
|
||||
let mut locked_output = output.lock().unwrap();
|
||||
let mut locked_output = output.lock().await;
|
||||
locked_output.set_input_source(InputMode::Live)?;
|
||||
}
|
||||
|
||||
@@ -93,11 +96,12 @@ impl CameraState for Streaming {
|
||||
let stream_thead = *stream;
|
||||
let handle = task::spawn(async move {
|
||||
let backoff = Backoff::new();
|
||||
info!("Pre start_video");
|
||||
let mut stream_data = arc_camera.start_video(stream_thead, 0).await?;
|
||||
|
||||
info!("Post start_video");
|
||||
while arc_abort_handle.is_live() {
|
||||
let mut data = stream_data.get_data()?;
|
||||
let mut locked_output = output_thread.lock().unwrap();
|
||||
let mut locked_output = output_thread.lock().await;
|
||||
for datum in data.drain(..) {
|
||||
locked_output.stream_recv(datum?)?;
|
||||
}
|
||||
@@ -166,7 +170,7 @@ impl Streaming {
|
||||
for (stream, arc_mutex_output) in self.outputs.drain() {
|
||||
let mutex_output =
|
||||
Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?;
|
||||
let output = mutex_output.into_inner()?;
|
||||
let output = mutex_output.into_inner();
|
||||
result.insert(stream, output);
|
||||
}
|
||||
Ok(result)
|
||||
@@ -180,15 +184,21 @@ impl Streaming {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn client_connected(&self) -> bool {
|
||||
self.outputs
|
||||
.iter()
|
||||
.any(|(_, output)| output.lock().unwrap().is_connected())
|
||||
pub(crate) async fn client_connected(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if output.lock().await.is_connected() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) fn can_pause(&self) -> bool {
|
||||
self.outputs
|
||||
.iter()
|
||||
.all(|(_, output)| output.lock().unwrap().has_last_iframe())
|
||||
pub(crate) async fn can_pause(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if !output.lock().await.has_last_iframe() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
+18
-5
@@ -33,10 +33,19 @@ impl AddressOrUid {
|
||||
}
|
||||
|
||||
// Convience method to get the BcCamera with the appropiate method
|
||||
pub(crate) async fn connect_camera(&self, channel_id: u8) -> Result<BcCamera, Error> {
|
||||
pub(crate) async fn connect_camera<T: Into<String>, U: Into<String>>(
|
||||
&self,
|
||||
channel_id: u8,
|
||||
username: T,
|
||||
passwd: Option<U>,
|
||||
) -> Result<BcCamera, Error> {
|
||||
match self {
|
||||
AddressOrUid::Address(host) => Ok(BcCamera::new_with_addr(host, channel_id).await?),
|
||||
AddressOrUid::Uid(host) => Ok(BcCamera::new_with_uid(host, channel_id).await?),
|
||||
AddressOrUid::Address(host) => {
|
||||
Ok(BcCamera::new_with_addr(host, channel_id, username, passwd).await?)
|
||||
}
|
||||
AddressOrUid::Uid(host) => {
|
||||
Ok(BcCamera::new_with_uid(host, channel_id, username, passwd).await?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,7 +64,11 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
|
||||
);
|
||||
|
||||
let camera = camera_addr
|
||||
.connect_camera(camera_config.channel_id)
|
||||
.connect_camera(
|
||||
camera_config.channel_id,
|
||||
&camera_config.username,
|
||||
camera_config.password.as_ref(),
|
||||
)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!(
|
||||
@@ -66,7 +79,7 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result<Bc
|
||||
|
||||
info!("{}: Logging in", camera_config.name);
|
||||
camera
|
||||
.login(&camera_config.username, camera_config.password.as_deref())
|
||||
.login()
|
||||
.await
|
||||
.with_context(|| format!("Failed to login to {}", camera_config.name))?;
|
||||
|
||||
|
||||
Reference in new issue
Block a user