usbHandleLib.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* usbHandleLib.c - handle utility functions */
  2. /* Copyright 2000 Wind River Systems, Inc. */
  3. /*
  4. Modification history
  5. --------------------
  6. 01b,07mar00,rcb  Add casts as necessary to reflect change of
  7.  GENERIC_HANDLE from UINT32 to pVOID.
  8. 01a,07jun99,rcb  First.
  9. */
  10. /*
  11. DESCRIPTION
  12. Implements a set of general-purpose handle creation and validation functions.
  13. Using these services, libraries can return handles to callers which can 
  14. subsequently be validated for authenticity.  This provides libraries with
  15. an additional measure of "bullet-proofing."
  16. */
  17. /* includes */
  18. #include "usb/usbPlatform.h"
  19. #include "usb/ossLib.h"
  20. #include "usb/usbHandleLib.h"     /* our API */
  21. /* defines */
  22. #define DEFAULT_HANDLES     1024    /* Default number of handles */
  23. /* INDEX() calculates the handle[] index based on a handle number */
  24. #define INDEX(h)    ((((UINT32) h) - 1) % numHandles)
  25. /* typedefs */
  26. /*
  27.  * HDL_DESCR - Describes each handle
  28.  */
  29. typedef struct hdl_descr
  30.     {
  31.     GENERIC_HANDLE handle;     /* Currently assigned handle */
  32.     UINT32 handleSig;     /* signature used to validate handle */
  33.     pVOID handleParam;     /* arbitrary parameter used by caller */
  34.     } HDL_DESCR, *pHDL_DESCR;
  35. /* locals */
  36. LOCAL int initCount = 0;     /* Initialization nesting count */
  37. LOCAL HDL_DESCR *handles = NULL;    /* Pointer to array of handles */
  38. LOCAL UINT32 numHandles = 0;     /* Number of handles allocated */
  39. LOCAL UINT32 usedHandles = 0;     /* Number of handles in use */
  40. LOCAL UINT32 nextHandleNum = 0;     /* Handle numbers always increment */
  41. LOCAL MUTEX_HANDLE mutex = NULL;    /* mutex for handle allocation */
  42. /* functions */
  43. /***************************************************************************
  44. *
  45. * usbHandleInitialize - Initializies the handle utility library
  46. *
  47. * Initializes the handle utility library.  Must be called at least once
  48. * prior to any other calls into the handle utility library.  Calls to 
  49. * usbHandleInitialize() may be nested, allowing multiple clients to use the
  50. * library without requiring that they be coordinated.
  51. *
  52. * <maxHandles> defines the maximum number of handles which should be
  53. * allocated by the library.  Passing a zero in <maxHandles> causes the 
  54. * library to allocate a default number of handles.  <maxHandles> is ignored 
  55. * on "nested" calls to usbHandleInitialize().  
  56. *
  57. * RETURNS: OK or ERROR
  58. *
  59. * ERRNO:
  60. *  S_usbHandleLib_OUT_OF_MEMORY
  61. *  S_usbHandleLib_OUT_OF_RESOURCES
  62. */
  63. STATUS usbHandleInitialize
  64.     (
  65.     UINT32 maxHandles     /* max handles allocated by library */
  66.     )
  67.     {
  68.     STATUS s = OK;
  69.     /* Check if we're already initialized */
  70.     if (++initCount > 1)
  71. return OK;
  72.     /* Create the handle control structures */
  73.     if (maxHandles == 0)
  74. maxHandles = DEFAULT_HANDLES;
  75.     if ((handles = OSS_CALLOC (sizeof (HDL_DESCR) * maxHandles)) == NULL)
  76. {
  77. s = ossStatus (S_usbHandleLib_OUT_OF_MEMORY);
  78. }
  79.     else
  80. {
  81. if (OSS_MUTEX_CREATE (&mutex) != OK)
  82.     {
  83.     s = ossStatus (S_usbHandleLib_OUT_OF_RESOURCES);
  84.     }
  85. else
  86.     {
  87.     /* Initialize handle array. */
  88.     numHandles = maxHandles;
  89.     usedHandles = 0;
  90.     nextHandleNum = 0;
  91.     return OK;
  92.     }
  93. OSS_FREE (handles);
  94. handles = NULL;
  95. }
  96.     /* Failed to initialize. */
  97.     --initCount;
  98.     return s;
  99.     }
  100. /***************************************************************************
  101. *
  102. * usbHandleShutdown - Shuts down the handle utility library
  103. *
  104. * Shuts down the handle utility library.  When calls to usbHandleInitialize()
  105. * have been nested,  usbHandleShutdown() must be called a corresponding number
  106. * of times.
  107. *
  108. * RETURNS: OK or ERROR
  109. */
  110. STATUS usbHandleShutdown (void)
  111.     {
  112.     if (initCount > 0)
  113. {
  114. if (--initCount == 0)
  115.     {
  116.     if (mutex != NULL)
  117. {
  118. OSS_MUTEX_DESTROY (mutex);
  119. mutex = NULL;
  120. }
  121.     if (handles != NULL)
  122. {
  123. OSS_FREE (handles);
  124. handles = NULL;
  125. numHandles = 0;
  126. }
  127.     }
  128. }
  129.     return OK;
  130.     }
  131. /***************************************************************************
  132. *
  133. * usbHandleCreate - Creates a new handle
  134. *
  135. * Creates a new handle.  The caller passes an arbitrary <handleSignature>
  136. * and a <handleParam>. The <handleSignature> will be used in subsequent
  137. * calls to usbHandleValidate().
  138. *
  139. * RETURNS: OK or ERROR
  140. *
  141. * ERRNO:
  142. *  S_usbHandleLib_NOT_INITIALIZED
  143. *  S_usbHandleLib_BAD_PARAM
  144. *  S_usbHandleLib_GENERAL_FAULT
  145. *  S_usbHandleLib_OUT_OF_HANDLES
  146. */
  147. STATUS usbHandleCreate
  148.     (
  149.     UINT32 handleSignature,     /* Arbitrary handle signature */
  150.     pVOID handleParam,     /* Arbitrary handle parameter */
  151.     pGENERIC_HANDLE pHandle     /* Newly allocated handle */
  152.     )
  153.     {
  154.     STATUS s = OK;
  155.     /* Are we initialized? */
  156.     if (initCount == 0)
  157. return ossStatus (S_usbHandleLib_NOT_INITIALIZED);
  158.     /* Check parameters */
  159.     if (pHandle == NULL)
  160. return ossStatus (S_usbHandleLib_BAD_PARAM);
  161.     /* Take the mutex */
  162.     if (OSS_MUTEX_TAKE (mutex, OSS_BLOCK) != OK)
  163. return ossStatus (S_usbHandleLib_GENERAL_FAULT);
  164.     /* Is there a free handle? */
  165.     if (usedHandles == numHandles) 
  166. {
  167. s = ossStatus (S_usbHandleLib_OUT_OF_HANDLES);
  168. }
  169.     else
  170. {
  171. /* Find a free handle and assign a handle number to it */
  172. *pHandle = NULL;
  173.     
  174. while (*pHandle == NULL)
  175.     {
  176.     /* We don't use handle number 0. (handle numbers are always 1
  177.     greater than the index into the handles[] array. */
  178.     if (nextHandleNum == 0)
  179. ++nextHandleNum;
  180.     if (handles [INDEX (nextHandleNum)].handle == 0)
  181. {
  182. /* We found a free handle */
  183. *pHandle = (GENERIC_HANDLE) nextHandleNum;
  184. handles [INDEX (nextHandleNum)].handle = (GENERIC_HANDLE) nextHandleNum;
  185. handles [INDEX (nextHandleNum)].handleSig = handleSignature;
  186. handles [INDEX (nextHandleNum)].handleParam = handleParam;
  187. }
  188.     /* Whether or not we used this handle, we increment nextHandleNum
  189.     so the next search will always begin after where we left off. */
  190.     if (++nextHandleNum > numHandles) 
  191. nextHandleNum = 0;
  192.     }
  193. }
  194.     /* Release the mutex and return */
  195.     OSS_MUTEX_RELEASE (mutex);
  196.     return s;
  197.     }
  198. /***************************************************************************
  199. *
  200. * usbHandleDestroy - Destroys a handle
  201. *
  202. * This functions destroys the <handle> created by calling usbHandleCreate().
  203. *
  204. * RETURNS: OK or ERROR
  205. *
  206. * ERRNO:
  207. *  S_usbHandleLib_GENERAL_FAULT
  208. *  S_usbHandleLib_BAD_HANDLE
  209. */
  210. STATUS usbHandleDestroy
  211.     (
  212.     GENERIC_HANDLE handle     /* handle to be destroyed */
  213.     )
  214.     {
  215.     STATUS s = OK;
  216.     
  217.     /* Take the mutex */
  218.     if (OSS_MUTEX_TAKE (mutex, OSS_BLOCK) != OK)
  219. return ossStatus (S_usbHandleLib_GENERAL_FAULT);
  220.     /* Is this a valid handle? */
  221.     if (handles [INDEX (handle)].handle != handle)
  222. {
  223. s = ossStatus (S_usbHandleLib_BAD_HANDLE);
  224. }
  225.     else
  226. {
  227. /* Release the indicated handle descriptor */
  228. handles [INDEX (handle)].handle = 0;
  229. handles [INDEX (handle)].handleSig = 0;
  230. handles [INDEX (handle)].handleParam = 0;   
  231. }
  232.     /* Release the mutext and return */
  233.     OSS_MUTEX_RELEASE (mutex);
  234.     return s;
  235.     }
  236. /***************************************************************************
  237. *
  238. * usbHandleValidate - Validates a handle
  239. *
  240. * This function validates <handle>.  The <handle> must match the
  241. * <handleSignature> used when the handle was originally created.  If the
  242. * handle is valid, the <pHandleParam> will be returned.
  243. *
  244. * RETURNS: OK or ERROR
  245. *
  246. * ERRNO:
  247. *  S_usbHandleLib_NOT_INITIALIZED
  248. *  S_usbHandleLib_BAD_HANDLE
  249. */
  250. STATUS usbHandleValidate
  251.     (
  252.     GENERIC_HANDLE handle,     /* handle to be validated */
  253.     UINT32 handleSignature,     /* signature used to validate handle */
  254.     pVOID *pHandleParam      /* Handle parameter on return */
  255.     )
  256.     {
  257.     
  258.     /* Are we initialized? */
  259.     if (initCount == 0)
  260. return ossStatus (S_usbHandleLib_NOT_INITIALIZED);
  261.     /* Is the indicated handle valid? */
  262.     if (handles [INDEX (handle)].handle != handle ||
  263. handles [INDEX (handle)].handleSig != handleSignature)
  264. return ossStatus (S_usbHandleLib_BAD_HANDLE);
  265.     /* Handle is valid.  Return param associated with handle */
  266.     if (pHandleParam != NULL)
  267. *pHandleParam = handles [INDEX (handle)].handleParam;
  268.     return OK;
  269.     }
  270. /* End of file. */