X-Git-Url: https://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fdata%2Fvirtmodem.c;h=c421fd1ac1e888d762203a29779715b5c4fc31e6;hp=7c7f4e24c8baca34a17f66589b8d65b5c323655e;hb=5fe84d523d76fc6fb98eab34d0db5501bdc982e5;hpb=833e1c7c90e13ceaba3dde8e7a36fcc8dfb1db3c diff --git a/common/data/virtmodem.c b/common/data/virtmodem.c index 7c7f4e2..c421fd1 100644 --- a/common/data/virtmodem.c +++ b/common/data/virtmodem.c @@ -35,8 +35,10 @@ #include #include #include +#ifndef UCLINUX #include #include +#endif /* UCLINUX */ #include #include "misc.h" @@ -49,31 +51,28 @@ /* Global variables */ + +#define GNOKII_DEV "/var/gnokii-dev" + //extern bool TerminateThread; -int ConnectCount; /* Local variables */ int PtyRDFD; /* File descriptor for reading and writing to/from */ int PtyWRFD; /* pty interface - only different in debug mode. */ -bool UseSTDIO; /* Use STDIO for debugging purposes instead of pty */ +static bool UseSTDIO; /* Use STDIO for debugging purposes instead of pty */ bool CommandMode; -pthread_t Thread; -bool RequestTerminate; - +static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime); +static int VM_PtySetup(char *bindir); /* If initialised in debug mode, stdin/out is used instead of ptys for interface. */ -bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType connection, char *bindir, bool debug_mode, bool GSMInit,char *synchronizetime) +bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType connection, char *bindir, bool debug_mode, bool GSMInit,char *synchronizetime) { - int rtn; - CommandMode = true; - RequestTerminate = false; - if (debug_mode == true) { UseSTDIO = true; } @@ -108,45 +107,55 @@ bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType return (false); } - /* Create and start thread, */ - rtn = pthread_create(&Thread, NULL, (void *) VM_ThreadLoop, (void *)NULL); - - if (rtn == EAGAIN || rtn == EINVAL) { - return (false); - } - return (true); +#ifndef UCLINUX + /* Create and start thread, */ + return VM_ThreadLoop(); +#else + return true; +#endif } -void VM_ThreadLoop(void) +static void VM_CharHandler(void); +extern GSM_Error N6110_SendStatusRequest(void); + +bool VM_ThreadLoop(void) { int res; - struct pollfd ufds; + fd_set readfds; + struct timeval timeout; /* Note we can't use signals here as they are already used in the FBUS code. This may warrant changing the FBUS code around sometime to use select instead to free up the SIGIO handler for mainline code. */ - ufds.fd=PtyRDFD; - ufds.events=POLLIN; - - while (!RequestTerminate) { + for (;;) { if (!CommandMode) { sleep(1); } else { /* If we are in data mode, leave it to datapump to get the data */ - res=poll(&ufds,1,500); + FD_ZERO(&readfds); + FD_SET(PtyRDFD,&readfds); + timeout.tv_sec=2; + timeout.tv_usec=0;/*500*1000;*/ + + res = select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout); switch (res) { case 0: /* Timeout */ +#if 0 + N6110_SendStatusRequest(); +#endif break; case -1: + if (errno==EINTR) + continue; perror("VM_ThreadLoop - select"); - exit (-1); + return (false); default: - if (ufds.revents==POLLIN) { + if (FD_ISSET(PtyRDFD,&readfds)) { VM_CharHandler(); } else usleep(500); /* Probably the file has been closed */ break; @@ -160,34 +169,35 @@ void VM_ThreadLoop(void) the virtual modem thread */ void VM_Terminate(void) { - - /* Request termination of thread */ - RequestTerminate = true; - - /* Now wait for thread to terminate. */ - pthread_join(Thread, NULL); - if (!UseSTDIO) { close (PtyRDFD); close (PtyWRFD); } } +static int VM_GetMasterPty(char **name); + /* Open pseudo tty interface and (in due course create a symlink to be /dev/gnokii etc. ) */ -int VM_PtySetup(char *bindir) +static int VM_PtySetup(char *bindir) { int err; +#ifndef UCLINUX char mgnokiidev[200]; +#endif /* UCLINUX */ char *slave_name; +#ifndef UCLINUX char cmdline[200]; +#endif /* UCLINUX */ +#ifndef UCLINUX if (bindir) { strncpy(mgnokiidev, bindir, 200); strcat(mgnokiidev, "/"); } strncat(mgnokiidev, "mgnokiidev", 200 - strlen(bindir)); +#endif /* UCLINUX */ if (UseSTDIO) { PtyRDFD = STDIN_FILENO; @@ -202,22 +212,40 @@ int VM_PtySetup(char *bindir) } PtyWRFD = PtyRDFD; +#ifndef UCLINUX /* Check we haven't been installed setuid root for some reason if so, don't create /dev/gnokii */ if (getuid() != geteuid()) { fprintf(stderr, _("gnokiid should not be installed setuid root!\n")); return (0); } +#endif #ifdef DEBUG - fprintf (stderr, _("Slave pty is %s, calling %s to create /dev/gnokii.\n"), slave_name, mgnokiidev); + fprintf (stderr, _("Slave pty is %s, calling %s to create \"%s\".\n"), slave_name, +#ifndef UCLINUX + mgnokiidev +#else /* UCLINUX */ + "" +#endif /* UCLINUX */ + ,GNOKII_DEV); #endif /* DEBUG */ +#ifndef UCLINUX /* Create command line, something line ./mkgnokiidev ttyp0 */ sprintf(cmdline, "%s %s", mgnokiidev, slave_name); /* And use system to call it. */ err = system (cmdline); +#else /* UCLINUX */ + + /* Remove symlink in case it already exists. Don't care if it fails. */ + unlink (GNOKII_DEV); + + /* Create symlink */ + err = symlink(slave_name, GNOKII_DEV); + +#endif /* UCLINUX */ return (err); @@ -225,7 +253,7 @@ int VM_PtySetup(char *bindir) /* Handler called when characters received from serial port. calls state machine code to process it. */ -void VM_CharHandler(void) +static void VM_CharHandler(void) { unsigned char buffer[255]; int res; @@ -250,7 +278,7 @@ void VM_CharHandler(void) } /* Initialise GSM interface, returning GSM_Error as appropriate */ -GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime) +static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime) { int count=0; GSM_Error error; @@ -289,7 +317,7 @@ GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_Conne Applications by Troan and Johnson */ -int VM_GetMasterPty(char **name) { +static int VM_GetMasterPty(char **name) { #ifdef USE_UNIX98PTYS int master, err;