Skip to content

Commit

Permalink
Bug fixes and making serde respect the status code to send
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumEntangledAndy committed Apr 17, 2021
1 parent 1227ff1 commit 644d888
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 24 deletions.
7 changes: 1 addition & 6 deletions src/bc/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn bc_modern_msg<'a, 'b>(
match encryption_protocol_byte {
0x00 => context.set_encrypted(EncryptionProtocol::Unencrypted),
0x01 => context.set_encrypted(EncryptionProtocol::BCEncrypt),
0x03 => context.set_encrypted(EncryptionProtocol::Aes(None)),
0x02 => context.set_encrypted(EncryptionProtocol::Aes(None)),
_ => return Err(Err::Error(make_error(buf, ErrorKind::MapRes))),
}
}
Expand Down Expand Up @@ -189,11 +189,6 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
let (buf, msg_num) = le_u16(buf)?;
let (buf, (response_code, class)) = tuple((le_u16, le_u16))(buf)?;

// All modern messages are encrypted. In addition, it seems that the camera firmware checks
// this field to see if some other messages should be encrypted. This is still somewhat fuzzy.
// A copy of the source code for the camera would be very useful.
let encrypted = response_code != 0;

let (buf, payload_offset) = cond(has_payload_offset(class), le_u32)(buf)?;

Ok((
Expand Down
2 changes: 1 addition & 1 deletion src/bc/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub enum EncryptionProtocol {
#[derive(Debug)]
pub struct BcContext {
pub(super) in_bin_mode: HashSet<u32>,
// Arc<AtomicEncryptionProtocol> because it is shared between context
// Arc<Mutex<EncryptionProtocol>> because it is shared between context
// and connection for deserialisation and serialistion respectivly
pub(super) encryption_protocol: Arc<Mutex<EncryptionProtocol>>,
}
Expand Down
17 changes: 2 additions & 15 deletions src/bc/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ impl Bc {
// First serialize ext
let (temp_buf, ext_len) = gen(
opt_ref(&modern.extension, |ext| {
bc_ext(
self.meta.channel_id as u32,
ext,
encryption_protocol,
)
bc_ext(self.meta.channel_id as u32, ext, encryption_protocol)
}),
vec![],
)?;
Expand Down Expand Up @@ -106,23 +102,14 @@ fn bc_payload<W: Write>(
}

fn bc_header<W: Write>(header: &BcHeader) -> impl SerializeFn<W> {
// TODO this is actually a u16 "response code" in modern messages
let (signaled_encryption, spare) = if header.class == 0x0000 {
(0x00, 0x00)
} else {
// Client is always 01dc
// Camera may reply with 01dd (encrypted) or 00dd (unencrypted)
(0x01, 0xdc)
};
tuple((
le_u32(MAGIC_HEADER),
le_u32(header.msg_id),
le_u32(header.body_len),
le_u8(header.channel_id),
le_u8(header.stream_type),
le_u16(header.msg_num),
le_u8(signaled_encryption),
le_u8(spare), // skipped byte
le_u16(header.response_code),
le_u16(header.class),
opt(header.payload_offset, le_u32),
))
Expand Down
4 changes: 2 additions & 2 deletions src/bc_protocol/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct BcConnection {
connection: Arc<Mutex<TcpStream>>,
subscribers: Arc<Mutex<BTreeMap<u32, Sender<Bc>>>>,
rx_thread: Option<JoinHandle<()>>,
// Arc<AtomicEncryptionProtocol> because it is shared between context
// Arc<Mutex<EncryptionProtocol>> because it is shared between context
// and connection for deserialisation and serialistion respectivly
encryption_protocol: Arc<Mutex<EncryptionProtocol>>,
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'a> BcSubscription<'a> {

bc.serialize(
&*self.conn.connection.lock().unwrap(),
self.conn.get_encrypted(),
&self.conn.get_encrypted(),
)?;
Ok(())
}
Expand Down

0 comments on commit 644d888

Please sign in to comment.