X-Git-Url: https://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fdevices%2Fdevice.c;h=be2f079f46811131ea06eb439b509dfd7cb23be7;hp=eb81b25d7fe3ae63e388ce25bf157224975e0892;hb=e35da9a4cc74781bc73fbc5a56c45c98af97fe0b;hpb=c7dcdfd34d2bed9cc64c5c9603e51ef8c8271951 diff --git a/common/devices/device.c b/common/devices/device.c index eb81b25..be2f079 100644 --- a/common/devices/device.c +++ b/common/devices/device.c @@ -42,7 +42,7 @@ static bool duringwrite; pthread_t selThread; #endif -static int device_portfd = -1; +int device_portfd = -1; /* * Structure to store the filedescriptor we use. @@ -126,7 +126,10 @@ size_t device_write(__const __ptr_t __buf, size_t __n) { u8 buffer[300]; size_t mysize; - while (duringwrite) {} + while (duringwrite) { + fprintf(stderr,"device_write: reentrance violation!\n"); + _exit(1); + } memcpy(buffer,__buf,__n); AppendLog(buffer,__n,true); @@ -136,6 +139,8 @@ size_t device_write(__const __ptr_t __buf, size_t __n) { default : mysize = serial_write(device_portfd, __buf, __n); break; } duringwrite=false; + if (mysize!=__n) + fprintf(stderr,"WARNING: device_write(__n=%ld)=%ld\n",(long)__n,(long)mysize); return mysize; } @@ -157,20 +162,71 @@ static void device_dumpserial(void) } #endif /* DEBUG */ -static void SigHandler(int status) -{ - - unsigned char buffer[2048]; +static char SigHandler_buffer[255]; +void SigHandler(int status) +{ int count, res; - res = device_read(buffer, 255); + LIVE_DISABLE; + LIVE; + + res = device_read(SigHandler_buffer, sizeof(SigHandler_buffer)); for (count = 0; count < res ; count ++) { - Protocol->StateMachine(buffer[count]); + Protocol->StateMachine(SigHandler_buffer[count]); } + LIVE_ENABLE; } +#ifdef UCLINUX +void usleep_watchdevice(unsigned long usecs) +{ +int err; +fd_set readfds; +struct timeval target,timeout; +long timeout_sec,timeout_usec; /* signed! */ + + if (gettimeofday(&target/*tv*/,NULL/*tz*/)) + perror("usleep_watchdevice()/gettimeofday(2) init"); + target.tv_usec+=usecs%1000000; + target.tv_sec +=usecs/1000000 + (target.tv_usec/1000000); + target.tv_usec%=1000000; + + for (;;) { + if (gettimeofday(&timeout/*tv*/,NULL/*tz*/)) + perror("usleep_watchdevice()/gettimeofday(2) loop"); + + timeout_sec =target.tv_sec -(long)timeout.tv_sec ; + timeout_usec=target.tv_usec-(long)timeout.tv_usec; + + if (timeout_usec<0) { + timeout_sec--; + timeout_usec+=1000000; + } + if (timeout_sec<0) + return; + + timeout.tv_sec =timeout_sec ; + timeout.tv_usec=timeout_usec; + + FD_ZERO(&readfds); + if (device_portfd>=0) + FD_SET(device_portfd,&readfds); + + err=select((device_portfd<0 ? 0 : device_portfd+1),&readfds,NULL,NULL,&timeout); + if ( err > 0 ) { + if (device_portfd>=0 && FD_ISSET(device_portfd,&readfds)) { + /* call singal handler to process incoming data */ + SigHandler(0); + } + } else { + if (err == -1) + perror("Error in SelectLoop"); + } + } +} +#endif #if defined(__svr4__) || defined(__FreeBSD__) /* thread for handling incoming data */ @@ -189,9 +245,9 @@ void SelectLoop() { if ( err > 0 ) { /* call singal handler to process incoming data */ SigHandler(0); - /* refresh timeout, just for linux :-( */ + /* refresh timeout, just for linux :-( */ /* required for irda */ - timeout.tv_sec=15; + timeout.tv_sec=15; } else { if (err == -1) perror("Error in SelectLoop"); @@ -206,11 +262,13 @@ bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionTyp char text[100]; +#ifndef UCLINUX #if defined(__svr4__) || defined(__FreeBSD__) int rtn; #else struct sigaction sig_io; #endif +#endif /* UCLINUX */ #ifndef UCLINUX #ifdef DEBUG @@ -267,6 +325,7 @@ bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionTyp // return false; // } +#ifndef UCLINUX #if defined(__svr4__) || defined(__FreeBSD__) #else /* Set up and install handler before enabling async IO on port. */ @@ -274,6 +333,7 @@ bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionTyp sig_io.sa_flags = 0; sigaction (SIGIO, &sig_io, NULL); #endif +#endif /* UCLINUX */ /* Open device. */ result = device_open(PortDevice, with_odd_parity); @@ -283,6 +343,7 @@ bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionTyp return false; } +#ifndef UCLINUX #if defined(__svr4__) || defined(__FreeBSD__) /* create a thread to handle incoming data from mobile phone */ rtn=pthread_create(&selThread,NULL,(void*)SelectLoop,(void*)NULL); @@ -291,6 +352,7 @@ bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionTyp return false; } #endif +#endif /* UCLINUX */ return true; }