ibCmd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /***************************************************************************
  2. lib/ibCmd.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 <assert.h>
  17. #include <stdint.h>
  18. #include <sys/ioctl.h>
  19. #include <pthread.h>
  20. #include <stdlib.h>
  21. int ibcmd(int ud, const void *cmd_buffer, long cnt)
  22. {
  23. ibConf_t *conf;
  24. ssize_t count;
  25. conf = enter_library( ud );
  26. if( conf == NULL )
  27. return exit_library( ud, 1 );
  28. // check that ud is an interface board
  29. if( conf->is_interface == 0 )
  30. {
  31. setIberr( EARG );
  32. return exit_library( ud, 1 );
  33. }
  34. count = my_ibcmd( conf, cmd_buffer, cnt);
  35. if(count < 0)
  36. {
  37. // report no listeners error XXX
  38. return exit_library( ud, 1);
  39. }
  40. if(count != cnt)
  41. {
  42. return exit_library( ud, 1 );
  43. }
  44. return exit_library( ud, 0 );
  45. }
  46. int ibcmda( int ud, const void *cmd_buffer, long cnt )
  47. {
  48. ibConf_t *conf;
  49. ibBoard_t *board;
  50. int retval;
  51. conf = general_enter_library( ud, 1, 0 );
  52. if( conf == NULL )
  53. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  54. // check that ud is an interface board
  55. if( conf->is_interface == 0 )
  56. {
  57. setIberr( EARG );
  58. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  59. }
  60. board = interfaceBoard( conf );
  61. if( is_cic( board ) == 0 )
  62. {
  63. setIberr( ECIC );
  64. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  65. }
  66. retval = gpib_aio_launch( ud, conf, GPIB_AIO_COMMAND,
  67. (void*)cmd_buffer, cnt );
  68. if( retval < 0 )
  69. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  70. return general_exit_library( ud, 0, 0, 0, 0, 0, 1 );
  71. }
  72. ssize_t my_ibcmd( ibConf_t *conf, const uint8_t *buffer, size_t count)
  73. {
  74. read_write_ioctl_t cmd;
  75. int retval;
  76. ibBoard_t *board;
  77. board = interfaceBoard( conf );
  78. if( is_cic( board ) == 0 )
  79. {
  80. setIberr( ECIC );
  81. return -1;
  82. }
  83. assert(sizeof(buffer) <= sizeof(cmd.buffer_ptr));
  84. cmd.buffer_ptr = (uintptr_t)buffer;
  85. cmd.count = count;
  86. cmd.handle = conf->handle;
  87. cmd.end = 0;
  88. set_timeout( board, conf->settings.usec_timeout);
  89. retval = ioctl( board->fileno, IBCMD, &cmd );
  90. if( retval < 0 )
  91. {
  92. switch( errno )
  93. {
  94. case ETIMEDOUT:
  95. setIberr( EBUS );
  96. conf->timed_out = 1;
  97. break;
  98. default:
  99. setIberr( EDVR );
  100. setIbcnt( errno );
  101. break;
  102. }
  103. return -1;
  104. }
  105. return cmd.count;
  106. }
  107. unsigned int create_send_setup( const ibBoard_t *board,
  108. const Addr4882_t addressList[], uint8_t *cmdString )
  109. {
  110. unsigned int i, j;
  111. unsigned int board_pad;
  112. int board_sad;
  113. if( addressList == NULL )
  114. {
  115. fprintf(stderr, "libgpib: bug! addressList NULL in create_send_setup()\n");
  116. return 0;
  117. }
  118. if( addressListIsValid( addressList ) == 0 )
  119. {
  120. fprintf(stderr, "libgpib: bug! bad address list\n");
  121. return 0;
  122. }
  123. i = 0;
  124. /* controller's talk address */
  125. if(query_pad(board, &board_pad) < 0) return 0;
  126. cmdString[i++] = MTA(board_pad);
  127. if(query_sad(board, &board_sad) < 0) return 0;
  128. if(board_sad >= 0 )
  129. cmdString[i++] = MSA(board_sad);
  130. cmdString[ i++ ] = UNL;
  131. for( j = 0; j < numAddresses( addressList ); j++ )
  132. {
  133. unsigned int pad;
  134. int sad;
  135. pad = extractPAD( addressList[ j ] );
  136. sad = extractSAD( addressList[ j ] );
  137. cmdString[ i++ ] = MLA( pad );
  138. if( sad >= 0)
  139. cmdString[ i++ ] = MSA( sad );
  140. }
  141. return i;
  142. }
  143. unsigned int send_setup_string( const ibConf_t *conf,
  144. uint8_t *cmdString )
  145. {
  146. ibBoard_t *board;
  147. Addr4882_t addressList[ 2 ];
  148. board = interfaceBoard( conf );
  149. addressList[ 0 ] = packAddress( conf->settings.pad, conf->settings.sad );
  150. addressList[ 1 ] = NOADDR;
  151. return create_send_setup( board, addressList, cmdString );
  152. }
  153. int send_setup( ibConf_t *conf )
  154. {
  155. uint8_t cmdString[8];
  156. int retval;
  157. retval = send_setup_string( conf, cmdString );
  158. if( my_ibcmd( conf, cmdString, retval ) < 0 )
  159. return -1;
  160. return 0;
  161. }
  162. int InternalSendSetup( ibConf_t *conf, const Addr4882_t addressList[] )
  163. {
  164. int i;
  165. ibBoard_t *board;
  166. uint8_t *cmd;
  167. int count;
  168. if( addressListIsValid( addressList ) == 0 ||
  169. numAddresses( addressList ) == 0 )
  170. {
  171. setIberr( EARG );
  172. return -1;
  173. }
  174. if( conf->is_interface == 0 )
  175. {
  176. setIberr( EDVR );
  177. return -1;
  178. }
  179. board = interfaceBoard( conf );
  180. if( is_cic( board ) == 0 )
  181. {
  182. setIberr( ECIC );
  183. return -1;
  184. }
  185. cmd = malloc( 16 + 2 * numAddresses( addressList ) );
  186. if( cmd == NULL )
  187. {
  188. setIberr( EDVR );
  189. setIbcnt( ENOMEM );
  190. return -1;
  191. }
  192. i = create_send_setup( board, addressList, cmd );
  193. //XXX detect no listeners (EBUS) error
  194. count = my_ibcmd( conf, cmd, i );
  195. free( cmd );
  196. cmd = NULL;
  197. if(count != i)
  198. {
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. void SendSetup( int boardID, const Addr4882_t addressList[] )
  204. {
  205. int retval;
  206. ibConf_t *conf;
  207. conf = enter_library( boardID );
  208. if( conf == NULL )
  209. {
  210. exit_library( boardID, 1 );
  211. return;
  212. }
  213. retval = InternalSendSetup( conf, addressList );
  214. if( retval < 0 )
  215. {
  216. exit_library( boardID, 1 );
  217. return;
  218. }
  219. exit_library( boardID, 0 );
  220. }
  221. void SendCmds( int boardID, const void *buffer, long count )
  222. {
  223. ibcmd( boardID, buffer, count );
  224. }