ibOnl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. lib/ibOnl.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 <stdlib.h>
  17. int board_online( ibBoard_t *board, int online )
  18. {
  19. if( online )
  20. {
  21. if( ibBoardOpen( board ) < 0 )
  22. return -1;
  23. }else
  24. {
  25. ibBoardClose( board );
  26. }
  27. return 0;
  28. }
  29. int conf_online( ibConf_t *conf, int online )
  30. {
  31. ibBoard_t *board;
  32. int retval;
  33. if( ( online && conf->board_is_open ) ||
  34. ( online == 0 && conf->board_is_open == 0 ) )
  35. return 0;
  36. board = interfaceBoard( conf );
  37. retval = board_online( board, online );
  38. if( retval < 0 ) return retval;
  39. if( retval < 0 ) return retval;
  40. if( online )
  41. {
  42. retval = open_gpib_handle( conf );
  43. }else
  44. {
  45. retval = close_gpib_handle( conf );
  46. }
  47. if( retval < 0 ) return retval;
  48. conf->board_is_open = online != 0;
  49. return 0;
  50. }
  51. int reinit_descriptor( ibConf_t *conf )
  52. {
  53. int retval;
  54. retval = internal_ibpad( conf, conf->defaults.pad );
  55. if( retval < 0 ) return retval;
  56. retval = internal_ibsad( conf, conf->defaults.sad );
  57. if( retval < 0 ) return retval;
  58. retval = my_ibbna( conf, conf->defaults.board );
  59. if( retval < 0 ) return retval;
  60. conf->settings.usec_timeout = conf->defaults.usec_timeout;
  61. conf->settings.spoll_usec_timeout = conf->defaults.usec_timeout;
  62. conf->settings.ppoll_usec_timeout = conf->defaults.usec_timeout;
  63. conf->settings.eos = conf->defaults.eos;
  64. conf->settings.eos_flags = conf->defaults.eos_flags;
  65. conf->settings.eos = conf->defaults.eos;
  66. conf->settings.ppoll_config = conf->defaults.ppoll_config;
  67. internal_ibeot( conf, conf->defaults.send_eoi );
  68. conf->settings.local_lockout = conf->defaults.local_lockout;
  69. conf->settings.local_ppc = conf->defaults.local_ppc;
  70. conf->settings.readdr = conf->defaults.readdr;
  71. return 0;
  72. }
  73. int ibonl( int ud, int onl )
  74. {
  75. ibConf_t *conf;
  76. int retval;
  77. ibBoard_t *board;
  78. int status;
  79. conf = general_enter_library( ud, 1, 1 );
  80. if( conf == NULL )
  81. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  82. retval = internal_ibstop( conf );
  83. if( retval < 0 )
  84. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  85. retval = conf_lock_board( conf );
  86. if( retval < 0 )
  87. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  88. if( onl )
  89. {
  90. retval = reinit_descriptor( conf );
  91. if( retval < 0 ) return exit_library( ud, 1 );
  92. else return exit_library( ud, 0 );
  93. }
  94. board = interfaceBoard( conf );
  95. status = general_exit_library( ud, 0, 0, 0, 0, CMPL, 1 );
  96. if( onl == 0 )
  97. retval = close_gpib_handle( conf );
  98. else
  99. retval = 0;
  100. conf_unlock_board( conf );
  101. if( retval < 0 )
  102. {
  103. fprintf( stderr, "libgpib: failed to mark device as closed!\n" );
  104. setIberr( EDVR );
  105. setIbcnt( errno );
  106. status |= ERR;
  107. setIbsta( status );
  108. sync_globals();
  109. return status;
  110. }
  111. if( ud >= GPIB_MAX_NUM_BOARDS )
  112. {
  113. // need to take more care to clean up before freeing XXX
  114. free( ibConfigs[ ud ] );
  115. ibConfigs[ ud ] = NULL;
  116. }
  117. return status;
  118. }