diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index e6ee85e..48c8524 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -20,7 +20,7 @@ use std::time::{Duration, Instant}; /// There can be only one subscriber per kind of message at a time. pub struct BcConnection { sink: Arc>, - subscribers: Arc>>>, + subscribers: Arc>>>, rx_thread: Option>, // Arc> because it is shared between context // and connection for deserialisation and serialistion respectivly @@ -31,7 +31,7 @@ pub struct BcConnection { impl BcConnection { pub fn new(source: BcSource) -> Result { - let subscribers: Arc>>> = Default::default(); + let subscribers: Arc>>> = Default::default(); let mut subs = subscribers.clone(); @@ -96,17 +96,17 @@ impl BcConnection { Ok(()) } - pub fn subscribe(&self, msg_id: u32) -> Result { + pub fn subscribe(&self, msg_num: u16) -> Result { let (tx, rx) = channel(); - match self.subscribers.lock().unwrap().entry(msg_id) { + match self.subscribers.lock().unwrap().entry(msg_num) { Entry::Vacant(vac_entry) => vac_entry.insert(tx), - Entry::Occupied(_) => return Err(Error::SimultaneousSubscription { msg_id }), + Entry::Occupied(_) => return Err(Error::SimultaneousSubscription { msg_num }), }; - Ok(BcSubscription::new(rx, msg_id, self)) + Ok(BcSubscription::new(rx, msg_num, self)) } - pub fn unsubscribe(&self, msg_id: u32) -> Result<()> { - self.subscribers.lock().unwrap().remove(&msg_id); + pub fn unsubscribe(&self, msg_num: u16) -> Result<()> { + self.subscribers.lock().unwrap().remove(&msg_num); Ok(()) } @@ -129,7 +129,7 @@ impl BcConnection { fn poll( context: &mut BcContext, connection: &BcSource, - subscribers: &mut Arc>>>, + subscribers: &mut Arc>>>, connections_keep_alive_msg: &Arc>>, ) -> Result<()> { // Don't hold the lock during deserialization so we don't poison the subscribers mutex if @@ -139,10 +139,11 @@ impl BcConnection { subscribers.lock().unwrap().clear(); err })?; + let msg_num = response.meta.msg_num; let msg_id = response.meta.msg_id; let mut locked_subs = subscribers.lock().unwrap(); - match locked_subs.entry(msg_id) { + match locked_subs.entry(msg_num) { Entry::Occupied(mut occ) => { if occ.get_mut().send(response).is_err() { // Exceedingly unlikely, unless you mishandle the subscription object @@ -152,7 +153,7 @@ impl BcConnection { } Entry::Vacant(_) => { if msg_id != MSG_ID_UDP_KEEP_ALIVE { - debug!("Ignoring uninteresting message ID {}", msg_id); + debug!("Ignoring uninteresting message num {}", msg_id); trace!("Contents: {:?}", response); } else { // This is a keep alive message let see what the camera says about it diff --git a/crates/core/src/bc_protocol/connection/bcsub.rs b/crates/core/src/bc_protocol/connection/bcsub.rs index d84fd39..307cff3 100644 --- a/crates/core/src/bc_protocol/connection/bcsub.rs +++ b/crates/core/src/bc_protocol/connection/bcsub.rs @@ -4,17 +4,17 @@ use std::sync::mpsc::Receiver; pub struct BcSubscription<'a> { pub rx: Receiver, - msg_id: u32, + msg_num: u16, conn: &'a BcConnection, } impl<'a> BcSubscription<'a> { - pub fn new(rx: Receiver, msg_id: u32, conn: &'a BcConnection) -> BcSubscription<'a> { - BcSubscription { rx, msg_id, conn } + pub fn new(rx: Receiver, msg_num: u16, conn: &'a BcConnection) -> BcSubscription<'a> { + BcSubscription { rx, msg_num, conn } } pub fn send(&self, bc: Bc) -> Result<()> { - assert!(bc.meta.msg_id == self.msg_id); + assert!(bc.meta.msg_num == self.msg_num); self.conn.send(bc)?; Ok(()) } @@ -24,6 +24,6 @@ impl<'a> BcSubscription<'a> { impl<'a> Drop for BcSubscription<'a> { fn drop(&mut self) { // It's fine if we can't unsubscribe as that means we already have - let _ = self.conn.unsubscribe(self.msg_id); + let _ = self.conn.unsubscribe(self.msg_num); } } diff --git a/crates/core/src/bc_protocol/connection/mod.rs b/crates/core/src/bc_protocol/connection/mod.rs index 4d6a235..a6de4b8 100644 --- a/crates/core/src/bc_protocol/connection/mod.rs +++ b/crates/core/src/bc_protocol/connection/mod.rs @@ -55,7 +55,7 @@ pub enum Error { UdpSerialization(#[error(source)] bcudp::ser::Error), #[error(display = "Simultaneous subscription")] - SimultaneousSubscription { msg_id: u32 }, + SimultaneousSubscription { msg_num: u16 }, #[error(display = "Timeout")] Timeout, diff --git a/crates/core/src/bc_protocol/ledstate.rs b/crates/core/src/bc_protocol/ledstate.rs index 6be34d8..46bb2fa 100644 --- a/crates/core/src/bc_protocol/ledstate.rs +++ b/crates/core/src/bc_protocol/ledstate.rs @@ -8,12 +8,13 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_get = connection.subscribe(MSG_ID_GET_LED_STATUS)?; + let msg_num = self.new_message_num(); + let sub_get = connection.subscribe(msg_num)?; let get = Bc { meta: BcMeta { msg_id: MSG_ID_GET_LED_STATUS, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, @@ -54,7 +55,9 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_set = connection.subscribe(MSG_ID_SET_LED_STATUS)?; + + let msg_num = self.new_message_num(); + let sub_set = connection.subscribe(msg_num)?; // led_version is a field recieved from the camera but not sent // we set to None to ensure we don't send it to the camera @@ -63,7 +66,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_SET_LED_STATUS, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index 2a61ad0..4e40a2f 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -13,7 +13,8 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to log in"); - let sub_login = connection.subscribe(MSG_ID_LOGIN)?; + let msg_num = self.new_message_num(); + let sub_login = connection.subscribe(msg_num)?; // Login flow is: Send legacy login message, expect back a modern message with Encryption // details. Then, re-send the login as a modern login message. Expect back a device info @@ -33,7 +34,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_LOGIN, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0xdc02, class: 0x6514, @@ -85,7 +86,7 @@ impl BcCamera { BcMeta { msg_id: MSG_ID_LOGIN, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/logout.rs b/crates/core/src/bc_protocol/logout.rs index b6f3749..76924d5 100644 --- a/crates/core/src/bc_protocol/logout.rs +++ b/crates/core/src/bc_protocol/logout.rs @@ -10,7 +10,8 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to log in"); - let sub_logout = connection.subscribe(MSG_ID_LOGOUT)?; + let msg_num = self.new_message_num(); + let sub_logout = connection.subscribe(msg_num)?; let username = credentials.username.clone(); let password = credentials @@ -23,7 +24,7 @@ impl BcCamera { BcMeta { msg_id: MSG_ID_LOGOUT, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/ping.rs b/crates/core/src/bc_protocol/ping.rs index 0c63b0e..8c75905 100644 --- a/crates/core/src/bc_protocol/ping.rs +++ b/crates/core/src/bc_protocol/ping.rs @@ -6,13 +6,14 @@ impl BcCamera { /// or error pub fn ping(&self) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected to ping"); - let sub_ping = connection.subscribe(MSG_ID_PING)?; + let msg_num = self.new_message_num(); + let sub_ping = connection.subscribe(msg_num)?; let ping = Bc { meta: BcMeta { msg_id: MSG_ID_PING, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/pirstate.rs b/crates/core/src/bc_protocol/pirstate.rs index 98e5281..433ad88 100644 --- a/crates/core/src/bc_protocol/pirstate.rs +++ b/crates/core/src/bc_protocol/pirstate.rs @@ -8,12 +8,13 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_get = connection.subscribe(MSG_ID_GET_PIR_ALARM)?; + let msg_num = self.new_message_num(); + let sub_get = connection.subscribe(msg_num)?; let get = Bc { meta: BcMeta { msg_id: MSG_ID_GET_PIR_ALARM, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, @@ -54,13 +55,14 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_set = connection.subscribe(MSG_ID_START_PIR_ALARM)?; + let msg_num = self.new_message_num(); + let sub_set = connection.subscribe(msg_num)?; let get = Bc { meta: BcMeta { msg_id: MSG_ID_START_PIR_ALARM, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/reboot.rs b/crates/core/src/bc_protocol/reboot.rs index ce6fe47..de696c3 100644 --- a/crates/core/src/bc_protocol/reboot.rs +++ b/crates/core/src/bc_protocol/reboot.rs @@ -5,13 +5,14 @@ impl BcCamera { /// Reboot the camera pub fn reboot(&self) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected to ping"); - let sub = connection.subscribe(MSG_ID_REBOOT)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; let msg = Bc { meta: BcMeta { msg_id: MSG_ID_REBOOT, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 0f5cd1d..560f364 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -54,7 +54,8 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to start video"); - let sub_video = connection.subscribe(MSG_ID_VIDEO)?; + let msg_num = self.new_message_num(); + let sub_video = connection.subscribe(msg_num)?; // On an E1 and swann cameras: // - mainStream always has a value of 0 @@ -96,7 +97,7 @@ impl BcCamera { BcMeta { msg_id: MSG_ID_VIDEO, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: stream_code, response_code: 0, class: 0x6414, // IDK why diff --git a/crates/core/src/bc_protocol/talk.rs b/crates/core/src/bc_protocol/talk.rs index 5d2b08a..522ca37 100644 --- a/crates/core/src/bc_protocol/talk.rs +++ b/crates/core/src/bc_protocol/talk.rs @@ -16,13 +16,14 @@ impl BcCamera { pub fn talk_stop(&self) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected"); - let sub = connection.subscribe(MSG_ID_TALKRESET)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; let msg = Bc { meta: BcMeta { msg_id: MSG_ID_TALKRESET, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, @@ -61,12 +62,13 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_get = connection.subscribe(MSG_ID_TALKABILITY)?; + let msg_num = self.new_message_num(); + let sub_get = connection.subscribe(msg_num)?; let get = Bc { meta: BcMeta { msg_id: MSG_ID_TALKABILITY, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, @@ -120,7 +122,8 @@ impl BcCamera { pub fn talk(&self, adpcm: &[u8], talk_config: TalkConfig) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected"); - let sub = connection.subscribe(MSG_ID_TALKCONFIG)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; if &talk_config.audio_config.audio_type != "adpcm" { return Err(Error::UnknownTalkEncoding); @@ -133,7 +136,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_TALKCONFIG, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, @@ -181,7 +184,8 @@ impl BcCamera { } let full_block_size = block_size + 4; // Block size + predictor state - let sub = connection.subscribe(MSG_ID_TALK)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; const BLOCK_PER_PAYLOAD: usize = 4; const BLOCK_HEADER_SIZE: usize = 4; @@ -200,7 +204,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_TALK, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, @@ -259,7 +263,8 @@ impl BcCamera { pub fn talk_stream(&self, rx: Receiver>, talk_config: TalkConfig) -> Result<()> { let connection = self.connection.as_ref().expect("Must be connected"); - let sub = connection.subscribe(MSG_ID_TALKCONFIG)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; if &talk_config.audio_config.audio_type != "adpcm" { return Err(Error::UnknownTalkEncoding); @@ -272,7 +277,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_TALKCONFIG, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, @@ -320,7 +325,8 @@ impl BcCamera { } let full_block_size = block_size + 4; // Block size + predictor state - let sub = connection.subscribe(MSG_ID_TALK)?; + let msg_num = self.new_message_num(); + let sub = connection.subscribe(msg_num)?; const BLOCK_PER_PAYLOAD: usize = 1; const BLOCK_HEADER_SIZE: usize = 4; @@ -379,7 +385,7 @@ impl BcCamera { meta: BcMeta { msg_id: MSG_ID_TALK, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/time.rs b/crates/core/src/bc_protocol/time.rs index bba2cd1..d92922c 100644 --- a/crates/core/src/bc_protocol/time.rs +++ b/crates/core/src/bc_protocol/time.rs @@ -15,12 +15,13 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get time"); - let sub_get_general = connection.subscribe(MSG_ID_GET_GENERAL)?; + let msg_num = self.new_message_num(); + let sub_get_general = connection.subscribe(msg_num)?; let get = Bc { meta: BcMeta { msg_id: MSG_ID_GET_GENERAL, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, @@ -98,12 +99,13 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to set time"); - let sub_set_general = connection.subscribe(MSG_ID_SET_GENERAL)?; + let msg_num = self.new_message_num(); + let sub_set_general = connection.subscribe(msg_num)?; let set = Bc::new_from_xml( BcMeta { msg_id: MSG_ID_SET_GENERAL, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, response_code: 0, stream_type: 0, class: 0x6414, diff --git a/crates/core/src/bc_protocol/version.rs b/crates/core/src/bc_protocol/version.rs index 2781421..db4c43e 100644 --- a/crates/core/src/bc_protocol/version.rs +++ b/crates/core/src/bc_protocol/version.rs @@ -8,13 +8,14 @@ impl BcCamera { .connection .as_ref() .expect("Must be connected to get version info"); - let sub_version = connection.subscribe(MSG_ID_VERSION)?; + let msg_num = self.new_message_num(); + let sub_version = connection.subscribe(msg_num)?; let version = Bc { meta: BcMeta { msg_id: MSG_ID_VERSION, channel_id: self.channel_id, - msg_num: self.new_message_num(), + msg_num, stream_type: 0, response_code: 0, class: 0x6414, // IDK why