summaryrefslogtreecommitdiffstats
path: root/src/cdma-sms.c
blob: 5beb8b0b821232ec555dc456bba64d19de53a822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2010-2011  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

#include <string.h>
#include <stdio.h>
#include <errno.h>

#include <glib.h>
#include <gdbus.h>
#include <sys/time.h>

#include "ofono.h"

#include "cdma-smsutil.h"

static GSList *g_drivers;

struct ofono_cdma_sms {
	const struct ofono_cdma_sms_driver *driver;
	void *driver_data;
	struct ofono_atom *atom;
};

static const GDBusMethodTable cdma_sms_manager_methods[] = {
	/* TODO */
	{ }
};

static const GDBusSignalTable cdma_sms_manager_signals[] = {
	{ GDBUS_SIGNAL("IncomingMessage",
			GDBUS_ARGS({ "message", "s"}, { "info", "a{sv}" })) },
	/* TODO */
	{ }
};

static void cdma_dispatch_text_message(struct ofono_cdma_sms *cdma_sms,
					const char *message,
					const char *oaddr)
{
	const char *path = __ofono_atom_get_path(cdma_sms->atom);
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *signal;
	DBusMessageIter iter;
	DBusMessageIter dict;
	const char *signal_name;

	/* TODO: Support ImmediateMessage */
	signal_name = "IncomingMessage";

	signal = dbus_message_new_signal(path,
					OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
					signal_name);
	if (signal == NULL)
		return;

	dbus_message_iter_init_append(signal, &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, "Sender", DBUS_TYPE_STRING, &oaddr);

	/* TODO: Other properties not supported yet */

	dbus_message_iter_close_container(&iter, &dict);

	g_dbus_send_message(conn, signal);

	/*TODO: Add the message to history*/
}

static void ofono_cdma_sms_process_wmt_deliver(struct ofono_cdma_sms *cdma_sms,
						const struct cdma_sms *incoming)
{
	char *message;
	const char *oaddr;
	const struct cdma_sms_ud *ud;

	ud = &incoming->p2p_msg.bd.wmt_deliver.ud;

	/*
	 * If incoming message does not contain USER DATA, still
	 * send indication to upper layer but with empty string.
	 */
	if (check_bitmap(incoming->p2p_msg.bd.subparam_bitmap,
				CDMA_SMS_SUBPARAM_ID_USER_DATA) == FALSE)
		message = g_new0(char, 1);
	else
		message = cdma_sms_decode_text(ud);

	if (message == NULL)
		return;

	oaddr = cdma_sms_address_to_string(&incoming->p2p_msg.oaddr);
	if (oaddr == NULL) {
		g_free(message);
		return;
	}

	cdma_dispatch_text_message(cdma_sms, message, oaddr);

	g_free(message);
}

static void ofono_cdma_sms_process_wmt(struct ofono_cdma_sms *cdma_sms,
					struct cdma_sms *incoming)
{
	/* TODO: Add duplicate detection support */

	switch (incoming->p2p_msg.bd.id.msg_type) {
	case CDMA_SMS_MSG_TYPE_RESERVED:
		break;
	case CDMA_SMS_MSG_TYPE_DELIVER:
		ofono_cdma_sms_process_wmt_deliver(cdma_sms, incoming);
		break;
	case CDMA_SMS_MSG_TYPE_SUBMIT:
	case CDMA_SMS_MSG_TYPE_CANCEL:
	case CDMA_SMS_MSG_TYPE_DELIVER_ACK:
	case CDMA_SMS_MSG_TYPE_USER_ACK:
	case CDMA_SMS_MSG_TYPE_READ_ACK:
	case CDMA_SMS_MSG_TYPE_DELIVER_REPORT:
	case CDMA_SMS_MSG_TYPE_SUBMIT_REPORT:
		/* TODO */
		break;
	}
}

static void ofono_cdma_sms_process_p2p(struct ofono_cdma_sms *cdma_sms,
					struct cdma_sms *incoming)
{
	switch (incoming->p2p_msg.teleservice_id) {
	case CDMA_SMS_TELESERVICE_ID_CMT91:
	case CDMA_SMS_TELESERVICE_ID_WPT:
		break; /* TODO: Not supported yet */
	case CDMA_SMS_TELESERVICE_ID_WMT:
		ofono_cdma_sms_process_wmt(cdma_sms, incoming);
		break;
	case CDMA_SMS_TELESERVICE_ID_VMN:
	case CDMA_SMS_TELESERVICE_ID_WAP:
	case CDMA_SMS_TELESERVICE_ID_WEMT:
	case CDMA_SMS_TELESERVICE_ID_SCPT:
	case CDMA_SMS_TELESERVICE_ID_CATPT:
		break; /* TODO: Not supported yet */
	}
}

void ofono_cdma_sms_deliver_notify(struct ofono_cdma_sms *cdma_sms,
					const unsigned char *pdu, int tpdu_len)
{
	static struct cdma_sms s;

	DBG("tpdu len %d", tpdu_len);

	memset(&s, 0, sizeof(struct cdma_sms));

	if (cdma_sms_decode(pdu, tpdu_len, &s) == FALSE)
		return;

	switch (s.type) {
	case CDMA_SMS_TP_MSG_TYPE_P2P:
		ofono_cdma_sms_process_p2p(cdma_sms, &s);
		break;
	case CDMA_SMS_TP_MSG_TYPE_BCAST:
	case CDMA_SMS_TP_MSG_TYPE_ACK:
		/*
		 * TODO: Support SMS Broadcast Message and SMS
		 * Acknowledge Message.
		 */
		break;
	}
}

int ofono_cdma_sms_driver_register(const struct ofono_cdma_sms_driver *d)
{
	DBG("driver: %p, name: %s", d, d->name);

	if (d->probe == NULL)
		return -EINVAL;

	g_drivers = g_slist_prepend(g_drivers, (void *)d);

	return 0;
}

void ofono_cdma_sms_driver_unregister(const struct ofono_cdma_sms_driver *d)
{
	DBG("driver: %p, name: %s", d, d->name);

	g_drivers = g_slist_remove(g_drivers, (void *)d);
}

static void cdma_sms_unregister(struct ofono_atom *atom)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_modem *modem = __ofono_atom_get_modem(atom);
	const char *path = __ofono_atom_get_path(atom);

	g_dbus_unregister_interface(conn, path,
					OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);

	ofono_modem_remove_interface(modem,
					OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
}

static void cdma_sms_remove(struct ofono_atom *atom)
{
	struct ofono_cdma_sms *cdma_sms = __ofono_atom_get_data(atom);

	DBG("atom: %p", atom);

	if (cdma_sms == NULL)
		return;

	if (cdma_sms->driver && cdma_sms->driver->remove)
		cdma_sms->driver->remove(cdma_sms);

	g_free(cdma_sms);
}

/*
 * Create a CDMA SMS driver
 *
 * This creates a CDMA SMS driver that is hung off a @modem
 * object. However, for the driver to be used by the system, it has to
 * be registered with the oFono core using ofono_sms_register().
 *
 * This is done once the modem driver determines that SMS is properly
 * supported by the hardware.
 */
struct ofono_cdma_sms *ofono_cdma_sms_create(struct ofono_modem *modem,
						unsigned int vendor,
						const char *driver,
						void *data)
{
	struct ofono_cdma_sms *cdma_sms;
	GSList *l;

	if (driver == NULL)
		return NULL;

	cdma_sms = g_try_new0(struct ofono_cdma_sms, 1);
	if (cdma_sms == NULL)
		return NULL;

	cdma_sms->atom = __ofono_modem_add_atom(modem,
						OFONO_ATOM_TYPE_CDMA_SMS,
						cdma_sms_remove, cdma_sms);

	for (l = g_drivers; l; l = l->next) {
		const struct ofono_cdma_sms_driver *drv = l->data;

		if (g_strcmp0(drv->name, driver))
			continue;

		if (drv->probe(cdma_sms, vendor, data) < 0)
			continue;

		cdma_sms->driver = drv;
		break;
	}

	return cdma_sms;
}

/*
 * Indicate oFono that a CDMA SMS driver is ready for operation
 *
 * This is called after ofono_cdma_sms_create() was done and the modem
 * driver determined that a modem supports SMS correctly. Once this
 * call succeeds, the D-BUS interface for SMS goes live.
 */
void ofono_cdma_sms_register(struct ofono_cdma_sms *cdma_sms)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_modem *modem = __ofono_atom_get_modem(cdma_sms->atom);
	const char *path = __ofono_atom_get_path(cdma_sms->atom);

	if (!g_dbus_register_interface(conn, path,
					OFONO_CDMA_MESSAGE_MANAGER_INTERFACE,
					cdma_sms_manager_methods,
					cdma_sms_manager_signals,
					NULL, cdma_sms, NULL)) {
		ofono_error("Could not create %s interface",
				OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);
		return;
	}

	ofono_modem_add_interface(modem, OFONO_CDMA_MESSAGE_MANAGER_INTERFACE);

	__ofono_atom_register(cdma_sms->atom, cdma_sms_unregister);
}

void ofono_cdma_sms_remove(struct ofono_cdma_sms *cdma_sms)
{
	__ofono_atom_free(cdma_sms->atom);
}

void ofono_cdma_sms_set_data(struct ofono_cdma_sms *cdma_sms, void *data)
{
	cdma_sms->driver_data = data;
}

void *ofono_cdma_sms_get_data(struct ofono_cdma_sms *cdma_sms)
{
	return cdma_sms->driver_data;
}