summaryrefslogtreecommitdiff
path: root/lib/dialer/ph-dialer-tapi.c
blob: 5aa5a64299bc92936756a01c801a5d37c71693b3 (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
/*
* Copyright 2012 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.0 (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 <tapi_common.h>
#include <ITapiSim.h>
#include <ITapiPhonebook.h>
#include <TapiUtility.h>
#include <regex.h>

#include "phone.h"
#include "phone-dialer.h"
#include "ph-dialer-utils.h"

#ifdef Status
	#undef Status
#endif

static TapiHandle *handle = NULL;

typedef enum {
	SIM_INCORRECT_PIN1_CODE,
	SIM_INCORRECT_PIN2_CODE,
	SIM_PIN1_CHANGE_SUCCESS,
	SIM_PIN2_CHANGE_SUCCESS,
	SIM_PIN1_BLOCKED,
	SIM_PIN2_BLOCKED,
} sim_stat_value;

typedef enum {
	TYPE_PIN1 = 0,
	TYPE_PIN2,
} pin_type_value;
typedef struct _tapi_receive_info {
	sim_stat_value stat;
	int retry_cnt;
} tapi_receive_info;

#define PIN1_REGEX "^\\*\\*04\\*[0-9]{4}\\*[0-9]{4}\\*[0-9]{4}#$"
#define PIN2_REGEX "^\\*\\*042\\*[0-9]{4}\\*[0-9]{4}\\*[0-9]{4}#$"
#define PUK_REGEX "\\*\\*05\\*[0-9]{8}\\*[0-9]{4}\\*[0-9]{4}#$"


int dialer_tapi_init()
{
	handle = tel_init(NULL);
	if (NULL == handle)
		return FALSE;
	return TRUE;
}

int dialer_tapi_deinit()
{
	int ret = tel_deinit(handle);
	handle = NULL;
	return ret;
}

int dialer_check_pin_str(const char* number, char **old_pin, char **new_pin, char **com_pin, int* pin_type)
{
	PH_FN_CALL;
	regex_t fsm;
	regmatch_t str[strlen(number)+1];
	char* origin_string;
	char * str_temp;

	if(regcomp(&fsm, PIN1_REGEX, REG_EXTENDED))
	{
		PH_DBG("regular expression fail");
		regfree(&fsm);
		return FALSE;
	}
	else {
		if(regexec(&fsm, number, strlen(number)+1, str, 0) == REG_NOMATCH)
		{
			PH_DBG("regular expression 1 matching fail");
			regfree(&fsm);
		}
		else
		{
			PH_DBG("regular expression 1 matching success");
			origin_string = strdup(number);
			str_temp = origin_string + 5;
			*old_pin = strdup(strtok(str_temp, "*"));
			*new_pin = strdup(strtok(NULL, "*"));
			*com_pin = strdup(strtok(NULL, "#"));
			*pin_type = TYPE_PIN1;
			free(origin_string);
			regfree(&fsm);
			return TRUE;
		}
	}

	if(regcomp(&fsm, PIN2_REGEX, REG_EXTENDED))
	{
		PH_DBG("regular expression fail");
		regfree(&fsm);
		return FALSE;
	}
	else
	{
		if(regexec(&fsm, number, strlen(number)+1, str, 0) == REG_NOMATCH)
		{
			PH_DBG("regular expression 2 matching fail");
			regfree(&fsm);
		}
		else
		{
			PH_DBG("regular expression 2 matching success");
			origin_string = strdup(number);
			str_temp = origin_string + 5;
			*old_pin = strdup(strtok(str_temp, "*"));
			*new_pin = strdup(strtok(NULL, "*"));
			*com_pin = strdup(strtok(NULL, "#"));
			*pin_type = TYPE_PIN2;
			free(origin_string);
			regfree(&fsm);
			return TRUE;
		}
	}
	return FALSE;
}

void dialer_pin_result_popup (tapi_receive_info* result, void* data)
{
	PH_FN_CALL;
	Evas_Object *popup;
	ph_dialer_data *dial_d  = (ph_dialer_data *)data;

	popup = elm_popup_add(dial_d->win);
	evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

	switch(result->stat)
	{
		case SIM_PIN1_CHANGE_SUCCESS :
		case SIM_PIN2_CHANGE_SUCCESS :
			phone_show_popup(dial_d->navi, S_(CT_SYS_POP_SUCCESS), 2.0);
			break;
		case SIM_INCORRECT_PIN1_CODE :
		case SIM_INCORRECT_PIN2_CODE :
			phone_show_popup(dial_d->navi, S_(PH_SYS_POP_INCORRECT_PASSWORD), 2.0);
			break;
		case SIM_PIN1_BLOCKED :
		case SIM_PIN2_BLOCKED :
			phone_show_popup(dial_d->navi, S_(PH_SYS_SK3_BLOCK), 2.0);
			break;
		default:
			elm_object_text_set(popup, "dafault case called");
			break;
	}
}


void dialer_sim_change_pins_cb(TapiHandle *handle, int result, void *data, void *user_data)
{
	PH_FN_CALL;
	TelSimPinOperationResult_t sec_rt = result;
	TelSimSecResult_t *sim_event_data = (TelSimSecResult_t *) data;
	tapi_receive_info result_info = { 0, };

	PH_DBG("sec_rt[%d]", sec_rt);
	PH_DBG("sim_event_data->type:%d", sim_event_data->type);
	PH_DBG("sim_event_data->retry_count[%d]", sim_event_data->retry_count);

	if(sec_rt == TAPI_SIM_PIN_OPERATION_SUCCESS)
	{
		if(sim_event_data->type == TAPI_SIM_PTYPE_PIN1)
			result_info.stat = SIM_PIN1_CHANGE_SUCCESS;
		else if ( sim_event_data->type == TAPI_SIM_PTYPE_PIN2)
			result_info.stat = SIM_PIN2_CHANGE_SUCCESS;
	}
 	else if (sec_rt ==TAPI_SIM_PIN_INCORRECT_PASSWORD)
	{
		if (sim_event_data->type == TAPI_SIM_PTYPE_PIN1)
		{
			result_info.stat  = SIM_INCORRECT_PIN1_CODE;
			result_info.retry_cnt = sim_event_data->retry_count;
		}
		else if (sim_event_data->type == TAPI_SIM_PTYPE_PIN2)
		{
			result_info.stat = SIM_INCORRECT_PIN2_CODE;
			result_info.retry_cnt = sim_event_data->retry_count;
		}
	}
	else if (sec_rt == TAPI_SIM_PUK_REQUIRED)
	{
		if (sim_event_data->type == TAPI_SIM_PTYPE_PIN1)
			result_info.stat = SIM_PIN1_BLOCKED;
		else if (sim_event_data->type == TAPI_SIM_PTYPE_PIN2)
			result_info.stat = SIM_PIN2_BLOCKED;
	}
	else {
		ERR("INCORRECTED");
		return;
	}
	dialer_pin_result_popup(&result_info, user_data);
}


void dialer_pin_operation(int type,  const char *old_pin, const char *new_pin, void* data)
{
	PH_FN_CALL;
	int tapi_ret = -100;
	ph_dialer_data *dial_d  = (ph_dialer_data *)data;

	char old_pw[PH_TEXT_MAX_LEN] = {0};
	char new_pw[PH_TEXT_MAX_LEN]={0};

	TelSimSecPw_t sim_old_sec_data = {0, };
	TelSimSecPw_t sim_new_sec_data = {0, };

	if(handle == NULL)
		PH_DBG("tapi handle is null");

	snprintf(old_pw, sizeof(old_pw), "%s", old_pin);
	snprintf(new_pw, sizeof(new_pw), "%s", new_pin);

	switch(type)
	{
		case TYPE_PIN1:
			sim_old_sec_data.type = TAPI_SIM_PTYPE_PIN1;
			sim_new_sec_data.type = TAPI_SIM_PTYPE_PIN1;
			sim_old_sec_data.pw_len = STRLEN(old_pw);
			sim_old_sec_data.pw = (unsigned char *)calloc(1, sim_old_sec_data.pw_len);
			memcpy(sim_old_sec_data.pw, old_pw, sim_old_sec_data.pw_len);
			sim_new_sec_data.pw_len = STRLEN(new_pw);
			sim_new_sec_data.pw = (unsigned char *)calloc(1, sim_new_sec_data.pw_len);
			memcpy(sim_new_sec_data.pw, new_pw, sim_new_sec_data.pw_len);
			break;
		case TYPE_PIN2:
			sim_old_sec_data.type = TAPI_SIM_PTYPE_PIN2;
			sim_new_sec_data.type = TAPI_SIM_PTYPE_PIN2;
			sim_old_sec_data.pw_len = STRLEN(old_pw);
			sim_old_sec_data.pw = (unsigned char *)calloc(1, sim_old_sec_data.pw_len);
			memcpy(sim_old_sec_data.pw, old_pw, sim_old_sec_data.pw_len);
			sim_new_sec_data.pw_len = STRLEN(new_pw);
			sim_new_sec_data.pw = (unsigned char *)calloc(1, sim_new_sec_data.pw_len);
			memcpy(sim_new_sec_data.pw, new_pw, sim_new_sec_data.pw_len);
			break;
		default:
			ERR("Never get here(type: %d)", type);
			break;
	}

	tapi_ret = tel_change_sim_pins(handle,&sim_old_sec_data,&sim_new_sec_data, dialer_sim_change_pins_cb, data);
	PH_DBG("return_num:%d", tapi_ret);

	if(tapi_ret != TAPI_API_SUCCESS)
	{
		ERR("TelTapiSimChangePIN err=%d ", tapi_ret);
		phone_show_popup(dial_d->navi, S_(CT_SYS_POP_ERROR), 2.0);
	}
	free(sim_old_sec_data.pw);
	free(sim_new_sec_data.pw);
}


int GCF_tapi_init()
{
	int status;
	int ret;
	TelSimPbList_t pb_list;

	handle = tel_init(NULL);
	if (NULL == handle)
		return FALSE;

	ret = tel_get_sim_pb_init_info(handle, &status, &pb_list);
	if (TAPI_API_SUCCESS != ret)
	{
		ERR("tel_get_sim_pb_init_info() is failed(%d)", ret);
		tel_deinit(handle);
		handle = NULL;
		return FALSE;
	}

	if (!status)
	{
		ERR("SIM is not available");
		tel_deinit(handle);
		handle = NULL;
		return FALSE;
	}
	return TRUE;
}

int GCF_tapi_deinit()
{
	int ret = tel_deinit(handle);
	handle = NULL;
	return ret;
}

static void sim_async_response_verify_puk(TapiHandle *handle, int result, void *data, void *user_data)
{
	PH_FN_CALL;
	TelSimPinOperationResult_t sec_rt = result;
	TelSimSecResult_t *pPinInfo = data;

	if (sec_rt == TAPI_SIM_PIN_OPERATION_SUCCESS)
	{
		if (pPinInfo->type == TAPI_SIM_PTYPE_PUK1)
		{
			ph_dialer_data *dial_d = user_data;
			phone_show_popup(dial_d->navi, T_(PH_GET_TEXT_BASIC, PHTEXT_REQUEST_SUCCESS), 2.0);
		}
		else if (pPinInfo->type == TAPI_SIM_PTYPE_PIN2)
			PH_DBG("Unblock PIN2 Success!");
	}
	else
	{
		if (pPinInfo->type == TAPI_SIM_PTYPE_PUK1)
		{
			ph_dialer_data *dial_d = user_data;
			phone_show_popup(dial_d->navi, T_(PH_GET_TEXT_BASIC, PHTEXT_REQUEST_FAIL), 2.0);
		}
		else if (pPinInfo->type == TAPI_SIM_PTYPE_PIN1 || pPinInfo->type == TAPI_SIM_PTYPE_PIN2 || pPinInfo->type == TAPI_SIM_PTYPE_PUK2)
		{
			ERR("%d Verification Failed! - PIN Required", pPinInfo->type);
			PH_DBG("Remainint attempts [%d]", pPinInfo->retry_count);
		}
	}

	GCF_tapi_deinit();
}

void GCF_test_puk_operation(const char *puk1, const char *pin1, void *user_data)
{
	PH_FN_CALL;
	char init_pin_val[PH_TEXT_MAX_LEN] = {0};
	char init_puk_val[PH_TEXT_MAX_LEN]={0};

	TelSimSecPw_t puk_data;
	TelSimSecPw_t new_pin_data;
	int ret;

	memset(&puk_data, 0, sizeof(TelSimSecPw_t));
	memset(&new_pin_data, 0, sizeof(TelSimSecPw_t));

	PH_DBG("puk1:%s", puk1 );
	PH_DBG("pin1: %s", pin1 );


	snprintf(init_puk_val, sizeof(init_puk_val), "%s", puk1);
	snprintf(init_pin_val, sizeof(init_pin_val), "%s", pin1);

	puk_data.type = TAPI_SIM_PTYPE_PUK1;   // 0x00
	puk_data.pw_len = strlen(init_puk_val);
	puk_data.pw = (unsigned char*)calloc(1, puk_data.pw_len);
	memcpy(puk_data.pw, init_puk_val, puk_data.pw_len);

	new_pin_data.type = TAPI_SIM_PTYPE_PIN1;   // 0x00
	new_pin_data.pw_len = strlen(init_pin_val);
	new_pin_data.pw = (unsigned char*)calloc(1, new_pin_data.pw_len);
	memcpy(new_pin_data.pw, init_pin_val, new_pin_data.pw_len);

	ret = tel_verify_sim_puks(handle, &puk_data, &new_pin_data, sim_async_response_verify_puk, user_data);
	if (ret != TAPI_API_SUCCESS)
	{
		ERR("TAPI API FAIL: Error Code [0x%x]", ret);
		GCF_tapi_deinit();
	}

	free(puk_data.pw);
	free(new_pin_data.pw);
}

static void sim_async_response_read_contact(TapiHandle *handle, int result, void *data, void *user_data)
{
	PH_FN_CALL;
	TelSimPbAccessResult_t sec_rt = result;
	TelSimPbRecord_t *sim_acces_info = data;

	if (sec_rt != TAPI_SIM_PB_SUCCESS)
	{
		ERR("SIM phone book access error [%d]", sec_rt);
		GCF_tapi_deinit();
		return;
	}

	if (sim_acces_info->phonebook_type == TAPI_SIM_PB_ADN) //KKC - ADN number value!
		ph_dialer_util_show_matched_one_number(user_data, (char *)sim_acces_info->name, (char *)sim_acces_info->number);
	else if (sim_acces_info->phonebook_type == TAPI_SIM_PB_3GSIM)
	{
		if(strlen(sim_acces_info->number) > 0)
			ph_dialer_util_show_matched_one_number(user_data, (char *)sim_acces_info->name, (char*)(sim_acces_info->number));
		if (sim_acces_info->anr1_ton == TAPI_SIM_TON_ABBREVIATED_NUMBER) //KKC - USIM ADN number value!
			ph_dialer_util_show_matched_one_number(user_data, NULL, (char*)(sim_acces_info->anr1));
		if (sim_acces_info->anr2_ton == TAPI_SIM_TON_ABBREVIATED_NUMBER) //KKC - USIM ADN number value!
			ph_dialer_util_show_matched_one_number(user_data, NULL, (char*)(sim_acces_info->anr2));
		if (sim_acces_info->anr3_ton == TAPI_SIM_TON_ABBREVIATED_NUMBER) //KKC - USIM ADN number value!
			ph_dialer_util_show_matched_one_number(user_data, NULL, (char*)(sim_acces_info->anr3));
	}
	GCF_tapi_deinit();
}

void GCF_test_read_contact(int index, void *user_data)
{
	TelSimPbType_t pb_type = 0;
	TelSimCardType_t card_type = 0;
	int ret;

	tel_get_sim_type(handle, &card_type);
	if (card_type == TAPI_SIM_CARD_TYPE_GSM)
		pb_type = TAPI_SIM_PB_ADN;
	else if (card_type == TAPI_SIM_CARD_TYPE_USIM)
		pb_type = TAPI_SIM_PB_3GSIM;

	ret = tel_read_sim_pb_record(handle, pb_type, index, sim_async_response_read_contact, user_data);
	if (ret != TAPI_API_SUCCESS) {
		ERR("TAPI API FAIL: Error Code [0x%x]", ret);
		GCF_tapi_deinit();
	}
}

/* Gcf Puk1*/
#define GCF_PUK1_PRE_VALUE "**05*"

int GCF_check_puk_str(const char* number, char **dest_puk, char **dest_pin1, char **dest_pin2, int size_dest)
{
	PH_FN_CALL;
	regex_t fsm;
	regmatch_t str[strlen(number)+1];
	char* origin_string;
	char * str_temp;

	if(regcomp(&fsm, PUK_REGEX, REG_EXTENDED))
	{
		PH_DBG("regular expression fail");
		regfree(&fsm);
		return FALSE;
	}
	else
	{
		if(regexec(&fsm, number, strlen(number)+1, str, 0) == REG_NOMATCH)
		{
			PH_DBG("matching fail");
			regfree(&fsm);
		}
		else
		{
			PH_DBG("matching success");
			origin_string = strdup(number);
			str_temp = origin_string + 5;
			*dest_puk = strdup(strtok(str_temp, "*"));
			*dest_pin1 = strdup(strtok(NULL, "*"));
			*dest_pin2 = strdup(strtok(NULL, "#"));
			free(origin_string);
			regfree(&fsm);
			return TRUE;
		}
	}
	return FALSE;
}

bool GCF_check_admin_sim(const char *number, int *ret_index)
{
	int count;
	char *dest;
	bool success;
	p_retvm_if(number==NULL || ret_index==NULL, FALSE, "Parameter is null");

	count = strlen(number);
	p_retvm_if(count < 2, FALSE, "number length is too short");
	dest = (char*)calloc(1, count);

	success = true;
	if (number[count-1]=='#')
	{
		int i=0;
		for (;i<count-1;i++)
		{
			if ('0' <= number[i] && number[i] <= '9')
				dest[i] = number[i];
			else
			{
				success = false;
				break;
			}
		}
		if (success)
			*ret_index = atoi(dest);
	}
	else
		success = false;
	free(dest);

	return success;
}