#[derive(Debug, Error)]
pub enum Error {
#[error("Packet parsing failed")]
- InvalidPacketError
+ InvalidPacketError,
+ #[error("{field} was {value:x}, which is not known")]
+ ConstraintOutOfBounds {
+ field: String,
+ value: u64,
+ },
}
pub trait Packet {
}
if (!constrained_descendants.empty()) {
- s << "_ => panic!(\"unexpected value " << "\"),";
+ s << "v => return Err(Error::ConstraintOutOfBounds{field: \"" << constraint_name
+ << "\".to_string(), value: v as u64}),";
}
s << "};\n";
}
fn on_event(data: &[u8]) {
+ log::error!("got event: {:02x?}", data);
let callbacks = CALLBACKS.lock().unwrap();
- callbacks.as_ref().unwrap().evt_tx.send(EventPacket::parse(data).unwrap()).unwrap();
+ match EventPacket::parse(data) {
+ Ok(p) => callbacks.as_ref().unwrap().evt_tx.send(p).unwrap(),
+ Err(e) => log::error!("failure to parse event: {:?} data: {:02x?}", e, data),
+ }
}
fn on_acl(data: &[u8]) {
let callbacks = CALLBACKS.lock().unwrap();
- callbacks.as_ref().unwrap().acl_tx.send(AclPacket::parse(data).unwrap()).unwrap();
+ match AclPacket::parse(data) {
+ Ok(p) => callbacks.as_ref().unwrap().acl_tx.send(p).unwrap(),
+ Err(e) => log::error!("failure to parse incoming ACL: {:?} data: {:02x?}", e, data),
+ }
}
fn on_sco(_data: &[u8]) {}
data: &[u8],
callback: cxx::UniquePtr<ffi::u8SliceOnceCallback>,
) {
- let packet = CommandPacket::parse(data).unwrap();
- let mut commands = hci.internal.commands.clone();
- hci.rt.spawn(async move {
- let resp = commands.send(packet).await.unwrap();
- callback.Run(&resp.to_bytes());
- });
+ log::error!("sending command: {:02x?}", data);
+ match CommandPacket::parse(data) {
+ Ok(packet) => {
+ let mut commands = hci.internal.commands.clone();
+ hci.rt.spawn(async move {
+ let resp = commands.send(packet).await.unwrap();
+ callback.Run(&resp.to_bytes());
+ });
+ }
+ Err(e) => panic!("could not parse command: {:?} {:02x?}", e, data),
+ }
}
pub fn hci_send_acl(hci: &mut Hci, data: &[u8]) {
- hci.rt.block_on(hci.internal.acl_tx.send(AclPacket::parse(data).unwrap())).unwrap();
+ match AclPacket::parse(data) {
+ Ok(packet) => hci.rt.block_on(hci.internal.acl_tx.send(packet)).unwrap(),
+ Err(e) => panic!("could not parse acl: {:?} {:02x?}", e, data),
+ }
}
pub fn hci_register_event(hci: &mut Hci, event: u8) {
// little endian command opcode
uint16_t command_op_code = (data[1] << 8 | data[0]);
- // Gd stack API requires opcode specification and calculates length, so
- // no need to provide opcode or length here.
- data += (kCommandOpcodeSize + kCommandLengthSize);
- len -= (kCommandOpcodeSize + kCommandLengthSize);
-
auto op_code = static_cast<const bluetooth::hci::OpCode>(command_op_code);
LOG_DEBUG("Sending command %s", bluetooth::hci::OpCodeText(op_code).c_str());