Use datadir variable for file path
[platform/core/telephony/tel-plugin-packetservice.git] / test_src / main.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <wait.h>
27 #include <errno.h>
28
29 #include <tzplatform_config.h>
30
31 #include <glib.h>
32 #include <gio/gio.h>
33 #include <db-util.h>
34 #include <libxml/xmlmemory.h>
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37
38 #define DATABASE_PATH tzplatform_mkpath(TZ_SYS_DB, ".dnet.db")
39 #define msg(fmt, args...) do { printf(fmt "\n", ##args); fflush(stdout); } while (0)
40
41 enum context_type {
42         CONTEXT_TYPE_UNKNOWN,
43         CONTEXT_TYPE_X25,
44         CONTEXT_TYPE_IP,
45         CONTEXT_TYPE_IHOST,
46         CONTEXT_TYPE_PPP,
47         CONTEXT_TYPE_IPV6,
48         CONTEXT_TYPE_IPV4V6,
49 };
50
51 enum context_role {
52         CONTEXT_ROLE_UNKNOWN,
53         CONTEXT_ROLE_INTERNET,
54         CONTEXT_ROLE_MMS,
55         CONTEXT_ROLE_PREPAID_INTERNET,
56         CONTEXT_ROLE_PREPAID_MMS,
57         CONTEXT_ROLE_TETHERING,
58         CONTEXT_ROLE_USER_DEFINED,
59 };
60
61 static int __system_command(char *command)
62 {
63         int pid = 0,
64         status = 0;
65         const char *environ[] = { NULL };
66
67         if (command == NULL)
68                 return -1;
69
70         msg("%s", command);
71
72         pid = fork();
73         if (pid == -1)
74                 return -1;
75
76         if (pid == 0) {
77                 char *argv[4];
78
79                 argv[0] = "sh";
80                 argv[1] = "-c";
81                 argv[2] = (char *)command;
82                 argv[3] = 0;
83
84                 execve("/bin/sh", argv, (char **)environ);
85                 exit(127);
86         }
87
88         do {
89                 if (waitpid(pid, &status, 0) == -1) {
90                         if (errno != EINTR)
91                                 return -1;
92                 } else {
93                         if (WIFEXITED(status))
94                                 return WEXITSTATUS(status);
95                         else if (WIFSIGNALED(status))
96                                 return WTERMSIG(status);
97                         else if (WIFSTOPPED(status))
98                                 return WSTOPSIG(status);
99                 }
100         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
101
102         return 0;
103 }
104
105 static void __load_xml_file(const char *docname, const char *groupname, void **i_doc, void **i_root_node)
106 {
107         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
108         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
109
110         msg("docname:%s, groupname:%s", docname, groupname);
111
112         *doc = xmlParseFile(docname);
113         if (*doc) {
114                 *root_node = xmlDocGetRootElement(*doc);
115                 if (*root_node) {
116                         msg("*root_node->name:%s", (*root_node)->name);
117                         if (0 == xmlStrcmp((*root_node)->name, (const unsigned char *)groupname)) {
118                                 msg("root_node is found !!!");
119                                 return;
120                         } else {
121                                 msg("Cannot find root node.");
122                                 *root_node = NULL;
123                         }
124                 }
125                 xmlFreeDoc(*doc);
126                 *doc = NULL;
127         } else {
128                 msg("fail to parse doc(%s)", docname);
129         }
130 }
131
132 static void __unload_xml_file(void **i_doc, void **i_root_node)
133 {
134         xmlDocPtr *doc = (xmlDocPtr *)i_doc;
135         xmlNodePtr *root_node = (xmlNodePtr *)i_root_node;
136
137         msg("unloading XML");
138         if (doc && *doc) {
139                 xmlFreeDoc(*doc);
140                 *doc = NULL;
141                 if (root_node)
142                         *root_node = NULL;
143         }
144 }
145
146 static void *create_handle(const char *path)
147 {
148         int rv = 0;
149         sqlite3 *handle = NULL;
150
151         rv = db_util_open(path, &handle, 0);
152         if (rv != SQLITE_OK) {
153                 msg("fail to connect database rv(%d)", rv);
154                 return NULL;
155         }
156
157         msg("connected to %s", path);
158         return handle;
159 }
160
161 static gboolean remove_handle(void *handle)
162 {
163         if (!handle)
164                 return FALSE;
165
166         db_util_close(handle);
167
168         msg("disconnected from database");
169         return TRUE;
170 }
171
172 static gboolean read_query_database(void *handle, const char *query, GHashTable *in_param,
173                 GHashTable *out_param, int out_param_cnt)
174 {
175         int rv = 0, index = 0, outter_index = 0;
176         sqlite3_stmt *stmt = NULL;
177         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
178
179         GHashTableIter iter;
180         gpointer key, value;
181
182         msg("read query");
183
184         memset(szQuery, '\0', 5001);
185         strncpy(szQuery, query, 5000);
186
187         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
188         if (rv != SQLITE_OK) {
189                 msg("fail to connect to table (%d)", rv);
190                 return FALSE;
191         }
192
193         if (in_param) {
194                 g_hash_table_iter_init(&iter, in_param);
195                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
196                         msg("key(%s), value(%s)", (const char *)key, (const char *)value);
197
198                         if (!value || g_strcmp0((const char *) value, "") == 0) {
199                                 msg("bind null");
200                                 rv = sqlite3_bind_null(stmt, atoi((const char *) key));
201                         } else {
202                                 msg("bind value");
203                                 rv = sqlite3_bind_text(stmt, atoi((const char *) key), (const char *) value, strlen((const char *) value),
204                                                 SQLITE_STATIC);
205                         }
206
207                         if (rv != SQLITE_OK) {
208                                 msg("fail to bind data (%d)", rv);
209                                 return FALSE;
210                         }
211                 }
212         }
213
214         rv = sqlite3_step(stmt);
215         msg("read query executed (%d)", rv);
216
217         while (rv == SQLITE_ROW) {
218
219                 char tmp_key_outter[10];
220                 GHashTable *out_param_data;
221
222                 out_param_data = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
223
224                 for (index = 0; index < out_param_cnt; index++) {
225                         char *tmp = NULL, tmp_key[10];
226                         tmp = (char *) sqlite3_column_text(stmt, index);
227                         snprintf(tmp_key, sizeof(tmp_key), "%d", index);
228                         g_hash_table_insert(out_param_data, g_strdup(tmp_key), g_strdup(tmp));
229                 }
230
231                 snprintf(tmp_key_outter, sizeof(tmp_key_outter), "%d", outter_index);
232                 g_hash_table_insert(out_param, g_strdup(tmp_key_outter), out_param_data);
233                 outter_index++;
234                 rv = sqlite3_step(stmt);
235         }
236
237         sqlite3_finalize(stmt);
238         return TRUE;
239 }
240
241 static gboolean query_database(void *handle, const char *query, GHashTable *in_param)
242 {
243         int rv = 0;
244         sqlite3_stmt *stmt = NULL;
245         char szQuery[5000+1];   /* +1 is for NULL Termination Character '\0' */
246
247         GHashTableIter iter;
248         gpointer key, value;
249         msg("query database");
250
251         memset(szQuery, '\0', 5001);
252         strncpy(szQuery, query, 5000);
253
254         rv = sqlite3_prepare_v2(handle, szQuery, strlen(szQuery), &stmt, NULL);
255         if (rv != SQLITE_OK) {
256                 msg("fail to connect to table (%d)", rv);
257                 return FALSE;
258         }
259
260         if (in_param) {
261                 g_hash_table_iter_init(&iter, in_param);
262                 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
263                         msg("key(%s), value(%s)", (const char *)key, (const char *)value);
264
265                         if (!value || g_strcmp0((const char *) value, "") == 0) {
266                                 msg("bind null");
267                                 rv = sqlite3_bind_null(stmt, atoi((const char *) key));
268                         } else {
269                                 msg("bind value");
270                                 rv = sqlite3_bind_text(stmt, atoi((const char *) key), (const char *) value, strlen((const char *) value),
271                                                 SQLITE_STATIC);
272                         }
273
274                         if (rv != SQLITE_OK) {
275                                 msg("fail to bind data (%d)", rv);
276                                 return FALSE;
277                         }
278                 }
279         }
280
281         rv = sqlite3_step(stmt);
282         msg("query executed (%d)", rv);
283         sqlite3_finalize(stmt);
284
285         if (rv != SQLITE_DONE)
286                 return FALSE;
287
288         return TRUE;
289 }
290
291 static gboolean __reset_database(void)
292 {
293         gpointer handle;
294         char szQuery[5000];
295         gboolean rv = FALSE;
296
297         /* Initialize Storage */
298         handle = create_handle(DATABASE_PATH);
299         if (handle == NULL) {
300                 msg("Failed to get Storage handle");
301                 return rv;
302         }
303
304         /* SQL query */
305         memset(szQuery, 0x0, sizeof(szQuery));
306         strcat(szQuery, " delete from pdp_profile");
307
308         rv = query_database(handle, szQuery, NULL);
309         msg("Reset profile table: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
310
311         /* De-initialize Storage */
312         remove_handle(handle);
313
314         return rv;
315 }
316
317 static int __insert_network_id_to_database(gchar *mccmnc)
318 {
319         gpointer handle;
320         GHashTable *in_param, *out_param;
321         char szQuery[5000];
322         gboolean rv = FALSE;
323
324         GHashTableIter iter;
325         gpointer key, value;
326         int network_id = 0;
327
328         /* Initialize Database */
329         handle = create_handle(DATABASE_PATH);
330         if (handle == NULL) {
331                 msg("Failed to get Storage handle");
332                 return rv;
333         }
334
335         /*
336          * Check the maximum Network ID that exists in database,
337          * if NONE exists, then 'Network ID' would be equal to 1
338          * else if there exists a valid maximum entry; 'Network ID' would be incremented value.
339          */
340         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
341                                 (GDestroyNotify) g_hash_table_destroy);
342
343         /* SQL query */
344         memset(szQuery, 0x0, sizeof(szQuery));
345         strcpy(szQuery, "select max(network_info_id) as network_id from network_info");
346
347         rv = read_query_database(handle, szQuery, NULL, out_param, 1);
348         msg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
349
350         g_hash_table_iter_init(&iter, out_param);
351         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
352                 GHashTableIter iter2;
353                 gpointer key2, value2;
354
355                 if (value) {
356                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
357                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
358                                 msg("key2(%s) value2(%s)", (const char *)key2, (const char *)value2);
359                                 if (g_str_equal(key2, "0") == TRUE) {
360                                         if (!value2 || g_strcmp0((const char *)value2, "") == 0)
361                                                 network_id = 0;
362                                         else
363                                                 network_id = atoi((const char *)value2);
364
365                                         /* TODO - Check this logic */
366                                         break;
367                                 }
368                         }
369                 }
370         }
371
372         /* Free Resources */
373         g_hash_table_destroy(out_param);
374
375         /* Increment Network ID */
376         network_id++;
377
378         /* SQL query */
379         memset(szQuery, 0x0, sizeof(szQuery));
380         strcpy(szQuery, " insert into network_info(network_info_id, network_name, mccmnc) values(?, ?, ?) ");
381
382         /* Initialize parameters */
383         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
384         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", network_id));  /* Network ID */
385         g_hash_table_insert(in_param, "2", g_strdup_printf("PLMN_%s", mccmnc));
386         g_hash_table_insert(in_param, "3", g_strdup(mccmnc));
387
388         rv = query_database(handle, szQuery, in_param);
389         if (rv == FALSE) {
390                 msg("Failed to insert query to Storage");
391                 network_id = 0;
392         }
393
394         /* Free resources */
395         g_hash_table_destroy(in_param);
396
397         /* De-initialize Storage */
398         remove_handle(handle);
399
400         return network_id;
401 }
402
403
404 static int __load_network_id_from_database(gchar *mccmnc)
405 {
406         gpointer handle;
407         GHashTable *in_param, *out_param;
408         char szQuery[5000];
409         gboolean rv = FALSE;
410
411         GHashTableIter iter;
412         gpointer key, value;
413
414         int network_id = -1;
415
416         /* Initialize Storage */
417         handle = create_handle(DATABASE_PATH);
418         if (handle == NULL) {
419                 msg("Failed to get Storage handle");
420                 return network_id;
421         }
422
423         /* Initialize parameters */
424         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
425         g_hash_table_insert(in_param, "1", g_strdup(mccmnc));
426
427         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
428                         (GDestroyNotify) g_hash_table_destroy);
429
430         /* SQL Query */
431         memset(szQuery, 0x0, sizeof(szQuery));
432         strcpy(szQuery, "select network_info_id from network_info where mccmnc = ? ");
433
434         rv = read_query_database(handle, szQuery, in_param, out_param, 1);
435         msg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
436
437         g_hash_table_iter_init(&iter, out_param);
438         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
439                 GHashTableIter iter2;
440                 gpointer key2, value2;
441
442                 if (value) {
443                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
444                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
445                                 if (g_str_equal(key2, "0") == TRUE) {
446                                         if (!value2 || (g_strcmp0((const char *)value2, "") == 0))
447                                                 network_id = 0;
448                                         else
449                                                 network_id = atoi((const char *)value2);
450
451                                         /* TODO - Check this out */
452                                         break;
453                                 }
454                         }
455                 }
456         }
457
458         /* Free resources */
459         g_hash_table_destroy(in_param);
460         g_hash_table_destroy(out_param);
461
462         /* De-initialize Storage */
463         remove_handle(handle);
464
465         return network_id;
466 }
467
468
469 static int __get_network_id(gchar *mccmnc)
470 {
471         int network_id;
472
473         network_id = __load_network_id_from_database(mccmnc);
474         msg("network id(%d)", network_id);
475         if (network_id > 0)
476                 return network_id;
477
478         network_id = __insert_network_id_to_database(mccmnc);
479         if (network_id <= 0)
480                 return -1;
481
482         return network_id;
483 }
484
485 static int __load_profile_id_from_database(void)
486 {
487         gpointer handle;
488         GHashTable *out_param;
489         char szQuery[5000];
490         gboolean rv = FALSE;
491
492         GHashTableIter iter;
493         gpointer key, value;
494
495         int profile_id = -1;
496
497         /* Initialize Database */
498         handle = create_handle(DATABASE_PATH);
499         if (handle == NULL) {
500                 msg("Failed to get Storage handle");
501                 return profile_id;
502         }
503
504         /* Initialize parameters */
505         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
506                         (GDestroyNotify) g_hash_table_destroy);
507
508         /* SQL query */
509         memset(szQuery, 0x0, sizeof(szQuery));
510         strcpy(szQuery, "select max(profile_id) as last_profile from pdp_profile");
511
512         rv = read_query_database(handle, szQuery, NULL, out_param, 1);
513         msg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
514
515         g_hash_table_iter_init(&iter, out_param);
516         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
517                 GHashTableIter iter2;
518                 gpointer key2, value2;
519
520                 if (value) {
521                         g_hash_table_iter_init(&iter2, (GHashTable *)value);
522                         while (g_hash_table_iter_next(&iter2, &key2, &value2) == TRUE) {
523                                 if (g_str_equal(key2, "0") == TRUE) {
524                                         if (!value2 || (g_strcmp0((const char *)value2, "") == 0))
525                                                 profile_id = 0;
526                                         else
527                                                 profile_id = atoi((const char *)value2);
528
529                                         /* TODO - Check this logic */
530                                         break;
531                                 }
532                         }
533                 }
534         }
535
536         /* Free resources */
537         g_hash_table_destroy(out_param);
538         /* De-initialize Storage */
539         remove_handle(handle);
540         return profile_id;
541 }
542
543 static gboolean __get_default_profile_from_database(int network_info_id, int svc_category_id)
544 {
545         gpointer handle;
546         GHashTable *in_param, *out_param;
547         char szQuery[5000];
548         gboolean rv, ret = FALSE;
549         guint profile_cnt;
550
551         /* Initialize Storage */
552         handle = create_handle(DATABASE_PATH);
553         if (handle == NULL) {
554                 msg("Failed to get Storage handle");
555                 return FALSE;
556         }
557
558         /* Initialize parameters */
559         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
560         g_hash_table_insert(in_param, "1", g_strdup_printf("%d", network_info_id));
561         g_hash_table_insert(in_param, "2", g_strdup_printf("%d", svc_category_id));
562
563         out_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
564                         (GDestroyNotify) g_hash_table_destroy);
565
566         /* SQL query */
567         memset(szQuery, 0x0, sizeof(szQuery));
568         strcpy(szQuery, "select profile_id from pdp_profile ");
569         strcat(szQuery, "where network_info_id = ? and svc_category_id = ? and default_internet_con = 1");
570
571         rv = read_query_database(handle, szQuery, in_param, out_param, 1);
572         msg("Read Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
573
574         profile_cnt = g_hash_table_size(out_param);
575         if (profile_cnt > 0) {
576                 msg("default profile for (svc_category_id: %d, network_info_id: %d) exists: count[%d]",
577                         svc_category_id, network_info_id, profile_cnt);
578                 ret = TRUE;
579         }
580         /* Free resources */
581         g_hash_table_destroy(in_param);
582         g_hash_table_destroy(out_param);
583
584         /* De-initialize Database */
585         remove_handle(handle);
586         return ret;
587 }
588
589 static gboolean __insert_apns_to_database(GHashTable *in_param)
590 {
591         gpointer handle;
592         char szQuery[5000];
593         gboolean rv = FALSE;
594
595         if (in_param == NULL) {
596                 msg("in_param is NULL !!!");
597                 return rv;
598         }
599
600         /* Initialize Database */
601         handle = create_handle(DATABASE_PATH);
602         if (handle == NULL) {
603                 msg("Failed to get db handle");
604                 return rv;
605         }
606         /* SQL query */
607         memset(szQuery, 0x0, sizeof(szQuery));
608         strcpy(szQuery, " insert into pdp_profile(");
609         strcat(szQuery, " profile_id, profile_name, apn, auth_type, auth_id, auth_pwd, ");
610         strcat(szQuery, " pdp_protocol, svc_category_id, proxy_ip_addr, home_url, linger_time, ");
611         strcat(szQuery, " network_info_id, hidden, editable, default_internet_con, user_defined) values(");
612         strcat(szQuery, " ?, ?, ?, ?, ?, ?, ");/* 1, 2, 3, 4, 5, 6(auth_pwd) */
613         strcat(szQuery, " ?, ?, ?, ?, 300, ");/* 7, 8, 9, 10(home_url) */
614         strcat(szQuery, " ?, 0, 1, ?, 0)");/* 11, 12(default_internet_con) */
615
616         rv = query_database(handle, szQuery, in_param);
617         msg("Insert to Database: [%s]", (rv == TRUE ? "SUCCESS" : "FAIL"));
618
619         /* De-initialize Database */
620         remove_handle(handle);
621         return rv;
622 }
623
624 static gboolean __duplicate_profile_by_type(GHashTable *in_param, gpointer node, int svc_category_id)
625 {
626         gpointer tmp;
627         xmlNode *cur_node = node;
628         gchar *in_tuple = NULL;
629         int profile_index;
630
631         if (!in_param || !node)
632                 return FALSE;
633
634         tmp = g_hash_table_lookup(in_param, "1");
635         if (tmp) { /* profile_id */
636                 profile_index = atoi((char *)tmp);
637                 profile_index++;
638                 g_hash_table_insert(in_param, "1", g_strdup_printf("%d", profile_index));
639                 msg("profile_id = %d", profile_index);
640         } else {
641                 return FALSE;
642         }
643
644         {/* svc_category_id */
645                 g_hash_table_insert(in_param, "8", g_strdup_printf("%d", svc_category_id));
646                 msg("svc_category_id = %d", svc_category_id);
647         }
648
649         {/* proxy ip */
650                 gchar *proxy_ip_addr = NULL, *proxy = NULL, *port = NULL;
651
652                 if (svc_category_id == CONTEXT_ROLE_MMS) {
653                         proxy = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsproxy");
654                         port = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsport");
655                 } else {
656                         proxy = (char *)xmlGetProp(cur_node, (const unsigned char *)"proxy");
657                         port = (char *)xmlGetProp(cur_node, (const unsigned char *)"port");
658                 }
659                 if (proxy && port) {
660                         proxy_ip_addr = g_strdup_printf("%s:%s", proxy, port);
661                         in_tuple = g_strdup(proxy_ip_addr);
662                         g_free(proxy_ip_addr);
663                 } else {
664                         in_tuple = g_strdup("");
665                 }
666                 g_hash_table_insert(in_param, "9", g_strdup(in_tuple));
667                 msg("proxy_ip_addr = %s", in_tuple);
668                 g_free(in_tuple);
669         }
670
671         {/* home url */
672                 gchar *mmsc = NULL;
673                 mmsc = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsc");
674                 if (mmsc && svc_category_id == CONTEXT_ROLE_MMS)
675                         in_tuple = g_strdup(mmsc);
676                 else
677                         in_tuple = g_strdup("");
678                 g_hash_table_insert(in_param, "10", g_strdup(in_tuple));
679                 msg("home_url = %s", in_tuple);
680                 g_free(in_tuple);
681         }
682
683         {/* default internet connection */
684                 int default_internet_con = 1; /* default */
685
686                 tmp = g_hash_table_lookup(in_param, "11"); /* network_info_id */
687                 if (tmp) {
688                         int network_info_id = atoi((char *)tmp);
689                         msg("network_info_id = %d", network_info_id);
690                         if (network_info_id > 0 && __get_default_profile_from_database(network_info_id, svc_category_id))
691                                 default_internet_con = 0;
692                 }
693                 g_hash_table_insert(in_param, "12", g_strdup_printf("%d", default_internet_con));
694                 msg("default_internet_con = %d", default_internet_con);
695         }
696
697         /* insert duplacte profile to database. */
698         return __insert_apns_to_database(in_param);
699 }
700
701 static GHashTable *__construct_profile_tuples(gpointer node)
702 {
703         xmlNode *cur_node = node;
704         GHashTable *in_param = NULL;
705         gchar *in_tuple = NULL;
706         int profile_id = 0, network_info_id = -1, svc_category_id = 0;
707
708         if (!cur_node)
709                 return NULL;
710
711         /* Initialize parameters */
712         in_param = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
713
714         {/* profile id */
715                 profile_id = __load_profile_id_from_database();
716                 profile_id++;
717                 g_hash_table_insert(in_param, "1", g_strdup_printf("%d", profile_id));
718                 msg("profile_id = %d", profile_id);
719         }
720
721         {/* profile name */
722                 gchar *profile_name = NULL;
723                 profile_name = (char *)xmlGetProp(cur_node, (const unsigned char *)"carrier");
724                 if (profile_name)
725                         in_tuple = g_strdup(profile_name);
726                 else
727                         in_tuple = g_strdup_printf("TEMP_PROFILE_%d", profile_id);
728
729                 g_hash_table_insert(in_param, "2", g_strdup(in_tuple));
730                 msg("profile_name = %s", in_tuple);
731                 g_free(in_tuple);
732         }
733
734         {/* apn */
735                 gchar *apn = NULL;
736                 apn = (char *)xmlGetProp(cur_node, (const unsigned char *)"apn");
737                 if (apn)
738                         in_tuple = g_strdup(apn);
739                 else
740                         in_tuple = g_strdup("");
741                 g_hash_table_insert(in_param, "3", g_strdup(in_tuple));
742                 msg("apn = %s", in_tuple);
743                 g_free(in_tuple);
744         }
745
746         {/* auth type */
747                 gchar *auth_type = NULL, *auth = NULL;
748                 auth_type = (char *)xmlGetProp(cur_node, (const unsigned char *)"auth_type");
749                 auth = (char *)xmlGetProp(cur_node, (const unsigned char *)"auth");
750                 if (auth_type)
751                         in_tuple = g_strdup(auth_type);
752                 else if (auth)
753                         in_tuple = g_strdup(auth);
754                 else
755                         in_tuple = g_strdup("0"); /* CONTEXT_AUTH_NONE */
756
757                 g_hash_table_insert(in_param, "4", g_strdup(in_tuple));
758                 msg("auth_type = %s", in_tuple);
759                 g_free(in_tuple);
760         }
761
762         {/* auth id */
763                 gchar *auth_id = NULL;
764                 auth_id = (char *)xmlGetProp(cur_node, (const unsigned char *)"user");
765                 if (auth_id)
766                         in_tuple = g_strdup(auth_id);
767                 else
768                         in_tuple = g_strdup("");
769                 g_hash_table_insert(in_param, "5", g_strdup(in_tuple));
770                 msg("auth_id = %s", in_tuple);
771                 g_free(in_tuple);
772         }
773
774         {/* auth pwd */
775                 gchar *auth_pwd = NULL;
776                 auth_pwd = (char *)xmlGetProp(cur_node, (const unsigned char *)"password");
777                 if (auth_pwd)
778                         in_tuple = g_strdup(auth_pwd);
779                 else
780                         in_tuple = g_strdup("");
781                 g_hash_table_insert(in_param, "6", g_strdup(in_tuple));
782                 msg("auth_pwd = %s", in_tuple);
783                 g_free(in_tuple);
784         }
785
786         {/* pdp protocol */
787                 gchar *protocol = NULL;
788                 int pdp_protocol = CONTEXT_TYPE_IP;
789                 protocol = (char *)xmlGetProp(cur_node, (const unsigned char *)"protocol");
790                 if (protocol) {
791                         if (!g_strcmp0(protocol, "IPV6"))
792                                 pdp_protocol = CONTEXT_TYPE_IPV6;
793                         else if (!g_strcmp0(protocol, "IPV4V6"))
794                                 pdp_protocol = CONTEXT_TYPE_IPV4V6;
795                 }
796                 g_hash_table_insert(in_param, "7", g_strdup_printf("%d", pdp_protocol));
797                 msg("protocol = %s", protocol);
798         }
799
800         {/* service category id */
801                 gchar *svc_type = NULL;
802                 svc_type = (char *)xmlGetProp(cur_node, (const unsigned char *)"type");
803                 if (NULL != g_strrstr(svc_type, "default"))
804                         svc_category_id = CONTEXT_ROLE_INTERNET;
805                 else if (!g_strcmp0(svc_type, "mms"))
806                         svc_category_id = CONTEXT_ROLE_MMS;
807                 else if (!g_strcmp0(svc_type, "dun"))
808                         svc_category_id = CONTEXT_ROLE_TETHERING;
809
810                 g_hash_table_insert(in_param, "8", g_strdup_printf("%d", svc_category_id));
811                 msg("svc_category_id = %d", svc_category_id);
812         }
813
814         {/* proxy ip */
815                 gchar *proxy_ip_addr = NULL, *proxy = NULL, *port = NULL;
816
817                 if (svc_category_id == CONTEXT_ROLE_MMS) {
818                         proxy = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsproxy");
819                         port = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsport");
820                 } else {
821                         proxy = (char *)xmlGetProp(cur_node, (const unsigned char *)"proxy");
822                         port = (char *)xmlGetProp(cur_node, (const unsigned char *)"port");
823                 }
824                 if (proxy && port) {
825                         proxy_ip_addr = g_strdup_printf("%s:%s", proxy, port);
826                         in_tuple = g_strdup(proxy_ip_addr);
827                         g_free(proxy_ip_addr);
828                 } else {
829                         in_tuple = g_strdup("");
830                 }
831                 g_hash_table_insert(in_param, "9", g_strdup(in_tuple));
832                 msg("proxy_ip_addr = %s", in_tuple);
833                 g_free(in_tuple);
834         }
835
836         {/* home url */
837                 gchar *mmsc = NULL;
838                 mmsc = (char *)xmlGetProp(cur_node, (const unsigned char *)"mmsc");
839                 if (mmsc && svc_category_id == CONTEXT_ROLE_MMS)
840                         in_tuple = g_strdup(mmsc);
841                 else
842                         in_tuple = g_strdup("");
843                 g_hash_table_insert(in_param, "10", g_strdup(in_tuple));
844                 msg("home_url = %s", in_tuple);
845                 g_free(in_tuple);
846         }
847
848         {/* network info id */
849                 gchar *plmn = NULL, *mcc = NULL, *mnc = NULL;
850                 mcc = (char *)xmlGetProp(cur_node, (const unsigned char *)"mcc");
851                 mnc = (char *)xmlGetProp(cur_node, (const unsigned char *)"mnc");
852
853                 if (mcc && mnc) {
854                         plmn = g_strdup_printf("%s%s", mcc, mnc);
855                         msg("mccmnc = %s", plmn);
856                         network_info_id = __get_network_id(plmn);
857                         g_free(plmn);
858                 }
859                 g_hash_table_insert(in_param, "11", g_strdup_printf("%d", network_info_id));
860                 msg("network_info_id = %d", network_info_id);
861         }
862
863         {/* default internet connection */
864                 int default_internet_con = 1;
865                 if (__get_default_profile_from_database(network_info_id, svc_category_id))
866                         default_internet_con = 0;
867                 g_hash_table_insert(in_param, "12", g_strdup_printf("%d", default_internet_con));
868                 msg("default_internet_con = %d", default_internet_con);
869         }
870
871         return in_param;
872 }
873
874 static gboolean __init_global_apns_from_xml(const char *file_path)
875 {
876         xmlNode *cur_node = NULL;
877         xmlNodePtr cur, root_node;
878         void *xml_doc = NULL, *xml_root_node = NULL;
879         char *version = NULL;
880         gboolean ret = FALSE;
881
882         /* remove pdp_profile table first. */
883         __reset_database();
884
885         __load_xml_file(file_path, "apns", &xml_doc, &xml_root_node);
886         if (!xml_root_node) {
887                 msg("[APNS CONF] Load error - Root node is NULL.");
888                 goto EXIT;
889         }
890         root_node = (xmlNodePtr)xml_root_node;
891         version = (char *)xmlGetProp(root_node, (const unsigned char *)"version");
892         if (version)
893                 msg("apns-conf.xml <apns version=\"%s\">", version);
894         cur = root_node->xmlChildrenNode;
895         /* Compare property */
896         for (cur_node = cur; cur_node; cur_node = cur_node->next) {
897                 if (cur_node->type == XML_ELEMENT_NODE) {
898                         GHashTable *in_param = NULL;
899                         gchar *svc_type = NULL;
900                         gboolean rv = FALSE;
901
902                         in_param = __construct_profile_tuples(cur_node);
903                         rv = __insert_apns_to_database(in_param);
904                         if (rv == FALSE)
905                                 continue;
906
907                         /* duplicate profiles for the same APNs */
908                         svc_type = (char *)xmlGetProp(cur_node, (const unsigned char *)"type");
909                         if (NULL != g_strrstr(svc_type, "default")) {
910                                 if (NULL != g_strrstr(svc_type, "mms")) {
911                                         /* type="default, supl, mms" */
912                                         __duplicate_profile_by_type(in_param, cur_node, CONTEXT_ROLE_MMS);
913                                         if (NULL != g_strrstr(svc_type, "dun")) {
914                                                 /* type="default, supl, mms, dun" */
915                                                 __duplicate_profile_by_type(in_param, cur_node, CONTEXT_ROLE_TETHERING);
916                                         }
917                                 } else if (NULL != g_strrstr(svc_type, "dun")) {
918                                         /* type="default, supl, dun" */
919                                         __duplicate_profile_by_type(in_param, cur_node, CONTEXT_ROLE_TETHERING);
920                                 }
921                         }
922                         g_hash_table_destroy(in_param);
923                 }
924         }
925 EXIT:
926         __unload_xml_file(&xml_doc, &xml_root_node);
927         return ret;
928 }
929
930
931 int main(int arg, char **argv)
932 {
933         int rv;
934         gchar *command = NULL;
935         __init_global_apns_from_xml("/usr/share/ps-plugin/apns-conf.xml");
936         rv = __system_command("/bin/mkdir /opt/usr/share/telephony");
937         msg("system command sent, rv(%d)", rv);
938         /* remove exist sql */
939         rv = __system_command("/bin/rm /opt/usr/share/telephony/dnet_db_init.sql");
940         msg("system command sent, rv(%d)", rv);
941         /* Dump pdp_profile to sql */
942         command = g_strdup_printf("/usr/bin/sqlite3 %s .dump | grep \"INSERT INTO \\\"pdp_profile\\\"\" > /opt/usr/share/telephony/dnet_db_init.sql", DATABASE_PATH);
943         rv = __system_command(command);
944         msg("system command sent, rv(%d)", rv);
945         g_free(command);
946         return 0;
947 }
948