Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / agent / framework / event / oma_ds_platform_event_handler.c
1 /*
2  * oma-ds-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 /**
19  *   @OMA_DS_Platform_Event_Handler.c
20  *   @version                                                                   0.1
21  *   @brief                                                                             This file is the source file of implementation of event callback function(from platform)
22  */
23
24 #include <sync_agent.h>
25 #include <plugin/plugin_slp_sysnoti_wap_push.h>
26
27 #include "framework/event/oma_ds_platform_event_handler.h"
28 #include "framework/task/oma_ds_engine_controller_task.h"
29 #include "framework/san-parser/pm_sanparser.h"
30 #include "common/common_define_internal.h"
31 #include "common/common_util.h"
32
33 #ifndef OMADS_AGENT_LOG
34 #undef LOG_TAG
35 #define LOG_TAG "OMA_DS_COMMON"
36 #endif
37
38 static void _request_periodic_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data);
39
40 static void _request_san_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data);
41
42 static void _request_periodic_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data)
43 {
44         _INNER_FUNC_ENTER;
45
46         unsigned int request_msg_id_to_cancel = 0;
47         get_periodic_sync_request_id(&request_msg_id_to_cancel);
48
49         _INNER_FUNC_EXIT;
50
51 }
52
53 static void _request_san_sync_task_finish_callback(sync_agent_ec_task_error_e task_error, unsigned int out_param_cnt, sync_agent_ec_param_param_s ** out_param_spec_array, void *usr_data)
54 {
55         _INNER_FUNC_ENTER;
56
57         unsigned int request_msg_id_to_cancel = 0;
58         get_san_sync_request_id(&request_msg_id_to_cancel);
59
60         _INNER_FUNC_EXIT;
61 }
62
63 int san_callback_parse(int item_id, void *user_data)
64 {
65         _EXTERN_FUNC_ENTER;
66         san_package_s *pSanPackage = NULL;
67         int accountID = -1;
68         char *sync_mode = NULL;
69         char *server_id = NULL;
70         bool result;
71
72         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
73         sync_agent_fw_account_s *fw_account = NULL;
74         GList *account_info_list = NULL;
75         GList *iter = NULL;
76
77         pmci_san_incoming_s *incoming_data = (pmci_san_incoming_s *) user_data;
78
79         switch (incoming_data->version) {
80         case 11:
81                 {
82                         pSanPackage = san_package_11_parser(incoming_data->msg_body, incoming_data->msg_size);
83                 }
84                 break;
85         case 12:
86                 {
87                         pSanPackage = san_package_12_parser(incoming_data->msg_body, incoming_data->msg_size);
88                 }
89                 break;
90         default:
91                 break;
92         }
93
94         sync_agent_da_return_e da_err = sync_agent_open_agent();
95         if (da_err != SYNC_AGENT_DA_SUCCESS) {
96                 _DEBUG_ERROR("da_err = %d", da_err);
97                 goto return_part;
98         }
99
100         if (pSanPackage == NULL) {
101                 _DEBUG_ERROR("pSanPackage is NULL");
102                 goto return_part;
103         }
104
105         sync_agent_fw_account_query_s query;
106         query.query = ACCOUNT_QUERY_BY_NONE;
107
108         acc_err = sync_agent_query_fw_account(&query, &account_info_list);
109         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
110                 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
111                 goto return_part;
112         }
113
114         for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
115                 fw_account = (sync_agent_fw_account_s *) iter->data;
116                 result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE, &sync_mode);
117                 if (result == true) {
118                         /*One device MUST NOT register multi account at same server... */
119                         if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
120                                 result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id);
121                                 if (result == true) {
122                                         /*One device MUST NOT register multi account at same server... */
123                                         if (strcmp(pSanPackage->server_id, server_id) == 0) {
124                                                 accountID = fw_account->account_id;
125                                                 break;
126                                         }
127                                 } else {
128                                         _DEBUG_ERROR("failed in get_Config");
129                                         goto return_part;
130                                 }
131                         }
132                 } else {
133                         _DEBUG_ERROR("failed in get_Config");
134                         goto return_part;
135                 }
136
137         }
138
139         if (accountID < 0)
140                 goto return_part;
141
142         char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH);
143
144         void *in_param_value_array[3] = { &accountID, &syncMode, &pSanPackage };
145         int in_param_index_array[3] = { 0, 1, 2 };
146         sync_agent_ec_value_type_e in_param_type[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT };
147         unsigned int request_msg_id = 0;
148         sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_type, in_param_value_array, _request_san_sync_task_finish_callback, NULL, (int *)&request_msg_id);
149
150         insert_request_msg_info(SYNC_MODE_SAN, request_msg_id);
151
152         sync_agent_close_agent();
153
154         if (pSanPackage != NULL)
155                 sanPackageParserFree((void *)pSanPackage);
156
157         _EXTERN_FUNC_EXIT;
158         return 1;
159
160  return_part:
161
162         sync_agent_free_fw_account_list(account_info_list);
163
164         if (sync_mode != NULL)
165                 free(sync_mode);
166
167         if (server_id != NULL)
168                 free(server_id);
169
170 /*      if( incoming_data != NULL){
171                 if (incoming_data->msg_body != NULL)
172                         free((void *)incoming_data->msg_body);
173
174                 free(incoming_data);
175         }*/
176
177         if (pSanPackage != NULL)
178                 sanPackageParserFree((void *)pSanPackage);
179
180         sync_agent_close_agent();
181
182         _EXTERN_FUNC_EXIT;
183         return 0;
184 }
185
186 void send_periodic_sync_msg(int scheduler_id, void *data)
187 {
188         _EXTERN_FUNC_ENTER;
189
190         _DEBUG_INFO("#######Scheduler Send Msg Success!!!!!##########");
191         _DEBUG_INFO("schedulerId = %d", scheduler_id);
192
193         int accountId = -1;
194         int alarmId = 0;
195         char *alarmId_str = NULL;
196         bool result;
197
198         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
199         sync_agent_fw_account_s *fw_account = NULL;
200         GList *account_info_list = NULL;
201         GList *iter = NULL;
202
203         sync_agent_da_return_e da_err = sync_agent_open_agent();
204         if (da_err != SYNC_AGENT_DA_SUCCESS) {
205                 _DEBUG_ERROR("failed in sync_agent_open_agent");
206                 goto error;
207         }
208
209         sync_agent_fw_account_query_s query;
210         query.query = ACCOUNT_QUERY_BY_NONE;
211
212         acc_err = sync_agent_query_fw_account(&query, &account_info_list);
213         if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
214                 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
215                 goto error;
216         }
217
218         for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
219                 fw_account = (sync_agent_fw_account_s *) iter->data;
220                 if (alarmId_str != NULL)
221                         free(alarmId_str);
222
223                 result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &alarmId_str);
224                 if (result == false) {
225                         _DEBUG_ERROR("failed in get_Config");
226                         goto error;
227                 }
228                 alarmId = atoi(alarmId_str);
229                 _DEBUG_INFO("alarm id = %d", alarmId);
230                 if (alarmId == scheduler_id) {
231                         _DEBUG_INFO("account_list[i] = %d", fw_account->account_id);
232                         accountId = fw_account->account_id;
233                         break;
234                 }
235         }
236
237         if (accountId != -1) {
238                 int in_param_index_array[3] = { 0, 1, 2 };
239                 sync_agent_ec_value_type_e in_param_value_type_array[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT,
240                         SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT
241                 };
242                 char *syncMode = strdup(DEFINE_SYNC_MODE_PERIODIC);
243
244                 void *in_param_value_array[3] = { &accountId, &syncMode, NULL };
245
246                 unsigned int request_msg_id = 0;
247                 sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_value_type_array, in_param_value_array, _request_periodic_sync_task_finish_callback, NULL, (int *)&request_msg_id);
248
249                 insert_request_msg_info(SYNC_MODE_PERIODIC, request_msg_id);
250         }
251
252  error:
253
254         sync_agent_free_fw_account_list(account_info_list);
255
256         if (alarmId_str != NULL)
257                 free(alarmId_str);
258
259         sync_agent_close_agent();
260
261         _EXTERN_FUNC_EXIT;
262 }
263
264 int send_ip_push_sync_msg(char *data)
265 {
266         _EXTERN_FUNC_ENTER;
267
268         san_package_s *pSanPackage = NULL;
269         char *id = NULL;
270         char *server_id = NULL;
271         int accountId = -1;
272         bool result = false;
273
274         sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
275         GList *account_info_list = NULL;
276         GList *iter = NULL;
277         sync_agent_fw_account_s *account_info = NULL;
278
279         sync_agent_da_return_e da_err = sync_agent_open_agent();
280         if (da_err != SYNC_AGENT_DA_SUCCESS) {
281                 _DEBUG_ERROR("failed in sync_agent_open_agent");
282                 goto error;
283         }
284
285         result = parse_ip_push_msg(data, &pSanPackage, &id);
286         if (result != true) {
287                 _DEBUG_ERROR("Failed in parseIPPushMsg");
288                 goto error;
289         }
290
291         cancel_current_sync_task();
292
293         sync_agent_fw_account_query_s query;
294         query.query = ACCOUNT_QUERY_BY_ACCESS_NAME;
295         query.access_name = "DIVE";
296
297         acc_err = sync_agent_query_fw_account(&query, &account_info_list);
298
299         for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
300                 account_info = (sync_agent_fw_account_s *) iter->data;
301
302                 _DEBUG_INFO("accountId = %d", account_info->account_id);
303                 result = get_config(account_info->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id);
304                 if (result == true) {
305                         if (strcmp(pSanPackage->server_id, server_id) == 0) {
306                                 accountId = account_info->account_id;
307                                 break;
308                         }
309                 } else {
310                         _DEBUG_ERROR("failed in get_Config");
311                         goto error;
312                 }
313         }
314
315         if (accountId < 0)
316                 goto error;
317
318         char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH);
319
320         void *in_param_value_array[3] = { &accountId, &syncMode, &pSanPackage };
321         int in_param_index_array[3] = { 0, 1, 2 };
322         sync_agent_ec_value_type_e in_param_value_type_array[3] = { SYNC_AGENT_EC_VALUE_TYPE_INT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT, SYNC_AGENT_EC_VALUE_TYPE_STRUCT };
323         unsigned int request_msg_id = 0;
324         sync_agent_request_async_task(EC_MSG_TYPE_SYNC_TASK_REQUEST, 0, 3, in_param_index_array, in_param_value_type_array, in_param_value_array, _request_san_sync_task_finish_callback, NULL, (int *)&request_msg_id);
325
326         insert_request_msg_info(SYNC_MODE_SAN, request_msg_id);
327
328         sync_agent_free_fw_account_list(account_info_list);
329
330         if (server_id != NULL)
331                 free(server_id);
332
333         if (pSanPackage != NULL)
334                 sanPackageParserFree((void *)pSanPackage);
335
336         sync_agent_close_agent();
337
338         _EXTERN_FUNC_EXIT;
339         return 1;
340
341  error:
342
343         sync_agent_free_fw_account_list(account_info_list);
344
345         if (server_id != NULL)
346                 free(server_id);
347
348         if (pSanPackage != NULL)
349                 sanPackageParserFree((void *)pSanPackage);
350
351         sync_agent_close_agent();
352
353         _EXTERN_FUNC_EXIT;
354         return 0;
355 }