This commit was manufactured by cvs2svn to create tag
[gnokii.git] / common / data / virtmodem.c
index 169b117..7c7f4e2 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-#ifndef UCLINUX
 #include <sys/poll.h>
 #include <pthread.h>
-#endif /* UCLINUX */
 #include <unistd.h>
 
 #include "misc.h"
 
        /* 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. */ 
 
-static bool    UseSTDIO;       /* Use STDIO for debugging purposes instead of pty */
+bool   UseSTDIO;       /* Use STDIO for debugging purposes instead of pty */
 bool   CommandMode;
 
-static GSM_Error       VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime);
-static int             VM_PtySetup(char *bindir);
+pthread_t              Thread;
+bool                   RequestTerminate;
+
 
        /* 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;
        }
@@ -107,62 +108,47 @@ bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType
                return (false);
        }
 
-#ifndef UCLINUX
-       /* Create and start thread, */
-       return VM_ThreadLoop();
-#else
-       return true;
-#endif
-}
+               /* Create and start thread, */
+       rtn = pthread_create(&Thread, NULL, (void *) VM_ThreadLoop, (void *)NULL);
 
-static void    VM_CharHandler(void);
-extern GSM_Error N6110_SendStatusRequest(void);
+    if (rtn == EAGAIN || rtn == EINVAL) {
+        return (false);
+    }
+       return (true);
+}
 
-bool VM_ThreadLoop(void)
+void   VM_ThreadLoop(void)
 {
        int res;
-       fd_set readfds;
-       struct timeval timeout;
+       struct pollfd ufds;
 
                /* 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. */
 
-       for (;;) {
+       ufds.fd=PtyRDFD;
+       ufds.events=POLLIN;
+
+       while (!RequestTerminate) {
          if (!CommandMode) {
            sleep(1);
          } else {  /* If we are in data mode, leave it to datapump to get the data */
 
-               FD_ZERO(&readfds);
-               FD_SET(PtyRDFD,&readfds);
-               FD_SET(device_portfd,&readfds);
-               timeout.tv_sec=2;
-               timeout.tv_usec=0;/*500*1000;*/
-
-               res = select((device_portfd > PtyRDFD ? device_portfd : PtyRDFD)+1,
-                               &readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout);
+               res=poll(&ufds,1,500);
 
                switch (res) {
                        case 0: /* Timeout */
-#if 0
-                               N6110_SendStatusRequest();
-#endif
                                break;
 
                        case -1:
-                               if (errno==EINTR)
-                                       continue;
                                perror("VM_ThreadLoop - select");
-                               return (false);
+                               exit (-1);
 
                        default:
-                         if (FD_ISSET(PtyRDFD,&readfds))
+                         if (ufds.revents==POLLIN) {
                            VM_CharHandler();
-                         if (FD_ISSET(device_portfd,&readfds))
-                           SigHandler(0);
-                         if (!FD_ISSET(PtyRDFD,&readfds) && !FD_ISSET(device_portfd,&readfds))
-                           usleep(500); /* Probably the file has been closed */
+                         } else usleep(500); /* Probably the file has been closed */
                          break;
                }
          }
@@ -174,35 +160,34 @@ bool 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. ) */
 
-static int             VM_PtySetup(char *bindir)
+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;
@@ -217,40 +202,22 @@ static 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 \"%s\".\n"), slave_name,
-#ifndef UCLINUX
-                       mgnokiidev
-#else /* UCLINUX */
-                       "<uClinux>"
-#endif /* UCLINUX */
-                       ,GNOKII_DEV);
+       fprintf (stderr, _("Slave pty is %s, calling %s to create /dev/gnokii.\n"), slave_name, mgnokiidev);
 #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);
 
@@ -258,7 +225,7 @@ static int          VM_PtySetup(char *bindir)
 
     /* Handler called when characters received from serial port.
        calls state machine code to process it. */
-static void    VM_CharHandler(void)
+void    VM_CharHandler(void)
 {
     unsigned char   buffer[255];
     int             res;
@@ -283,7 +250,7 @@ static void    VM_CharHandler(void)
 }     
 
        /* Initialise GSM interface, returning GSM_Error as appropriate  */
-static GSM_Error       VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime)
+GSM_Error      VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime)
 {
        int             count=0;
        GSM_Error       error;
@@ -322,7 +289,7 @@ static GSM_Error    VM_GSMInitialise(char *model, char *port, char *initlength, GS
    Applications by Troan and Johnson */
 
 
-static int     VM_GetMasterPty(char **name) { 
+int    VM_GetMasterPty(char **name) { 
 
 #ifdef USE_UNIX98PTYS
        int master, err;
@@ -348,11 +315,8 @@ static int VM_GetMasterPty(char **name) {
    /* search for an unused pty */
    for (i=0; i<16 && master <= 0; i++) {
       for (j=0; j<16 && master <= 0; j++) {
-static const char *ptyp8="pqrstuvwxyzPQRST";
-static const char *ptyp9="0123456789abcdef";
-
-         (*name)[8] = ptyp8[i];
-         (*name)[9] = ptyp9[j];
+         (*name)[8] = "pqrstuvwxyzPQRST"[i];
+         (*name)[9] = "0123456789abcdef"[j];
          /* open the master pty */
          if ((master = open(*name, O_RDWR | O_NOCTTY | O_NONBLOCK )) < 0) {
             if (errno == ENOENT) {