Skip to content

Commit

Permalink
Allow opening a non owning session
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Sep 2, 2023
1 parent ca4785b commit 3f67a0b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ni-fpga/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ pub enum Error {
FixedPointPrecision(f64, u8, u8, bool),
#[error("Library Open Failed: {0}")]
DlOpen(DlOpenError),
#[error("Cannot close an unowned fpga session")]
ClosingUnownedSession,
}
42 changes: 31 additions & 11 deletions ni-fpga/src/nifpga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl StatusHelper for ffi::Status {
pub struct NiFpga {
session: Session,
api: NiFpgaApiContainer,
owns_session: bool,
}

macro_rules! type_wrapper {
Expand Down Expand Up @@ -53,11 +54,7 @@ macro_rules! type_wrapper {
.to_result()
}

pub fn $writearr_fun_name(
&self,
indicator: Offset,
value: &[$type],
) -> Result<(), Error> {
pub fn $writearr_fun_name(&self, indicator: Offset, value: &[$type]) -> Result<(), Error> {
self.api
.base
.$writearr_ffi_name(self.session, indicator, value.as_ptr(), value.len())
Expand Down Expand Up @@ -195,6 +192,19 @@ impl NiFpga {
}
}

pub fn from_session(session: Session) -> Result<Self, Error> {
let api = match NiFpgaApi::load() {
Ok(api) => api,
Err(err) => return Err(Error::DlOpen(err)),
};

Ok(Self {
session,
api,
owns_session: false,
})
}

pub fn open(
bitfile: &CString,
signature: &CString,
Expand All @@ -218,23 +228,33 @@ impl NiFpga {
)
.to_result()
{
Ok(_) => Ok(Self { session, api }),
Ok(_) => Ok(Self {
session,
api,
owns_session: true,
}),
Err(err) => Err(err),
}
}

pub fn close(self, attribute: u32) -> Result<(), Error> {
self.api
.base
.NiFpgaDll_Close(self.session, attribute)
.to_result()
match self.owns_session {
true => self
.api
.base
.NiFpgaDll_Close(self.session, attribute)
.to_result(),
false => Err(Error::ClosingUnownedSession),
}
}
}

impl Drop for NiFpga {
fn drop(&mut self) {
// TODO figure out what to do here with attribute
// and the return value
self.api.base.NiFpgaDll_Close(self.session, 0);
if self.owns_session {
self.api.base.NiFpgaDll_Close(self.session, 0);
}
}
}

0 comments on commit 3f67a0b

Please sign in to comment.