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
|
/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2010 Intel Corporation. All rights reserved.
*
* 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 <stdlib.h>
#include <glib.h>
#include <ofono/types.h>
#include "stkutil.h"
#include "smsutil.h"
#include "simutil.h"
#include "util.h"
static gboolean parse_dataobj_text(struct stk_command *command,
struct comprehension_tlv_iter *iter)
{
unsigned int len;
if (comprehension_tlv_iter_next(iter) != TRUE)
return FALSE;
if (comprehension_tlv_iter_get_tag(iter) !=
STK_DATA_OBJECT_TYPE_TEXT)
return FALSE;
len = comprehension_tlv_iter_get_length(iter);
/* DCS followed by some text, cannot be 1 */
if (len == 1)
return FALSE;
if (len > 0) {
const unsigned char *data =
comprehension_tlv_iter_get_data(iter);
unsigned char dcs = data[0];
char *utf8;
switch (dcs) {
case 0x00:
{
long written;
unsigned long max_to_unpack = (len - 1) * 8 / 7;
unsigned char *unpacked = unpack_7bit(data + 1, len - 1,
0, FALSE,
max_to_unpack,
&written, 0);
if (unpacked == NULL)
return FALSE;
utf8 = convert_gsm_to_utf8(unpacked, written,
NULL, NULL, 0);
g_free(unpacked);
break;
}
case 0x04:
utf8 = convert_gsm_to_utf8(data + 1, len - 1,
NULL, NULL, 0);
break;
case 0x08:
utf8 = g_convert((const gchar *) data + 1, len - 1,
"UTF-8//TRANSLIT", "UCS-2BE",
NULL, NULL, NULL);
break;
default:
return FALSE;;
}
if (utf8 == NULL)
return FALSE;
command->display_text.text = utf8;
} else
command->display_text.text = NULL;
return TRUE;
}
static void destroy_display_text(struct stk_command *command)
{
g_free(command->display_text.text);
}
static gboolean parse_display_text(struct stk_command *command,
struct comprehension_tlv_iter *iter)
{
if (parse_dataobj_text(command, iter) == FALSE)
return FALSE;
command->destructor = destroy_display_text;
return TRUE;
}
struct stk_command *stk_command_new_from_pdu(const unsigned char *pdu,
unsigned int len)
{
struct ber_tlv_iter ber;
struct comprehension_tlv_iter iter;
const unsigned char *data;
struct stk_command *command;
gboolean ok;
ber_tlv_iter_init(&ber, pdu, len);
if (ber_tlv_iter_next(&ber) != TRUE)
return NULL;
/* We should be wrapped in a Proactive UICC Command Tag 0xD0 */
if (ber_tlv_iter_get_short_tag(&ber) != 0xD0)
return NULL;
ber_tlv_iter_recurse_comprehension(&ber, &iter);
/*
* Now parse actual command details, they come in order with
* Command Details TLV first, followed by Device Identities TLV
*/
if (comprehension_tlv_iter_next(&iter) != TRUE)
return NULL;
if (comprehension_tlv_iter_get_tag(&iter) !=
STK_DATA_OBJECT_TYPE_COMMAND_DETAILS)
return NULL;
if (comprehension_tlv_iter_get_length(&iter) != 0x03)
return NULL;
data = comprehension_tlv_iter_get_data(&iter);
command = g_new0(struct stk_command, 1);
command->number = data[0];
command->type = data[1];
command->qualifier = data[2];
if (comprehension_tlv_iter_next(&iter) != TRUE)
goto fail;
if (comprehension_tlv_iter_get_tag(&iter) !=
STK_DATA_OBJECT_TYPE_DEVICE_IDENTITIES)
goto fail;
if (comprehension_tlv_iter_get_length(&iter) != 0x02)
goto fail;
data = comprehension_tlv_iter_get_data(&iter);
command->src = data[0];
command->dst = data[1];
switch (command->type) {
case STK_COMMAND_TYPE_DISPLAY_TEXT:
ok = parse_display_text(command, &iter);
break;
default:
ok = FALSE;
break;
};
if (ok)
return command;
fail:
if (command->destructor)
command->destructor(command);
g_free(command);
return NULL;
}
void stk_command_free(struct stk_command *command)
{
if (command->destructor)
command->destructor(command);
g_free(command);
}
|