summaryrefslogtreecommitdiffstats
path: root/ss.c
blob: 035c97a107aa1db853af400fd19923a0950852f9 (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
/**
 * This file is part of samsung-ril.
 *
 * Copyright (C) 2012 Paul Kocialkowski <contact@oaulk.fr>
 *
 * samsung-ril is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * samsung-ril 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 samsung-ril.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define LOG_TAG "RIL-SS"
#include <utils/Log.h>

#include "samsung-ril.h"
#include "util.h"

void ipc_ss_ussd_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res = (struct ipc_gen_phone_res *) info->data;
	int rc;

	rc = ipc_gen_phone_res_check(phone_res);
	if(rc < 0) {
		LOGE("There was an error, aborting USSD request");

		RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		ril_state.ussd_state = 0;

		return;
	}

	RIL_onRequestComplete(reqGetToken(info->aseq), RIL_E_SUCCESS, NULL, 0);
}

void ril_request_send_ussd(RIL_Token t, void *data, size_t datalen)
{
	char *data_enc = NULL;
	int data_enc_len = 0;

	char *message =NULL;
	struct ipc_ss_ussd *ussd = NULL;

	int message_size = 0xc0;

	switch(ril_state.ussd_state) {
		case 0:
		case IPC_SS_USSD_NO_ACTION_REQUIRE:
		case IPC_SS_USSD_TERMINATED_BY_NET:
		case IPC_SS_USSD_OTHER_CLIENT:
		case IPC_SS_USSD_NOT_SUPPORT:
		case IPC_SS_USSD_TIME_OUT:
			LOGD("USSD Tx encoding is GSM7");

			data_enc_len = ascii2gsm7(data, &data_enc, datalen);
			// message_size = data_enc_len + sizeof(struct ipc_ss_ussd);

			if(data_enc_len > message_size) {
				LOGE("USSD message size is too long, aborting");
				RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

				free(data_enc);

				return;
			}

			message = malloc(message_size);
			memset(message, 0, message_size);

			ussd = (struct ipc_ss_ussd *) message;
			ussd->state = IPC_SS_USSD_NO_ACTION_REQUIRE;
			ussd->dcs = 0x0f; // GSM7 in that case
			ussd->length = data_enc_len;

			memcpy((void *) (message + sizeof(struct ipc_ss_ussd)), data_enc, data_enc_len);

			free(data_enc);

			break;
		case IPC_SS_USSD_ACTION_REQUIRE:
		default:
			LOGD("USSD Tx encoding is ASCII");

			data_enc_len = asprintf(&data_enc, "%s", data);
			// message_size = data_enc_len + sizeof(struct ipc_ss_ussd);

			if(data_enc_len > message_size) {
				LOGE("USSD message size is too long, aborting");
				RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);

				free(data_enc);

				return;
			}

			message = malloc(message_size);
			memset(message, 0, message_size);

			ussd = (struct ipc_ss_ussd *) message;
			ussd->state = IPC_SS_USSD_ACTION_REQUIRE;
			ussd->dcs = 0x0f; // ASCII in that case
			ussd->length = data_enc_len;

			memcpy((void *) (message + sizeof(struct ipc_ss_ussd)), data_enc, data_enc_len);

			free(data_enc);

			break;
	}

	if(message == NULL) {
		LOGE("USSD message is empty, aborting");

		RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
		return;
	}

	ipc_gen_phone_res_expect_to_func(reqGetId(t), IPC_SS_USSD,
		ipc_ss_ussd_complete);

	ipc_fmt_send(IPC_SS_USSD, IPC_TYPE_EXEC, (void *) message, message_size, reqGetId(t));
}

void ril_request_cancel_ussd(RIL_Token t, void *data, size_t datalen)
{
	struct ipc_ss_ussd ussd;

	memset(&ussd, 0, sizeof(ussd));

	ussd.state = IPC_SS_USSD_TERMINATED_BY_NET;
	ril_state.ussd_state = IPC_SS_USSD_TERMINATED_BY_NET;
	
	ipc_gen_phone_res_expect_to_complete(reqGetId(t), IPC_SS_USSD);

	ipc_fmt_send(IPC_SS_USSD, IPC_TYPE_EXEC, (void *) &ussd, sizeof(ussd), reqGetId(t));
}

void ipc2ril_ussd_state(struct ipc_ss_ussd *ussd, char *message[2])
{
	switch(ussd->state) {
		case IPC_SS_USSD_NO_ACTION_REQUIRE:
			asprintf(&message[0], "%d", 0);
			break;
		case IPC_SS_USSD_ACTION_REQUIRE:
			asprintf(&message[0], "%d", 1);
			break;
		case IPC_SS_USSD_TERMINATED_BY_NET:
			asprintf(&message[0], "%d", 2);
			break;
		case IPC_SS_USSD_OTHER_CLIENT:
			asprintf(&message[0], "%d", 3);
			break;
		case IPC_SS_USSD_NOT_SUPPORT:
			asprintf(&message[0], "%d", 4);
			break;
		case IPC_SS_USSD_TIME_OUT:
			asprintf(&message[0], "%d", 5);
			break;
	}
}

typedef enum {
	SMS_CODING_SCHEME_UNKNOWN = 0,
	SMS_CODING_SCHEME_GSM7,
	SMS_CODING_SCHEME_UCS2
} SmsCodingScheme;

static SmsCodingScheme sms_get_coding_scheme(int dataCoding)
{
	switch (dataCoding >> 4) {
	case 0x00:
	case 0x02:
	case 0x03:
		return SMS_CODING_SCHEME_GSM7;
	case 0x01:
		if (dataCoding == 0x10)
			return SMS_CODING_SCHEME_GSM7;
		if (dataCoding == 0x11)
			return SMS_CODING_SCHEME_UCS2;
		break;
	case 0x04:
	case 0x05:
	case 0x06:
	case 0x07:
		if (dataCoding & 0x20)
			return SMS_CODING_SCHEME_UNKNOWN;
		if (((dataCoding >> 2) & 3) == 0)
			return SMS_CODING_SCHEME_GSM7;
		if (((dataCoding >> 2) & 3) == 2)
			return SMS_CODING_SCHEME_UCS2;
		break;
	case 0xF:
		if (!(dataCoding & 4))
			return SMS_CODING_SCHEME_GSM7;
		break;
	}
	return SMS_CODING_SCHEME_UNKNOWN;
}

void ipc_ss_ussd(struct ipc_message_info *info)
{
	char *data_dec = NULL;
	int data_dec_len = 0;
	SmsCodingScheme codingScheme;

	char *message[2];

	struct ipc_ss_ussd *ussd = NULL;
	unsigned char state;

	memset(message, 0, sizeof(message) / sizeof(char *));	

	ussd = (struct ipc_ss_ussd *) info->data;

	ipc2ril_ussd_state(ussd, message);

	ril_state.ussd_state = ussd->state;

	if(ussd->length > 0 && info->length > 0 && info->data != NULL) {
		codingScheme = sms_get_coding_scheme(ussd->dcs);
		switch(codingScheme) {
			case SMS_CODING_SCHEME_GSM7:
				LOGD("USSD Rx encoding is GSM7");

				data_dec_len = gsm72ascii(info->data
					+ sizeof(struct ipc_ss_ussd), &data_dec, info->length - sizeof(struct ipc_ss_ussd));
				asprintf(&message[1], "%s", data_dec);
				message[1][data_dec_len] = '\0';

				break;
			case SMS_CODING_SCHEME_UCS2:
				LOGD("USSD Rx encoding %x is UCS2", ussd->dcs);

				data_dec_len = info->length - sizeof(struct ipc_ss_ussd);
				message[1] = malloc(data_dec_len * 4 + 1);

				int i, result = 0;
				char *ucs2 = (char*)info->data + sizeof(struct ipc_ss_ussd);
				for (i = 0; i < data_dec_len; i += 2) {
					int c = (ucs2[i] << 8) | ucs2[1 + i];
					result += utf8_write(message[1], result, c);
				}
				message[1][result] = '\0';
				break;
			default:
				LOGD("USSD Rx encoding %x is unknown, assuming ASCII",
					ussd->dcs);

				data_dec_len = info->length - sizeof(struct ipc_ss_ussd);
				asprintf(&message[1], "%s", info->data + sizeof(struct ipc_ss_ussd));
				message[1][data_dec_len] = '\0';
				break;
		}
	}

	RIL_onUnsolicitedResponse(RIL_UNSOL_ON_USSD, message, sizeof(message));
}