Skip to contentSkip to conversation

mmsd: support delivery_ind notification

PATCH New Ronald Tessier 1 participant 7 messages Sep 12, 2012

lore
Share & tools
lore
#0 lore raw

v2 cover mmsd: support delivery_ind notification

These patches concern mmsd and add support for delivery report notification. I splitted the old "Process delivery_ind notification" patch in 2 parts to use MMS_META_UUID_SUFFIX in a separate patch and process delivery_ind notification in the other one.
The ReportChanged signal documentation now comes before its implementation in the serie. Furthermore, the signal now sends a global status (delivered/partially_delivered/not_delivered) with the details as a dictionary.

Ronald Tessier (6):
service: Use MMS_META_UUID_SUFFIX
mmsutil: Decode delivery_ind msg
service: Process delivery_ind notification
doc: Add ReportChanged signal description
test: Add ReportChanged to monitored signals
service: Send a delivery changed signal

 doc/message-api.txt |    8 ++
 src/mmsutil.c       |   21 ++++-
 src/service.c       |  231 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 test/monitor-mms    |   13 +++
 4 files changed, 268 insertions(+), 5 deletions(-)

--
1.7.9.5

#1 lore raw

PATCH v2 1/6 service: Use MMS_META_UUID_SUFFIX

---
 src/service.c |   10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/service.c +6 -4
index b3ecc1e..ab88370 100644
@@ -1514,15 +1514,17 @@ static void load_messages(struct mms_service *service)
 		return;
 
 	while ((file = g_dir_read_name(dir)) != NULL) {
-		const size_t suffix_len = 7;
 		char *uuid;
+		size_t uuid_len;
 
-		if (g_str_has_suffix(file, ".status") == FALSE)
+		if (g_str_has_suffix(file, MMS_META_UUID_SUFFIX) == FALSE)
 			continue;
-		if (strlen(file) - suffix_len == 0)
+
+		uuid_len = strlen(file) - MMS_META_UUID_SUFFIX_LEN;
+		if (uuid_len == 0)
 			continue;
 
-		uuid = g_strndup(file, strlen(file) - suffix_len);
+		uuid = g_strndup(file, uuid_len);
 
 		process_message_on_start(service, uuid);
 

--
1.7.9.5

#2 lore raw

PATCH v2 2/6 mmsutil: Decode delivery_ind msg

In reply to Ronald Tessier
---
 src/mmsutil.c |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
src/mmsutil.c +20 -1
index a9a12eb..69e4b1d 100644
@@ -964,6 +964,23 @@ static gboolean decode_send_req(struct wsp_header_iter *iter,
 	return TRUE;
 }
 
+static gboolean decode_delivery_ind(struct wsp_header_iter *iter,
+						struct mms_message *out)
+{
+	return mms_parse_headers(iter, MMS_HEADER_MMS_VERSION,
+				HEADER_FLAG_MANDATORY | HEADER_FLAG_PRESET_POS,
+				&out->version,
+				MMS_HEADER_MESSAGE_ID,
+				HEADER_FLAG_MANDATORY, &out->di.msgid,
+				MMS_HEADER_TO,
+				HEADER_FLAG_MANDATORY, &out->di.to,
+				MMS_HEADER_DATE,
+				HEADER_FLAG_MANDATORY, &out->di.date,
+				MMS_HEADER_STATUS,
+				HEADER_FLAG_MANDATORY, &out->di.dr_status,
+				MMS_HEADER_INVALID);
+}
+
 #define CHECK_WELL_KNOWN_HDR(hdr)			\
 	if (wsp_header_iter_next(&iter) == FALSE)	\
 		return FALSE;				\
@@ -1016,7 +1033,7 @@ gboolean mms_message_decode(const unsigned char *pdu,
 	case MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND:
 		return FALSE;
 	case MMS_MESSAGE_TYPE_DELIVERY_IND:
-		return FALSE;
+		return decode_delivery_ind(&iter, out);
 	}
 
 	return FALSE;
@@ -1051,6 +1068,8 @@ void mms_message_free(struct mms_message *msg)
 	case MMS_MESSAGE_TYPE_ACKNOWLEDGE_IND:
 		break;
 	case MMS_MESSAGE_TYPE_DELIVERY_IND:
+		g_free(msg->di.msgid);
+		g_free(msg->di.to);
 		break;
 	}
 

--
1.7.9.5

#3 lore raw

PATCH v2 3/6 service: Process delivery_ind notification

In reply to Ronald Tessier
---
 src/service.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)
src/service.c +104 -0
index ab88370..e867aa1 100644
@@ -1300,6 +1300,107 @@ static void emit_service_removed(struct mms_service *service)
 				&service->path, DBUS_TYPE_INVALID);
 }
 
+static gboolean get_meta_by_msgid(struct mms_service *service,
+						const char *msgid,
+						char *uuid)
+{
+	GDir *dir;
+	GKeyFile *meta;
+	const char *file;
+	const char *homedir;
+	const char *service_id;
+	char *service_path;
+
+	homedir = g_get_home_dir();
+	if (homedir == NULL)
+		return FALSE;
+
+	service_id = service->identity;
+
+	service_path = g_strdup_printf("%s/.mms/%s/", homedir, service_id);
+
+	dir = g_dir_open(service_path, 0, NULL);
+	g_free(service_path);
+	if (dir == NULL)
+		return FALSE;
+
+	while ((file = g_dir_read_name(dir)) != NULL) {
+		char *id;
+
+		if (g_str_has_suffix(file, MMS_META_UUID_SUFFIX) == FALSE)
+			continue;
+
+		if (strlen(file) != MMS_META_UUID_LEN +
+						MMS_META_UUID_SUFFIX_LEN)
+			continue;
+
+		strncpy(uuid, file, MMS_META_UUID_LEN);
+		uuid[MMS_META_UUID_LEN] = 0;
+
+		meta = mms_store_meta_open(service_id, uuid);
+		if (meta == NULL)
+			goto bail;
+
+		id = g_key_file_get_string(meta, "info", "id", NULL);
+
+		mms_store_meta_close(service_id, uuid, meta, FALSE);
+
+		if (id == NULL)
+			continue;
+
+		if (g_strcmp0(msgid, id) == 0) {
+			g_free(id);
+			g_dir_close(dir);
+			return TRUE;
+		}
+
+		g_free(id);
+	}
+
+bail:
+	g_dir_close(dir);
+
+	mms_error("Report received with unmatched message id <%s>", msgid);
+
+	return FALSE;
+}
+
+static void process_delivery_ind_notification(struct mms_service *service,
+						struct mms_message *di_msg)
+{
+	GKeyFile *meta;
+	char uuid[MMS_META_UUID_LEN + 1];
+	char *path;
+	char *to;
+
+	if (get_meta_by_msgid(service, di_msg->di.msgid, uuid) == FALSE)
+		goto exit;
+
+	meta = mms_store_meta_open(service->identity, uuid);
+	if (meta == NULL)
+		return;
+
+	to = g_strdup(di_msg->di.to);
+
+	mms_address_to_string(to);
+
+	g_key_file_set_string(meta, "delivery_status", to,
+				delivery_status[di_msg->di.dr_status - 127]);
+
+	g_free(to);
+
+	mms_store_meta_close(service->identity, uuid, meta, TRUE);
+
+	path = g_strdup_printf("%s/%s/%s", MMS_PATH, service->identity, uuid);
+
+	g_free(path);
+
+exit:
+	mms_store_remove(service->identity, di_msg->uuid);
+
+	mms_message_free(di_msg);
+}
+
 static gboolean load_message_from_store(const char *service_id,
 				const char *uuid, struct mms_message *msg)
 {
@@ -1485,6 +1586,7 @@ register_sr:
 		}
 	} else if (msg->type == MMS_MESSAGE_TYPE_DELIVERY_IND) {
 		request = NULL;
+		process_delivery_ind_notification(service, msg);
 	} else
 		request = NULL;
 
@@ -2403,6 +2505,8 @@ void mms_service_push_notify(struct mms_service *service,
 
 		mms_store_meta_close(service->identity, uuid, meta, TRUE);
 
+		process_delivery_ind_notification(service, msg);
+
 		return;
 	}
 

--
1.7.9.5

#4 lore raw

PATCH v2 4/6 doc: Add ReportChanged signal description

In reply to Ronald Tessier
---
 doc/message-api.txt |    8 ++++++++
 1 file changed, 8 insertions(+)
doc/message-api.txt +8 -0
index 2922e5d..033253c 100644
@@ -31,6 +31,14 @@ Signals		PropertyChanged(string name, variant value)
 			The only expected property change is for the
 			message status.
 
+		ReportChanged(string type, variant status, dict details)
+
+			Signal that is sent when a report has been received and
+			processed. It contains the type of the report
+			("delivery_report" or other), the global status of the
+			report (delivered/partially_delivered/not_delivered) and
+			provide exact status details as a dictionary.
+
 Properties	string Status [readonly]
 
 			The status of the message.  Possible values are

--
1.7.9.5

#5 lore raw

PATCH v2 5/6 test: Add ReportChanged to monitored signals

In reply to Ronald Tessier
---
 test/monitor-mms |   13 +++++++++++++
 1 file changed, 13 insertions(+)
test/monitor-mms +13 -0
index 188d028..ab77765 100755
@@ -29,6 +29,13 @@ def property_changed(name, value, member, path, interface):
 	iface = interface[interface.rfind(".") + 1:]
 	print "{%s} [%s] %s %s" % (iface, name, member, value)
 
+def report_changed(type, status, details, path, interface):
+	iface = interface[interface.rfind(".") + 1:]
+	print "{%s} [%s] <%s> : %s" % (iface, path, type, status)
+	for key in details.keys():
+		val = str(details[key])
+		print "    %s => %s" % (key, val)
+
 if __name__ == '__main__':
 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
@@ -75,5 +82,11 @@ if __name__ == '__main__':
 						path_keyword="path",
 						interface_keyword="interface")
 
+	bus.add_signal_receiver(report_changed,
+					bus_name="org.ofono.mms",
+					signal_name = "ReportChanged",
+						path_keyword="path",
+						interface_keyword="interface")
+
 	mainloop = gobject.MainLoop()
 	mainloop.run()

--
1.7.9.5

#6 lore raw

PATCH v2 6/6 service: Send a delivery changed signal

In reply to Ronald Tessier
---
 src/service.c |  125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 121 insertions(+), 4 deletions(-)
src/service.c +121 -4
index e867aa1..f72bb6f 100644
@@ -80,6 +80,15 @@ static const char *delivery_status[] = {
 	"unreachable"
 };
 
+#define MMS_DELIVERY_STATUS_FLAG_NONE		(1 << 0)
+#define MMS_DELIVERY_STATUS_FLAG_EXPIRED	(1 << 1)
+#define MMS_DELIVERY_STATUS_FLAG_RETRIEVED	(1 << 2)
+#define MMS_DELIVERY_STATUS_FLAG_REJECTED	(1 << 3)
+#define MMS_DELIVERY_STATUS_FLAG_DEFERRED	(1 << 4)
+#define MMS_DELIVERY_STATUS_FLAG_INDETERMINATE	(1 << 5)
+#define MMS_DELIVERY_STATUS_FLAG_FORWARDED	(1 << 6)
+#define MMS_DELIVERY_STATUS_FLAG_UNREACHABLE	(1 << 7)
+
 struct mms_request;
 
 typedef gboolean (*mms_request_result_cb_t) (struct mms_request *request);
@@ -218,6 +227,83 @@ static void emit_msg_status_changed(const char *path, const char *new_status)
 	g_dbus_send_message(connection, signal);
 }
 
+static char *compute_delivery_status(char **rec_status)
+{
+	unsigned int status = 0;
+
+	while (*rec_status) {
+		if (strcmp(*rec_status, delivery_status[0]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_NONE;
+		else if (strcmp(*rec_status, delivery_status[1]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_EXPIRED;
+		else if (strcmp(*rec_status, delivery_status[2]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_RETRIEVED;
+		else if (strcmp(*rec_status, delivery_status[3]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_REJECTED;
+		else if (strcmp(*rec_status, delivery_status[4]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_DEFERRED;
+		else if (strcmp(*rec_status, delivery_status[5]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_INDETERMINATE;
+		else if (strcmp(*rec_status, delivery_status[6]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_FORWARDED;
+		else if (strcmp(*rec_status, delivery_status[7]) == 0)
+			status |= MMS_DELIVERY_STATUS_FLAG_UNREACHABLE;
+		rec_status++;
+	}
+
+	if (status & MMS_DELIVERY_STATUS_FLAG_RETRIEVED) {
+		if (status == MMS_DELIVERY_STATUS_FLAG_RETRIEVED)
+			return g_strdup("delivered");
+		return g_strdup("partially_delivered");
+	}
+
+	return g_strdup("not_delivered");
+}
+
+static void emit_msg_delivery_changed(const char *path, char **rec,
+							char **rec_status)
+{
+	DBusMessage *signal;
+	DBusMessageIter iter;
+	DBusMessageIter variant;
+	DBusMessageIter dict;
+	const char *type = "delivery_report";
+	char *dlv_status;
+
+	signal = dbus_message_new_signal(path, MMS_MESSAGE_INTERFACE,
+							"ReportChanged");
+	if (signal == NULL)
+		return;
+
+	dbus_message_iter_init_append(signal, &iter);
+
+	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &type);
+
+	dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
+					DBUS_TYPE_STRING_AS_STRING, &variant);
+
+	dlv_status = compute_delivery_status(rec_status);
+
+	dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &dlv_status);
+
+	g_free(dlv_status);
+
+	dbus_message_iter_close_container(&iter, &variant);
+
+	mms_dbus_dict_open(&iter, &dict);
+
+	while (*rec) {
+		mms_dbus_dict_append_basic(&dict, *rec, DBUS_TYPE_STRING,
+								&(*rec_status));
+		rec++;
+		rec_status++;
+	}
+
+	mms_dbus_dict_close(&iter, &dict);
+
+	g_dbus_send_message(connection, signal);
+}
+
 static DBusMessage *msg_mark_read(DBusConnection *conn,
 					DBusMessage *msg, void *user_data)
 {
@@ -269,6 +355,10 @@ static const GDBusMethodTable message_methods[] = {
 static const GDBusSignalTable message_signals[] = {
 	{ GDBUS_SIGNAL("PropertyChanged",
 			GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
+	{ GDBUS_SIGNAL("ReportChanged",
+			GDBUS_ARGS({ "name", "s" },
+					{ "glob_status", "v" },
+					{ "details", "a{sv}" })) },
 	{ }
 };
 
@@ -1372,6 +1462,11 @@ static void process_delivery_ind_notification(struct mms_service *service,
 	char uuid[MMS_META_UUID_LEN + 1];
 	char *path;
 	char *to;
+	char **tos;
+	char **statutes;
+	gsize length;
+	unsigned int i;
+	const char *new_status;
 
 	if (get_meta_by_msgid(service, di_msg->di.msgid, uuid) == FALSE)
 		goto exit;
@@ -1384,17 +1479,39 @@ static void process_delivery_ind_notification(struct mms_service *service,
 
 	mms_address_to_string(to);
 
-	g_key_file_set_string(meta, "delivery_status", to,
-				delivery_status[di_msg->di.dr_status - 127]);
+	new_status = delivery_status[di_msg->di.dr_status - 127];
 
-	g_free(to);
+	g_key_file_set_string(meta, "delivery_status", to, new_status);
+
+	path = g_strdup_printf("%s/%s/%s", MMS_PATH, service->identity, uuid);
+
+	statutes = NULL;
+
+	tos = g_key_file_get_keys(meta, "delivery_status", &length, NULL);
+	if (tos == NULL)
+		goto bail;
+
+	statutes = g_try_new0(char *, length + 1);
+	if (statutes == NULL)
+		goto bail;
+
+	for (i = 0; i < length; i++)
+		statutes[i] = g_key_file_get_string(meta, "delivery_status",
+								tos[i], NULL);
 
 	mms_store_meta_close(service->identity, uuid, meta, TRUE);
 
-	path = g_strdup_printf("%s/%s/%s", MMS_PATH, service->identity, uuid);
+	emit_msg_delivery_changed(path, tos, statutes);
+
+bail:
+	g_strfreev(statutes);
+
+	g_strfreev(tos);
 
 	g_free(path);
 
+	g_free(to);
+
 exit:
 	mms_store_remove(service->identity, di_msg->uuid);
 

--
1.7.9.5