ibWrt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /***************************************************************************
  2. lib/ibWrt.c
  3. -------------------
  4. copyright : (C) 2001,2002 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 <assert.h>
  16. #include "ib_internal.h"
  17. #include <stdint.h>
  18. #include <sys/ioctl.h>
  19. #include <stdlib.h>
  20. #include <pthread.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. int find_eos( const uint8_t *buffer, size_t length, int eos, int eos_flags )
  25. {
  26. unsigned int i;
  27. unsigned int compare_mask;
  28. if( eos_flags & BIN ) compare_mask = 0xff;
  29. else compare_mask = 0x7f;
  30. for( i = 0; i < length; i++ )
  31. {
  32. if( ( buffer[i] & compare_mask ) == ( eos & compare_mask ) )
  33. return i;
  34. }
  35. return -1;
  36. }
  37. int send_data(ibConf_t *conf, const void *buffer, size_t count, int send_eoi, size_t *bytes_written)
  38. {
  39. ibBoard_t *board;
  40. read_write_ioctl_t write_cmd;
  41. int retval;
  42. board = interfaceBoard( conf );
  43. set_timeout( board, conf->settings.usec_timeout );
  44. assert(sizeof(buffer) <= sizeof(write_cmd.buffer_ptr));
  45. write_cmd.buffer_ptr = (uintptr_t)buffer;
  46. write_cmd.count = count;
  47. write_cmd.end = send_eoi;
  48. write_cmd.handle = conf->handle;
  49. retval = ioctl( board->fileno, IBWRT, &write_cmd);
  50. if(retval < 0)
  51. {
  52. switch( errno )
  53. {
  54. case ETIMEDOUT:
  55. conf->timed_out = 1;
  56. setIberr( EABO );
  57. break;
  58. case EINTR:
  59. setIberr( EABO );
  60. break;
  61. case EIO:
  62. setIberr( ENOL );
  63. break;
  64. case EFAULT:
  65. write_cmd.count = 0;
  66. //fall-through
  67. default:
  68. setIberr( EDVR );
  69. setIbcnt( errno );
  70. break;
  71. }
  72. }
  73. *bytes_written = write_cmd.count;
  74. conf->end = send_eoi && (*bytes_written == count);
  75. if(retval < 0) return retval;
  76. return 0;
  77. }
  78. int send_data_smart_eoi(ibConf_t *conf, const void *buffer, size_t count,
  79. int force_eoi, size_t *bytes_written)
  80. {
  81. int eoi_on_eos;
  82. int eos_found = 0;
  83. int send_eoi;
  84. unsigned long block_size;
  85. int retval;
  86. eoi_on_eos = conf->settings.eos_flags & XEOS;
  87. block_size = count;
  88. if( eoi_on_eos )
  89. {
  90. retval = find_eos( buffer, count, conf->settings.eos, conf->settings.eos_flags );
  91. if( retval < 0 ) eos_found = 0;
  92. else
  93. {
  94. block_size = retval;
  95. eos_found = 1;
  96. }
  97. }
  98. send_eoi = force_eoi || ( eoi_on_eos && eos_found );
  99. if(send_data(conf, buffer, block_size, send_eoi, bytes_written) < 0)
  100. {
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. int my_ibwrt( ibConf_t *conf,
  106. const uint8_t *buffer, size_t count, size_t *bytes_written)
  107. {
  108. ibBoard_t *board;
  109. size_t block_size;
  110. int retval;
  111. *bytes_written = 0;
  112. board = interfaceBoard( conf );
  113. set_timeout( board, conf->settings.usec_timeout );
  114. if( conf->is_interface == 0 )
  115. {
  116. // set up addressing
  117. if( send_setup( conf ) < 0 )
  118. {
  119. return -1;
  120. }
  121. }
  122. while( count )
  123. {
  124. retval = send_data_smart_eoi( conf, buffer, count, conf->settings.send_eoi, &block_size);
  125. *bytes_written += block_size;
  126. if(retval < 0)
  127. {
  128. return -1;
  129. }
  130. count -= block_size;
  131. buffer += block_size;
  132. }
  133. return 0;
  134. }
  135. int ibwrt( int ud, const void *rd, long cnt )
  136. {
  137. ibConf_t *conf;
  138. size_t count;
  139. int retval;
  140. conf = enter_library( ud );
  141. if( conf == NULL )
  142. return exit_library( ud, 1 );
  143. conf->end = 0;
  144. retval = my_ibwrt(conf, rd, cnt, &count);
  145. if(retval < 0)
  146. {
  147. if(ThreadIberr() != EDVR) setIbcnt(count);
  148. return exit_library( ud, 1 );
  149. }
  150. setIbcnt(count);
  151. return general_exit_library( ud, 0, 0, 0, DCAS, 0, 0 );
  152. }
  153. int ibwrta( int ud, const void *buffer, long cnt )
  154. {
  155. ibConf_t *conf;
  156. int retval;
  157. conf = general_enter_library( ud, 1, 0 );
  158. if( conf == NULL )
  159. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  160. retval = gpib_aio_launch( ud, conf, GPIB_AIO_WRITE,
  161. (void*)buffer, cnt );
  162. if( retval < 0 )
  163. return general_exit_library( ud, 1, 0, 0, 0, 0, 1 );
  164. return general_exit_library( ud, 0, 0, 0, 0, 0, 1 );
  165. }
  166. int my_ibwrtf( ibConf_t *conf, const char *file_path, size_t *bytes_written)
  167. {
  168. ibBoard_t *board;
  169. long count;
  170. size_t block_size;
  171. int retval;
  172. FILE *data_file;
  173. struct stat file_stats;
  174. uint8_t buffer[ 0x4000 ];
  175. *bytes_written = 0;
  176. board = interfaceBoard( conf );
  177. data_file = fopen( file_path, "r" );
  178. if( data_file == NULL )
  179. {
  180. setIberr( EFSO );
  181. setIbcnt( errno );
  182. return -1;
  183. }
  184. retval = fstat( fileno( data_file ), &file_stats );
  185. if( retval < 0 )
  186. {
  187. setIberr( EFSO );
  188. setIbcnt( errno );
  189. return -1;
  190. }
  191. count = file_stats.st_size;
  192. if( conf->is_interface == 0 )
  193. {
  194. // set up addressing
  195. if( send_setup( conf ) < 0 )
  196. {
  197. return -1;
  198. }
  199. }
  200. set_timeout( board, conf->settings.usec_timeout );
  201. while( count )
  202. {
  203. size_t fread_count;
  204. int send_eoi;
  205. size_t buffer_offset = 0;
  206. fread_count = fread( buffer, 1, sizeof( buffer ), data_file );
  207. if( fread_count == 0 )
  208. {
  209. setIberr( EFSO );
  210. setIbcnt( errno );
  211. return -1;
  212. }
  213. while(buffer_offset < fread_count)
  214. {
  215. send_eoi = conf->settings.send_eoi && (count == fread_count - buffer_offset);
  216. retval = send_data_smart_eoi(conf, buffer + buffer_offset,
  217. fread_count - buffer_offset, send_eoi, &block_size);
  218. count -= block_size;
  219. buffer_offset += block_size;
  220. *bytes_written += block_size;
  221. if(retval < 0)
  222. {
  223. return -1;
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. int ibwrtf( int ud, const char *file_path )
  230. {
  231. ibConf_t *conf;
  232. size_t count;
  233. int retval;
  234. conf = enter_library( ud );
  235. if( conf == NULL )
  236. return exit_library( ud, 1 );
  237. conf->end = 0;
  238. retval = my_ibwrtf(conf, file_path, &count);
  239. if(retval < 0)
  240. {
  241. if(ThreadIberr() != EDVR) setIbcnt(count);
  242. return exit_library( ud, 1 );
  243. }
  244. setIbcnt( count );
  245. return general_exit_library( ud, 0, 0, 0, DCAS, 0, 0 );
  246. }
  247. int InternalSendDataBytes( ibConf_t *conf, const void *buffer,
  248. size_t count, int eotmode)
  249. {
  250. int retval;
  251. size_t num_bytes;
  252. size_t bytes_written = 0;
  253. if( conf->is_interface == 0 )
  254. {
  255. setIberr(EARG);
  256. return -1;
  257. }
  258. switch( eotmode )
  259. {
  260. case DABend:
  261. case NLend:
  262. case NULLend:
  263. break;
  264. default:
  265. setIberr( EARG );
  266. return -1;
  267. break;
  268. }
  269. retval = send_data( conf, buffer, count, eotmode == DABend, &num_bytes);
  270. bytes_written += num_bytes;
  271. if( retval < 0 )
  272. {
  273. setIbcnt(bytes_written);
  274. return retval;
  275. }
  276. if( eotmode == NLend )
  277. {
  278. retval = send_data( conf, "\n", 1, 1, &num_bytes);
  279. bytes_written += num_bytes;
  280. if( retval < 0 )
  281. {
  282. setIbcnt(bytes_written);
  283. return retval;
  284. }
  285. }
  286. setIbcnt(bytes_written);
  287. return 0;
  288. }
  289. void SendDataBytes( int boardID, const void *buffer,
  290. long count, int eotmode )
  291. {
  292. ibConf_t *conf;
  293. int retval;
  294. conf = enter_library( boardID );
  295. if( conf == NULL )
  296. {
  297. exit_library( boardID, 1 );
  298. return;
  299. }
  300. retval = InternalSendDataBytes( conf, buffer, count, eotmode );
  301. if( retval < 0 )
  302. {
  303. exit_library( boardID, 1 );
  304. return;
  305. }
  306. general_exit_library( boardID, 0, 0, 0, DCAS, 0, 0 );
  307. }
  308. int InternalSendList( ibConf_t *conf, const Addr4882_t addressList[],
  309. const void *buffer, long count, int eotmode )
  310. {
  311. ibBoard_t *board;
  312. int retval;
  313. if( addressListIsValid( addressList ) == 0 ||
  314. numAddresses( addressList ) == 0 )
  315. {
  316. setIberr(EARG);
  317. return -1;
  318. }
  319. if( conf->is_interface == 0 )
  320. {
  321. setIberr(EARG);
  322. return -1;
  323. }
  324. board = interfaceBoard( conf );
  325. if( is_cic( board ) == 0 )
  326. {
  327. setIberr( ECIC );
  328. return -1;
  329. }
  330. retval = InternalSendSetup( conf, addressList );
  331. if( retval < 0 ) return retval;
  332. retval = InternalSendDataBytes( conf, buffer, count, eotmode );
  333. if( retval < 0 ) return retval;
  334. return 0;
  335. }
  336. void SendList( int boardID, const Addr4882_t addressList[],
  337. const void *buffer, long count, int eotmode )
  338. {
  339. ibConf_t *conf;
  340. int retval;
  341. conf = enter_library( boardID );
  342. if( conf == NULL )
  343. {
  344. exit_library( boardID, 1 );
  345. return;
  346. }
  347. retval = InternalSendList( conf, addressList, buffer, count, eotmode );
  348. if( retval < 0 )
  349. {
  350. exit_library( boardID, 1 );
  351. return;
  352. }
  353. general_exit_library( boardID, 0, 0, 0, DCAS, 0, 0 );
  354. }
  355. void Send( int boardID, Addr4882_t address, const void *buffer, long count,
  356. int eotmode )
  357. {
  358. Addr4882_t addressList[ 2 ];
  359. addressList[ 0 ] = address;
  360. addressList[ 1 ] = NOADDR;
  361. SendList( boardID, addressList, buffer, count, eotmode );
  362. }