@@ -23,6 +23,7 @@ impl StatusHelper for ffi::Status {
23
23
pub struct NiFpga {
24
24
session : Session ,
25
25
api : NiFpgaApiContainer ,
26
+ owns_session : bool ,
26
27
}
27
28
28
29
macro_rules! type_wrapper {
@@ -53,11 +54,7 @@ macro_rules! type_wrapper {
53
54
. to_result( )
54
55
}
55
56
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 > {
61
58
self . api
62
59
. base
63
60
. $writearr_ffi_name( self . session, indicator, value. as_ptr( ) , value. len( ) )
@@ -195,6 +192,19 @@ impl NiFpga {
195
192
}
196
193
}
197
194
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
+
198
208
pub fn open (
199
209
bitfile : & CString ,
200
210
signature : & CString ,
@@ -218,23 +228,33 @@ impl NiFpga {
218
228
)
219
229
. to_result ( )
220
230
{
221
- Ok ( _) => Ok ( Self { session, api } ) ,
231
+ Ok ( _) => Ok ( Self {
232
+ session,
233
+ api,
234
+ owns_session : true ,
235
+ } ) ,
222
236
Err ( err) => Err ( err) ,
223
237
}
224
238
}
225
239
226
240
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
+ }
231
249
}
232
250
}
233
251
234
252
impl Drop for NiFpga {
235
253
fn drop ( & mut self ) {
236
254
// TODO figure out what to do here with attribute
237
255
// 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
+ }
239
259
}
240
260
}
0 commit comments