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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
#define LOG_TAG "RIL-SIM"
#include <utils/Log.h>
#include "samsung-ril.h"
#include "util.h"
extern const struct RIL_Env *rilenv;
extern struct radio_state radio;
extern struct ipc_client *ipc_client;
/**
* Update the radio state based on SIM status
*
* Out: RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED
* Indicate when value of RIL_RadioState has changed
* Callee will invoke RIL_RadioStateRequest method on main thread
*/
void updateRadioState(SIM_Status status)
{
/* If power mode isn't at least normal, don't update RIL state */
if(radio.power_mode < POWER_MODE_NORMAL)
return;
switch(status) {
case SIM_READY:
radio.radio_state = RADIO_STATE_SIM_READY;
break;
case SIM_NOT_READY:
radio.radio_state = RADIO_STATE_SIM_NOT_READY;
break;
case SIM_ABSENT:
case SIM_PIN:
case SIM_PUK:
case SIM_BLOCKED:
case SIM_NETWORK_PERSO:
case SIM_NETWORK_SUBSET_PERSO:
case SIM_CORPORATE_PERSO:
case SIM_SERVICE_PROVIDER_PERSO:
radio.radio_state = RADIO_STATE_SIM_LOCKED_OR_ABSENT;
break;
default:
radio.radio_state = RADIO_STATE_SIM_NOT_READY;
break;
}
RadioTokensCheck();
RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, NULL, 0);
}
/**
* In: IPC_SEC_PIN_STATUS
* Provides SIM initialization/lock status
*
* Out: RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED
* Indicates that SIM state changes.
* Callee will invoke RIL_REQUEST_GET_SIM_STATUS on main thread
*
* Out: RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED
* Indicate when value of RIL_RadioState has changed
* Callee will invoke RIL_RadioStateRequest method on main thread
*/
void respondSimStatusChanged(RIL_Token t, void *data, int length)
{
struct ipc_sec_pin_status_noti *pin_status = (struct ipc_sec_pin_status_noti*)data;
/* Don't consider this if modem isn't in normal power mode. */
if(radio.power_mode < POWER_MODE_NORMAL)
return;
if(radio.power_mode == POWER_MODE_NORMAL && radio.tokens.radio_power != 0) {
RIL_onRequestComplete(radio.tokens.radio_power, RIL_E_SUCCESS, NULL, 0);
radio.tokens.radio_power = 0;
}
/* Determine SIM status */
switch(pin_status->type) {
case IPC_SEC_PIN_SIM_INITIALIZING:
radio.sim_status = SIM_NOT_READY;
break;
case IPC_SEC_PIN_SIM_LOCK_SC:
switch(pin_status->key) {
case IPC_SEC_PIN_SIM_LOCK_SC_PIN1_REQ:
radio.sim_status = SIM_PIN;
break;
case IPC_SEC_PIN_SIM_LOCK_SC_PUK_REQ:
radio.sim_status = SIM_PUK;
break;
case IPC_SEC_PIN_SIM_LOCK_SC_CARD_BLOCKED:
radio.sim_status = SIM_BLOCKED;
break;
default:
radio.sim_status = SIM_ABSENT;
LOGE("%s: unknown SC substate %d --> setting SIM_ABSENT", __FUNCTION__, pin_status->key);
break;
}
break;
case IPC_SEC_PIN_SIM_LOCK_FD:
radio.sim_status = SIM_ABSENT;
LOGE("%s: FD lock present (unhandled state --> setting SIM_ABSENT)", __FUNCTION__);
break;
case IPC_SEC_PIN_SIM_LOCK_PN:
radio.sim_status = SIM_NETWORK_PERSO;
break;
case IPC_SEC_PIN_SIM_LOCK_PU:
radio.sim_status = SIM_NETWORK_SUBSET_PERSO;
break;
case IPC_SEC_PIN_SIM_LOCK_PP:
radio.sim_status = SIM_SERVICE_PROVIDER_PERSO;
break;
case IPC_SEC_PIN_SIM_LOCK_PC:
radio.sim_status = SIM_CORPORATE_PERSO;
break;
case IPC_SEC_PIN_SIM_INIT_COMPLETE:
radio.sim_status = SIM_READY;
break;
case IPC_SEC_PIN_SIM_PB_INIT_COMPLETE:
/* Ignore phone book init complete */
return;
case IPC_SEC_PIN_SIM_SIM_LOCK_REQUIRED:
case IPC_SEC_PIN_SIM_INSIDE_PF_ERROR:
case IPC_SEC_PIN_SIM_CARD_NOT_PRESENT:
case IPC_SEC_PIN_SIM_CARD_ERROR:
default:
/* Catchall for locked, card error and unknown states */
radio.sim_status = SIM_ABSENT;
break;
}
/* Update radio state based on SIM state */
updateRadioState(radio.sim_status);
RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
}
/**
* In: RIL_REQUEST_GET_SIM_STATUS
* Requests status of the SIM interface and the SIM card
*/
void requestSimStatus(RIL_Token t)
{
static RIL_AppStatus app_status_array[] = {
/* SIM_ABSENT = 0 */
{ RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
/* SIM_NOT_READY = 1 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
/* SIM_READY = 2 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
/* SIM_PIN = 3 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
/* SIM_PUK = 4 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
/* SIM_BLOCKED = 4 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_PERM_BLOCKED, RIL_PINSTATE_UNKNOWN },
/* SIM_NETWORK_PERSO = 6 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
/* SIM_NETWORK_SUBSET_PERSO = 7 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK_SUBSET,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
/* SIM_CORPORATE_PERSO = 8 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_CORPORATE,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
/* SIM_SERVICE_PROVIDER_PERSO = 9 */
{ RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_SERVICE_PROVIDER,
NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
};
int i, num_applications;
RIL_CardState card_state;
RIL_CardStatus card_status;
/* Card is assumed to be present if not explicitly absent */
if(radio.sim_status == SIM_ABSENT) {
card_state = RIL_CARDSTATE_ABSENT;
} else {
card_state = RIL_CARDSTATE_PRESENT;
}
/* Fill in struct */
card_status.card_state = card_state;
card_status.universal_pin_state = RIL_PINSTATE_UNKNOWN;
card_status.gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
card_status.cdma_subscription_app_index = RIL_CARD_MAX_APPS;
card_status.num_applications = 0;
/* Initialize apps */
for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
card_status.applications[i] = app_status_array[0];
}
/* If a card is present, add the gsm/umts application */
if(card_status.card_state == RIL_CARDSTATE_PRESENT) {
card_status.gsm_umts_subscription_app_index = 0;
card_status.num_applications = 1;
card_status.applications[0] = app_status_array[radio.sim_status];
/* FIXME: if USIM, set apptype */
//card_status.applications[0] = RIL_APPTYPE_USIM
}
RIL_onRequestComplete(t, RIL_E_SUCCESS, &card_status, sizeof(card_status));
}
/**
* In: RIL_REQUEST_SIM_IO
* Request SIM I/O operation.
* This is similar to the TS 27.007 "restricted SIM" operation
* where it assumes all of the EF selection will be done by the
* callee.
*
* Out: IPC_SEC_RSIM_ACCESS
* Performs a restricted SIM read operation
*/
void requestSimIo(RIL_Token t, void *data, size_t datalen)
{
const RIL_SIM_IO *sim_io;
unsigned char message[262];
struct ipc_sec_rsim_access_request *rsim_data;
unsigned char *rsim_payload;
int payload_length;
sim_io = (const RIL_SIM_IO*)data;
rsim_payload = message + sizeof(*rsim_data);
/* Set up RSIM header */
rsim_data = (struct ipc_sec_rsim_access_request*)message;
rsim_data->command = sim_io->command;
rsim_data->fileid = sim_io->fileid;
rsim_data->p1 = sim_io->p1;
rsim_data->p2 = sim_io->p2;
rsim_data->p3 = sim_io->p3;
/* Add payload if present */
if(sim_io->data) {
payload_length = (2 * strlen(sim_io->data));
if(sizeof(*rsim_data) + payload_length > sizeof(message))
return;
hex2bin(sim_io->data, strlen(sim_io->data), rsim_payload);
}
ipc_client_send(ipc_client, IPC_SEC_RSIM_ACCESS, IPC_TYPE_GET, (unsigned char*)&message, sizeof(message), getRequestId(t));
}
/**
* In: IPC_SEC_RSIM_ACCESS
* Provides restricted SIM read operation result
*
* Out: RIL_REQUEST_SIM_IO
* Request SIM I/O operation.
* This is similar to the TS 27.007 "restricted SIM" operation
* where it assumes all of the EF selection will be done by the
* callee.
*/
void respondSecRsimAccess(RIL_Token t, void *data, int length)
{
struct ipc_sec_rsim_access_response *rsim_resp = (struct ipc_sec_rsim_access_response*)data;
const unsigned char *data_ptr = ((unsigned char*)data + sizeof(*rsim_resp));
char *sim_resp;
RIL_SIM_IO_Response response;
response.sw1 = rsim_resp->sw1;
response.sw2 = rsim_resp->sw2;
if(rsim_resp->len) {
sim_resp = (char*)malloc(rsim_resp->len * 2 + 1);
bin2hex(data_ptr, rsim_resp->len, sim_resp);
response.simResponse = sim_resp;
} else {
response.simResponse = malloc(1);
response.simResponse[0] = '\0';
}
RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
free(response.simResponse);
}
/**
* In: RIL_REQUEST_GET_IMSI
* Get the SIM IMSI
* Only valid when radio state is "RADIO_STATE_SIM_READY"
*
* Out: IPC_MISC_ME_IMSI
* Requests ME's IMSI
*/
void requestIMSI(RIL_Token t)
{
ipc_client_send_get(IPC_MISC_ME_IMSI, getRequestId(t));
}
/**
* In: IPC_MISC_ME_IMSI
* Provides ME's IMSI
*
* Out: RIL_REQUEST_GET_IMSI
* Get the SIM IMSI
* Only valid when radio state is "RADIO_STATE_SIM_READY"
*/
void respondIMSI(struct ipc_message_info *request)
{
unsigned char *imsi_length;
char *imsi;
if(request->length < 1) {
LOGE("%s: zero data length", __FUNCTION__);
RIL_onRequestComplete(getToken(request->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
return;
}
imsi_length = (unsigned char*)request->data;
if(((int)request->length) < *imsi_length + 1) {
LOGE("%s: missing IMSI data", __FUNCTION__);
RIL_onRequestComplete(getToken(request->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
return;
}
/* Copy IMSI */
imsi = (char*)malloc(*imsi_length+1);
memcpy(imsi, ((unsigned char*)request->data)+1, *imsi_length);
imsi[*imsi_length] = '\0';
RIL_onRequestComplete(getToken(request->aseq), RIL_E_SUCCESS, imsi, *imsi_length+1);
}
/**
* In: RIL_REQUEST_ENTER_SIM_PIN
* Supplies SIM PIN. Only called if RIL_CardStatus has RIL_APPSTATE_PIN state
*
* Out: IPC_SEC_PIN_STATUS SET
* Attempts to unlock SIM PIN1
*
* Out: IPC_SEC_LOCK_INFO
* Retrieves PIN1 lock status
*/
void requestEnterSimPin(RIL_Token t, void *data, size_t datalen)
{
struct ipc_sec_pin_status_set pin_status;
char *pin = ((char**)data)[0];
unsigned char buf[9];
/* 1. Send PIN */
if(strlen(data) > 16) {
LOGE("%s: pin exceeds maximum length", __FUNCTION__);
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
}
ipc_sec_pin_status_set_setup(&pin_status, IPC_SEC_PIN_TYPE_PIN1, pin, NULL);
ipc_client_send_set(IPC_SEC_PIN_STATUS, getRequestId(t), (unsigned char*)&pin_status, sizeof(pin_status));
/* 2. Get lock status */
memset(buf, 0, sizeof(buf));
buf[0] = 1;
buf[1] = IPC_SEC_PIN_TYPE_PIN1;
ipc_client_send(ipc_client, IPC_SEC_LOCK_INFO, IPC_TYPE_GET, buf, sizeof(buf), getRequestId(t));
}
/**
* In: IPC_GEN_PHONE_RES
* Provides result of IPC_SEC_PIN_STATUS SET
*
* Out: RIL_REQUEST_ENTER_SIM_PIN
* Returns PIN SIM unlock result
*/
void respondSecPinStatus(struct ipc_message_info *request)
{
struct ipc_gen_phone_res *gen_res = (struct ipc_gen_phone_res*)request->data;
int attempts = -1;
if(gen_res->code == 0x0010) {
RIL_onRequestComplete(getToken(request->aseq), RIL_E_PASSWORD_INCORRECT, &attempts, sizeof(int*));
} else if(gen_res->code == 0x8000) {
RIL_onRequestComplete(getToken(request->aseq), RIL_E_SUCCESS, &attempts, sizeof(int*));
} else {
LOGE("%s: unhandled code %04x", __FUNCTION__, gen_res->code);
}
}
/**
* In: IPC_SEC_LOCK_INFO
* Provides number of retries left for a lock type
*/
void respondLockInfo(struct ipc_message_info *request)
{
/*
* FIXME: solid way of handling lockinfo and sim unlock response together
* so we can return the number of attempts left in respondSecPinStatus
*/
int attempts;
struct ipc_sec_lock_info_response *lock_info = (struct ipc_sec_lock_info_response*)request->data;
if(lock_info->type == IPC_SEC_PIN_TYPE_PIN1) {
attempts = lock_info->attempts;
LOGD("%s: PIN1 %d attempts left", __FUNCTION__, attempts);
} else {
LOGE("%s: unhandled lock type %d", __FUNCTION__, lock_info->type);
}
}
/**
* In: RIL_REQUEST_QUERY_FACILITY_LOCK
* Query the status of a facility lock state
*
* Out: IPC_SEC_PHONE_LOCK GET
*/
void requestQueryFacilityLock(RIL_Token t, void *data, size_t datalen)
{
unsigned char lock_type;
char *facility = ((char**)data)[0];
if(!strcmp(facility, "SC")) {
lock_type = IPC_SEC_PIN_SIM_LOCK_SC;
} else if(!strcmp(facility, "FD")) {
lock_type = IPC_SEC_PIN_SIM_LOCK_FD;
} else {
LOGE("%s: unsupported facility: %s", __FUNCTION__, facility);
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
}
ipc_client_send(ipc_client, IPC_SEC_PHONE_LOCK, IPC_TYPE_GET, &lock_type, sizeof(lock_type), getRequestId(t));
}
/**
* In: IPC_SEC_PHONE_LOCK
*
* Out: RIL_REQUEST_QUERY_FACILITY_LOCK
* Query the status of a facility lock state
*/
void respondSecPhoneLock(struct ipc_message_info *request)
{
int status;
struct ipc_sec_phone_lock_response *lock = (struct ipc_sec_phone_lock_response*)request->data;
status = lock->status;
RIL_onRequestComplete(getToken(request->aseq), RIL_E_SUCCESS, &status, sizeof(int*));
}
/**
* In: RIL_REQUEST_SET_FACILITY_LOCK
* Enable/disable one facility lock
*
* Out: IPC_SEC_PHONE_LOCK SET
*/
void requestSetFacilityLock(RIL_Token t, void *data, size_t datalen)
{
unsigned char lock_type;
char *facility = ((char**)data)[0];
if(!strcmp(facility, "SC")) {
lock_type = IPC_SEC_PIN_SIM_LOCK_SC;
} else if(!strcmp(facility, "FD")) {
lock_type = IPC_SEC_PIN_SIM_LOCK_FD;
} else {
LOGE("%s: unsupported facility: %s", __FUNCTION__, facility);
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
}
ipc_client_send(ipc_client, IPC_SEC_PHONE_LOCK, IPC_TYPE_GET, &lock_type, sizeof(lock_type), getRequestId(t));
}
|