Implemented connection type "tcp" (GCT_TCP), use <hostname>:<port> as "port"
[gnokii.git] / smsd / lowlevel.c
index 8a665f3..d5191d2 100644 (file)
   $Id$
   
   $Log$
-  Revision 1.1.1.1  2001/11/25 21:59:23  short
-  :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
+  Revision 1.1.1.4  2002/04/03 01:44:15  short
+  Implemented connection type "tcp" (GCT_TCP), use <hostname>:<port> as "port"
+
+  Revision 1.1.1.3  2002/04/03 00:08:22  short
+  Found in "gnokii-working" directory, some November-patches version
 
   Revision 1.4  2001/05/30 14:36:47  pkot
   Fix smsd to use StateMachine and let it compile.
@@ -131,12 +134,14 @@ static void InitModelInf (void)
 static GSM_Error fbusinit(bool enable_monitoring)
 {
   int count=0;
-  static GSM_Error error=GE_NOLINK;
+  GSM_Error error=GE_NOLINK;
   GSM_ConnectionType connection=GCT_Serial;
   static GSM_Statemachine sm;
 
   if (!strcmp(smsdConfig.connection, "infrared"))
     connection = GCT_Infrared;
+  if (!strcmp(smsdConfig.connection, "tcp"))
+    connection = GCT_TCP;
 
   /* Initialise the code for the GSM interface. */     
 
@@ -159,10 +164,12 @@ static GSM_Error fbusinit(bool enable_monitoring)
   g_print("After usleep. GSM_LinkOK: %d\n", *GSM_LinkOK);
 #endif
 
-  if (*GSM_LinkOK == true)
-    InitModelInf ();
+  if (*GSM_LinkOK != true) {
+    return (GE_NOLINK);
+  }
 
-  return *GSM_LinkOK;
+  InitModelInf ();
+  return(GE_NONE);
 }
 
 
@@ -174,7 +181,7 @@ void InitPhoneMonitor (void)
   phoneMonitor.phone.imei = g_strdup (_("unknown"));
   phoneMonitor.supported = 0;
   phoneMonitor.working = FALSE;
-  phoneMonitor.sms.unRead = phoneMonitor.sms.number = 0;
+  phoneMonitor.sms.unRead = phoneMonitor.sms.used = phoneMonitor.sms.slots = 0;
   phoneMonitor.sms.messages = NULL;
   pthread_mutex_init (&smsMutex, NULL);
   pthread_cond_init (&smsCond, NULL);
@@ -201,11 +208,12 @@ static inline void FreeArray (GSList **array)
 }
 
 
-static void RefreshSMS (const gint number)
+static void RefreshSMS (const gint slots)
 {
   GSM_Error error;
   GSM_SMSMessage *msg;
   register gint i;
+  gint unread = 0;
 
 # ifdef XDEBUG
   g_print ("RefreshSMS is running...\n");
@@ -213,41 +221,43 @@ static void RefreshSMS (const gint number)
 
   pthread_mutex_lock (&smsMutex);
   FreeArray (&(phoneMonitor.sms.messages));
-  phoneMonitor.sms.number = 0;
+  phoneMonitor.sms.used = 0;
 //  pthread_mutex_unlock (&smsMutex);
 
-  i = 0;
-  while (1)
+  for (i=1; i<=slots; i++)
   {
     msg = g_malloc (sizeof (GSM_SMSMessage));
     msg->MemoryType = GMT_SM;
-    msg->Location = ++i;
+    msg->Location = i;
 
     if ((error = GSM->GetSMSMessage (msg)) == GE_NONE)
     {
   //    pthread_mutex_lock (&smsMutex);
       phoneMonitor.sms.messages = g_slist_append (phoneMonitor.sms.messages, msg);
-      phoneMonitor.sms.number++;
+      phoneMonitor.sms.used++;
+      if (msg->Type == GST_MT && msg->Status == GSS_NOTSENTREAD)
+        unread++;
   //    pthread_mutex_unlock (&smsMutex);
-      if (phoneMonitor.sms.number == number)
-      {
-        pthread_cond_signal (&smsCond);
-        pthread_mutex_unlock (&smsMutex);
-        return;
-      }
     }
     else if (error == GE_INVALIDSMSLOCATION)  /* All positions are readed */
     {
       g_free (msg);
-      pthread_cond_signal (&smsCond);
-      pthread_mutex_unlock (&smsMutex);
       break;
     }
     else
       g_free (msg);
 
-    usleep (750000);
+    /* FIXME: Why is any delay here?
+     */
+    /* usleep (750000); */
   }
+  /* Update it after the whole run as otherwise "Refreshing SMSes..."
+   * would collide with "Short Message received" message.
+   */
+  phoneMonitor.sms.unRead = unread;
+
+  pthread_cond_signal (&smsCond);
+  pthread_mutex_unlock (&smsMutex);
 }
 
 
@@ -260,7 +270,7 @@ static gint A_SendSMSMessage (gpointer data)
   if (d)
   {
     pthread_mutex_lock (&sendSMSMutex);
-    error = d->status = GSM->SendSMSMessage (d->sms, 0);
+    error = d->status = GSM->SendSMSMessage (d->sms);
     pthread_cond_signal (&sendSMSCond);
     pthread_mutex_unlock (&sendSMSMutex);
   }
@@ -304,7 +314,7 @@ gint (*DoAction[])(gpointer) = {
 
 void *Connect (void *a)
 {
-  GSM_SMSStatus SMSStatus = {0, 0};
+  GSM_SMSStatus SMSStatus = {0, 0, 0};
   PhoneEvent *event;
   GSM_Error error;
 
@@ -313,7 +323,7 @@ void *Connect (void *a)
   g_print ("Initializing connection...\n");
 # endif
 
-  while (!fbusinit (true))
+  while (fbusinit (true))
     sleep (1);
 
 # ifdef XDEBUG
@@ -326,11 +336,25 @@ void *Connect (void *a)
 
     if (GSM->GetSMSStatus (&SMSStatus) == GE_NONE)
     {
+      /* Change of "UnRead" shouldn't be interesting - user may have read some of his
+       * SMSes by the phone interface.
+       */
       if (phoneMonitor.sms.unRead != SMSStatus.UnRead ||
-          phoneMonitor.sms.number != SMSStatus.Number)
+          phoneMonitor.sms.used   != SMSStatus.Used ||
+          phoneMonitor.sms.slots  != SMSStatus.Slots)  /* shouldn't change, just to be sure */
       {
+       /* We are primarily interested in SMSStatus.Slots so try to fix it up if it is broken
+        */
+       if (SMSStatus.UnRead > SMSStatus.Used)
+         SMSStatus.Used = SMSStatus.UnRead;
+       if (SMSStatus.Used > SMSStatus.Slots)
+         SMSStatus.Slots = SMSStatus.Used;
+        phoneMonitor.sms.slots = SMSStatus.Slots;      /* shouldn't change, just to be sure */
+       /* phoneMonitor.sms.{unRead,used} will be updated by RefreshSMS()
+        */
+
         phoneMonitor.working = TRUE;
-        RefreshSMS (SMSStatus.Number);
+        RefreshSMS (SMSStatus.Slots);
         phoneMonitor.working = FALSE;
       }
 
@@ -344,7 +368,7 @@ void *Connect (void *a)
 #     endif
       phoneMonitor.working = TRUE;
       if (event->event <= Event_Exit)
-        if ((error = DoAction[event->event] (event->data)) != GE_NONE)
+        if (((error = DoAction[event->event] (event->data)) != GE_NONE) && error != GE_SMSSENDOK)
           g_print (_("Event %d failed with return code %d!\n"), event->event, error);
       g_free (event);
     }