Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / agent / dm-engine / dm_common_engine.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 <stdio.h>
20
21 /*sync-agent*/
22 #include <sync_agent.h>
23
24 /*dm-agent*/
25 #include "common/dm_common_def.h"
26 #include "common/dm_common.h"
27 #include "common/util/util.h"
28 #include "framework/task/oma_dm_task_request.h"
29 #include "framework/san-parser/pm_sanparser.h"
30 #include "dm-engine/dm_common_engine.h"
31 #include "dm-engine/fumo/fumo_engine.h"
32 #include "dm-engine/lawmo/lawmo_engine.h"
33 #include "mo-handler/dm_mo_common_internal.h"
34 #include "mo-handler/dm_mo_common.h"
35 #include "serviceadapter/dm-phase-handler/dm_phase_handler.h"
36 #include "serviceadapter/sa_define.h"
37 #include "serviceadapter/sa_session.h"
38 #include "serviceadapter/sa_session_internal.h"
39 #include "ipc_common.h"
40 #include "ipc_agent.h"
41
42 #ifndef OMADM_AGENT_LOG
43 #undef LOG_TAG
44 #define LOG_TAG "COMMON_ENGINE"
45 #endif
46
47 static DM_ERROR _change_engine_status(char *server_id, char *full_path, char *correlator, int service_engine_status);
48
49 static DM_ERROR _change_engine_status(char *server_id, char *full_path, char *correlator, int service_engine_status)
50 {
51         _INNER_FUNC_ENTER;
52         DM_ERROR ret = DM_OK;
53
54         retvm_if((server_id) == NULL, COMMON_ERR_IS_NULL, "full_path is NULL!!");
55         retvm_if((full_path) == NULL, COMMON_ERR_IS_NULL, "full_path is NULL!!");
56
57         int task_id;
58         task_id = sync_agent_get_self_request_id();
59         _DEBUG_TRACE("service engine db update");
60
61         engine_status *status;
62
63         int is_exist;
64         is_exist = IsExist_Engine_id(service_engine_status);
65
66         if (is_exist == 1) {
67                 ret = Get_Engine_Status(service_engine_status, &status);
68                 _DEBUG_TRACE("get engine status : %d", ret);
69         } else {
70                 status = (engine_status *) calloc(1, sizeof(engine_status));
71                 _DEBUG_TRACE("get engine status error");
72         }
73
74         if (status == NULL) {
75                 _DEBUG_TRACE("calloc failed !!");
76                 ret = COMMON_ERR_ALLOC;
77                 goto error;
78         }
79
80         if (correlator != NULL) {
81                 status->correlator = strdup(correlator);
82         } else {
83                 status->correlator = NULL;
84         }
85
86         status->engine_status = DM_SERVICE_ING;
87         status->engine_id = service_engine_status;
88         status->mo_path = strdup(full_path);
89         status->server_url = NULL;
90         status->result_status = DM_OK;
91         status->server_id = strdup(server_id);
92         status->task_id = task_id;
93         status->ui_noti_type = 0;
94         status->download_click = 0;
95         //status->ui_mode = ;
96
97         /* one engine */
98         /*      if(IsExist_Engine_id(SERVICE_ENGINE)) {
99            ret = Delete_Engine_Status(SERVICE_ENGINE);
100            } */
101         ret = Update_Engine_Status(status);
102         _DEBUG_TRACE(" update engine status : %d \n", ret);
103
104         if (status != NULL)
105                 Free_Memory_Engine_Status(&status, 1);
106
107         if (ret != DM_OK) {
108 //              DB_End_Transaction(TRANSACTION_ROLLBACK_);
109                 goto error;
110         }
111         //sleep(10);
112 /*      ret = Update_Engine_Status_Column(IDLE_ENGINE, VALUE_ENGINE_STATUS, &service_engine_status);
113         _DEBUG_TRACE(" update engine status : %d \n", ret);
114         if (ret != DM_OK) {
115                 DB_End_Transaction(TRANSACTION_ROLLBACK_);
116                 goto error;
117         }*/
118
119         _INNER_FUNC_EXIT;
120         return ret;
121  error:
122
123         _DEBUG_TRACE(" end error : %d \n", ret);
124         _INNER_FUNC_EXIT;
125         return ret;
126 }
127
128 void init_Dm_Engine()
129 {
130         _EXTERN_FUNC_ENTER;
131
132         /* always rebooting operation but excepted bootstrap */
133         reset_devinfo();
134         reset_devdetail();
135
136         _EXTERN_FUNC_EXIT;
137 }
138
139 DM_ERROR engine_start(char *server_id, char *full_path, char *correlator, ENGINE_ID * service_engine_id, int *resultCode)
140 {
141         _EXTERN_FUNC_ENTER;
142
143         DM_ERROR ret = DM_OK;
144
145         if (server_id == NULL || full_path == NULL) {
146                 (*resultCode) = CLIENT_ERROR;
147                 ret = COMMON_ERR_IS_NULL;
148                 goto error;
149         }
150
151         sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
152         sync_agent_dm_mo_type_e mo_type;
153
154         err_code = sync_agent_get_mo_type(full_path, &mo_type);
155         _DEBUG_INFO("get mo type result : %d", err_code);
156
157         (*resultCode) = DM_ERR_ACCEPTED_FOR_PROCESSING;
158
159         _DEBUG_INFO(" ------------------------------------------------------------------------------------------------------------------------------------- ");
160         _DEBUG_INFO(" mo_type : %d , server id : %s , full_path : %s , correlator : %s  ", mo_type, server_id, full_path, correlator);
161         _DEBUG_INFO(" ------------------------------------------------------------------------------------------------------------------------------------- ");
162
163         switch (mo_type) {
164         case SYNC_AGENT_DM_MO_TYPE_FUMO:
165
166                 _DEBUG_INFO("---------------------------------------------------------------- fumo engine ready --------------------------------------------------------------------- ");
167                 ret = _change_engine_status(server_id, full_path, correlator, FUMO_SERVICE_ENGINE);
168                 if (ret != DM_OK)
169                         goto error;
170
171                 if (correlator == NULL) {
172                         /*because of gcf server */
173                         /*ret = fumo_exec(full_path, correlator, (FUMO_Error *)resultCode);
174                            if(ret !=DM_OK)
175                            goto error; */
176                 }
177
178                 *service_engine_id = FUMO_SERVICE_ENGINE;
179
180                 break;
181         case SYNC_AGENT_DM_MO_TYPE_LAWMO:
182                 _DEBUG_INFO("---------------------------------------------------------------- lawmo engine ready --------------------------------------------------------------------- ");
183                 ret = _change_engine_status(server_id, full_path, correlator, LAWMO_SERVICE_ENGINE);
184                 if (ret != DM_OK)
185                         goto error;
186
187                 //if( !correlator) {
188                 // 1 : this is just check , available operation
189                 ret = lawmo_exec(full_path, correlator, (LAWMO_Result_code *) resultCode, CHECK_OPERATION);
190                 if (ret != DM_OK)
191                         goto error;
192                 //}
193
194                 *service_engine_id = LAWMO_SERVICE_ENGINE;
195
196                 break;
197         default:
198                 (*resultCode) = CLIENT_ERROR;
199                 _DEBUG_INFO(" not existed engine \n");
200                 break;
201         }
202
203         _DEBUG_INFO("====================result code : %d====================\n", *resultCode);
204         _DEBUG_INFO(" end!!  resut code : %d\n", *resultCode);
205         _EXTERN_FUNC_EXIT;
206         return ret;
207  error:
208
209         _DEBUG_INFO("====================result code : %d====================\n", *resultCode);
210         _DEBUG_INFO(" error end %d  result code : %d", ret, *resultCode);
211         _EXTERN_FUNC_EXIT;
212         return ret;
213 }
214
215 DM_ERROR service_engine_start(ENGINE_ID service_engine_id, DM_ERROR * service_ret)
216 {
217         _EXTERN_FUNC_ENTER;
218
219         _DEBUG_INFO(" start : service_engine_id : %d", service_engine_id);
220         DM_ERROR ret = DM_OK;
221         engine_status *service_status = NULL;
222         engine_status *after_service_status = NULL;
223         char *full_path = NULL;
224         char *correlator = NULL;
225         char *server_id = NULL;
226         int service_engine_status;
227         bool cancel_flag = 0;
228
229         cancel_flag = sync_agent_check_cancel_flag();
230         if (cancel_flag != 0) {
231                 ret = DM_ERR_OPERATION_CANCELLED;
232                 goto error;
233         }
234
235         if (IsExist_Engine_id(service_engine_id) == 1) {
236
237                 _DEBUG_INFO("service engine id = %d\n", service_engine_id);
238                 _DEBUG_INFO(" engine must re-start");
239
240                 ret = Get_Engine_Status(service_engine_id, &service_status);
241                 _DEBUG_INFO(" get engine status : %d \n", ret);
242                 if (ret != DM_OK)
243                         goto error;
244
245                 if (service_status == NULL) {
246                         _DEBUG_INFO("service status is null");
247                         ret = COMMON_ERR_IS_NULL;
248                         goto error;
249                 }
250
251                 if (service_status->engine_status == DM_SERVICE_UNKNOWN) {
252                         _DEBUG_INFO("engine status unknown");
253                         goto error;
254                 }
255
256                 if (service_status->engine_status == DM_SERVICE_ING) {
257                         _DEBUG_INFO("-------------------------------------------- service type ------------------------------------------------ : %d", service_engine_id);
258
259                         int engine_return = OPERATION_SUCCEEDED;
260
261                         if (service_status->mo_path != NULL) {
262                                 full_path = strdup(service_status->mo_path);
263                                 _DEBUG_INFO("full path : %s ", full_path);
264                         } else {
265                                 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
266                                 goto error;
267                         }
268
269                         if (service_status->server_id != NULL) {
270                                 server_id = strdup(service_status->server_id);
271                                 _DEBUG_INFO("server_id : %s", server_id);
272                         } else {
273                                 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
274                                 goto error;
275                         }
276
277                         if (service_status->correlator != NULL) {
278                                 correlator = strdup(service_status->correlator);
279                                 _DEBUG_INFO("correlator : %s", correlator);
280                         }
281
282                         _DEBUG_INFO("service status : %d", service_status->engine_status);
283
284                         switch (service_engine_id) {
285                         case FUMO_SERVICE_ENGINE:
286                                 _DEBUG_INFO(" fumo exec");
287                                 ret = fumo_exec(full_path, correlator, (FUMO_Error *) (&engine_return));
288                                 _DEBUG_INFO("fumo ret : %d", ret);
289                                 *service_ret = ret;
290                                 if (ret != DM_OK && ret != DM_ERR_USER_CANDELLED && ret != DM_ERR_REMINDER_INTERVAL /*|| ret != DM_ERR_DEVICE_ABORTED */ )
291                                         goto error;
292                                 break;
293                         case LAWMO_SERVICE_ENGINE:
294                                 _DEBUG_INFO(" lawmo exec");
295                                 ret = lawmo_exec(full_path, correlator, (LAWMO_Result_code *) (&engine_return), NON_CHECK_OPERATION);
296                                 _DEBUG_INFO("lawmo ret : %d", ret);
297                                 *service_ret = ret;
298                                 if (ret != DM_OK)
299                                         goto error;
300                                 break;
301                         default:
302                                 break;
303                         }
304                         _DEBUG_INFO("exe resutlt : %d , server id : %s", engine_return, server_id);
305                         _DEBUG_INFO("correlator : %s", correlator);
306
307                         ret = Get_Engine_Status(service_engine_id, &after_service_status);
308                         _DEBUG_INFO(" get engine status : %d \n", ret);
309                         if (ret != DM_OK)
310                                 goto error;
311
312                         if (after_service_status == NULL) {
313                                 _DEBUG_INFO("service status is null");
314                                 ret = COMMON_ERR_IS_NULL;
315                                 goto error;
316                         }
317
318                         if (after_service_status->download_click != RESUME_STATUS_INSTALL_BEFORE) {
319                                 /*engine status generic alert status */
320                                 service_engine_status = DM_GENERICALERT_ING;
321                                 ret = Update_Engine_Status_Column(service_engine_id, VALUE_ENGINE_STATUS, &service_engine_status);
322                                 _DEBUG_INFO("service engine status generic alert");
323
324                                 ret = Update_Engine_Status_Column(service_engine_id, VALUE_RESULT_STATUS, &engine_return);
325                                 _DEBUG_INFO("service engine status generic alert status : %d", engine_return);
326                         }
327
328                         str_free(&full_path);
329                         str_free(&correlator);
330                         str_free(&server_id);
331
332                         if (service_status != NULL) {
333                                 Free_Memory_Engine_Status(&service_status, 1);
334                         }
335                         if (after_service_status != NULL) {
336                                 Free_Memory_Engine_Status(&after_service_status, 1);
337                         }
338
339                 } else {
340                         if (service_status != NULL) {
341                                 Free_Memory_Engine_Status(&service_status, 1);
342                         }
343                         _DEBUG_INFO(" non service engine ing");
344                 }
345
346         } else {
347                 _DEBUG_INFO(" non service engine ");
348         }
349
350         _EXTERN_FUNC_EXIT;
351         return ret;
352
353  error:
354         if (service_status != NULL) {
355                 Free_Memory_Engine_Status(&service_status, 1);
356         }
357
358         if (after_service_status != NULL) {
359                 Free_Memory_Engine_Status(&after_service_status, 1);
360         }
361
362         str_free(&full_path);
363         str_free(&correlator);
364         str_free(&server_id);
365         _DEBUG_INFO(" error end %d ", ret);
366         _EXTERN_FUNC_EXIT;
367         return ret;
368 }
369
370 DM_ERROR set_engine_status_idle(ENGINE_ID service_engine_id)
371 {
372         _EXTERN_FUNC_ENTER;
373
374         DM_ERROR ret = DM_OK;
375
376         if (IsExist_Engine_id(service_engine_id) == 1) {
377                 _DEBUG_INFO("service engine id = %d\n", service_engine_id);
378         }
379
380         ret = set_end_engine_status_idle();
381         if (ret != DM_OK)
382                 goto error;
383
384         _EXTERN_FUNC_EXIT;
385         return ret;
386  error:
387         _DEBUG_INFO(" error end %d ", ret);
388         _EXTERN_FUNC_EXIT;
389         return ret;
390 }
391
392 DM_ERROR set_end_engine_status_idle()
393 {
394         _EXTERN_FUNC_ENTER;
395
396         DM_ERROR ret = DM_OK;
397
398         int common_engine_status = DM_IDLE;
399         _DEBUG_INFO(" engine idle");
400         ret = Update_Engine_Status_Column(IDLE_ENGINE, VALUE_ENGINE_STATUS, &common_engine_status);
401         if (ret != DM_OK)
402                 goto error;
403
404         _EXTERN_FUNC_EXIT;
405         return ret;
406
407  error:
408         _DEBUG_INFO(" error end %d ", ret);
409         _EXTERN_FUNC_EXIT;
410         return ret;
411 }
412
413 DM_ERROR dm_common_start(Event_Contents * pEvent_data)
414 {
415         _EXTERN_FUNC_ENTER;
416
417         DM_ERROR ret = DM_OK;
418
419         retvm_if((pEvent_data) == NULL, COMMON_ERR_IS_NULL, "pEvent_data is NULL!!");
420
421         ret = dm_common_operation(pEvent_data);
422         if (ret != DM_OK) {
423                 goto error;
424         }
425
426         _EXTERN_FUNC_EXIT;
427         return ret;
428  error:
429
430         _DEBUG_INFO(" end!! error : %d\n", ret);
431         _EXTERN_FUNC_EXIT;
432         return ret;
433
434 }
435
436 DM_ERROR dm_common_operation(Event_Contents * pEvent_data)
437 {
438         _EXTERN_FUNC_ENTER;
439
440         DM_ERROR ret = DM_OK;
441
442         retvm_if((pEvent_data) == NULL, COMMON_ERR_IS_NULL, "pEvent_data is NULL!!");
443
444         int return_ret = 0;
445         return_ret = remove(OMA_DM_MSG_PATH);
446         _DEBUG_INFO(" result file remove /tmp/dm.txt =[%d]\n", return_ret);
447
448         DM_ERROR service_ret = DM_OK;
449         ENGINE_ID service_engine_id = NO_SERVICE_ENGINE;
450         char *session_id = NULL;
451         char *server_id = NULL;
452         int session_type;
453
454         ret = get_server_info(pEvent_data, &server_id, &session_id, &session_type);
455         if (ret != DM_OK) {
456                 goto error;
457         }
458
459         get_service_engine_id(&service_engine_id, pEvent_data->ui_mode);
460
461         /*pkg 1 ~ pkg2 */
462         int isFinish = 0;
463         int isgeneticAlert = 0;
464         void *session = NULL;
465         _DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
466         _DEBUG_INFO(" setupphase Msg \n");
467         _DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
468         ret = setup_phase(1, (Session **) & session, server_id, session_id, session_type, &service_engine_id, &isFinish, isgeneticAlert);
469         if (ret != DM_OK) {
470                 _DEBUG_INFO("connect to server fail in dm common operation (authentication fail or network fail)");
471                 _DEBUG_INFO(" result =[%d]\n", ret);
472                 service_ret = ret;
473                 goto error;
474         }
475
476         while (!isFinish) {
477
478                 /* pkg 3 ~ pkg 4 */
479                 /*isFinish = 0; */
480                 _DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
481                 _DEBUG_INFO(" management Msg \n");
482                 _DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
483                 ret = management_phase(1, (Session **) (&session), NULL, &service_engine_id, &isFinish);
484                 if (ret != DM_OK) {
485                         service_ret = ret;
486                         goto error;
487                 }
488
489                 _DEBUG_INFO("isFinish : %d\n", isFinish);
490                 _DEBUG_INFO("----------------------------------------------------------------------------------------------------------\n");
491
492                 ret = service_engine_start(service_engine_id, &service_ret);
493                 if (ret != DM_OK)
494                         goto error;
495
496                 ret = genericalert_operation(&session, &isFinish, service_engine_id);
497                 if (ret != DM_OK)
498                         goto error;
499
500                 set_engine_status_idle(service_engine_id);
501         }
502
503         terminate_oma_dm_ui(service_ret, service_engine_id);
504
505         _EXTERN_FUNC_EXIT;
506         return ret;
507
508  error:
509
510         terminate_oma_dm_ui(service_ret, service_engine_id);
511
512         if (ret == DM_ERR_OPERATION_CANCELLED && service_ret == DM_ERR_OPERATION_CANCELLED) {
513                 Delete_Engine_Status(service_engine_id);
514                 _DEBUG_INFO("Delete fumo service engine");
515         }
516
517         set_engine_status_idle(service_engine_id);
518
519         if (service_engine_id != NO_SERVICE_ENGINE) {
520
521                 if (service_engine_id != FUMO_SERVICE_ENGINE && check_existed_fumo_reminder_interval() == 0) {
522                         Delete_Engine_Status(service_engine_id);
523                         _DEBUG_INFO("Delete fumo service engine");
524                 }
525         }
526
527         _DEBUG_INFO(" end error ret : %d\n", ret);
528         _EXTERN_FUNC_EXIT;
529         return ret;
530 }
531
532 DM_ERROR genericalert_operation(void **session, int *isFinish, ENGINE_ID service_engine_id)
533 {
534
535         _EXTERN_FUNC_ENTER;
536
537         DM_ERROR ret = DM_OK;
538         char *pServer_id = NULL;
539         engine_status *status = NULL;
540
541         if (IsExist_Engine_id(service_engine_id) == 1) {
542                 ret = Get_Engine_Status(service_engine_id, &status);
543                 if (ret != DM_OK)
544                         goto error;
545                 _DEBUG_INFO(" result =[%d]\n", ret);
546
547                 _DEBUG_INFO("server id : %s", status->server_id);
548                 _DEBUG_INFO("engine_id : %d", status->engine_id);
549                 _DEBUG_INFO("correlator : %s", status->correlator);
550                 _DEBUG_INFO("engine_status : %d", status->engine_status);
551                 _DEBUG_INFO("result status : %d", status->result_status);
552                 _DEBUG_INFO("mo path : %s", status->mo_path);
553                 _DEBUG_INFO("server url : %s", status->server_url);
554
555                 if (status->engine_status == DM_GENERICALERT_ING) {
556                         _DEBUG_INFO(" ----------------------------------------------------------------------generic alert-------------------------------------------------------------------------------");
557                         _DEBUG_INFO("service engine id = %d\n", service_engine_id);
558
559                         isFinish = 0;
560                         int isgeneticAlert = 1;
561
562                         if (status->server_id != NULL) {
563                                 pServer_id = strdup(status->server_id);
564                                 _DEBUG_INFO(" server id is %s", status->server_id);
565                         } else {
566                                 _DEBUG_INFO(" server id is NULL");
567                                 ret = COMMON_ERR_IS_NULL;
568                                 goto error;
569                         }
570                         //void * session = NULL;
571
572                         ret = generic_alert(1, (Session **) session, pServer_id, NULL, isFinish, isgeneticAlert, service_engine_id, status->result_status);
573                         if (ret != DM_OK)
574                                 goto error;
575                         _DEBUG_INFO(" result =[%d]\n", ret);
576
577                         /*update engine_status */
578                         status->engine_status = DM_SERVICE_UNKNOWN;
579                         ret = Update_Engine_Status(status);
580                         _DEBUG_INFO(" update engine status : %d \n", ret);
581
582                         str_free(&pServer_id);
583                 }
584         } else {
585                 _DEBUG_INFO("non generic alert");
586         }
587
588         if (status != NULL) {
589                 Free_Memory_Engine_Status(&status, 1);
590         }
591
592         _EXTERN_FUNC_EXIT;
593         return ret;
594
595  error:
596
597         str_free(&pServer_id);
598         if (status != NULL)
599                 Free_Memory_Engine_Status(&status, 1);
600
601         _DEBUG_INFO(" error end  ret : %d\n", ret);
602         _EXTERN_FUNC_EXIT;
603         return ret;
604
605 }