adding dio_tcsetattr and ASYNC support

This commit is contained in:
Alan Knowles 2002-08-15 09:23:41 +00:00
parent 6adcd3d353
commit 0dc8cdd4c7
2 changed files with 183 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h>
#define le_fd_name "Direct I/O File Descriptor" #define le_fd_name "Direct I/O File Descriptor"
static int le_fd; static int le_fd;
@ -42,6 +43,7 @@ function_entry dio_functions[] = {
PHP_FE(dio_read, NULL) PHP_FE(dio_read, NULL)
PHP_FE(dio_write, NULL) PHP_FE(dio_write, NULL)
PHP_FE(dio_close, NULL) PHP_FE(dio_close, NULL)
PHP_FE(dio_tcsetattr, NULL)
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };
@ -92,6 +94,7 @@ PHP_MINIT_FUNCTION(dio)
#ifdef O_SYNC #ifdef O_SYNC
RDIOC(O_SYNC); RDIOC(O_SYNC);
#endif #endif
RDIOC(O_ASYNC);
RDIOC(O_NOCTTY); RDIOC(O_NOCTTY);
RDIOC(S_IRWXU); RDIOC(S_IRWXU);
RDIOC(S_IRUSR); RDIOC(S_IRUSR);
@ -408,6 +411,185 @@ PHP_FUNCTION(dio_fcntl)
} }
/* }}} */ /* }}} */
/* {{{ proto mixed dio_tcsetattr(resource fd, array args )
Perform a c library tcsetattr on fd */
PHP_FUNCTION(dio_tcsetattr)
{
zval *r_fd;
zval *arg = NULL;
php_fd_t *f;
int cmd;
struct termios newtio;
int Baud_Rate, Data_Bits=8, Stop_Bits=0, Parity=1;
long BAUD,DATABITS,STOPBITS,PARITYON,PARITY;
HashTable *fh;
zval **element;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &r_fd, &arg) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
if (Z_TYPE_P(arg) != IS_ARRAY) {
zend_error(E_WARNING,"tcsetattr, third argument should be an associative array");
return;
}
fh = HASH_OF(arg);
if (zend_hash_find(fh, "baud", sizeof("baud"), (void **) &element) == FAILURE) {
Baud_Rate = 9600;
}
else {
Baud_Rate = Z_LVAL_PP(element);
}
if (zend_hash_find(fh, "bits", sizeof("bits"), (void **) &element) == FAILURE) {
Data_Bits = 8;
}
else {
Data_Bits = Z_LVAL_PP(element);
}
if (zend_hash_find(fh, "stop", sizeof("stop"), (void **) &element) == FAILURE) {
Stop_Bits = 8;
}
else {
Stop_Bits = Z_LVAL_PP(element);
}
if (zend_hash_find(fh, "parity", sizeof("parity"), (void **) &element) == FAILURE) {
Parity = 0;
}
else {
Parity = Z_LVAL_PP(element);
}
/* assign to correct values... */
switch (Baud_Rate) {
case 38400:
BAUD = B38400;
break;
case 19200:
BAUD = B19200;
break;
case 9600:
BAUD = B9600;
break;
case 4800:
BAUD = B4800;
break;
case 2400:
BAUD = B2400;
break;
case 1800:
BAUD = B1800;
break;
case 1200:
BAUD = B1200;
break;
case 600:
BAUD = B600;
break;
case 300:
BAUD = B300;
break;
case 200:
BAUD = B200;
break;
case 150:
BAUD = B150;
break;
case 134:
BAUD = B134;
break;
case 110:
BAUD = B110;
break;
case 75:
BAUD = B75;
break;
case 50:
BAUD = B50;
break;
default:
zend_error(E_WARNING, "invalid baud rate %d", Baud_Rate);
RETURN_FALSE;
}
switch (Data_Bits) {
case 8:
DATABITS = CS8;
break;
case 7:
DATABITS = CS7;
break;
case 6:
DATABITS = CS6;
break;
case 5:
DATABITS = CS5;
break;
default:
zend_error(E_WARNING, "invalid data bits %d", Data_Bits);
RETURN_FALSE;
}
switch (Stop_Bits) {
case 1:
STOPBITS = 0;
break;
case 2:
STOPBITS = CSTOPB;
break;
default:
zend_error(E_WARNING, "invalid stop bits %d", Stop_Bits);
RETURN_FALSE;
}
switch (Parity)
{
case 0:
PARITYON = 0;
PARITY = 0;
break;
case 1:
PARITYON = PARENB;
PARITY = PARODD;
break;
case 2:
PARITYON = PARENB;
PARITY = 0;
break;
default:
zend_error(E_WARNING, "invalid parity %d", Parity);
RETURN_FALSE;
}
newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0; /* ICANON; */
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(f->fd, TCIFLUSH);
tcsetattr(f->fd,TCSANOW,&newtio);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void dio_close(resource fd) /* {{{ proto void dio_close(resource fd)
Close the file descriptor given by fd */ Close the file descriptor given by fd */
PHP_FUNCTION(dio_close) PHP_FUNCTION(dio_close)

View File

@ -44,6 +44,7 @@ PHP_FUNCTION(dio_read);
PHP_FUNCTION(dio_write); PHP_FUNCTION(dio_write);
PHP_FUNCTION(dio_fcntl); PHP_FUNCTION(dio_fcntl);
PHP_FUNCTION(dio_close); PHP_FUNCTION(dio_close);
PHP_FUNCTION(dio_tcsetattr);
typedef struct { typedef struct {
int fd; int fd;