:pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
[gnokii.git] / smsd / lowlevel.c
1 /*
2
3   S M S D
4
5   A Linux/Unix GUI for Nokia mobile phones.
6   Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
7   & Ján Derfiòák <ja@mail.upjs.sk>.
8
9   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11   $Id$
12   
13   $Log$
14   Revision 1.1.1.1  2001/11/25 21:59:23  short
15   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
16
17   Revision 1.4  2001/05/30 14:36:47  pkot
18   Fix smsd to use StateMachine and let it compile.
19
20   Revision 1.3  2001/03/29 08:42:59  ja
21   Enabling compilation of smsd.
22
23   Revision 1.2  2001/02/02 08:09:57  ja
24   New dialogs for 6210/7110 in xgnokii. Fixed the smsd for new capabilty code.
25
26   
27 */
28
29 #include <unistd.h>
30 #include <pthread.h>
31 #include <string.h>
32 #include <glib.h>
33 #include "misc.h"
34 #include "gsm-common.h"
35 #include "gsm-api.h"
36 #include "fbus-6110.h"
37 #include "fbus-3810.h"
38 #include "smsd.h"
39 #include "lowlevel.h"
40
41 pthread_t monitor_th;
42 PhoneMonitor phoneMonitor;
43 pthread_mutex_t smsMutex;
44 pthread_cond_t  smsCond;
45 pthread_mutex_t sendSMSMutex;
46 pthread_cond_t  sendSMSCond;
47 static pthread_mutex_t eventsMutex;
48 static GSList *ScheduledEvents = NULL;
49
50
51 inline void InsertEvent (PhoneEvent *event)
52 {
53 # ifdef XDEBUG
54   g_print ("Inserting Event: %d\n", event->event);
55 # endif
56   pthread_mutex_lock (&eventsMutex);
57   ScheduledEvents = g_slist_prepend (ScheduledEvents, event);
58   pthread_mutex_unlock (&eventsMutex);
59 }
60
61
62 inline static PhoneEvent *RemoveEvent (void)
63 {
64   GSList *list;
65   PhoneEvent *event = NULL;
66
67   pthread_mutex_lock (&eventsMutex);
68   list = g_slist_last (ScheduledEvents);
69   if (list)
70   {
71     event = (PhoneEvent *) list->data;
72     ScheduledEvents = g_slist_remove_link (ScheduledEvents, list);
73     g_slist_free_1 (list);
74   }
75   pthread_mutex_unlock (&eventsMutex);
76
77   return (event);
78 }
79
80
81 static void InitModelInf (void)
82 {
83   gchar buf[64];
84   GSM_Error error;
85   register gint i = 0;
86
87   while ((error = GSM->GetModel(buf)) != GE_NONE && i++ < 15)
88     sleep(1);
89
90   if (error == GE_NONE)
91   {
92     g_free (phoneMonitor.phone.model);
93     phoneMonitor.phone.version = g_strdup (buf);
94     phoneMonitor.phone.model = GetModel (buf);
95     if (phoneMonitor.phone.model == NULL)
96       phoneMonitor.phone.model = g_strdup (_("unknown"));
97
98     phoneMonitor.supported = GetPhoneModel(buf)->flags;
99   }
100
101   i = 0;
102   while ((error = GSM->GetRevision (buf)) != GE_NONE && i++ < 5)
103     sleep(1);
104
105   if (error == GE_NONE)
106   {
107     g_free (phoneMonitor.phone.revision);
108     phoneMonitor.phone.revision = g_strdup (buf);
109   }
110
111   i = 0;
112   while ((error = GSM->GetIMEI (buf)) != GE_NONE && i++ < 5)
113     sleep(1);
114
115   if (error == GE_NONE)
116   {
117     g_free (phoneMonitor.phone.imei);
118     phoneMonitor.phone.imei = g_strdup (buf);
119   }
120
121
122 #ifdef XDEBUG
123   g_print ("Version: %s\n", phoneMonitor.phone.version);
124   g_print ("Model: %s\n", phoneMonitor.phone.model);
125   g_print ("IMEI: %s\n", phoneMonitor.phone.imei);
126   g_print ("Revision: %s\n", phoneMonitor.phone.revision);
127 #endif
128 }
129
130
131 static GSM_Error fbusinit(bool enable_monitoring)
132 {
133   int count=0;
134   static GSM_Error error=GE_NOLINK;
135   GSM_ConnectionType connection=GCT_Serial;
136   static GSM_Statemachine sm;
137
138   if (!strcmp(smsdConfig.connection, "infrared"))
139     connection = GCT_Infrared;
140
141   /* Initialise the code for the GSM interface. */     
142
143   if (error == GE_NOLINK)
144     error = GSM_Initialise (smsdConfig.model, smsdConfig.port,
145                             smsdConfig.initlength, connection, NULL, &sm);
146
147 #ifdef XDEBUG
148   g_print ("fbusinit: error %d\n", error);
149 #endif
150
151   if (error != GE_NONE) {
152     g_print (_("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
153     return (error);
154   }
155
156   while (count++ < 40 && *GSM_LinkOK == false)
157     usleep(50000);
158 #ifdef XDEBUG
159   g_print("After usleep. GSM_LinkOK: %d\n", *GSM_LinkOK);
160 #endif
161
162   if (*GSM_LinkOK == true)
163     InitModelInf ();
164
165   return *GSM_LinkOK;
166 }
167
168
169 void InitPhoneMonitor (void)
170 {
171   phoneMonitor.phone.model = g_strdup (_("unknown"));
172   phoneMonitor.phone.version = phoneMonitor.phone.model;
173   phoneMonitor.phone.revision = g_strdup (_("unknown"));
174   phoneMonitor.phone.imei = g_strdup (_("unknown"));
175   phoneMonitor.supported = 0;
176   phoneMonitor.working = FALSE;
177   phoneMonitor.sms.unRead = phoneMonitor.sms.number = 0;
178   phoneMonitor.sms.messages = NULL;
179   pthread_mutex_init (&smsMutex, NULL);
180   pthread_cond_init (&smsCond, NULL);
181   pthread_mutex_init (&sendSMSMutex, NULL);
182   pthread_cond_init (&sendSMSCond, NULL);
183   pthread_mutex_init (&eventsMutex, NULL);
184 }
185
186
187 static inline void FreeElement (gpointer data, gpointer userData)
188 {
189   g_free ((GSM_SMSMessage *) data);
190 }
191
192
193 static inline void FreeArray (GSList **array)
194 {
195   if (*array)
196   {
197     g_slist_foreach (*array, FreeElement, NULL);
198     g_slist_free (*array);
199     *array = NULL;
200   }
201 }
202
203
204 static void RefreshSMS (const gint number)
205 {
206   GSM_Error error;
207   GSM_SMSMessage *msg;
208   register gint i;
209
210 # ifdef XDEBUG
211   g_print ("RefreshSMS is running...\n");
212 # endif
213
214   pthread_mutex_lock (&smsMutex);
215   FreeArray (&(phoneMonitor.sms.messages));
216   phoneMonitor.sms.number = 0;
217 //  pthread_mutex_unlock (&smsMutex);
218
219   i = 0;
220   while (1)
221   {
222     msg = g_malloc (sizeof (GSM_SMSMessage));
223     msg->MemoryType = GMT_SM;
224     msg->Location = ++i;
225
226     if ((error = GSM->GetSMSMessage (msg)) == GE_NONE)
227     {
228   //    pthread_mutex_lock (&smsMutex);
229       phoneMonitor.sms.messages = g_slist_append (phoneMonitor.sms.messages, msg);
230       phoneMonitor.sms.number++;
231   //    pthread_mutex_unlock (&smsMutex);
232       if (phoneMonitor.sms.number == number)
233       {
234         pthread_cond_signal (&smsCond);
235         pthread_mutex_unlock (&smsMutex);
236         return;
237       }
238     }
239     else if (error == GE_INVALIDSMSLOCATION)  /* All positions are readed */
240     {
241       g_free (msg);
242       pthread_cond_signal (&smsCond);
243       pthread_mutex_unlock (&smsMutex);
244       break;
245     }
246     else
247       g_free (msg);
248
249     usleep (750000);
250   }
251 }
252
253
254 static gint A_SendSMSMessage (gpointer data)
255 {
256   D_SMSMessage *d = (D_SMSMessage *) data;
257   GSM_Error error;
258
259   error = d->status = GE_UNKNOWN;
260   if (d)
261   {
262     pthread_mutex_lock (&sendSMSMutex);
263     error = d->status = GSM->SendSMSMessage (d->sms, 0);
264     pthread_cond_signal (&sendSMSCond);
265     pthread_mutex_unlock (&sendSMSMutex);
266   }
267
268   if (d->status == GE_SMSSENDOK)
269     return (GE_NONE);
270   else
271     return (error);
272 }
273
274
275 static gint A_DeleteSMSMessage (gpointer data)
276 {
277   GSM_SMSMessage *sms = (GSM_SMSMessage *) data;
278   GSM_Error error = GE_UNKNOWN;
279
280   if (sms)
281   {
282     error = GSM->DeleteSMSMessage(sms);
283 //    I don't use copy, I don't need free message.
284 //    g_free (sms);
285   }
286
287   return (error);
288 }
289
290
291 static gint A_Exit (gpointer data)
292 {
293   pthread_exit (0);
294   return (0); /* just to be proper */
295 }
296
297
298 gint (*DoAction[])(gpointer) = {
299   A_SendSMSMessage,
300   A_DeleteSMSMessage,
301   A_Exit
302 };
303
304
305 void *Connect (void *a)
306 {
307   GSM_SMSStatus SMSStatus = {0, 0};
308   PhoneEvent *event;
309   GSM_Error error;
310
311
312 # ifdef XDEBUG
313   g_print ("Initializing connection...\n");
314 # endif
315
316   while (!fbusinit (true))
317     sleep (1);
318
319 # ifdef XDEBUG
320   g_print ("Phone connected. Starting monitoring...\n");
321 # endif
322
323   while (1)
324   {
325     phoneMonitor.working = FALSE;
326
327     if (GSM->GetSMSStatus (&SMSStatus) == GE_NONE)
328     {
329       if (phoneMonitor.sms.unRead != SMSStatus.UnRead ||
330           phoneMonitor.sms.number != SMSStatus.Number)
331       {
332         phoneMonitor.working = TRUE;
333         RefreshSMS (SMSStatus.Number);
334         phoneMonitor.working = FALSE;
335       }
336
337       phoneMonitor.sms.unRead = SMSStatus.UnRead;
338     }
339
340     while ((event = RemoveEvent ()) != NULL)
341     {
342 #     ifdef XDEBUG      
343       g_print ("Processing Event: %d\n", event->event);
344 #     endif
345       phoneMonitor.working = TRUE;
346       if (event->event <= Event_Exit)
347         if ((error = DoAction[event->event] (event->data)) != GE_NONE)
348           g_print (_("Event %d failed with return code %d!\n"), event->event, error);
349       g_free (event);
350     }
351   }
352 }