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.1 2001/11/25 21:59:23 short
15 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
17 Revision 1.4 2001/05/30 14:36:47 pkot
18 Fix smsd to use StateMachine and let it compile.
20 Revision 1.3 2001/03/29 08:42:59 ja
21 Enabling compilation of smsd.
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.
34 #include "gsm-common.h"
36 #include "fbus-6110.h"
37 #include "fbus-3810.h"
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;
51 inline void InsertEvent (PhoneEvent *event)
54 g_print ("Inserting Event: %d\n", event->event);
56 pthread_mutex_lock (&eventsMutex);
57 ScheduledEvents = g_slist_prepend (ScheduledEvents, event);
58 pthread_mutex_unlock (&eventsMutex);
62 inline static PhoneEvent *RemoveEvent (void)
65 PhoneEvent *event = NULL;
67 pthread_mutex_lock (&eventsMutex);
68 list = g_slist_last (ScheduledEvents);
71 event = (PhoneEvent *) list->data;
72 ScheduledEvents = g_slist_remove_link (ScheduledEvents, list);
73 g_slist_free_1 (list);
75 pthread_mutex_unlock (&eventsMutex);
81 static void InitModelInf (void)
87 while ((error = GSM->GetModel(buf)) != GE_NONE && i++ < 15)
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"));
98 phoneMonitor.supported = GetPhoneModel(buf)->flags;
102 while ((error = GSM->GetRevision (buf)) != GE_NONE && i++ < 5)
105 if (error == GE_NONE)
107 g_free (phoneMonitor.phone.revision);
108 phoneMonitor.phone.revision = g_strdup (buf);
112 while ((error = GSM->GetIMEI (buf)) != GE_NONE && i++ < 5)
115 if (error == GE_NONE)
117 g_free (phoneMonitor.phone.imei);
118 phoneMonitor.phone.imei = g_strdup (buf);
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);
131 static GSM_Error fbusinit(bool enable_monitoring)
134 static GSM_Error error=GE_NOLINK;
135 GSM_ConnectionType connection=GCT_Serial;
136 static GSM_Statemachine sm;
138 if (!strcmp(smsdConfig.connection, "infrared"))
139 connection = GCT_Infrared;
141 /* Initialise the code for the GSM interface. */
143 if (error == GE_NOLINK)
144 error = GSM_Initialise (smsdConfig.model, smsdConfig.port,
145 smsdConfig.initlength, connection, NULL, &sm);
148 g_print ("fbusinit: error %d\n", error);
151 if (error != GE_NONE) {
152 g_print (_("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
156 while (count++ < 40 && *GSM_LinkOK == false)
159 g_print("After usleep. GSM_LinkOK: %d\n", *GSM_LinkOK);
162 if (*GSM_LinkOK == true)
169 void InitPhoneMonitor (void)
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);
187 static inline void FreeElement (gpointer data, gpointer userData)
189 g_free ((GSM_SMSMessage *) data);
193 static inline void FreeArray (GSList **array)
197 g_slist_foreach (*array, FreeElement, NULL);
198 g_slist_free (*array);
204 static void RefreshSMS (const gint number)
211 g_print ("RefreshSMS is running...\n");
214 pthread_mutex_lock (&smsMutex);
215 FreeArray (&(phoneMonitor.sms.messages));
216 phoneMonitor.sms.number = 0;
217 // pthread_mutex_unlock (&smsMutex);
222 msg = g_malloc (sizeof (GSM_SMSMessage));
223 msg->MemoryType = GMT_SM;
226 if ((error = GSM->GetSMSMessage (msg)) == GE_NONE)
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)
234 pthread_cond_signal (&smsCond);
235 pthread_mutex_unlock (&smsMutex);
239 else if (error == GE_INVALIDSMSLOCATION) /* All positions are readed */
242 pthread_cond_signal (&smsCond);
243 pthread_mutex_unlock (&smsMutex);
254 static gint A_SendSMSMessage (gpointer data)
256 D_SMSMessage *d = (D_SMSMessage *) data;
259 error = d->status = GE_UNKNOWN;
262 pthread_mutex_lock (&sendSMSMutex);
263 error = d->status = GSM->SendSMSMessage (d->sms, 0);
264 pthread_cond_signal (&sendSMSCond);
265 pthread_mutex_unlock (&sendSMSMutex);
268 if (d->status == GE_SMSSENDOK)
275 static gint A_DeleteSMSMessage (gpointer data)
277 GSM_SMSMessage *sms = (GSM_SMSMessage *) data;
278 GSM_Error error = GE_UNKNOWN;
282 error = GSM->DeleteSMSMessage(sms);
283 // I don't use copy, I don't need free message.
291 static gint A_Exit (gpointer data)
294 return (0); /* just to be proper */
298 gint (*DoAction[])(gpointer) = {
305 void *Connect (void *a)
307 GSM_SMSStatus SMSStatus = {0, 0};
313 g_print ("Initializing connection...\n");
316 while (!fbusinit (true))
320 g_print ("Phone connected. Starting monitoring...\n");
325 phoneMonitor.working = FALSE;
327 if (GSM->GetSMSStatus (&SMSStatus) == GE_NONE)
329 if (phoneMonitor.sms.unRead != SMSStatus.UnRead ||
330 phoneMonitor.sms.number != SMSStatus.Number)
332 phoneMonitor.working = TRUE;
333 RefreshSMS (SMSStatus.Number);
334 phoneMonitor.working = FALSE;
337 phoneMonitor.sms.unRead = SMSStatus.UnRead;
340 while ((event = RemoveEvent ()) != NULL)
343 g_print ("Processing Event: %d\n", event->event);
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);