sms: Use __ofono_atom_find
[platform/upstream/ofono.git] / src / cdma-sms.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2010-2011  Nokia Corporation and/or its subsidiary(-ies).
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29
30 #include <glib.h>
31 #include <gdbus.h>
32 #include <sys/time.h>
33
34 #include "ofono.h"
35
36 #include "cdma-smsutil.h"
37
38 static GSList *g_drivers;
39
40 struct ofono_cdma_sms {
41         const struct ofono_cdma_sms_driver *driver;
42         void *driver_data;
43         struct ofono_atom *atom;
44 };
45
46 static GDBusMethodTable cdma_sms_manager_methods[] = {
47         /* TODO */
48         { }
49 };
50
51 static GDBusSignalTable cdma_sms_manager_signals[] = {
52         { "IncomingMessage",    "sa{sv}"        },
53         /* TODO */
54         { }
55 };
56
57 static void cdma_dispatch_text_message(struct ofono_cdma_sms *cdma_sms,
58                                         const char *message,
59                                         const char *oaddr)
60 {
61         const char *path = __ofono_atom_get_path(cdma_sms->atom);
62         DBusConnection *conn = ofono_dbus_get_connection();
63         DBusMessage *signal;
64         DBusMessageIter iter;
65         DBusMessageIter dict;
66         const char *signal_name;
67
68         /* TODO: Support ImmediateMessage */
69         signal_name = "IncomingMessage";
70
71         signal = dbus_message_new_signal(path,
72                                         OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
73                                         signal_name);
74         if (signal == NULL)
75                 return;
76
77         dbus_message_iter_init_append(signal, &iter);
78
79         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
80
81         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
82                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
83                                         &dict);
84
85         ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &oaddr);
86
87         /* TODO: Other properties not supported yet */
88
89         dbus_message_iter_close_container(&iter, &dict);
90
91         g_dbus_send_message(conn, signal);
92
93         /*TODO: Add the message to history*/
94 }
95
96 static void ofono_cdma_sms_process_wmt_deliver(struct ofono_cdma_sms *cdma_sms,
97                                                 const struct cdma_sms *incoming)
98 {
99         char *message;
100         const char *oaddr;
101         const struct cdma_sms_ud *ud;
102
103         ud = &incoming->p2p_msg.bd.wmt_deliver.ud;
104
105         /*
106          * If incoming message does not contain USER DATA, still
107          * send indication to upper layer but with empty string.
108          */
109         if (check_bitmap(incoming->p2p_msg.bd.subparam_bitmap,
110                                 CDMA_SMS_SUBPARAM_ID_USER_DATA) == FALSE)
111                 message = g_new0(char, 1);
112         else
113                 message = cdma_sms_decode_text(ud);
114
115         if (message == NULL)
116                 return;
117
118         oaddr = cdma_sms_address_to_string(&incoming->p2p_msg.oaddr);
119         if (oaddr == NULL) {
120                 g_free(message);
121                 return;
122         }
123
124         cdma_dispatch_text_message(cdma_sms, message, oaddr);
125
126         g_free(message);
127 }
128
129 static void ofono_cdma_sms_process_wmt(struct ofono_cdma_sms *cdma_sms,
130                                         struct cdma_sms *incoming)
131 {
132         /* TODO: Add duplicate detection support */
133
134         switch (incoming->p2p_msg.bd.id.msg_type) {
135         case CDMA_SMS_MSG_TYPE_RESERVED:
136                 break;
137         case CDMA_SMS_MSG_TYPE_DELIVER:
138                 ofono_cdma_sms_process_wmt_deliver(cdma_sms, incoming);
139                 break;
140         case CDMA_SMS_MSG_TYPE_SUBMIT:
141         case CDMA_SMS_MSG_TYPE_CANCEL:
142         case CDMA_SMS_MSG_TYPE_DELIVER_ACK:
143         case CDMA_SMS_MSG_TYPE_USER_ACK:
144         case CDMA_SMS_MSG_TYPE_READ_ACK:
145         case CDMA_SMS_MSG_TYPE_DELIVER_REPORT:
146         case CDMA_SMS_MSG_TYPE_SUBMIT_REPORT:
147                 /* TODO */
148                 break;
149         }
150 }
151
152 static void ofono_cdma_sms_process_p2p(struct ofono_cdma_sms *cdma_sms,
153                                         struct cdma_sms *incoming)
154 {
155         switch (incoming->p2p_msg.teleservice_id) {
156         case CDMA_SMS_TELESERVICE_ID_CMT91:
157         case CDMA_SMS_TELESERVICE_ID_WPT:
158                 break; /* TODO: Not supported yet */
159         case CDMA_SMS_TELESERVICE_ID_WMT:
160                 ofono_cdma_sms_process_wmt(cdma_sms, incoming);
161                 break;
162         case CDMA_SMS_TELESERVICE_ID_VMN:
163         case CDMA_SMS_TELESERVICE_ID_WAP:
164         case CDMA_SMS_TELESERVICE_ID_WEMT:
165         case CDMA_SMS_TELESERVICE_ID_SCPT:
166         case CDMA_SMS_TELESERVICE_ID_CATPT:
167                 break; /* TODO: Not supported yet */
168         }
169 }
170
171 void ofono_cdma_sms_deliver_notify(struct ofono_cdma_sms *cdma_sms,
172                                         unsigned char *pdu, int tpdu_len)
173 {
174         static struct cdma_sms s;
175
176         DBG("tpdu len %d", tpdu_len);
177
178         memset(&s, 0, sizeof(struct cdma_sms));
179
180         if (cdma_sms_decode(pdu, tpdu_len, &s) == FALSE)
181                 return;
182
183         switch (s.type) {
184         case CDMA_SMS_TP_MSG_TYPE_P2P:
185                 ofono_cdma_sms_process_p2p(cdma_sms, &s);
186                 break;
187         case CDMA_SMS_TP_MSG_TYPE_BCAST:
188         case CDMA_SMS_TP_MSG_TYPE_ACK:
189                 /*
190                  * TODO: Support SMS Broadcast Message and SMS
191                  * Acknowledge Message.
192                  */
193                 break;
194         }
195 }
196
197 int ofono_cdma_sms_driver_register(const struct ofono_cdma_sms_driver *d)
198 {
199         DBG("driver: %p, name: %s", d, d->name);
200
201         if (d->probe == NULL)
202                 return -EINVAL;
203
204         g_drivers = g_slist_prepend(g_drivers, (void *)d);
205
206         return 0;
207 }
208
209 void ofono_cdma_sms_driver_unregister(const struct ofono_cdma_sms_driver *d)
210 {
211         DBG("driver: %p, name: %s", d, d->name);
212
213         g_drivers = g_slist_remove(g_drivers, (void *)d);
214 }
215
216 static void cdma_sms_unregister(struct ofono_atom *atom)
217 {
218         DBusConnection *conn = ofono_dbus_get_connection();
219         struct ofono_modem *modem = __ofono_atom_get_modem(atom);
220         const char *path = __ofono_atom_get_path(atom);
221
222         g_dbus_unregister_interface(conn, path,
223                                         OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
224
225         ofono_modem_remove_interface(modem,
226                                         OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
227 }
228
229 static void cdma_sms_remove(struct ofono_atom *atom)
230 {
231         struct ofono_cdma_sms *cdma_sms = __ofono_atom_get_data(atom);
232
233         DBG("atom: %p", atom);
234
235         if (cdma_sms == NULL)
236                 return;
237
238         if (cdma_sms->driver && cdma_sms->driver->remove)
239                 cdma_sms->driver->remove(cdma_sms);
240
241         g_free(cdma_sms);
242 }
243
244 /*
245  * Create a CDMA SMS driver
246  *
247  * This creates a CDMA SMS driver that is hung off a @modem
248  * object. However, for the driver to be used by the system, it has to
249  * be registered with the oFono core using ofono_sms_register().
250  *
251  * This is done once the modem driver determines that SMS is properly
252  * supported by the hardware.
253  */
254 struct ofono_cdma_sms *ofono_cdma_sms_create(struct ofono_modem *modem,
255                                                 unsigned int vendor,
256                                                 const char *driver,
257                                                 void *data)
258 {
259         struct ofono_cdma_sms *cdma_sms;
260         GSList *l;
261
262         if (driver == NULL)
263                 return NULL;
264
265         cdma_sms = g_try_new0(struct ofono_cdma_sms, 1);
266         if (cdma_sms == NULL)
267                 return NULL;
268
269         cdma_sms->atom = __ofono_modem_add_atom(modem,
270                                                 OFONO_ATOM_TYPE_CDMA_SMS,
271                                                 cdma_sms_remove, cdma_sms);
272
273         for (l = g_drivers; l; l = l->next) {
274                 const struct ofono_cdma_sms_driver *drv = l->data;
275
276                 if (g_strcmp0(drv->name, driver))
277                         continue;
278
279                 if (drv->probe(cdma_sms, vendor, data) < 0)
280                         continue;
281
282                 cdma_sms->driver = drv;
283                 break;
284         }
285
286         return cdma_sms;
287 }
288
289 /*
290  * Indicate oFono that a CDMA SMS driver is ready for operation
291  *
292  * This is called after ofono_cdma_sms_create() was done and the modem
293  * driver determined that a modem supports SMS correctly. Once this
294  * call succeeds, the D-BUS interface for SMS goes live.
295  */
296 void ofono_cdma_sms_register(struct ofono_cdma_sms *cdma_sms)
297 {
298         DBusConnection *conn = ofono_dbus_get_connection();
299         struct ofono_modem *modem = __ofono_atom_get_modem(cdma_sms->atom);
300         const char *path = __ofono_atom_get_path(cdma_sms->atom);
301
302         if (!g_dbus_register_interface(conn, path,
303                                         OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
304                                         cdma_sms_manager_methods,
305                                         cdma_sms_manager_signals,
306                                         NULL, cdma_sms, NULL)) {
307                 ofono_error("Could not create %s interface",
308                                 OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
309                 return;
310         }
311
312         ofono_modem_add_interface(modem, OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
313
314         __ofono_atom_register(cdma_sms->atom, cdma_sms_unregister);
315 }
316
317 void ofono_cdma_sms_remove(struct ofono_cdma_sms *cdma_sms)
318 {
319         __ofono_atom_free(cdma_sms->atom);
320 }
321
322 void ofono_cdma_sms_set_data(struct ofono_cdma_sms *cdma_sms, void *data)
323 {
324         cdma_sms->driver_data = data;
325 }
326
327 void *ofono_cdma_sms_get_data(struct ofono_cdma_sms *cdma_sms)
328 {
329         return cdma_sms->driver_data;
330 }