sim: Reset additional state info
[platform/upstream/ofono.git] / src / sms.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28
29 #include <glib.h>
30 #include <gdbus.h>
31 #include <sys/time.h>
32
33 #include "ofono.h"
34
35 #include "common.h"
36 #include "util.h"
37 #include "smsutil.h"
38 #include "storage.h"
39 #include "simutil.h"
40 #include "message.h"
41
42 #define uninitialized_var(x) x = x
43
44 #define MESSAGE_MANAGER_FLAG_CACHED 0x1
45 #define MESSAGE_MANAGER_FLAG_TXQ_ACTIVE 0x2
46
47 #define SETTINGS_STORE "sms"
48 #define SETTINGS_GROUP "Settings"
49
50 #define TXQ_MAX_RETRIES 4
51 #define NETWORK_TIMEOUT 332
52
53 static gboolean tx_next(gpointer user_data);
54
55 static GSList *g_drivers = NULL;
56
57 struct sms_handler {
58         struct ofono_watchlist_item item;
59         int dst;
60         int src;
61 };
62
63 struct ofono_sms {
64         int flags;
65         DBusMessage *pending;
66         struct ofono_phone_number sca;
67         struct sms_assembly *assembly;
68         guint ref;
69         GQueue *txq;
70         unsigned long tx_counter;
71         guint tx_source;
72         struct ofono_message_waiting *mw;
73         unsigned int mw_watch;
74         ofono_bool_t registered;
75         struct ofono_netreg *netreg;
76         unsigned int netreg_watch;
77         unsigned int status_watch;
78         struct ofono_sim *sim;
79         GKeyFile *settings;
80         char *imsi;
81         int bearer;
82         enum sms_alphabet alphabet;
83         const struct ofono_sms_driver *driver;
84         void *driver_data;
85         struct ofono_atom *atom;
86         ofono_bool_t use_delivery_reports;
87         struct status_report_assembly *sr_assembly;
88         GHashTable *messages;
89         struct ofono_watchlist *text_handlers;
90         struct ofono_watchlist *datagram_handlers;
91 };
92
93 struct pending_pdu {
94         unsigned char pdu[176];
95         int tpdu_len;
96         int pdu_len;
97 };
98
99 struct tx_queue_entry {
100         struct pending_pdu *pdus;
101         unsigned char num_pdus;
102         unsigned char cur_pdu;
103         struct sms_address receiver;
104         struct ofono_uuid uuid;
105         unsigned int retry;
106         unsigned int flags;
107         ofono_sms_txq_submit_cb_t cb;
108         void *data;
109         ofono_destroy_func destroy;
110         unsigned long id;
111 };
112
113 static gboolean uuid_equal(gconstpointer v1, gconstpointer v2)
114 {
115         return memcmp(v1, v2, OFONO_SHA1_UUID_LEN) == 0;
116 }
117
118 static gboolean port_equal(int received, int expected)
119 {
120         return expected == -1 || received == expected;
121 }
122
123 static guint uuid_hash(gconstpointer v)
124 {
125         const struct ofono_uuid *uuid = v;
126         guint h;
127
128         memcpy(&h, uuid->uuid, sizeof(h));
129
130         return h;
131 }
132
133 static const char *sms_bearer_to_string(int bearer)
134 {
135         switch (bearer) {
136         case 0:
137                 return "ps-only";
138         case 1:
139                 return "cs-only";
140         case 2:
141                 return "ps-preferred";
142         case 3:
143                 return "cs-preferred";
144         };
145
146         return NULL;
147 }
148
149 static gboolean sms_bearer_from_string(const char *str, int *bearer)
150 {
151         if (g_str_equal(str, "ps-only"))
152                 *bearer = 0;
153         else if (g_str_equal(str, "cs-only"))
154                 *bearer = 1;
155         else if (g_str_equal(str, "ps-preferred"))
156                 *bearer = 2;
157         else if (g_str_equal(str, "cs-preferred"))
158                 *bearer = 3;
159         else
160                 return FALSE;
161
162         return TRUE;
163 }
164
165 static const char *sms_alphabet_to_string(enum sms_alphabet alphabet)
166 {
167         switch (alphabet) {
168         case SMS_ALPHABET_TURKISH:
169                 return "turkish";
170         case SMS_ALPHABET_SPANISH:
171                 return "spanish";
172         case SMS_ALPHABET_PORTUGUESE:
173                 return "portuguese";
174         case SMS_ALPHABET_DEFAULT:
175                 return "default";
176         }
177
178         return NULL;
179 }
180
181 static gboolean sms_alphabet_from_string(const char *str,
182                                                 enum sms_alphabet *alphabet)
183 {
184         if (g_str_equal(str, "default"))
185                 *alphabet = SMS_ALPHABET_DEFAULT;
186         else if (g_str_equal(str, "turkish"))
187                 *alphabet = SMS_ALPHABET_TURKISH;
188         else if (g_str_equal(str, "spanish"))
189                 *alphabet = SMS_ALPHABET_SPANISH;
190         else if (g_str_equal(str, "portuguese"))
191                 *alphabet = SMS_ALPHABET_PORTUGUESE;
192         else
193                 return FALSE;
194
195         return TRUE;
196 }
197
198 static unsigned int add_sms_handler(struct ofono_watchlist *watchlist,
199                                         int dst, int src, void *notify,
200                                         void *data, ofono_destroy_func destroy)
201 {
202         struct sms_handler *handler;
203
204         if (notify == NULL)
205                 return 0;
206
207         handler = g_try_new0(struct sms_handler, 1);
208         if (handler == NULL)
209                 return 0;
210
211         handler->dst = dst;
212         handler->src = src;
213         handler->item.notify = notify;
214         handler->item.notify_data = data;
215         handler->item.destroy = destroy;
216
217         return __ofono_watchlist_add_item(watchlist,
218                                 (struct ofono_watchlist_item *) handler);
219 }
220
221 unsigned int __ofono_sms_text_watch_add(struct ofono_sms *sms,
222                                         ofono_sms_text_notify_cb_t cb,
223                                         void *data, ofono_destroy_func destroy)
224 {
225         if (sms == NULL)
226                 return 0;
227
228         DBG("%p", sms);
229
230         return add_sms_handler(sms->text_handlers, -1, -1, cb, data, destroy);
231 }
232
233 gboolean __ofono_sms_text_watch_remove(struct ofono_sms *sms,
234                                         unsigned int id)
235 {
236         if (sms == NULL)
237                 return FALSE;
238
239         DBG("%p", sms);
240
241         return __ofono_watchlist_remove_item(sms->text_handlers, id);
242 }
243
244 unsigned int __ofono_sms_datagram_watch_add(struct ofono_sms *sms,
245                                         ofono_sms_datagram_notify_cb_t cb,
246                                         int dst, int src, void *data,
247                                         ofono_destroy_func destroy)
248 {
249         if (sms == NULL)
250                 return 0;
251
252         DBG("%p: dst %d, src %d", sms, dst, src);
253
254         return add_sms_handler(sms->datagram_handlers, dst, src, cb, data,
255                                 destroy);
256 }
257
258 gboolean __ofono_sms_datagram_watch_remove(struct ofono_sms *sms,
259                                         unsigned int id)
260 {
261         if (sms == NULL)
262                 return FALSE;
263
264         DBG("%p", sms);
265
266         return __ofono_watchlist_remove_item(sms->datagram_handlers, id);
267 }
268
269 const char *__ofono_sms_message_path_from_uuid(struct ofono_sms *sms,
270                                                 const struct ofono_uuid *uuid)
271 {
272         return message_path_from_uuid(sms->atom, uuid);
273 }
274
275 static void set_bearer(struct ofono_sms *sms, int bearer)
276 {
277         DBusConnection *conn = ofono_dbus_get_connection();
278         const char *path = __ofono_atom_get_path(sms->atom);
279         const char *value;
280
281         if (sms->bearer == bearer)
282                 return;
283
284         sms->bearer = bearer;
285
286         value = sms_bearer_to_string(sms->bearer);
287
288         ofono_dbus_signal_property_changed(conn, path,
289                                                 OFONO_MESSAGE_MANAGER_INTERFACE,
290                                                 "Bearer",
291                                                 DBUS_TYPE_STRING, &value);
292 }
293
294 static void set_alphabet(struct ofono_sms *sms, enum sms_alphabet alphabet)
295 {
296         DBusConnection *conn = ofono_dbus_get_connection();
297         const char *path = __ofono_atom_get_path(sms->atom);
298         const char *value;
299
300         if (sms->alphabet == alphabet)
301                 return;
302
303         sms->alphabet = alphabet;
304
305         value = sms_alphabet_to_string(sms->alphabet);
306
307         ofono_dbus_signal_property_changed(conn, path,
308                                                 OFONO_MESSAGE_MANAGER_INTERFACE,
309                                                 "Alphabet",
310                                                 DBUS_TYPE_STRING, &value);
311 }
312
313 static void set_sca(struct ofono_sms *sms,
314                         const struct ofono_phone_number *sca)
315 {
316         DBusConnection *conn = ofono_dbus_get_connection();
317         const char *path = __ofono_atom_get_path(sms->atom);
318         const char *value;
319
320         if (sms->sca.type == sca->type &&
321                         !strcmp(sms->sca.number, sca->number))
322                 return;
323
324         sms->sca.type = sca->type;
325         strncpy(sms->sca.number, sca->number, OFONO_MAX_PHONE_NUMBER_LENGTH);
326         sms->sca.number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
327
328         value = phone_number_to_string(&sms->sca);
329
330         ofono_dbus_signal_property_changed(conn, path,
331                                                 OFONO_MESSAGE_MANAGER_INTERFACE,
332                                                 "ServiceCenterAddress",
333                                                 DBUS_TYPE_STRING, &value);
334 }
335
336 static DBusMessage *generate_get_properties_reply(struct ofono_sms *sms,
337                                                         DBusMessage *msg)
338 {
339         DBusMessage *reply;
340         DBusMessageIter iter;
341         DBusMessageIter dict;
342         const char *sca;
343         const char *bearer;
344         const char *alphabet;
345
346         reply = dbus_message_new_method_return(msg);
347         if (reply == NULL)
348                 return NULL;
349
350         dbus_message_iter_init_append(reply, &iter);
351
352         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
353                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
354                                                 &dict);
355
356         sca = phone_number_to_string(&sms->sca);
357
358         ofono_dbus_dict_append(&dict, "ServiceCenterAddress", DBUS_TYPE_STRING,
359                                 &sca);
360
361         ofono_dbus_dict_append(&dict, "UseDeliveryReports", DBUS_TYPE_BOOLEAN,
362                                 &sms->use_delivery_reports);
363
364         bearer = sms_bearer_to_string(sms->bearer);
365         ofono_dbus_dict_append(&dict, "Bearer", DBUS_TYPE_STRING, &bearer);
366
367         alphabet = sms_alphabet_to_string(sms->alphabet);
368         ofono_dbus_dict_append(&dict, "Alphabet", DBUS_TYPE_STRING, &alphabet);
369
370         dbus_message_iter_close_container(&iter, &dict);
371
372         return reply;
373 }
374
375 static void sms_sca_query_cb(const struct ofono_error *error,
376                                 const struct ofono_phone_number *sca,
377                                 void *data)
378 {
379         struct ofono_sms *sms = data;
380
381         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
382                 goto out;
383
384         set_sca(sms, sca);
385
386         sms->flags |= MESSAGE_MANAGER_FLAG_CACHED;
387
388 out:
389         if (sms->pending) {
390                 DBusMessage *reply = generate_get_properties_reply(sms,
391                                                                 sms->pending);
392                 __ofono_dbus_pending_reply(&sms->pending, reply);
393         }
394 }
395
396 static DBusMessage *sms_get_properties(DBusConnection *conn,
397                                         DBusMessage *msg, void *data)
398 {
399         struct ofono_sms *sms = data;
400
401         if (sms->flags & MESSAGE_MANAGER_FLAG_CACHED)
402                 return generate_get_properties_reply(sms, msg);
403
404         if (sms->pending)
405                 return __ofono_error_busy(msg);
406
407         if (sms->driver->sca_query == NULL)
408                 return __ofono_error_not_implemented(msg);
409
410         sms->pending = dbus_message_ref(msg);
411
412         sms->driver->sca_query(sms, sms_sca_query_cb, sms);
413
414         return NULL;
415 }
416
417 static void bearer_set_query_callback(const struct ofono_error *error,
418                                         int bearer, void *data)
419 {
420         struct ofono_sms *sms = data;
421         DBusMessage *reply;
422
423         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
424                 ofono_error("Set Bearer succeeded, but query failed");
425                 reply = __ofono_error_failed(sms->pending);
426                 __ofono_dbus_pending_reply(&sms->pending, reply);
427                 return;
428         }
429
430         reply = dbus_message_new_method_return(sms->pending);
431         __ofono_dbus_pending_reply(&sms->pending, reply);
432
433         set_bearer(sms, bearer);
434 }
435
436 static void bearer_set_callback(const struct ofono_error *error, void *data)
437 {
438         struct ofono_sms *sms = data;
439
440         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
441                 DBG("Setting Bearer failed");
442                 __ofono_dbus_pending_reply(&sms->pending,
443                                         __ofono_error_failed(sms->pending));
444                 return;
445         }
446
447         sms->driver->bearer_query(sms, bearer_set_query_callback, sms);
448 }
449
450 static void sca_set_query_callback(const struct ofono_error *error,
451                                         const struct ofono_phone_number *sca,
452                                         void *data)
453 {
454         struct ofono_sms *sms = data;
455         DBusMessage *reply;
456
457         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
458                 ofono_error("Set SCA succeeded, but query failed");
459                 sms->flags &= ~MESSAGE_MANAGER_FLAG_CACHED;
460                 reply = __ofono_error_failed(sms->pending);
461                 __ofono_dbus_pending_reply(&sms->pending, reply);
462                 return;
463         }
464
465         set_sca(sms, sca);
466
467         reply = dbus_message_new_method_return(sms->pending);
468         __ofono_dbus_pending_reply(&sms->pending, reply);
469 }
470
471 static void sca_set_callback(const struct ofono_error *error, void *data)
472 {
473         struct ofono_sms *sms = data;
474
475         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
476                 DBG("Setting SCA failed");
477                 __ofono_dbus_pending_reply(&sms->pending,
478                                         __ofono_error_failed(sms->pending));
479                 return;
480         }
481
482         sms->driver->sca_query(sms, sca_set_query_callback, sms);
483 }
484
485 static DBusMessage *sms_set_property(DBusConnection *conn, DBusMessage *msg,
486                                         void *data)
487 {
488         struct ofono_sms *sms = data;
489         DBusMessageIter iter;
490         DBusMessageIter var;
491         const char *property;
492
493         if (sms->pending)
494                 return __ofono_error_busy(msg);
495
496         if (!dbus_message_iter_init(msg, &iter))
497                 return __ofono_error_invalid_args(msg);
498
499         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
500                 return __ofono_error_invalid_args(msg);
501
502         dbus_message_iter_get_basic(&iter, &property);
503         dbus_message_iter_next(&iter);
504
505         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
506                 return __ofono_error_invalid_args(msg);
507
508         dbus_message_iter_recurse(&iter, &var);
509
510         if (!strcmp(property, "ServiceCenterAddress")) {
511                 const char *value;
512                 struct ofono_phone_number sca;
513
514                 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
515                         return __ofono_error_invalid_args(msg);
516
517                 dbus_message_iter_get_basic(&var, &value);
518
519                 if (strlen(value) == 0 || !valid_phone_number_format(value))
520                         return __ofono_error_invalid_format(msg);
521
522                 if (sms->driver->sca_set == NULL ||
523                                 sms->driver->sca_query == NULL)
524                         return __ofono_error_not_implemented(msg);
525
526                 string_to_phone_number(value, &sca);
527
528                 sms->pending = dbus_message_ref(msg);
529
530                 sms->driver->sca_set(sms, &sca, sca_set_callback, sms);
531                 return NULL;
532         }
533
534         if (!strcmp(property, "Bearer")) {
535                 const char *value;
536                 int bearer;
537
538                 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
539                         return __ofono_error_invalid_args(msg);
540
541                 dbus_message_iter_get_basic(&var, &value);
542
543                 if (sms_bearer_from_string(value, &bearer) != TRUE)
544                         return __ofono_error_invalid_format(msg);
545
546                 if (sms->driver->bearer_set == NULL ||
547                                 sms->driver->bearer_query == NULL)
548                         return __ofono_error_not_implemented(msg);
549
550                 sms->pending = dbus_message_ref(msg);
551
552                 sms->driver->bearer_set(sms, bearer, bearer_set_callback, sms);
553                 return NULL;
554         }
555
556         if (!strcmp(property, "UseDeliveryReports")) {
557                 const char *path = __ofono_atom_get_path(sms->atom);
558                 dbus_bool_t value;
559
560                 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
561                         return __ofono_error_invalid_args(msg);
562
563                 dbus_message_iter_get_basic(&var, &value);
564
565                 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
566
567                 if (sms->use_delivery_reports != (ofono_bool_t) value) {
568                         sms->use_delivery_reports = value;
569                         ofono_dbus_signal_property_changed(conn, path,
570                                                 OFONO_MESSAGE_MANAGER_INTERFACE,
571                                                 "UseDeliveryReports",
572                                                 DBUS_TYPE_BOOLEAN, &value);
573                 }
574
575                 return NULL;
576         }
577
578         if (!strcmp(property, "Alphabet")) {
579                 const char *value;
580                 enum sms_alphabet alphabet;
581
582                 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
583                         return __ofono_error_invalid_args(msg);
584
585                 dbus_message_iter_get_basic(&var, &value);
586
587                 if (!sms_alphabet_from_string(value, &alphabet))
588                         return __ofono_error_invalid_format(msg);
589
590                 set_alphabet(sms, alphabet);
591
592                 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
593                 return NULL;
594         }
595
596         return __ofono_error_invalid_args(msg);
597 }
598
599 /*
600  * Destroy/release the contents of a 'struct tx_queue_entry'
601  *
602  * This releases resources allocated *inside* @entry and @entry
603  * itself.
604  */
605 static void tx_queue_entry_destroy(struct tx_queue_entry *entry)
606 {
607         if (entry->destroy)
608                 entry->destroy(entry->data);
609
610         g_free(entry->pdus);
611         g_free(entry);
612 }
613
614 static void tx_queue_entry_destroy_foreach(gpointer _entry, gpointer unused)
615 {
616         tx_queue_entry_destroy(_entry);
617 }
618
619 static void sms_tx_queue_remove_entry(struct ofono_sms *sms, GList *entry_list,
620                                         enum message_state tx_state)
621 {
622         struct tx_queue_entry *entry = entry_list->data;
623         struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
624
625         g_queue_delete_link(sms->txq, entry_list);
626
627         DBG("%p", entry);
628
629         if (entry->cb)
630                 entry->cb(tx_state == MESSAGE_STATE_SENT, entry->data);
631
632         if (entry->flags & OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY) {
633                 enum ofono_history_sms_status hs;
634
635                 switch(tx_state) {
636                 case MESSAGE_STATE_SENT:
637                         hs = OFONO_HISTORY_SMS_STATUS_SUBMITTED;
638                         break;
639                 case MESSAGE_STATE_FAILED:
640                         hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_FAILED;
641                         break;
642                 case MESSAGE_STATE_CANCELLED:
643                         hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_CANCELLED;
644                         break;
645                 default:
646                         ofono_error("Unexpected sms state %d", tx_state);
647                         goto done;
648                 }
649
650                 __ofono_history_sms_send_status(modem, &entry->uuid,
651                                                                 time(NULL), hs);
652         }
653
654         if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
655                 struct message *m;
656
657                 sms_tx_backup_free(sms->imsi, entry->id, entry->flags,
658                                         ofono_uuid_to_str(&entry->uuid));
659
660                 m = g_hash_table_lookup(sms->messages, &entry->uuid);
661
662                 if (m != NULL) {
663                         message_set_state(m, tx_state);
664                         g_hash_table_remove(sms->messages, &entry->uuid);
665                         message_emit_removed(m,
666                                         OFONO_MESSAGE_MANAGER_INTERFACE);
667                         message_dbus_unregister(m);
668                 }
669         }
670
671 done:
672         tx_queue_entry_destroy(entry);
673 }
674
675 static void tx_finished(const struct ofono_error *error, int mr, void *data)
676 {
677         struct ofono_sms *sms = data;
678         struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
679         gboolean ok = error->type == OFONO_ERROR_TYPE_NO_ERROR;
680         enum message_state tx_state;
681
682         DBG("tx_finished %p", entry);
683
684         sms->flags &= ~MESSAGE_MANAGER_FLAG_TXQ_ACTIVE;
685
686         if (ok == FALSE) {
687                 /* Retry again when back in online mode */
688                 /* Note this does not increment retry count */
689                 if (sms->registered == FALSE)
690                         return;
691
692                 tx_state = MESSAGE_STATE_FAILED;
693
694                 /* Retry done only for Network Timeout failure */
695                 if (error->type == OFONO_ERROR_TYPE_CMS &&
696                                 error->error != NETWORK_TIMEOUT)
697                         goto next_q;
698
699                 if (!(entry->flags & OFONO_SMS_SUBMIT_FLAG_RETRY))
700                         goto next_q;
701
702                 entry->retry += 1;
703
704                 if (entry->retry < TXQ_MAX_RETRIES) {
705                         DBG("Sending failed, retry in %d secs",
706                                         entry->retry * 5);
707                         sms->tx_source = g_timeout_add_seconds(entry->retry * 5,
708                                                                 tx_next, sms);
709                         return;
710                 }
711
712                 DBG("Max retries reached, giving up");
713                 goto next_q;
714         }
715
716         if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS)
717                 sms_tx_backup_remove(sms->imsi, entry->id, entry->flags,
718                                                 ofono_uuid_to_str(&entry->uuid),
719                                                 entry->cur_pdu);
720
721         entry->cur_pdu += 1;
722         entry->retry = 0;
723
724         if (entry->flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR)
725                 status_report_assembly_add_fragment(sms->sr_assembly,
726                                                         entry->uuid.uuid,
727                                                         &entry->receiver,
728                                                         mr, time(NULL),
729                                                         entry->num_pdus);
730
731         if (entry->cur_pdu < entry->num_pdus) {
732                 sms->tx_source = g_timeout_add(0, tx_next, sms);
733                 return;
734         }
735
736         tx_state = MESSAGE_STATE_SENT;
737
738 next_q:
739         sms_tx_queue_remove_entry(sms, g_queue_peek_head_link(sms->txq),
740                                         tx_state);
741
742         if (sms->registered == FALSE)
743                 return;
744
745         if (g_queue_peek_head(sms->txq)) {
746                 DBG("Scheduling next");
747                 sms->tx_source = g_timeout_add(0, tx_next, sms);
748         }
749 }
750
751 static gboolean tx_next(gpointer user_data)
752 {
753         struct ofono_sms *sms = user_data;
754         int send_mms = 0;
755         struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
756         struct pending_pdu *pdu = &entry->pdus[entry->cur_pdu];
757
758         DBG("tx_next: %p", entry);
759
760         sms->tx_source = 0;
761
762         if (sms->registered == FALSE)
763                 return FALSE;
764
765         if (g_queue_get_length(sms->txq) > 1
766                         || (entry->num_pdus - entry->cur_pdu) > 1)
767                 send_mms = 1;
768
769         sms->flags |= MESSAGE_MANAGER_FLAG_TXQ_ACTIVE;
770
771         sms->driver->submit(sms, pdu->pdu, pdu->pdu_len, pdu->tpdu_len,
772                                 send_mms, tx_finished, sms);
773
774         return FALSE;
775 }
776
777 static void netreg_status_watch(int status, int lac, int ci, int tech,
778                                         const char *mcc, const char *mnc,
779                                         void *data)
780 {
781         struct ofono_sms *sms = data;
782
783         switch (status) {
784         case NETWORK_REGISTRATION_STATUS_REGISTERED:
785         case NETWORK_REGISTRATION_STATUS_ROAMING:
786                 sms->registered = TRUE;
787                 break;
788         default:
789                 sms->registered = FALSE;
790                 break;
791         }
792
793         if (sms->registered == FALSE)
794                 return;
795
796         if (sms->tx_source > 0)
797                 return;
798
799         if (g_queue_get_length(sms->txq))
800                 sms->tx_source = g_timeout_add(0, tx_next, sms);
801 }
802
803 static void netreg_watch(struct ofono_atom *atom,
804                                 enum ofono_atom_watch_condition cond,
805                                 void *data)
806 {
807         struct ofono_sms *sms = data;
808         int status;
809
810         if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
811                 sms->registered = FALSE;
812                 sms->status_watch = 0;
813                 sms->netreg = NULL;
814                 return;
815         }
816
817         sms->netreg = __ofono_atom_get_data(atom);
818         sms->status_watch = __ofono_netreg_add_status_watch(sms->netreg,
819                                         netreg_status_watch, sms, NULL);
820
821         status = ofono_netreg_get_status(sms->netreg);
822         netreg_status_watch(status, 0, 0, 0, NULL, NULL, sms);
823 }
824
825
826 /**
827  * Generate a UUID from an SMS PDU List
828  *
829  * @param pdu Pointer to array of PDUs data to generate the ID from
830  * @param pdus Number of entries in the \e pdu array
831  * @return 0 in error (no memory or serious code inconsistency in the
832  *     input data structures), otherwise the SMS UUID.
833  *
834  * @internal
835  *
836  * The current time is added to avoid the UUID being the same when the
837  * same message is sent to the same destination repeatedly. Note we
838  * need a high resolution time (not just seconds), otherwise resending
839  * in the same second (not that rare) could yield the same UUID.
840  */
841 static gboolean sms_uuid_from_pdus(const struct pending_pdu *pdu,
842                                         unsigned char pdus,
843                                         struct ofono_uuid *uuid)
844
845 {
846         GChecksum *checksum;
847         gsize uuid_size = sizeof(uuid->uuid);
848         unsigned int cnt;
849         struct timeval now;
850
851         checksum = g_checksum_new(G_CHECKSUM_SHA1);
852         if (checksum == NULL)
853                 return FALSE;
854
855         for (cnt = 0; cnt < pdus; cnt++)
856                 g_checksum_update(checksum, pdu[cnt].pdu, pdu[cnt].pdu_len);
857
858         gettimeofday(&now, NULL);
859         g_checksum_update(checksum, (void *) &now, sizeof(now));
860
861         g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
862         g_checksum_free(checksum);
863
864         return TRUE;
865 }
866
867 static struct tx_queue_entry *tx_queue_entry_new(GSList *msg_list,
868                                                         unsigned int flags)
869 {
870         struct tx_queue_entry *entry;
871         int i = 0;
872         GSList *l;
873
874         entry = g_try_new0(struct tx_queue_entry, 1);
875         if (entry == NULL)
876                 return NULL;
877
878         entry->num_pdus = g_slist_length(msg_list);
879
880         entry->pdus = g_try_new0(struct pending_pdu, entry->num_pdus);
881         if (entry->pdus == NULL)
882                 goto error;
883
884         if (flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR) {
885                 struct sms *head = msg_list->data;
886
887                 memcpy(&entry->receiver, &head->submit.daddr,
888                                 sizeof(entry->receiver));
889         }
890
891         entry->flags = flags;
892
893         for (l = msg_list; l; l = l->next) {
894                 struct pending_pdu *pdu = &entry->pdus[i++];
895                 struct sms *s = l->data;
896
897                 sms_encode(s, &pdu->pdu_len, &pdu->tpdu_len, pdu->pdu);
898
899                 DBG("pdu_len: %d, tpdu_len: %d",
900                                 pdu->pdu_len, pdu->tpdu_len);
901         }
902
903         if (flags & OFONO_SMS_SUBMIT_FLAG_REUSE_UUID)
904                 return entry;
905
906         if (sms_uuid_from_pdus(entry->pdus, entry->num_pdus, &entry->uuid))
907                 return entry;
908
909 error:
910         g_free(entry->pdus);
911         g_free(entry);
912
913         return NULL;
914 }
915
916 static void tx_queue_entry_set_submit_notify(struct tx_queue_entry *entry,
917                                                 ofono_sms_txq_submit_cb_t cb,
918                                                 void *data,
919                                                 ofono_destroy_func destroy)
920 {
921         entry->cb = cb;
922         entry->data = data;
923         entry->destroy = destroy;
924 }
925
926 static void message_queued(struct ofono_sms *sms,
927                                 const struct ofono_uuid *uuid, void *data)
928 {
929         DBusConnection *conn = ofono_dbus_get_connection();
930         DBusMessage *msg = data;
931         const char *path;
932
933         path = __ofono_sms_message_path_from_uuid(sms, uuid);
934         g_dbus_send_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &path,
935                                         DBUS_TYPE_INVALID);
936 }
937
938 /*
939  * Pre-process a SMS text message and deliver it [D-Bus SendMessage()]
940  *
941  * @conn: D-Bus connection
942  * @msg: message data (telephone number and text)
943  * @data: SMS object to use for transmision
944  *
945  * An alphabet is chosen for the text and it (might be) segmented in
946  * fragments by sms_text_prepare() into @msg_list. A queue list @entry
947  * is created by tx_queue_entry_new() and g_queue_push_tail()
948  * appends that entry to the SMS transmit queue. Then the tx_next()
949  * function is scheduled to run to process the queue.
950  */
951 static DBusMessage *sms_send_message(DBusConnection *conn, DBusMessage *msg,
952                                         void *data)
953 {
954         struct ofono_sms *sms = data;
955         const char *to;
956         const char *text;
957         GSList *msg_list;
958         struct ofono_modem *modem;
959         unsigned int flags;
960         gboolean use_16bit_ref = FALSE;
961         int err;
962         struct ofono_uuid uuid;
963
964         if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &to,
965                                         DBUS_TYPE_STRING, &text,
966                                         DBUS_TYPE_INVALID))
967                 return __ofono_error_invalid_args(msg);
968
969         if (valid_phone_number_format(to) == FALSE)
970                 return __ofono_error_invalid_format(msg);
971
972         msg_list = sms_text_prepare_with_alphabet(to, text, sms->ref,
973                                                 use_16bit_ref,
974                                                 sms->use_delivery_reports,
975                                                 sms->alphabet);
976
977         if (msg_list == NULL)
978                 return __ofono_error_invalid_format(msg);
979
980         flags = OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY;
981         flags |= OFONO_SMS_SUBMIT_FLAG_RETRY;
982         flags |= OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS;
983         if (sms->use_delivery_reports)
984                 flags |= OFONO_SMS_SUBMIT_FLAG_REQUEST_SR;
985
986         err = __ofono_sms_txq_submit(sms, msg_list, flags, &uuid,
987                                         message_queued, msg);
988
989         g_slist_foreach(msg_list, (GFunc) g_free, NULL);
990         g_slist_free(msg_list);
991
992         if (err < 0)
993                 return __ofono_error_failed(msg);
994
995         modem = __ofono_atom_get_modem(sms->atom);
996         __ofono_history_sms_send_pending(modem, &uuid, to, time(NULL), text);
997
998         return NULL;
999 }
1000
1001 static DBusMessage *sms_get_messages(DBusConnection *conn, DBusMessage *msg,
1002                                         void *data)
1003 {
1004         struct ofono_sms *sms = data;
1005         DBusMessage *reply;
1006         DBusMessageIter iter;
1007         DBusMessageIter array;
1008         DBusMessageIter entry, dict;
1009         const char *path;
1010         GHashTableIter hashiter;
1011         gpointer key, value;
1012         struct message *m;
1013         const struct ofono_uuid *uuid;
1014
1015         reply = dbus_message_new_method_return(msg);
1016         if (reply == NULL)
1017                 return NULL;
1018
1019         dbus_message_iter_init_append(reply, &iter);
1020
1021         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1022                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
1023                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1024                                         DBUS_TYPE_ARRAY_AS_STRING
1025                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1026                                         DBUS_TYPE_STRING_AS_STRING
1027                                         DBUS_TYPE_VARIANT_AS_STRING
1028                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1029                                         DBUS_STRUCT_END_CHAR_AS_STRING,
1030                                         &array);
1031
1032         g_hash_table_iter_init(&hashiter, sms->messages);
1033
1034         while (g_hash_table_iter_next(&hashiter, &key, &value)) {
1035                 m = value;
1036                 uuid = message_get_uuid(m);
1037
1038                 path = __ofono_sms_message_path_from_uuid(sms, uuid);
1039
1040                 dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
1041                                                         NULL, &entry);
1042                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1043                                                 &path);
1044                 dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
1045                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
1046                                         &dict);
1047
1048                 message_append_properties(m, &dict);
1049                 dbus_message_iter_close_container(&entry, &dict);
1050                 dbus_message_iter_close_container(&array, &entry);
1051         }
1052
1053         dbus_message_iter_close_container(&iter, &array);
1054
1055         return reply;
1056 }
1057
1058 static gint entry_compare_by_uuid(gconstpointer a, gconstpointer b)
1059 {
1060         const struct tx_queue_entry *entry = a;
1061         const struct ofono_uuid *uuid = b;
1062
1063         return memcmp(&entry->uuid, uuid, sizeof(entry->uuid));
1064 }
1065
1066 int __ofono_sms_txq_cancel(struct ofono_sms *sms, const struct ofono_uuid *uuid)
1067 {
1068         GList *l;
1069         struct tx_queue_entry *entry;
1070
1071         l = g_queue_find_custom(sms->txq, uuid, entry_compare_by_uuid);
1072
1073         if (l == NULL)
1074                 return -ENOENT;
1075
1076         entry = l->data;
1077
1078         if (entry == g_queue_peek_head(sms->txq)) {
1079                 /*
1080                  * Fail if any pdu was already transmitted or if we are
1081                  * waiting the answer from driver.
1082                  */
1083                 if (entry->cur_pdu > 0)
1084                         return -EPERM;
1085
1086                 if (sms->flags & MESSAGE_MANAGER_FLAG_TXQ_ACTIVE)
1087                         return -EPERM;
1088                 /*
1089                  * Make sure we don't call tx_next() if there are no entries
1090                  * and that next entry doesn't have to wait a 'retry time'
1091                  * from this one.
1092                  */
1093                 if (sms->tx_source) {
1094                         g_source_remove(sms->tx_source);
1095                         sms->tx_source = 0;
1096
1097                         if (g_queue_get_length(sms->txq) > 1)
1098                                 sms->tx_source = g_timeout_add(0, tx_next, sms);
1099                 }
1100         }
1101
1102         sms_tx_queue_remove_entry(sms, l, MESSAGE_STATE_CANCELLED);
1103
1104         return 0;
1105 }
1106
1107 static GDBusMethodTable sms_manager_methods[] = {
1108         { "GetProperties",    "",    "a{sv}",        sms_get_properties,
1109                                                 G_DBUS_METHOD_FLAG_ASYNC },
1110         { "SetProperty",      "sv",  "",             sms_set_property,
1111                                                 G_DBUS_METHOD_FLAG_ASYNC },
1112         { "SendMessage",      "ss",  "o",             sms_send_message,
1113                                                 G_DBUS_METHOD_FLAG_ASYNC },
1114         { "GetMessages",       "",    "a(oa{sv})",    sms_get_messages },
1115         { }
1116 };
1117
1118 static GDBusSignalTable sms_manager_signals[] = {
1119         { "PropertyChanged",    "sv"            },
1120         { "IncomingMessage",    "sa{sv}"        },
1121         { "ImmediateMessage",   "sa{sv}"        },
1122         { "MessageAdded",       "oa{sv}"        },
1123         { "MessageRemoved",     "o"             },
1124         { }
1125 };
1126
1127 static gboolean compute_incoming_msgid(GSList *sms_list,
1128                                                 struct ofono_uuid *uuid)
1129 {
1130         GChecksum *checksum;
1131         GSList *l;
1132         const struct sms *s;
1133         unsigned char buf[176];
1134         gsize uuid_size = sizeof(uuid->uuid);
1135         int len;
1136
1137         checksum = g_checksum_new(G_CHECKSUM_SHA1);
1138         if (checksum == NULL)
1139                 return FALSE;
1140
1141         for (l = sms_list; l; l = l->next) {
1142                 s = l->data;
1143
1144                 if (sms_encode(s, &len, NULL, buf) == FALSE) {
1145                         g_checksum_free(checksum);
1146                         return FALSE;
1147                 }
1148
1149                 g_checksum_update(checksum, buf, len);
1150         }
1151
1152         g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
1153         g_checksum_free(checksum);
1154
1155         return TRUE;
1156 }
1157
1158 static void dispatch_app_datagram(struct ofono_sms *sms,
1159                                         const struct ofono_uuid *uuid,
1160                                         int dst, int src,
1161                                         unsigned char *buf, unsigned len,
1162                                         const struct sms_address *addr,
1163                                         const struct sms_scts *scts)
1164 {
1165         const char *sender = sms_address_to_string(addr);
1166         time_t ts;
1167         struct tm remote;
1168         struct tm local;
1169
1170         ofono_sms_datagram_notify_cb_t notify;
1171         struct sms_handler *h;
1172         GSList *l;
1173
1174         ts = sms_scts_to_time(scts, &remote);
1175         localtime_r(&ts, &local);
1176
1177         for (l = sms->datagram_handlers->items; l; l = l->next) {
1178                 h = l->data;
1179                 notify = h->item.notify;
1180
1181                 if (!port_equal(dst, h->dst) || !port_equal(src, h->src))
1182                         continue;
1183
1184                 notify(sender, &remote, &local, dst, src, buf, len,
1185                         h->item.notify_data);
1186         }
1187 }
1188
1189 static void dispatch_text_message(struct ofono_sms *sms,
1190                                         const struct ofono_uuid *uuid,
1191                                         const char *message,
1192                                         enum sms_class cls,
1193                                         const struct sms_address *addr,
1194                                         const struct sms_scts *scts)
1195 {
1196         struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1197         DBusConnection *conn = ofono_dbus_get_connection();
1198         const char *path = __ofono_atom_get_path(sms->atom);
1199         DBusMessage *signal;
1200         DBusMessageIter iter;
1201         DBusMessageIter dict;
1202         char buf[128];
1203         const char *signal_name;
1204         time_t ts;
1205         struct tm remote;
1206         struct tm local;
1207         const char *str = buf;
1208         ofono_sms_text_notify_cb_t notify;
1209         struct sms_handler *h;
1210         GSList *l;
1211
1212         if (message == NULL)
1213                 return;
1214
1215         if (cls == SMS_CLASS_0)
1216                 signal_name = "ImmediateMessage";
1217         else
1218                 signal_name = "IncomingMessage";
1219
1220         signal = dbus_message_new_signal(path, OFONO_MESSAGE_MANAGER_INTERFACE,
1221                                                 signal_name);
1222
1223         if (signal == NULL)
1224                 return;
1225
1226         dbus_message_iter_init_append(signal, &iter);
1227
1228         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
1229
1230         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1231                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
1232                                                 &dict);
1233
1234         ts = sms_scts_to_time(scts, &remote);
1235         localtime_r(&ts, &local);
1236
1237         strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &local);
1238         buf[127] = '\0';
1239         ofono_dbus_dict_append(&dict, "LocalSentTime", DBUS_TYPE_STRING, &str);
1240
1241         strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &remote);
1242         buf[127] = '\0';
1243         ofono_dbus_dict_append(&dict, "SentTime", DBUS_TYPE_STRING, &str);
1244
1245         str = sms_address_to_string(addr);
1246         ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &str);
1247
1248         dbus_message_iter_close_container(&iter, &dict);
1249
1250         g_dbus_send_message(conn, signal);
1251
1252         if (cls == SMS_CLASS_0)
1253                 return;
1254
1255         for (l = sms->text_handlers->items; l; l = l->next) {
1256                 h = l->data;
1257                 notify = h->item.notify;
1258
1259                 notify(str, &remote, &local, message, h->item.notify_data);
1260         }
1261
1262         __ofono_history_sms_received(modem, uuid, str, &remote, &local,
1263                                         message);
1264 }
1265
1266 static void sms_dispatch(struct ofono_sms *sms, GSList *sms_list)
1267 {
1268         GSList *l;
1269         const struct sms *s;
1270         struct ofono_uuid uuid;
1271         enum sms_charset uninitialized_var(old_charset);
1272         enum sms_class cls;
1273         int srcport = -1;
1274         int dstport = -1;
1275
1276         DBG("");
1277
1278         if (sms_list == NULL)
1279                 return;
1280
1281         /*
1282          * Qutoting 23.040: The TP elements in the SMS‑SUBMIT PDU, apart from
1283          * TP‑MR, TP-SRR, TP‑UDL and TP‑UD, should remain unchanged for each
1284          * SM which forms part of a concatenated SM, otherwise this may lead
1285          * to irrational behaviour
1286          *
1287          * This means that we assume that at least the charset is the same
1288          * across all parts of the SMS in the case of 8-bit data.  Other
1289          * cases can be handled by converting to UTF8.
1290          *
1291          * We also check that if 8-bit or 16-bit application addressing is
1292          * used, the addresses are the same across all segments.
1293          */
1294
1295         for (l = sms_list; l; l = l->next) {
1296                 guint8 dcs;
1297                 gboolean comp = FALSE;
1298                 enum sms_charset charset;
1299                 int cdst = -1;
1300                 int csrc = -1;
1301                 gboolean is_8bit;
1302
1303                 s = l->data;
1304                 dcs = s->deliver.dcs;
1305
1306                 if (sms_mwi_dcs_decode(dcs, NULL, &charset, NULL, NULL))
1307                         cls = SMS_CLASS_UNSPECIFIED;
1308                 else if (!sms_dcs_decode(dcs, &cls, &charset, &comp, NULL)) {
1309                         ofono_error("The deliver DCS is not recognized");
1310                         return;
1311                 }
1312
1313                 if (comp) {
1314                         ofono_error("Compressed data not supported");
1315                         return;
1316                 }
1317
1318                 if (l == sms_list)
1319                         old_charset = charset;
1320
1321                 if (charset == SMS_CHARSET_8BIT && charset != old_charset) {
1322                         ofono_error("Can't concatenate disparate charsets");
1323                         return;
1324                 }
1325
1326                 if (sms_extract_app_port(s, &cdst, &csrc, &is_8bit)) {
1327                         csrc = is_8bit ? (csrc << 16) : csrc;
1328                         cdst = is_8bit ? (cdst << 16) : cdst;
1329
1330                         if (l == sms_list) {
1331                                 srcport = csrc;
1332                                 dstport = cdst;
1333                         }
1334                 }
1335
1336                 DBG("dst %d src %d", cdst, csrc);
1337
1338                 if (srcport != csrc || dstport != cdst) {
1339                         ofono_error("Source / Destination ports across "
1340                                         "concatenated message are not the "
1341                                         "same, ignoring");
1342                         return;
1343                 }
1344         }
1345
1346         if (!compute_incoming_msgid(sms_list, &uuid))
1347                 return;
1348
1349         s = sms_list->data;
1350
1351         /* Handle datagram */
1352         if (old_charset == SMS_CHARSET_8BIT) {
1353                 unsigned char *buf;
1354                 long len;
1355
1356                 if (srcport == -1 || dstport == -1) {
1357                         ofono_error("Got an 8-bit encoded message, however "
1358                                         "no valid src/address port, ignore");
1359                         return;
1360                 }
1361
1362                 buf = sms_decode_datagram(sms_list, &len);
1363                 if (buf == NULL)
1364                         return;
1365
1366                 dispatch_app_datagram(sms, &uuid, dstport, srcport, buf, len,
1367                                         &s->deliver.oaddr, &s->deliver.scts);
1368
1369                 g_free(buf);
1370         } else {
1371                 char *message = sms_decode_text(sms_list);
1372
1373                 if (message == NULL)
1374                         return;
1375
1376                 dispatch_text_message(sms, &uuid, message, cls,
1377                                         &s->deliver.oaddr, &s->deliver.scts);
1378
1379                 g_free(message);
1380         }
1381 }
1382
1383 static void handle_deliver(struct ofono_sms *sms, const struct sms *incoming)
1384 {
1385         GSList *l;
1386         guint16 ref;
1387         guint8 max;
1388         guint8 seq;
1389
1390         DBG("");
1391
1392         if (sms_extract_concatenation(incoming, &ref, &max, &seq)) {
1393                 GSList *sms_list;
1394
1395                 if (sms->assembly == NULL)
1396                         return;
1397
1398                 sms_list = sms_assembly_add_fragment(sms->assembly,
1399                                                 incoming, time(NULL),
1400                                                 &incoming->deliver.oaddr,
1401                                                 ref, max, seq);
1402
1403                 if (sms_list == NULL)
1404                         return;
1405
1406                 sms_dispatch(sms, sms_list);
1407                 g_slist_foreach(sms_list, (GFunc) g_free, NULL);
1408                 g_slist_free(sms_list);
1409
1410                 return;
1411         }
1412
1413         l = g_slist_append(NULL, (void *) incoming);
1414         sms_dispatch(sms, l);
1415         g_slist_free(l);
1416 }
1417
1418 static void handle_sms_status_report(struct ofono_sms *sms,
1419                                                 const struct sms *incoming)
1420 {
1421         struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1422         gboolean delivered;
1423         struct ofono_uuid uuid;
1424
1425         DBG("");
1426
1427         if (status_report_assembly_report(sms->sr_assembly, incoming, uuid.uuid,
1428                                                 &delivered) == FALSE)
1429                 return;
1430
1431         __ofono_history_sms_send_status(modem, &uuid, time(NULL),
1432                         delivered ? OFONO_HISTORY_SMS_STATUS_DELIVERED :
1433                         OFONO_HISTORY_SMS_STATUS_DELIVER_FAILED);
1434 }
1435
1436
1437 static inline gboolean handle_mwi(struct ofono_sms *sms, struct sms *s)
1438 {
1439         gboolean discard;
1440
1441         DBG("");
1442
1443         if (sms->mw == NULL)
1444                 return FALSE;
1445
1446         __ofono_message_waiting_mwi(sms->mw, s, &discard);
1447
1448         return discard;
1449 }
1450
1451 void ofono_sms_deliver_notify(struct ofono_sms *sms, unsigned char *pdu,
1452                                 int len, int tpdu_len)
1453 {
1454         struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1455         struct ofono_atom *stk_atom;
1456         struct ofono_atom *sim_atom;
1457         struct sms s;
1458         enum sms_class cls;
1459
1460         DBG("len %d tpdu len %d", len, tpdu_len);
1461
1462         if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
1463                 ofono_error("Unable to decode PDU");
1464                 return;
1465         }
1466
1467         if (s.type != SMS_TYPE_DELIVER) {
1468                 ofono_error("Expecting a DELIVER pdu");
1469                 return;
1470         }
1471
1472         if (s.deliver.pid == SMS_PID_TYPE_SM_TYPE_0) {
1473                 DBG("Explicitly ignoring type 0 SMS");
1474                 return;
1475         }
1476
1477         /*
1478          * This is an older style MWI notification, process MWI
1479          * headers and handle it like any other message
1480          */
1481         if (s.deliver.pid == SMS_PID_TYPE_RETURN_CALL) {
1482                 if (handle_mwi(sms, &s))
1483                         return;
1484
1485                 goto out;
1486         }
1487
1488         /*
1489          * The DCS indicates this is an MWI notification, process it
1490          * and then handle the User-Data as any other message
1491          */
1492         if (sms_mwi_dcs_decode(s.deliver.dcs, NULL, NULL, NULL, NULL)) {
1493                 if (handle_mwi(sms, &s))
1494                         return;
1495
1496                 goto out;
1497         }
1498
1499         if (!sms_dcs_decode(s.deliver.dcs, &cls, NULL, NULL, NULL)) {
1500                 ofono_error("Unknown / Reserved DCS.  Ignoring");
1501                 return;
1502         }
1503
1504         switch (s.deliver.pid) {
1505         case SMS_PID_TYPE_ME_DOWNLOAD:
1506                 if (cls == SMS_CLASS_1) {
1507                         ofono_error("ME Download message ignored");
1508                         return;
1509                 }
1510
1511                 break;
1512         case SMS_PID_TYPE_ME_DEPERSONALIZATION:
1513                 if (s.deliver.dcs == 0x11) {
1514                         ofono_error("ME Depersonalization message ignored");
1515                         return;
1516                 }
1517
1518                 break;
1519         case SMS_PID_TYPE_USIM_DOWNLOAD:
1520         case SMS_PID_TYPE_ANSI136:
1521                 /* If not Class 2, handle in a "normal" way */
1522                 if (cls != SMS_CLASS_2)
1523                         break;
1524
1525                 sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
1526
1527                 if (sim_atom == NULL)
1528                         return;
1529
1530                 if (!__ofono_sim_service_available(
1531                                         __ofono_atom_get_data(sim_atom),
1532                                         SIM_UST_SERVICE_DATA_DOWNLOAD_SMS_PP,
1533                                         SIM_SST_SERVICE_DATA_DOWNLOAD_SMS_PP))
1534                         return;
1535
1536                 stk_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_STK);
1537
1538                 if (stk_atom == NULL)
1539                         return;
1540
1541                 __ofono_sms_sim_download(__ofono_atom_get_data(stk_atom),
1542                                                 &s, NULL, sms);
1543
1544                 /*
1545                  * Passing the USIM response back to network is not
1546                  * currently supported
1547                  *
1548                  * TODO: store in EFsms if not handled
1549                  */
1550                 return;
1551         default:
1552                 break;
1553         }
1554
1555         /*
1556          * Check to see if the SMS has any other MWI related headers,
1557          * as sometimes they are "tacked on" by the SMSC.
1558          * While we're doing this we also check for messages containing
1559          * WCMP headers or headers that can't possibly be in a normal
1560          * message.  If we find messages like that, we ignore them.
1561          */
1562         if (s.deliver.udhi) {
1563                 struct sms_udh_iter iter;
1564                 enum sms_iei iei;
1565
1566                 if (!sms_udh_iter_init(&s, &iter))
1567                         goto out;
1568
1569                 while ((iei = sms_udh_iter_get_ie_type(&iter)) !=
1570                                 SMS_IEI_INVALID) {
1571                         if (iei > 0x25) {
1572                                 ofono_error("Reserved / Unknown / USAT"
1573                                                 "header in use, ignore");
1574                                 return;
1575                         }
1576
1577                         switch (iei) {
1578                         case SMS_IEI_SPECIAL_MESSAGE_INDICATION:
1579                         case SMS_IEI_ENHANCED_VOICE_MAIL_INFORMATION:
1580                                 /*
1581                                  * TODO: ignore if not in the very first
1582                                  * segment of a concatenated SM so as not
1583                                  * to repeat the indication.
1584                                  */
1585                                 if (handle_mwi(sms, &s))
1586                                         return;
1587
1588                                 goto out;
1589                         case SMS_IEI_WCMP:
1590                                 ofono_error("No support for WCMP, ignoring");
1591                                 return;
1592                         default:
1593                                 sms_udh_iter_next(&iter);
1594                         }
1595                 }
1596         }
1597
1598 out:
1599         handle_deliver(sms, &s);
1600 }
1601
1602 void ofono_sms_status_notify(struct ofono_sms *sms, unsigned char *pdu,
1603                                 int len, int tpdu_len)
1604 {
1605         struct sms s;
1606         enum sms_class cls;
1607
1608         DBG("len %d tpdu len %d", len, tpdu_len);
1609
1610         if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
1611                 ofono_error("Unable to decode PDU");
1612                 return;
1613         }
1614
1615         if (s.type != SMS_TYPE_STATUS_REPORT) {
1616                 ofono_error("Expecting a STATUS REPORT pdu");
1617                 return;
1618         }
1619
1620         if (s.status_report.srq) {
1621                 ofono_error("Waiting an answer to SMS-SUBMIT, not SMS-COMMAND");
1622                 return;
1623         }
1624
1625         if (!sms_dcs_decode(s.status_report.dcs, &cls, NULL, NULL, NULL)) {
1626                 ofono_error("Unknown / Reserved DCS.  Ignoring");
1627                 return;
1628         }
1629
1630         handle_sms_status_report(sms, &s);
1631 }
1632
1633 int ofono_sms_driver_register(const struct ofono_sms_driver *d)
1634 {
1635         DBG("driver: %p, name: %s", d, d->name);
1636
1637         if (d->probe == NULL)
1638                 return -EINVAL;
1639
1640         g_drivers = g_slist_prepend(g_drivers, (void *) d);
1641
1642         return 0;
1643 }
1644
1645 void ofono_sms_driver_unregister(const struct ofono_sms_driver *d)
1646 {
1647         DBG("driver: %p, name: %s", d, d->name);
1648
1649         g_drivers = g_slist_remove(g_drivers, (void *) d);
1650 }
1651
1652 static void sms_unregister(struct ofono_atom *atom)
1653 {
1654         struct ofono_sms *sms = __ofono_atom_get_data(atom);
1655         DBusConnection *conn = ofono_dbus_get_connection();
1656         struct ofono_modem *modem = __ofono_atom_get_modem(atom);
1657         const char *path = __ofono_atom_get_path(atom);
1658
1659         g_dbus_unregister_interface(conn, path,
1660                                         OFONO_MESSAGE_MANAGER_INTERFACE);
1661         ofono_modem_remove_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
1662
1663         if (sms->mw_watch) {
1664                 __ofono_modem_remove_atom_watch(modem, sms->mw_watch);
1665                 sms->mw_watch = 0;
1666                 sms->mw = NULL;
1667         }
1668
1669         if (sms->status_watch) {
1670                 __ofono_netreg_remove_status_watch(sms->netreg,
1671                                                         sms->status_watch);
1672                 sms->status_watch = 0;
1673         }
1674
1675         if (sms->netreg_watch) {
1676                 __ofono_modem_remove_atom_watch(modem, sms->netreg_watch);
1677                 sms->netreg_watch = 0;
1678         }
1679
1680         sms->netreg = NULL;
1681
1682         if (sms->messages) {
1683                 GHashTableIter iter;
1684                 struct message *m;
1685                 gpointer key, value;
1686
1687                 g_hash_table_iter_init(&iter, sms->messages);
1688
1689                 while (g_hash_table_iter_next(&iter, &key, &value)) {
1690                         m = value;
1691                         message_dbus_unregister(m);
1692                 }
1693
1694                 g_hash_table_destroy(sms->messages);
1695                 sms->messages = NULL;
1696         }
1697
1698         __ofono_watchlist_free(sms->text_handlers);
1699         sms->text_handlers = NULL;
1700
1701         __ofono_watchlist_free(sms->datagram_handlers);
1702         sms->datagram_handlers = NULL;
1703 }
1704
1705 static void sms_remove(struct ofono_atom *atom)
1706 {
1707         struct ofono_sms *sms = __ofono_atom_get_data(atom);
1708
1709         DBG("atom: %p", atom);
1710
1711         if (sms == NULL)
1712                 return;
1713
1714         if (sms->driver && sms->driver->remove)
1715                 sms->driver->remove(sms);
1716
1717         if (sms->tx_source) {
1718                 g_source_remove(sms->tx_source);
1719                 sms->tx_source = 0;
1720         }
1721
1722         if (sms->assembly) {
1723                 sms_assembly_free(sms->assembly);
1724                 sms->assembly = NULL;
1725         }
1726
1727         if (sms->txq) {
1728                 g_queue_foreach(sms->txq, tx_queue_entry_destroy_foreach, NULL);
1729                 g_queue_free(sms->txq);
1730                 sms->txq = NULL;
1731         }
1732
1733         if (sms->settings) {
1734                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1735                                         "NextReference", sms->ref);
1736                 g_key_file_set_boolean(sms->settings, SETTINGS_GROUP,
1737                                         "UseDeliveryReports",
1738                                         sms->use_delivery_reports);
1739                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1740                                         "Bearer", sms->bearer);
1741                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1742                                         "Alphabet", sms->alphabet);
1743
1744                 storage_close(sms->imsi, SETTINGS_STORE, sms->settings, TRUE);
1745
1746                 g_free(sms->imsi);
1747                 sms->imsi = NULL;
1748                 sms->settings = NULL;
1749         }
1750
1751         if (sms->sr_assembly) {
1752                 status_report_assembly_free(sms->sr_assembly);
1753                 sms->sr_assembly = NULL;
1754         }
1755
1756         g_free(sms);
1757 }
1758
1759
1760 /*
1761  * Create a SMS driver
1762  *
1763  * This creates a SMS driver that is hung off a @modem
1764  * object. However, for the driver to be used by the system, it has to
1765  * be registered with the oFono core using ofono_sms_register().
1766  *
1767  * This is done once the modem driver determines that SMS is properly
1768  * supported by the hardware.
1769  */
1770 struct ofono_sms *ofono_sms_create(struct ofono_modem *modem,
1771                                         unsigned int vendor,
1772                                         const char *driver,
1773                                         void *data)
1774 {
1775         struct ofono_sms *sms;
1776         GSList *l;
1777
1778         if (driver == NULL)
1779                 return NULL;
1780
1781         sms = g_try_new0(struct ofono_sms, 1);
1782
1783         if (sms == NULL)
1784                 return NULL;
1785
1786         sms->sca.type = 129;
1787         sms->ref = 1;
1788         sms->txq = g_queue_new();
1789         sms->messages = g_hash_table_new(uuid_hash, uuid_equal);
1790
1791         sms->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_SMS,
1792                                                 sms_remove, sms);
1793
1794         for (l = g_drivers; l; l = l->next) {
1795                 const struct ofono_sms_driver *drv = l->data;
1796
1797                 if (g_strcmp0(drv->name, driver))
1798                         continue;
1799
1800                 if (drv->probe(sms, vendor, data) < 0)
1801                         continue;
1802
1803                 sms->driver = drv;
1804                 break;
1805         }
1806
1807         return sms;
1808 }
1809
1810 static void mw_watch(struct ofono_atom *atom,
1811                         enum ofono_atom_watch_condition cond, void *data)
1812 {
1813         struct ofono_sms *sms = data;
1814
1815         if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
1816                 sms->mw = NULL;
1817                 return;
1818         }
1819
1820         sms->mw = __ofono_atom_get_data(atom);
1821 }
1822
1823 static void sms_load_settings(struct ofono_sms *sms, const char *imsi)
1824 {
1825         GError *error;
1826
1827         sms->settings = storage_open(imsi, SETTINGS_STORE);
1828
1829         if (sms->settings == NULL)
1830                 return;
1831
1832         sms->imsi = g_strdup(imsi);
1833
1834         error = NULL;
1835         sms->ref = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
1836                                                 "NextReference", &error);
1837
1838         if (error || sms->ref > 65536) {
1839                 g_error_free(error);
1840                 sms->ref = 1;
1841                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1842                                         "NextReference", sms->ref);
1843         }
1844
1845         error = NULL;
1846         sms->use_delivery_reports =
1847                 g_key_file_get_boolean(sms->settings, SETTINGS_GROUP,
1848                                         "UseDeliveryReports", &error);
1849
1850         if (error) {
1851                 g_error_free(error);
1852                 g_key_file_set_boolean(sms->settings, SETTINGS_GROUP,
1853                                         "UseDeliveryReports",
1854                                         sms->use_delivery_reports);
1855         }
1856
1857         error = NULL;
1858         sms->bearer = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
1859                                                         "Bearer", &error);
1860
1861         if (error || sms_bearer_to_string(sms->bearer) == NULL) {
1862                 g_error_free(error);
1863                 sms->bearer = 3; /* Default to CS then PS */
1864                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1865                                         "Bearer", sms->bearer);
1866         }
1867
1868         error = NULL;
1869         sms->alphabet = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
1870                                                 "Alphabet", &error);
1871
1872         if (error || sms_alphabet_to_string(sms->alphabet) == NULL) {
1873                 g_error_free(error);
1874                 sms->alphabet = SMS_ALPHABET_DEFAULT;
1875                 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1876                                         "Alphabet", sms->alphabet);
1877         }
1878 }
1879
1880 static void bearer_init_callback(const struct ofono_error *error, void *data)
1881 {
1882         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
1883                 ofono_error("Error bootstrapping SMS Bearer Preference");
1884 }
1885
1886 static void sms_restore_tx_queue(struct ofono_sms *sms)
1887 {
1888         GQueue *backupq;
1889         struct txq_backup_entry *backup_entry;
1890
1891         DBG("");
1892
1893         backupq = sms_tx_queue_load(sms->imsi);
1894
1895         if (backupq == NULL)
1896                 return;
1897
1898         while ((backup_entry = g_queue_pop_head(backupq))) {
1899                 struct message *m;
1900                 struct tx_queue_entry *txq_entry;
1901
1902                 backup_entry->flags |= OFONO_SMS_SUBMIT_FLAG_REUSE_UUID;
1903                 txq_entry = tx_queue_entry_new(backup_entry->msg_list,
1904                                                         backup_entry->flags);
1905                 if (txq_entry == NULL)
1906                         goto loop_out;
1907
1908                 txq_entry->flags &= ~OFONO_SMS_SUBMIT_FLAG_REUSE_UUID;
1909                 memcpy(&txq_entry->uuid.uuid, &backup_entry->uuid,
1910                                                                 SMS_MSGID_LEN);
1911
1912                 m = message_create(&txq_entry->uuid, sms->atom);
1913                 if (m == NULL) {
1914                         tx_queue_entry_destroy(txq_entry);
1915
1916                         goto loop_out;
1917                 }
1918
1919                 if (message_dbus_register(m) == FALSE) {
1920                         tx_queue_entry_destroy(txq_entry);
1921
1922                         goto loop_out;
1923                 }
1924
1925                 message_set_data(m, txq_entry);
1926                 g_hash_table_insert(sms->messages, &txq_entry->uuid, m);
1927
1928                 txq_entry->id = sms->tx_counter++;
1929                 g_queue_push_tail(sms->txq, txq_entry);
1930
1931 loop_out:
1932                 g_slist_foreach(backup_entry->msg_list, (GFunc)g_free, NULL);
1933                 g_slist_free(backup_entry->msg_list);
1934                 g_free(backup_entry);
1935         }
1936
1937         if (g_queue_get_length(sms->txq) > 0)
1938                 sms->tx_source = g_timeout_add(0, tx_next, sms);
1939
1940         g_queue_free(backupq);
1941 }
1942
1943 /*
1944  * Indicate oFono that a SMS driver is ready for operation
1945  *
1946  * This is called after ofono_sms_create() was done and the modem
1947  * driver determined that a modem supports SMS correctly. Once this
1948  * call succeeds, the D-BUS interface for SMS goes live.
1949  */
1950 void ofono_sms_register(struct ofono_sms *sms)
1951 {
1952         DBusConnection *conn = ofono_dbus_get_connection();
1953         struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1954         const char *path = __ofono_atom_get_path(sms->atom);
1955         struct ofono_atom *sim_atom;
1956
1957         if (!g_dbus_register_interface(conn, path,
1958                                         OFONO_MESSAGE_MANAGER_INTERFACE,
1959                                         sms_manager_methods,
1960                                         sms_manager_signals,
1961                                         NULL, sms, NULL)) {
1962                 ofono_error("Could not create %s interface",
1963                                 OFONO_MESSAGE_MANAGER_INTERFACE);
1964                 return;
1965         }
1966
1967         ofono_modem_add_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
1968
1969         sms->mw_watch = __ofono_modem_add_atom_watch(modem,
1970                                         OFONO_ATOM_TYPE_MESSAGE_WAITING,
1971                                         mw_watch, sms, NULL);
1972
1973         sms->netreg_watch = __ofono_modem_add_atom_watch(modem,
1974                                         OFONO_ATOM_TYPE_NETREG,
1975                                         netreg_watch, sms, NULL);
1976
1977         sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
1978
1979         /*
1980          * If we have a sim atom, we can uniquely identify the SIM,
1981          * otherwise create an sms assembly which doesn't backup the fragment
1982          * store.
1983          */
1984         if (sim_atom) {
1985                 const char *imsi;
1986
1987                 sms->sim = __ofono_atom_get_data(sim_atom);
1988                 imsi = ofono_sim_get_imsi(sms->sim);
1989                 sms->assembly = sms_assembly_new(imsi);
1990
1991                 sms->sr_assembly = status_report_assembly_new(imsi);
1992
1993                 sms_load_settings(sms, imsi);
1994         } else {
1995                 sms->assembly = sms_assembly_new(NULL);
1996                 sms->sr_assembly = status_report_assembly_new(NULL);
1997                 sms->bearer = 3; /* Default to CS then PS */
1998         }
1999
2000         if (sms->driver->bearer_set)
2001                 sms->driver->bearer_set(sms, sms->bearer,
2002                                                 bearer_init_callback, sms);
2003
2004         sms_restore_tx_queue(sms);
2005
2006         sms->text_handlers = __ofono_watchlist_new(g_free);
2007         sms->datagram_handlers = __ofono_watchlist_new(g_free);
2008
2009         __ofono_atom_register(sms->atom, sms_unregister);
2010 }
2011
2012 void ofono_sms_remove(struct ofono_sms *sms)
2013 {
2014         __ofono_atom_free(sms->atom);
2015 }
2016
2017 void ofono_sms_set_data(struct ofono_sms *sms, void *data)
2018 {
2019         sms->driver_data = data;
2020 }
2021
2022 void *ofono_sms_get_data(struct ofono_sms *sms)
2023 {
2024         return sms->driver_data;
2025 }
2026
2027 unsigned short __ofono_sms_get_next_ref(struct ofono_sms *sms)
2028 {
2029         return sms->ref;
2030 }
2031
2032 int __ofono_sms_txq_submit(struct ofono_sms *sms, GSList *list,
2033                                 unsigned int flags,
2034                                 struct ofono_uuid *uuid,
2035                                 ofono_sms_txq_queued_cb_t cb, void *data)
2036 {
2037         struct message *m = NULL;
2038         struct tx_queue_entry *entry;
2039
2040         entry = tx_queue_entry_new(list, flags);
2041         if (entry == NULL)
2042                 return -ENOMEM;
2043
2044         if (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
2045                 m = message_create(&entry->uuid, sms->atom);
2046                 if (m == NULL)
2047                         goto err;
2048
2049                 if (message_dbus_register(m) == FALSE)
2050                         goto err;
2051
2052                 message_set_data(m, entry);
2053
2054                 g_hash_table_insert(sms->messages, &entry->uuid, m);
2055         }
2056
2057         if (list->next != NULL) {
2058                 if (sms->ref == 65536)
2059                         sms->ref = 1;
2060                 else
2061                         sms->ref = sms->ref + 1;
2062         }
2063
2064         entry->id = sms->tx_counter++;
2065
2066         g_queue_push_tail(sms->txq, entry);
2067
2068         if (sms->registered && g_queue_get_length(sms->txq) == 1)
2069                 sms->tx_source = g_timeout_add(0, tx_next, sms);
2070
2071         if (uuid)
2072                 memcpy(uuid, &entry->uuid, sizeof(*uuid));
2073
2074         if (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
2075                 const char *uuid_str;
2076                 unsigned char i;
2077
2078                 uuid_str = ofono_uuid_to_str(&entry->uuid);
2079
2080                 for (i = 0; i < entry->num_pdus; i++) {
2081                         struct pending_pdu *pdu;
2082
2083                         pdu = &entry->pdus[i];
2084
2085                         sms_tx_backup_store(sms->imsi, entry->id, entry->flags,
2086                                                 uuid_str, i, pdu->pdu,
2087                                                 pdu->pdu_len, pdu->tpdu_len);
2088                 }
2089         }
2090
2091         if (cb)
2092                 cb(sms, &entry->uuid, data);
2093
2094         if (m && (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS))
2095                 message_emit_added(m, OFONO_MESSAGE_MANAGER_INTERFACE);
2096
2097         return 0;
2098
2099 err:
2100         tx_queue_entry_destroy(entry);
2101
2102         return -EINVAL;
2103 }
2104
2105 int __ofono_sms_txq_set_submit_notify(struct ofono_sms *sms,
2106                                         struct ofono_uuid *uuid,
2107                                         ofono_sms_txq_submit_cb_t cb,
2108                                         void *data,
2109                                         ofono_destroy_func destroy)
2110 {
2111         struct message *m;
2112         struct tx_queue_entry *entry;
2113
2114         m = g_hash_table_lookup(sms->messages, uuid);
2115         if (m == NULL)
2116                 return -ENOENT;
2117
2118         entry = message_get_data(m);
2119         if (entry == NULL)
2120                 return -ENOTSUP;
2121
2122         tx_queue_entry_set_submit_notify(entry, cb, data, destroy);
2123
2124         return 0;
2125 }