First version, development moved to 5110-connected machine
[gnokii.git] / common / data / virtmodem.c
index 7c7f4e2..664127f 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 */
 
 //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 bool    VM_ThreadLoop(void);
+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,34 +105,34 @@ 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);
+       /* Create and start thread, */
+       return VM_ThreadLoop();
 }
 
-void   VM_ThreadLoop(void)
+static void    VM_CharHandler(void);
+
+static 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=0;
+               timeout.tv_usec=500*1000;
+
+               res = select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout);
 
                switch (res) {
                        case 0: /* Timeout */
@@ -143,10 +140,10 @@ void      VM_ThreadLoop(void)
 
                        case -1:
                                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 +157,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 +200,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 /dev/gnokii.\n"), slave_name,
+#ifndef UCLINUX
+                       mgnokiidev
+#else /* UCLINUX */
+                       "<uClinux>"
+#endif /* UCLINUX */
+                       );
 #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 ("/dev/gnokii");
+
+       /* Create symlink */
+       err = symlink(slave_name, "/dev/gnokii");
+
+#endif /* UCLINUX */
        
        return (err);
 
@@ -225,7 +241,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 +266,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 +305,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;