summaryrefslogtreecommitdiff
path: root/src/tapi.c
blob: 4d5fb802ae4294da91cbfd4082ac6b9ca6a4220a (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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 *  pwlock
 *
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <vconf.h>

#include "util.h"
#include "tapi.h"
#include "langs.h"

#ifndef PACKAGE_NAME
#define PACKAGE_NAME "org.tizen.pwlock"
#endif

static struct tapi_event tapi_events[] = {
	{TAPI_NOTI_MODEM_POWER, pwlock_tapi_noti_modem_power_cb,},
	{TAPI_NOTI_SIM_STATUS, pwlock_tapi_noti_sim_status_cb,},
};

static int _pwlock_tapi_register_noti_events(struct tapi *t)
{
	int r;
	int i;

	if (t == NULL)
		return -1;

	for (i = 0; i < t->evt_sz; i++) {
		r = tel_register_noti_event(t->handle, t->evt[i].event, t->evt[i].tapi_notification_cb, t);
		_DBG("TAPI register event : %s", t->evt[i].event);
		if (r != TAPI_API_SUCCESS) {
			_ERR("TAPI register event %s return %d",
			     t->evt[i].event, r);
			return -1;
		}
	}

	return 0;
}

static void _pwlock_tapi_deregister_noti_events(struct tapi *t)
{
	int r;
	int i;

	if (t == NULL)
		return;


	for (i = 0; i < t->evt_sz; i++) {
		r = tel_deregister_noti_event(t->handle, t->evt[i].event);
		if (r == TAPI_API_SUCCESS)
			_DBG("TAPI [%s] event unregister success", t->evt[i].event);
		else
			_ERR("TAPI [%s] event unregister return %d", t->evt[i].event, r);
	}
}

void pwlock_tapi_get_sim_lock_info_cb(TapiHandle *handle, int result, void *data, void *user_data)
{
	TelSimPinOperationResult_t sec_rt = result;
	TelSimLockInfo_t *lock = data;
	struct tapi *t = user_data;
	struct pwlock_tapi_info ti;

	_DBG("%s", __func__);

	_DBG("TAPI_SERVICE_SIM lock info reponse received");
	_DBG("set_rt[%d]", sec_rt);
	_DBG("lock->lock_type[%d]", lock->lock_type);
	_DBG("lock->lock_status[%d]-0:not need, 1:pin, 2:puk, 3:pin2, 4:puk2, 5:blocked", lock->lock_status);
	_DBG("lock->retry_count[%d]", lock->retry_count);

	ti.st = SIM_LOCK_INFO;
	ti.retry_cnt = lock->retry_count;
	
	if (t->cb)
		t->cb(&ti, t->cb_data);
}

void pwlock_tapi_noti_modem_power_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
{
	;
}

void pwlock_tapi_noti_sim_status_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data)
{
	TelSimCardStatus_t *sim_status = data;
	struct tapi *t = user_data;
	struct pwlock_tapi_info ti;

	if (sim_status == NULL) {
		_ERR("pwlock_tapi_noti_sim_status_cb:status is null");
		return ;
	}

	_DBG("Sim status: 0x%x", *sim_status);

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

	switch (*sim_status) {
	case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
	case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
	case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
	case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
	case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
	case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
	case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
		ti.st = SIM_REQUIRED_EVENT;
		break;
	case TAPI_SIM_STATUS_CARD_ERROR:
		ti.st = SIM_ERROR;
		break;
	case TAPI_SIM_STATUS_SIM_INITIALIZING:
		ti.st = SIM_WAITING;
		break;
	default:
		ti.st = SIM_OK;
		break;
	}

	if (t->cb)
		t->cb(&ti, t->cb_data);
}

void pwlock_tapi_verify_sim_pins_and_puks_cb(TapiHandle *handle, int result, void *data, void *user_data)
{
	TelSimPinOperationResult_t sec_rt = result;
	TelSimSecResult_t *res = data;
	struct tapi *t = user_data;
	struct pwlock_tapi_info ti;

	if (t == NULL) {
		_ERR("pwlock_tapi_verify_sim_pins_cb:struct tapi is null");
		return ;
	}
	if (res == NULL) {
		_ERR("pwlock_tapi_verify_sim_pins_cb:res is null");
		return ;
	}

	_DBG("sim pin/puk check result: 0x%x", sec_rt);

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

	switch (sec_rt) {
	/*
	case TAPI_SIM_OPERATION_TIMEOUT:
		_DBG("\tTimeout");
		ti.st = SIM_ERROR;
		break;
	*/
	case TAPI_SIM_PIN_OPERATION_SUCCESS:
		_DBG("\tType: %d", res->type);
		ti.st = SIM_OK;
		break;
	case TAPI_SIM_PIN_REQUIRED:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_PIN;
		ti.retry_cnt = res->retry_count;
		break;
	case TAPI_SIM_PUK_REQUIRED:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_PUK;
		ti.retry_cnt = res->retry_count;
		break;
	case TAPI_SIM_LOCK_REQUIRED:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_LOCK;
		ti.retry_cnt = res->retry_count;
		break;
	case TAPI_SIM_NCK_REQUIRED:		/**< Network Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_NCK;
		break;
	case TAPI_SIM_NSCK_REQUIRED:	/**< Network Subset Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_NSCK;
		break;
	case TAPI_SIM_SPCK_REQUIRED:	/**< Service Provider Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_SPCK;
		break;
	case TAPI_SIM_CCK_REQUIRED:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_CCK;
		break;
	default:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		if (res->type == TAPI_SIM_PTYPE_SIM)
			ti.st = SIM_REQ_LOCK;
		else
			ti.st = SIM_RETRY;
		ti.retry_cnt = res->retry_count;
		break;
	}

	_DBG("tapi_cb ti.st: %d", ti.st);

	if (t->cb)
		t->cb(&ti, t->cb_data);
}

void pwlock_tapi_disable_sim_facility_cb(TapiHandle *handle, int result, void *data, void *user_data)
{
	TelSimPinOperationResult_t sec_rt = result;
	TelSimSecResult_t *res = data;
	struct tapi *t = user_data;
	struct pwlock_tapi_info ti;

	if (t == NULL) {
		_ERR("pwlock_tapi_verify_sim_pins_cb:struct tapi is null");
		return ;
	}
	if (res == NULL) {
		_ERR("pwlock_tapi_verify_sim_pins_cb:res is null");
		return ;
	}

	_DBG("PER setting result: 0x%x", sec_rt);

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

	switch (sec_rt) {
	/*
	case TAPI_SIM_OPERATION_TIMEOUT:
		_DBG("\tTimeout");
		ti.st = SIM_ERROR;
		break;
	*/
	case TAPI_SIM_PIN_OPERATION_SUCCESS:
		_DBG("\tType: %d", res->type);
		ti.st = SIM_OK;
		break;
	case TAPI_SIM_NCK_REQUIRED:		/**< Network Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_NCK;
		break;
	case TAPI_SIM_NSCK_REQUIRED:		/**< Network Subset Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_NSCK;
		break;
	case TAPI_SIM_SPCK_REQUIRED:		/**< Service Provider Control Key Required */
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_SPCK;
		break;
	case TAPI_SIM_CCK_REQUIRED:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_CCK;
		break;
	default:
		_DBG("\tType: %d", res->type);
		_DBG("\tRetry: %d", res->retry_count);
		ti.st = SIM_REQ_LOCK;
		break;
	}

	/*  TODO: retry or error??? */
	if (t->cb)
		t->cb(&ti, t->cb_data);
}

static enum sim_stat check_sim(TelSimCardStatus_t stat)
{
	enum sim_stat r;
	switch (stat) {
	case TAPI_SIM_STATUS_CARD_NOT_PRESENT:	/*  Card not present. */
	case TAPI_SIM_STATUS_CARD_REMOVED:	/* Card removed. */
		r = SIM_EMPTY;
		break;
		/* case 0xFF: // Sim is Initializing state.      //the value is not in TelSimCardStatus_t, remove it */
	case TAPI_SIM_STATUS_SIM_INITIALIZING:	/*  Sim is Initializing state. */
		r = SIM_WAITING;
		break;
	case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:	/* Sim Initialization ok. */
		r = SIM_OK;
		break;
	case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:	/* PIN required state. */
		r = SIM_REQ_PIN;
		break;
	case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:	/* PUK required state. */
		r = SIM_REQ_PUK;
		break;
	case TAPI_SIM_STATUS_CARD_BLOCKED:	/* PIN/PUK blocked(permanently blocked) */
		r = SIM_BLOCKED;
		break;
	case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:	/* PH-SIM (phone-sim) locked state. */
		r = SIM_REQ_LOCK;
		break;
	case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:	/* Network Control Key required state. */
		r = SIM_REQ_NCK;
		break;
	case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:	/* Network Subset Control Key required state. */
		r = SIM_REQ_NSCK;
		break;
	case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:	/* Service Provider Control Key required state. */
		r = SIM_REQ_SPCK;
		break;
	case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:	/* Corporate Control Key required state. */
		r = SIM_REQ_CCK;
		break;
	case 0xff:		/* for tapi default value, timing issue */
		r = SIM_RETRY;
		break;
		/* TODO: implement more locks... */
	case TAPI_SIM_STATUS_CARD_ERROR:	/* Bad card , On the fly SIM gone bad. */
	default:
		r = SIM_ERROR;
		break;
	}
	return r;
}

enum sim_stat pwlock_tapi_check_sim(struct tapi *t, int *changed)
{
	TapiResult_t r = TAPI_API_SUCCESS;
	int card_changed = 0;
	enum sim_stat res = -1;
	TelSimCardStatus_t st = 0x00;

	_DBG("Enum size: %d", sizeof(st));
	r = tel_get_sim_init_info(t->handle, &st, &card_changed);

	_DBG("tel_get_sim_init_info return %d, sim state: %d", r, st);

	if (r == TAPI_API_SUCCESS) {
		*changed = card_changed;
		_DBG("Get tapi sim status: 0x%x card changed: %d", st,
		     *changed);
	}

	switch (r) {
	case TAPI_API_SERVICE_NOT_READY:
		res = SIM_RETRY;
		break;
	case TAPI_API_SUCCESS:
		res = check_sim(st);
		break;
	default:
		res = SIM_ERROR;
		break;
	}

	return res;
}

int pwlock_tapi_ready_check(void)
{
	int api_ret = 0;
	int vconf_val = 0;

	_DBG("%s", __func__);

	api_ret = vconf_get_int(VCONFKEY_TELEPHONY_TAPI_STATE, &vconf_val);
	if (api_ret != 0) {
		/*
		   if appn check very fast before telephony boot up, telephony can not be ready yet
		   in this point, appn should register vconf callback fuction with timer
		   timer will be used to waiting telephony boot up with limitation.
		   user should set proper time. (currently it`s up to experience)
		 */
		_DBG("fail to get vconf key value. this means telephony does not start");
	} else {
		if (vconf_val == VCONFKEY_TELEPHONY_TAPI_STATE_NONE) {
			_DBG("TAPI is not ready");
		} else {
			_DBG("TAPI is ready");
		}
	}
	return vconf_val;
}

int pwlock_tapi_get_sim_lock_info(struct tapi* t)
{
	int r;
	TelSimLockType_t pin_type = 0;

	_DBG("%s", __func__);
	
	pin_type = TAPI_SIM_LOCK_SC;

	r = tel_get_sim_lock_info(t->handle, pin_type, pwlock_tapi_get_sim_lock_info_cb, t);

	_DBG("tel_get_sim_lock_info result : %d", r);

	return 0;
}

static int verify_pins(struct tapi *t, char *code, int type)
{
	int r;
	TelSimSecPw_t pin;

	memset(&pin, 0, sizeof(pin));
	pin.type = type;
	pin.pw_len = strlen(code);
	pin.pw = (unsigned char *)code;

	_DBG("SIM PIN/PUK verifying...");

	r = tel_verifiy_sim_pins(t->handle, &pin, pwlock_tapi_verify_sim_pins_and_puks_cb, t);

	if (r != TAPI_API_SUCCESS) {
		_ERR("TAPI verify pins error %d", r);
		return -1;
	}

	return 0;
}

int pwlock_tapi_verify_pins(struct tapi *t, char *code)
{
	if (t == NULL)
		return -1;

	if (code == NULL || code[0] == '\0')
		return -1;

	return verify_pins(t, code, TAPI_SIM_PTYPE_PIN1);
}

int pwlock_tapi_verify_lock(struct tapi *t, char *code)
{
	if (t == NULL)
		return -1;

	if (code == NULL || code[0] == '\0')
		return -1;

	return verify_pins(t, code, TAPI_SIM_PTYPE_SIM);
}

int pwlock_tapi_verify_puks(struct tapi *t, char *code, char *newcode)
{
	int r;
	TelSimSecPw_t puk, pin;

	if (code == NULL || code[0] == '\0'
	    || newcode == NULL || newcode[0] == '\0')
		return -1;

	memset(&puk, 0, sizeof(puk));
	puk.type = TAPI_SIM_PTYPE_PUK1;
	puk.pw_len = strlen(code);
	puk.pw = (unsigned char *)code;

	memset(&pin, 0, sizeof(pin));
	pin.type = TAPI_SIM_PTYPE_PIN1;
	pin.pw_len = strlen(newcode);
	pin.pw = (unsigned char *)newcode;

	r = tel_verify_sim_puks(t->handle, &puk, &pin, pwlock_tapi_verify_sim_pins_and_puks_cb, t);
	if (r != TAPI_API_SUCCESS)
		return -1;

	return 0;
}

int pwlock_tapi_disable_net_pers(struct tapi *t, char *code, TelSimLockType_t type)
{
	int err_code = TAPI_API_SUCCESS;

	if (code == NULL || code[0] == '\0')
		return -1;

	TelSimFacilityPw_t facility_pw = { 0, };
	facility_pw.lock_type = type;
	facility_pw.pw = (unsigned char *)code;
	facility_pw.pw_len = strlen(code);

	err_code = tel_disable_sim_facility(t->handle, &facility_pw, pwlock_tapi_disable_sim_facility_cb, t);

	if (err_code != TAPI_API_SUCCESS) {
		_ERR("TAPI API FAIL: Error Code [0x%x]", err_code);
		return -1;
	}
	/* WAIT EVENT RESPONSE FROM HERE */
	return 0;
}

struct tapi *pwlock_tapi_init(void (*cb) (struct pwlock_tapi_info *, void *), void *data)
{
	int r;
	struct appdata *ad = (struct appdata *)data;
	struct tapi *t;
	TapiHandle *tapi_handle = NULL;

	if (ad == NULL) {
		_ERR("TAPI init error: ad is null");
		return NULL;
	}

	if (cb == NULL)
		return NULL;

	tapi_handle = tel_init(NULL);
	if (tapi_handle == NULL) {
		_ERR("TAPI init error");
		return NULL;
	}
	_DBG("TAPI init");

	t = calloc(1, sizeof(struct tapi));
	if (t == NULL) {
		_ERR("TAPI alloc data error");
		goto err;
	}
	t->handle = tapi_handle;
	t->evt = tapi_events;
	t->evt_sz = sizeof(tapi_events) / sizeof(tapi_events[0]);
	t->cb = cb;
	t->cb_data = data;

	r = _pwlock_tapi_register_noti_events(t);
	if (r == -1) {
		_ERR("TAPI register events error");
		goto err;
	}

	return t;
 err:
	if (t) {
		_pwlock_tapi_deregister_noti_events(t);
		free(t);
	}
	if (tapi_handle != NULL) {
		tel_deinit(tapi_handle);
	}

	return NULL;
}

void pwlock_tapi_exit(struct tapi **t)
{
	if (t == NULL || *t == NULL)
		return;

	_pwlock_tapi_deregister_noti_events(*t);

	if ((*t)->handle != NULL) {
		tel_deinit((*t)->handle);
	}

	free(*t);
	*t = NULL;
}