Quick patch to add a function dio_modemget() to the DirectIO extension.

Does an ioctl(TIOCMGET) to get the status of the modem control lines.

array dio_modemget(fd descriptor)

Returns an array with elements "dsr", "cts", etc equal to 1 or 0.
Example output:

array(6) {
    ["dsr"]=>
    int(0)
    ["cts"]=>
    int(0)
    ["dcd"]=>
    int(0)
    ["ri"]=>
    int(0)
    ["rts"]=>
    int(1)
    ["dtr"]=>
    int(1)
}


I wrote this for a script that checks the status of a UPS (which is provided on the serial control lines). A test script is also attached.

Btw, this is my first attempt at both working on PHP itself, and
submitting a patch. If anything here is wrong, let me know! :p

ttyl, greg
--
Greg MacLellan

? autom4te-2.53.cache
? dio_modemget.patch
? dio_modemget.patch.2
Index: ext/dio/dio.c
===================================================================
RCS file: /repository/php-src/ext/dio/dio.c,v
retrieving revision 1.21.2.3
diff -u -r1.21.2.3 dio.c
--- ext/dio/dio.c       7 Mar 2003 13:42:11 -0000       1.21.2.3
+++ ext/dio/dio.c       15 Sep 2003 22:56:07 -0000
@@ -27,6 +27,7 @@
 
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/ioctl.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
@@ -44,6 +45,7 @@
        PHP_FE(dio_write,     NULL)
        PHP_FE(dio_close,     NULL)
         PHP_FE(dio_tcsetattr,     NULL)
+       PHP_FE(dio_modemget,      NULL)
        {NULL, NULL, NULL}
 };
 
@@ -54,7 +56,7 @@
        dio_functions,
        PHP_MINIT(dio),
        NULL,
-       NULL,   
+       NULL,
        NULL,
        PHP_MINFO(dio),
        "0.1",
@@ -592,6 +594,37 @@
        zend_list_delete(Z_LVAL_P(r_fd));
 }
 /* }}} */
+
+
+/* {{{ proto array dio_modemget(resource fd)
+   Get modem bit status for the file descriptor fd */
+PHP_FUNCTION(dio_modemget)
+{
+       zval        *r_fd;
+       php_fd_t    *f;
+       int status;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
+               return;
+       }
+       ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
+
+       if (ioctl(f->fd, TIOCMGET, &status) == -1) {
+               php_error(E_WARNING, "%s(): cannot ioctl(TIOCMGET) %d: %s",
+                                 get_active_function_name(TSRMLS_C), f->fd, 
strerror(errno));
+               RETURN_FALSE;
+       }
+
+       array_init(return_value);
+       ADD_FIELD("dsr", (TIOCM_DSR & status) == TIOCM_DSR);
+       ADD_FIELD("cts", (TIOCM_CTS & status) == TIOCM_CTS);
+       ADD_FIELD("dcd", (TIOCM_CAR & status) == TIOCM_CAR);
+       ADD_FIELD("ri",  (TIOCM_RNG & status) == TIOCM_RNG);
+       ADD_FIELD("rts", (TIOCM_RTS & status) == TIOCM_RTS);
+       ADD_FIELD("dtr", (TIOCM_DTR & status) == TIOCM_DTR);
+}
+/* }}} */
+
 
 /*
  * Local variables:
Index: ext/dio/php_dio.h
===================================================================
RCS file: /repository/php-src/ext/dio/php_dio.h,v
retrieving revision 1.4.4.1
diff -u -r1.4.4.1 php_dio.h
--- ext/dio/php_dio.h   31 Dec 2002 16:34:25 -0000      1.4.4.1
+++ ext/dio/php_dio.h   15 Sep 2003 22:56:07 -0000
@@ -45,6 +45,7 @@
 PHP_FUNCTION(dio_fcntl);
 PHP_FUNCTION(dio_close);
 PHP_FUNCTION(dio_tcsetattr);
+PHP_FUNCTION(dio_modemget);
 
 typedef struct {
        int fd;

#!/usr/bin/php
<?php

$fd = dio_open("/dev/ttyS0", O_RDONLY);

$status = dio_modemget($fd);

dio_close($fd);

var_dump($status);


?>


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to