Refactor tx_finished() and create a function to remove an entry from the
tx queue. This function will be used also when a message is cancelled.
Thus, handle the case in which state is MESSAGE_STATE_CANCELLED as well.

Based on patch from Yang Gu <gya...@gmail.com>
---
 src/sms.c |   99 ++++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 59 insertions(+), 40 deletions(-)

diff --git a/src/sms.c b/src/sms.c
index 73e067e..7db91cb 100644
--- a/src/sms.c
+++ b/src/sms.c
@@ -65,6 +65,7 @@ struct ofono_sms {
        struct sms_assembly *assembly;
        guint ref;
        GQueue *txq;
+       enum message_state tx_state;
        unsigned long tx_counter;
        guint tx_source;
        struct ofono_message_waiting *mw;
@@ -538,15 +539,65 @@ static void tx_queue_entry_destroy_foreach(gpointer 
_entry, gpointer unused)
        tx_queue_entry_destroy(_entry);
 }
 
+static void sms_tx_queue_remove_entry(struct ofono_sms *sms, GList *entry_list)
+{
+       struct tx_queue_entry *entry = entry_list->data;
+       struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
+
+       g_queue_delete_link(sms->txq, entry_list);
+
+       DBG("%p", entry);
+
+       if (entry->flags & OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY) {
+               enum ofono_history_sms_status hs;
+
+               switch(sms->tx_state) {
+               case MESSAGE_STATE_SENT:
+                       hs = OFONO_HISTORY_SMS_STATUS_SUBMITTED;
+                       break;
+               case MESSAGE_STATE_FAILED:
+                       hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_FAILED;
+                       break;
+               case MESSAGE_STATE_CANCELLED:
+                       hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_CANCELLED;
+                       break;
+               default:
+                       ofono_error("Unexpected sms state %d", sms->tx_state);
+
+                       return;
+               }
+
+               __ofono_history_sms_send_status(modem, &entry->uuid,
+                                                               time(NULL), hs);
+       }
+
+       if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
+               struct message *m;
+
+               sms_tx_backup_free(sms->imsi, entry->id, entry->flags,
+                                       ofono_uuid_to_str(&entry->uuid));
+
+               m = g_hash_table_lookup(sms->messages, &entry->uuid);
+
+               if (m != NULL) {
+                       message_set_state(m, sms->tx_state);
+                       g_hash_table_remove(sms->messages, &entry->uuid);
+                       message_emit_removed(m,
+                                       OFONO_MESSAGE_MANAGER_INTERFACE);
+                       message_dbus_unregister(m);
+               }
+       }
+
+       tx_queue_entry_destroy(entry);
+}
+
 static void tx_finished(const struct ofono_error *error, int mr, void *data)
 {
        struct ofono_sms *sms = data;
-       struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
        struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
        gboolean ok = error->type == OFONO_ERROR_TYPE_NO_ERROR;
-       struct message *m = NULL;
 
-       DBG("tx_finished");
+       DBG("tx_finished %p", entry);
 
        if (ok == FALSE) {
                /* Retry again when back in online mode */
@@ -554,6 +605,8 @@ static void tx_finished(const struct ofono_error *error, 
int mr, void *data)
                if (sms->registered == FALSE)
                        return;
 
+               sms->tx_state = MESSAGE_STATE_FAILED;
+
                if (!(entry->flags & OFONO_SMS_SUBMIT_FLAG_RETRY))
                        goto next_q;
 
@@ -591,47 +644,13 @@ static void tx_finished(const struct ofono_error *error, 
int mr, void *data)
                return;
        }
 
-next_q:
-       entry = g_queue_pop_head(sms->txq);
+       sms->tx_state = MESSAGE_STATE_SENT;
 
+next_q:
        if (entry->cb)
                entry->cb(ok, entry->data);
 
-       if (entry->flags & OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY) {
-               enum ofono_history_sms_status hs;
-
-               if (ok)
-                       hs = OFONO_HISTORY_SMS_STATUS_SUBMITTED;
-               else
-                       hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_FAILED;
-
-               __ofono_history_sms_send_status(modem, &entry->uuid,
-                                               time(NULL), hs);
-       }
-
-       if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
-               enum message_state ms;
-
-               sms_tx_backup_free(sms->imsi, entry->id, entry->flags,
-                                       ofono_uuid_to_str(&entry->uuid));
-
-               if (ok)
-                       ms = MESSAGE_STATE_SENT;
-               else
-                       ms = MESSAGE_STATE_FAILED;
-
-               m = g_hash_table_lookup(sms->messages, &entry->uuid);
-
-               if (m != NULL) {
-                       message_set_state(m, ms);
-                       g_hash_table_remove(sms->messages, &entry->uuid);
-                       message_emit_removed(m,
-                                       OFONO_MESSAGE_MANAGER_INTERFACE);
-                       message_dbus_unregister(m);
-               }
-       }
-
-       tx_queue_entry_destroy(entry);
+       sms_tx_queue_remove_entry(sms, g_queue_peek_head_link(sms->txq));
 
        if (sms->registered == FALSE)
                return;
-- 
1.7.4

_______________________________________________
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono

Reply via email to