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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* This file is part of oFono - Open Source Telephony
*
* Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: Alexander Kanavin <alexander.kanavin@nokia.com>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <gisi/client.h>
#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/phonebook.h>
#include "util.h"
#include "isi.h"
#define PHONEBOOK_TIMEOUT 5
#define PN_SIM 0x09
enum pb_message_id {
SIM_PB_REQ_SIM_PB_READ = 0xDC,
SIM_PB_RESP_SIM_PB_READ = 0xDD
};
enum pb_service_types {
SIM_PB_READ = 0x0F
};
enum pb_sub_block_id {
SIM_PB_INFO_REQUEST = 0xE4,
SIM_PB_STATUS = 0xFB,
SIM_PB_LOCATION = 0xFE,
SIM_PB_LOCATION_SEARCH = 0xFF
};
enum pb_type {
SIM_PB_ADN = 0xC8
};
enum pb_tag {
SIM_PB_ANR = 0xCA,
SIM_PB_EMAIL = 0xDD,
SIM_PB_SNE = 0xF7
};
enum pb_status {
SIM_SERV_OK = 0x01
};
static GIsiClient *client = NULL;
struct pb_data {
};
static char *ucs2_to_utf8(const unsigned char *str, long len)
{
char *utf8;
utf8 = g_convert((const char *)str, len, "UTF-8//TRANSLIT", "UCS-2BE",
NULL, NULL, NULL);
return utf8;
}
static struct pb_data *phonebook_create()
{
struct pb_data *pb = g_try_new0(struct pb_data, 1);
return pb;
}
static void phonebook_destroy(struct pb_data *data)
{
g_free(data);
}
static int decode_read_response(const unsigned char *msg, size_t len,
struct ofono_phonebook *pb)
{
unsigned int i, p;
char *name = NULL;
char *number = NULL;
char *adn = NULL;
char *snr = NULL;
char *email = NULL;
unsigned int location = 0;
unsigned int status = 0;
unsigned int messageid;
unsigned int servicetype;
unsigned int num_subblocks;
dump_msg(msg, len);
if (len < 3)
goto error;
messageid = msg[0];
servicetype = msg[1];
num_subblocks = msg[2];
if (messageid != SIM_PB_RESP_SIM_PB_READ || servicetype != SIM_PB_READ)
goto error;
p = 3;
for (i=0; i < num_subblocks; i++) {
unsigned int subblock_type;
unsigned int subblock_len;
if (p + 4 > len)
goto error;
subblock_type = (msg[p] << 8) + msg[p+1];
subblock_len = (msg[p+2] << 8) + msg[p+3];
switch (subblock_type) {
case SIM_PB_ADN: {
unsigned int namelength;
unsigned int numberlength;
if (p + 8 > len)
goto error;
location = (msg[p + 4] << 8) + msg[p + 5];
namelength = msg[p + 6];
numberlength = msg[p + 7];
if (p + 8 + namelength * 2 + numberlength * 2 > len)
goto error;
name = ucs2_to_utf8(msg + p + 8, namelength * 2);
number = ucs2_to_utf8(msg + p + 8 + namelength * 2,
numberlength * 2);
DBG("ADN subblock: name %s number %s location %i",
name, number, location);
break;
}
case SIM_PB_SNE: {
unsigned int locsne;
unsigned int snelength;
unsigned int snefiller;
if (p + 8 > len)
goto error;
locsne = (msg[p + 4] << 8) + msg[p + 5];
snelength = msg[p + 6];
snefiller = msg[p + 7];
if (p + 8 + snelength * 2 > len)
goto error;
adn = ucs2_to_utf8(msg + p + 8, snelength * 2);
DBG("SNE subblock: name %s", adn);
break;
}
case SIM_PB_ANR: {
unsigned int locanr;
unsigned int anrlength;
unsigned int anrfiller;
if (p + 8 > len)
goto error;
locanr = (msg[p + 4] << 8) + msg[p + 5];
anrlength = msg[p + 6];
anrfiller = msg[p + 7];
if (p + 8 + anrlength * 2 > len)
goto error;
snr = ucs2_to_utf8(msg + p + 8, anrlength * 2);
DBG("ANR subblock: number %s", snr);
break;
}
case SIM_PB_EMAIL: {
unsigned int locemail;
unsigned int emaillength;
unsigned int emailfiller;
if (p + 8 > len)
goto error;
locemail = (msg[p + 4] << 8) + msg[p + 5];
emaillength = msg[p + 6];
emailfiller = msg[p + 7];
if (p + 8 + emaillength * 2 > len)
goto error;
email = ucs2_to_utf8(msg + p + 8, emaillength * 2);
DBG("EMAIL subblock: email %s", email);
break;
}
case SIM_PB_STATUS:
if (p + 5 > len)
goto error;
status = msg[p + 4];
DBG("STATUS subblock: status %i", status);
break;
default:
DBG("Unknown subblock in read response: type %i length %i",
subblock_type, subblock_len);
break;
}
p += subblock_len;
}
if (status == SIM_SERV_OK) {
ofono_phonebook_entry(pb, -1, number, -1, name, -1, NULL,
snr, -1, adn, email, NULL, NULL);
return location;
} else {
return -1;
}
error:
DBG("Malformed read response");
return -1;
}
static void read_next_entry(int location, GIsiResponseFunc read_cb, struct isi_cb_data *cbd)
{
ofono_phonebook_cb_t cb = cbd->cb;
const unsigned char msg[] = {
SIM_PB_REQ_SIM_PB_READ,
SIM_PB_READ,
2, /* number of subblocks */
0, SIM_PB_LOCATION_SEARCH, /* subblock id */
0, 8, /* subblock size */
0, SIM_PB_ADN,
location >> 8, location & 0xFF, /* read next entry after specified by location */
0, SIM_PB_INFO_REQUEST, /* subblock id */
0, 16, /* subblock size */
4, /* number of tags */
0, /* filler */
0, SIM_PB_ADN, /* tags */
0, SIM_PB_SNE,
0, SIM_PB_ANR,
0, SIM_PB_EMAIL,
0, 0 /* filler */
};
if (location < 0)
goto error;
if (!cbd)
goto error;
if (g_isi_request_make(client, msg, sizeof(msg), PHONEBOOK_TIMEOUT,
read_cb, cbd))
return;
error:
{
DECLARE_FAILURE(error);
cb(&error, cbd->data);
g_free(cbd);
}
}
static bool read_resp_cb(GIsiClient *client, const void *restrict data,
size_t len, uint16_t object, void *opaque)
{
const unsigned char *msg = data;
struct isi_cb_data *cbd = opaque;
ofono_phonebook_cb_t cb = cbd->cb;
int location;
if(!msg) {
DBG("ISI client error: %d", g_isi_client_error(client));
goto error;
}
location = decode_read_response(data, len, cbd->user);
if (location != -1) {
read_next_entry(location, read_resp_cb, cbd);
return;
}
{
DECLARE_SUCCESS(e);
cb(&e, cbd->data);
}
goto out;
error:
{
DECLARE_FAILURE(e);
cb(&e, cbd->data);
}
out:
g_free(cbd);
return true;
}
static void isi_export_entries(struct ofono_phonebook *pb, const char *storage,
ofono_phonebook_cb_t cb, void *data)
{
struct isi_cb_data *cbd = isi_cb_data_new(NULL, cb, data);
const unsigned char msg[] = {
SIM_PB_REQ_SIM_PB_READ,
SIM_PB_READ,
2, /* number of subblocks */
0, SIM_PB_LOCATION, /* subblock id */
0, 8, /* subblock size */
0, SIM_PB_ADN,
0xFF, 0xFF, /* read first entry in the phonebook */
0, SIM_PB_INFO_REQUEST, /* subblock id */
0, 16, /* subblock size */
4, /* number of tags */
0, /* filler */
0, SIM_PB_ADN, /* tags */
0, SIM_PB_SNE,
0, SIM_PB_ANR,
0, SIM_PB_EMAIL,
0, 0 /* filler */
};
if (!cbd)
goto error;
if (strcmp(storage, "SM"))
goto error;
cbd->user = pb;
if (g_isi_request_make(client, msg, sizeof(msg), PHONEBOOK_TIMEOUT,
read_resp_cb, cbd))
return;
error:
if (cbd)
g_free(cbd);
{
DECLARE_FAILURE(error);
cb(&error, data);
}
}
static gboolean isi_phonebook_register(gpointer user)
{
struct ofono_phonebook *pb = user;
ofono_phonebook_register(pb);
return FALSE;
}
static int isi_phonebook_probe(struct ofono_phonebook *pb)
{
struct pb_data *pbd;
if (!client) {
/* FIXME: pass GIsiModem object */
client = g_isi_client_create(NULL, PN_SIM);
if (!client)
return -ENOMEM;
}
ofono_phonebook_set_data(pb, phonebook_create());
/* FIXME: If this is running on a phone itself, phonebook initialization needs to be done here */
g_idle_add(isi_phonebook_register, pb);
return 0;
}
static int isi_phonebook_remove(struct ofono_phonebook *pb)
{
struct pb_data *pbd = ofono_phonebook_get_data(pb);
if (client) {
g_isi_client_destroy(client);
client = NULL;
}
if (pbd)
phonebook_destroy(pbd);
return 0;
}
static struct ofono_phonebook_driver driver = {
.name = "isi",
.probe = isi_phonebook_probe,
.remove = isi_phonebook_remove,
.export_entries = isi_export_entries
};
void isi_phonebook_init()
{
ofono_phonebook_driver_register(&driver);
}
void isi_phonebook_exit()
{
ofono_phonebook_driver_unregister(&driver);
}
|