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>.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
14 Revision 1.1.1.2 2001/12/05 03:16:54 short
15 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Dec 4 22:45 CET 2001
17 Revision 1.5 2001/12/03 15:34:54 pkot
18 Update to libsms and new structure
20 Revision 1.4 2001/05/30 14:36:47 pkot
21 Fix smsd to use StateMachine and let it compile.
23 Revision 1.3 2001/03/29 08:42:59 ja
24 Enabling compilation of smsd.
26 Revision 1.2 2001/02/02 08:09:57 ja
27 New dialogs for 6210/7110 in xgnokii. Fixed the smsd for new capabilty code.
37 #include "gsm-common.h"
39 #include "phones/nk7110.h"
40 #include "phones/nk6100.h"
41 #include "phones/nk3110.h"
42 #include "phones/nk2110.h"
47 PhoneMonitor phoneMonitor;
48 pthread_mutex_t smsMutex;
49 pthread_cond_t smsCond;
50 pthread_mutex_t sendSMSMutex;
51 pthread_cond_t sendSMSCond;
52 static pthread_mutex_t eventsMutex;
53 static GSList *ScheduledEvents = NULL;
56 inline void InsertEvent (PhoneEvent *event)
59 g_print ("Inserting Event: %d\n", event->event);
61 pthread_mutex_lock (&eventsMutex);
62 ScheduledEvents = g_slist_prepend (ScheduledEvents, event);
63 pthread_mutex_unlock (&eventsMutex);
67 inline static PhoneEvent *RemoveEvent (void)
70 PhoneEvent *event = NULL;
72 pthread_mutex_lock (&eventsMutex);
73 list = g_slist_last (ScheduledEvents);
76 event = (PhoneEvent *) list->data;
77 ScheduledEvents = g_slist_remove_link (ScheduledEvents, list);
78 g_slist_free_1 (list);
80 pthread_mutex_unlock (&eventsMutex);
86 static void InitModelInf (void)
92 while ((error = GSM->GetModel(buf)) != GE_NONE && i++ < 15)
97 g_free (phoneMonitor.phone.model);
98 phoneMonitor.phone.version = g_strdup (buf);
99 phoneMonitor.phone.model = GetModel (buf);
100 if (phoneMonitor.phone.model == NULL)
101 phoneMonitor.phone.model = g_strdup (_("unknown"));
103 phoneMonitor.supported = GetPhoneModel(buf)->flags;
107 while ((error = GSM->GetRevision (buf)) != GE_NONE && i++ < 5)
110 if (error == GE_NONE)
112 g_free (phoneMonitor.phone.revision);
113 phoneMonitor.phone.revision = g_strdup (buf);
117 while ((error = GSM->GetIMEI (buf)) != GE_NONE && i++ < 5)
120 if (error == GE_NONE)
122 g_free (phoneMonitor.phone.imei);
123 phoneMonitor.phone.imei = g_strdup (buf);
128 g_print ("Version: %s\n", phoneMonitor.phone.version);
129 g_print ("Model: %s\n", phoneMonitor.phone.model);
130 g_print ("IMEI: %s\n", phoneMonitor.phone.imei);
131 g_print ("Revision: %s\n", phoneMonitor.phone.revision);
136 static GSM_Error fbusinit(bool enable_monitoring)
139 static GSM_Error error=GE_NOLINK;
140 GSM_ConnectionType connection=GCT_Serial;
141 static GSM_Statemachine sm;
143 if (!strcmp(smsdConfig.connection, "infrared"))
144 connection = GCT_Infrared;
146 /* Initialise the code for the GSM interface. */
148 if (error == GE_NOLINK)
149 error = GSM_Initialise (smsdConfig.model, smsdConfig.port,
150 smsdConfig.initlength, connection, NULL, &sm);
153 g_print ("fbusinit: error %d\n", error);
156 if (error != GE_NONE) {
157 g_print (_("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
161 while (count++ < 40 && *GSM_LinkOK == false)
164 g_print("After usleep. GSM_LinkOK: %d\n", *GSM_LinkOK);
167 if (*GSM_LinkOK == true)
174 void InitPhoneMonitor (void)
176 phoneMonitor.phone.model = g_strdup (_("unknown"));
177 phoneMonitor.phone.version = phoneMonitor.phone.model;
178 phoneMonitor.phone.revision = g_strdup (_("unknown"));
179 phoneMonitor.phone.imei = g_strdup (_("unknown"));
180 phoneMonitor.supported = 0;
181 phoneMonitor.working = FALSE;
182 phoneMonitor.sms.unRead = phoneMonitor.sms.number = 0;
183 phoneMonitor.sms.messages = NULL;
184 pthread_mutex_init (&smsMutex, NULL);
185 pthread_cond_init (&smsCond, NULL);
186 pthread_mutex_init (&sendSMSMutex, NULL);
187 pthread_cond_init (&sendSMSCond, NULL);
188 pthread_mutex_init (&eventsMutex, NULL);
192 static inline void FreeElement (gpointer data, gpointer userData)
194 g_free ((GSM_SMSMessage *) data);
198 static inline void FreeArray (GSList **array)
202 g_slist_foreach (*array, FreeElement, NULL);
203 g_slist_free (*array);
209 static void RefreshSMS (const gint number)
216 g_print ("RefreshSMS is running...\n");
219 pthread_mutex_lock (&smsMutex);
220 FreeArray (&(phoneMonitor.sms.messages));
221 phoneMonitor.sms.number = 0;
222 // pthread_mutex_unlock (&smsMutex);
227 msg = g_malloc (sizeof (GSM_SMSMessage));
228 msg->MemoryType = GMT_SM;
231 if ((error = GSM->GetSMSMessage (msg)) == GE_NONE)
233 // pthread_mutex_lock (&smsMutex);
234 phoneMonitor.sms.messages = g_slist_append (phoneMonitor.sms.messages, msg);
235 phoneMonitor.sms.number++;
236 // pthread_mutex_unlock (&smsMutex);
237 if (phoneMonitor.sms.number == number)
239 pthread_cond_signal (&smsCond);
240 pthread_mutex_unlock (&smsMutex);
244 else if (error == GE_INVALIDSMSLOCATION) /* All positions are readed */
247 pthread_cond_signal (&smsCond);
248 pthread_mutex_unlock (&smsMutex);
259 static gint A_SendSMSMessage (gpointer data)
261 D_SMSMessage *d = (D_SMSMessage *) data;
264 error = d->status = GE_UNKNOWN;
267 pthread_mutex_lock (&sendSMSMutex);
268 error = d->status = GSM->SendSMSMessage (d->sms, 0);
269 pthread_cond_signal (&sendSMSCond);
270 pthread_mutex_unlock (&sendSMSMutex);
273 if (d->status == GE_SMSSENDOK)
280 static gint A_DeleteSMSMessage (gpointer data)
282 GSM_SMSMessage *sms = (GSM_SMSMessage *) data;
283 GSM_Error error = GE_UNKNOWN;
287 error = GSM->DeleteSMSMessage(sms);
288 // I don't use copy, I don't need free message.
296 static gint A_Exit (gpointer data)
299 return (0); /* just to be proper */
303 gint (*DoAction[])(gpointer) = {
310 void *Connect (void *a)
312 GSM_SMSMemoryStatus SMSStatus = {0, 0};
318 g_print ("Initializing connection...\n");
321 while (!fbusinit (true))
325 g_print ("Phone connected. Starting monitoring...\n");
330 phoneMonitor.working = FALSE;
332 if (GSM->GetSMSStatus (&SMSStatus) == GE_NONE)
334 if (phoneMonitor.sms.unRead != SMSStatus.Unread ||
335 phoneMonitor.sms.number != SMSStatus.Number)
337 phoneMonitor.working = TRUE;
338 RefreshSMS (SMSStatus.Number);
339 phoneMonitor.working = FALSE;
342 phoneMonitor.sms.unRead = SMSStatus.Unread;
345 while ((event = RemoveEvent ()) != NULL)
348 g_print ("Processing Event: %d\n", event->event);
350 phoneMonitor.working = TRUE;
351 if (event->event <= Event_Exit)
352 if ((error = DoAction[event->event] (event->data)) != GE_NONE)
353 g_print (_("Event %d failed with return code %d!\n"), event->event, error);