---
src/smsagent.c | 416 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/smsagent.h | 66 +++++++++
2 files changed, 482 insertions(+), 0 deletions(-)
create mode 100644 src/smsagent.c
create mode 100644 src/smsagent.h
diff --git a/src/smsagent.c b/src/smsagent.c
new file mode 100644
index 0000000..bd99fa0
--- /dev/null
+++ b/src/smsagent.c
@@ -0,0 +1,416 @@
+/*
+ *
+ * oFono - Open Source Telephony
+ *
+ * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include <glib.h>
+#include <gdbus.h>
+
+#include "ofono.h"
+
+#include "common.h"
+#include "smsagent.h"
+
+enum sms_agent_port {
+ SMS_AGENT_PORT_WAP_PUSH = 2948,
+ SMS_AGENT_PORT_WAP_PUSH_S = 2949, /* Secure push?? */
+ SMS_AGENT_PORT_VCARD = 9204,
+ SMS_AGENT_PORT_VCAL = 9205,
+ SMS_AGENT_PORT_VCARD_S = 9206, /* vCard secure?? */
+ SMS_AGENT_PORT_VCAL_S = 9207, /* vCal secure?? */
+};
+
+struct sms_agent {
+ char *path;
+ char *name;
+ enum sms_agent_type type;
+ guint disconnect_watch;
+ sms_agent_remove_cb remove_cb;
+ void *remove_data;
+ GSList *reqs;
+};
+
+struct sms_agent_request {
+ struct sms_agent *agent;
+ DBusMessage *msg;
+ DBusPendingCall *call;
+ sms_agent_dispatch_cb dispatch_cb;
+ void *dispatch_data;
+ ofono_destroy_func destroy;
+};
+
+static inline const char *sms_agent_string_from_port(int port)
+{
+ switch (port) {
+ case SMS_AGENT_PORT_WAP_PUSH:
+ case SMS_AGENT_PORT_WAP_PUSH_S:
+ return "application/vnd.oma.push";
+
+ case SMS_AGENT_PORT_VCARD:
+ case SMS_AGENT_PORT_VCARD_S:
+ return "text/x-vcard";
+
+ case SMS_AGENT_PORT_VCAL:
+ case SMS_AGENT_PORT_VCAL_S:
+ return "text/calendar";
+ }
+ return NULL;
+}
+
+static inline ofono_bool_t sms_agent_type_matches_port(enum sms_agent_type type,
+ int port)
+{
+ switch (type) {
+ case SMS_AGENT_TYPE_TEXT:
+ return FALSE;
+
+ case SMS_AGENT_TYPE_VCARD:
+ return port == SMS_AGENT_PORT_VCARD
+ || port == SMS_AGENT_PORT_VCARD_S;
+
+ case SMS_AGENT_TYPE_VCAL:
+ return port == SMS_AGENT_PORT_VCAL
+ || port == SMS_AGENT_PORT_VCAL_S;
+
+ case SMS_AGENT_TYPE_WAP_PUSH:
+ return port == SMS_AGENT_PORT_WAP_PUSH
+ || port == SMS_AGENT_PORT_WAP_PUSH_S;
+ }
+ return FALSE;
+}
+
+
+static struct sms_agent_request *sms_agent_request_create(struct sms_agent *agent,
+ sms_agent_dispatch_cb cb,
+ void *user_data,
+ ofono_destroy_func destroy)
+{
+ struct sms_agent_request *req;
+
+ req = g_try_new0(struct sms_agent_request, 1);
+ if (!req)
+ return NULL;
+
+ req->agent = agent;
+ req->dispatch_cb = cb;
+ req->dispatch_data = user_data;
+ req->destroy = destroy;
+
+ return req;
+}
+
+static void sms_agent_request_destroy(struct sms_agent_request *req)
+{
+ if (req->msg) {
+ dbus_message_unref(req->msg);
+ req->msg = NULL;
+ }
+
+ if (req->call) {
+ dbus_pending_call_unref(req->call);
+ req->call = NULL;
+ }
+
+ if (req->destroy)
+ req->destroy(req->dispatch_data);
+
+ g_free(req);
+}
+
+static void sms_agent_request_cancel(struct sms_agent_request *req)
+{
+ if (!req->call)
+ return;
+
+ dbus_pending_call_cancel(req->call);
+
+ sms_agent_request_destroy(req);
+}
+
+static void sms_agent_send_noreply(struct sms_agent *agent, const char *method)
+{
+ DBusConnection *conn = ofono_dbus_get_connection();
+ DBusMessage *message;
+
+ message = dbus_message_new_method_call(agent->name, agent->path,
+ OFONO_MESSAGE_AGENT_INTERFACE,
+ method);
+
+ if (!message)
+ return;
+
+ dbus_message_set_no_reply(message, TRUE);
+
+ DBG("Sending: '%s' to '%s' at '%s'", method,
agent->name, agent->path);
+ g_dbus_send_message(conn, message);
+}
+
+static inline void sms_agent_send_release(struct sms_agent *agent)
+{
+ sms_agent_send_noreply(agent, "Release");
+}
+
+static void sms_agent_disconnect_cb(DBusConnection *conn, void *data)
+{
+ struct sms_agent *agent = data;
+
+ ofono_debug("Agent exited without calling Unregister");
+
+ agent->disconnect_watch = 0;
+
+ if (agent->remove_cb)
+ agent->remove_cb(agent, agent->remove_data);
+}
+
+struct sms_agent *sms_agent_create(const char *path, const char *name,
+ enum sms_agent_type type,
+ sms_agent_remove_cb cb,
+ void *user_data)
+{
+ struct sms_agent *agent = g_try_new0(struct sms_agent, 1);
+ DBusConnection *conn = ofono_dbus_get_connection();
+
+ if (!agent)
+ return NULL;
+
+ agent->path = g_strdup(path);
+ agent->name = g_strdup(name);
+ agent->remove_cb = cb;
+ agent->remove_data = user_data;
+ agent->type = type;
+
+ agent->disconnect_watch = g_dbus_add_disconnect_watch(conn, name,
+ sms_agent_disconnect_cb,
+ agent, NULL);
+ return agent;
+}
+
+void sms_agent_destroy(struct sms_agent *agent)
+{
+ DBusConnection *conn = ofono_dbus_get_connection();
+
+ if (!agent)
+ return;
+
+ if (agent->disconnect_watch) {
+ sms_agent_send_release(agent);
+
+ g_dbus_remove_watch(conn, agent->disconnect_watch);
+ agent->disconnect_watch = 0;
+ }
+
+ g_slist_foreach(agent->reqs, (GFunc)sms_agent_request_cancel, NULL);
+ g_slist_free(agent->reqs);
+
+ g_free(agent->path);
+ g_free(agent->name);
+ g_free(agent);
+}
+
+ofono_bool_t sms_agent_matches(struct sms_agent *agent, const char *path,
+ const char *name)
+{
+ return (path && strcmp(agent->path, path) == 0)
+ && (name && strcmp(agent->name, name) == 0);
+}
+
+static enum sms_agent_result sms_agent_check_error(struct sms_agent *agent,
+ DBusMessage *reply)
+{
+ enum sms_agent_result result = SMS_AGENT_RESULT_FAILED;
+ DBusError err;
+
+ dbus_error_init(&err);
+
+ if (!dbus_set_error_from_message(&err, reply))
+ return SMS_AGENT_RESULT_OK;
+
+ if (g_str_equal(err.name, DBUS_ERROR_NO_REPLY))
+ result = SMS_AGENT_RESULT_TIMEOUT;
+
+ ofono_debug("MessageAgent %s replied with error %s, %s",
+ agent->name, err.name, err.message);
+
+ dbus_error_free(&err);
+ return result;
+}
+
+static void sms_agent_dispatch_reply_cb(DBusPendingCall *call, void *data)
+{
+ struct sms_agent_request *req = data;
+ struct sms_agent *agent = req->agent;
+ sms_agent_dispatch_cb cb = req->dispatch_cb;
+ void *dispatch_data = req->dispatch_data;
+ DBusMessage *reply = dbus_pending_call_steal_reply(req->call);
+ enum sms_agent_result result;
+
+ result = sms_agent_check_error(agent, reply);
+
+ agent->reqs = g_slist_remove(agent->reqs, req);
+ sms_agent_request_destroy(req);
+
+ if (cb)
+ cb(agent, result, dispatch_data);
+
+ dbus_message_unref(reply);
+}
+
+int sms_agent_dispatch_text(struct sms_agent *agent, const char *from,
+ const char *sent_time,
+ const char *local_sent_time,
+ const char *message,
+ sms_agent_dispatch_cb cb, void *user_data,
+ ofono_destroy_func destroy)
+{
+ struct sms_agent_request *req;
+ DBusConnection *conn = ofono_dbus_get_connection();
+ DBusMessageIter iter;
+ DBusMessageIter dict;
+
+ if (agent->type != SMS_AGENT_TYPE_TEXT)
+ return -EOPNOTSUPP;
+
+ req = sms_agent_request_create(agent, cb, user_data, destroy);
+ if (!req)
+ return -ENOMEM;
+
+ req->msg = dbus_message_new_method_call(agent->name, agent->path,
+ OFONO_MESSAGE_AGENT_INTERFACE,
+ "HandleMessage");
+ if (!req->msg) {
+ sms_agent_request_destroy(req);
+ return -ENOMEM;
+ }
+
+ dbus_message_iter_init_append(req->msg, &iter);
+
+ dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ OFONO_PROPERTIES_ARRAY_SIGNATURE,
+ &dict);
+
+ ofono_dbus_dict_append(&dict, "LocalSentTime", DBUS_TYPE_STRING,
+ &local_sent_time);
+ ofono_dbus_dict_append(&dict, "SentTime", DBUS_TYPE_STRING,
+ &sent_time);
+ ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &from);
+
+ dbus_message_iter_close_container(&iter, &dict);
+
+ if (!dbus_connection_send_with_reply(conn, req->msg, &req->call, -1)) {
+ ofono_error("Sending D-Bus method failed");
+ sms_agent_request_destroy(req);
+ return -EIO;
+ }
+
+ agent->reqs = g_slist_append(agent->reqs, req);
+
+ dbus_pending_call_set_notify(req->call, sms_agent_dispatch_reply_cb,
+ req, NULL);
+
+ return 0;
+}
+
+int sms_agent_dispatch_data(struct sms_agent *agent, const char *from,
+ const char *sent_time,
+ const char *local_sent_time,
+ int src_port, int dst_port,
+ const unsigned char *message,
+ unsigned int len,
+ sms_agent_dispatch_cb cb, void *user_data,
+ ofono_destroy_func destroy)
+{
+ struct sms_agent_request *req;
+ DBusConnection *conn = ofono_dbus_get_connection();
+ DBusMessageIter iter;
+ DBusMessageIter array;
+ DBusMessageIter dict;
+ const char *type;
+ dbus_uint16_t src16;
+ dbus_uint16_t dst16;
+
+ if (!sms_agent_type_matches_port(agent->type, dst_port))
+ return -EOPNOTSUPP;
+
+ req = sms_agent_request_create(agent, cb, user_data, destroy);
+ if (!req)
+ return -ENOMEM;
+
+ req->msg = dbus_message_new_method_call(agent->name, agent->path,
+ OFONO_MESSAGE_AGENT_INTERFACE,
+ "HandleDatagram");
+ if (!req->msg) {
+ sms_agent_request_destroy(req);
+ return -ENOMEM;
+ }
+
+ dbus_message_iter_init_append(req->msg, &iter);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "y",
&array);
+ dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE, &message, len);
+ dbus_message_iter_close_container(&iter, &array);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+ OFONO_PROPERTIES_ARRAY_SIGNATURE,
+ &dict);
+
+ ofono_dbus_dict_append(&dict, "LocalSentTime", DBUS_TYPE_STRING,
+ &local_sent_time);
+ ofono_dbus_dict_append(&dict, "SentTime", DBUS_TYPE_STRING,
+ &sent_time);
+ ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &from);
+
+ src16 = src_port;
+ dst16 = dst_port;
+
+ type = sms_agent_string_from_port(dst_port);
+ if (type)
+ ofono_dbus_dict_append(&dict, "ContentType", DBUS_TYPE_STRING,
+ &type);
+ else
+ ofono_dbus_dict_append(&dict, "DestinationPort", DBUS_TYPE_UINT16,
+ &dst16);
+
+ ofono_dbus_dict_append(&dict, "SourcePort", DBUS_TYPE_UINT16,
&src16);
+
+ dbus_message_iter_close_container(&iter, &dict);
+
+ if (!dbus_connection_send_with_reply(conn, req->msg, &req->call, -1)) {
+ ofono_error("Sending D-Bus method failed");
+ sms_agent_request_destroy(req);
+ return -EIO;
+ }
+
+ agent->reqs = g_slist_append(agent->reqs, req);
+
+ dbus_pending_call_set_notify(req->call, sms_agent_dispatch_reply_cb,
+ req, NULL);
+
+ return 0;
+}
diff --git a/src/smsagent.h b/src/smsagent.h
new file mode 100644
index 0000000..e9f95bc
--- /dev/null
+++ b/src/smsagent.h
@@ -0,0 +1,66 @@
+/*
+ *
+ * oFono - Open Source Telephony
+ *
+ * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+struct sms_agent;
+
+enum sms_agent_type {
+ SMS_AGENT_TYPE_TEXT = 0,
+ SMS_AGENT_TYPE_VCARD,
+ SMS_AGENT_TYPE_VCAL,
+ SMS_AGENT_TYPE_WAP_PUSH,
+};
+
+enum sms_agent_result {
+ SMS_AGENT_RESULT_OK = 0,
+ SMS_AGENT_RESULT_FAILED,
+ SMS_AGENT_RESULT_TIMEOUT,
+};
+
+typedef void (*sms_agent_remove_cb) (struct sms_agent *agent, void *data);
+
+typedef void (*sms_agent_dispatch_cb) (struct sms_agent *agent,
+ enum sms_agent_result result,
+ void *data);
+
+struct sms_agent *sms_agent_create(const char *path, const char *name,
+ enum sms_agent_type type,
+ sms_agent_remove_cb cb, void *data);
+
+void sms_agent_destroy(struct sms_agent *agent);
+
+ofono_bool_t sms_agent_matches(struct sms_agent *agent, const char *name,
+ const char *path);
+
+int sms_agent_dispatch_text(struct sms_agent *agent, const char *from,
+ const char *sent_time,
+ const char *local_sent_time,
+ const char *message,
+ sms_agent_dispatch_cb cb, void *user_data,
+ ofono_destroy_func destroy);
+
+int sms_agent_dispatch_data(struct sms_agent *agent, const char *from,
+ const char *sent_time,
+ const char *local_sent_time,
+ int src_port, int dst_port,
+ const unsigned char *message,
+ unsigned int len,
+ sms_agent_dispatch_cb cb, void *user_data,
+ ofono_destroy_func destroy);
--
1.7.0.4