3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/socket.h>
25 #include <sync_agent.h>
26 #include <plugin/plugin_slp_sysnoti_wap_push.h>
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"
43 #ifndef OMADM_AGENT_LOG
45 #define LOG_TAG "OMA_DM_CP_PEH"
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);
54 static int _dm_notification_operation(const char *out_message, unsigned int outlen)
60 retvm_if((out_message) == NULL, COMMON_ERR_IS_NULL, "out_message is NULL!!");
62 SanPackage *pSanPackage = NULL;
63 Event_Contents *ev_data = NULL;
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;
73 ev_data->type = IP_PUSH_EVENT;
74 if ((pSanPackage->sessionID) == NULL || (pSanPackage->serverID) == NULL) {
75 _DEBUG_TRACE("san message null");
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;
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);
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);
92 switch (engine_type) {
93 case SAMSUNG_FUMO_TYPE:
96 * check samsung dm acc
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);
104 ret = fumo_service_start(ev_data);
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);
120 ret = fumo_service_start(ev_data);
125 case SAMSUNG_FMM_TYPE:
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);
137 ret = dm_lawmo_common_task_request(ev_data);
144 _DEBUG_TRACE("dm non engine ", ret);
149 _DEBUG_TRACE("san message null");
153 free_sanpackageparser(pSanPackage);
158 event_data_free((void *)ev_data);
159 free_sanpackageparser(pSanPackage);
161 _DEBUG_INFO("end error");
165 int dm_wap_push_incomming_callback(int item_id, void *user_data)
169 DM_ERROR ret = DM_OK;
171 retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
173 pmci_san_incoming_s *incomming_struct = (pmci_san_incoming_s *) user_data;
174 if (incomming_struct != NULL) {
177 db_ret = sync_agent_open_agent();
178 _DEBUG_INFO("OPEN DACI : %d", db_ret);
180 /* generate ext_id */
182 char **ext_id_list = NULL;
184 ext_id_list = sync_agent_generate_item_luid(1, 1);
185 if (ext_id_list == NULL) {
189 ext_id = atoi(ext_id_list[0]);
190 _DEBUG_INFO("ext_id : %d", ext_id);
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);
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]);
205 if (ext_id_list != NULL)
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]);
215 if (ext_id_list != NULL)
222 db_ret = sync_agent_close_agent();
223 _DEBUG_INFO("CLOSE DACI : %d", db_ret);
226 switch (incomming_struct->version) {
227 case 13: /*SAN_DM_XML = 13 */
229 /*pSanPackage = sanPackage11Parser( msgBody, msgSize ) ; */
232 case 14: /*SAN_DM_WBXML = 14 */
234 /*pSanPackage = sanPackage12Parser( msgBody, msgSize ) ; */
237 case 15: /*SAN_DM_NOTI = 15 */
239 int result = _dm_notification_operation(incomming_struct->msg_body, incomming_struct->msg_size);
240 _DEBUG_INFO("wap push parser : %d", result);
243 case 16: /*SAN_CP_XML = 16 */
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);
250 case 17: /*SAN_CP_WBXML = 17 */
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);
271 int dm_wap_push_operation_callback(int item_id, void *user_data)
275 DM_ERROR ret = DM_OK;
277 retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
279 CP_ERROR err = CP_ERROR_SUCCESS;
281 pmci_san_operation_s *operation_struct = (pmci_san_operation_s *) user_data;
282 if (operation_struct != NULL) {
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);
288 if (operation_struct->msg_id == -1 && operation_struct->ext_id == -1) { // delete all
289 _DEBUG_INFO("operation : delete all !!");
291 err = process_Delete_All_CP_Data();
292 if (err != CP_ERROR_SUCCESS) {
293 _DEBUG_INFO("process_Delete_All_CP_Data() fail !!");
297 _DEBUG_INFO("process_Delete_All_CP_Data() success !!");
299 } else if (operation_struct->msg_id == -1 && operation_struct->ext_id != -1) { // delete 1 msg
300 _DEBUG_INFO("operation : delete 1 msg !!");
302 err = process_Delete_CP_Data(operation_struct->ext_id);
303 if (err != CP_ERROR_SUCCESS) {
304 _DEBUG_INFO("process_Delete_CP_Data() fail !!");
308 _DEBUG_INFO("process_Delete_CP_Data() success !!");
311 _DEBUG_INFO("operation : install !!");
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 !!");
320 _DEBUG_INFO("process_Check_Pincode() success !!");
322 /* process_Setting_CP() */
323 if (is_correct == 1) {
324 _DEBUG_INFO("NETWPIN case - Valid PIN !!");
326 err = process_Setting_CP(operation_struct->ext_id);
327 if (err != CP_ERROR_SUCCESS) {
328 _DEBUG_INFO("process_Setting_CP() fail !!");
331 _DEBUG_INFO("process_Setting_CP() success !!");
333 } else if (is_correct == 0) {
334 _DEBUG_INFO("NETWPIN case - Invalid PIN !!");
342 if (operation_struct != NULL)
343 free(operation_struct);
352 int dm_ip_push_callback(int item_id, void *user_data)
356 DM_ERROR ret = DM_OK;
358 retvm_if((user_data) == NULL, -1, "user_data is NULL!!");
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==");
365 pushmessage = (char *)user_data;
367 _DEBUG_INFO("user_data : %s", (char *)user_data);
368 _DEBUG_INFO("push contents : %s", pushmessage);
370 if (pushmessage != NULL) {
371 char *app_type = strchr(pushmessage, '|') + 1;
372 char *message = NULL;
373 message = strstr(app_type, "DS|");
375 if (message != NULL) {
376 _DEBUG_INFO("DS event : %s", message);
377 ret = _send_event_ds(message);
382 message = strchr(app_type, '|') + 1;
383 _DEBUG_INFO("message :%s", message);
385 unsigned int outlen = 0;
386 unsigned char *out_message = g_base64_decode(message, &outlen);
388 int result = _dm_notification_operation((const char *)out_message, outlen);
389 _DEBUG_INFO("ip push parser : %d", result);
392 _DEBUG_INFO("push message null");
397 str_free(&pushmessage);
408 str_free(&pushmessage);
411 _DEBUG_INFO("end error ");
416 int dm_telephony_incomming_callback(int item_id, void *user_data)
420 _DEBUG_INFO("----------------------------------------------------incomming tel----------------------------------------------------");
422 DM_ERROR ret = DM_OK;
424 engine_status *status = NULL;
427 _DEBUG_INFO("dm db open = %d", ret); */
429 is_exist = IsExist_Engine_id(FUMO_SERVICE_ENGINE);
430 _DEBUG_INFO("is engine : %d", is_exist);
433 ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
437 if (status != NULL) {
438 if (status->ui_mode != NULL) {
439 if (strcmp(status->ui_mode, OMADM_FUMO_BACKGROUND_UI_TYPE) != 0) {
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);
451 _DEBUG_INFO("back ground ui");
464 if (status != NULL) {
465 Free_Memory_Engine_Status(&status, 1);
477 if (status != NULL) {
478 Free_Memory_Engine_Status(&status, 1);
480 _DEBUG_INFO("ERROR END = %d", ret);
486 int dm_telephony_end_callback(int item_id, void *user_data)
490 _DEBUG_INFO("----------------------------------------------------end tel----------------------------------------------------");
492 DM_ERROR ret = DM_OK;
493 sync_agent_da_return_e err = SYNC_AGENT_DA_SUCCESS;
495 engine_status *status = NULL;
498 char *server_id = NULL;
501 _DEBUG_INFO("dm db open = %d", ret);
503 err = sync_agent_open_agent();
504 _DEBUG_INFO("sync agent db open = %d", ret);
506 is_exist = IsExist_Engine_id(FUMO_SERVICE_ENGINE);
507 _DEBUG_INFO("is engine : %d", is_exist);
515 ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
519 _DEBUG_INFO("sync agent db close = %d", ret);
524 if (status != NULL && status->ui_mode != NULL) {
525 if (!strcmp(status->ui_mode, OMADM_FUMO_BACKGROUND_UI_TYPE)) {
527 is_alarm = check_existed_fumo_reminder_interval();
529 _DEBUG_INFO("have alarm");
533 ret = get_Serverid(SAMSUNG_FUMO_TYPE, &server_id);
537 err = sync_agent_close_agent();
538 _DEBUG_INFO("sync agent db close = %d", ret);
539 Free_Memory_Engine_Status(&status, 1);
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);
553 ret = dm_fumo_common_task_request(ev_data);
556 err = sync_agent_close_agent();
557 _DEBUG_INFO("sync agent db close = %d", ret);
558 Free_Memory_Engine_Status(&status, 1);
563 _DEBUG_INFO("server id is null");
575 err = sync_agent_close_agent();
576 _DEBUG_INFO("sync agent db close = %d", ret);
577 Free_Memory_Engine_Status(&status, 1);
583 int network_on_callback()
586 _DEBUG_INFO("network on");
591 int network_off_callback()
594 _DEBUG_INFO("network off");
599 int network_change_callback()
604 _DEBUG_INFO("changed network");
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) {
623 ret = auto_operate_service_engine();
625 _DEBUG_INFO("error : %d", ret);
634 int alarm_callback(int item_id, void *user_data)
637 _DEBUG_INFO("#######Scheduler Send Msg Success!!!!!########## : %d", item_id);
639 DM_ERROR ret = DM_OK;
644 sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS;
645 sync_agent_da_config_s *config = NULL;
649 db_ret = sync_agent_open_agent();
651 result = sync_agent_is_exist_config(FUMO_INTERVAL_TYPE, FUMO_INTERVAL, &exist);
652 if (result != SYNC_AGENT_DA_SUCCESS) {
657 _DEBUG_INFO("exist : %d", exist);
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);
669 sync_agent_free_config_list(list);
678 sync_agent_free_config_list(list);
682 result = sync_agent_remove_service_data(1, item_id);
683 _DEBUG_INFO("delete alarm %d", result);
687 result = sync_agent_is_exist_config(FUMO_ACCOUNT_ALARM_TYPE, FUMO_ACCOUNT_ALARM, &exist);
688 if (result != SYNC_AGENT_DA_SUCCESS) {
693 _DEBUG_INFO("exist : %d", exist);
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);
705 sync_agent_free_config_list(list);
714 sync_agent_free_config_list(list);
718 result = sync_agent_is_exist_config(LAWMO_ACCOUNT_ALARM_TYPE, LAWMO_ACCOUNT_ALARM, &exist);
719 if (result != SYNC_AGENT_DA_SUCCESS) {
724 _DEBUG_INFO("exist : %d", exist);
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);
736 sync_agent_free_config_list(list);
745 sync_agent_free_config_list(list);
749 db_ret = sync_agent_close_agent();
750 _DEBUG_INFO("CLOSE DACI : %d", db_ret);
756 static DM_ERROR _fumo_interval_alarm(int item_id)
760 DM_ERROR ret = DM_OK;
761 char *server_id = NULL;
762 engine_status *status = NULL;
763 Event_Contents *ev_data = NULL;
767 _DEBUG_TRACE("status db : %d", ret);
770 * check samsung dm acc
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);
778 if (IsExist_Engine_id(FUMO_SERVICE_ENGINE) != 0) {
779 ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
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);
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;
796 status->download_click = RESUME_STATUS_DOWNLOAD;
799 ret = Update_Engine_Status(status);
800 _DEBUG_TRACE(" update engine status : %d \n", ret);
804 Free_Memory_Engine_Status(&status, 1);
810 /*************************************************/
812 if (server_id == NULL) {
813 ret = get_Serverid(SAMSUNG_FUMO_TYPE, &server_id);
814 if (ret != DM_OK || server_id == NULL)
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;
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);
830 _DEBUG_TRACE("alarm : %s", ev_data->server_id);
831 ret = dm_fumo_common_task_request(ev_data);
836 str_free(&server_id);
842 str_free(&server_id);
843 event_data_free((void *)ev_data);
847 Free_Memory_Engine_Status(&status, 1);
849 _DEBUG_TRACE("end error : %d", ret);
854 static DM_ERROR _fumo_account_registration_alarm(int item_id)
858 DM_ERROR ret = DM_OK;
860 ret = register_fota_account();
862 _DEBUG_TRACE("fail register fota account : %d ", ret);
863 ret = DM_ERR_FOTA_ACCOUNT_REGISTRATION;
865 _DEBUG_TRACE("success register fota account : %d ", ret);
872 static DM_ERROR _lawmo_account_registration_alarm(int item_id)
876 DM_ERROR ret = DM_OK;
878 ret = register_lawmo_account(item_id, NULL);
880 _DEBUG_TRACE("fail register lawmo account : %d ", ret);
881 ret = DM_ERR_LAWMO_ACCOUNT_REGISTRATION;
883 _DEBUG_TRACE("success register lawmo account : %d ", ret);
890 static DM_ERROR _send_event_ds(char *event_msg)
896 struct sockaddr_un clientaddr;
900 retvm_if((event_msg) == NULL, DM_ERR_EVENT_DS, "event_msg is NULL!!");
902 msg = strchr(event_msg, '|') + 1;
903 _DEBUG_TRACE("msg = %s", msg);
905 sync_agent_event_data_s *event = sync_agent_create_event(event_type);
907 _DEBUG_TRACE("event is NULL");
909 return DM_ERR_EVENT_DS;
912 sync_agent_append_event_data_param(event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, msg);
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);
919 return DM_ERR_EVENT_DS;
922 bzero(&clientaddr, sizeof(clientaddr));
923 clientaddr.sun_family = AF_UNIX;
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);
933 return CP_ERROR_FAIL;
936 client_len = sizeof(clientaddr);
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);
943 return DM_ERR_EVENT_DS;
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);
956 void event_data_free(void *event_data)
960 retm_if((event_data) == NULL, "event_data is NULL!!");
962 Event_Contents *pEvent_data = (Event_Contents *) event_data;
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));