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
35 struct ofono_uuid uuid;
36 enum message_state state;
37 struct ofono_atom *atom;
41 static const char *message_state_to_string(enum message_state s)
44 case MESSAGE_STATE_PENDING:
46 case MESSAGE_STATE_SENT:
48 case MESSAGE_STATE_FAILED:
50 case MESSAGE_STATE_CANCELLED:
57 static DBusMessage *message_get_properties(DBusConnection *conn,
58 DBusMessage *msg, void *data)
60 struct message *m = data;
65 reply = dbus_message_new_method_return(msg);
69 dbus_message_iter_init_append(reply, &iter);
71 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
72 OFONO_PROPERTIES_ARRAY_SIGNATURE,
74 message_append_properties(m, &dict);
75 dbus_message_iter_close_container(&iter, &dict);
80 static DBusMessage *message_cancel(DBusConnection *conn,
81 DBusMessage *msg, void *data)
83 struct message *m = data;
86 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID) == FALSE)
87 return __ofono_error_invalid_args(msg);
89 if (m->state != MESSAGE_STATE_PENDING)
90 return __ofono_error_not_available(msg);
92 res = __ofono_sms_txq_cancel(__ofono_atom_get_data(m->atom), &m->uuid);
96 return __ofono_error_not_found(msg);
98 return __ofono_error_access_denied(msg);
100 return dbus_message_new_method_return(msg);
102 return __ofono_error_failed(msg);
106 static GDBusMethodTable message_methods[] = {
107 { "GetProperties", "", "a{sv}", message_get_properties },
108 { "Cancel", "", "", message_cancel },
112 static GDBusSignalTable message_signals[] = {
113 { "PropertyChanged", "sv" },
117 struct message *message_create(const struct ofono_uuid *uuid,
118 struct ofono_atom *atom)
122 v = g_try_new0(struct message, 1);
126 memcpy(&v->uuid, uuid, sizeof(*uuid));
133 static void message_destroy(gpointer userdata)
135 struct message *m = userdata;
140 gboolean message_dbus_register(struct message *m)
142 DBusConnection *conn = ofono_dbus_get_connection();
143 const char *path = message_path_from_uuid(m->atom, &m->uuid);
145 if (!g_dbus_register_interface(conn, path, OFONO_MESSAGE_INTERFACE,
146 message_methods, message_signals,
147 NULL, m, message_destroy)) {
148 ofono_error("Could not register Message %s", path);
157 void message_dbus_unregister(struct message *m)
159 DBusConnection *conn = ofono_dbus_get_connection();
160 const char *path = message_path_from_uuid(m->atom, &m->uuid);
162 g_dbus_unregister_interface(conn, path, OFONO_MESSAGE_INTERFACE);
167 const struct ofono_uuid *message_get_uuid(const struct message *m)
172 void message_set_state(struct message *m, enum message_state new_state)
174 DBusConnection *conn = ofono_dbus_get_connection();
178 if (m->state == new_state)
181 path = message_path_from_uuid(m->atom, &m->uuid);
183 m->state = new_state;
184 state = message_state_to_string(m->state);
186 ofono_dbus_signal_property_changed(conn, path, OFONO_MESSAGE_INTERFACE,
192 void message_append_properties(struct message *m, DBusMessageIter *dict)
194 const char *state = message_state_to_string(m->state);
196 ofono_dbus_dict_append(dict, "State", DBUS_TYPE_STRING, &state);
199 void message_emit_added(struct message *m, const char *interface)
202 DBusMessageIter iter;
203 DBusMessageIter dict;
205 const char *atompath = __ofono_atom_get_path(m->atom);
207 signal = dbus_message_new_signal(atompath, interface, "MessageAdded");
211 path = message_path_from_uuid(m->atom, &m->uuid);
213 dbus_message_iter_init_append(signal, &iter);
215 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
217 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
218 OFONO_PROPERTIES_ARRAY_SIGNATURE,
220 message_append_properties(m, &dict);
221 dbus_message_iter_close_container(&iter, &dict);
223 g_dbus_send_message(ofono_dbus_get_connection(), signal);
226 void message_emit_removed(struct message *m, const char *interface)
228 DBusConnection *conn = ofono_dbus_get_connection();
229 const char *atompath = __ofono_atom_get_path(m->atom);
230 const char *path = message_path_from_uuid(m->atom, &m->uuid);
232 g_dbus_emit_signal(conn, atompath, interface, "MessageRemoved",
233 DBUS_TYPE_OBJECT_PATH,
238 const char *message_path_from_uuid(struct ofono_atom *atom,
239 const struct ofono_uuid *uuid)
241 static char path[256];
242 const char *atompath = __ofono_atom_get_path(atom);
244 snprintf(path, sizeof(path), "%s/message_%s", atompath,
245 ofono_uuid_to_str(uuid));
250 void *message_get_data(struct message *m)
255 void message_set_data(struct message *m, void *data)