@@ -23,6 +23,7 @@ impl StatusHelper for ffi::Status {
2323pub struct NiFpga {
2424 session : Session ,
2525 api : NiFpgaApiContainer ,
26+ owns_session : bool ,
2627}
2728
2829macro_rules! type_wrapper {
@@ -53,11 +54,7 @@ macro_rules! type_wrapper {
5354 . to_result( )
5455 }
5556
56- pub fn $writearr_fun_name(
57- & self ,
58- indicator: Offset ,
59- value: & [ $type] ,
60- ) -> Result <( ) , Error > {
57+ pub fn $writearr_fun_name( & self , indicator: Offset , value: & [ $type] ) -> Result <( ) , Error > {
6158 self . api
6259 . base
6360 . $writearr_ffi_name( self . session, indicator, value. as_ptr( ) , value. len( ) )
@@ -195,6 +192,19 @@ impl NiFpga {
195192 }
196193 }
197194
195+ pub fn from_session ( session : Session ) -> Result < Self , Error > {
196+ let api = match NiFpgaApi :: load ( ) {
197+ Ok ( api) => api,
198+ Err ( err) => return Err ( Error :: DlOpen ( err) ) ,
199+ } ;
200+
201+ Ok ( Self {
202+ session,
203+ api,
204+ owns_session : false ,
205+ } )
206+ }
207+
198208 pub fn open (
199209 bitfile : & CString ,
200210 signature : & CString ,
@@ -218,23 +228,33 @@ impl NiFpga {
218228 )
219229 . to_result ( )
220230 {
221- Ok ( _) => Ok ( Self { session, api } ) ,
231+ Ok ( _) => Ok ( Self {
232+ session,
233+ api,
234+ owns_session : true ,
235+ } ) ,
222236 Err ( err) => Err ( err) ,
223237 }
224238 }
225239
226240 pub fn close ( self , attribute : u32 ) -> Result < ( ) , Error > {
227- self . api
228- . base
229- . NiFpgaDll_Close ( self . session , attribute)
230- . to_result ( )
241+ match self . owns_session {
242+ true => self
243+ . api
244+ . base
245+ . NiFpgaDll_Close ( self . session , attribute)
246+ . to_result ( ) ,
247+ false => Err ( Error :: ClosingUnownedSession ) ,
248+ }
231249 }
232250}
233251
234252impl Drop for NiFpga {
235253 fn drop ( & mut self ) {
236254 // TODO figure out what to do here with attribute
237255 // and the return value
238- self . api . base . NiFpgaDll_Close ( self . session , 0 ) ;
256+ if self . owns_session {
257+ self . api . base . NiFpgaDll_Close ( self . session , 0 ) ;
258+ }
239259 }
240260}
0 commit comments