5 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "mns-tizen.h"
40 #define OBEX_MNS_UUID \
41 "\xBB\x58\x2B\x41\x42\x0C\x11\xDB\xB0\xDE\x08\x00\x20\x0C\x9A\x66"
42 #define OBEX_MNS_UUID_LEN 16
44 #define MNS_INTERFACE "org.openobex.MessageNotification"
45 #define ERROR_INF MNS_INTERFACE ".Error"
46 #define MNS_UUID "00001133-0000-1000-8000-00805f9b34fb"
49 EVENT_TYPE_NEW_MESSAGE,
50 EVENT_TYPE_DELIVERY_SUCCESS,
51 EVENT_TYPE_SENDING_SUCCESS,
52 EVENT_TYPE_DELIVERY_FAILURE,
53 EVENT_TYPE_SENDING_FAILURE,
54 EVENT_TYPE_MEMORY_FULL,
55 EVENT_TYPE_MEMORY_AVAILABLE,
56 EVENT_TYPE_MESSAGE_DELETED,
57 EVENT_TYPE_MESSAGE_SHIFT,
61 struct sendevent_apparam {
62 uint8_t masinstanceid_tag;
63 uint8_t masinstanceid_len;
64 uint8_t masinstanceid;
65 } __attribute__ ((packed));
68 struct obc_session *session;
72 static DBusConnection *conn = NULL;
74 static int get_event_type(gchar *event_type)
76 DBG("event_type = %s\n", event_type);
78 if (!g_strcmp0(event_type, "NewMessage"))
79 return EVENT_TYPE_NEW_MESSAGE;
80 else if (!g_strcmp0(event_type, "DeliverySuccess"))
81 return EVENT_TYPE_DELIVERY_SUCCESS;
82 else if (!g_strcmp0(event_type, "SendingSuccess"))
83 return EVENT_TYPE_SENDING_SUCCESS;
84 else if (!g_strcmp0(event_type, "DeliveryFailure"))
85 return EVENT_TYPE_DELIVERY_FAILURE;
86 else if (!g_strcmp0(event_type, "SendingFailure"))
87 return EVENT_TYPE_SENDING_FAILURE;
88 else if (!g_strcmp0(event_type, "MemoryFull"))
89 return EVENT_TYPE_MEMORY_FULL;
90 else if (!g_strcmp0(event_type, "MemoryAvailable"))
91 return EVENT_TYPE_MEMORY_AVAILABLE;
92 else if (!g_strcmp0(event_type, "MessageDeleted"))
93 return EVENT_TYPE_MESSAGE_DELETED;
94 else if (!g_strcmp0(event_type, "MessageShift"))
95 return EVENT_TYPE_MESSAGE_SHIFT;
97 return EVENT_TYPE_UNKNOWN;
101 static gchar *generate_event_report(gchar *event_type,
102 guint64 handle, gchar *folder,
103 gchar *old_folder, gchar *msg_type)
108 event = get_event_type(event_type);
109 if (event == EVENT_TYPE_UNKNOWN)
112 buf = g_string_new("<MAP-event-report version=\"1.0\">");
113 g_string_append_printf(buf, "<event type=\"%s\" ", event_type);
115 if (event == EVENT_TYPE_MEMORY_FULL ||
116 event == EVENT_TYPE_MEMORY_AVAILABLE)
119 g_string_append_printf(buf, "handle=\"%llx\" ", handle);
120 g_string_append_printf(buf, "folder=\"%s\" ", folder);
122 if (event == EVENT_TYPE_MESSAGE_SHIFT)
123 g_string_append_printf(buf, " old_folder=\"%s\" ", old_folder);
125 g_string_append_printf(buf, "msg_type=\"%s\" ", msg_type);
128 g_string_append(buf, "/></MAP-event-report>");
130 return g_string_free(buf, FALSE);
133 static DBusMessage *send_event(DBusConnection *connection,
134 DBusMessage *message, void *user_data)
136 struct mns_data *mns = user_data;
137 struct obc_transfer *transfer;
138 struct sendevent_apparam apparam;
148 if (dbus_message_get_args(message, NULL,
149 DBUS_TYPE_STRING, &event_type,
150 DBUS_TYPE_UINT64, &handle,
151 DBUS_TYPE_STRING, &folder,
152 DBUS_TYPE_STRING, &old_folder,
153 DBUS_TYPE_STRING, &msg_type,
154 DBUS_TYPE_INVALID) == FALSE)
155 return g_dbus_create_error(message,
156 "org.openobex.Error.InvalidArguments",
159 buf = generate_event_report(event_type, handle, folder,
160 old_folder, msg_type);
162 return g_dbus_create_error(message,
163 "org.openobex.Error.InvalidArguments", NULL);
165 transfer = obc_transfer_put("x-bt/MAP-event-report", NULL, NULL,
166 buf, strlen(buf), &err);
170 if (transfer == NULL)
173 apparam.masinstanceid_tag = MAP_AP_MASINSTANCEID;
174 apparam.masinstanceid_len = 1;
175 /* Obexd currently supports single SDP for MAS */
176 apparam.masinstanceid = 0;
178 obc_transfer_set_apparam(transfer, &apparam);
180 if (obc_session_queue(mns->session, transfer, NULL, NULL, &err))
181 return dbus_message_new_method_return(message);
184 reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
190 static GDBusMethodTable mns_methods[] = {
191 { GDBUS_ASYNC_METHOD("SendEvent",
192 GDBUS_ARGS({ "event_type", "s" }, { "handle", "t" },
193 { "folder", "s" }, { "old_folder", "s" },
194 { "msg_type", "s" }),
200 static void mns_free(void *data)
202 struct mns_data *mns = data;
204 obc_session_unref(mns->session);
208 static int mns_probe(struct obc_session *session)
210 struct mns_data *mns;
213 path = obc_session_get_path(session);
217 mns = g_try_new0(struct mns_data, 1);
221 mns->session = obc_session_ref(session);
223 if (!g_dbus_register_interface(conn, path, MNS_INTERFACE, mns_methods,
224 NULL, NULL, mns, mns_free)) {
232 static void mns_remove(struct obc_session *session)
234 const char *path = obc_session_get_path(session);
238 g_dbus_unregister_interface(conn, path, MNS_INTERFACE);
241 static struct obc_driver mns = {
244 .target = OBEX_MNS_UUID,
245 .target_len = OBEX_MNS_UUID_LEN,
256 conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
260 err = obc_driver_register(&mns);
262 dbus_connection_unref(conn);
274 dbus_connection_unref(conn);
277 obc_driver_unregister(&mns);