summaryrefslogtreecommitdiff
path: root/src/agent/framework/event/oma_ds_platform_event_handler.c
blob: b348375be1dae2ffe7dbcce4892b658df824cfe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * oma-ds-agent
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *   @OMA_DS_Platform_Event_Handler.c
 *   @version									0.1
 *   @brief										This file is the source file of implementation of event callback function(from platform)
 */

#include <sync_agent.h>
#include <plugin/plugin_slp_sysnoti_wap_push.h>

#include "framework/event/oma_ds_platform_event_handler.h"
#include "framework/task/oma_ds_engine_controller_task.h"
#include "framework/san-parser/pm_sanparser.h"
#include "common/common_define_internal.h"
#include "common/common_util.h"

#ifndef OMADS_AGENT_LOG
#undef LOG_TAG
#define LOG_TAG "OMA_DS_COMMON"
#endif

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);

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);

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)
{
	_INNER_FUNC_ENTER;

	unsigned int request_msg_id_to_cancel = 0;
	get_periodic_sync_request_id(&request_msg_id_to_cancel);

	_INNER_FUNC_EXIT;

}

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)
{
	_INNER_FUNC_ENTER;

	unsigned int request_msg_id_to_cancel = 0;
	get_san_sync_request_id(&request_msg_id_to_cancel);

	_INNER_FUNC_EXIT;
}

int san_callback_parse(int item_id, void *user_data)
{
	_EXTERN_FUNC_ENTER;
	san_package_s *pSanPackage = NULL;
	int accountID = -1;
	char *sync_mode = NULL;
	char *server_id = NULL;
	bool result;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	sync_agent_fw_account_s *fw_account = NULL;
	GList *account_info_list = NULL;
	GList *iter = NULL;

	pmci_san_incoming_s *incoming_data = (pmci_san_incoming_s *) user_data;

	switch (incoming_data->version) {
	case 11:
		{
			pSanPackage = san_package_11_parser(incoming_data->msg_body, incoming_data->msg_size);
		}
		break;
	case 12:
		{
			pSanPackage = san_package_12_parser(incoming_data->msg_body, incoming_data->msg_size);
		}
		break;
	default:
		break;
	}

	sync_agent_da_return_e da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("da_err = %d", da_err);
		goto return_part;
	}

	if (pSanPackage == NULL) {
		_DEBUG_ERROR("pSanPackage is NULL");
		goto return_part;
	}

	sync_agent_fw_account_query_s query;
	query.query = ACCOUNT_QUERY_BY_NONE;

	acc_err = sync_agent_query_fw_account(&query, &account_info_list);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("sync_agent_query_fw_account is failed");
		goto return_part;
	}

	for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
		fw_account = (sync_agent_fw_account_s *) iter->data;
		result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SYNC_MODE, &sync_mode);
		if (result == true) {
			/*One device MUST NOT register multi account at same server... */
			if (strcmp(sync_mode, DEFINE_SYNC_MODE_PUSH) == 0) {
				result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id);
				if (result == true) {
					/*One device MUST NOT register multi account at same server... */
					if (strcmp(pSanPackage->server_id, server_id) == 0) {
						accountID = fw_account->account_id;
						break;
					}
				} else {
					_DEBUG_ERROR("failed in get_Config");
					goto return_part;
				}
			}
		} else {
			_DEBUG_ERROR("failed in get_Config");
			goto return_part;
		}

	}

	if (accountID < 0)
		goto return_part;

	char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH);

	void *in_param_value_array[3] = { &accountID, &syncMode, &pSanPackage };
	int in_param_index_array[3] = { 0, 1, 2 };
	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 };
	unsigned int request_msg_id = 0;
	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);

	insert_request_msg_info(SYNC_MODE_SAN, request_msg_id, accountID);

	sync_agent_close_agent();

	if (pSanPackage != NULL)
		sanPackageParserFree((void *)pSanPackage);

	_EXTERN_FUNC_EXIT;
	return 1;

 return_part:

	sync_agent_free_fw_account_list(account_info_list);

	if (sync_mode != NULL)
		free(sync_mode);

	if (server_id != NULL)
		free(server_id);

/*	if( incoming_data != NULL){
		if (incoming_data->msg_body != NULL)
			free((void *)incoming_data->msg_body);

		free(incoming_data);
	}*/

	if (pSanPackage != NULL)
		sanPackageParserFree((void *)pSanPackage);

	sync_agent_close_agent();

	_EXTERN_FUNC_EXIT;
	return 0;
}

void send_periodic_sync_msg(int scheduler_id, void *data)
{
	_EXTERN_FUNC_ENTER;

	_DEBUG_INFO("#######Scheduler Send Msg Success!!!!!##########");
	_DEBUG_INFO("schedulerId = %d", scheduler_id);

	int accountId = -1;
	int alarmId = 0;
	char *alarmId_str = NULL;
	bool result;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	sync_agent_fw_account_s *fw_account = NULL;
	GList *account_info_list = NULL;
	GList *iter = NULL;

	sync_agent_da_return_e da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_open_agent");
		goto error;
	}

	sync_agent_fw_account_query_s query;
	query.query = ACCOUNT_QUERY_BY_NONE;

	acc_err = sync_agent_query_fw_account(&query, &account_info_list);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("sync_agent_query_fw_account is failed");
		goto error;
	}

	for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
		fw_account = (sync_agent_fw_account_s *) iter->data;
		if (alarmId_str != NULL)
			free(alarmId_str);

		result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_ALARM_ID, &alarmId_str);
		if (result == false) {
			_DEBUG_ERROR("failed in get_Config");
			goto error;
		}
		alarmId = atoi(alarmId_str);
		_DEBUG_INFO("alarm id = %d", alarmId);
		if (alarmId == scheduler_id) {
			_DEBUG_INFO("account_list[i] = %d", fw_account->account_id);
			accountId = fw_account->account_id;
			break;
		}
	}

	if (accountId != -1) {
		int in_param_index_array[3] = { 0, 1, 2 };
		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
		};
		char *syncMode = strdup(DEFINE_SYNC_MODE_PERIODIC);

		void *in_param_value_array[3] = { &accountId, &syncMode, NULL };

		unsigned int request_msg_id = 0;
		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);

		insert_request_msg_info(SYNC_MODE_PERIODIC, request_msg_id, accountId);
	}

 error:

	sync_agent_free_fw_account_list(account_info_list);

	if (alarmId_str != NULL)
		free(alarmId_str);

	sync_agent_close_agent();

	_EXTERN_FUNC_EXIT;
}

int send_ip_push_sync_msg(char *data)
{
	_EXTERN_FUNC_ENTER;

	san_package_s *pSanPackage = NULL;
	char *id = NULL;
	char *server_id = NULL;
	int accountId = -1;
	bool result = false;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	GList *account_info_list = NULL;
	GList *iter = NULL;
	sync_agent_fw_account_s *account_info = NULL;

	sync_agent_da_return_e da_err = sync_agent_open_agent();
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("failed in sync_agent_open_agent");
		goto error;
	}

	result = parse_ip_push_msg(data, &pSanPackage, &id);
	if (result != true) {
		_DEBUG_ERROR("Failed in parseIPPushMsg");
		goto error;
	}

	cancel_current_sync_task();

	sync_agent_fw_account_query_s query;
	query.query = ACCOUNT_QUERY_BY_ACCESS_NAME;
	query.access_name = "DIVE";

	acc_err = sync_agent_query_fw_account(&query, &account_info_list);

	for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
		account_info = (sync_agent_fw_account_s *) iter->data;

		_DEBUG_INFO("accountId = %d", account_info->account_id);
		result = get_config(account_info->account_id, DEFINE_CONFIG_KEY_PROFILE_SERVER_ID, &server_id);
		if (result == true) {
			if (strcmp(pSanPackage->server_id, server_id) == 0) {
				accountId = account_info->account_id;
				break;
			}
		} else {
			_DEBUG_ERROR("failed in get_Config");
			goto error;
		}
	}

	if (accountId < 0)
		goto error;

	char *syncMode = strdup(DEFINE_SYNC_MODE_PUSH);

	void *in_param_value_array[3] = { &accountId, &syncMode, &pSanPackage };
	int in_param_index_array[3] = { 0, 1, 2 };
	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 };
	unsigned int request_msg_id = 0;
	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);

	insert_request_msg_info(SYNC_MODE_SAN, request_msg_id, accountId);

	sync_agent_free_fw_account_list(account_info_list);

	if (server_id != NULL)
		free(server_id);

//	if (pSanPackage != NULL)
//		sanPackageParserFree((void *)pSanPackage);

	sync_agent_close_agent();

	_EXTERN_FUNC_EXIT;
	return 1;

 error:

	sync_agent_free_fw_account_list(account_info_list);

	if (server_id != NULL)
		free(server_id);

	if (pSanPackage != NULL)
		sanPackageParserFree((void *)pSanPackage);

	sync_agent_close_agent();

	_EXTERN_FUNC_EXIT;
	return 0;
}