This commit was generated by cvs2svn to compensate for changes in r158,
[gnokii.git] / smsd / lowlevel.c
index ea1c394..ea36182 100644 (file)
   $Id$
   
   $Log$
-  Revision 1.1.1.2  2001/12/05 03:16:54  short
-  :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Dec  4 22:45 CET 2001
-
-  Revision 1.5  2001/12/03 15:34:54  pkot
-  Update to libsms and new structure
+  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.
 #include "misc.h"
 #include "gsm-common.h"
 #include "gsm-api.h"
-#include "phones/nk7110.h"
-#include "phones/nk6100.h"
-#include "phones/nk3110.h"
-#include "phones/nk2110.h"
+#include "fbus-6110.h"
+#include "fbus-3810.h"
 #include "smsd.h"
 #include "lowlevel.h"
 
@@ -136,7 +131,7 @@ 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;
 
@@ -164,10 +159,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);
 }
 
 
@@ -179,7 +176,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);
@@ -206,11 +203,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");
@@ -218,41 +216,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->Number = ++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);
 }
 
 
@@ -265,7 +265,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);
   }
@@ -309,7 +309,7 @@ gint (*DoAction[])(gpointer) = {
 
 void *Connect (void *a)
 {
-  GSM_SMSMemoryStatus SMSStatus = {0, 0};
+  GSM_SMSStatus SMSStatus = {0, 0, 0};
   PhoneEvent *event;
   GSM_Error error;
 
@@ -318,7 +318,7 @@ void *Connect (void *a)
   g_print ("Initializing connection...\n");
 # endif
 
-  while (!fbusinit (true))
+  while (fbusinit (true))
     sleep (1);
 
 # ifdef XDEBUG
@@ -331,15 +331,29 @@ void *Connect (void *a)
 
     if (GSM->GetSMSStatus (&SMSStatus) == GE_NONE)
     {
-      if (phoneMonitor.sms.unRead != SMSStatus.Unread ||
-          phoneMonitor.sms.number != SMSStatus.Number)
+      /* 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.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;
       }
 
-      phoneMonitor.sms.unRead = SMSStatus.Unread;
+      phoneMonitor.sms.unRead = SMSStatus.UnRead;
     }
 
     while ((event = RemoveEvent ()) != NULL)
@@ -349,7 +363,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);
     }