Found in "gnokii-working" directory, some November-patches version
[gnokii.git] / common / misc.c
index 34d81b7..bf9887b 100644 (file)
 
   A Linux/Unix toolset and driver for Nokia mobile phones.
 
+  Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
+
   Released under the terms of the GNU GPL, see file COPYING for more details.
 
-*/
+  $Id$
+  
+  $Log$
+  Revision 1.1.1.9  2002/04/03 00:07:59  short
+  Found in "gnokii-working" directory, some November-patches version
 
-#include <string.h>\r
-#include <ctype.h>\r
-#include <time.h>\r
-
-#ifndef WIN32
-  #include <sys/types.h>
-  #include <sys/stat.h>
-  #include <stdlib.h>
-  #include <fcntl.h>
-  #include <signal.h>
-  #include <unistd.h>
-  #include <errno.h>
-#endif\r
-\r
-#include "misc.h"\r
-#include "gsm-common.h"\r
-
-#ifndef HAVE_TIMEOPS
-
-/* FIXME: I have timersub defined in sys/time.h :-( PJ
-   FIXME: Jano wants this function too... PJ
-
-int timersub(struct timeval *a, struct timeval *b, struct timeval *result) {
-  do {
-    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
-    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;
-    if ((result)->tv_usec < 0) {
-      --(result)->tv_sec;
-      (result)->tv_usec += 1000000;
-    }
-  } while (0);
-}
-*/
+  Revision 1.18  2001/09/09 21:45:49  machek
+  Cleanups from Ladislav Michl <ladis@psi.cz>:
 
-#endif
+  *) do *not* internationalize debug messages
 
-int GetLine(FILE *File, char *Line, int count) {
+  *) some whitespace fixes, do not use //
 
-  char *ptr;
+  *) break is unneccessary after return
 
-  if (fgets(Line, count, File)) {
-    ptr=Line+strlen(Line)-1;
+  Revision 1.17  2001/08/09 12:34:34  pkot
+  3330 and 6250 support - I have no idea if it does work (mygnokii)
 
-    while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line) *ptr--='\0';
+  Revision 1.16  2001/03/21 23:36:04  chris
+  Added the statemachine
+  This will break gnokii --identify and --monitor except for 6210/7110
 
-    return strlen(Line);
-  } else return -1;
-}
+  Revision 1.15  2001/03/06 10:38:52  machek
+  Dancall models added to the global list.
 
-/*
- * like atoi, but of a non-null-terminated string of a specified portion
- */
-int mem_to_int(const char str[], int len)
-{
-  char aux[81];
+  Revision 1.14  2001/02/06 13:55:23  pkot
+  Enabled authentication in 51xx models
 
-  strncpy(aux, str, len);
-  aux[len]=0;
-  return( atoi(aux) );
-} 
+  Revision 1.13  2001/02/02 08:09:56  ja
+  New dialogs for 6210/7110 in xgnokii. Fixed the smsd for new capabilty code.
 
-/*
- * make hexdump of Message
- */
-#ifdef DEBUG
-void hexdump(u16 MessageLength, u8 *MessageBuffer)
-{
-  int count;
-  int n=0;
-  char string1[80]="";
-  char string2[80]="";
-  char hex1[10];
-  char hex2[10];
-  for (count = 0; count < MessageLength; count ++)
-  {
-    n++;
-
-    switch (MessageBuffer[count]) {
-      case 0x09:
-        sprintf(hex1,"%02x  ",MessageBuffer[count]);
-        strcpy(hex2,".");
-        break;
-      default:
-        if (isprint(MessageBuffer[count]))
-          sprintf(hex1,"%02x%c ",MessageBuffer[count],MessageBuffer[count]);
-        else
-          sprintf(hex1,"%02x  ",MessageBuffer[count]);
-
-        if (isprint(MessageBuffer[count])) sprintf(hex2,"%c",MessageBuffer[count]);
-                                      else strcpy(hex2,".");
-        break;
-    }
-
-    if ( n!=15 && count != MessageLength-1 ) hex1[3]='|';
-    strcat(string1,hex1);
-    strcat(string2,hex2);
-    if ( n==15 || count == MessageLength-1 )
-    {      
-      fprintf(stdout,"%-60s%03x %s\n",string1,count+1,string2);
-      strcpy(string1,"");
-      strcpy(string2,"");
-      n=0;
-    }
-  }//for count
-
-  if (n!=0) fprintf (stdout,_("\n")); 
-  fflush(stdout);
-}
 
-void txhexdump(u16 MessageLength, u8 *MessageBuffer)
-{ 
-  int count;
-  int n=0;
-  for (count = 0; count < MessageLength; count ++)
-   {
-    n++;
-    fprintf(stdout,_("%02x"),MessageBuffer[count]);
-    switch (MessageBuffer[count]) {
-      case 0x09:
-        fprintf(stdout,_(" |"));
-        break;
-      default:
-        if (isprint(MessageBuffer[count])) fprintf(stdout, _("%c|"),MessageBuffer[count]);
-                                      else fprintf(stdout,_(" |"));
-        break;
-    }
-
-    if (n==18)
-    { 
-      fprintf (stdout,_("\n"));
-      n=0;
-    }
-   }//for count
-
-  if (n!=0) fprintf (stdout,_("\n")); 
-
-  fflush(stdout);
-}
-#endif
+*/
 
-#ifndef WIN32
+#include <string.h>
+#include <stdlib.h>
+#include "misc.h"
 
-#define max_buf_len 128
-#define lock_path "/var/lock/LCK.."
 
-/* Lock the device. Return allocated string with a lock name */
-char *lock_device(const char* port)
+int GetLine(FILE *File, char *Line, int count)
 {
-       char *lock_file = NULL;
-       char buffer[max_buf_len];
-       char *aux = rindex(port, '/');
-       int fd, len = strlen(aux) + strlen(lock_path);
-
-       memset(buffer, 0, sizeof(buffer));
-       lock_file = calloc(len + 1, 1);
-       if (!lock_file) {
-               fprintf(stderr, _("Cannot lock device\n"));
-               return NULL;
+       char *ptr;
+
+       if (fgets(Line, count, File)) {
+               ptr = Line + strlen(Line) - 1;
+
+               while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line)
+                       *ptr--='\0';
+
+               return strlen(Line);
        }
-       /* I think we don't need to use strncpy, as we should have enough
-        * buffer due to strlen results
-        */
-       strcpy(lock_file, lock_path);
-       strcat(lock_file, aux);
-
-       /* Check for the stale lockfile.
-        * The code taken from minicom by Miquel van Smoorenburg */
-       if ((fd = open(lock_file, O_RDONLY)) >= 0) {
-               char buf[max_buf_len];
-               int pid, n = 0;
-
-               n = read(fd, buf, sizeof(buf) - 1);
-               close(fd);
-               if (n > 0) {
-                       pid = -1;
-                       if (n == 4)
-                               /* Kermit-style lockfile. */
-                               pid = *(int *)buf;
-                       else {
-                               /* Ascii lockfile. */
-                               buf[n] = 0;
-                               sscanf(buf, "%d", &pid);
-                       }
-                       if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
-                               fprintf(stderr, _("Lockfile is stale. Overriding it..\n"));
-                               sleep(1);
-                               unlink(lock_file);
-                       } else
-                               n = 0;
+       else
+               return 0;
+}
+
+static PhoneModel models[] = {
+       {NULL,    "", 0 },
+       {"2711",  "?????", PM_SMS },            /* Dancall */
+       {"2731",  "?????", PM_SMS },
+       {"1611",  "NHE-5", 0 },
+       {"2110i", "NHE-4", PM_SMS },
+       {"2148i", "NHK-4", 0 },
+       {"3110",  "0310" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-8 */
+       {"3210",  "NSE-8", PM_SMS | PM_DTMF },
+       {"3210",  "NSE-9", PM_SMS | PM_DTMF },
+       {"3310",  "NHM-5", PM_SMS | PM_DTMF },
+       {"3330",  "NHM-6", PM_SMS | PM_DTMF },
+       {"3810",  "0305" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-9 */
+       {"5110",  "NSE-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"5130",  "NSK-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"5160",  "NSW-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"5190",  "NSB-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6110",  "NSE-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6120",  "NSC-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6130",  "NSK-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6150",  "NSM-1", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"616x",  "NSW-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6185",  "NSD-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6190",  "NSB-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
+       {"6210",  "NPE-3", PM_CALLERGROUP | PM_CALENDAR | PM_EXTPBK },
+       {"6250",  "NHM-3", PM_CALLERGROUP | PM_CALENDAR | PM_EXTPBK },
+       {"7110",  "NSE-5", PM_CALLERGROUP | PM_SPEEDDIAL | PM_EXTPBK },
+       {"8810",  "NSE-6", PM_SMS | PM_DTMF | PM_DATA },
+       {"8110i", "0423",  PM_SMS | PM_DTMF | PM_DATA }, /* Guess for NHE-6 */
+       {"8110",  "0423" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-6BX */
+       {"9000i", "RAE-4", 0 },
+       {"9110",  "RAE-2", 0 },
+       {"550",   "THF-10", 0 },
+       {"540",   "THF-11", 0 },
+       {"650",   "THF-12", 0 },
+       {"640",   "THF-13", 0 },
+/* "AT" modele, all are prefixed by "AT-" to not to clash with FBUS interface to the same phone! 
+ */
+       {"M20",   "AT-M20",                            PM_SMS },
+       {"9110",  "AT-RAE-2",                          PM_SMS },
+       {"9210",  "AT-Nokia Communicator GSM900/1800", PM_SMS },
+       {NULL,    NULL, 0 }
+};
+
+PhoneModel *GetPhoneModel (const char *num)
+{
+       register int i = 0;
+
+       while (models[i].number != NULL) {
+               if (strcmp (num, models[i].number) == 0) {
+                       dprintf("Found model\n");
+                       return (&models[i]);
                }
-               if (n == 0) {
-                       free(lock_file);
-                       fprintf(stderr, _("Device is already locked.\n"));
-                       return NULL;
+               else {
+                       dprintf("comparing %s and %s\n", num, models[i].number);
                }
+               i++;
        }
 
-       /* Try to create a new file, with 0644 mode */
-       fd = open(lock_file, O_CREAT | O_EXCL, 0644);
-       if (fd == -1) {
-               free(lock_file);
-               fprintf(stderr, _("Cannot lock device\n"));
-               return NULL;
-       }
-       sprintf(buffer, "%10ld gnokii\n", (long)getpid());
-       write(fd, buffer, strlen(buffer));
-       close(fd);
-       return lock_file;
+       return (&models[0]);
+}
+
+inline char *GetModel (const char *num)
+{
+       return (GetPhoneModel(num)->model);
 }
 
-/* Removes lock and frees memory */
-bool unlock_device(char *lock_file)
+#ifndef HAVE_VASPRINTF
+/* Adapted from snprintf(3) man page: */
+int gvasprintf(char **destp,const char *fmt,va_list ap)
 {
-       int err;
+int n,size=0x100;
+char *p,*pnew;
 
-       if (!lock_file) {
-               fprintf(stderr, _("Cannot unlock device\n"));
-               return false;
+       if (!(p=malloc(size))) {
+               *destp=NULL;
+               return(-1);
+               }
+       for (;;) {
+               /* Try to print in the allocated space. */
+               n=gvsprintf(p,size,fmt,ap);
+               /* If that worked, return the string. */
+               if (n>-1 && n<size) {
+                       *destp=p;
+                       return(n);
+                       }
+               /* Else try again with more space. */
+               if (n>-1)       /* glibc 2.1 */
+                       size=n+1;       /* precisely what is needed */
+               else            /* glibc 2.0 */
+                       size*=2;        /* twice the old size */
+               if (!(pnew=realloc(p,size))) {
+                       free(p);
+                       *destp=NULL;
+                       return(-1);
+                       }
+               p=pnew;
        }
-       err = unlink(lock_file);
-       free(lock_file);
-       return (err + 1);
 }
-#endif /* WIN32 */
+#endif
+
+#ifndef HAVE_ASPRINTF
+int gasprintf(char **destp,const char *fmt,...)
+{
+va_list ap;
+int r;
+
+       va_start(ap,fmt);
+       r=gvasprintf(destp,fmt,ap);
+       va_end(ap);
+       return(r);
+}
+#endif