summaryrefslogtreecommitdiff
path: root/src/agent/dm-engine/dm_common_engine.c
blob: 42fa7adf7ba12b52231644d8489daba3385eeb95 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
 * oma-dm-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.
 */

/*lib*/
#include <stdio.h>

/*sync-agent*/
#include <sync_agent.h>

/*dm-agent*/
#include "common/dm_common_def.h"
#include "common/dm_common.h"
#include "common/util/util.h"
#include "framework/task/oma_dm_task_request.h"
#include "framework/san-parser/pm_sanparser.h"
#include "dm-engine/dm_common_engine.h"
#include "dm-engine/fumo/fumo_engine.h"
#include "dm-engine/lawmo/lawmo_engine.h"
#include "mo-handler/dm_mo_common_internal.h"
#include "mo-handler/dm_mo_common.h"
#include "serviceadapter/dm-phase-handler/dm_phase_handler.h"
#include "serviceadapter/sa_define.h"
#include "serviceadapter/sa_session.h"
#include "serviceadapter/sa_session_internal.h"
#include "ipc_common.h"
#include "ipc_agent.h"

#ifndef OMADM_AGENT_LOG
#undef LOG_TAG
#define LOG_TAG	"COMMON_ENGINE"
#endif

static DM_ERROR _change_engine_status(char *server_id, char *full_path, char *correlator, int service_engine_status);

static DM_ERROR _change_engine_status(char *server_id, char *full_path, char *correlator, int service_engine_status)
{
	_INNER_FUNC_ENTER;
	DM_ERROR ret = DM_OK;

	retvm_if((server_id) == NULL, COMMON_ERR_IS_NULL, "full_path is NULL!!");
	retvm_if((full_path) == NULL, COMMON_ERR_IS_NULL, "full_path is NULL!!");

	int task_id;
	task_id = sync_agent_get_self_request_id();
	_DEBUG_TRACE("service engine db update");

	engine_status *status;

	int is_exist;
	is_exist = IsExist_Engine_id(service_engine_status);

	if (is_exist == 1) {
		ret = Get_Engine_Status(service_engine_status, &status);
		_DEBUG_TRACE("get engine status : %d", ret);
	} else {
		status = (engine_status *) calloc(1, sizeof(engine_status));
		_DEBUG_TRACE("get engine status error");
	}

	if (status == NULL) {
		_DEBUG_TRACE("calloc failed !!");
		ret = COMMON_ERR_ALLOC;
		goto error;
	}

	if (correlator != NULL) {
		status->correlator = strdup(correlator);
	} else {
		status->correlator = NULL;
	}

	status->engine_status = DM_SERVICE_ING;
	status->engine_id = service_engine_status;
	status->mo_path = strdup(full_path);
	status->server_url = NULL;
	status->result_status = DM_OK;
	status->server_id = strdup(server_id);
	status->task_id = task_id;
	status->ui_noti_type = 0;
	status->download_click = 0;
	//status->ui_mode = ;

	/* one engine */
	/*      if(IsExist_Engine_id(SERVICE_ENGINE)) {
	   ret = Delete_Engine_Status(SERVICE_ENGINE);
	   } */
	ret = Update_Engine_Status(status);
	_DEBUG_TRACE(" update engine status : %d \n", ret);

	if (status != NULL)
		Free_Memory_Engine_Status(&status, 1);

	if (ret != DM_OK) {
//              DB_End_Transaction(TRANSACTION_ROLLBACK_);
		goto error;
	}
	//sleep(10);
/*	ret = Update_Engine_Status_Column(IDLE_ENGINE, VALUE_ENGINE_STATUS, &service_engine_status);
	_DEBUG_TRACE(" update engine status : %d \n", ret);
	if (ret != DM_OK) {
		DB_End_Transaction(TRANSACTION_ROLLBACK_);
		goto error;
	}*/

	_INNER_FUNC_EXIT;
	return ret;
 error:

	_DEBUG_TRACE(" end error : %d \n", ret);
	_INNER_FUNC_EXIT;
	return ret;
}

void init_Dm_Engine()
{
	_EXTERN_FUNC_ENTER;

	/* always rebooting operation but excepted bootstrap */
	reset_devinfo();
	reset_devdetail();

	_EXTERN_FUNC_EXIT;
}

DM_ERROR engine_start(char *server_id, char *full_path, char *correlator, ENGINE_ID * service_engine_id, int *resultCode)
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	if (server_id == NULL || full_path == NULL) {
		(*resultCode) = CLIENT_ERROR;
		ret = COMMON_ERR_IS_NULL;
		goto error;
	}

	sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
	sync_agent_dm_mo_type_e mo_type;

	err_code = sync_agent_get_mo_type(full_path, &mo_type);
	_DEBUG_INFO("get mo type result : %d", err_code);

	(*resultCode) = DM_ERR_ACCEPTED_FOR_PROCESSING;

	_DEBUG_INFO(" ------------------------------------------------------------------------------------------------------------------------------------- ");
	_DEBUG_INFO(" mo_type : %d , server id : %s , full_path : %s , correlator : %s  ", mo_type, server_id, full_path, correlator);
	_DEBUG_INFO(" ------------------------------------------------------------------------------------------------------------------------------------- ");

	switch (mo_type) {
	case SYNC_AGENT_DM_MO_TYPE_FUMO:

		_DEBUG_INFO("---------------------------------------------------------------- fumo engine ready --------------------------------------------------------------------- ");
		ret = _change_engine_status(server_id, full_path, correlator, FUMO_SERVICE_ENGINE);
		if (ret != DM_OK)
			goto error;

		if (correlator == NULL) {
			/*because of gcf server */
			/*ret = fumo_exec(full_path, correlator, (FUMO_Error *)resultCode);
			   if(ret !=DM_OK)
			   goto error; */
		}

		*service_engine_id = FUMO_SERVICE_ENGINE;

		break;
	case SYNC_AGENT_DM_MO_TYPE_LAWMO:
		_DEBUG_INFO("---------------------------------------------------------------- lawmo engine ready --------------------------------------------------------------------- ");
		ret = _change_engine_status(server_id, full_path, correlator, LAWMO_SERVICE_ENGINE);
		if (ret != DM_OK)
			goto error;

		//if( !correlator) {
		// 1 : this is just check , available operation
		ret = lawmo_exec(full_path, correlator, (LAWMO_Result_code *) resultCode, CHECK_OPERATION);
		if (ret != DM_OK)
			goto error;
		//}

		*service_engine_id = LAWMO_SERVICE_ENGINE;

		break;
	default:
		(*resultCode) = CLIENT_ERROR;
		_DEBUG_INFO(" not existed engine \n");
		break;
	}

	_DEBUG_INFO("====================result code : %d====================\n", *resultCode);
	_DEBUG_INFO(" end!!  resut code : %d\n", *resultCode);
	_EXTERN_FUNC_EXIT;
	return ret;
 error:

	_DEBUG_INFO("====================result code : %d====================\n", *resultCode);
	_DEBUG_INFO(" error end %d  result code : %d", ret, *resultCode);
	_EXTERN_FUNC_EXIT;
	return ret;
}

DM_ERROR service_engine_start(ENGINE_ID service_engine_id, DM_ERROR * service_ret)
{
	_EXTERN_FUNC_ENTER;

	_DEBUG_INFO(" start : service_engine_id : %d", service_engine_id);
	DM_ERROR ret = DM_OK;
	engine_status *service_status = NULL;
	engine_status *after_service_status = NULL;
	char *full_path = NULL;
	char *correlator = NULL;
	char *server_id = NULL;
	int service_engine_status;
	bool cancel_flag = 0;

	cancel_flag = sync_agent_check_cancel_flag();
	if (cancel_flag != 0) {
		ret = DM_ERR_OPERATION_CANCELLED;
		goto error;
	}

	if (IsExist_Engine_id(service_engine_id) == 1) {

		_DEBUG_INFO("service engine id = %d\n", service_engine_id);
		_DEBUG_INFO(" engine must re-start");

		ret = Get_Engine_Status(service_engine_id, &service_status);
		_DEBUG_INFO(" get engine status : %d \n", ret);
		if (ret != DM_OK)
			goto error;

		if (service_status == NULL) {
			_DEBUG_INFO("service status is null");
			ret = COMMON_ERR_IS_NULL;
			goto error;
		}

		if (service_status->engine_status == DM_SERVICE_UNKNOWN) {
			_DEBUG_INFO("engine status unknown");
			goto error;
		}

		if (service_status->engine_status == DM_SERVICE_ING) {
			_DEBUG_INFO("-------------------------------------------- service type ------------------------------------------------ : %d", service_engine_id);

			int engine_return = OPERATION_SUCCEEDED;

			if (service_status->mo_path != NULL) {
				full_path = strdup(service_status->mo_path);
				_DEBUG_INFO("full path : %s ", full_path);
			} else {
				ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
				goto error;
			}

			if (service_status->server_id != NULL) {
				server_id = strdup(service_status->server_id);
				_DEBUG_INFO("server_id : %s", server_id);
			} else {
				ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
				goto error;
			}

			if (service_status->correlator != NULL) {
				correlator = strdup(service_status->correlator);
				_DEBUG_INFO("correlator : %s", correlator);
			}

			_DEBUG_INFO("service status : %d", service_status->engine_status);

			switch (service_engine_id) {
			case FUMO_SERVICE_ENGINE:
				_DEBUG_INFO(" fumo exec");
				ret = fumo_exec(full_path, correlator, (FUMO_Error *) (&engine_return));
				_DEBUG_INFO("fumo ret : %d", ret);
				*service_ret = ret;
				if (ret != DM_OK && ret != DM_ERR_USER_CANDELLED && ret != DM_ERR_REMINDER_INTERVAL /*|| ret != DM_ERR_DEVICE_ABORTED */ )
					goto error;
				break;
			case LAWMO_SERVICE_ENGINE:
				_DEBUG_INFO(" lawmo exec");
				ret = lawmo_exec(full_path, correlator, (LAWMO_Result_code *) (&engine_return), NON_CHECK_OPERATION);
				_DEBUG_INFO("lawmo ret : %d", ret);
				*service_ret = ret;
				if (ret != DM_OK)
					goto error;
				break;
			default:
				break;
			}
			_DEBUG_INFO("exe resutlt : %d , server id : %s", engine_return, server_id);
			_DEBUG_INFO("correlator : %s", correlator);

			ret = Get_Engine_Status(service_engine_id, &after_service_status);
			_DEBUG_INFO(" get engine status : %d \n", ret);
			if (ret != DM_OK)
				goto error;

			if (after_service_status == NULL) {
				_DEBUG_INFO("service status is null");
				ret = COMMON_ERR_IS_NULL;
				goto error;
			}

			if (after_service_status->download_click != RESUME_STATUS_INSTALL_BEFORE) {
				/*engine status generic alert status */
				service_engine_status = DM_GENERICALERT_ING;
				ret = Update_Engine_Status_Column(service_engine_id, VALUE_ENGINE_STATUS, &service_engine_status);
				_DEBUG_INFO("service engine status generic alert");

				ret = Update_Engine_Status_Column(service_engine_id, VALUE_RESULT_STATUS, &engine_return);
				_DEBUG_INFO("service engine status generic alert status : %d", engine_return);
			}

			str_free(&full_path);
			str_free(&correlator);
			str_free(&server_id);

			if (service_status != NULL) {
				Free_Memory_Engine_Status(&service_status, 1);
			}
			if (after_service_status != NULL) {
				Free_Memory_Engine_Status(&after_service_status, 1);
			}

		} else {
			if (service_status != NULL) {
				Free_Memory_Engine_Status(&service_status, 1);
			}
			_DEBUG_INFO(" non service engine ing");
		}

	} else {
		_DEBUG_INFO(" non service engine ");
	}

	_EXTERN_FUNC_EXIT;
	return ret;

 error:
	if (service_status != NULL) {
		Free_Memory_Engine_Status(&service_status, 1);
	}

	if (after_service_status != NULL) {
		Free_Memory_Engine_Status(&after_service_status, 1);
	}

	str_free(&full_path);
	str_free(&correlator);
	str_free(&server_id);
	_DEBUG_INFO(" error end %d ", ret);
	_EXTERN_FUNC_EXIT;
	return ret;
}

DM_ERROR set_engine_status_idle(ENGINE_ID service_engine_id)
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	if (IsExist_Engine_id(service_engine_id) == 1) {
		_DEBUG_INFO("service engine id = %d\n", service_engine_id);
	}

	ret = set_end_engine_status_idle();
	if (ret != DM_OK)
		goto error;

	_EXTERN_FUNC_EXIT;
	return ret;
 error:
	_DEBUG_INFO(" error end %d ", ret);
	_EXTERN_FUNC_EXIT;
	return ret;
}

DM_ERROR set_end_engine_status_idle()
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	int common_engine_status = DM_IDLE;
	_DEBUG_INFO(" engine idle");
	ret = Update_Engine_Status_Column(IDLE_ENGINE, VALUE_ENGINE_STATUS, &common_engine_status);
	if (ret != DM_OK)
		goto error;

	_EXTERN_FUNC_EXIT;
	return ret;

 error:
	_DEBUG_INFO(" error end %d ", ret);
	_EXTERN_FUNC_EXIT;
	return ret;
}

DM_ERROR dm_common_start(Event_Contents * pEvent_data)
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	retvm_if((pEvent_data) == NULL, COMMON_ERR_IS_NULL, "pEvent_data is NULL!!");

	ret = dm_common_operation(pEvent_data);
	if (ret != DM_OK) {
		goto error;
	}

	_EXTERN_FUNC_EXIT;
	return ret;
 error:

	_DEBUG_INFO(" end!! error : %d\n", ret);
	_EXTERN_FUNC_EXIT;
	return ret;

}

DM_ERROR dm_common_operation(Event_Contents * pEvent_data)
{
	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;

	retvm_if((pEvent_data) == NULL, COMMON_ERR_IS_NULL, "pEvent_data is NULL!!");

	int return_ret = 0;
	return_ret = remove(OMA_DM_MSG_PATH);
	_DEBUG_INFO(" result file remove /tmp/dm.txt =[%d]\n", return_ret);

	DM_ERROR service_ret = DM_OK;
	ENGINE_ID service_engine_id = NO_SERVICE_ENGINE;
	char *session_id = NULL;
	char *server_id = NULL;
	int session_type;

	ret = get_server_info(pEvent_data, &server_id, &session_id, &session_type);
	if (ret != DM_OK) {
		goto error;
	}

	get_service_engine_id(&service_engine_id, pEvent_data->ui_mode);

	/*pkg 1 ~ pkg2 */
	int isFinish = 0;
	int isgeneticAlert = 0;
	void *session = NULL;
	_DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
	_DEBUG_INFO(" setupphase Msg \n");
	_DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
	ret = setup_phase(1, (Session **) & session, server_id, session_id, session_type, &service_engine_id, &isFinish, isgeneticAlert);
	if (ret != DM_OK) {
		_DEBUG_INFO("connect to server fail in dm common operation (authentication fail or network fail)");
		_DEBUG_INFO(" result =[%d]\n", ret);
		service_ret = ret;
		goto error;
	}

	while (!isFinish) {

		/* pkg 3 ~ pkg 4 */
		/*isFinish = 0; */
		_DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
		_DEBUG_INFO(" management Msg \n");
		_DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
		ret = management_phase(1, (Session **) (&session), NULL, &service_engine_id, &isFinish);
		if (ret != DM_OK) {
			service_ret = ret;
			goto error;
		}

		_DEBUG_INFO("isFinish : %d\n", isFinish);
		_DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");

		ret = service_engine_start(service_engine_id, &service_ret);
		if (ret != DM_OK)
			goto error;

		ret = genericalert_operation(&session, &isFinish, service_engine_id);
		if (ret != DM_OK)
			goto error;

		set_engine_status_idle(service_engine_id);
	}

	terminate_oma_dm_ui(service_ret, service_engine_id);

	_EXTERN_FUNC_EXIT;
	return ret;

 error:

	terminate_oma_dm_ui(service_ret, service_engine_id);

	if (ret == DM_ERR_OPERATION_CANCELLED && service_ret == DM_ERR_OPERATION_CANCELLED) {
		Delete_Engine_Status(service_engine_id);
		_DEBUG_INFO("Delete fumo service engine");
	}

	set_engine_status_idle(service_engine_id);

	if (service_engine_id != NO_SERVICE_ENGINE) {

		if (service_engine_id != FUMO_SERVICE_ENGINE && check_existed_fumo_reminder_interval() == 0) {
			Delete_Engine_Status(service_engine_id);
			_DEBUG_INFO("Delete fumo service engine");
		}
	}

	_DEBUG_INFO(" end error ret : %d\n", ret);
	_EXTERN_FUNC_EXIT;
	return ret;
}

DM_ERROR genericalert_operation(void **session, int *isFinish, ENGINE_ID service_engine_id)
{

	_EXTERN_FUNC_ENTER;

	DM_ERROR ret = DM_OK;
	char *pServer_id = NULL;
	engine_status *status = NULL;

	if (IsExist_Engine_id(service_engine_id) == 1) {
		ret = Get_Engine_Status(service_engine_id, &status);
		if (ret != DM_OK)
			goto error;
		_DEBUG_INFO(" result =[%d]\n", ret);

		_DEBUG_INFO("server id : %s", status->server_id);
		_DEBUG_INFO("engine_id : %d", status->engine_id);
		_DEBUG_INFO("correlator : %s", status->correlator);
		_DEBUG_INFO("engine_status : %d", status->engine_status);
		_DEBUG_INFO("result status : %d", status->result_status);
		_DEBUG_INFO("mo path : %s", status->mo_path);
		_DEBUG_INFO("server url : %s", status->server_url);

		if (status->engine_status == DM_GENERICALERT_ING) {
			_DEBUG_INFO(" ----------------------------------------------------------------------generic alert-------------------------------------------------------------------------------");
			_DEBUG_INFO("service engine id = %d\n", service_engine_id);

			isFinish = 0;
			int isgeneticAlert = 1;

			if (status->server_id != NULL) {
				pServer_id = strdup(status->server_id);
				_DEBUG_INFO(" server id is %s", status->server_id);
			} else {
				_DEBUG_INFO(" server id is NULL");
				ret = COMMON_ERR_IS_NULL;
				goto error;
			}
			//void * session = NULL;

			ret = generic_alert(1, (Session **) session, pServer_id, NULL, isFinish, isgeneticAlert, service_engine_id, status->result_status);
			if (ret != DM_OK)
				goto error;
			_DEBUG_INFO(" result =[%d]\n", ret);

			/*update engine_status */
			status->engine_status = DM_SERVICE_UNKNOWN;
			ret = Update_Engine_Status(status);
			_DEBUG_INFO(" update engine status : %d \n", ret);

			str_free(&pServer_id);
		}
	} else {
		_DEBUG_INFO("non generic alert");
	}

	if (status != NULL) {
		Free_Memory_Engine_Status(&status, 1);
	}

	_EXTERN_FUNC_EXIT;
	return ret;

 error:

	str_free(&pServer_id);
	if (status != NULL)
		Free_Memory_Engine_Status(&status, 1);

	_DEBUG_INFO(" error end  ret : %d\n", ret);
	_EXTERN_FUNC_EXIT;
	return ret;

}