Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / agent / framework / platform-event-handler / dm_platform_event_handler.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*lib*/
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 /*sync-agent*/
25 #include <sync_agent.h>
26 #include <plugin/plugin_slp_sysnoti_wap_push.h>
27
28 /*common*/
29 #include "common/dm_common_def.h"
30 #include "common/dm_common.h"
31 #include "common/util/util.h"
32 #include "framework/task/oma_dm_task_request.h"
33 #include "framework/platform-event-handler/dm_platform_event_handler.h"
34 #include "framework/san-parser/pm_sanparser.h"
35 #include "dm-engine/fumo/fumo_account.h"
36 #include "dm-engine/fumo/fumo_engine.h"
37 #include "dm-engine/lawmo/lawmo_account.h"
38 #include "dm-engine/lawmo/lawmo_engine.h"
39 #include "dm-engine/cp/dm_cp_processor.h"
40 #include "dm-engine/cp/dm_cp_security.h"
41 #include "mo-handler/dm_mo_common.h"
42
43 #ifndef OMADM_AGENT_LOG
44 #undef LOG_TAG
45 #define LOG_TAG "OMA_DM_CP_PEH"
46 #endif
47
48 static DM_ERROR _fumo_interval_alarm(int item_id);
49 static DM_ERROR _fumo_account_registration_alarm(int item_id);
50 static DM_ERROR _lawmo_account_registration_alarm(int item_id);
51 static DM_ERROR _send_event_ds(char *event_msg);
52 static int _dm_notification_operation(const char *out_message, unsigned int outlen);
53
54 static int _dm_notification_operation(const char *out_message, unsigned int outlen)
55 {
56         _INNER_FUNC_ENTER;
57
58         DM_ERROR ret = DM_OK;
59
60         retvm_if((out_message) == NULL, COMMON_ERR_IS_NULL, "out_message is NULL!!");
61
62         SanPackage *pSanPackage = NULL;
63         Event_Contents *ev_data = NULL;
64         int is_exist = 0;
65         pSanPackage = sanpackage_noti_parser((char *)out_message, outlen);
66         if (pSanPackage != NULL) {
67                 ev_data = (Event_Contents *) calloc(1, sizeof(Event_Contents));
68                 if (ev_data == NULL) {
69                         _DEBUG_TRACE("calloc failed !!");
70                         free_sanpackageparser(pSanPackage);
71                         return COMMON_ERR_ALLOC;
72                 }
73                 ev_data->type = IP_PUSH_EVENT;
74                 if ((pSanPackage->sessionID) == NULL || (pSanPackage->serverID) == NULL) {
75                         _DEBUG_TRACE("san message null");
76                         goto error;
77                 }
78                 ev_data->session_id = strdup((char *)pSanPackage->sessionID);
79                 ev_data->server_id = strdup((char *)pSanPackage->serverID);
80                 ev_data->noti_type = pSanPackage->uiMode;
81
82                 _DEBUG_TRACE("event type ip push : %d", ev_data->type);
83                 _DEBUG_TRACE("session id : %s", ev_data->session_id);
84                 _DEBUG_TRACE("server id : %s", ev_data->server_id);
85                 _DEBUG_TRACE("noti type : %d", ev_data->noti_type);
86
87                 /* UI CONFIRM */
88                 SERVICE_SERVER_TYPE engine_type;
89                 engine_type = get_service_engine_type(ev_data->server_id);
90                 _DEBUG_TRACE("engine type : %d", engine_type);
91
92                 switch (engine_type) {
93                 case SAMSUNG_FUMO_TYPE:
94                         {
95                                 /*
96                                  * check samsung dm acc
97                                  */
98                                 ret = is_exist_dmacc(SAMSUNG_FUMO_TYPE, &is_exist);
99                                 if (ret != DM_OK || is_exist == 0) {
100                                         _DEBUG_TRACE("is exist dmacc error : %d or not exist dm acc : %d", ret, is_exist);
101                                         goto error;
102                                 }
103
104                                 ret = fumo_service_start(ev_data);
105                                 if (ret != DM_OK)
106                                         goto error;
107                         }
108                         break;
109                 case GCF_TYPE:
110                         {
111                                 /*
112                                  * check gcf dm acc
113                                  */
114                                 ret = is_exist_dmacc(GCF_TYPE, &is_exist);
115                                 if (ret != DM_OK || is_exist == 0) {
116                                         _DEBUG_TRACE("is exist dmacc error : %d or not exist dm acc : %d", ret, is_exist);
117                                         goto error;
118                                 }
119
120                                 ret = fumo_service_start(ev_data);
121                                 if (ret != DM_OK)
122                                         goto error;
123                         }
124                         break;
125                 case SAMSUNG_FMM_TYPE:
126                         {
127
128                                 /*
129                                  * check gcf dm acc
130                                  */
131                                 ret = is_exist_dmacc(SAMSUNG_FMM_TYPE, &is_exist);
132                                 if (ret != DM_OK || is_exist == 0) {
133                                         _DEBUG_TRACE("is exist dmacc error : %d or not exist dm acc : %d", ret, is_exist);
134                                         goto error;
135                                 }
136
137                                 ret = dm_lawmo_common_task_request(ev_data);
138                                 if (ret != DM_OK)
139                                         goto error;
140
141                         }
142                         break;
143                 default:
144                         _DEBUG_TRACE("dm non engine ", ret);
145                         break;
146                 }
147
148         } else {
149                 _DEBUG_TRACE("san message null");
150                 goto error;
151         }
152
153         free_sanpackageparser(pSanPackage);
154         _INNER_FUNC_EXIT;
155         return 1;
156
157  error:
158         event_data_free((void *)ev_data);
159         free_sanpackageparser(pSanPackage);
160         _INNER_FUNC_EXIT;
161         _DEBUG_INFO("end error");
162         return 0;
163 }
164
165 int dm_wap_push_incomming_callback(int item_id, void *user_data)
166 {
167         _EXTERN_FUNC_ENTER;
168
169         DM_ERROR ret = DM_OK;
170
171         retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
172
173         pmci_san_incoming_s *incomming_struct = (pmci_san_incoming_s *) user_data;
174         if (incomming_struct != NULL) {
175                 /* db open */
176                 int db_ret = 0;
177                 db_ret = sync_agent_open_agent();
178                 _DEBUG_INFO("OPEN DACI : %d", db_ret);
179
180                 /* generate ext_id */
181                 int ext_id = -1;
182                 char **ext_id_list = NULL;
183                 while (1) {
184                         ext_id_list = sync_agent_generate_item_luid(1, 1);
185                         if (ext_id_list == NULL) {
186                                 ret = -1;
187                                 return ret;
188                         }
189                         ext_id = atoi(ext_id_list[0]);
190                         _DEBUG_INFO("ext_id : %d", ext_id);
191
192                         /* create folder for generated ext_id */
193                         char folder_name[50] = { 0, };
194                         snprintf(folder_name, sizeof(folder_name), "%s/%d", CP_DATA_REPOSITORY_PATH, ext_id);
195                         _DEBUG_INFO("folder name : %s", folder_name);
196
197                         int folder_ret = sync_agent_create_directory(folder_name);
198                         if (folder_ret != 1) {
199                                 _DEBUG_INFO("sync_agent_create_directory( ext_id : %d ) failed !!", ext_id);
200                                 _DEBUG_INFO("retry sync_agent_generate_item_luid() !!");
201 //                              return DM_ERR_UNKNOWN;
202                                 if (ext_id_list[0] != NULL)
203                                         free(ext_id_list[0]);
204
205                                 if (ext_id_list != NULL)
206                                         free(ext_id_list);
207
208                                 ext_id = -1;
209                                 ext_id_list = NULL;
210                         } else {
211                                 _DEBUG_INFO("sync_agent_create_directory( ext_id : %d ) success !!", ext_id);
212                                 if (ext_id_list[0] != NULL)
213                                         free(ext_id_list[0]);
214
215                                 if (ext_id_list != NULL)
216                                         free(ext_id_list);
217
218                                 break;
219                         }
220                 }
221
222                 db_ret = sync_agent_close_agent();
223                 _DEBUG_INFO("CLOSE DACI : %d", db_ret);
224
225                 //              switch( version )
226                 switch (incomming_struct->version) {
227                 case 13:        /*SAN_DM_XML  = 13 */
228                         {
229                                 /*pSanPackage = sanPackage11Parser( msgBody, msgSize ) ; */
230                         }
231                         break;
232                 case 14:        /*SAN_DM_WBXML  = 14 */
233                         {
234                                 /*pSanPackage = sanPackage12Parser( msgBody, msgSize ) ; */
235                         }
236                         break;
237                 case 15:        /*SAN_DM_NOTI  = 15 */
238                         {
239                                 int result = _dm_notification_operation(incomming_struct->msg_body, incomming_struct->msg_size);
240                                 _DEBUG_INFO("wap push parser : %d", result);
241                         }
242                         break;
243                 case 16:        /*SAN_CP_XML = 16 */
244                         {
245                                 //                      CP_ERROR result = process_Recieved_CP_XML(msgBody, msgSize);
246                                 CP_ERROR result = process_Recieved_CP_XML(incomming_struct->msg_body, incomming_struct->msg_size, ext_id);
247                                 _DEBUG_INFO("xml cp parser : %d", result);
248                         }
249                         break;
250                 case 17:        /*SAN_CP_WBXML = 17 */
251                         {
252                                 //                      CP_ERROR result = process_Recieved_CP_WBXML(msgBody, msgSize);
253                                 CP_ERROR result = process_Recieved_CP_WBXML(incomming_struct->msg_body, incomming_struct->msg_size, ext_id);
254                                 _DEBUG_INFO("wxml cp parser : %d", result);
255                         }
256                         break;
257                 default:
258                         break;
259                 }
260         }
261
262         _DEBUG_INFO("end");
263
264         if (ret == DM_OK)
265                 ret = 1;
266
267         _EXTERN_FUNC_EXIT;
268         return ret;
269 }
270
271 int dm_wap_push_operation_callback(int item_id, void *user_data)
272 {
273         _EXTERN_FUNC_ENTER;
274
275         DM_ERROR ret = DM_OK;
276
277         retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
278
279         CP_ERROR err = CP_ERROR_SUCCESS;
280
281         pmci_san_operation_s *operation_struct = (pmci_san_operation_s *) user_data;
282         if (operation_struct != NULL) {
283
284                 process_Set_msgId(operation_struct->msg_id);
285                 _DEBUG_INFO("msg id : %d", operation_struct->msg_id);
286                 _DEBUG_INFO("ext id : %d", operation_struct->ext_id);
287
288                 if (operation_struct->msg_id == -1 && operation_struct->ext_id == -1) { // delete all
289                         _DEBUG_INFO("operation : delete all !!");
290
291                         err = process_Delete_All_CP_Data();
292                         if (err != CP_ERROR_SUCCESS) {
293                                 _DEBUG_INFO("process_Delete_All_CP_Data() fail !!");
294                                 //                              ret =   DM_ERR_CP
295                                 goto return_part;
296                         } else {
297                                 _DEBUG_INFO("process_Delete_All_CP_Data() success !!");
298                         }
299                 } else if (operation_struct->msg_id == -1 && operation_struct->ext_id != -1) {  // delete 1 msg
300                         _DEBUG_INFO("operation : delete 1 msg !!");
301
302                         err = process_Delete_CP_Data(operation_struct->ext_id);
303                         if (err != CP_ERROR_SUCCESS) {
304                                 _DEBUG_INFO("process_Delete_CP_Data() fail !!");
305                                 //                              ret =   DM_ERR_CP
306                                 goto return_part;
307                         } else {
308                                 _DEBUG_INFO("process_Delete_CP_Data() success !!");
309                         }
310                 } else {        // install
311                         _DEBUG_INFO("operation : install !!");
312
313                         int is_correct = -1;
314                         err = process_Check_Pincode(1, NULL, &is_correct, operation_struct->ext_id);
315                         if (err != CP_ERROR_SUCCESS) {
316                                 _DEBUG_INFO("process_Check_Pincode() fail !!");
317                                 //                              ret =   DM_ERR_CP
318                                 goto return_part;
319                         } else {
320                                 _DEBUG_INFO("process_Check_Pincode() success !!");
321
322                                 /* process_Setting_CP() */
323                                 if (is_correct == 1) {
324                                         _DEBUG_INFO("NETWPIN case - Valid PIN !!");
325
326                                         err = process_Setting_CP(operation_struct->ext_id);
327                                         if (err != CP_ERROR_SUCCESS) {
328                                                 _DEBUG_INFO("process_Setting_CP() fail !!");
329                                                 goto return_part;
330                                         } else {
331                                                 _DEBUG_INFO("process_Setting_CP() success !!");
332                                         }
333                                 } else if (is_correct == 0) {
334                                         _DEBUG_INFO("NETWPIN case - Invalid PIN !!");
335                                         err = CP_ERROR_FAIL;
336                                 }
337                         }
338                 }
339         }
340
341  return_part:
342         if (operation_struct != NULL)
343                 free(operation_struct);
344
345         if (ret == DM_OK)
346                 ret = 1;
347
348         _EXTERN_FUNC_EXIT;
349         return ret;
350 }
351
352 int dm_ip_push_callback(int item_id, void *user_data)
353 {
354         _EXTERN_FUNC_ENTER;
355
356         DM_ERROR ret = DM_OK;
357
358         retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
359
360         OPER_MODE operationMode;
361         operationMode = OPER_SERVER_INITIATION_FROM_IP;
362         char *pushmessage = NULL;
363         //ip_push_struct->message = strdup("IPP_20111216000000000000000001|DM|Ft4/CLTiWx/Y0AK+fBIayALoAAAAlPcKeDZnMXExNHI3NQ==");
364
365         pushmessage = (char *)user_data;
366
367         _DEBUG_INFO("user_data : %s", (char *)user_data);
368         _DEBUG_INFO("push contents : %s", pushmessage);
369
370         if (pushmessage != NULL) {
371                 char *app_type = strchr(pushmessage, '|') + 1;
372                 char *message = NULL;
373                 message = strstr(app_type, "DS|");
374
375                 if (message != NULL) {
376                         _DEBUG_INFO("DS event : %s", message);
377                         ret = _send_event_ds(message);
378                         _EXTERN_FUNC_EXIT;
379                         return ret;
380                 }
381
382                 message = strchr(app_type, '|') + 1;
383                 _DEBUG_INFO("message :%s", message);
384
385                 unsigned int outlen = 0;
386                 unsigned char *out_message = g_base64_decode(message, &outlen);
387
388                 int result = _dm_notification_operation((const char *)out_message, outlen);
389                 _DEBUG_INFO("ip push parser : %d", result);
390
391         } else {
392                 _DEBUG_INFO("push message null");
393                 //do nothing
394                 goto error;
395         }
396
397         str_free(&pushmessage);
398
399         if (ret == DM_OK) {
400                 ret = 1;
401         }
402
403         _EXTERN_FUNC_EXIT;
404
405         return ret;
406
407  error:
408         str_free(&pushmessage);
409
410         _EXTERN_FUNC_EXIT;
411         _DEBUG_INFO("end error ");
412         return ret;
413
414 }
415
416 int dm_telephony_incomming_callback(int item_id, void *user_data)
417 {
418         _EXTERN_FUNC_ENTER;
419
420         _DEBUG_INFO("----------------------------------------------------incomming tel----------------------------------------------------");
421
422         DM_ERROR ret = DM_OK;
423         int is_exist = 0;
424         engine_status *status = NULL;
425
426         /*ret =  DB_Open();
427            _DEBUG_INFO("dm db open = %d", ret); */
428
429         is_exist = IsExist_Engine_id(FUMO_SERVICE_ENGINE);
430         _DEBUG_INFO("is engine : %d", is_exist);
431
432         if (is_exist != 0) {
433                 ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
434                 if (ret != DM_OK)
435                         goto error;
436
437                 if (status != NULL) {
438                         if (status->ui_mode != NULL) {
439                                 if (strcmp(status->ui_mode, OMADM_FUMO_BACKGROUND_UI_TYPE) != 0) {
440
441                                         if (status->engine_status == DM_SERVICE_ING || status->engine_status == DM_SERVICE_UNKNOWN) {
442                                                 _DEBUG_INFO("cancel fumo task id  = %d ", status->task_id);
443                                                 cancel_task(status->task_id);
444                                                 _DEBUG_INFO("terminate ui");
445                                                 terminate_oma_dm_ui(DM_ERR_NOT_FOUND, FUMO_SERVICE_ENGINE);
446                                         } else {
447                                                 /*do nothing */
448                                         }
449                                 } else {
450                                         /*do nothing */
451                                         _DEBUG_INFO("back ground ui");
452                                 }
453                         } else {
454                                 /*do nothing */
455                         }
456                 } else {
457                         /*do nothing */
458                 }
459         } else {
460                 /*do nothing */
461         }
462         /*sleep(1);
463            DB_Close(); */
464         if (status != NULL) {
465                 Free_Memory_Engine_Status(&status, 1);
466         }
467
468         if (ret == DM_OK) {
469                 ret = 1;
470         }
471
472         _EXTERN_FUNC_EXIT;
473         return 1;
474  error:
475         /*sleep(1);
476            DB_Close(); */
477         if (status != NULL) {
478                 Free_Memory_Engine_Status(&status, 1);
479         }
480         _DEBUG_INFO("ERROR END = %d", ret);
481
482         _EXTERN_FUNC_EXIT;
483         return ret;
484 }
485
486 int dm_telephony_end_callback(int item_id, void *user_data)
487 {
488         _EXTERN_FUNC_ENTER;
489
490         _DEBUG_INFO("----------------------------------------------------end tel----------------------------------------------------");
491
492         DM_ERROR ret = DM_OK;
493         sync_agent_da_return_e err = SYNC_AGENT_DA_SUCCESS;
494
495         engine_status *status = NULL;
496         int is_exist = 0;
497         int is_alarm = 0;
498         char *server_id = NULL;
499
500         /*ret =  DB_Open();
501            _DEBUG_INFO("dm db open = %d", ret);
502          */
503         err = sync_agent_open_agent();
504         _DEBUG_INFO("sync agent db open = %d", ret);
505
506         is_exist = IsExist_Engine_id(FUMO_SERVICE_ENGINE);
507         _DEBUG_INFO("is engine : %d", is_exist);
508         if (is_exist == 0) {
509                 sleep(1);
510                 //DB_Close();
511                 _EXTERN_FUNC_EXIT;
512                 return 1;
513         }
514
515         ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
516         if (ret != DM_OK) {
517                 sleep(1);
518                 //DB_Close();
519                 _DEBUG_INFO("sync agent db close = %d", ret);
520                 _EXTERN_FUNC_EXIT;
521                 return 1;
522         }
523
524         if (status != NULL && status->ui_mode != NULL) {
525                 if (!strcmp(status->ui_mode, OMADM_FUMO_BACKGROUND_UI_TYPE)) {
526
527                         is_alarm = check_existed_fumo_reminder_interval();
528                         if (is_alarm == 1) {
529                                 _DEBUG_INFO("have alarm");
530                         } else {
531                                 sleep(5);
532
533                                 ret = get_Serverid(SAMSUNG_FUMO_TYPE, &server_id);
534                                 if (ret != DM_OK) {
535                                         sleep(1);
536                                         //DB_Close();
537                                         err = sync_agent_close_agent();
538                                         _DEBUG_INFO("sync agent db close = %d", ret);
539                                         Free_Memory_Engine_Status(&status, 1);
540                                         _EXTERN_FUNC_EXIT;
541                                         return 1;
542                                 }
543
544                                 if (server_id != NULL) {
545                                         //current time > alarm time
546                                         Event_Contents *ev_data = (Event_Contents *) calloc(1, sizeof(Event_Contents));
547                                         ev_data->type = UI_EVENT;
548                                         ev_data->session_id = NULL;
549                                         ev_data->server_id = server_id;
550                                         ev_data->noti_type = NOTI_TYPE_USERINTERACTION;
551                                         ev_data->ui_mode = strdup(OMADM_FUMO_NOTI_UI_TYPE);
552
553                                         ret = dm_fumo_common_task_request(ev_data);
554                                         if (ret != DM_OK) {
555                                                 //DB_Close();
556                                                 err = sync_agent_close_agent();
557                                                 _DEBUG_INFO("sync agent db close = %d", ret);
558                                                 Free_Memory_Engine_Status(&status, 1);
559                                                 _EXTERN_FUNC_EXIT;
560                                                 return 1;
561                                         }
562                                 } else {
563                                         _DEBUG_INFO("server id is null");
564                                 }
565                         }
566                 }
567         }
568
569         if (ret == DM_OK) {
570                 ret = 1;
571         }
572
573         sleep(1);
574         //DB_Close();
575         err = sync_agent_close_agent();
576         _DEBUG_INFO("sync agent db close = %d", ret);
577         Free_Memory_Engine_Status(&status, 1);
578
579         _EXTERN_FUNC_EXIT;
580         return 1;
581 }
582
583 int network_on_callback()
584 {
585         _EXTERN_FUNC_ENTER;
586         _DEBUG_INFO("network on");
587         _EXTERN_FUNC_EXIT;
588         return 1;
589 }
590
591 int network_off_callback()
592 {
593         _EXTERN_FUNC_ENTER;
594         _DEBUG_INFO("network off");
595         _EXTERN_FUNC_EXIT;
596         return 1;
597 }
598
599 int network_change_callback()
600 {
601         _EXTERN_FUNC_ENTER;
602
603         int ret = 0;
604         _DEBUG_INFO("changed network");
605
606         ret = 1;
607 /*for prevent*/
608 /*
609  * int config = 0;
610
611         //each company's
612          //check alarm
613
614         if(config == 0) {
615                 _DEBUG_INFO("wifi only off mode download");
616         } else if (config == 1) {
617                 ret = get_wifi_state();
618                 _DEBUG_INFO("wifi only mode : %d", ret);
619                 if (ret != SYNC_AGENT_NA_SUCCESS) {
620                         return 0;
621                 }
622
623                 ret = auto_operate_service_engine();
624                 if(ret !=DM_OK ) {
625                         _DEBUG_INFO("error : %d", ret);
626                         return 0;
627                 }
628         }*/
629
630         _EXTERN_FUNC_EXIT;
631         return ret;
632 }
633
634 int alarm_callback(int item_id, void *user_data)
635 {
636         _EXTERN_FUNC_ENTER;
637         _DEBUG_INFO("#######Scheduler Send Msg Success!!!!!########## : %d", item_id);
638
639         DM_ERROR ret = DM_OK;
640         int value = -1;
641         int count = 0;
642
643         int exist = 0;
644         sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS;
645         sync_agent_da_config_s *config = NULL;
646         GList *list = NULL;
647
648         int db_ret = 0;
649         db_ret = sync_agent_open_agent();
650
651         result = sync_agent_is_exist_config(FUMO_INTERVAL_TYPE, FUMO_INTERVAL, &exist);
652         if (result != SYNC_AGENT_DA_SUCCESS) {
653                 _EXTERN_FUNC_EXIT;
654                 return -1;
655         }
656
657         _DEBUG_INFO("exist : %d", exist);
658
659         if (exist != 0) {
660                 result = sync_agent_get_config_list(FUMO_INTERVAL_TYPE, &list);
661                 count = g_list_length(list);
662                 _DEBUG_INFO("count : %d", count);
663                 if (count != 0 && list != NULL) {
664                         config = (sync_agent_da_config_s *) g_list_nth_data(list, 0);
665                         if (chartoint(config->value, &value) == 1) {
666                                 if (value == item_id) {
667                                         ret = _fumo_interval_alarm(item_id);
668                                         if (ret != DM_OK) {
669                                                 sync_agent_free_config_list(list);
670                                                 _EXTERN_FUNC_EXIT;
671                                                 return -1;
672                                         }
673                                 }
674                         }
675                 }
676                 count = 0;
677                 value = -1;
678                 sync_agent_free_config_list(list);
679                 list = NULL;
680         } else {
681
682                 result = sync_agent_remove_service_data(1, item_id);
683                 _DEBUG_INFO("delete alarm %d", result);
684
685         }
686
687         result = sync_agent_is_exist_config(FUMO_ACCOUNT_ALARM_TYPE, FUMO_ACCOUNT_ALARM, &exist);
688         if (result != SYNC_AGENT_DA_SUCCESS) {
689                 _EXTERN_FUNC_EXIT;
690                 return -1;
691         }
692
693         _DEBUG_INFO("exist : %d", exist);
694
695         if (exist != 0) {
696                 result = sync_agent_get_config_list(FUMO_ACCOUNT_ALARM_TYPE, &list);
697                 count = g_list_length(list);
698                 _DEBUG_INFO("count : %d", count);
699                 if (count != 0 && list != NULL) {
700                         config = (sync_agent_da_config_s *) g_list_nth_data(list, 0);
701                         if (chartoint(config->value, &value) == 1) {
702                                 if (value == item_id) {
703                                         ret = _fumo_account_registration_alarm(item_id);
704                                         if (ret != DM_OK) {
705                                                 sync_agent_free_config_list(list);
706                                                 _EXTERN_FUNC_EXIT;
707                                                 return -1;
708                                         }
709                                 }
710                         }
711                 }
712                 count = 0;
713                 value = -1;
714                 sync_agent_free_config_list(list);
715                 list = NULL;
716         }
717
718         result = sync_agent_is_exist_config(LAWMO_ACCOUNT_ALARM_TYPE, LAWMO_ACCOUNT_ALARM, &exist);
719         if (result != SYNC_AGENT_DA_SUCCESS) {
720                 _EXTERN_FUNC_EXIT;
721                 return -1;
722         }
723
724         _DEBUG_INFO("exist : %d", exist);
725
726         if (exist != 0) {
727                 result = sync_agent_get_config_list(LAWMO_ACCOUNT_ALARM_TYPE, &list);
728                 count = g_list_length(list);
729                 _DEBUG_INFO("count : %d", count);
730                 if (count != 0 && list != NULL) {
731                         config = (sync_agent_da_config_s *) g_list_nth_data(list, 0);
732                         if (chartoint(config->value, &value) == 1) {
733                                 if (value == item_id) {
734                                         ret = _lawmo_account_registration_alarm(item_id);
735                                         if (ret != DM_OK) {
736                                                 sync_agent_free_config_list(list);
737                                                 _EXTERN_FUNC_EXIT;
738                                                 return -1;
739                                         }
740                                 }
741                         }
742                 }
743                 count = 0;
744                 value = -1;
745                 sync_agent_free_config_list(list);
746                 list = NULL;
747         }
748
749         db_ret = sync_agent_close_agent();
750         _DEBUG_INFO("CLOSE DACI : %d", db_ret);
751
752         _EXTERN_FUNC_EXIT;
753         return 1;
754 }
755
756 static DM_ERROR _fumo_interval_alarm(int item_id)
757 {
758         _INNER_FUNC_ENTER;
759
760         DM_ERROR ret = DM_OK;
761         char *server_id = NULL;
762         engine_status *status = NULL;
763         Event_Contents *ev_data = NULL;
764         int is_exist = 0;
765
766         ret = DB_Open();
767         _DEBUG_TRACE("status db : %d", ret);
768
769         /*
770          * check samsung dm acc
771          */
772         ret = is_exist_dmacc(SAMSUNG_FUMO_TYPE, &is_exist);
773         if (ret != DM_OK || is_exist == 0) {
774                 _DEBUG_TRACE("is exist dmacc error : %d or not exist dm acc : %d", ret, is_exist);
775                 goto error;
776         }
777
778         if (IsExist_Engine_id(FUMO_SERVICE_ENGINE) != 0) {
779                 ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
780                 if (ret != DM_OK)
781                         goto error;
782
783                 _DEBUG_TRACE(" result =[%d]\n", ret);
784                 /*update engine_status */
785                 if (status != NULL) {
786                         if (status->server_id != NULL) {
787                                 server_id = strdup(status->server_id);
788                         }
789
790                         status->engine_status = DM_SERVICE_ING;
791                         if (status->download_click == RESUME_STATUS_INSTALL_IDLE) {
792                                 status->download_click = RESUME_STATUS_INSTALL;
793                         } else if (status->download_click == RESUME_STATUS_DOWNLOAD_IDLE) {
794                                 status->download_click = RESUME_STATUS_DOWNLOAD;
795                         } else {
796                                 status->download_click = RESUME_STATUS_DOWNLOAD;
797                         }
798
799                         ret = Update_Engine_Status(status);
800                         _DEBUG_TRACE(" update engine status : %d \n", ret);
801
802                         DB_Close();
803
804                         Free_Memory_Engine_Status(&status, 1);
805                 }
806         } else {
807
808         }
809
810         /*************************************************/
811
812         if (server_id == NULL) {
813                 ret = get_Serverid(SAMSUNG_FUMO_TYPE, &server_id);
814                 if (ret != DM_OK || server_id == NULL)
815                         goto error;
816         }
817
818         ev_data = (Event_Contents *) calloc(1, sizeof(Event_Contents));
819         if (ev_data == NULL) {
820                 _DEBUG_TRACE("calloc failed !!");
821                 ret = COMMON_ERR_ALLOC;
822                 goto error;
823         }
824         ev_data->type = UI_EVENT;
825         ev_data->session_id = NULL;
826         ev_data->server_id = strdup(server_id);
827         ev_data->noti_type = NOTI_TYPE_USERINTERACTION;
828         ev_data->ui_mode = strdup(OMADM_FUMO_NOTI_UI_TYPE);
829
830         _DEBUG_TRACE("alarm : %s", ev_data->server_id);
831         ret = dm_fumo_common_task_request(ev_data);
832         if (ret != DM_OK) {
833                 goto error;
834         }
835
836         str_free(&server_id);
837
838         _INNER_FUNC_EXIT;
839         return ret;
840
841  error:
842         str_free(&server_id);
843         event_data_free((void *)ev_data);
844
845         DB_Close();
846         if (status != NULL)
847                 Free_Memory_Engine_Status(&status, 1);
848
849         _DEBUG_TRACE("end error : %d", ret);
850         _INNER_FUNC_EXIT;
851         return ret;
852 }
853
854 static DM_ERROR _fumo_account_registration_alarm(int item_id)
855 {
856         _INNER_FUNC_ENTER;
857
858         DM_ERROR ret = DM_OK;
859
860         ret = register_fota_account();
861         if (ret == 0) {
862                 _DEBUG_TRACE("fail register fota account : %d ", ret);
863                 ret = DM_ERR_FOTA_ACCOUNT_REGISTRATION;
864         } else {
865                 _DEBUG_TRACE("success register fota account : %d ", ret);
866         }
867
868         _INNER_FUNC_EXIT;
869         return ret;
870 }
871
872 static DM_ERROR _lawmo_account_registration_alarm(int item_id)
873 {
874         _INNER_FUNC_ENTER;
875
876         DM_ERROR ret = DM_OK;
877
878         ret = register_lawmo_account(item_id, NULL);
879         if (ret == 0) {
880                 _DEBUG_TRACE("fail register lawmo account : %d ", ret);
881                 ret = DM_ERR_LAWMO_ACCOUNT_REGISTRATION;
882         } else {
883                 _DEBUG_TRACE("success register lawmo account : %d ", ret);
884         }
885
886         _INNER_FUNC_EXIT;
887         return ret;
888 }
889
890 static DM_ERROR _send_event_ds(char *event_msg)
891 {
892         _INNER_FUNC_ENTER;
893
894         int client_len;
895         int client_sockfd;
896         struct sockaddr_un clientaddr;
897         int event_type = 9;
898         char *msg = NULL;
899
900         retvm_if((event_msg) == NULL, DM_ERR_EVENT_DS, "event_msg is NULL!!");
901
902         msg = strchr(event_msg, '|') + 1;
903         _DEBUG_TRACE("msg = %s", msg);
904
905         sync_agent_event_data_s *event = sync_agent_create_event(event_type);
906         if (event == NULL) {
907                 _DEBUG_TRACE("event is NULL");
908                 _INNER_FUNC_EXIT;
909                 return DM_ERR_EVENT_DS;
910         }
911
912         sync_agent_append_event_data_param(event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, msg);
913
914         client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
915         if (client_sockfd == -1) {
916                 _DEBUG_TRACE("socket create Error!!");
917                 sync_agent_free_event(event);
918                 _INNER_FUNC_EXIT;
919                 return DM_ERR_EVENT_DS;
920         }
921
922         bzero(&clientaddr, sizeof(clientaddr));
923         clientaddr.sun_family = AF_UNIX;
924
925         int str_len = 0;
926
927         str_len = g_strlcpy(clientaddr.sun_path, IPC_DS, (int)sizeof(clientaddr.sun_path));
928         if (str_len >= sizeof(clientaddr.sun_path)) {
929                 _DEBUG_TRACE("buffer overflow");
930                 sync_agent_free_event(event);
931                 close(client_sockfd);
932                 _INNER_FUNC_EXIT;
933                 return CP_ERROR_FAIL;
934         }
935
936         client_len = sizeof(clientaddr);
937
938         if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0) {
939                 _DEBUG_TRACE("socket connect error");
940                 sync_agent_free_event(event);
941                 close(client_sockfd);
942                 _INNER_FUNC_EXIT;
943                 return DM_ERR_EVENT_DS;
944         }
945
946         int send_data_size = (event->data - event->size) + 1;
947         int write_ret = write(client_sockfd, (void *)(event->size), send_data_size);
948         _DEBUG_TRACE("write ret : %d", write_ret);
949         close(client_sockfd);
950         sync_agent_free_event(event);
951
952         _INNER_FUNC_EXIT;
953         return DM_OK;
954 }
955
956 void event_data_free(void *event_data)
957 {
958         _EXTERN_FUNC_ENTER;
959
960         retm_if((event_data) == NULL, "event_data is NULL!!");
961
962         Event_Contents *pEvent_data = (Event_Contents *) event_data;
963
964         pEvent_data->type = 0;
965         pEvent_data->noti_type = 0;
966         str_free(&(pEvent_data->server_id));
967         str_free(&(pEvent_data->session_id));
968         str_free(&(pEvent_data->ui_mode));
969
970         free(pEvent_data);
971
972         _EXTERN_FUNC_EXIT;
973 }