ibTmo.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /***************************************************************************
  2. lib/ibTmo.c
  3. -------------------
  4. copyright : (C) 2001,2002,2003 by Frank Mori Hess
  5. email : fmhess@users.sourceforge.net
  6. ***************************************************************************/
  7. /***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "ib_internal.h"
  16. #include <sys/ioctl.h>
  17. static const int default_ppoll_usec_timeout = 2;
  18. unsigned int timeout_to_usec( enum gpib_timeout timeout )
  19. {
  20. switch ( timeout )
  21. {
  22. default:
  23. case TNONE:
  24. return 0;
  25. break;
  26. case T10us:
  27. return 10;
  28. break;
  29. case T30us:
  30. return 30;
  31. break;
  32. case T100us:
  33. return 100;
  34. break;
  35. case T300us:
  36. return 300;
  37. break;
  38. case T1ms:
  39. return 1000;
  40. break;
  41. case T3ms:
  42. return 3000;
  43. break;
  44. case T10ms:
  45. return 10000;
  46. break;
  47. case T30ms:
  48. return 30000;
  49. break;
  50. case T100ms:
  51. return 100000;
  52. break;
  53. case T300ms:
  54. return 300000;
  55. break;
  56. case T1s:
  57. return 1000000;
  58. break;
  59. case T3s:
  60. return 3000000;
  61. break;
  62. case T10s:
  63. return 10000000;
  64. break;
  65. case T30s:
  66. return 30000000;
  67. break;
  68. case T100s:
  69. return 100000000;
  70. break;
  71. case T300s:
  72. return 300000000;
  73. break;
  74. case T1000s:
  75. return 1000000000;
  76. break;
  77. }
  78. return 0;
  79. }
  80. unsigned int ppoll_timeout_to_usec( unsigned int timeout )
  81. {
  82. if( timeout == 0 )
  83. return default_ppoll_usec_timeout;
  84. else
  85. return timeout_to_usec(timeout);
  86. }
  87. unsigned int usec_to_timeout( unsigned int usec )
  88. {
  89. if( usec == 0 ) return TNONE;
  90. else if( usec <= 10 ) return T10us;
  91. else if( usec <= 30 ) return T30us;
  92. else if( usec <= 100 ) return T100us;
  93. else if( usec <= 300 ) return T300us;
  94. else if( usec <= 1000 ) return T1ms;
  95. else if( usec <= 3000 ) return T3ms;
  96. else if( usec <= 10000 ) return T10ms;
  97. else if( usec <= 30000 ) return T30ms;
  98. else if( usec <= 100000 ) return T100ms;
  99. else if( usec <= 300000 ) return T300ms;
  100. else if( usec <= 1000000 ) return T1s;
  101. else if( usec <= 3000000 ) return T3s;
  102. else if( usec <= 10000000 ) return T10s;
  103. else if( usec <= 30000000 ) return T30s;
  104. else if( usec <= 100000000 ) return T100s;
  105. else if( usec <= 300000000 ) return T300s;
  106. else if( usec <= 1000000000 ) return T1000s;
  107. return TNONE;
  108. }
  109. unsigned int usec_to_ppoll_timeout( unsigned int usec )
  110. {
  111. if( usec <= default_ppoll_usec_timeout ) return 0;
  112. else return usec_to_timeout( usec );
  113. }
  114. int internal_ibtmo( ibConf_t *conf, int timeout )
  115. {
  116. if( timeout < TNONE || timeout > T1000s )
  117. {
  118. setIberr( EARG );
  119. return -1;
  120. }
  121. conf->settings.usec_timeout = timeout_to_usec( timeout );
  122. return 0;
  123. }
  124. int ibtmo(int ud, int v )
  125. {
  126. ibConf_t *conf;
  127. int retval;
  128. conf = general_enter_library( ud, 1, 0 );
  129. if( conf == NULL )
  130. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  131. retval = internal_ibtmo( conf, v );
  132. if( retval < 0 )
  133. {
  134. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  135. }
  136. return general_exit_library( ud, 0, 0, 0, 0, 0, 1 );
  137. }
  138. int set_timeout( const ibBoard_t *board, unsigned int usec_timeout)
  139. {
  140. return ioctl( board->fileno, IBTMO, &usec_timeout);
  141. }