summaryrefslogtreecommitdiff
path: root/drivers/qmimodem/ussd.c
blob: e5399b9d8da233a701d48fe3f6252a1bb11e7327 (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
332
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2011-2012  Intel Corporation. All rights reserved.
 *  Copyright (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 *
 *  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 <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/ussd.h>
#include <smsutil.h>
#include "qmi.h"

#include "qmimodem.h"

#include "voice.h"

struct ussd_data {
	struct qmi_service *voice;
	uint16_t major;
	uint16_t minor;
};

static int validate_ussd_data(const struct qmi_ussd_data *data, uint16_t size)
{
	if (data == NULL)
		return 1;

	if (size < sizeof(*data))
		return 1;

	if (size < sizeof(*data) + data->length)
		return 1;

	if (data->dcs < QMI_USSD_DCS_ASCII || data->dcs > QMI_USSD_DCS_UCS2)
		return 1;

	return 0;
}

static int convert_qmi_dcs_gsm_dcs(int qmi_dcs, int *gsm_dcs)
{
	switch (qmi_dcs) {
	case QMI_USSD_DCS_ASCII:
		*gsm_dcs = USSD_DCS_8BIT;
		break;
	case QMI_USSD_DCS_8BIT:
		*gsm_dcs = USSD_DCS_8BIT;
		break;
	case QMI_USSD_DCS_UCS2:
		*gsm_dcs = USSD_DCS_UCS2;
		break;
	default:
		return 1;
	}

	return 0;
}

static void async_ind(struct qmi_result *result, void *user_data)
{
	struct ofono_ussd *ussd = user_data;
	const struct qmi_ussd_data *qmi_ussd;
	uint8_t user_action_required = 0;
	int notify_status = OFONO_USSD_STATUS_NOTIFY;
	uint16_t len;
	int gsm_dcs;

	DBG("");

	qmi_ussd = qmi_result_get(result, QMI_VOICE_PARAM_USSD_IND_DATA, &len);
	if (qmi_ussd == NULL)
		return;

	if (validate_ussd_data(qmi_ussd, len))
		goto error;

	if (convert_qmi_dcs_gsm_dcs(qmi_ussd->dcs, &gsm_dcs))
		goto error;

	if (qmi_result_get_uint8(result, QMI_VOICE_PARAM_USSD_IND_USER_ACTION,
					&user_action_required)) {
		if (user_action_required == QMI_USSD_USER_ACTION_REQUIRED)
			notify_status = OFONO_USSD_STATUS_ACTION_REQUIRED;
	}

	ofono_ussd_notify(ussd, notify_status, gsm_dcs,
				qmi_ussd->data, qmi_ussd->length);
	return;

error:
	ofono_ussd_notify(ussd, OFONO_USSD_STATUS_TERMINATED, 0, NULL, 0);
}

static void async_orig_ind(struct qmi_result *result, void *user_data)
{
	struct ofono_ussd *ussd = user_data;
	const struct qmi_ussd_data *qmi_ussd;
	uint16_t error = 0;
	uint16_t len;
	int gsm_dcs;

	DBG("");

	qmi_result_get_uint16(result, QMI_VOICE_PARAM_ASYNC_USSD_ERROR, &error);

	switch (error) {
	case 0:
		/* no error */
		break;
	case 92:
		qmi_result_get_uint16(result,
					QMI_VOICE_PARAM_ASYNC_USSD_FAILURE_CASE,
					&error);
		DBG("Failure Cause: 0x%04x", error);
		goto error;
	default:
		DBG("USSD Error 0x%04x", error);
		goto error;
	}

	qmi_ussd = qmi_result_get(result, QMI_VOICE_PARAM_ASYNC_USSD_DATA,
					&len);
	if (qmi_ussd == NULL)
		return;

	if (validate_ussd_data(qmi_ussd, len))
		goto error;

	if (convert_qmi_dcs_gsm_dcs(qmi_ussd->dcs, &gsm_dcs))
		goto error;

	ofono_ussd_notify(ussd, OFONO_USSD_STATUS_NOTIFY, gsm_dcs,
				qmi_ussd->data, qmi_ussd->length);
	return;

error:
	ofono_ussd_notify(ussd, OFONO_USSD_STATUS_TERMINATED, 0, NULL, 0);
}

static void create_voice_cb(struct qmi_service *service, void *user_data)
{
	struct ofono_ussd *ussd = user_data;
	struct ussd_data *data = ofono_ussd_get_data(ussd);

	DBG("");

	if (service == NULL) {
		ofono_error("Failed to request Voice service");
		ofono_ussd_remove(ussd);
		return;
	}

	if (!qmi_service_get_version(service, &data->major, &data->minor)) {
		ofono_error("Failed to get Voice service version");
		ofono_ussd_remove(ussd);
		return;
	}

	data->voice = qmi_service_ref(service);

	qmi_service_register(data->voice, QMI_VOICE_USSD_IND,
					async_ind, ussd, NULL);

	qmi_service_register(data->voice, QMI_VOICE_ASYNC_ORIG_USSD,
					async_orig_ind, ussd, NULL);

	ofono_ussd_register(ussd);
}

static int qmi_ussd_probe(struct ofono_ussd *ussd,
				unsigned int vendor, void *user_data)
{
	struct qmi_device *device = user_data;
	struct ussd_data *data;

	DBG("");

	data = g_new0(struct ussd_data, 1);

	ofono_ussd_set_data(ussd, data);

	qmi_service_create_shared(device, QMI_SERVICE_VOICE,
						create_voice_cb, ussd, NULL);

	return 0;
}

static void qmi_ussd_remove(struct ofono_ussd *ussd)
{
	struct ussd_data *data = ofono_ussd_get_data(ussd);

	DBG("");

	ofono_ussd_set_data(ussd, NULL);

	qmi_service_unref(data->voice);

	g_free(data);
}

static void qmi_ussd_cancel(struct ofono_ussd *ussd,
				ofono_ussd_cb_t cb, void *user_data)
{
	struct ussd_data *ud = ofono_ussd_get_data(ussd);

	DBG("");

	if (qmi_service_send(ud->voice, QMI_VOICE_CANCEL_USSD, NULL,
					NULL, NULL, NULL) > 0)
		CALLBACK_WITH_SUCCESS(cb, user_data);
	else
		CALLBACK_WITH_FAILURE(cb, user_data);
}

/*
 * The cb is called when the request (on modem layer) reports success or
 * failure. It doesn't contain a network result. We get the network answer
 * via VOICE_IND.
 */
static void qmi_ussd_request_cb(struct qmi_result *result, void *user_data)
{
	struct cb_data *cbd = user_data;
	ofono_ussd_cb_t cb = cbd->cb;

	DBG("");

	qmi_result_print_tlvs(result);

	if (qmi_result_set_error(result, NULL)) {
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void qmi_ussd_request(struct ofono_ussd *ussd, int dcs,
			const unsigned char *pdu, int len,
			ofono_ussd_cb_t cb, void *data)
{
	struct ussd_data *ud = ofono_ussd_get_data(ussd);
	struct cb_data *cbd = cb_data_new(cb, data);
	struct qmi_ussd_data *qmi_ussd;
	struct qmi_param *param;
	char *utf8 = NULL;
	long utf8_len = 0;

	DBG("");

	switch (dcs) {
	case 0xf: /* 7bit GSM unspecific */
		utf8 = ussd_decode(dcs, len, pdu);
		if (!utf8)
			goto error;

		utf8_len = strlen(utf8);
		break;
	default:
		DBG("Unsupported USSD Data Coding Scheme 0x%x", dcs);
		goto error;
	}

	/*
	 * So far only DCS_ASCII works.
	 * DCS_8BIT and DCS_UCS2 is broken, because the modem firmware
	 * (least on a EC20) encodes those in-correctly onto the air interface,
	 * resulting in wrong decoded USSD data.
	 */
	qmi_ussd = alloca(sizeof(struct qmi_ussd_data) + utf8_len);
	qmi_ussd->dcs = QMI_USSD_DCS_ASCII;
	qmi_ussd->length = len;
	memcpy(qmi_ussd->data, utf8, utf8_len);
	g_free(utf8);

	param = qmi_param_new();
	if (param == NULL)
		goto error;

	qmi_param_append(param, QMI_VOICE_PARAM_USS_DATA,
			sizeof(struct qmi_ussd_data) + utf8_len, qmi_ussd);

	if (qmi_service_send(ud->voice, QMI_VOICE_ASYNC_ORIG_USSD, param,
					qmi_ussd_request_cb, cbd, g_free) > 0)
		return;

	qmi_param_free(param);
error:
	g_free(cbd);
	CALLBACK_WITH_FAILURE(cb, data);
}

static const struct ofono_ussd_driver driver = {
	.name		= "qmimodem",
	.probe		= qmi_ussd_probe,
	.remove		= qmi_ussd_remove,
	.request	= qmi_ussd_request,
	.cancel		= qmi_ussd_cancel
};

void qmi_ussd_init(void)
{
	ofono_ussd_driver_register(&driver);
}

void qmi_ussd_exit(void)
{
	ofono_ussd_driver_unregister(&driver);
}