bc0f5247179ce4cc0bfb78c29805c0981199be87
[platform/adaptation/emulator/vmodem-daemon-emulator.git] / vmodem / db / db_ss.c
1 /*
2  *  telephony-emulator
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * Sooyoung Ha <yoosah.ha@samsung.com>
8  * Sungmin Ha <sungmin82.ha@samsung.com>
9  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  * 
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the
13  * Free Software Foundation; either version 2.1 of the License, or (at your option)
14  * any later version.
15  * 
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this library; if not, write to the Free Software Foundation, Inc., 51
23  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  * 
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include <glib.h>
36 #include <glib/gstdio.h>
37
38 #include "vgsm_call.h"
39 #include "vgsm_ss.h"
40 #include "db_ss.h"
41 #include "logmsg.h"
42 #include "fileio.h"
43 //#include "state.h"
44 #include "at_func.h"
45
46 #define ALP_NOTIFY_DATABASE_DIR  "./db"
47 #define ALP_NOTIFY_DATABASE_FILE "ss.db"
48 #define ALP_NOTIFY_DATABASE_MODE (S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG)
49
50 // ----------------------------------------------------------------------------
51 // sqlite3 specific code
52 // ----------------------------------------------------------------------------
53
54 #include <sqlite3.h>
55
56 //090225
57 static int cw_count=0;
58
59 unsigned int    g_cw_entry_count;
60 unsigned int    g_cf_entry_count;
61 unsigned int    g_cb_entry_count;
62
63 #ifndef TRUE
64 #define TRUE    1
65 #endif /* TRUE */
66
67 #ifndef FALSE
68 #define FALSE   0
69 #endif /* FALSE */
70
71 call_waiting_entry_t            g_cw_entry[20];
72 call_barring_entry_t            g_cb_entry[20];
73 call_forwarding_entry_t         g_cf_entry[20];
74 int                             g_mo_voice_is_barred = FALSE;
75 int                             g_mt_voice_is_barred = FALSE;
76 int                             g_mo_video_is_barred = FALSE;
77 int                             g_mt_video_is_barred = FALSE;
78 int                             g_cf_is_activated = FALSE;
79 char* g_cb_pwd="1111";
80 static int g_cb_pwd_fail_count = 0;
81
82 // end
83
84 // callback to handle each row from "SELECT ALL * FROM ss"
85 // where each row is a persistent registration
86 static int vgsm_ss_sqlite_restore_callback(void * ref, int ncol, char ** cols, char ** name)
87 {
88         ss_cmd_e_type * type = (ss_cmd_e_type*)ref;
89
90         log_msg(MSGL_VGSM_INFO,"ncol : %d \n", ncol);
91         switch(*type)
92         {
93                 case SS_CMD_CF:
94                         {
95                                 call_forwarding_entry_t         entry;
96                                 memset(entry.number, 0, sizeof(entry.number));
97
98                                 log_msg(MSGL_VGSM_INFO,"class = %d, type = %d, number = %s(%d), reply time = %d, ss_mode = %d \n", atoi(*cols), atoi(*(cols+1)), *(cols+2), strlen(*(cols+2)), atoi(*(cols+3)), atoi(*(cols+4)));
99                                 entry.tel_class = atoi(*cols);
100                                 entry.type = atoi(*(cols+1));
101                                 memcpy(&entry.number, *(cols+2), strlen(*(cols+2)));
102                                 entry.replyTime = atoi(*(cols+3));
103                                 entry.ss_mode = atoi(*(cols+4));
104                                 g_cf_entry_count++;
105
106                                 set_call_forwarding_entry(&entry, g_cf_entry_count);
107                         }
108                         break;
109                 case SS_CMD_CB:
110                         {
111                                 call_barring_entry_t    entry;
112
113                                 log_msg(MSGL_VGSM_INFO,"class = %d, type = %d, ss_mode = %d \n", atoi(*cols), atoi(*(cols+1)), atoi(*(cols+2)));
114                                 entry.tel_class = atoi(*cols);
115                                 entry.type = atoi(*(cols+1));
116                                 entry.ss_mode = atoi(*(cols+2));
117
118                                 g_cb_entry_count++;
119                                 set_call_barring_entry(&entry, g_cb_entry_count);
120                                 
121                                 if(entry.tel_class == AT_CALL_SS_CLASS_VIDEO)   // video call
122                                 {
123                                         if(entry.type == SS_CB_TYPE_AB)
124                                         {
125                                                 set_outgoing_video_call_barring_state(entry.ss_mode);
126                                                 set_incoming_video_call_barring_state(entry.ss_mode);
127                                         }
128                                         else if(entry.type == SS_CB_TYPE_BAOC)
129                                         {
130                                                 set_outgoing_video_call_barring_state(entry.ss_mode);
131                                         }
132                                         else if(entry.type == SS_CB_TYPE_BAIC)
133                                         {
134                                                 set_incoming_video_call_barring_state(entry.ss_mode);
135                                         }
136                                 }
137                                 else            // voice call
138                                 {
139                                         if(entry.type == SS_CB_TYPE_AB)
140                                         {
141                                                 set_outgoing_voice_call_barring_state(entry.ss_mode);
142                                                 set_incoming_voice_call_barring_state(entry.ss_mode);
143                                         }
144                                         else if(entry.type == SS_CB_TYPE_BAOC)
145                                         {
146                                                 set_outgoing_voice_call_barring_state(entry.ss_mode);
147                                         }
148                                         else if(entry.type == SS_CB_TYPE_BAIC)
149                                         {
150                                                 set_incoming_voice_call_barring_state(entry.ss_mode);
151                                         }
152                                 }
153                         }
154                         break;
155                 case SS_CMD_CW:
156                         {
157                                 call_waiting_entry_t    entry;
158                                 memset(entry.number, 0, sizeof(entry.number));
159
160                                 entry.tel_class = atoi(*cols);
161                                 entry.ss_mode = atoi(*(cols+1));
162                                 memcpy(&entry.number, *(cols+2), strlen(*(cols+2)));
163                                 log_msg(MSGL_VGSM_INFO,"class = %d, ss_mode = %d\n", entry.tel_class, entry.ss_mode);
164                                 g_cw_entry_count++;
165
166                                 set_call_waiting_entry(&entry, g_cw_entry_count);
167                         }
168                         break;
169         }
170
171         // should always return zero from the callback
172         return 0;
173
174 }
175
176 // initialize the database
177 // create the ss table
178 static int vgsm_ss_sqlite_init(void)
179 {
180         sqlite3 * db = 0;
181         int err;
182         char * mesg;
183
184
185         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init\n");
186
187         char directory[256];
188         char dbname[256];
189
190         //char *dbname = ALP_NOTIFY_DATABASE_DIR "/" ALP_NOTIFY_DATABASE_FILE;
191         //char *directory = ALP_NOTIFY_DATABASE_DIR;
192         char *bin_path = get_bin_path();
193         sprintf(directory,"%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR);
194         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
195
196         if (bin_path)
197                 g_free(bin_path);
198
199         /* make sure database directory exists */
200         if (!g_file_test (directory, G_FILE_TEST_IS_DIR)) {
201
202                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: directory %s does not exit or is not directory\n", directory);
203
204                 /* check if exists and if so, attempt to remove non-directory */
205                 if (g_file_test (directory, G_FILE_TEST_EXISTS)) {
206                         if (g_remove (directory) < 0) {
207
208                                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: "
209                                                 "unable to remove %s: %d %s\n",
210                                                 directory, errno, strerror(errno));
211
212                                 err = errno;
213                                 goto Done;
214                         }
215                 }
216
217                 /* create directory */
218                 if (g_mkdir_with_parents (directory, ALP_NOTIFY_DATABASE_MODE) < 0) {
219                         /* unable to create directory */
220
221                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: "
222                                         "unable to create %s: %d %s\n",
223                                         directory, errno, strerror(errno));
224
225                         err = errno;
226                         goto Done;
227                 }
228         }
229
230         // open the database
231         err = sqlite3_open(dbname, &db);
232         if (err != SQLITE_OK) {
233                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: sqlite3_open err=%d\n",err);
234
235                 goto Done;
236         }
237
238         // create the call forwarding  table
239         err = sqlite3_exec(db,"CREATE TABLE forwarding ( class INT , type INT, number TEXT, replytime  INT, ss_mode INT)", 0, 0, &mesg);
240         if (err != SQLITE_OK) {
241                 //log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init(forwarding): sqlite3_exec err=%d\n",err);
242                 sqlite3_exec(db,"DELETE FROM forwarding",0,0,&mesg);
243                 if (mesg) {
244                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: sqlite3_exec: %s\n",mesg);
245                         sqlite3_free(mesg);
246                 }
247                 //goto Done; //Á¸Àç ÇÑ´Ù¸é, ±× ÀÌÈÄ tableÀº ¸ø¸¸µç´Ù. ÀÌ ºÎºÐ »èÁ¦°ËÅä.
248         }
249
250         // create the call barring  table
251         err = sqlite3_exec(db,"CREATE TABLE barring ( class INT , type INT, ss_mode INT)", 0, 0, &mesg);
252         if (err != SQLITE_OK) {
253                 //log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init(barring): sqlite3_exec err=%d\n",err);
254                 sqlite3_exec(db,"DELETE FROM barring",0,0,&mesg);
255                 if (mesg) {
256                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: sqlite3_exec: %s\n",mesg);
257                         sqlite3_free(mesg);
258                 }
259                 //goto Done;
260         }
261
262         // create the call waiting  table
263         err = sqlite3_exec(db,"CREATE TABLE waiting ( class INT , ss_mode INT, number TEXT)", 0, 0, &mesg);
264         if (err != SQLITE_OK) {
265                 //log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init(waiting): sqlite3_exec err=%d\n",err);
266                 sqlite3_exec(db,"DELETE FROM waiting",0,0,&mesg);
267                 if (mesg) {
268                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_init: sqlite3_exec: %s\n",mesg);
269                         sqlite3_free(mesg);
270                 }
271                 //goto Done;
272         }
273
274 Done:
275         // close the database
276         if (db) sqlite3_close(db);
277
278         // return status
279         return err == SQLITE_OK ? TRUE: err;
280 }
281
282 static int count_callback(void *some_entry, int ncol, char **cols, char **azColName)
283 {
284         unsigned char * count;
285
286         count = (unsigned char *)(some_entry);
287
288         *count += 1;
289         //090225
290         cw_count = *count;
291
292         return 0;
293 }
294
295 int  vgsm_ss_sqlite_cw_check_exist (int tel_class)
296 {
297         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cw_check_exist\n");
298
299         char str[512];
300         unsigned char counter = 0;
301         sqlite3 * db = 0;
302         int err;
303         char * mesg;
304
305         char dbname[256];
306
307         char *bin_path = get_bin_path();
308         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
309
310         if (bin_path)
311                 g_free(bin_path);
312
313         err = sqlite3_open(dbname, &db);
314         if (err != SQLITE_OK) {
315
316                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cw_check_exist: sqlite3_open err=%d\n", err);
317
318                 goto Done;
319         }
320
321         sprintf(str, "SELECT * FROM waiting WHERE class = '%d'", tel_class);
322
323         log_msg(MSGL_VGSM_INFO,"count sql = <%s> \n", str);
324
325         err = sqlite3_exec(db, str, count_callback, &counter, &mesg);
326         if (err != SQLITE_OK) {
327
328                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cw_check_exist: sqlite3_exec err=%d\n", err);
329
330                 if (mesg) {
331
332                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cw_check_exist: sqlite3_exec: %s\n", mesg);
333
334                         sqlite3_free(mesg);
335                 }
336                 goto Done;
337         }
338
339         if(counter > 0)
340                 return TRUE;
341
342 Done:
343         // close the database
344         if (db) sqlite3_close(db);
345
346         return FALSE;
347 }
348
349 static int  vgsm_ss_sqlite_cb_check_exist (int tel_class, int type)
350 {
351         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cb_check_exist\n");
352
353         char str[512];
354         unsigned char counter = 0;
355         sqlite3 * db = 0;
356         int err;
357         char * mesg;
358
359
360         char dbname[256];
361
362         char *bin_path = get_bin_path();
363         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
364
365         if (bin_path)
366                 g_free(bin_path);
367
368         err = sqlite3_open(dbname, &db);
369         if (err != SQLITE_OK) {
370
371                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cb_check_exist: sqlite3_open err=%d\n", err);
372
373                 goto Done;
374         }
375
376         sprintf(str, "SELECT * FROM barring WHERE (class = '%d' AND type = '%d')", tel_class, type);
377
378         log_msg(MSGL_VGSM_INFO,"count sql = <%s> \n", str);
379
380         err = sqlite3_exec(db, str, count_callback, &counter, &mesg);
381         if (err != SQLITE_OK) {
382
383                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cb_check_exist: sqlite3_exec err=%d\n", err);
384
385                 if (mesg) {
386
387                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cb_check_exist: sqlite3_exec: %s\n", mesg);
388
389                         sqlite3_free(mesg);
390                 }
391                 goto Done;
392         }
393
394         if(counter > 0)
395                 return TRUE;
396
397 Done:
398         // close the database
399         if (db) sqlite3_close(db);
400
401         return FALSE;
402 }
403
404 static int  vgsm_ss_sqlite_cf_check_exist (int tel_class, int type)
405 {
406         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cf_check_exist\n");
407
408         char str[512];
409         unsigned char counter = 0;
410         sqlite3 * db = 0;
411         int err;
412         char * mesg;
413
414         char dbname[256];
415
416         char *bin_path = get_bin_path();
417         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
418
419         if (bin_path)
420                 g_free(bin_path);
421
422         log_msg(MSGL_VGSM_INFO,"check 1 in vgsm_ss_sqlite_cf_check_exist\n");
423         err = sqlite3_open(dbname, &db);
424         log_msg(MSGL_VGSM_INFO,"check 2 in vgsm_ss_sqlite_cf_check_exist\n");
425         if (err != SQLITE_OK) {
426
427                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cf_check_exist: sqlite3_open err=%d\n", err);
428
429                 goto Done;
430         }
431
432         sprintf(str, "SELECT * FROM forwarding WHERE (class = '%d' AND type = '%d')", tel_class, type);
433
434         log_msg(MSGL_VGSM_INFO,"count sql = <%s> \n", str);
435
436         err = sqlite3_exec(db, str, count_callback, &counter, &mesg);
437         if (err != SQLITE_OK) {
438
439                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cf_check_exist: sqlite3_exec err=%d\n", err);
440
441                 if (mesg) {
442
443                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_cf_check_exist: sqlite3_exec: %s\n", mesg);
444
445                         sqlite3_free(mesg);
446                 }
447                 goto Done;
448         }
449
450         if(counter > 0)
451                 return TRUE;
452
453 Done:
454         // close the database
455         if (db) sqlite3_close(db);
456
457         return FALSE;
458 }
459
460 // add a new registration to the database
461 // insert a row into the database which contains the registration information
462 static int vgsm_ss_sqlite_add(char * sqlString)
463 {
464         sqlite3 * db = 0;
465         int err;
466         char * mesg;
467
468         char str[512];
469         //      char str[1000];
470         //
471 #if 0                                                   // - this is for checking sqlString
472         int fd;
473
474         if(fd = open("/root/sql_tmp",O_WRONLY|O_CREAT) == -1)
475         {
476                 log_msg(MSGL_VGSM_INFO,"err=%d\n","Error opening the file");
477                 log_msg(MSGL_VGSM_INFO,"err=%d\n","Error opening the file");
478                 log_msg(MSGL_VGSM_INFO,"err=%d\n","Error opening the file");
479         }
480         else
481         {
482                 write(fd, sqlString, strlen(*sqlString));
483         }
484 #endif
485
486 #if 1
487         sprintf(str, "<ADD>SELECT * FROM waiting WHERE class = '%s'", sqlString);
488         log_msg(MSGL_VGSM_INFO,"count sql = <%s> \n", str);
489 #endif
490
491
492         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_add\n");
493
494         char dbname[256];
495
496         char *bin_path = get_bin_path();
497         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
498
499         if (bin_path)
500                 g_free(bin_path);
501
502         // open the database
503         err = sqlite3_open(dbname,&db);
504         if (err != SQLITE_OK) {
505
506                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_add: sqlite3_open err=%d\n",err);
507
508                 goto Done;
509         }
510
511         // create the sql string
512         //      sprintf(str,"INSERT INTO ss VALUES (%d,%d,%d,'%s')",id, PLMN_status, number_PLMN, ACT);
513
514         if(sqlString == NULL)
515                 goto Done;
516
517         log_msg(MSGL_VGSM_INFO," SQL statement : %s\n", sqlString);
518
519         // add a row
520         err = sqlite3_exec(db, sqlString, 0, 0, &mesg);
521         if (err != SQLITE_OK) {
522
523                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_add: sqlite3_exec err=%d\n",err);
524
525                 if (mesg) {
526
527                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_add: sqlite3_exec: %s\n",mesg);
528
529                         sqlite3_free(mesg);
530                 }
531                 goto Done;
532         }
533 Done:
534         // close the database
535         if (db) sqlite3_close(db);
536
537         // return status
538         //return err == SQLITE_OK ? TRUE: err;
539         return err;
540 }
541
542 // restore all previously save registrations
543 // select all rows and process each one in the callback
544 static int vgsm_ss_sqlite_restore(ss_cmd_e_type type)
545 {
546         char str[128];
547         sqlite3 * db = 0;
548         int err;
549         char * mesg;
550
551         char dbname[256];
552
553         char *bin_path = get_bin_path();
554         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
555
556         if (bin_path)
557                 g_free(bin_path);
558
559         // open the database
560         err = sqlite3_open(dbname, &db);
561         if (err != SQLITE_OK) {
562
563                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_restore: sqlite3_open err=%d\n", err);
564
565                 goto Done;
566         }
567
568         switch(type)
569         {
570                 case SS_CMD_CF:
571                         sprintf(str, "SELECT * FROM %s", "forwarding");
572                         break;
573                 case SS_CMD_CB:
574                         sprintf(str, "SELECT * FROM %s", "barring");
575                         break;
576                 case SS_CMD_CW:
577                         sprintf(str, "SELECT * FROM %s", "waiting");
578                         break;
579         }
580
581         g_cw_entry_count = 0;
582         g_cf_entry_count = 0;
583         g_cb_entry_count = 0;
584
585         log_msg(MSGL_VGSM_INFO, "%s\n", str);
586         err = sqlite3_exec(db,str, vgsm_ss_sqlite_restore_callback, &type, &mesg);
587         if(err)
588         {
589         }
590
591         log_msg(MSGL_VGSM_INFO,"<<<<>>>>>cw(%d) cf(%d) cb(%d)\n", g_cw_entry_count, g_cf_entry_count, g_cb_entry_count);
592         if(type == SS_CMD_CW && g_cw_entry_count == 0)
593                 set_call_waiting_entry(NULL, 0);
594         if(type ==  SS_CMD_CF && g_cf_entry_count == 0)
595                 set_call_forwarding_entry(NULL, 0);
596         if(type == SS_CMD_CB && g_cb_entry_count == 0)
597                 set_call_barring_entry(NULL, 0);
598
599         if (err != SQLITE_OK) {
600                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_restore: sqlite3_exec err=%d\n", err);
601
602                 if (mesg) {
603                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_restore: sqlite3_exec: %s\n", mesg);
604
605                         sqlite3_free(mesg);
606                 }
607                 goto Done;
608         }
609
610 Done:
611         // close the database
612         if (db) sqlite3_close(db);
613
614         // return status
615         return err;
616 }
617
618 // remove a registration from the database
619 // delete a row from the database which contains the registration information
620 static int vgsm_ss_sqlite_remove(ss_cmd_e_type type, int tel_class, int ss_type)
621 {
622         sqlite3 * db = 0;
623         int err;
624         char * mesg;
625         char str[1000];
626
627
628         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_remove\n");
629
630         char dbname[256];
631
632         char *bin_path = get_bin_path();
633         sprintf(dbname,"%s/%s/%s", bin_path, ALP_NOTIFY_DATABASE_DIR, ALP_NOTIFY_DATABASE_FILE);
634
635         if (bin_path)
636                 g_free(bin_path);
637
638         // open the database
639         err = sqlite3_open(dbname,&db);
640         if (err != SQLITE_OK) {
641
642                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_remove: sqlite3_open err=%d\n",err);
643
644                 goto Done;
645         }
646
647         // create the sql string
648         switch(type)
649         {
650                 case SS_CMD_CF:
651                         //090325
652                         //sprintf(str,"DELETE FROM forwarding WHERE (class = '%d' AND type = '%d')", tel_class, ss_type);
653                         if(ss_type == SS_CF_TYPE_CF_ALL) // SS_CF_TYPE_CF_ALL
654                                 sprintf(str,"DELETE FROM forwarding");                          // for defect X200001252, set Cancel all in Simulator
655                         else if(tel_class == -1 || tel_class == 0x10) // All teleservices
656                                 sprintf(str,"DELETE FROM forwarding WHERE type = '%d'", ss_type);
657                         else
658                                 //      sprintf(str,"DELETE FROM forwarding WHERE type = '%d'", ss_type);
659                                 sprintf(str,"DELETE FROM forwarding WHERE (class = '%d' AND type = '%d')", tel_class, ss_type);
660
661                         break;
662                 case SS_CMD_CB:
663                         //090325
664                         //sprintf(str,"DELETE FROM barring WHERE (class = '%d' AND type = '%d')", tel_class, ss_type);
665                         sprintf(str,"DELETE FROM barring WHERE type = '%d'", ss_type);
666                         break;
667                 case SS_CMD_CW:
668                         sprintf(str,"DELETE FROM waiting WHERE (class = '%d')", tel_class);
669                         break;
670         }
671
672         log_msg(MSGL_VGSM_INFO,"delete sql = <%s> \n", str);
673
674         // remove the row
675         err = sqlite3_exec(db, str, 0, 0, &mesg);
676         if (err != SQLITE_OK) {
677
678                 log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_remove: sqlite3_exec err=%d\n",err);
679
680                 if (mesg) {
681
682                         log_msg(MSGL_VGSM_INFO,"vgsm_ss_sqlite_remove: sqlite3_exec: %s\n",mesg);
683
684                         sqlite3_free(mesg);
685                 }
686                 goto Done;
687         }
688
689         return vgsm_ss_sqlite_restore(type);
690
691 Done:
692         // close the database
693         if (db) sqlite3_close(db);
694
695         // return status
696         return err;
697 }
698
699
700 static int vgsm_ss_sqlite_make_sql(ss_cmd_e_type type, void * entry)
701 {
702         char     str[1024];
703         int err = 0;
704
705         switch(type)
706         {
707                 case SS_CMD_CF:
708                         {
709                                 call_forwarding_entry_t * cf_entry = (call_forwarding_entry_t*)entry;
710                                 log_msg(MSGL_VGSM_INFO, "ss_mode = %d\n", cf_entry->ss_mode);
711                                 if(vgsm_ss_sqlite_cf_check_exist(cf_entry->tel_class, cf_entry->type))
712                                 {
713                                         if(cf_entry->ss_mode == SS_MODE_DEREG) // GSM_SS_MODE_DEREG
714                                         {
715                                                 if(cf_entry->type == SS_CF_TYPE_CFU)
716                                                 {
717                                                         log_msg(MSGL_VGSM_INFO, "g_cf_is_activated = FALSE\n");
718                                                         g_cf_is_activated = FALSE;
719                                                 }
720
721                                                 return vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, cf_entry->type);
722                                         }
723                                         else if(cf_entry->ss_mode == SS_MODE_REG && cf_entry->type == SS_CF_TYPE_CFU)
724                                         {
725                                                 log_msg(MSGL_VGSM_INFO, "g_cf_is_activated = TRUE\n");
726                                                 g_cf_is_activated = TRUE;
727                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFB);
728                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRy);
729                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRc);
730                                                 
731                                                 memset(str, '\0', sizeof(str));
732                                                 sprintf(str, "UPDATE forwarding SET number = '%s', replytime = '%d', ss_mode  = '%d' "
733                                                                 "WHERE (class = '%d' AND type = '%d')", cf_entry->number, cf_entry->replyTime, cf_entry->ss_mode, cf_entry->tel_class, cf_entry->type);
734                                                 log_msg(MSGL_VGSM_INFO, "\n");
735                                         }
736                                         else
737                                         {
738                                                 if(g_cf_is_activated == TRUE)
739                                                 {
740                                                         vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFB);
741                                                         vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRy);
742                                                         vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRc);
743
744                                                         return vgsm_ss_sqlite_restore(type);
745                                                 }
746
747                                                 memset(str, '\0', sizeof(str));
748                                                 sprintf(str, "UPDATE forwarding SET number = '%s', replytime = '%d', ss_mode  = '%d' "
749                                                                 "WHERE (class = '%d' AND type = '%d')", cf_entry->number, cf_entry->replyTime, cf_entry->ss_mode, cf_entry->tel_class, cf_entry->type);
750                                                 log_msg(MSGL_VGSM_INFO, "\n");
751                                         }
752                                 }
753                                 else if(cf_entry->ss_mode == SS_MODE_DEREG)
754                                 {
755                                         if(cf_entry->type == SS_CF_TYPE_CFU)
756                                         {
757                                                 log_msg(MSGL_VGSM_INFO, "g_cf_is_activated = FALSE\n");
758                                                 g_cf_is_activated = FALSE;
759                                         }
760
761                                         log_msg(MSGL_VGSM_INFO,"###%d, %d, '%s',%d, %d\n",cf_entry->tel_class, cf_entry->type, cf_entry->number, cf_entry->replyTime, cf_entry->ss_mode);
762                                         vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, cf_entry->type);
763                                 }
764                                 else
765                                 {
766                                         if(cf_entry->ss_mode == SS_MODE_REG && cf_entry->type == SS_CF_TYPE_CFU)
767                                         {
768                                                 log_msg(MSGL_VGSM_INFO, "g_cf_is_activated = TRUE\n");
769                                                 g_cf_is_activated = TRUE;
770                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFB);
771                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRy);
772                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRc);
773                                         }
774                                         else if(g_cf_is_activated == TRUE)
775                                         {
776                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFB);
777                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRy);
778                                                 vgsm_ss_sqlite_remove(SS_CMD_CF, cf_entry->tel_class, SS_CF_TYPE_CFNRc);
779
780                                                 return vgsm_ss_sqlite_restore(type);
781                                         }
782                                         
783                                         memset(str, '\0', sizeof(str));
784                                         sprintf(str,"INSERT INTO forwarding VALUES (%d, %d, '%s',%d, %d)", cf_entry->tel_class, cf_entry->type, cf_entry->number, cf_entry->replyTime, cf_entry->ss_mode);
785                                         log_msg(MSGL_VGSM_INFO,"####%d, %d, '%s',%d, %d\n",cf_entry->tel_class, cf_entry->type, cf_entry->number, cf_entry->replyTime, cf_entry->ss_mode);
786                                 }
787                         }
788                         break;
789                 case SS_CMD_CB:
790                         {
791                                 call_barring_entry_t * cb_entry = (call_barring_entry_t*)entry;
792                                 log_msg(MSGL_VGSM_INFO,"tel_class : %x, ssmode : %x, sstype : %x\n",cb_entry->tel_class, cb_entry->ss_mode, cb_entry->type);
793                                 if(vgsm_ss_sqlite_cb_check_exist(cb_entry->tel_class, cb_entry->type))
794                                 {
795                                         if(cb_entry->ss_mode == SS_MODE_DEREG) // GSM_SS_MODE_DEREG
796                                                 return vgsm_ss_sqlite_remove(SS_CMD_CB, cb_entry->tel_class, cb_entry->type);
797                                         else
798                                         {
799                                                 if(cb_entry->type == SS_CB_TYPE_BAOC && cb_entry->ss_mode == SS_MODE_ACT)
800                                                 {
801                                                         memset(str, '\0', sizeof(str));
802                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, SS_CB_TYPE_BAOC);
803                                                         if(vgsm_ss_sqlite_add((char*)&str))
804                                                                 return err;
805                                                         memset(str, '\0', sizeof(str));
806                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, SS_CB_TYPE_BOIC);
807                                                         if(vgsm_ss_sqlite_add((char*)&str))
808                                                                 return err;
809                                                         memset(str, '\0', sizeof(str));
810                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, SS_CB_TYPE_BOIC_NOT_HC);
811                                                 }
812                                                 else if(cb_entry->type == SS_CB_TYPE_BAIC && cb_entry->ss_mode == SS_MODE_ACT)
813                                                 {
814                                                         memset(str, '\0', sizeof(str));
815                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, SS_CB_TYPE_BAIC);
816                                                         if(vgsm_ss_sqlite_add((char*)&str))
817                                                                 return err;
818                                                         memset(str, '\0', sizeof(str));
819                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, SS_CB_TYPE_BIC_ROAM);
820                                                 }
821                                                 else
822                                                 {       
823                                                         memset(str, '\0', sizeof(str));
824                                                         sprintf(str, "UPDATE barring SET ss_mode  = '%d' WHERE (class = '%d' AND type = '%d')", cb_entry->ss_mode, cb_entry->tel_class, cb_entry->type);
825                                                 }
826                                         }
827                                 }
828                                 //090327        // this is point of preventing deregisteration data in cb
829                                 else if(cb_entry->ss_mode != SS_MODE_DEACT)     // GSM_SS_MODE_DEACTIVATE
830                                 {
831                                         if(cb_entry->type == SS_CB_TYPE_BAOC && cb_entry->ss_mode == SS_MODE_ACT)
832                                         {
833                                                 memset(str, '\0', sizeof(str));
834                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, SS_CB_TYPE_BAOC, cb_entry->ss_mode);
835                                                 log_msg(MSGL_VGSM_INFO, "%s\n", str);
836                                                 if(vgsm_ss_sqlite_add((char*)&str))
837                                                         return err;
838                                                 memset(str, '\0', sizeof(str));
839                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, SS_CB_TYPE_BOIC, cb_entry->ss_mode);        
840                                                 log_msg(MSGL_VGSM_INFO, "%s\n", str);
841                                                 if(vgsm_ss_sqlite_add((char*)&str))
842                                                         return err;
843                                                 memset(str, '\0', sizeof(str));
844                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, SS_CB_TYPE_BOIC_NOT_HC, cb_entry->ss_mode);
845                                                 log_msg(MSGL_VGSM_INFO, "%s\n", str);
846                                         }
847                                         else if(cb_entry->type == SS_CB_TYPE_BAIC && cb_entry->ss_mode == SS_MODE_ACT)
848                                         {
849                                                 memset(str, '\0', sizeof(str));
850                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, SS_CB_TYPE_BAIC, cb_entry->ss_mode);
851                                                 if(vgsm_ss_sqlite_add((char*)&str))
852                                                         return err;
853                                                 memset(str, '\0', sizeof(str));
854                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, SS_CB_TYPE_BIC_ROAM, cb_entry->ss_mode);
855                                         }
856                                         else
857                                         {
858                                                 memset(str, '\0', sizeof(str));
859                                                 sprintf(str,"INSERT INTO barring VALUES (%d, %d, %d)", cb_entry->tel_class, cb_entry->type, cb_entry->ss_mode);
860                                         }
861                                 }
862                         }
863                         break;
864                 case SS_CMD_CW:
865                         {
866                                 call_waiting_entry_t * cw_entry = (call_waiting_entry_t*)entry;
867                                 /*******090225*****************************************************************************************************/
868                                 if(cw_entry->tel_class == -1 || cw_entry->tel_class == 0x10)            // for defect X200001045, inconsistency problem
869                                 {
870                                         if(cw_entry->ss_mode == SS_MODE_DEREG || cw_entry->ss_mode == SS_MODE_DEACT) // GSM_SS_MODE_DEREG or GSM_SS_MODE_DEACTIVATE
871                                                 //                                              return vgsm_ss_sqlite_remove(SS_CMD_CW, cw_entry->tel_class, 0);
872                                         {
873                                                 memset(str, '\0', sizeof(str));
874                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 1);
875                                                 if(vgsm_ss_sqlite_add((char*)&str))
876                                                         return err;
877                                                 memset(str, '\0', sizeof(str));
878                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 4);
879                                                 if(vgsm_ss_sqlite_add((char*)&str))
880                                                         return err;
881                                                 memset(str, '\0', sizeof(str));
882                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 8);
883                                                 if(vgsm_ss_sqlite_add((char*)&str))
884                                                         return err;
885                                                 memset(str, '\0', sizeof(str));
886                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 32);
887                                         }
888                                         else
889                                         {
890                                                 memset(str, '\0', sizeof(str));
891                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 1);
892                                                 if(vgsm_ss_sqlite_add((char*)&str))
893                                                         return err;
894                                                 memset(str, '\0', sizeof(str));
895                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 4);
896                                                 if(vgsm_ss_sqlite_add((char*)&str))
897                                                         return err;
898                                                 memset(str, '\0', sizeof(str));
899                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 8);
900                                                 if(vgsm_ss_sqlite_add((char*)&str))
901                                                         return err;
902                                                 memset(str, '\0', sizeof(str));
903                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 32);
904                                         }
905                                 }
906                                 else
907                                 {
908                                         if(vgsm_ss_sqlite_cw_check_exist(cw_entry->tel_class))
909                                         {
910                                         /*      if(cw_entry->ss_mode == SS_MODE_DEREG || cw_entry->ss_mode == SS_MODE_DEACT) // GSM_SS_MODE_DEREG or GSM_SS_MODE_DEACT
911                                                         return  vgsm_ss_sqlite_remove(SS_CMD_CW, cw_entry->tel_class, 0);
912                                                 else*/
913                                                 memset(str, '\0', sizeof(str));
914                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 1);
915                                                 if(vgsm_ss_sqlite_add((char*)&str))
916                                                         return err;
917                                                 memset(str, '\0', sizeof(str));
918                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 4);
919                                                 if(vgsm_ss_sqlite_add((char*)&str))
920                                                         return err;
921                                                 memset(str, '\0', sizeof(str));
922                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 8);
923                                                 if(vgsm_ss_sqlite_add((char*)&str))
924                                                         return err;
925                                                 memset(str, '\0', sizeof(str));
926                                                 sprintf(str, "UPDATE waiting SET number = '%s', ss_mode  = '%d' WHERE class = '%d'", cw_entry->number, cw_entry->ss_mode, 32);
927                                         }
928                                         else
929                                         {
930                                                 memset(str, '\0', sizeof(str));
931                                                 sprintf(str,"INSERT INTO waiting VALUES (%d, %d, '%s')", cw_entry->tel_class, cw_entry->ss_mode, cw_entry->number);
932                                                 log_msg(MSGL_VGSM_INFO, "%s\n", str);
933                                         }
934                                 }
935                         }
936                         break;
937         }
938
939         if(vgsm_ss_sqlite_add((char*)&str))
940                 return err;
941
942         return vgsm_ss_sqlite_restore(type);
943
944 }
945
946 // ----------------------------------------------------------------------------
947 // end of sqlite3 specific code
948 // ----------------------------------------------------------------------------
949
950 // initialize the database
951 int vgsm_ss_database_init()
952 {
953         log_msg(MSGL_VGSM_INFO,"vgsm_ss_database_init\n");
954
955         // initialize the database
956         return vgsm_ss_sqlite_init();
957 }
958
959 // add a registration
960 int vgsm_ss_database_add(ss_cmd_e_type type, void * entry)
961 {
962         log_msg(MSGL_VGSM_INFO,"vgsm_ss_database_add\n");
963
964         return vgsm_ss_sqlite_make_sql(type, entry);
965 }
966
967 // remove a registration
968 int vgsm_ss_database_remove(ss_cmd_e_type type, int tel_class, int ss_type)
969 {
970
971         log_msg(MSGL_VGSM_INFO,"vgsm_ss_database_remove\n");
972
973         if(ss_type == SS_CF_TYPE_CFU)
974         {
975                 log_msg(MSGL_VGSM_INFO, "g_cf_is_activated = FALSE\n");
976                 g_cf_is_activated = FALSE;
977         }
978         // remove the registration from the database
979         return vgsm_ss_sqlite_remove(type, tel_class, ss_type);
980 }
981
982 // restore all previously save registrations
983 int vgsm_ss_database_restore(ss_cmd_e_type type)
984 {
985         log_msg(MSGL_VGSM_INFO,"vgsm_ss_database_restore : ss_cmd=%d \n", type);
986
987         return vgsm_ss_sqlite_restore(type);
988 }
989
990 /*========== voice =================*/
991
992 int get_outgoing_voice_call_barring_state(void)
993 {
994         log_msg(MSGL_VGSM_INFO,"%d\n", g_mo_voice_is_barred);
995         return g_mo_voice_is_barred;
996 }
997
998 int get_incoming_voice_call_barring_state(void)
999 {
1000         log_msg(MSGL_VGSM_INFO,"%d\n", g_mt_voice_is_barred);
1001         return g_mt_voice_is_barred;
1002 }
1003
1004 void set_outgoing_voice_call_barring_state(int state)
1005 {
1006         log_msg(MSGL_VGSM_INFO,"%d\n", state);
1007         g_mo_voice_is_barred = state == 0x03 ? TRUE : FALSE;
1008 }
1009
1010 void set_incoming_voice_call_barring_state(int state)
1011 {
1012         log_msg(MSGL_VGSM_INFO,"%d\n", state);
1013         g_mt_voice_is_barred = state == 0x03 ? TRUE : FALSE;
1014 }
1015
1016 /*========== video =================*/
1017
1018 int get_outgoing_video_call_barring_state(void)
1019 {
1020         log_msg(MSGL_VGSM_INFO,"%d\n", g_mo_video_is_barred);
1021         return g_mo_video_is_barred;
1022 }
1023
1024 int get_incoming_video_call_barring_state(void)
1025 {
1026         log_msg(MSGL_VGSM_INFO,"%d\n", g_mt_video_is_barred);
1027         return g_mt_video_is_barred;
1028 }
1029
1030 void set_outgoing_video_call_barring_state(int state)
1031 {
1032         log_msg(MSGL_VGSM_INFO,"%d\n", state);
1033         g_mo_video_is_barred = state == 0x03 ? TRUE : FALSE;
1034 }
1035
1036 void set_incoming_video_call_barring_state(int state)
1037 {
1038         log_msg(MSGL_VGSM_INFO,"%d\n", state);
1039         g_mt_video_is_barred = state == 0x03 ? TRUE : FALSE;
1040 }
1041
1042 void set_call_waiting_entry(call_waiting_entry_t * entry, int num)
1043 {
1044         if(num == 0)
1045                 memset(g_cw_entry, 0, sizeof(g_cw_entry));
1046         else
1047         {
1048                 g_cw_entry[num-1].tel_class = entry->tel_class;
1049                 g_cw_entry[num-1].ss_mode = entry->ss_mode;
1050                 g_cw_entry[0].count = num;
1051                 memset(&g_cw_entry[num-1].number, 0, MAX_GSM_DIALED_DIGITS_NUMBER);
1052                 memcpy(&g_cw_entry[num-1].number, &entry->number, strlen((char*)&entry->number));
1053         }
1054         log_msg(MSGL_VGSM_INFO,"[set_call_waiting_entry]--------class : %d, ss_mode : %d, num : %d\n", g_cw_entry[num-1].tel_class, g_cw_entry[num-1].ss_mode, num);
1055 }
1056
1057 call_waiting_entry_t * get_call_waiting_entry()
1058 {
1059         return (call_waiting_entry_t *)&g_cw_entry;
1060 }
1061
1062 call_waiting_entry_t g_cw_entry_tmp;
1063
1064 call_waiting_entry_t * find_call_waiting_entry(int tel_class)
1065 {
1066         int i, status=4;
1067         call_waiting_entry_t * entry ;
1068
1069         for(i = 0; i<g_cw_entry[0].count; i++)
1070         {
1071                 log_msg(MSGL_VGSM_INFO,"%d. [%s] class : %d, ss_mode : %d with matching telclass : %d\n"
1072                         , i, __FUNCTION__, g_cw_entry[i].tel_class, g_cw_entry[i].ss_mode, tel_class);
1073
1074                 if(g_cw_entry[i].tel_class == tel_class){
1075                         log_msg(MSGL_VGSM_INFO,"Found:  class : %d, ss_mode : %d\n"
1076                                 , g_cw_entry[i].tel_class, g_cw_entry[i].ss_mode);
1077
1078                         return &g_cw_entry[i];
1079                 }
1080         }
1081         
1082         log_msg(MSGL_VGSM_INFO,"Not Found:  Fake return class : %d, ss_mode : %d\n"
1083                 , tel_class, status);
1084
1085         entry = &g_cw_entry_tmp ;
1086
1087         memset(entry, 0, sizeof(call_waiting_entry_t));
1088         entry->tel_class = tel_class;
1089         entry->ss_mode = status;
1090         entry->count = 1;
1091         strcpy(entry->number, SUBSCRIBER_NUM);
1092         return entry;
1093 }
1094
1095 void set_call_barring_entry(call_barring_entry_t * entry, int num)
1096 {
1097         if(num == 0)
1098         {
1099                 memset(g_cb_entry, 0, sizeof(call_barring_entry_t)*20);
1100                 g_cb_entry[0].count = 0;
1101         }
1102         else
1103         {
1104                 g_cb_entry[num-1].tel_class = entry->tel_class;
1105                 g_cb_entry[num-1].type = entry->type;
1106                 g_cb_entry[num-1].ss_mode = entry->ss_mode;
1107                 g_cb_entry[0].count = num;
1108         }
1109         //      log_msg(MSGL_VGSM_INFO,"[set_call_barring_entry]--------class : %d, type : %d, ss_mode : %d, num : %d\n", g_cb_entry[num-1].tel_class, g_cb_entry[num-1].type, g_cb_entry[num-1].ss_mode, num);
1110 }
1111
1112 call_barring_entry_t * get_call_barring_entry()
1113 {
1114         return (call_barring_entry_t *)&g_cb_entry;
1115 }
1116
1117 //class¿Í type°ªÀº ´Ù½Ã ¸®ÅÏÇØÁÙ ÇÊ¿ä°¡ ¾ø´Ù. -> status¸¸ ¾Ë·ÁÁָ頵Ê
1118
1119 call_barring_entry_t g_cb_entry_tmp;
1120
1121 int send_call_barring_entry(call_barring_entry_t* entry)
1122 {
1123         unsigned char * data = 0;
1124         int data_len =4 ;
1125         int ss_status, ret;
1126
1127         log_msg(MSGL_VGSM_INFO,"entry->count = %d \n", entry->count);
1128         if(entry->ss_mode == SS_MODE_REG || entry->ss_mode == SS_MODE_ACT)
1129                 ss_status = AT_SS_STATUS_ACTIVE;
1130         else
1131                 ss_status = AT_SS_STATUS_NOT_ACTIVE;
1132         
1133         data_len =4;
1134         data = malloc(sizeof(unsigned char) * data_len);
1135         data[0] = entry->type;
1136         data[1] = 1;    // entry->count 
1137         data[2] = entry->tel_class;
1138         data[3] = ss_status;
1139
1140         log_msg(MSGL_VGSM_INFO,"data[0] : %x\ndata[1] : %x\ndata[2] : %x\ndata[3] : %x\nss_mode : %x\n", data[0], data[1], data[2], data[3], entry->ss_mode);
1141
1142         ret = oem_tx_ss_cb_resp(data, data_len);
1143         free(data);
1144         
1145         return 0;
1146 }
1147
1148 call_barring_entry_t *  find_call_barring_entry(int tel_class, int type)
1149 {
1150         int i, status = SS_MODE_DEACT, found = 0;
1151         call_barring_entry_t * entry = (call_barring_entry_t*)malloc(sizeof(call_barring_entry_t));
1152
1153         log_msg(MSGL_VGSM_INFO,"1. [find_call_barring_entry]--------telclass : %d, type : %d\n", tel_class, type );
1154         for(i = 0; i<g_cb_entry[0].count; i++)
1155         {
1156                 log_msg(MSGL_VGSM_INFO,"2. [find_call_barring_entry]--------class : %d, ss_mode : %d, type : %d\n", g_cb_entry[i].tel_class, g_cb_entry[i].ss_mode, g_cb_entry[i].type);
1157                 
1158                 if((tel_class == AT_CALL_SS_CLASS_ALL) ? (g_cb_entry[i].type == type) : (g_cb_entry[i].tel_class == tel_class) && (g_cb_entry[i].type == type))
1159                 {
1160                         log_msg(MSGL_VGSM_INFO,"entry: count %d \n", g_cb_entry[i].count);
1161
1162                         entry = &g_cb_entry_tmp;
1163
1164                         memset(entry, 0, sizeof(call_barring_entry_t));
1165                         entry->tel_class = g_cb_entry[i].tel_class;
1166                         //entry->tel_class = class;
1167                         entry->type = g_cb_entry[i].type;
1168                         if(entry->tel_class == AT_CALL_SS_CLASS_VIDEO)          // video call
1169                         {
1170                                 if(g_cb_entry[i].type == SS_CB_TYPE_BOIC || g_cb_entry[i].type == SS_CB_TYPE_BOIC_NOT_HC)
1171                                 {
1172                                         if(get_outgoing_video_call_barring_state())
1173                                                 entry->ss_mode = SS_MODE_ACT;
1174                                         else
1175                                                 entry->ss_mode = g_cb_entry[i].ss_mode;
1176                                 }
1177                                 else if(g_cb_entry[i].type == SS_CB_TYPE_BIC_ROAM)
1178                                 {
1179                                         if(get_incoming_video_call_barring_state())
1180                                                 entry->ss_mode = SS_MODE_ACT;
1181                                         else
1182                                                 entry->ss_mode = g_cb_entry[i].ss_mode;
1183                                 }
1184                                 else
1185                                         entry->ss_mode = g_cb_entry[i].ss_mode;
1186                         }
1187                         else                                    // voice call
1188                         {
1189                                 if(g_cb_entry[i].type == SS_CB_TYPE_BOIC || g_cb_entry[i].type == SS_CB_TYPE_BOIC_NOT_HC)
1190                                 {
1191                                         if(get_outgoing_voice_call_barring_state())
1192                                                 entry->ss_mode = SS_MODE_ACT;
1193                                         else
1194                                                 entry->ss_mode = g_cb_entry[i].ss_mode;
1195                                 }
1196                                 else if(g_cb_entry[i].type == SS_CB_TYPE_BIC_ROAM)
1197                                 {
1198                                         if(get_incoming_voice_call_barring_state())
1199                                                 entry->ss_mode = SS_MODE_ACT;
1200                                         else
1201                                                 entry->ss_mode = g_cb_entry[i].ss_mode;
1202                                 }
1203                                 else
1204                                         entry->ss_mode = g_cb_entry[i].ss_mode;
1205                         }
1206  
1207                         entry->count = g_cb_entry[i].count;     // it should be 0 ??? check plz...
1208                         
1209                         send_call_barring_entry(entry);
1210                         found++;
1211                 }
1212         }
1213         if(found > 0)
1214                 return entry;
1215
1216         log_msg(MSGL_VGSM_INFO,"not found \n");
1217
1218         if(tel_class == AT_CALL_SS_CLASS_VIDEO)                 // video call
1219         {       
1220                 if(type == SS_CB_TYPE_BAIC || type == SS_CB_TYPE_BIC_ROAM)      // incoming
1221                 {
1222                         log_msg(MSGL_VGSM_INFO,"class: %d, type: %d \n", tel_class, type);
1223                         if(get_incoming_video_call_barring_state())
1224                                 status = SS_MODE_ACT;
1225                         else
1226                                 status = SS_MODE_DEACT;
1227                 }
1228                 else                                                    // outgoing
1229                 {
1230                         log_msg(MSGL_VGSM_INFO,"class: %d, type: %d \n", tel_class, type);
1231                         if(get_outgoing_video_call_barring_state())
1232                                 status = SS_MODE_ACT;
1233                         else
1234                                 status = SS_MODE_DEACT;
1235                 }
1236         }
1237         else                                            // voice call
1238         {
1239                 if(type == SS_CB_TYPE_BAIC || type == SS_CB_TYPE_BIC_ROAM)      // incoming
1240                 {
1241                         log_msg(MSGL_VGSM_INFO,"class: %d, type: %d \n", tel_class, type);
1242                         if(get_incoming_voice_call_barring_state())
1243                                 status = SS_MODE_ACT;
1244                         else
1245                                 status = SS_MODE_DEACT;
1246                 }
1247                 else                                                    // outgoing
1248                 {
1249                         log_msg(MSGL_VGSM_INFO,"class: %d, type: %d \n", tel_class, type);
1250                         if(get_outgoing_voice_call_barring_state())
1251                                 status = SS_MODE_ACT;
1252                         else
1253                                 status = SS_MODE_DEACT;
1254                 }
1255         }
1256         // À̺κРȮÀÎÇÏÀÚ. db¿¡ ¾ø´Â°ÍÀ» µ¥ÀÌŸ¸¦ ¾î\89F°Ô ÁÙÁö »ý°¢....
1257         entry = &g_cb_entry_tmp ;
1258
1259         memset(entry, 0, sizeof(call_barring_entry_t));
1260         entry->type = type;
1261         entry->ss_mode = status;
1262         entry->count = 1;
1263
1264         if(tel_class == AT_CALL_SS_CLASS_ALL)
1265         {
1266                 entry->tel_class = AT_CALL_SS_CLASS_VOICE;
1267                 send_call_barring_entry(entry);
1268                 
1269                 entry->tel_class = AT_CALL_SS_CLASS_VIDEO;
1270                 send_call_barring_entry(entry);
1271         }
1272         else
1273         {
1274                 entry->tel_class = tel_class;
1275                 send_call_barring_entry(entry);
1276         }
1277         
1278         return entry;
1279 }
1280
1281 void set_call_forwarding_entry(call_forwarding_entry_t * entry, int num)
1282 {
1283         log_msg(MSGL_VGSM_INFO, "num: %d\n", num);
1284         if(num == 0)
1285                 memset(g_cf_entry, 0, sizeof(call_forwarding_entry_t) * 20);
1286         else
1287         {
1288                 g_cf_entry[num-1].tel_class = entry->tel_class;
1289                 g_cf_entry[num-1].type = entry->type;
1290                 memset(&g_cf_entry[num-1].number, 0, MAX_GSM_DIALED_DIGITS_NUMBER);
1291                 memcpy(&g_cf_entry[num-1].number, &entry->number, strlen((char*)&entry->number));
1292                 g_cf_entry[num-1].replyTime = entry->replyTime;
1293                 g_cf_entry[num-1].ss_mode = entry->ss_mode;
1294         }
1295         g_cf_entry[0].count = num;
1296         //      log_msg(MSGL_VGSM_INFO,"[set_call_forwarding_entry]--------[%s] -> <%s>\n", entry->number, g_cf_entry[num-1].number);
1297 }
1298
1299 call_forwarding_entry_t * get_call_forwarding_entry()
1300 {
1301         return (call_forwarding_entry_t *)&g_cf_entry;
1302 }
1303
1304 call_forwarding_entry_t g_cf_entry_tmp;
1305
1306 call_forwarding_entry_t * find_call_forwarding_entry(int tel_class, int type)
1307 {
1308         int i, class;
1309         call_forwarding_entry_t * entry ;
1310
1311         log_msg(MSGL_VGSM_INFO,"tel_class=0x%x  type=%d\n", tel_class,type);
1312
1313         for(i = 0; i<g_cf_entry[0].count; i++)
1314         {
1315                 log_msg(MSGL_VGSM_INFO,"tel_class=0x%x  type=%d\n", g_cf_entry[i].tel_class,g_cf_entry[i].type);
1316                 // 090221 090418
1317                 if(g_cf_entry[i].tel_class == -1)
1318                         class = 1;      // voice
1319                 if((class == tel_class) && (g_cf_entry[i].type == type))
1320                 {
1321                         log_msg(MSGL_VGSM_INFO,"entry: count %d \n", g_cf_entry[i].count);
1322
1323                         entry = &g_cf_entry_tmp;
1324
1325                         memset(entry, 0, sizeof(call_forwarding_entry_t));
1326                         //entry->tel_class = g_cf_entry[i].tel_class;
1327                         entry->tel_class = class;
1328                         entry->type = g_cf_entry[i].type;
1329                         entry->ss_mode = g_cf_entry[i].ss_mode;
1330                         entry->count = g_cf_entry[i].count;     // it should be 0 ??? check plz...
1331                         strcpy(entry->number, g_cf_entry[i].number);
1332                         entry->replyTime = g_cf_entry[i].replyTime;
1333                         //entry->number[0] = '\0';
1334                         //entry->replyTime = 0;
1335
1336                         return entry;
1337                         //return &g_cf_entry[i];
1338                 }
1339         }
1340         log_msg(MSGL_VGSM_INFO,"entry is not found !!! \n");
1341
1342         // À̺κРȮÀÎÇÏÀÚ. db¿¡ ¾ø´Â°ÍÀ» µ¥ÀÌŸ¸¦ ¾î\89F°Ô ÁÙÁö »ý°¢....
1343         //entry = (call_forwarding_entry_t *)malloc(sizeof(call_forwarding_entry_t));
1344         entry = &g_cf_entry_tmp;
1345
1346         memset(entry, 0, sizeof(call_forwarding_entry_t));
1347         entry->tel_class = tel_class;
1348         entry->type = type;
1349         entry->ss_mode = SS_MODE_DEACT;
1350         entry->count = 1;       // it should be 0 ??? check plz...
1351         entry->number[0] = '\0';
1352         entry->replyTime = 0;
1353
1354         return entry;
1355
1356 }
1357
1358 ///////////////////////////////////////////////////////////
1359
1360 char* get_callbarring_pwd(void)
1361 {
1362         return g_cb_pwd;
1363 }
1364
1365 void set_callbarring_pwd(char* cb_pwd)
1366 {
1367         strcpy(g_cb_pwd,cb_pwd);
1368 }
1369
1370 int increase_callbarring_pwd_fail_count(void)
1371 {
1372         g_cb_pwd_fail_count++;
1373         return g_cb_pwd_fail_count;
1374
1375 }
1376
1377 void clear_callbarring_pwd_fail_count(void)
1378 {
1379         g_cb_pwd_fail_count = 0;
1380 }
1381