3 * oFono - Open Source Telephony
5 * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
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
36 #include "cdma-smsutil.h"
38 static GSList *g_drivers;
40 struct ofono_cdma_sms {
41 const struct ofono_cdma_sms_driver *driver;
43 struct ofono_atom *atom;
46 static const GDBusMethodTable cdma_sms_manager_methods[] = {
51 static const GDBusSignalTable cdma_sms_manager_signals[] = {
52 { _GDBUS_SIGNAL("IncomingMessage", "sa{sv}",
53 GDBUS_ARGS({ "message", "s"}, { "info", "a{sv}" })) },
58 static void cdma_dispatch_text_message(struct ofono_cdma_sms *cdma_sms,
62 const char *path = __ofono_atom_get_path(cdma_sms->atom);
63 DBusConnection *conn = ofono_dbus_get_connection();
67 const char *signal_name;
69 /* TODO: Support ImmediateMessage */
70 signal_name = "IncomingMessage";
72 signal = dbus_message_new_signal(path,
73 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
78 dbus_message_iter_init_append(signal, &iter);
80 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
82 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
83 OFONO_PROPERTIES_ARRAY_SIGNATURE,
86 ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &oaddr);
88 /* TODO: Other properties not supported yet */
90 dbus_message_iter_close_container(&iter, &dict);
92 g_dbus_send_message(conn, signal);
94 /*TODO: Add the message to history*/
97 static void ofono_cdma_sms_process_wmt_deliver(struct ofono_cdma_sms *cdma_sms,
98 const struct cdma_sms *incoming)
102 const struct cdma_sms_ud *ud;
104 ud = &incoming->p2p_msg.bd.wmt_deliver.ud;
107 * If incoming message does not contain USER DATA, still
108 * send indication to upper layer but with empty string.
110 if (check_bitmap(incoming->p2p_msg.bd.subparam_bitmap,
111 CDMA_SMS_SUBPARAM_ID_USER_DATA) == FALSE)
112 message = g_new0(char, 1);
114 message = cdma_sms_decode_text(ud);
119 oaddr = cdma_sms_address_to_string(&incoming->p2p_msg.oaddr);
125 cdma_dispatch_text_message(cdma_sms, message, oaddr);
130 static void ofono_cdma_sms_process_wmt(struct ofono_cdma_sms *cdma_sms,
131 struct cdma_sms *incoming)
133 /* TODO: Add duplicate detection support */
135 switch (incoming->p2p_msg.bd.id.msg_type) {
136 case CDMA_SMS_MSG_TYPE_RESERVED:
138 case CDMA_SMS_MSG_TYPE_DELIVER:
139 ofono_cdma_sms_process_wmt_deliver(cdma_sms, incoming);
141 case CDMA_SMS_MSG_TYPE_SUBMIT:
142 case CDMA_SMS_MSG_TYPE_CANCEL:
143 case CDMA_SMS_MSG_TYPE_DELIVER_ACK:
144 case CDMA_SMS_MSG_TYPE_USER_ACK:
145 case CDMA_SMS_MSG_TYPE_READ_ACK:
146 case CDMA_SMS_MSG_TYPE_DELIVER_REPORT:
147 case CDMA_SMS_MSG_TYPE_SUBMIT_REPORT:
153 static void ofono_cdma_sms_process_p2p(struct ofono_cdma_sms *cdma_sms,
154 struct cdma_sms *incoming)
156 switch (incoming->p2p_msg.teleservice_id) {
157 case CDMA_SMS_TELESERVICE_ID_CMT91:
158 case CDMA_SMS_TELESERVICE_ID_WPT:
159 break; /* TODO: Not supported yet */
160 case CDMA_SMS_TELESERVICE_ID_WMT:
161 ofono_cdma_sms_process_wmt(cdma_sms, incoming);
163 case CDMA_SMS_TELESERVICE_ID_VMN:
164 case CDMA_SMS_TELESERVICE_ID_WAP:
165 case CDMA_SMS_TELESERVICE_ID_WEMT:
166 case CDMA_SMS_TELESERVICE_ID_SCPT:
167 case CDMA_SMS_TELESERVICE_ID_CATPT:
168 break; /* TODO: Not supported yet */
172 void ofono_cdma_sms_deliver_notify(struct ofono_cdma_sms *cdma_sms,
173 unsigned char *pdu, int tpdu_len)
175 static struct cdma_sms s;
177 DBG("tpdu len %d", tpdu_len);
179 memset(&s, 0, sizeof(struct cdma_sms));
181 if (cdma_sms_decode(pdu, tpdu_len, &s) == FALSE)
185 case CDMA_SMS_TP_MSG_TYPE_P2P:
186 ofono_cdma_sms_process_p2p(cdma_sms, &s);
188 case CDMA_SMS_TP_MSG_TYPE_BCAST:
189 case CDMA_SMS_TP_MSG_TYPE_ACK:
191 * TODO: Support SMS Broadcast Message and SMS
192 * Acknowledge Message.
198 int ofono_cdma_sms_driver_register(const struct ofono_cdma_sms_driver *d)
200 DBG("driver: %p, name: %s", d, d->name);
202 if (d->probe == NULL)
205 g_drivers = g_slist_prepend(g_drivers, (void *)d);
210 void ofono_cdma_sms_driver_unregister(const struct ofono_cdma_sms_driver *d)
212 DBG("driver: %p, name: %s", d, d->name);
214 g_drivers = g_slist_remove(g_drivers, (void *)d);
217 static void cdma_sms_unregister(struct ofono_atom *atom)
219 DBusConnection *conn = ofono_dbus_get_connection();
220 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
221 const char *path = __ofono_atom_get_path(atom);
223 g_dbus_unregister_interface(conn, path,
224 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
226 ofono_modem_remove_interface(modem,
227 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
230 static void cdma_sms_remove(struct ofono_atom *atom)
232 struct ofono_cdma_sms *cdma_sms = __ofono_atom_get_data(atom);
234 DBG("atom: %p", atom);
236 if (cdma_sms == NULL)
239 if (cdma_sms->driver && cdma_sms->driver->remove)
240 cdma_sms->driver->remove(cdma_sms);
246 * Create a CDMA SMS driver
248 * This creates a CDMA SMS driver that is hung off a @modem
249 * object. However, for the driver to be used by the system, it has to
250 * be registered with the oFono core using ofono_sms_register().
252 * This is done once the modem driver determines that SMS is properly
253 * supported by the hardware.
255 struct ofono_cdma_sms *ofono_cdma_sms_create(struct ofono_modem *modem,
260 struct ofono_cdma_sms *cdma_sms;
266 cdma_sms = g_try_new0(struct ofono_cdma_sms, 1);
267 if (cdma_sms == NULL)
270 cdma_sms->atom = __ofono_modem_add_atom(modem,
271 OFONO_ATOM_TYPE_CDMA_SMS,
272 cdma_sms_remove, cdma_sms);
274 for (l = g_drivers; l; l = l->next) {
275 const struct ofono_cdma_sms_driver *drv = l->data;
277 if (g_strcmp0(drv->name, driver))
280 if (drv->probe(cdma_sms, vendor, data) < 0)
283 cdma_sms->driver = drv;
291 * Indicate oFono that a CDMA SMS driver is ready for operation
293 * This is called after ofono_cdma_sms_create() was done and the modem
294 * driver determined that a modem supports SMS correctly. Once this
295 * call succeeds, the D-BUS interface for SMS goes live.
297 void ofono_cdma_sms_register(struct ofono_cdma_sms *cdma_sms)
299 DBusConnection *conn = ofono_dbus_get_connection();
300 struct ofono_modem *modem = __ofono_atom_get_modem(cdma_sms->atom);
301 const char *path = __ofono_atom_get_path(cdma_sms->atom);
303 if (!g_dbus_register_interface(conn, path,
304 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
305 cdma_sms_manager_methods,
306 cdma_sms_manager_signals,
307 NULL, cdma_sms, NULL)) {
308 ofono_error("Could not create %s interface",
309 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
313 ofono_modem_add_interface(modem, OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
315 __ofono_atom_register(cdma_sms->atom, cdma_sms_unregister);
318 void ofono_cdma_sms_remove(struct ofono_cdma_sms *cdma_sms)
320 __ofono_atom_free(cdma_sms->atom);
323 void ofono_cdma_sms_set_data(struct ofono_cdma_sms *cdma_sms, void *data)
325 cdma_sms->driver_data = data;
328 void *ofono_cdma_sms_get_data(struct ofono_cdma_sms *cdma_sms)
330 return cdma_sms->driver_data;