001    /**
002       \file       NiRioStatus.h
003       \author     Dave Madden <david.madden@ni.com>
004       \date       02/21/2006
005    
006       � Copyright 2006-2007. National Instruments. All rights reserved.
007    */
008    
009    package com.ni.rio;
010    
011    import com.sun.cldc.jna.ptr.IntByReference;
012    
013    /**
014     * The NiRioStatus class encapsulates a cRIO status value.
015     * 
016     * The NiRioStatus class also defines the various status constants used by the cRIO. 
017     * 
018     * @todo Why are we storing the status value in an IntByReference? We should be able to use a simple int field.
019     */
020    public class NiRioStatus
021    {
022       public static final class FatalStatusException extends IllegalStateException
023       {
024          NiRioStatus m_status;
025          public FatalStatusException(NiRioStatus status, String message)
026          {
027             super(message);
028             m_status = status;
029          }
030          public NiRioStatus getStatus()
031          {
032             return m_status;
033          }
034       }
035    
036       public NiRioStatus()
037       {
038          status = new IntByReference(kRioStatusSuccess);
039       }
040    
041       private IntByReference status;
042    
043       public int getPointer()
044       {
045          return status.getPointer().address().toUWord().toPrimitive();
046       }
047    
048       // Valid RIO ranges: [-63199, -63000], [63000,63199]
049       public static final int kRioStatusOffset = -63000;
050    
051       /// The operation was successful
052       public static final int kRioStatusSuccess = 0;
053    
054       // Errors
055    
056       // -------------------------------------------------------------------------
057       // FIFO Error messages...
058       // -------------------------------------------------------------------------
059    
060       /// DMA to the FPGA target is not supported by the controller associated with the FPGA (e.g. cRIO-9002/4).
061       public static final int kRioStatusDmaOutputNotSupported        = kRioStatusOffset - 1;
062    
063       // -------------------------------------------------------------------------
064       // IO Manager
065       // -------------------------------------------------------------------------
066    
067       /// <internal>The parameters do not describe a valid address range</internal>
068       public static final int kRIOStatusIOInvalidAddressRange        = kRioStatusOffset - 10;
069    
070       /// <internal>The buffer supplied for the I/O operation in invalid</internal>
071       public static final int kRIOStatusIOInvalidBuffer              = kRioStatusOffset - 11;
072    
073       // -------------------------------------------------------------------------
074       // Device Errors
075       // -------------------------------------------------------------------------
076    
077       /// The operation could not complete because another session has reconfigured the device.
078       ///
079       /// DEPRECATED in 230
080       public static final int kRIOStatusDeviceReconfigured           = kRioStatusOffset - 30;
081    
082       /// <internal>The operation is not allowed because another session is accessing the device. Close all other sessions and retry.</internal>
083       public static final int kRIOStatusDeviceInvariant              = kRioStatusOffset - 31;
084    
085       /// <internal>Download is not allowed because another session is accessing the device. Close all other sessions and retry</internal>.
086       public static final int kRIOStatusDeviceInvalidStateTransition = kRioStatusOffset - 32;
087    
088       /// The operation was prohibited by the device access settings on the remote system.
089       public static final int kRIOStatusAccessDenied                 = kRioStatusOffset - 33;
090    
091    
092       // -------------------------------------------------------------------------
093       // RPC/Network Errors
094       // -------------------------------------------------------------------------
095    
096       /// An RPC connection could not be made to the remote device. The device may be offline, disconnected, or NI-RIO software may be missing or configured incorrectly
097       public static final int kRIOStatusRPCConnectionError           = kRioStatusOffset - 40;
098    
099       /// The RPC server had an error. Close any open sessions and reconnect. The current operation cannot complete.
100       public static final int kRIOStatusRPCServerError               = kRioStatusOffset - 41;
101    
102       /// A fault on the network caused the operation to fail.
103       public static final int kRIOStatusNetworkFault                 = kRioStatusOffset - 42;
104    
105       /// The current session is invalid. The target may have reset or been
106       /// rebooted. The current operation cannot complete. Try again.
107       public static final int kRIOStatusRioRpcSessionError           = kRioStatusOffset - 43;
108    
109       // -------------------------------------------------------------------------
110       // Session Errors
111       // -------------------------------------------------------------------------
112       /// The specified trigger line is already reserved
113       public static final int kRIOStatusTriggerReserved              = kRioStatusOffset - 50;
114    
115       /// The specified trigger line is not reserved by the current session.
116       public static final int kRIOStatusTriggerNotReserved           = kRioStatusOffset - 51;
117    
118       /// Trigger lines are not supported or enabled. For PXI chasses, identify the controller and chassis via MAX.
119       public static final int kRIOStatusTriggerNotSupported          = kRioStatusOffset - 52;
120    
121    
122       // -------------------------------------------------------------------------
123       // Event Errors
124       // -------------------------------------------------------------------------
125    
126       /// <internal>The specified event type is invalid.</internal>
127       public static final int kRIOStatusEventInvalid                 = kRioStatusOffset - 70;
128    
129       /// The specified RIO event has already been enabled for this session.
130       public static final int kRIOStatusEventEnabled                 = kRioStatusOffset - 71;
131    
132       /// The specified RIO event has not been enabled for this session.
133       public static final int kRIOStatusEventNotEnabled              = kRioStatusOffset - 72;
134    
135       ///  The specified RIO event did not complete within the specified time limit.
136       public static final int kRIOStatusEventTimedOut                = kRioStatusOffset - 73;
137    
138       /// <internal>The specified operation on a the specified Rio event is invalid at this time</internal>.
139       public static final int kRIOStatusEventInvalidOperation        = kRioStatusOffset - 74;
140    
141       // -------------------------------------------------------------------------
142       // API Errors
143       // -------------------------------------------------------------------------
144    
145       /// Allocated buffer is too small
146       public static final int kRIOStatusBufferInvalidSize            = kRioStatusOffset - 80;
147    
148       /// Caller did not allocate a buffer
149       public static final int kRIOStatusBufferNotAllocated           = kRioStatusOffset - 81;
150    
151       /// The fifo is reservered in another session
152       public static final int kRIOStatusFifoReserved                 = kRioStatusOffset - 82;
153    
154       // -------------------------------------------------------------------------
155       // PAL Replacements
156       // -------------------------------------------------------------------------
157    
158       /// A hardware failure has occurred. The operation could not be completed as specified.
159       public static final int kRIOStatusHardwareFault                = kRioStatusOffset - 150;
160    
161       /// <internal>The resource was already initialized and cannot be initialized again. The operation could not be completed as specified.</internal>
162       public static final int kRIOStatusResourceInitialized          = kRioStatusOffset - 151;
163    
164       /// <internal>The requested resource was not found </internal>.
165       public static final int kRIOStatusResourceNotFound             = kRioStatusOffset - 152;
166    
167       // -------------------------------------------------------------------------
168       // Configuration
169       // -------------------------------------------------------------------------
170    
171       /// An invalid alias was specified. RIO aliases may only contain alphanumerics, '-', and '_'.
172       public static final int kRIOStatusInvalidAliasName             = kRioStatusOffset - 180;
173    
174       /// The supplied alias was not found.
175       public static final int kRIOStatusAliasNotFound                = kRioStatusOffset - 181;
176    
177       /// An invalid device access setting was specified. RIO device access patterns may only contain alphanumerics, '-', '_', '.', and '*'.
178       public static final int kRIOStatusInvalidDeviceAccess          = kRioStatusOffset - 182;
179    
180       /// An invalid port was specified. The RIO Server port must be between 0 and 65535, where 0 indicates a dynamically assigned port. Port 3580 is reserved and cannot be used.
181       public static final int kRIOStatusInvalidPort                  = kRioStatusOffset - 183;
182    
183       // -------------------------------------------------------------------------
184       // Misc.
185       // -------------------------------------------------------------------------
186    
187       /// This platform does not support connections to remote targets.
188       public static final int kRIOStatusRemoteTarget                 = kRioStatusOffset - 187;
189    
190       /// The operation is no longer supported
191       public static final int kRIOStatusDeprecatedFunction           = kRioStatusOffset - 188;
192    
193       /// The supplied search pattern isn't understood
194       public static final int kRIOStatusInvalidPattern               = kRioStatusOffset - 189;
195    
196       /// <internal>The specified device control code is not recognized.</internal>
197       public static final int kRIOStatusBadDeviceControlCode         = kRioStatusOffset - 190;
198    
199       /// The supplied resource name is not valid. Use MAX to find the proper resource name.
200       public static final int kRIOStatusInvalidResourceName          = kRioStatusOffset - 192;
201    
202       /// The requested feature is not supported in the current version of the driver software on either the host or the target.
203       public static final int kRIOStatusFeatureNotSupported          = kRioStatusOffset - 193;
204    
205       /// The version of the target software is incompatible. Upgrade target software to get full functionality. (For use by host interface).
206       public static final int kRIOStatusVersionMismatch              = kRioStatusOffset - 194;
207    
208       /// A handle for device communication is invalid. The device connection has been lost.
209       public static final int kRIOStatusInvalidHandle                = kRioStatusOffset - 195;
210    
211       /// An invalid attribute has been specified.
212       public static final int kRIOStatusInvalidAttribute             = kRioStatusOffset - 196;
213    
214       /// An invalid attribute value has been specified.
215       public static final int kRIOStatusInvalidAttributeValue        = kRioStatusOffset - 197;
216    
217       /// The system has run out of resource handles. Try closing some sessions.
218       public static final int kRIOStatusOutOfHandles                 = kRioStatusOffset - 198;
219    
220       /// <internal>The server does not recognize the command</internal>
221       public static final int kRIOStatusInvalidFunction              = kRioStatusOffset - 199;
222    
223       // -------------------------------------------------------------------------
224       // APAL translation
225       // #define nNIAPALS100_tStatus_kOffset    -52000
226       // -------------------------------------------------------------------------
227    
228       /// nNIAPALS100_tStatus_kMemoryFull. The specified number of bytes could not be allocated
229       public static final int kRIOStatusMemoryFull                   = -52000;
230    
231       /// nNIAPALS100_tStatus_kPageLockFailed. The memory could not be page locked
232       public static final int kRIOStatusPageLockFailed               = -52001;
233    
234       /// nNIAPALS100_tStatus_kSoftwareFault. An unexpected software error occurred.
235       public static final int kRIOStatusSoftwareFault                = -52003;
236    
237       /// nNIAPALS100_tStatus_kDynamicCastFailed. The dynamic cast operation failed.
238       public static final int kRIOStatusDynamicCastFailed            = -52004;
239    
240       /// nNIAPALS100_tStatus_kInvalidParameter. The parameter recieved by the function is not valid.
241       public static final int kRIOStatusInvalidParameter             = -52005;
242    
243       /// nNIAPALS100_tStatus_kOperationTimedOut. The requested operation did not complete in time.
244       public static final int kRIOStatusOperationTimedOut            = -52007;
245    
246       /// nNIAPALS100_tStatus_kOSFault. An unexpected operationg system error occurred.
247       public static final int kRIOStatusOSFault                      = -52008;
248    
249       /// nNIAPALS100_tStatus_kResourceMarkedForDelete. The requested resource has been marked for deletion and is rejecting new requests.
250       public static final int kRIOStatusMarkedForDelete              = -52009;
251    
252       /// nNIAPALS100_tStatus_kResourceNotInitialized. The requested resource must be initializaed before use.
253       public static final int kRIOStatusResourceNotInitialized       = -52010;
254    
255       /// nNIAPALS100_tStatus_kOperationPending. The operation has begun and will complete asynchronously.
256       public static final int kRIOStatusOperationPending             = -52011;
257    
258       /// nNIAPALS100_tStatus_kEndOfData. There is no more data available to read or no more space available in which to write.
259       public static final int kRIOStatusEndOfData                    = -52012;
260    
261       /// nNIAPALS100_tStatus_kObjectNameCollision. The name or handle requested has already been reserved by another client
262       public static final int kRIOStatusObjectNameCollision          = -52013;
263    
264       /// nNIAPALS100_tStatus_kSyncronizationObjectAbandoned. The synchronization object was abandoned by the previous holder
265       public static final int kRIOStatusSyncObjectAbandoned          = -52014;
266    
267       /// nNIAPALS100_tStatus_kSyncronizationAcquireFailed. The acquisition of the synchronization object failed
268       public static final int kRIOStatusSyncAcquireFailed            = -52015;
269    
270       /// nNIAPALS100_tStatus_kThreadAlreadyStarted. The thread was running when you attempted to start it (redundant state change).
271       public static final int kRIOStatusThreadAlreadyStarted         = -52016;
272    
273       /// nNIAPALS100_tStatus_kInvalidStateTransition. The operation you requested is redundant or invalid when the object is in this state.
274       public static final int kRIOStatusInvalidStateTransition       = -52017;
275    
276       // -------------------------------------------------------------------------
277       // Traditional PAL translation
278       // #define nNIAPALS100_tStatus_kOffset    -50000
279       // -------------------------------------------------------------------------
280       /// An attribute whether explicit or implicit is not relevant or is not relevant given the current system state. The operation could not be completed as specified.
281       public static final int kRIOStatusIrrelevantAttribute          = -50001;
282    
283       /// A selector - usually of an enumerated type - is inappropriate or out of range. The operation could not be completed as specified.
284       public static final int kRIOStatusBadSelector                  = -50003;
285    
286       /// The specified software component is not available. The component was not loaded.
287       public static final int kRIOStatusComponentNotFound            = -50251;
288    
289       /// The specified device is not present or is deactivated. The operation could not be completed as specified.
290       public static final int kRIOStatusDeviceNotFound               = -50300;
291    
292    
293       // -------------------------------------------------------------------------
294       // LabVIEW errors
295       // -------------------------------------------------------------------------
296    
297       /// Generic file I/O error.
298       public static final int kRIOStatusFileError                    = -6;
299    
300       //
301       // Helper functions
302       //
303    
304       public boolean isFatal()
305       {
306          return status.getValue() < 0;
307       }
308    
309       public boolean isNotFatal()
310       {
311          return status.getValue() >= 0;
312       }
313    
314       /**
315        * Update the status code to the most serious of the existing status code or the new status code. If the 
316        * resulting code is "fatal" throw NiRioStatus.FatalStatusException.
317        * 
318        * @param newStatusCode 
319        */
320       public void setStatus(int newStatusCode)
321       {
322          if (status.getValue() >= 0 && (status.getValue() == 0 || newStatusCode < 0))
323          {
324             status.setValue(newStatusCode);
325          }
326          assertNonfatal();
327       }
328    
329       public void setStatus(NiRioStatus newStatus)
330       {
331          setStatus(newStatus.getStatusCode());
332       }
333    
334       public int getStatusCode()
335       {
336          return status.getValue();
337       }
338    
339       public void assertNonfatal() throws IllegalStateException
340       {
341          if (isFatal())
342          {
343             throw new NiRioStatus.FatalStatusException(this, "Fatal status code detected:  " + Integer.toString(getStatusCode()));
344          }
345       }
346    }