3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
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.
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.
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
42 #define uninitialized_var(x) x = x
44 #define MESSAGE_MANAGER_FLAG_CACHED 0x1
45 #define MESSAGE_MANAGER_FLAG_TXQ_ACTIVE 0x2
47 #define SETTINGS_STORE "sms"
48 #define SETTINGS_GROUP "Settings"
50 #define TXQ_MAX_RETRIES 4
51 #define NETWORK_TIMEOUT 332
53 static gboolean tx_next(gpointer user_data);
55 static GSList *g_drivers = NULL;
58 struct ofono_watchlist_item item;
66 struct ofono_phone_number sca;
67 struct sms_assembly *assembly;
70 unsigned long tx_counter;
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;
82 enum sms_alphabet alphabet;
83 const struct ofono_sms_driver *driver;
85 struct ofono_atom *atom;
86 ofono_bool_t use_delivery_reports;
87 struct status_report_assembly *sr_assembly;
89 struct ofono_watchlist *text_handlers;
90 struct ofono_watchlist *datagram_handlers;
94 unsigned char pdu[176];
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;
107 ofono_sms_txq_submit_cb_t cb;
109 ofono_destroy_func destroy;
113 static gboolean uuid_equal(gconstpointer v1, gconstpointer v2)
115 return memcmp(v1, v2, OFONO_SHA1_UUID_LEN) == 0;
118 static gboolean port_equal(int received, int expected)
120 return expected == -1 || received == expected;
123 static guint uuid_hash(gconstpointer v)
125 const struct ofono_uuid *uuid = v;
128 memcpy(&h, uuid->uuid, sizeof(h));
133 static const char *sms_bearer_to_string(int bearer)
141 return "ps-preferred";
143 return "cs-preferred";
149 static gboolean sms_bearer_from_string(const char *str, int *bearer)
151 if (g_str_equal(str, "ps-only"))
153 else if (g_str_equal(str, "cs-only"))
155 else if (g_str_equal(str, "ps-preferred"))
157 else if (g_str_equal(str, "cs-preferred"))
165 static const char *sms_alphabet_to_string(enum sms_alphabet alphabet)
168 case SMS_ALPHABET_TURKISH:
170 case SMS_ALPHABET_SPANISH:
172 case SMS_ALPHABET_PORTUGUESE:
174 case SMS_ALPHABET_DEFAULT:
181 static gboolean sms_alphabet_from_string(const char *str,
182 enum sms_alphabet *alphabet)
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;
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)
202 struct sms_handler *handler;
207 handler = g_try_new0(struct sms_handler, 1);
213 handler->item.notify = notify;
214 handler->item.notify_data = data;
215 handler->item.destroy = destroy;
217 return __ofono_watchlist_add_item(watchlist,
218 (struct ofono_watchlist_item *) handler);
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)
230 return add_sms_handler(sms->text_handlers, -1, -1, cb, data, destroy);
233 gboolean __ofono_sms_text_watch_remove(struct ofono_sms *sms,
241 return __ofono_watchlist_remove_item(sms->text_handlers, id);
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)
252 DBG("%p: dst %d, src %d", sms, dst, src);
254 return add_sms_handler(sms->datagram_handlers, dst, src, cb, data,
258 gboolean __ofono_sms_datagram_watch_remove(struct ofono_sms *sms,
266 return __ofono_watchlist_remove_item(sms->datagram_handlers, id);
269 const char *__ofono_sms_message_path_from_uuid(struct ofono_sms *sms,
270 const struct ofono_uuid *uuid)
272 return message_path_from_uuid(sms->atom, uuid);
275 static void set_bearer(struct ofono_sms *sms, int bearer)
277 DBusConnection *conn = ofono_dbus_get_connection();
278 const char *path = __ofono_atom_get_path(sms->atom);
281 if (sms->bearer == bearer)
284 sms->bearer = bearer;
286 value = sms_bearer_to_string(sms->bearer);
288 ofono_dbus_signal_property_changed(conn, path,
289 OFONO_MESSAGE_MANAGER_INTERFACE,
291 DBUS_TYPE_STRING, &value);
294 static void set_alphabet(struct ofono_sms *sms, enum sms_alphabet alphabet)
296 DBusConnection *conn = ofono_dbus_get_connection();
297 const char *path = __ofono_atom_get_path(sms->atom);
300 if (sms->alphabet == alphabet)
303 sms->alphabet = alphabet;
305 value = sms_alphabet_to_string(sms->alphabet);
307 ofono_dbus_signal_property_changed(conn, path,
308 OFONO_MESSAGE_MANAGER_INTERFACE,
310 DBUS_TYPE_STRING, &value);
313 static void set_sca(struct ofono_sms *sms,
314 const struct ofono_phone_number *sca)
316 DBusConnection *conn = ofono_dbus_get_connection();
317 const char *path = __ofono_atom_get_path(sms->atom);
320 if (sms->sca.type == sca->type &&
321 !strcmp(sms->sca.number, sca->number))
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';
328 value = phone_number_to_string(&sms->sca);
330 ofono_dbus_signal_property_changed(conn, path,
331 OFONO_MESSAGE_MANAGER_INTERFACE,
332 "ServiceCenterAddress",
333 DBUS_TYPE_STRING, &value);
336 static DBusMessage *generate_get_properties_reply(struct ofono_sms *sms,
340 DBusMessageIter iter;
341 DBusMessageIter dict;
344 const char *alphabet;
346 reply = dbus_message_new_method_return(msg);
350 dbus_message_iter_init_append(reply, &iter);
352 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
353 OFONO_PROPERTIES_ARRAY_SIGNATURE,
356 sca = phone_number_to_string(&sms->sca);
358 ofono_dbus_dict_append(&dict, "ServiceCenterAddress", DBUS_TYPE_STRING,
361 ofono_dbus_dict_append(&dict, "UseDeliveryReports", DBUS_TYPE_BOOLEAN,
362 &sms->use_delivery_reports);
364 bearer = sms_bearer_to_string(sms->bearer);
365 ofono_dbus_dict_append(&dict, "Bearer", DBUS_TYPE_STRING, &bearer);
367 alphabet = sms_alphabet_to_string(sms->alphabet);
368 ofono_dbus_dict_append(&dict, "Alphabet", DBUS_TYPE_STRING, &alphabet);
370 dbus_message_iter_close_container(&iter, &dict);
375 static void sms_sca_query_cb(const struct ofono_error *error,
376 const struct ofono_phone_number *sca,
379 struct ofono_sms *sms = data;
381 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
386 sms->flags |= MESSAGE_MANAGER_FLAG_CACHED;
390 DBusMessage *reply = generate_get_properties_reply(sms,
392 __ofono_dbus_pending_reply(&sms->pending, reply);
396 static DBusMessage *sms_get_properties(DBusConnection *conn,
397 DBusMessage *msg, void *data)
399 struct ofono_sms *sms = data;
401 if (sms->flags & MESSAGE_MANAGER_FLAG_CACHED)
402 return generate_get_properties_reply(sms, msg);
405 return __ofono_error_busy(msg);
407 if (sms->driver->sca_query == NULL)
408 return __ofono_error_not_implemented(msg);
410 sms->pending = dbus_message_ref(msg);
412 sms->driver->sca_query(sms, sms_sca_query_cb, sms);
417 static void bearer_set_query_callback(const struct ofono_error *error,
418 int bearer, void *data)
420 struct ofono_sms *sms = data;
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);
430 reply = dbus_message_new_method_return(sms->pending);
431 __ofono_dbus_pending_reply(&sms->pending, reply);
433 set_bearer(sms, bearer);
436 static void bearer_set_callback(const struct ofono_error *error, void *data)
438 struct ofono_sms *sms = data;
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));
447 sms->driver->bearer_query(sms, bearer_set_query_callback, sms);
450 static void sca_set_query_callback(const struct ofono_error *error,
451 const struct ofono_phone_number *sca,
454 struct ofono_sms *sms = data;
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);
467 reply = dbus_message_new_method_return(sms->pending);
468 __ofono_dbus_pending_reply(&sms->pending, reply);
471 static void sca_set_callback(const struct ofono_error *error, void *data)
473 struct ofono_sms *sms = data;
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));
482 sms->driver->sca_query(sms, sca_set_query_callback, sms);
485 static DBusMessage *sms_set_property(DBusConnection *conn, DBusMessage *msg,
488 struct ofono_sms *sms = data;
489 DBusMessageIter iter;
491 const char *property;
494 return __ofono_error_busy(msg);
496 if (!dbus_message_iter_init(msg, &iter))
497 return __ofono_error_invalid_args(msg);
499 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
500 return __ofono_error_invalid_args(msg);
502 dbus_message_iter_get_basic(&iter, &property);
503 dbus_message_iter_next(&iter);
505 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
506 return __ofono_error_invalid_args(msg);
508 dbus_message_iter_recurse(&iter, &var);
510 if (!strcmp(property, "ServiceCenterAddress")) {
512 struct ofono_phone_number sca;
514 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
515 return __ofono_error_invalid_args(msg);
517 dbus_message_iter_get_basic(&var, &value);
519 if (strlen(value) == 0 || !valid_phone_number_format(value))
520 return __ofono_error_invalid_format(msg);
522 if (sms->driver->sca_set == NULL ||
523 sms->driver->sca_query == NULL)
524 return __ofono_error_not_implemented(msg);
526 string_to_phone_number(value, &sca);
528 sms->pending = dbus_message_ref(msg);
530 sms->driver->sca_set(sms, &sca, sca_set_callback, sms);
534 if (!strcmp(property, "Bearer")) {
538 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
539 return __ofono_error_invalid_args(msg);
541 dbus_message_iter_get_basic(&var, &value);
543 if (sms_bearer_from_string(value, &bearer) != TRUE)
544 return __ofono_error_invalid_format(msg);
546 if (sms->driver->bearer_set == NULL ||
547 sms->driver->bearer_query == NULL)
548 return __ofono_error_not_implemented(msg);
550 sms->pending = dbus_message_ref(msg);
552 sms->driver->bearer_set(sms, bearer, bearer_set_callback, sms);
556 if (!strcmp(property, "UseDeliveryReports")) {
557 const char *path = __ofono_atom_get_path(sms->atom);
560 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
561 return __ofono_error_invalid_args(msg);
563 dbus_message_iter_get_basic(&var, &value);
565 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
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);
578 if (!strcmp(property, "Alphabet")) {
580 enum sms_alphabet alphabet;
582 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
583 return __ofono_error_invalid_args(msg);
585 dbus_message_iter_get_basic(&var, &value);
587 if (!sms_alphabet_from_string(value, &alphabet))
588 return __ofono_error_invalid_format(msg);
590 set_alphabet(sms, alphabet);
592 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
596 return __ofono_error_invalid_args(msg);
600 * Destroy/release the contents of a 'struct tx_queue_entry'
602 * This releases resources allocated *inside* @entry and @entry
605 static void tx_queue_entry_destroy(struct tx_queue_entry *entry)
608 entry->destroy(entry->data);
614 static void tx_queue_entry_destroy_foreach(gpointer _entry, gpointer unused)
616 tx_queue_entry_destroy(_entry);
619 static void sms_tx_queue_remove_entry(struct ofono_sms *sms, GList *entry_list,
620 enum message_state tx_state)
622 struct tx_queue_entry *entry = entry_list->data;
623 struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
625 g_queue_delete_link(sms->txq, entry_list);
630 entry->cb(tx_state == MESSAGE_STATE_SENT, entry->data);
632 if (entry->flags & OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY) {
633 enum ofono_history_sms_status hs;
636 case MESSAGE_STATE_SENT:
637 hs = OFONO_HISTORY_SMS_STATUS_SUBMITTED;
639 case MESSAGE_STATE_FAILED:
640 hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_FAILED;
642 case MESSAGE_STATE_CANCELLED:
643 hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_CANCELLED;
646 ofono_error("Unexpected sms state %d", tx_state);
650 __ofono_history_sms_send_status(modem, &entry->uuid,
654 if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
657 sms_tx_backup_free(sms->imsi, entry->id, entry->flags,
658 ofono_uuid_to_str(&entry->uuid));
660 m = g_hash_table_lookup(sms->messages, &entry->uuid);
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);
672 tx_queue_entry_destroy(entry);
675 static void tx_finished(const struct ofono_error *error, int mr, void *data)
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;
682 DBG("tx_finished %p", entry);
684 sms->flags &= ~MESSAGE_MANAGER_FLAG_TXQ_ACTIVE;
687 /* Retry again when back in online mode */
688 /* Note this does not increment retry count */
689 if (sms->registered == FALSE)
692 tx_state = MESSAGE_STATE_FAILED;
694 /* Retry done only for Network Timeout failure */
695 if (error->type == OFONO_ERROR_TYPE_CMS &&
696 error->error != NETWORK_TIMEOUT)
699 if (!(entry->flags & OFONO_SMS_SUBMIT_FLAG_RETRY))
704 if (entry->retry < TXQ_MAX_RETRIES) {
705 DBG("Sending failed, retry in %d secs",
707 sms->tx_source = g_timeout_add_seconds(entry->retry * 5,
712 DBG("Max retries reached, giving up");
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),
724 if (entry->flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR)
725 status_report_assembly_add_fragment(sms->sr_assembly,
731 if (entry->cur_pdu < entry->num_pdus) {
732 sms->tx_source = g_timeout_add(0, tx_next, sms);
736 tx_state = MESSAGE_STATE_SENT;
739 sms_tx_queue_remove_entry(sms, g_queue_peek_head_link(sms->txq),
742 if (sms->registered == FALSE)
745 if (g_queue_peek_head(sms->txq)) {
746 DBG("Scheduling next");
747 sms->tx_source = g_timeout_add(0, tx_next, sms);
751 static gboolean tx_next(gpointer user_data)
753 struct ofono_sms *sms = user_data;
755 struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
756 struct pending_pdu *pdu = &entry->pdus[entry->cur_pdu];
758 DBG("tx_next: %p", entry);
762 if (sms->registered == FALSE)
765 if (g_queue_get_length(sms->txq) > 1
766 || (entry->num_pdus - entry->cur_pdu) > 1)
769 sms->flags |= MESSAGE_MANAGER_FLAG_TXQ_ACTIVE;
771 sms->driver->submit(sms, pdu->pdu, pdu->pdu_len, pdu->tpdu_len,
772 send_mms, tx_finished, sms);
777 static void netreg_status_watch(int status, int lac, int ci, int tech,
778 const char *mcc, const char *mnc,
781 struct ofono_sms *sms = data;
784 case NETWORK_REGISTRATION_STATUS_REGISTERED:
785 case NETWORK_REGISTRATION_STATUS_ROAMING:
786 sms->registered = TRUE;
789 sms->registered = FALSE;
793 if (sms->registered == FALSE)
796 if (sms->tx_source > 0)
799 if (g_queue_get_length(sms->txq))
800 sms->tx_source = g_timeout_add(0, tx_next, sms);
803 static void netreg_watch(struct ofono_atom *atom,
804 enum ofono_atom_watch_condition cond,
807 struct ofono_sms *sms = data;
810 if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
811 sms->registered = FALSE;
812 sms->status_watch = 0;
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);
821 status = ofono_netreg_get_status(sms->netreg);
822 netreg_status_watch(status, 0, 0, 0, NULL, NULL, sms);
827 * Generate a UUID from an SMS PDU List
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.
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.
841 static gboolean sms_uuid_from_pdus(const struct pending_pdu *pdu,
843 struct ofono_uuid *uuid)
847 gsize uuid_size = sizeof(uuid->uuid);
851 checksum = g_checksum_new(G_CHECKSUM_SHA1);
852 if (checksum == NULL)
855 for (cnt = 0; cnt < pdus; cnt++)
856 g_checksum_update(checksum, pdu[cnt].pdu, pdu[cnt].pdu_len);
858 gettimeofday(&now, NULL);
859 g_checksum_update(checksum, (void *) &now, sizeof(now));
861 g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
862 g_checksum_free(checksum);
867 static struct tx_queue_entry *tx_queue_entry_new(GSList *msg_list,
870 struct tx_queue_entry *entry;
874 entry = g_try_new0(struct tx_queue_entry, 1);
878 entry->num_pdus = g_slist_length(msg_list);
880 entry->pdus = g_try_new0(struct pending_pdu, entry->num_pdus);
881 if (entry->pdus == NULL)
884 if (flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR) {
885 struct sms *head = msg_list->data;
887 memcpy(&entry->receiver, &head->submit.daddr,
888 sizeof(entry->receiver));
891 entry->flags = flags;
893 for (l = msg_list; l; l = l->next) {
894 struct pending_pdu *pdu = &entry->pdus[i++];
895 struct sms *s = l->data;
897 sms_encode(s, &pdu->pdu_len, &pdu->tpdu_len, pdu->pdu);
899 DBG("pdu_len: %d, tpdu_len: %d",
900 pdu->pdu_len, pdu->tpdu_len);
903 if (flags & OFONO_SMS_SUBMIT_FLAG_REUSE_UUID)
906 if (sms_uuid_from_pdus(entry->pdus, entry->num_pdus, &entry->uuid))
916 static void tx_queue_entry_set_submit_notify(struct tx_queue_entry *entry,
917 ofono_sms_txq_submit_cb_t cb,
919 ofono_destroy_func destroy)
923 entry->destroy = destroy;
926 static void message_queued(struct ofono_sms *sms,
927 const struct ofono_uuid *uuid, void *data)
929 DBusConnection *conn = ofono_dbus_get_connection();
930 DBusMessage *msg = data;
933 path = __ofono_sms_message_path_from_uuid(sms, uuid);
934 g_dbus_send_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &path,
939 * Pre-process a SMS text message and deliver it [D-Bus SendMessage()]
941 * @conn: D-Bus connection
942 * @msg: message data (telephone number and text)
943 * @data: SMS object to use for transmision
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.
951 static DBusMessage *sms_send_message(DBusConnection *conn, DBusMessage *msg,
954 struct ofono_sms *sms = data;
958 struct ofono_modem *modem;
960 gboolean use_16bit_ref = FALSE;
962 struct ofono_uuid uuid;
964 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &to,
965 DBUS_TYPE_STRING, &text,
967 return __ofono_error_invalid_args(msg);
969 if (valid_phone_number_format(to) == FALSE)
970 return __ofono_error_invalid_format(msg);
972 msg_list = sms_text_prepare_with_alphabet(to, text, sms->ref,
974 sms->use_delivery_reports,
977 if (msg_list == NULL)
978 return __ofono_error_invalid_format(msg);
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;
986 err = __ofono_sms_txq_submit(sms, msg_list, flags, &uuid,
987 message_queued, msg);
989 g_slist_foreach(msg_list, (GFunc) g_free, NULL);
990 g_slist_free(msg_list);
993 return __ofono_error_failed(msg);
995 modem = __ofono_atom_get_modem(sms->atom);
996 __ofono_history_sms_send_pending(modem, &uuid, to, time(NULL), text);
1001 static DBusMessage *sms_get_messages(DBusConnection *conn, DBusMessage *msg,
1004 struct ofono_sms *sms = data;
1006 DBusMessageIter iter;
1007 DBusMessageIter array;
1008 DBusMessageIter entry, dict;
1010 GHashTableIter hashiter;
1011 gpointer key, value;
1013 const struct ofono_uuid *uuid;
1015 reply = dbus_message_new_method_return(msg);
1019 dbus_message_iter_init_append(reply, &iter);
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,
1032 g_hash_table_iter_init(&hashiter, sms->messages);
1034 while (g_hash_table_iter_next(&hashiter, &key, &value)) {
1036 uuid = message_get_uuid(m);
1038 path = __ofono_sms_message_path_from_uuid(sms, uuid);
1040 dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
1042 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1044 dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
1045 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1048 message_append_properties(m, &dict);
1049 dbus_message_iter_close_container(&entry, &dict);
1050 dbus_message_iter_close_container(&array, &entry);
1053 dbus_message_iter_close_container(&iter, &array);
1058 static gint entry_compare_by_uuid(gconstpointer a, gconstpointer b)
1060 const struct tx_queue_entry *entry = a;
1061 const struct ofono_uuid *uuid = b;
1063 return memcmp(&entry->uuid, uuid, sizeof(entry->uuid));
1066 int __ofono_sms_txq_cancel(struct ofono_sms *sms, const struct ofono_uuid *uuid)
1069 struct tx_queue_entry *entry;
1071 l = g_queue_find_custom(sms->txq, uuid, entry_compare_by_uuid);
1078 if (entry == g_queue_peek_head(sms->txq)) {
1080 * Fail if any pdu was already transmitted or if we are
1081 * waiting the answer from driver.
1083 if (entry->cur_pdu > 0)
1086 if (sms->flags & MESSAGE_MANAGER_FLAG_TXQ_ACTIVE)
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'
1093 if (sms->tx_source) {
1094 g_source_remove(sms->tx_source);
1097 if (g_queue_get_length(sms->txq) > 1)
1098 sms->tx_source = g_timeout_add(0, tx_next, sms);
1102 sms_tx_queue_remove_entry(sms, l, MESSAGE_STATE_CANCELLED);
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 },
1118 static GDBusSignalTable sms_manager_signals[] = {
1119 { "PropertyChanged", "sv" },
1120 { "IncomingMessage", "sa{sv}" },
1121 { "ImmediateMessage", "sa{sv}" },
1122 { "MessageAdded", "oa{sv}" },
1123 { "MessageRemoved", "o" },
1127 static gboolean compute_incoming_msgid(GSList *sms_list,
1128 struct ofono_uuid *uuid)
1130 GChecksum *checksum;
1132 const struct sms *s;
1133 unsigned char buf[176];
1134 gsize uuid_size = sizeof(uuid->uuid);
1137 checksum = g_checksum_new(G_CHECKSUM_SHA1);
1138 if (checksum == NULL)
1141 for (l = sms_list; l; l = l->next) {
1144 if (sms_encode(s, &len, NULL, buf) == FALSE) {
1145 g_checksum_free(checksum);
1149 g_checksum_update(checksum, buf, len);
1152 g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
1153 g_checksum_free(checksum);
1158 static void dispatch_app_datagram(struct ofono_sms *sms,
1159 const struct ofono_uuid *uuid,
1161 unsigned char *buf, unsigned len,
1162 const struct sms_address *addr,
1163 const struct sms_scts *scts)
1165 const char *sender = sms_address_to_string(addr);
1170 ofono_sms_datagram_notify_cb_t notify;
1171 struct sms_handler *h;
1174 ts = sms_scts_to_time(scts, &remote);
1175 localtime_r(&ts, &local);
1177 for (l = sms->datagram_handlers->items; l; l = l->next) {
1179 notify = h->item.notify;
1181 if (!port_equal(dst, h->dst) || !port_equal(src, h->src))
1184 notify(sender, &remote, &local, dst, src, buf, len,
1185 h->item.notify_data);
1189 static void dispatch_text_message(struct ofono_sms *sms,
1190 const struct ofono_uuid *uuid,
1191 const char *message,
1193 const struct sms_address *addr,
1194 const struct sms_scts *scts)
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;
1203 const char *signal_name;
1207 const char *str = buf;
1208 ofono_sms_text_notify_cb_t notify;
1209 struct sms_handler *h;
1212 if (message == NULL)
1215 if (cls == SMS_CLASS_0)
1216 signal_name = "ImmediateMessage";
1218 signal_name = "IncomingMessage";
1220 signal = dbus_message_new_signal(path, OFONO_MESSAGE_MANAGER_INTERFACE,
1226 dbus_message_iter_init_append(signal, &iter);
1228 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
1230 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1231 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1234 ts = sms_scts_to_time(scts, &remote);
1235 localtime_r(&ts, &local);
1237 strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &local);
1239 ofono_dbus_dict_append(&dict, "LocalSentTime", DBUS_TYPE_STRING, &str);
1241 strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &remote);
1243 ofono_dbus_dict_append(&dict, "SentTime", DBUS_TYPE_STRING, &str);
1245 str = sms_address_to_string(addr);
1246 ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &str);
1248 dbus_message_iter_close_container(&iter, &dict);
1250 g_dbus_send_message(conn, signal);
1252 if (cls == SMS_CLASS_0)
1255 for (l = sms->text_handlers->items; l; l = l->next) {
1257 notify = h->item.notify;
1259 notify(str, &remote, &local, message, h->item.notify_data);
1262 __ofono_history_sms_received(modem, uuid, str, &remote, &local,
1266 static void sms_dispatch(struct ofono_sms *sms, GSList *sms_list)
1269 const struct sms *s;
1270 struct ofono_uuid uuid;
1271 enum sms_charset uninitialized_var(old_charset);
1278 if (sms_list == NULL)
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
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.
1291 * We also check that if 8-bit or 16-bit application addressing is
1292 * used, the addresses are the same across all segments.
1295 for (l = sms_list; l; l = l->next) {
1297 gboolean comp = FALSE;
1298 enum sms_charset charset;
1304 dcs = s->deliver.dcs;
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");
1314 ofono_error("Compressed data not supported");
1319 old_charset = charset;
1321 if (charset == SMS_CHARSET_8BIT && charset != old_charset) {
1322 ofono_error("Can't concatenate disparate charsets");
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;
1330 if (l == sms_list) {
1336 DBG("dst %d src %d", cdst, csrc);
1338 if (srcport != csrc || dstport != cdst) {
1339 ofono_error("Source / Destination ports across "
1340 "concatenated message are not the "
1346 if (!compute_incoming_msgid(sms_list, &uuid))
1351 /* Handle datagram */
1352 if (old_charset == SMS_CHARSET_8BIT) {
1356 if (srcport == -1 || dstport == -1) {
1357 ofono_error("Got an 8-bit encoded message, however "
1358 "no valid src/address port, ignore");
1362 buf = sms_decode_datagram(sms_list, &len);
1366 dispatch_app_datagram(sms, &uuid, dstport, srcport, buf, len,
1367 &s->deliver.oaddr, &s->deliver.scts);
1371 char *message = sms_decode_text(sms_list);
1373 if (message == NULL)
1376 dispatch_text_message(sms, &uuid, message, cls,
1377 &s->deliver.oaddr, &s->deliver.scts);
1383 static void handle_deliver(struct ofono_sms *sms, const struct sms *incoming)
1392 if (sms_extract_concatenation(incoming, &ref, &max, &seq)) {
1395 if (sms->assembly == NULL)
1398 sms_list = sms_assembly_add_fragment(sms->assembly,
1399 incoming, time(NULL),
1400 &incoming->deliver.oaddr,
1403 if (sms_list == NULL)
1406 sms_dispatch(sms, sms_list);
1407 g_slist_foreach(sms_list, (GFunc) g_free, NULL);
1408 g_slist_free(sms_list);
1413 l = g_slist_append(NULL, (void *) incoming);
1414 sms_dispatch(sms, l);
1418 static void handle_sms_status_report(struct ofono_sms *sms,
1419 const struct sms *incoming)
1421 struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1423 struct ofono_uuid uuid;
1427 if (status_report_assembly_report(sms->sr_assembly, incoming, uuid.uuid,
1428 &delivered) == FALSE)
1431 __ofono_history_sms_send_status(modem, &uuid, time(NULL),
1432 delivered ? OFONO_HISTORY_SMS_STATUS_DELIVERED :
1433 OFONO_HISTORY_SMS_STATUS_DELIVER_FAILED);
1437 static inline gboolean handle_mwi(struct ofono_sms *sms, struct sms *s)
1443 if (sms->mw == NULL)
1446 __ofono_message_waiting_mwi(sms->mw, s, &discard);
1451 void ofono_sms_deliver_notify(struct ofono_sms *sms, unsigned char *pdu,
1452 int len, int tpdu_len)
1454 struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
1455 struct ofono_atom *stk_atom;
1456 struct ofono_atom *sim_atom;
1460 DBG("len %d tpdu len %d", len, tpdu_len);
1462 if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
1463 ofono_error("Unable to decode PDU");
1467 if (s.type != SMS_TYPE_DELIVER) {
1468 ofono_error("Expecting a DELIVER pdu");
1472 if (s.deliver.pid == SMS_PID_TYPE_SM_TYPE_0) {
1473 DBG("Explicitly ignoring type 0 SMS");
1478 * This is an older style MWI notification, process MWI
1479 * headers and handle it like any other message
1481 if (s.deliver.pid == SMS_PID_TYPE_RETURN_CALL) {
1482 if (handle_mwi(sms, &s))
1489 * The DCS indicates this is an MWI notification, process it
1490 * and then handle the User-Data as any other message
1492 if (sms_mwi_dcs_decode(s.deliver.dcs, NULL, NULL, NULL, NULL)) {
1493 if (handle_mwi(sms, &s))
1499 if (!sms_dcs_decode(s.deliver.dcs, &cls, NULL, NULL, NULL)) {
1500 ofono_error("Unknown / Reserved DCS. Ignoring");
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");
1512 case SMS_PID_TYPE_ME_DEPERSONALIZATION:
1513 if (s.deliver.dcs == 0x11) {
1514 ofono_error("ME Depersonalization message ignored");
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)
1525 sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
1527 if (sim_atom == NULL)
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))
1536 stk_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_STK);
1538 if (stk_atom == NULL)
1541 __ofono_sms_sim_download(__ofono_atom_get_data(stk_atom),
1545 * Passing the USIM response back to network is not
1546 * currently supported
1548 * TODO: store in EFsms if not handled
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.
1562 if (s.deliver.udhi) {
1563 struct sms_udh_iter iter;
1566 if (!sms_udh_iter_init(&s, &iter))
1569 while ((iei = sms_udh_iter_get_ie_type(&iter)) !=
1572 ofono_error("Reserved / Unknown / USAT"
1573 "header in use, ignore");
1578 case SMS_IEI_SPECIAL_MESSAGE_INDICATION:
1579 case SMS_IEI_ENHANCED_VOICE_MAIL_INFORMATION:
1581 * TODO: ignore if not in the very first
1582 * segment of a concatenated SM so as not
1583 * to repeat the indication.
1585 if (handle_mwi(sms, &s))
1590 ofono_error("No support for WCMP, ignoring");
1593 sms_udh_iter_next(&iter);
1599 handle_deliver(sms, &s);
1602 void ofono_sms_status_notify(struct ofono_sms *sms, unsigned char *pdu,
1603 int len, int tpdu_len)
1608 DBG("len %d tpdu len %d", len, tpdu_len);
1610 if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
1611 ofono_error("Unable to decode PDU");
1615 if (s.type != SMS_TYPE_STATUS_REPORT) {
1616 ofono_error("Expecting a STATUS REPORT pdu");
1620 if (s.status_report.srq) {
1621 ofono_error("Waiting an answer to SMS-SUBMIT, not SMS-COMMAND");
1625 if (!sms_dcs_decode(s.status_report.dcs, &cls, NULL, NULL, NULL)) {
1626 ofono_error("Unknown / Reserved DCS. Ignoring");
1630 handle_sms_status_report(sms, &s);
1633 int ofono_sms_driver_register(const struct ofono_sms_driver *d)
1635 DBG("driver: %p, name: %s", d, d->name);
1637 if (d->probe == NULL)
1640 g_drivers = g_slist_prepend(g_drivers, (void *) d);
1645 void ofono_sms_driver_unregister(const struct ofono_sms_driver *d)
1647 DBG("driver: %p, name: %s", d, d->name);
1649 g_drivers = g_slist_remove(g_drivers, (void *) d);
1652 static void sms_unregister(struct ofono_atom *atom)
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);
1659 g_dbus_unregister_interface(conn, path,
1660 OFONO_MESSAGE_MANAGER_INTERFACE);
1661 ofono_modem_remove_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
1663 if (sms->mw_watch) {
1664 __ofono_modem_remove_atom_watch(modem, sms->mw_watch);
1669 if (sms->status_watch) {
1670 __ofono_netreg_remove_status_watch(sms->netreg,
1672 sms->status_watch = 0;
1675 if (sms->netreg_watch) {
1676 __ofono_modem_remove_atom_watch(modem, sms->netreg_watch);
1677 sms->netreg_watch = 0;
1682 if (sms->messages) {
1683 GHashTableIter iter;
1685 gpointer key, value;
1687 g_hash_table_iter_init(&iter, sms->messages);
1689 while (g_hash_table_iter_next(&iter, &key, &value)) {
1691 message_dbus_unregister(m);
1694 g_hash_table_destroy(sms->messages);
1695 sms->messages = NULL;
1698 __ofono_watchlist_free(sms->text_handlers);
1699 sms->text_handlers = NULL;
1701 __ofono_watchlist_free(sms->datagram_handlers);
1702 sms->datagram_handlers = NULL;
1705 static void sms_remove(struct ofono_atom *atom)
1707 struct ofono_sms *sms = __ofono_atom_get_data(atom);
1709 DBG("atom: %p", atom);
1714 if (sms->driver && sms->driver->remove)
1715 sms->driver->remove(sms);
1717 if (sms->tx_source) {
1718 g_source_remove(sms->tx_source);
1722 if (sms->assembly) {
1723 sms_assembly_free(sms->assembly);
1724 sms->assembly = NULL;
1728 g_queue_foreach(sms->txq, tx_queue_entry_destroy_foreach, NULL);
1729 g_queue_free(sms->txq);
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);
1744 storage_close(sms->imsi, SETTINGS_STORE, sms->settings, TRUE);
1748 sms->settings = NULL;
1751 if (sms->sr_assembly) {
1752 status_report_assembly_free(sms->sr_assembly);
1753 sms->sr_assembly = NULL;
1761 * Create a SMS driver
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().
1767 * This is done once the modem driver determines that SMS is properly
1768 * supported by the hardware.
1770 struct ofono_sms *ofono_sms_create(struct ofono_modem *modem,
1771 unsigned int vendor,
1775 struct ofono_sms *sms;
1781 sms = g_try_new0(struct ofono_sms, 1);
1786 sms->sca.type = 129;
1788 sms->txq = g_queue_new();
1789 sms->messages = g_hash_table_new(uuid_hash, uuid_equal);
1791 sms->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_SMS,
1794 for (l = g_drivers; l; l = l->next) {
1795 const struct ofono_sms_driver *drv = l->data;
1797 if (g_strcmp0(drv->name, driver))
1800 if (drv->probe(sms, vendor, data) < 0)
1810 static void mw_watch(struct ofono_atom *atom,
1811 enum ofono_atom_watch_condition cond, void *data)
1813 struct ofono_sms *sms = data;
1815 if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
1820 sms->mw = __ofono_atom_get_data(atom);
1823 static void sms_load_settings(struct ofono_sms *sms, const char *imsi)
1827 sms->settings = storage_open(imsi, SETTINGS_STORE);
1829 if (sms->settings == NULL)
1832 sms->imsi = g_strdup(imsi);
1835 sms->ref = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
1836 "NextReference", &error);
1838 if (error || sms->ref > 65536) {
1839 g_error_free(error);
1841 g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
1842 "NextReference", sms->ref);
1846 sms->use_delivery_reports =
1847 g_key_file_get_boolean(sms->settings, SETTINGS_GROUP,
1848 "UseDeliveryReports", &error);
1851 g_error_free(error);
1852 g_key_file_set_boolean(sms->settings, SETTINGS_GROUP,
1853 "UseDeliveryReports",
1854 sms->use_delivery_reports);
1858 sms->bearer = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
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);
1869 sms->alphabet = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
1870 "Alphabet", &error);
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);
1880 static void bearer_init_callback(const struct ofono_error *error, void *data)
1882 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
1883 ofono_error("Error bootstrapping SMS Bearer Preference");
1886 static void sms_restore_tx_queue(struct ofono_sms *sms)
1889 struct txq_backup_entry *backup_entry;
1893 backupq = sms_tx_queue_load(sms->imsi);
1895 if (backupq == NULL)
1898 while ((backup_entry = g_queue_pop_head(backupq))) {
1900 struct tx_queue_entry *txq_entry;
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)
1908 txq_entry->flags &= ~OFONO_SMS_SUBMIT_FLAG_REUSE_UUID;
1909 memcpy(&txq_entry->uuid.uuid, &backup_entry->uuid,
1912 m = message_create(&txq_entry->uuid, sms->atom);
1914 tx_queue_entry_destroy(txq_entry);
1919 if (message_dbus_register(m) == FALSE) {
1920 tx_queue_entry_destroy(txq_entry);
1925 message_set_data(m, txq_entry);
1926 g_hash_table_insert(sms->messages, &txq_entry->uuid, m);
1928 txq_entry->id = sms->tx_counter++;
1929 g_queue_push_tail(sms->txq, txq_entry);
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);
1937 if (g_queue_get_length(sms->txq) > 0)
1938 sms->tx_source = g_timeout_add(0, tx_next, sms);
1940 g_queue_free(backupq);
1944 * Indicate oFono that a SMS driver is ready for operation
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.
1950 void ofono_sms_register(struct ofono_sms *sms)
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;
1957 if (!g_dbus_register_interface(conn, path,
1958 OFONO_MESSAGE_MANAGER_INTERFACE,
1959 sms_manager_methods,
1960 sms_manager_signals,
1962 ofono_error("Could not create %s interface",
1963 OFONO_MESSAGE_MANAGER_INTERFACE);
1967 ofono_modem_add_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
1969 sms->mw_watch = __ofono_modem_add_atom_watch(modem,
1970 OFONO_ATOM_TYPE_MESSAGE_WAITING,
1971 mw_watch, sms, NULL);
1973 sms->netreg_watch = __ofono_modem_add_atom_watch(modem,
1974 OFONO_ATOM_TYPE_NETREG,
1975 netreg_watch, sms, NULL);
1977 sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
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
1987 sms->sim = __ofono_atom_get_data(sim_atom);
1988 imsi = ofono_sim_get_imsi(sms->sim);
1989 sms->assembly = sms_assembly_new(imsi);
1991 sms->sr_assembly = status_report_assembly_new(imsi);
1993 sms_load_settings(sms, imsi);
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 */
2000 if (sms->driver->bearer_set)
2001 sms->driver->bearer_set(sms, sms->bearer,
2002 bearer_init_callback, sms);
2004 sms_restore_tx_queue(sms);
2006 sms->text_handlers = __ofono_watchlist_new(g_free);
2007 sms->datagram_handlers = __ofono_watchlist_new(g_free);
2009 __ofono_atom_register(sms->atom, sms_unregister);
2012 void ofono_sms_remove(struct ofono_sms *sms)
2014 __ofono_atom_free(sms->atom);
2017 void ofono_sms_set_data(struct ofono_sms *sms, void *data)
2019 sms->driver_data = data;
2022 void *ofono_sms_get_data(struct ofono_sms *sms)
2024 return sms->driver_data;
2027 unsigned short __ofono_sms_get_next_ref(struct ofono_sms *sms)
2032 int __ofono_sms_txq_submit(struct ofono_sms *sms, GSList *list,
2034 struct ofono_uuid *uuid,
2035 ofono_sms_txq_queued_cb_t cb, void *data)
2037 struct message *m = NULL;
2038 struct tx_queue_entry *entry;
2040 entry = tx_queue_entry_new(list, flags);
2044 if (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
2045 m = message_create(&entry->uuid, sms->atom);
2049 if (message_dbus_register(m) == FALSE)
2052 message_set_data(m, entry);
2054 g_hash_table_insert(sms->messages, &entry->uuid, m);
2057 if (list->next != NULL) {
2058 if (sms->ref == 65536)
2061 sms->ref = sms->ref + 1;
2064 entry->id = sms->tx_counter++;
2066 g_queue_push_tail(sms->txq, entry);
2068 if (sms->registered && g_queue_get_length(sms->txq) == 1)
2069 sms->tx_source = g_timeout_add(0, tx_next, sms);
2072 memcpy(uuid, &entry->uuid, sizeof(*uuid));
2074 if (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
2075 const char *uuid_str;
2078 uuid_str = ofono_uuid_to_str(&entry->uuid);
2080 for (i = 0; i < entry->num_pdus; i++) {
2081 struct pending_pdu *pdu;
2083 pdu = &entry->pdus[i];
2085 sms_tx_backup_store(sms->imsi, entry->id, entry->flags,
2086 uuid_str, i, pdu->pdu,
2087 pdu->pdu_len, pdu->tpdu_len);
2092 cb(sms, &entry->uuid, data);
2094 if (m && (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS))
2095 message_emit_added(m, OFONO_MESSAGE_MANAGER_INTERFACE);
2100 tx_queue_entry_destroy(entry);
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,
2109 ofono_destroy_func destroy)
2112 struct tx_queue_entry *entry;
2114 m = g_hash_table_lookup(sms->messages, uuid);
2118 entry = message_get_data(m);
2122 tx_queue_entry_set_submit_notify(entry, cb, data, destroy);