ibFindLstn.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /***************************************************************************
  2. lib/ibFindLstn.c
  3. -------------------
  4. copyright : (C) 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 <unistd.h>
  17. #include <stdlib.h>
  18. int listenerFound( ibConf_t *conf, const Addr4882_t addressList[] )
  19. {
  20. uint8_t *cmd;
  21. int i, j;
  22. short line_status;
  23. int retval;
  24. if( addressList == NULL )
  25. return 0;
  26. if( addressListIsValid( addressList ) == 0 )
  27. return -1;
  28. cmd = malloc( 16 + 2 * numAddresses( addressList ) );
  29. if( cmd == NULL )
  30. {
  31. setIberr( EDVR );
  32. setIbcnt( ENOMEM );
  33. return -1;
  34. }
  35. j = 0;
  36. cmd[ j++ ] = UNL;
  37. for( i = 0; i < numAddresses( addressList ); i++ )
  38. {
  39. int pad, sad;
  40. pad = extractPAD( addressList[ i ] );
  41. sad = extractSAD( addressList[ i ] );
  42. cmd[ j++ ] = MLA( pad );
  43. if( sad >= 0 )
  44. cmd[ j++ ] = MSA( sad );
  45. }
  46. retval = my_ibcmd( conf, cmd, j );
  47. free( cmd );
  48. cmd = NULL;
  49. if( retval < 0 ) return retval;
  50. retval = internal_ibgts( conf, 0 );
  51. if( retval < 0 ) return -1;
  52. usleep( 1500 );
  53. retval = internal_iblines( conf, &line_status );
  54. if( retval < 0 ) return retval;
  55. if( ( line_status & ValidNDAC ) &&
  56. ( line_status & BusNDAC ) )
  57. {
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. int secondaryListenerFound( ibConf_t *conf, unsigned int pad )
  63. {
  64. Addr4882_t testAddress[ 32 ];
  65. int j;
  66. for( j = 0; j <= gpib_addr_max; j++ )
  67. testAddress[ j ] = packAddress( pad, j );
  68. testAddress[ j ] = NOADDR;
  69. return listenerFound( conf, testAddress );
  70. }
  71. void FindLstn( int boardID, const Addr4882_t padList[],
  72. Addr4882_t resultList[], int maxNumResults )
  73. {
  74. int i;
  75. ibConf_t *conf;
  76. ibBoard_t *board;
  77. int retval;
  78. int resultIndex;
  79. short line_status;
  80. conf = enter_library( boardID );
  81. if( conf == NULL )
  82. {
  83. exit_library( boardID, 1 );
  84. return;
  85. }
  86. if( conf->is_interface == 0 )
  87. {
  88. setIberr( EDVR );
  89. exit_library( boardID, 1 );
  90. return;
  91. }
  92. board = interfaceBoard( conf );
  93. retval = internal_iblines( conf, &line_status );
  94. if( retval < 0 )
  95. {
  96. exit_library( boardID, 1 );
  97. return;
  98. }
  99. if( ( line_status & ValidNDAC ) == 0 )
  100. {
  101. setIberr( ECAP );
  102. exit_library( boardID, 1 );
  103. return;
  104. }
  105. resultIndex = 0;
  106. for( i = 0; i < numAddresses( padList ); i++ )
  107. {
  108. Addr4882_t pad;
  109. Addr4882_t testAddress[ 2 ];
  110. pad = GetPAD( padList[ i ] );
  111. testAddress[ 0 ] = pad;
  112. testAddress[ 1 ] = NOADDR;
  113. retval = listenerFound( conf, testAddress );
  114. if( retval < 0 )
  115. {
  116. // XXX status/error settings
  117. exit_library( boardID, 1 );
  118. return;
  119. }
  120. if( retval > 0 )
  121. {
  122. if( resultIndex >= maxNumResults )
  123. {
  124. setIberr( ETAB );
  125. exit_library( boardID, 1 );
  126. return;
  127. }
  128. resultList[ resultIndex++ ] = testAddress[ 0 ];
  129. setIbcnt( resultIndex );
  130. }else
  131. {
  132. retval = secondaryListenerFound( conf, pad );
  133. if( retval < 0 )
  134. {
  135. exit_library( boardID, 1 );
  136. return;
  137. }
  138. if( retval > 0 )
  139. {
  140. int j;
  141. for( j = 0; j <= gpib_addr_max; j++ )
  142. {
  143. testAddress[ 0 ] = packAddress( pad, j );
  144. testAddress[ 1 ] = NOADDR;
  145. retval = listenerFound( conf, testAddress );
  146. if( retval < 0 )
  147. {
  148. exit_library( boardID, 1 );
  149. return;
  150. }
  151. if( retval > 1 )
  152. {
  153. if( resultIndex >= maxNumResults )
  154. {
  155. setIberr( ETAB );
  156. exit_library( boardID, 1 );
  157. return;
  158. }
  159. resultList[ resultIndex++ ] = testAddress[ 0 ];
  160. setIbcnt( resultIndex );
  161. }
  162. }
  163. }
  164. }
  165. }
  166. exit_library( boardID, 0 );
  167. } /* FindLstn */
  168. int ibln( int ud, int pad, int sad, short *found_listener )
  169. {
  170. ibConf_t *conf;
  171. Addr4882_t addressList[ 2 ];
  172. int retval;
  173. conf = enter_library( ud );
  174. if( conf == NULL )
  175. return exit_library( ud, 1 );
  176. switch( sad )
  177. {
  178. case ALL_SAD:
  179. retval = secondaryListenerFound( conf, pad );
  180. break;
  181. case NO_SAD:
  182. default:
  183. addressList[ 0 ] = MakeAddr( pad, sad );
  184. addressList[ 1 ] = NOADDR;
  185. retval = listenerFound( conf, addressList );
  186. break;
  187. }
  188. if( retval < 0 ) return exit_library( ud, 1 );
  189. *found_listener = retval;
  190. return exit_library( ud, 0 );
  191. }