gpib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* -*- linux-c -*-
  2. A PHP extension for GPIB commands
  3. written by
  4. Michel Billaud, billaud@labri.fr,
  5. Laboratoire Bordelais de Recherche en Informatique
  6. Université Bordeaux 1 - France
  7. billaud@labri.fr
  8. for
  9. the Minerva "Emerge" project, in February 2003
  10. (C) Michel Billaud, 2003
  11. ***************************************************************************
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. ***************************************************************************
  19. STATUS: alpha code
  20. LIMITS: Only ib* command are considered for the moment.
  21. CREDITS:
  22. The list of variable and function names was borrowed from
  23. the linux-gpib-3.1.92 files. http://linux-gpib.sourceforge.net
  24. MODIFICATIONS
  25. * 2003/02/25 : in ibrd, string terminated after ibcnt chars
  26. * 2003/03/14 : more comments
  27. TODO:
  28. - support for $r = ibrd($ud, &$buffer)
  29. - integrate more constants (for ibconf)
  30. NOTES :
  31. * installation under Debian/Woody requires the php4-dev package
  32. * for convenience, install the gpib.so where dynamic extensions live
  33. and add the line
  34. extension=gpib.so
  35. to the /etc/php/{apache,cgi}/php.ini files
  36. BIBLIOGRAPHY:
  37. [php4] PHP Developpement d'applications WEB, Tobias Ratschiller et
  38. Till Gerken, Campus Press, pp. 327-...
  39. file://localhost/opt/doc.php.nexen.html/zend.structure.exporting-functions.html */
  40. #ifdef USE_LINUX_GPIB
  41. #include "gpib/ib.h"
  42. #endif
  43. #ifdef USE_NI_GPIB
  44. #include <ugpib.h>
  45. #endif
  46. #include "php.h"
  47. #include "ext/standard/info.h"
  48. /* ---- macros for declarations -------------------------- */
  49. #define FUN_ACCESSOR(functionName) \
  50. ZEND_FUNCTION(functionName)\
  51. { \
  52. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) \
  53. { \
  54. RETURN_NULL(); \
  55. } \
  56. RETURN_LONG(functionName); \
  57. }
  58. #define FUN_STRING(functionName) \
  59. ZEND_FUNCTION(functionName) \
  60. { \
  61. char *s; \
  62. int s_len; \
  63. int r; \
  64. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  65. "s", &s , &s_len \
  66. ) == FAILURE) { \
  67. RETURN_NULL(); \
  68. } \
  69. r = functionName(s); \
  70. RETURN_LONG(r); \
  71. }
  72. #define FUN_INT_STRING(functionName) \
  73. ZEND_FUNCTION(functionName) \
  74. { \
  75. long n; \
  76. char *s; \
  77. int s_len; \
  78. int r; \
  79. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  80. "ls", &n , &s , &s_len \
  81. ) == FAILURE) { \
  82. RETURN_NULL(); \
  83. } \
  84. r = functionName(n,s); \
  85. RETURN_LONG(r); \
  86. }
  87. #define FUN_INT_STRING_INT(functionName) \
  88. ZEND_FUNCTION(functionName) \
  89. { \
  90. long n,nn; \
  91. char *s; \
  92. int s_len; \
  93. int r; \
  94. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  95. "lsl", &n , &s , &s_len ,&nn\
  96. ) == FAILURE) { \
  97. RETURN_NULL(); \
  98. } \
  99. r = functionName(n,s,nn); \
  100. RETURN_LONG(r); \
  101. }
  102. #define FUN_INT(functionName) \
  103. ZEND_FUNCTION(functionName) \
  104. { \
  105. long n; \
  106. int r; \
  107. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  108. "l", &n \
  109. ) == FAILURE) { \
  110. RETURN_NULL(); \
  111. } \
  112. r = functionName(n); \
  113. RETURN_LONG(r); \
  114. }
  115. #define FUN_INT_INT(functionName) \
  116. ZEND_FUNCTION(functionName) \
  117. { \
  118. long n,nn; \
  119. int r; \
  120. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  121. "ll", &n, &nn \
  122. ) == FAILURE) { \
  123. RETURN_NULL(); \
  124. } \
  125. r = functionName(n,nn); \
  126. RETURN_LONG(r); \
  127. }
  128. #define FUN_6INT(functionName) \
  129. ZEND_FUNCTION(functionName) \
  130. { \
  131. long n1,n2,n3,n4,n5,n6; \
  132. int r; \
  133. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  134. "llllll", &n1,&n2,&n3,&n4,&n5,&n6 \
  135. ) == FAILURE) { \
  136. RETURN_NULL(); \
  137. } \
  138. r = functionName(n1,n2,n3,n4,n5,n6); \
  139. RETURN_LONG(r); \
  140. }
  141. #define FUN_INT_PINT(functionName) \
  142. ZEND_FUNCTION(functionName) { \
  143. long n; \
  144. zval *z; \
  145. char result; \
  146. long r; \
  147. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  148. "lz", &n, &z) \
  149. == FAILURE) { \
  150. return; \
  151. } \
  152. if (!PZVAL_IS_REF(z)) { \
  153. zend_error(E_WARNING, "Not a reference"); \
  154. RETURN_NULL(); \
  155. } \
  156. r=functionName(n,&result); \
  157. ZVAL_LONG(z,result); \
  158. RETURN_LONG(r); \
  159. }
  160. #define FUN_INT_PSHORT(functionName) \
  161. ZEND_FUNCTION(functionName) { \
  162. long n; \
  163. zval *z; \
  164. short result; \
  165. long r; \
  166. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  167. "lz", &n, &z) \
  168. == FAILURE) { \
  169. return; \
  170. } \
  171. if (!PZVAL_IS_REF(z)) { \
  172. zend_error(E_WARNING, "Not a reference"); \
  173. RETURN_NULL(); \
  174. } \
  175. r=functionName(n,&result); \
  176. ZVAL_LONG(z,result); \
  177. RETURN_LONG(r); \
  178. }
  179. #define FUN_INT_INT_PINT(functionName) \
  180. ZEND_FUNCTION(functionName) { \
  181. long n,nn; \
  182. zval *z; \
  183. int result; \
  184. long r; \
  185. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \
  186. "llz", &n, &nn, &z) \
  187. == FAILURE) { \
  188. return; \
  189. } \
  190. if (!PZVAL_IS_REF(z)) { \
  191. zend_error(E_WARNING, "Not a reference"); \
  192. RETURN_NULL(); \
  193. } \
  194. r=functionName(n,nn,&result); \
  195. ZVAL_LONG(z,result); \
  196. RETURN_LONG(r); \
  197. }
  198. #define NOT_IMPLEMENTED_YET(functionName) \
  199. ZEND_FUNCTION(functionName) \
  200. { \
  201. zend_error(E_WARNING, \
  202. "GPIB-PHP: Function not implemented yet"); \
  203. }
  204. /*
  205. * predefinition of all functions
  206. */
  207. ZEND_FUNCTION(ibfind);
  208. ZEND_FUNCTION(ibbna);
  209. ZEND_FUNCTION(ibclr);
  210. ZEND_FUNCTION(ibcac);
  211. ZEND_FUNCTION(ibloc);
  212. ZEND_FUNCTION(ibonl);
  213. ZEND_FUNCTION(ibpad);
  214. ZEND_FUNCTION(ibpct);
  215. ZEND_FUNCTION(ibppc);
  216. ZEND_FUNCTION(ibrdf);
  217. ZEND_FUNCTION(ibrsc);
  218. ZEND_FUNCTION(ibrsv);
  219. ZEND_FUNCTION(ibsad);
  220. ZEND_FUNCTION(ibsic);
  221. ZEND_FUNCTION(ibsre);
  222. ZEND_FUNCTION(ibtmo);
  223. ZEND_FUNCTION(ibtrg);
  224. ZEND_FUNCTION(ibwait);
  225. ZEND_FUNCTION(ibwrtf);
  226. #ifdef USE_LINUX_GPIB
  227. ZEND_FUNCTION(gpib_error_string);
  228. #endif
  229. ZEND_FUNCTION(ibsta);
  230. ZEND_FUNCTION(ibcnt);
  231. ZEND_FUNCTION(iberr);
  232. ZEND_FUNCTION(ibwrt);
  233. ZEND_FUNCTION(ibrd);
  234. ZEND_FUNCTION (ibask);
  235. ZEND_FUNCTION (ibcmd);
  236. ZEND_FUNCTION (ibcmda);
  237. ZEND_FUNCTION (ibdev);
  238. ZEND_FUNCTION (ibevent);
  239. ZEND_FUNCTION (iblines);
  240. ZEND_FUNCTION (ibrpp);
  241. ZEND_FUNCTION (ibrsp);
  242. ZEND_MINIT_FUNCTION(start_module);
  243. PHP_MINFO_FUNCTION(gpib);
  244. /*
  245. * table of all PHP GPIB functions
  246. */
  247. zend_function_entry php_gpib_functions[] =
  248. {
  249. ZEND_FE(ibfind, NULL)
  250. ZEND_FE(ibbna, NULL)
  251. ZEND_FE(ibclr, NULL)
  252. ZEND_FE(ibcac, NULL)
  253. ZEND_FE(ibloc, NULL)
  254. ZEND_FE(ibonl, NULL)
  255. ZEND_FE(ibpad, NULL)
  256. ZEND_FE(ibpct, NULL)
  257. ZEND_FE(ibppc, NULL)
  258. ZEND_FE(ibrdf, NULL)
  259. ZEND_FE(ibrsc, NULL)
  260. ZEND_FE(ibrsv, NULL)
  261. ZEND_FE(ibsad, NULL)
  262. ZEND_FE(ibsic, NULL)
  263. ZEND_FE(ibsre, NULL)
  264. ZEND_FE(ibtmo, NULL)
  265. ZEND_FE(ibtrg, NULL)
  266. ZEND_FE(ibwait, NULL)
  267. ZEND_FE(ibwrtf, NULL)
  268. #ifdef USE_LINUX_GPIB
  269. ZEND_FE(gpib_error_string, NULL)
  270. #endif
  271. ZEND_FE(ibsta, NULL)
  272. ZEND_FE(ibcnt, NULL)
  273. ZEND_FE(iberr, NULL)
  274. ZEND_FE(ibwrt, NULL)
  275. ZEND_FE(ibrd, NULL)
  276. ZEND_FE (ibask, NULL)
  277. ZEND_FE (ibcmd, NULL)
  278. ZEND_FE (ibcmda, NULL)
  279. ZEND_FE (ibdev, NULL)
  280. ZEND_FE (ibevent, NULL)
  281. ZEND_FE (iblines, NULL)
  282. ZEND_FE (ibrpp, NULL)
  283. ZEND_FE (ibrsp, NULL)
  284. { NULL, NULL, NULL}
  285. };
  286. zend_module_entry php_gpib_module_entry =
  287. {
  288. STANDARD_MODULE_HEADER,
  289. "gpib",
  290. php_gpib_functions,
  291. ZEND_MINIT(start_module), /* module startup */
  292. NULL, /* module shutdown */
  293. NULL, /* request startup */
  294. NULL, /* request shutdown */
  295. PHP_MINFO(gpib), /* info function */
  296. NO_VERSION_YET,
  297. STANDARD_MODULE_PROPERTIES
  298. };
  299. PHP_MINFO_FUNCTION(gpib)
  300. {
  301. php_info_print_table_start();
  302. php_info_print_table_row(2,"GPIB Support","Enabled");
  303. #ifdef USE_LINUX_GPIB
  304. php_info_print_table_row(2,"Supported library",
  305. "Linux GPIB "
  306. "(http://linux-gpib/sourceforge.net)");
  307. #endif
  308. #ifdef USE_NI_GPIB
  309. php_info_print_table_row(2,"Supported library",
  310. "National Instruments");
  311. #endif
  312. php_info_print_table_end();
  313. }
  314. /*
  315. * Definition of PHP constants
  316. */
  317. ZEND_MINIT_FUNCTION(start_module)
  318. {
  319. REGISTER_LONG_CONSTANT("DCAS", DCAS, CONST_CS | CONST_PERSISTENT);
  320. REGISTER_LONG_CONSTANT("DTAS", DTAS, CONST_CS | CONST_PERSISTENT);
  321. REGISTER_LONG_CONSTANT("LACS", LACS, CONST_CS | CONST_PERSISTENT);
  322. REGISTER_LONG_CONSTANT("TACS", TACS, CONST_CS | CONST_PERSISTENT);
  323. REGISTER_LONG_CONSTANT("ATN", ATN, CONST_CS | CONST_PERSISTENT);
  324. REGISTER_LONG_CONSTANT("CIC", CIC, CONST_CS | CONST_PERSISTENT);
  325. REGISTER_LONG_CONSTANT("REM", REM, CONST_CS | CONST_PERSISTENT);
  326. REGISTER_LONG_CONSTANT("LOK", LOK, CONST_CS | CONST_PERSISTENT);
  327. REGISTER_LONG_CONSTANT("CMPL", CMPL, CONST_CS | CONST_PERSISTENT);
  328. REGISTER_LONG_CONSTANT("EVENT", EVENT, CONST_CS | CONST_PERSISTENT);
  329. REGISTER_LONG_CONSTANT("SPOLL", SPOLL, CONST_CS | CONST_PERSISTENT);
  330. REGISTER_LONG_CONSTANT("RQS", RQS, CONST_CS | CONST_PERSISTENT);
  331. REGISTER_LONG_CONSTANT("SRQI", SRQI, CONST_CS | CONST_PERSISTENT);
  332. REGISTER_LONG_CONSTANT("END", END, CONST_CS | CONST_PERSISTENT);
  333. REGISTER_LONG_CONSTANT("TIMO", TIMO, CONST_CS | CONST_PERSISTENT);
  334. REGISTER_LONG_CONSTANT("ERR", ERR, CONST_CS | CONST_PERSISTENT);
  335. REGISTER_LONG_CONSTANT("EDVR", 0, CONST_CS | CONST_PERSISTENT);
  336. REGISTER_LONG_CONSTANT("ECIC", 1, CONST_CS | CONST_PERSISTENT);
  337. REGISTER_LONG_CONSTANT("ENOL", 2, CONST_CS | CONST_PERSISTENT);
  338. REGISTER_LONG_CONSTANT("EADR", 3, CONST_CS | CONST_PERSISTENT);
  339. REGISTER_LONG_CONSTANT("EARG", 4, CONST_CS | CONST_PERSISTENT);
  340. REGISTER_LONG_CONSTANT("ESAC", 5, CONST_CS | CONST_PERSISTENT);
  341. REGISTER_LONG_CONSTANT("EABO", 6, CONST_CS | CONST_PERSISTENT);
  342. REGISTER_LONG_CONSTANT("ENEB", 7, CONST_CS | CONST_PERSISTENT);
  343. REGISTER_LONG_CONSTANT("EDMA", 8, CONST_CS | CONST_PERSISTENT);
  344. REGISTER_LONG_CONSTANT("EOIP", 10, CONST_CS | CONST_PERSISTENT);
  345. REGISTER_LONG_CONSTANT("ECAP", 11, CONST_CS | CONST_PERSISTENT);
  346. REGISTER_LONG_CONSTANT("EFSO", 12, CONST_CS | CONST_PERSISTENT);
  347. REGISTER_LONG_CONSTANT("EBUS", 14, CONST_CS | CONST_PERSISTENT);
  348. REGISTER_LONG_CONSTANT("ESTB", 15, CONST_CS | CONST_PERSISTENT);
  349. REGISTER_LONG_CONSTANT("ESRQ", 16, CONST_CS | CONST_PERSISTENT);
  350. REGISTER_LONG_CONSTANT("ETAB", 20, CONST_CS | CONST_PERSISTENT);
  351. REGISTER_LONG_CONSTANT("TNONE", 0, CONST_CS | CONST_PERSISTENT);
  352. REGISTER_LONG_CONSTANT("T10us", 1, CONST_CS | CONST_PERSISTENT);
  353. REGISTER_LONG_CONSTANT("T30us", 2, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_LONG_CONSTANT("T100us", 3, CONST_CS | CONST_PERSISTENT);
  355. REGISTER_LONG_CONSTANT("T300us", 4, CONST_CS | CONST_PERSISTENT);
  356. REGISTER_LONG_CONSTANT("T1ms", 5, CONST_CS | CONST_PERSISTENT);
  357. REGISTER_LONG_CONSTANT("T3ms", 6, CONST_CS | CONST_PERSISTENT);
  358. REGISTER_LONG_CONSTANT("T10ms", 7, CONST_CS | CONST_PERSISTENT);
  359. REGISTER_LONG_CONSTANT("T30ms", 8, CONST_CS | CONST_PERSISTENT);
  360. REGISTER_LONG_CONSTANT("T100ms", 9, CONST_CS | CONST_PERSISTENT);
  361. REGISTER_LONG_CONSTANT("T300ms", 10, CONST_CS | CONST_PERSISTENT);
  362. REGISTER_LONG_CONSTANT("T1s", 11, CONST_CS | CONST_PERSISTENT);
  363. REGISTER_LONG_CONSTANT("T3s", 12, CONST_CS | CONST_PERSISTENT);
  364. REGISTER_LONG_CONSTANT("T10s", 13, CONST_CS | CONST_PERSISTENT);
  365. REGISTER_LONG_CONSTANT("T30s", 14, CONST_CS | CONST_PERSISTENT);
  366. REGISTER_LONG_CONSTANT("T100s", 15, CONST_CS | CONST_PERSISTENT);
  367. REGISTER_LONG_CONSTANT("T300s", 16, CONST_CS | CONST_PERSISTENT);
  368. REGISTER_LONG_CONSTANT("T1000s", 17, CONST_CS | CONST_PERSISTENT);
  369. REGISTER_LONG_CONSTANT("EOS_ASK",0x1c00, CONST_CS | CONST_PERSISTENT);
  370. REGISTER_LONG_CONSTANT("REOS", 0x0400, CONST_CS | CONST_PERSISTENT);
  371. REGISTER_LONG_CONSTANT("XEOS", 0x800, CONST_CS | CONST_PERSISTENT);
  372. REGISTER_LONG_CONSTANT("BIN", 0x1000, CONST_CS | CONST_PERSISTENT);
  373. return(SUCCESS);
  374. }
  375. #if COMPILE_DL
  376. DLEXPORT zend_module_entry *get_module(void)
  377. {
  378. return &php_gpib_module_entry;
  379. }
  380. #endif
  381. /*
  382. * the accessors for the status variables and
  383. * most functions are defined by macros
  384. */
  385. FUN_ACCESSOR(iberr)
  386. FUN_ACCESSOR(ibcnt)
  387. FUN_ACCESSOR(ibsta)
  388. FUN_STRING(ibfind)
  389. FUN_INT_STRING(ibbna)
  390. FUN_INT(ibclr)
  391. FUN_INT_INT(ibcac)
  392. FUN_INT(ibloc)
  393. FUN_INT_INT(ibonl)
  394. FUN_INT_INT(ibpad)
  395. FUN_INT(ibpct)
  396. FUN_INT_INT(ibppc)
  397. FUN_INT_STRING(ibrdf)
  398. FUN_INT_INT(ibrsc)
  399. FUN_INT_INT(ibrsv)
  400. FUN_INT_INT(ibsad)
  401. FUN_INT(ibsic)
  402. FUN_INT_INT(ibsre)
  403. FUN_INT_INT(ibtmo)
  404. FUN_INT(ibtrg)
  405. FUN_INT_INT(ibwait)
  406. FUN_INT_STRING(ibwrtf)
  407. FUN_INT_PINT(ibrpp)
  408. FUN_INT_PINT(ibrsp)
  409. FUN_INT_PSHORT(ibevent)
  410. FUN_INT_PSHORT(iblines)
  411. FUN_INT_INT_PINT(ibask)
  412. FUN_INT_STRING_INT(ibcmd)
  413. FUN_INT_STRING_INT(ibcmda)
  414. FUN_6INT(ibdev)
  415. /*
  416. * a few functions need a special treatment
  417. */
  418. #ifdef USE_LINUX_GPIB
  419. ZEND_FUNCTION(gpib_error_string)
  420. {
  421. long n;
  422. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  423. "l", &n
  424. ) == FAILURE) {
  425. return;
  426. }
  427. RETURN_STRING((char*)gpib_error_string(n), 1);
  428. }
  429. #endif
  430. ZEND_FUNCTION(ibwrt)
  431. {
  432. long n;
  433. char *s;
  434. int s_len;
  435. int len;
  436. if (ZEND_NUM_ARGS() == 2) {
  437. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  438. "ls", &n, &s, &s_len)
  439. == FAILURE) {
  440. return;
  441. }
  442. len = s_len;
  443. }
  444. else
  445. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  446. "lsl", &n, &s, &s_len, &len)
  447. == FAILURE) {
  448. return;
  449. }
  450. RETURN_LONG(ibwrt(n,s,len));
  451. }
  452. ZEND_FUNCTION(ibrd)
  453. {
  454. /* at the moment only the traditional form is supported
  455. $r = ibrd($ud,&$string,$count)
  456. (read at most $count bytes into $string)
  457. but
  458. $r = ibrd($ud,&$string)
  459. (read unlimited number of bytes) will be. Soon. Really.
  460. */
  461. long n;
  462. zval *z;
  463. long len;
  464. char *p;
  465. long r;
  466. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
  467. "lzl", &n, &z, &len)
  468. == FAILURE) {
  469. return;
  470. }
  471. if (!PZVAL_IS_REF(z)) {
  472. zend_error(E_WARNING, "Not a reference");
  473. RETURN_NULL();
  474. }
  475. p = (char *) malloc(len + 1);
  476. memset(p,0,len+1);
  477. r=ibrd(n,p,len);
  478. p[ibcnt]='\0';
  479. ZVAL_STRING(z,p,1);
  480. free(p);
  481. RETURN_LONG(r);
  482. }