pub extern crate bnr_xfs;
pub extern crate time;
pub use bnr_xfs::*;
pub mod arrays;
pub mod cash;
pub mod denominations;
pub mod history;
pub mod init;
pub mod maintenance;
pub mod status;
pub mod sys_config;
use std::sync::{Mutex, MutexGuard};
pub(crate) static HANDLE: Mutex<Option<DeviceHandle>> = Mutex::new(None);
pub(crate) fn init_handle(handle: DeviceHandle) -> Result<()> {
let mut lock = HANDLE.lock()?;
if lock.is_some() {
Err(Error::Usb("Global DeviceHandle already initialized".into()))
} else {
lock.replace(handle);
Ok(())
}
}
pub(crate) fn deinit_handle() -> Result<()> {
let mut handle = HANDLE.lock()?;
handle.take();
Ok(())
}
pub(crate) fn lock_handle() -> Result<MutexGuard<'static, Option<DeviceHandle>>> {
Ok(HANDLE.lock()?)
}
pub fn with_handle<T>(f: impl Fn(&DeviceHandle) -> Result<T>) -> Result<T> {
let mut handle = lock_handle()?;
let uninit_msg = "Uninitialized device handle";
match f(handle.as_ref().ok_or(Error::Usb(uninit_msg.into()))?) {
Ok(res) => Ok(res),
Err(err) => {
let err_msg = format!("{err}");
if err_msg.contains("No such device") {
log::info!("Reconnecting to BNR device...");
if let Err(err) = handle
.as_mut()
.ok_or(Error::Usb(uninit_msg.into()))?
.reconnect()
{
log::warn!("{err}");
Err(err)
} else {
Err(Error::Usb("Device reconnected successfully".into()))
}
} else {
Err(err)
}
}
}
}