Subscribe on message number
This commit is contained in:
13 files changed
+74
-54
No files matched your search
@@ -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<Mutex<BcSource>>,
|
||||
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
|
||||
subscribers: Arc<Mutex<BTreeMap<u16, Sender<Bc>>>>,
|
||||
rx_thread: Option<JoinHandle<()>>,
|
||||
// Arc<Mutex<EncryptionProtocol>> 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<Self> {
|
||||
let subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>> = Default::default();
|
||||
let subscribers: Arc<Mutex<BTreeMap<u16, Sender<Bc>>>> = Default::default();
|
||||
|
||||
let mut subs = subscribers.clone();
|
||||
|
||||
@@ -96,17 +96,17 @@ impl BcConnection {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn subscribe(&self, msg_id: u32) -> Result<BcSubscription> {
|
||||
pub fn subscribe(&self, msg_num: u16) -> Result<BcSubscription> {
|
||||
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<Mutex<BTreeMap<u32, Sender<Bc>>>>,
|
||||
subscribers: &mut Arc<Mutex<BTreeMap<u16, Sender<Bc>>>>,
|
||||
connections_keep_alive_msg: &Arc<Mutex<Option<Bc>>>,
|
||||
) -> 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
|
||||
|
||||
@@ -4,17 +4,17 @@ use std::sync::mpsc::Receiver;
|
||||
|
||||
pub struct BcSubscription<'a> {
|
||||
pub rx: Receiver<Bc>,
|
||||
msg_id: u32,
|
||||
msg_num: u16,
|
||||
conn: &'a BcConnection,
|
||||
}
|
||||
|
||||
impl<'a> BcSubscription<'a> {
|
||||
pub fn new(rx: Receiver<Bc>, msg_id: u32, conn: &'a BcConnection) -> BcSubscription<'a> {
|
||||
BcSubscription { rx, msg_id, conn }
|
||||
pub fn new(rx: Receiver<Bc>, 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);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Vec<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);
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user