cvs: pecl /usblib CREDITS EXPERIMENTAL config.m4 config.w32 php_usblib.c php_usblib.h usblib.php /usblib/tests 001.phpt
| From: | Andrey Hristov | Date: | Tue, 07 Jun 2005 21:19:59 +0000 |
| Subject: | cvs: pecl /usblib CREDITS EXPERIMENTAL config.m4 config.w32 php_usblib.c php_usblib.h usblib.php /usblib/tests 001.phpt | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-3239@lists.php.net to get a copy of this message | ||
andrey Tue Jun 7 17:19:59 2005 EDT
Added files:
/pecl/usblib CREDITS EXPERIMENTAL config.m4 config.w32 php_usblib.c
php_usblib.h usblib.php
/pecl/usblib/tests 001.phpt
Log:
add usblib extension that wraps libusb 1.0 (dev) from
http://libusb.sf.net
http://cvs.php.net/co.php/pecl/usblib/config.m4?r=1.1&p=1 Index: pecl/usblib/config.m4 +++ pecl/usblib/config.m4 dnl $Id: config.m4,v 1.1 2005/06/07 21:19:59 andrey Exp $ PHP_ARG_WITH(usblib, for usblib support, [ --with-usblib Include usblib support]) if test "$PHP_USBLIB" != "no"; then dnl # --with-usblib -> check with-path SEARCH_PATH="/usr/local" # you might want to change this SEARCH_FOR="/include/usb.h" # you most likely want to change this if test -r $PHP_USBLIB/$SEARCH_FOR; then # path given as parameter USBLIB_DIR=$PHP_USBLIB else # search default path list AC_MSG_CHECKING([for usblib files in default path]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then USBLIB_DIR=$i AC_MSG_RESULT(found in $i) fi done fi if test -z "$USBLIB_DIR"; then AC_MSG_RESULT([not found]) AC_MSG_ERROR([Please reinstall the usblib distribution]) fi PHP_ADD_INCLUDE($USBLIB_DIR/include) dnl # --with-usblib -> check for lib and symbol presence LIBNAME=usb # you may want to change this LIBSYMBOL=usb_init # you most likely want to change this PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, [ PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $USBLIB_DIR/lib, USBLIB_SHARED_LIBADD) AC_DEFINE(HAVE_USBLIBLIB,1,[ ]) ],[ AC_MSG_ERROR([wrong usblib lib version or lib not found]) ],[ -L$USBLIB_DIR/lib -lm -ldl ]) PHP_SUBST(USBLIB_SHARED_LIBADD) PHP_NEW_EXTENSION(usblib, php_usblib.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/usblib/config.w32?r=1.1&p=1 Index: pecl/usblib/config.w32 +++ pecl/usblib/config.w32 // $Id: config.w32,v 1.1 2005/06/07 21:19:59 andrey Exp $ // vim:ft=javascript // If your extension references something external, use ARG_WITH ARG_WITH("usblib", "for usblib support", "no"); if (PHP_USBLIB != "no") { EXTENSION("usblib", "php_usblib.c"); } http://cvs.php.net/co.php/pecl/usblib/php_usblib.c?r=1.1&p=1 Index: pecl/usblib/php_usblib.c +++ pecl/usblib/php_usblib.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Andrey Hristov <andrey php net> | +----------------------------------------------------------------------+ */ /* $Id: php_usblib.c,v 1.1 2005/06/07 21:19:59 andrey Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_usblib.h" #define USB_BUS_RES_NAME "USB Bus" #define USB_DEV_RES_NAME "USB Device" /* True global resources - no need for thread safety here */ static int le_usb_bus, le_usb_device; #ifdef COMPILE_DL_USBLIB ZEND_GET_MODULE(usblib) #endif /* {{{ _close_php_usb_bus */ static void _close_php_usb_bus(zend_rsrc_list_entry *rsrc TSRMLS_DC) { struct usb_bus *bus = (struct usb_bus *) rsrc->ptr; // return; if (bus) { usb_free_bus(bus); bus = NULL; } } /* }}} */ /* {{{ _close_php_usb_device */ static void _close_php_usb_device(zend_rsrc_list_entry *rsrc TSRMLS_DC) { struct usb_device *dev = (struct usb_device *) rsrc->ptr; // return; if (dev) { usb_free_dev(dev); dev = NULL; } } /* }}} */ /* {{{ proto bool usblib_init(void) Always returns true */ PHP_FUNCTION(usblib_init) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_init() does not expect any parameters"); RETURN_FALSE; } usb_init(); RETURN_TRUE; } /* }}} */ /* {{{ proto bool usblib_set_debug(int level) Always returns true */ PHP_FUNCTION(usblib_set_debug) { long level; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &level) == FAILURE) { RETURN_FALSE; } usb_set_debug(level); RETURN_TRUE; } /* }}} */ /* {{{ proto int usblib_find_busses(void) */ PHP_FUNCTION(usblib_find_busses) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_find_busses() does not expect any parameters"); RETURN_FALSE; } RETURN_LONG(usb_find_busses()); } /* }}} */ /* {{{ proto int usblib_find_devices(void) */ PHP_FUNCTION(usblib_find_devices) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_find_devices() does not expect any parameters"); RETURN_FALSE; } RETURN_LONG(usb_find_devices()); } /* }}} */ /* {{{ proto string usblib_strerror(void) "No error" when there is no error */ PHP_FUNCTION(usblib_strerror) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_strerror() does not expect any parameters"); RETURN_FALSE; } RETURN_STRING(usb_strerror(), 1); } /* }}} */ /* {{{ proto array usblib_get_busses(void) Gets all USB Bus handles */ PHP_FUNCTION(usblib_get_busses) { struct usb_bus *bus, *bus_tmp; zval *bus_element; if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_get_busses() does not expect any parameters"); RETURN_FALSE; } if (!(bus = usb_get_busses())) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while calling usb_get_busses()"); RETURN_FALSE; } array_init(return_value); for (bus_tmp = bus; bus_tmp; bus_tmp = bus_tmp->next) { MAKE_STD_ZVAL(bus_element); ZEND_REGISTER_RESOURCE(bus_element, bus_tmp, le_usb_bus); add_next_index_zval(return_value, bus_element); } } /* }}} */ /* {{{ proto array usblib_get_devices(resource usb_bus) Gets all devices for specific USB Bus */ PHP_FUNCTION(usblib_get_devices) { struct usb_bus *bus; zval *bus_element, *tmp; struct usb_device *dev; int c_id = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &bus_element) == FAILURE) { RETURN_FALSE; } if (!bus_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for bus_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(bus, struct usb_bus *, &bus_element, c_id, USB_BUS_RES_NAME, le_usb_bus); array_init(return_value); for (dev = bus->devices; dev; dev = dev->next) { MAKE_STD_ZVAL(tmp); ZEND_REGISTER_RESOURCE(tmp, dev, le_usb_device); add_next_index_zval(return_value, tmp); } } /* }}} */ /* {{{ proto bool usblib_free_device(resource usb_device) Frees device */ PHP_FUNCTION(usblib_free_device) { struct usb_device *dev; zval *dev_element, *tmp; int c_id = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &dev_element) == FAILURE) { RETURN_FALSE; } if (!dev_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for dev_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(dev, struct usb_device *, &dev_element, c_id, USB_DEV_RES_NAME, le_usb_device); usb_free_dev(dev); dev = NULL; zend_list_delete(Z_LVAL_P(dev_element)); ZVAL_NULL(dev_element); RETURN_TRUE; } /* }}} */ /* {{{ proto bool usblib_get_device_info(resource usb_device) Frees device */ PHP_FUNCTION(usblib_get_device_info) { struct usb_device *dev; struct usb_dev_handle *udev; zval *dev_element, *tmp, *dev_descriptor, *dev_config, *dev_configs; zval *dev_ifaces, *dev_iface, *dev_endpoints, *dev_endpoint; zval *dev_alt_settings, *dev_alt_setting; int c_id = -1, i, j, k, l; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &dev_element) == FAILURE) { RETURN_FALSE; } if (!dev_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for dev_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(dev, struct usb_device *, &dev_element, c_id, USB_DEV_RES_NAME, le_usb_device); array_init(return_value); if ((udev = usb_open(dev))) { char desc[256]; if (dev->descriptor.iManufacturer && usb_get_string_simple(udev, dev->descriptor.iManufacturer, desc, sizeof(desc))) { add_assoc_stringl(return_value, "vendor", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "vendor", "", 0, 1); } if (dev->descriptor.iProduct && usb_get_string_simple(udev, dev->descriptor.iProduct, desc, sizeof(desc))) { add_assoc_stringl(return_value, "product", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "product", "", 0, 1); } if (dev->descriptor.iSerialNumber) { usb_get_string_simple(udev, dev->descriptor.iSerialNumber, desc, sizeof(desc)); add_assoc_stringl(return_value, "serial_no", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "serial_no", "", 0, 1); } } else { add_assoc_stringl(return_value, "vendor", "", 0, 1); add_assoc_stringl(return_value, "product", "", 0, 1); add_assoc_stringl(return_value, "serial_no", "", 0, 1); } add_assoc_bool(return_value, "root_dev", dev->bus->root_dev == dev); add_assoc_bool(return_value, "has_children", dev->num_children); MAKE_STD_ZVAL(dev_descriptor); array_init(dev_descriptor); add_assoc_long(dev_descriptor, "length", dev->descriptor.bLength); add_assoc_long(dev_descriptor, "descriptor_type", dev->descriptor.bDescriptorType); add_assoc_long(dev_descriptor, "usb", dev->descriptor.bcdUSB); add_assoc_long(dev_descriptor, "device_class", dev->descriptor.bDeviceClass); add_assoc_long(dev_descriptor, "device_subclass", dev->descriptor.bDeviceSubClass); add_assoc_long(dev_descriptor, "device_protocol", dev->descriptor.bDeviceProtocol); add_assoc_long(dev_descriptor, "max_packet_size", dev->descriptor.bMaxPacketSize0); add_assoc_long(dev_descriptor, "vendor", dev->descriptor.idVendor); add_assoc_long(dev_descriptor, "product", dev->descriptor.idProduct); add_assoc_long(dev_descriptor, "manufacturer", dev->descriptor.iManufacturer); add_assoc_long(dev_descriptor, "serial_number", dev->descriptor.iSerialNumber); add_assoc_long(dev_descriptor, "config_num", dev->descriptor.bNumConfigurations); add_assoc_zval(return_value, "device_descriptor", dev_descriptor); MAKE_STD_ZVAL(dev_configs); array_init(dev_configs); for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) { struct usb_config_descriptor *cfg = &dev->config[i]; MAKE_STD_ZVAL(dev_config); array_init(dev_config); add_assoc_long(dev_config, "config", cfg->iConfiguration); add_assoc_long(dev_config, "total_len", cfg->wTotalLength); add_assoc_long(dev_config, "max_power", cfg->MaxPower); add_assoc_long(dev_config, "attributes", cfg->bmAttributes); add_assoc_long(dev_config, "config_value", cfg->bConfigurationValue); add_assoc_long(dev_config, "num_interfaces",cfg->bNumInterfaces); MAKE_STD_ZVAL(dev_ifaces); array_init(dev_ifaces); for (j = 0; j < cfg->bNumInterfaces; ++j) { struct usb_interface *iface = &cfg->interface[j]; MAKE_STD_ZVAL(dev_iface); array_init(dev_iface); MAKE_STD_ZVAL(dev_alt_settings); array_init(dev_alt_settings); for (k = 0; k < iface->num_altsetting; ++k) { struct usb_interface_descriptor *iface_desc = &iface->altsetting[k]; MAKE_STD_ZVAL(dev_alt_setting); array_init(dev_alt_setting); add_assoc_long(dev_alt_setting, "interface_number", iface_desc->bInterfaceNumber); add_assoc_long(dev_alt_setting, "alternate_setting", iface_desc->bAlternateSetting); add_assoc_long(dev_alt_setting, "num_endpoints", iface_desc->bNumEndpoints); add_assoc_long(dev_alt_setting, "interface_class", iface_desc->bInterfaceClass); add_assoc_long(dev_alt_setting, "interface_subclass",iface_desc->bInterfaceSubClass); add_assoc_long(dev_alt_setting, "interface_protocol",iface_desc->bInterfaceProtocol); add_assoc_long(dev_alt_setting, "interface", iface_desc->iInterface); MAKE_STD_ZVAL(dev_endpoints); array_init(dev_endpoints); for (l = 0; l < iface_desc->bNumEndpoints; ++l) { struct usb_endpoint_descriptor *endp = &iface_desc->endpoint[k]; MAKE_STD_ZVAL(dev_endpoint); array_init(dev_endpoint); add_assoc_long(dev_endpoint, "endpoint_address",endp->bEndpointAddress); add_assoc_long(dev_endpoint, "attributes", endp->bmAttributes); add_assoc_long(dev_endpoint, "max_packet_size", endp->wMaxPacketSize); add_assoc_long(dev_endpoint, "interval", endp->bInterval); add_assoc_long(dev_endpoint, "refresh", endp->bRefresh); add_assoc_long(dev_endpoint, "synch_address", endp->bSynchAddress); add_next_index_zval(dev_endpoints, dev_endpoint); } add_assoc_zval(dev_alt_setting, "endpoints", dev_endpoints); add_next_index_zval(dev_alt_settings, dev_alt_setting); } add_assoc_zval(dev_iface, "alternate_settings", dev_alt_settings); add_next_index_zval(dev_ifaces, dev_iface); } add_assoc_zval(dev_config, "interfaces", dev_ifaces); add_next_index_zval(dev_configs, dev_config); } add_assoc_zval(return_value, "device_configurations", dev_configs); if (udev) { usb_close(udev); } } /* }}} */ /* {{{ usblib_functions[] * * Every user visible function must have an entry in usblib_functions[]. */ function_entry usblib_functions[] = { PHP_FE(usblib_init, NULL) PHP_FE(usblib_set_debug, NULL) PHP_FE(usblib_find_busses, NULL) PHP_FE(usblib_find_devices, NULL) PHP_FE(usblib_strerror, NULL) PHP_FE(usblib_get_busses, NULL) PHP_FE(usblib_get_devices, NULL) PHP_FE(usblib_free_device, NULL) PHP_FE(usblib_get_device_info, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(usblib) { le_usb_bus = zend_register_list_destructors_ex(_close_php_usb_bus, NULL, USB_BUS_RES_NAME, module_number); le_usb_device = zend_register_list_destructors_ex(_close_php_usb_bus, NULL, USB_DEV_RES_NAME, module_number); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(usblib) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(usblib) { php_info_print_table_start(); php_info_print_table_header(2, "libusb support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ usblib_module_entry */ zend_module_entry usblib_module_entry = { STANDARD_MODULE_HEADER, "usblib", usblib_functions, PHP_MINIT(usblib), NULL, NULL, NULL, PHP_MINFO(usblib), "0.1", /* Replace with version number for your extension */ STANDARD_MODULE_PROPERTIES }; /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/usblib/php_usblib.h?r=1.1&p=1 Index: pecl/usblib/php_usblib.h +++ pecl/usblib/php_usblib.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Andrey Hristov <andrey php net> | +----------------------------------------------------------------------+ */ /* $Id: php_usblib.h,v 1.1 2005/06/07 21:19:59 andrey Exp $ */ #ifndef PHP_USBLIB_H #define PHP_USBLIB_H #include "usb.h" extern zend_module_entry usblib_module_entry; #define phpext_usblib_ptr &usblib_module_entry #ifdef PHP_WIN32 #define PHP_USBLIB_API __declspec(dllexport) #else #define PHP_USBLIB_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(usblib); PHP_MINFO_FUNCTION(usblib); /* Declare any global variables you may need between the BEGIN and END macros here: ZEND_BEGIN_MODULE_GLOBALS(usblib) long global_value; char *global_string; ZEND_END_MODULE_GLOBALS(usblib) */ /* In every utility function you add that needs to use variables in php_usblib_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as USBLIB_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define USBLIB_G(v) TSRMG(usblib_globals_id, zend_usblib_globals *, v) #else #define USBLIB_G(v) (usblib_globals.v) #endif #endif /* PHP_USBLIB_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/usblib/usblib.php?r=1.1&p=1 Index: pecl/usblib/usblib.php +++ pecl/usblib/usblib.php <?php $br = (php_sapi_name() == "cli")? "":"<br>"; if(!extension_loaded('usblib')) { dl('usblib.' . PHP_SHLIB_SUFFIX); } $module = 'usblib'; $functions = get_extension_funcs($module); echo "Functions available in the test extension:$br\n"; foreach($functions as $func) { echo $func."$br\n"; } echo "$br\n"; ?> http://cvs.php.net/co.php/pecl/usblib/tests/001.phpt?r=1.1&p=1 Index: pecl/usblib/tests/001.phpt +++ pecl/usblib/tests/001.phpt --TEST-- Testing --FILE-- <?php error_reporting(E_ALL & ~E_STRICT); if (!extension_loaded("usblib")) dl('usblib.so'); var_dump(usblib_init()); var_dump(usblib_set_debug(1024)); var_dump(usblib_set_debug(0)); var_dump(usblib_find_busses()); var_dump(usblib_find_devices()); var_dump(usblib_strerror()); var_dump("Busses", $busses = usblib_get_busses()); foreach ($busses as $v) { var_dump("Devices", usblib_get_devices($v)); } ?> ---END--- --EXPECT-- bool(true) usb_set_debug: Setting debugging level to 1024 (on) bool(true) usb_set_debug: Setting debugging level to 0 (off) bool(true) int(%d) int(%d) ---END---
http://cvs.php.net/co.php/pecl/usblib/config.m4?r=1.1&p=1 Index: pecl/usblib/config.m4 +++ pecl/usblib/config.m4 dnl $Id: config.m4,v 1.1 2005/06/07 21:19:59 andrey Exp $ PHP_ARG_WITH(usblib, for usblib support, [ --with-usblib Include usblib support]) if test "$PHP_USBLIB" != "no"; then dnl # --with-usblib -> check with-path SEARCH_PATH="/usr/local" # you might want to change this SEARCH_FOR="/include/usb.h" # you most likely want to change this if test -r $PHP_USBLIB/$SEARCH_FOR; then # path given as parameter USBLIB_DIR=$PHP_USBLIB else # search default path list AC_MSG_CHECKING([for usblib files in default path]) for i in $SEARCH_PATH ; do if test -r $i/$SEARCH_FOR; then USBLIB_DIR=$i AC_MSG_RESULT(found in $i) fi done fi if test -z "$USBLIB_DIR"; then AC_MSG_RESULT([not found]) AC_MSG_ERROR([Please reinstall the usblib distribution]) fi PHP_ADD_INCLUDE($USBLIB_DIR/include) dnl # --with-usblib -> check for lib and symbol presence LIBNAME=usb # you may want to change this LIBSYMBOL=usb_init # you most likely want to change this PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, [ PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $USBLIB_DIR/lib, USBLIB_SHARED_LIBADD) AC_DEFINE(HAVE_USBLIBLIB,1,[ ]) ],[ AC_MSG_ERROR([wrong usblib lib version or lib not found]) ],[ -L$USBLIB_DIR/lib -lm -ldl ]) PHP_SUBST(USBLIB_SHARED_LIBADD) PHP_NEW_EXTENSION(usblib, php_usblib.c, $ext_shared) fi http://cvs.php.net/co.php/pecl/usblib/config.w32?r=1.1&p=1 Index: pecl/usblib/config.w32 +++ pecl/usblib/config.w32 // $Id: config.w32,v 1.1 2005/06/07 21:19:59 andrey Exp $ // vim:ft=javascript // If your extension references something external, use ARG_WITH ARG_WITH("usblib", "for usblib support", "no"); if (PHP_USBLIB != "no") { EXTENSION("usblib", "php_usblib.c"); } http://cvs.php.net/co.php/pecl/usblib/php_usblib.c?r=1.1&p=1 Index: pecl/usblib/php_usblib.c +++ pecl/usblib/php_usblib.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Andrey Hristov <andrey php net> | +----------------------------------------------------------------------+ */ /* $Id: php_usblib.c,v 1.1 2005/06/07 21:19:59 andrey Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_usblib.h" #define USB_BUS_RES_NAME "USB Bus" #define USB_DEV_RES_NAME "USB Device" /* True global resources - no need for thread safety here */ static int le_usb_bus, le_usb_device; #ifdef COMPILE_DL_USBLIB ZEND_GET_MODULE(usblib) #endif /* {{{ _close_php_usb_bus */ static void _close_php_usb_bus(zend_rsrc_list_entry *rsrc TSRMLS_DC) { struct usb_bus *bus = (struct usb_bus *) rsrc->ptr; // return; if (bus) { usb_free_bus(bus); bus = NULL; } } /* }}} */ /* {{{ _close_php_usb_device */ static void _close_php_usb_device(zend_rsrc_list_entry *rsrc TSRMLS_DC) { struct usb_device *dev = (struct usb_device *) rsrc->ptr; // return; if (dev) { usb_free_dev(dev); dev = NULL; } } /* }}} */ /* {{{ proto bool usblib_init(void) Always returns true */ PHP_FUNCTION(usblib_init) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_init() does not expect any parameters"); RETURN_FALSE; } usb_init(); RETURN_TRUE; } /* }}} */ /* {{{ proto bool usblib_set_debug(int level) Always returns true */ PHP_FUNCTION(usblib_set_debug) { long level; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &level) == FAILURE) { RETURN_FALSE; } usb_set_debug(level); RETURN_TRUE; } /* }}} */ /* {{{ proto int usblib_find_busses(void) */ PHP_FUNCTION(usblib_find_busses) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_find_busses() does not expect any parameters"); RETURN_FALSE; } RETURN_LONG(usb_find_busses()); } /* }}} */ /* {{{ proto int usblib_find_devices(void) */ PHP_FUNCTION(usblib_find_devices) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_find_devices() does not expect any parameters"); RETURN_FALSE; } RETURN_LONG(usb_find_devices()); } /* }}} */ /* {{{ proto string usblib_strerror(void) "No error" when there is no error */ PHP_FUNCTION(usblib_strerror) { if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_strerror() does not expect any parameters"); RETURN_FALSE; } RETURN_STRING(usb_strerror(), 1); } /* }}} */ /* {{{ proto array usblib_get_busses(void) Gets all USB Bus handles */ PHP_FUNCTION(usblib_get_busses) { struct usb_bus *bus, *bus_tmp; zval *bus_element; if (ZEND_NUM_ARGS()) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "usblib_get_busses() does not expect any parameters"); RETURN_FALSE; } if (!(bus = usb_get_busses())) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while calling usb_get_busses()"); RETURN_FALSE; } array_init(return_value); for (bus_tmp = bus; bus_tmp; bus_tmp = bus_tmp->next) { MAKE_STD_ZVAL(bus_element); ZEND_REGISTER_RESOURCE(bus_element, bus_tmp, le_usb_bus); add_next_index_zval(return_value, bus_element); } } /* }}} */ /* {{{ proto array usblib_get_devices(resource usb_bus) Gets all devices for specific USB Bus */ PHP_FUNCTION(usblib_get_devices) { struct usb_bus *bus; zval *bus_element, *tmp; struct usb_device *dev; int c_id = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &bus_element) == FAILURE) { RETURN_FALSE; } if (!bus_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for bus_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(bus, struct usb_bus *, &bus_element, c_id, USB_BUS_RES_NAME, le_usb_bus); array_init(return_value); for (dev = bus->devices; dev; dev = dev->next) { MAKE_STD_ZVAL(tmp); ZEND_REGISTER_RESOURCE(tmp, dev, le_usb_device); add_next_index_zval(return_value, tmp); } } /* }}} */ /* {{{ proto bool usblib_free_device(resource usb_device) Frees device */ PHP_FUNCTION(usblib_free_device) { struct usb_device *dev; zval *dev_element, *tmp; int c_id = -1; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &dev_element) == FAILURE) { RETURN_FALSE; } if (!dev_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for dev_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(dev, struct usb_device *, &dev_element, c_id, USB_DEV_RES_NAME, le_usb_device); usb_free_dev(dev); dev = NULL; zend_list_delete(Z_LVAL_P(dev_element)); ZVAL_NULL(dev_element); RETURN_TRUE; } /* }}} */ /* {{{ proto bool usblib_get_device_info(resource usb_device) Frees device */ PHP_FUNCTION(usblib_get_device_info) { struct usb_device *dev; struct usb_dev_handle *udev; zval *dev_element, *tmp, *dev_descriptor, *dev_config, *dev_configs; zval *dev_ifaces, *dev_iface, *dev_endpoints, *dev_endpoint; zval *dev_alt_settings, *dev_alt_setting; int c_id = -1, i, j, k, l; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &dev_element) == FAILURE) { RETURN_FALSE; } if (!dev_element) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Got NULL for dev_element"); RETURN_FALSE; } ZEND_FETCH_RESOURCE(dev, struct usb_device *, &dev_element, c_id, USB_DEV_RES_NAME, le_usb_device); array_init(return_value); if ((udev = usb_open(dev))) { char desc[256]; if (dev->descriptor.iManufacturer && usb_get_string_simple(udev, dev->descriptor.iManufacturer, desc, sizeof(desc))) { add_assoc_stringl(return_value, "vendor", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "vendor", "", 0, 1); } if (dev->descriptor.iProduct && usb_get_string_simple(udev, dev->descriptor.iProduct, desc, sizeof(desc))) { add_assoc_stringl(return_value, "product", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "product", "", 0, 1); } if (dev->descriptor.iSerialNumber) { usb_get_string_simple(udev, dev->descriptor.iSerialNumber, desc, sizeof(desc)); add_assoc_stringl(return_value, "serial_no", desc, strlen(desc), 1); } else { add_assoc_stringl(return_value, "serial_no", "", 0, 1); } } else { add_assoc_stringl(return_value, "vendor", "", 0, 1); add_assoc_stringl(return_value, "product", "", 0, 1); add_assoc_stringl(return_value, "serial_no", "", 0, 1); } add_assoc_bool(return_value, "root_dev", dev->bus->root_dev == dev); add_assoc_bool(return_value, "has_children", dev->num_children); MAKE_STD_ZVAL(dev_descriptor); array_init(dev_descriptor); add_assoc_long(dev_descriptor, "length", dev->descriptor.bLength); add_assoc_long(dev_descriptor, "descriptor_type", dev->descriptor.bDescriptorType); add_assoc_long(dev_descriptor, "usb", dev->descriptor.bcdUSB); add_assoc_long(dev_descriptor, "device_class", dev->descriptor.bDeviceClass); add_assoc_long(dev_descriptor, "device_subclass", dev->descriptor.bDeviceSubClass); add_assoc_long(dev_descriptor, "device_protocol", dev->descriptor.bDeviceProtocol); add_assoc_long(dev_descriptor, "max_packet_size", dev->descriptor.bMaxPacketSize0); add_assoc_long(dev_descriptor, "vendor", dev->descriptor.idVendor); add_assoc_long(dev_descriptor, "product", dev->descriptor.idProduct); add_assoc_long(dev_descriptor, "manufacturer", dev->descriptor.iManufacturer); add_assoc_long(dev_descriptor, "serial_number", dev->descriptor.iSerialNumber); add_assoc_long(dev_descriptor, "config_num", dev->descriptor.bNumConfigurations); add_assoc_zval(return_value, "device_descriptor", dev_descriptor); MAKE_STD_ZVAL(dev_configs); array_init(dev_configs); for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) { struct usb_config_descriptor *cfg = &dev->config[i]; MAKE_STD_ZVAL(dev_config); array_init(dev_config); add_assoc_long(dev_config, "config", cfg->iConfiguration); add_assoc_long(dev_config, "total_len", cfg->wTotalLength); add_assoc_long(dev_config, "max_power", cfg->MaxPower); add_assoc_long(dev_config, "attributes", cfg->bmAttributes); add_assoc_long(dev_config, "config_value", cfg->bConfigurationValue); add_assoc_long(dev_config, "num_interfaces",cfg->bNumInterfaces); MAKE_STD_ZVAL(dev_ifaces); array_init(dev_ifaces); for (j = 0; j < cfg->bNumInterfaces; ++j) { struct usb_interface *iface = &cfg->interface[j]; MAKE_STD_ZVAL(dev_iface); array_init(dev_iface); MAKE_STD_ZVAL(dev_alt_settings); array_init(dev_alt_settings); for (k = 0; k < iface->num_altsetting; ++k) { struct usb_interface_descriptor *iface_desc = &iface->altsetting[k]; MAKE_STD_ZVAL(dev_alt_setting); array_init(dev_alt_setting); add_assoc_long(dev_alt_setting, "interface_number", iface_desc->bInterfaceNumber); add_assoc_long(dev_alt_setting, "alternate_setting", iface_desc->bAlternateSetting); add_assoc_long(dev_alt_setting, "num_endpoints", iface_desc->bNumEndpoints); add_assoc_long(dev_alt_setting, "interface_class", iface_desc->bInterfaceClass); add_assoc_long(dev_alt_setting, "interface_subclass",iface_desc->bInterfaceSubClass); add_assoc_long(dev_alt_setting, "interface_protocol",iface_desc->bInterfaceProtocol); add_assoc_long(dev_alt_setting, "interface", iface_desc->iInterface); MAKE_STD_ZVAL(dev_endpoints); array_init(dev_endpoints); for (l = 0; l < iface_desc->bNumEndpoints; ++l) { struct usb_endpoint_descriptor *endp = &iface_desc->endpoint[k]; MAKE_STD_ZVAL(dev_endpoint); array_init(dev_endpoint); add_assoc_long(dev_endpoint, "endpoint_address",endp->bEndpointAddress); add_assoc_long(dev_endpoint, "attributes", endp->bmAttributes); add_assoc_long(dev_endpoint, "max_packet_size", endp->wMaxPacketSize); add_assoc_long(dev_endpoint, "interval", endp->bInterval); add_assoc_long(dev_endpoint, "refresh", endp->bRefresh); add_assoc_long(dev_endpoint, "synch_address", endp->bSynchAddress); add_next_index_zval(dev_endpoints, dev_endpoint); } add_assoc_zval(dev_alt_setting, "endpoints", dev_endpoints); add_next_index_zval(dev_alt_settings, dev_alt_setting); } add_assoc_zval(dev_iface, "alternate_settings", dev_alt_settings); add_next_index_zval(dev_ifaces, dev_iface); } add_assoc_zval(dev_config, "interfaces", dev_ifaces); add_next_index_zval(dev_configs, dev_config); } add_assoc_zval(return_value, "device_configurations", dev_configs); if (udev) { usb_close(udev); } } /* }}} */ /* {{{ usblib_functions[] * * Every user visible function must have an entry in usblib_functions[]. */ function_entry usblib_functions[] = { PHP_FE(usblib_init, NULL) PHP_FE(usblib_set_debug, NULL) PHP_FE(usblib_find_busses, NULL) PHP_FE(usblib_find_devices, NULL) PHP_FE(usblib_strerror, NULL) PHP_FE(usblib_get_busses, NULL) PHP_FE(usblib_get_devices, NULL) PHP_FE(usblib_free_device, NULL) PHP_FE(usblib_get_device_info, NULL) {NULL, NULL, NULL} }; /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(usblib) { le_usb_bus = zend_register_list_destructors_ex(_close_php_usb_bus, NULL, USB_BUS_RES_NAME, module_number); le_usb_device = zend_register_list_destructors_ex(_close_php_usb_bus, NULL, USB_DEV_RES_NAME, module_number); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(usblib) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(usblib) { php_info_print_table_start(); php_info_print_table_header(2, "libusb support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ usblib_module_entry */ zend_module_entry usblib_module_entry = { STANDARD_MODULE_HEADER, "usblib", usblib_functions, PHP_MINIT(usblib), NULL, NULL, NULL, PHP_MINFO(usblib), "0.1", /* Replace with version number for your extension */ STANDARD_MODULE_PROPERTIES }; /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/usblib/php_usblib.h?r=1.1&p=1 Index: pecl/usblib/php_usblib.h +++ pecl/usblib/php_usblib.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Andrey Hristov <andrey php net> | +----------------------------------------------------------------------+ */ /* $Id: php_usblib.h,v 1.1 2005/06/07 21:19:59 andrey Exp $ */ #ifndef PHP_USBLIB_H #define PHP_USBLIB_H #include "usb.h" extern zend_module_entry usblib_module_entry; #define phpext_usblib_ptr &usblib_module_entry #ifdef PHP_WIN32 #define PHP_USBLIB_API __declspec(dllexport) #else #define PHP_USBLIB_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(usblib); PHP_MINFO_FUNCTION(usblib); /* Declare any global variables you may need between the BEGIN and END macros here: ZEND_BEGIN_MODULE_GLOBALS(usblib) long global_value; char *global_string; ZEND_END_MODULE_GLOBALS(usblib) */ /* In every utility function you add that needs to use variables in php_usblib_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as USBLIB_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define USBLIB_G(v) TSRMG(usblib_globals_id, zend_usblib_globals *, v) #else #define USBLIB_G(v) (usblib_globals.v) #endif #endif /* PHP_USBLIB_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/co.php/pecl/usblib/usblib.php?r=1.1&p=1 Index: pecl/usblib/usblib.php +++ pecl/usblib/usblib.php <?php $br = (php_sapi_name() == "cli")? "":"<br>"; if(!extension_loaded('usblib')) { dl('usblib.' . PHP_SHLIB_SUFFIX); } $module = 'usblib'; $functions = get_extension_funcs($module); echo "Functions available in the test extension:$br\n"; foreach($functions as $func) { echo $func."$br\n"; } echo "$br\n"; ?> http://cvs.php.net/co.php/pecl/usblib/tests/001.phpt?r=1.1&p=1 Index: pecl/usblib/tests/001.phpt +++ pecl/usblib/tests/001.phpt --TEST-- Testing --FILE-- <?php error_reporting(E_ALL & ~E_STRICT); if (!extension_loaded("usblib")) dl('usblib.so'); var_dump(usblib_init()); var_dump(usblib_set_debug(1024)); var_dump(usblib_set_debug(0)); var_dump(usblib_find_busses()); var_dump(usblib_find_devices()); var_dump(usblib_strerror()); var_dump("Busses", $busses = usblib_get_busses()); foreach ($busses as $v) { var_dump("Devices", usblib_get_devices($v)); } ?> ---END--- --EXPECT-- bool(true) usb_set_debug: Setting debugging level to 1024 (on) bool(true) usb_set_debug: Setting debugging level to 0 (off) bool(true) int(%d) int(%d) ---END---