summaryrefslogtreecommitdiffstats
path: root/drivers/isimodem/ussd.c
blob: 71f246d079a7283e9e5c53fda96c76f665b03876 (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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2009-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

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <errno.h>

#include <glib.h>

#include <gisi/client.h>
#include <gisi/message.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/ussd.h>

#include "smsutil.h"
#include "util.h"

#include "isimodem.h"
#include "isiutil.h"
#include "ss.h"
#include "debug.h"

struct ussd_info {
	uint8_t dcs;
	uint8_t type;
	uint8_t len;
};

struct ussd_data {
	GIsiClient *client;
	GIsiVersion version;
	int mt_session;
};

static gboolean check_response_status(const GIsiMessage *msg, uint8_t msgid)
{
	if (g_isi_msg_error(msg) < 0) {
		DBG("Error: %s", g_isi_msg_strerror(msg));
		return FALSE;
	}

	if (g_isi_msg_id(msg) != msgid) {
		DBG("Unexpected msg: %s",
			ss_message_id_name(g_isi_msg_id(msg)));
		return FALSE;
	}
	return TRUE;
}

static void ussd_notify_ack(struct ussd_data *ud)
{
	const uint8_t msg[] = {
		SS_GSM_USSD_SEND_REQ,
		SS_GSM_USSD_NOTIFY,
		0,	/* subblock count */
	};

	g_isi_client_send(ud->client, msg, sizeof(msg), NULL, NULL, NULL);
}

static void ussd_ind_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_ussd *ussd = data;
	struct ussd_data *ud = ofono_ussd_get_data(ussd);
	struct ussd_info *info;
	size_t len = sizeof(struct ussd_info);
	uint8_t *string;
	int status;

	if (g_isi_msg_id(msg) != SS_GSM_USSD_RECEIVE_IND)
		return;

	if (!g_isi_msg_data_get_struct(msg, 0, (const void **) &info, len))
		return;

	if (!g_isi_msg_data_get_struct(msg, len, (const void **) &string,
					info->len))
		return;

	switch (info->type) {
	case 0:
		/* Nothing - this is response to NOTIFY_ACK REQ */
		return;

	case SS_GSM_USSD_MT_REPLY:
		/* This never happens, but.. */
		status = OFONO_USSD_STATUS_LOCAL_CLIENT_RESPONDED;
		break;

	case SS_GSM_USSD_COMMAND:

		/* Ignore, we get SS_GSM_USSD_REQUEST, too */
		if (ud->mt_session)
			return;

		status = OFONO_USSD_STATUS_ACTION_REQUIRED;
		break;

	case SS_GSM_USSD_NOTIFY:
		status = OFONO_USSD_STATUS_NOTIFY;
		ussd_notify_ack(ud);
		break;

	case SS_GSM_USSD_END:
		status = OFONO_USSD_STATUS_TERMINATED;
		ud->mt_session = 0;
		break;

	case SS_GSM_USSD_REQUEST:
		ud->mt_session = 1;
		status = OFONO_USSD_STATUS_ACTION_REQUIRED;
		break;

	default:
		status = OFONO_USSD_STATUS_NOT_SUPPORTED;
	}

	DBG("type: %u %s, dcs: 0x%02x, len: %u",
		info->type, ss_ussd_type_name(info->type), info->dcs,
		info->len);

	ofono_ussd_notify(ussd, status, info->dcs, string, info->len);
}

static void ussd_send_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	ofono_ussd_cb_t cb = cbd->cb;

	if (check_response_status(msg, SS_GSM_USSD_SEND_RESP))
		CALLBACK_WITH_SUCCESS(cb, cbd->data);
	else
		CALLBACK_WITH_FAILURE(cb, cbd->data);
}

static void isi_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 isi_cb_data *cbd = isi_cb_data_new(ussd, cb, data);

	size_t sb_len = ALIGN4(4 + len);
	size_t pad_len = sb_len - (4 + len);

	const uint8_t padding[4] = { 0 };
	const uint8_t msg[] = {
		SS_GSM_USSD_SEND_REQ,
		ud->mt_session ? SS_GSM_USSD_MT_REPLY : SS_GSM_USSD_COMMAND,
		1,		/* subblock count */
		SS_GSM_USSD_STRING,
		sb_len,
		dcs,		/* DCS */
		len,		/* string length */
		/* USSD string goes here */
	};
	struct iovec iov[3] = {
		{ (uint8_t *) msg, sizeof(msg) },
		{ (uint8_t *) pdu, len },
		{ (uint8_t *) padding, pad_len },
	};

	if (cbd == NULL || ud == NULL)
		goto error;

	if (g_isi_client_vsend(ud->client, iov, 3, ussd_send_resp_cb, cbd,
				g_free))
		return;

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

static void isi_cancel(struct ofono_ussd *ussd, ofono_ussd_cb_t cb, void *data)
{
	struct ussd_data *ud = ofono_ussd_get_data(ussd);
	struct isi_cb_data *cbd = isi_cb_data_new(ussd, cb, data);
	const uint8_t msg[] = {
		SS_GSM_USSD_SEND_REQ,
		SS_GSM_USSD_END,
		0, /* subblock count */
	};

	if (cbd == NULL || ud == NULL)
		goto error;

	if (g_isi_client_send(ud->client, msg, sizeof(msg), ussd_send_resp_cb,
				cbd, g_free))
		return;

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

static void ussd_reachable_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_ussd *ussd = data;
	struct ussd_data *ud = ofono_ussd_get_data(ussd);

	if (g_isi_msg_error(msg) < 0) {
		ofono_ussd_remove(ussd);
		return;
	}

	ISI_RESOURCE_DBG(msg);

	g_isi_client_ind_subscribe(ud->client, SS_GSM_USSD_RECEIVE_IND,
					ussd_ind_cb, ussd);

	ofono_ussd_register(ussd);
}

static int isi_ussd_probe(struct ofono_ussd *ussd, unsigned int vendor,
				void *user)
{
	GIsiModem *modem = user;
	struct ussd_data *ud;

	ud = g_try_new0(struct ussd_data, 1);

	if (ud == NULL)
		return -ENOMEM;

	ud->client = g_isi_client_create(modem, PN_SS);
	if (ud->client == NULL) {
		g_free(ud);
		return -ENOMEM;
	}

	ofono_ussd_set_data(ussd, ud);

	g_isi_client_verify(ud->client, ussd_reachable_cb, ussd, NULL);

	return 0;
}

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

	ofono_ussd_set_data(ussd, NULL);

	if (data == NULL)
		return;

	g_isi_client_destroy(data->client);
	g_free(data);
}

static struct ofono_ussd_driver driver = {
	.name			= "isimodem",
	.probe			= isi_ussd_probe,
	.remove			= isi_ussd_remove,
	.request		= isi_request,
	.cancel			= isi_cancel
};

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

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