Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / initialization / parser.c
1 /*
2  * sync-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 #include <dlfcn.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
21
22 #include "utility/sync_util.h"
23 #include "utility/fw_mainloop.h"
24
25 #include "engine-controller/internal.h"
26
27 #include "data-adapter/agent.h"
28 #include "data-adapter/agent_handler_manager.h"
29 #include "data-adapter/agent_handler_manager_internal.h"
30
31 #include "utility/fw_sequential_id_provider.h"
32
33 #include "event/handler.h"
34 #include "event/config.h"
35
36 #include "plugin/struct.h"
37 #include "plugin/data_connector_plugin.h"
38 #include "plugin/data_converter_plugin.h"
39 #include "plugin/account_plugin.h"
40 #include "plugin/network_access_plugin.h"
41 #include "plugin/device_info_plugin.h"
42 #include "plugin/platform_monitor_plugin.h"
43 #include "plugin/mo_plugin.h"
44 #include "plugin/manager.h"
45 #include "plugin/device_manager_plugin.h"
46
47 #include "device-manager/mo_error.h"
48 #include "device-manager/mo_database_handler.h"
49
50 #include "device/information.h"
51
52 #include "network-access/callbacks.h"
53
54 #include "initialization/parser.h"
55
56 #ifndef SYNC_AGENT_LOG
57 #undef LOG_TAG
58 #define LOG_TAG "AF_INIT"
59 #endif
60
61 static char *agent_key = NULL;
62
63 static int use_main_loop = 0;
64
65 static sync_agent_init_error_e _process_node(xmlNode * root_element);
66
67 static sync_agent_deinit_error_e _process_node_deinit(xmlNode * root_element);
68
69 static sync_agent_init_error_e __process_engine_controller(xmlNode * engine_controller_node);
70
71 static sync_agent_init_error_e __process_framework_db(xmlNode * framework_db_node);
72
73 static sync_agent_init_error_e __process_id_provider(xmlNode * id_provider_node);
74
75 static sync_agent_init_error_e __process_event(xmlNode * event_node);
76
77 static sync_agent_init_error_e __process_noti(xmlNode * event_node);
78
79 static sync_agent_init_error_e __process_plugin(xmlNode * plugin_mgr_node);
80
81 static sync_agent_deinit_error_e __process_plugin_deinit(xmlNode * plugin_mgr_node);
82
83 static sync_agent_init_error_e __process_device_manage(xmlNode * device_manage_node);
84
85 static char *__get_child_node_content(xmlNode * parent_node, const char *node_name);
86
87 static int __compare_uchar_with_char(const xmlChar * str, const char *str2);
88
89 sync_agent_init_error_e init_parse_init_config(const char *init_config_file_path)
90 {
91         _EXTERN_FUNC_ENTER;
92
93         xmlDoc *doc = NULL;
94         xmlNode *root_element = NULL;
95
96         doc = xmlReadFile(init_config_file_path, 0, 0);
97         if (doc == NULL) {
98                 _DEBUG_ERROR("Failed to parse %s", init_config_file_path);
99                 return SYNC_AGENT_INIT_FAIL;
100         }
101
102         root_element = xmlDocGetRootElement(doc);
103
104         sync_agent_init_error_e init_err = _process_node(root_element);
105
106         xmlFreeDoc(doc);
107
108         _DEBUG_INFO("[End]");
109
110         _EXTERN_FUNC_EXIT;
111
112         return init_err;
113 }
114
115 sync_agent_deinit_error_e deinit_parse_config(const char *config_file_path)
116 {
117         _EXTERN_FUNC_ENTER;
118
119         xmlDoc *doc = NULL;
120         xmlNode *root_element = NULL;
121
122         doc = xmlReadFile(config_file_path, 0, 0);
123         if (doc == NULL) {
124                 _DEBUG_ERROR("Failed to parse %s", config_file_path);
125                 return SYNC_AGENT_DEINIT_FAIL;
126         }
127
128         root_element = xmlDocGetRootElement(doc);
129
130         sync_agent_deinit_error_e deinit_err = _process_node_deinit(root_element);
131
132         xmlFreeDoc(doc);
133
134         _DEBUG_INFO("[End]");
135
136         _EXTERN_FUNC_EXIT;
137
138         return deinit_err;
139 }
140
141 char *init_get_agent_key(void)
142 {
143         _EXTERN_FUNC_ENTER;
144
145         _EXTERN_FUNC_EXIT;
146
147         return agent_key;
148 }
149
150 int init_use_main_loop(void)
151 {
152         _EXTERN_FUNC_ENTER;
153
154         _EXTERN_FUNC_EXIT;
155
156         return use_main_loop;
157 }
158
159 static sync_agent_init_error_e _process_node(xmlNode * root_element)
160 {
161         _INNER_FUNC_ENTER;
162
163         sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
164
165         if (root_element == NULL) {
166                 return SYNC_AGENT_INIT_FAIL;
167         }
168
169         xmlNode *cursor_node = root_element->children;
170         while (cursor_node != NULL) {
171                 if (cursor_node->type == XML_ELEMENT_NODE) {
172                         _DEBUG_TRACE("%s", cursor_node->name);
173                         if (__compare_uchar_with_char(cursor_node->name, "Agent-Key")) {
174                                 agent_key = (char *)xmlNodeGetContent(cursor_node);
175                                 _DEBUG_TRACE("Agent-Key : %s", agent_key);
176                         } else if (__compare_uchar_with_char(cursor_node->name, "MainLoop")) {
177                                 xmlChar *main_loop = xmlNodeGetContent(cursor_node);
178                                 if (__compare_uchar_with_char(main_loop, "1")) {
179                                         use_main_loop = 1;
180                                 }
181                         } else if (__compare_uchar_with_char(cursor_node->name, "EngineController")) {
182                                 init_error = __process_engine_controller(cursor_node);
183                         } else if (__compare_uchar_with_char(cursor_node->name, "FrameworkDB")) {
184                                 init_error = __process_framework_db(cursor_node);
185                         } else if (__compare_uchar_with_char(cursor_node->name, "ID-Provider")) {
186                                 init_error = __process_id_provider(cursor_node);
187                         } else if (__compare_uchar_with_char(cursor_node->name, "Event")) {
188                                 init_error = __process_event(cursor_node);
189                         } else if (__compare_uchar_with_char(cursor_node->name, "Noti")) {
190                                 init_error = __process_noti(cursor_node);
191                         } else if (__compare_uchar_with_char(cursor_node->name, "PlugIn-Mgr")) {
192                                 init_error = __process_plugin(cursor_node);
193                         } else if (__compare_uchar_with_char(cursor_node->name, "DeviceManage")) {
194                                 init_error = __process_device_manage(cursor_node);
195                         }
196                 }
197
198                 cursor_node = cursor_node->next;
199         }
200
201         _INNER_FUNC_EXIT;
202
203         return init_error;
204 }
205
206 static sync_agent_deinit_error_e _process_node_deinit(xmlNode * root_element)
207 {
208         _INNER_FUNC_ENTER;
209
210         sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
211
212         if (root_element == NULL) {
213                 return SYNC_AGENT_DEINIT_FAIL;
214         }
215
216         xmlNode *cursor_node = root_element->children;
217         while (cursor_node != NULL) {
218                 if (cursor_node->type == XML_ELEMENT_NODE) {
219                         _DEBUG_TRACE("%s", cursor_node->name);
220                         if (__compare_uchar_with_char(cursor_node->name, "Agent-Key")) {
221                                 agent_key = (char *)xmlNodeGetContent(cursor_node);
222                                 _DEBUG_TRACE("Agent-Key : %s", agent_key);
223                         } else if (__compare_uchar_with_char(cursor_node->name, "FrameworkDB")) {
224                                 sync_agent_da_return_e da_error = sync_agent_close_agent();
225                                 if (da_error != SYNC_AGENT_DA_SUCCESS) {
226                                         _DEBUG_ERROR("sync_agent_close_agent() failed !! [%d]", da_error);
227                                         return SYNC_AGENT_DEINIT_FAIL;
228                                 } else {
229                                         _DEBUG_TRACE("sync_agent_close_agent() success !!");
230                                 }
231
232                                 da_error = da_free_agentdb_handler_mgr();
233                                 if (da_error != SYNC_AGENT_DA_SUCCESS) {
234                                         _DEBUG_ERROR("da_free_agentdb_handler_mgr() failed !! [%d]", da_error);
235                                         return SYNC_AGENT_DEINIT_FAIL;
236                                 } else {
237                                         _DEBUG_TRACE("da_free_agentdb_handler_mgr() success !!");
238                                 }
239                         } else if (__compare_uchar_with_char(cursor_node->name, "Event") || __compare_uchar_with_char(cursor_node->name, "Noti")) {
240                                 sync_agent_event_error_e event_error = sync_agent_clean_event_handler();
241                                 if (event_error != SYNC_AGENT_EVENT_SUCCESS) {
242                                         _DEBUG_ERROR("sync_agent_clean_event_handler() failed !! [%d]", event_error);
243                                         return SYNC_AGENT_DEINIT_FAIL;
244                                 } else {
245                                         _DEBUG_TRACE("sync_agent_clean_event_handler() success !!");
246                                 }
247                         } else if (__compare_uchar_with_char(cursor_node->name, "PlugIn-Mgr")) {
248                                 deinit_error = __process_plugin_deinit(cursor_node);
249                                 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
250                                         _DEBUG_ERROR("__process_plugin_deinit() failed !! [%d]", deinit_error);
251                                         return deinit_error;
252                                 } else {
253                                         _DEBUG_TRACE("__process_plugin_deinit() success !!");
254                                 }
255                         } else if (__compare_uchar_with_char(cursor_node->name, "DeviceManage")) {
256                                 sync_agent_dm_mo_error_e mo_error = dm_mo_close();
257                                 if (mo_error != SYNC_AGENT_DM_MO_SUCCESS) {
258                                         _DEBUG_ERROR("Failed to dm_mo_close() : %d", mo_error);
259                                         return SYNC_AGENT_DEINIT_FAIL;
260                                 }
261
262                                 mo_error = dm_mo_free_modb_handler_mgr();
263                                 if (mo_error != SYNC_AGENT_DM_MO_SUCCESS) {
264                                         _DEBUG_ERROR("dm_mo_free_modb_handler_mgr() failed !! [%d]", mo_error);
265                                         return SYNC_AGENT_DEINIT_FAIL;
266                                 } else {
267                                         _DEBUG_TRACE("dm_mo_free_modb_handler_mgr() success !!");
268                                 }
269                         }
270                 }
271
272                 cursor_node = cursor_node->next;
273         }
274
275         _INNER_FUNC_EXIT;
276
277         return deinit_error;
278 }
279
280 /********************** Implement static function *************************/
281
282 static sync_agent_init_error_e __process_engine_controller(xmlNode * engine_controller_node)
283 {
284         _INNER_FUNC_ENTER;
285
286         retvm_if(engine_controller_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
287
288         xmlNode *max_thread = engine_controller_node->children;
289         if (max_thread == NULL) {
290                 return SYNC_AGENT_INIT_PARSER_FAIL;
291         }
292
293         char *max_thread_count_str = __get_child_node_content(engine_controller_node, "Max-Thread");
294         int max_thread_count = atoi(max_thread_count_str);
295
296         if (max_thread_count <= 0) {
297                 return SYNC_AGENT_INIT_ENGINE_CONTROLLER_FAIL;
298         }
299
300         bool init_success = ec_init_engine_controller(max_thread_count);
301         if (init_success) {
302                 _DEBUG_VERBOSE("Engine controller initialize success");
303         } else {
304                 _DEBUG_ERROR("Engine controller initialize fail");
305                 return SYNC_AGENT_INIT_ENGINE_CONTROLLER_FAIL;
306         }
307
308         _INNER_FUNC_EXIT;
309
310         return SYNC_AGENT_INIT_SUCCESS;
311 }
312
313 static sync_agent_init_error_e __process_framework_db(xmlNode * framework_db_node)
314 {
315         _INNER_FUNC_ENTER;
316
317         retvm_if(framework_db_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
318
319         const char *use_DB_str = (char *)xmlGetProp(framework_db_node, (xmlChar *) "use");
320         int use_DB = atoi(use_DB_str);
321         if (!use_DB) {
322                 _DEBUG_VERBOSE("Not Use Framework Common DB");
323                 return SYNC_AGENT_INIT_SUCCESS;
324         }
325
326         char *db_path = __get_child_node_content(framework_db_node, "Path");
327         _DEBUG_VERBOSE("Common DB Path : %s", db_path);
328
329         da_set_agent_db_file_path(db_path);
330
331         sync_agent_da_return_e da_err = da_alloc_agentdb_handler_mgr();
332         if (da_err != SYNC_AGENT_DA_SUCCESS) {
333                 return SYNC_AGENT_INIT_COMMON_DB_FAIL;
334         }
335
336         da_err = sync_agent_open_agent();
337         if (da_err != SYNC_AGENT_DA_SUCCESS) {
338                 return SYNC_AGENT_INIT_COMMON_DB_FAIL;
339         }
340
341         da_err = da_create_agent_default_table_wrapper();
342         if (da_err != SYNC_AGENT_DA_SUCCESS) {
343                 return SYNC_AGENT_INIT_COMMON_DB_FAIL;
344         }
345
346         _INNER_FUNC_EXIT;
347
348         return SYNC_AGENT_INIT_SUCCESS;
349 }
350
351 static sync_agent_init_error_e __process_id_provider(xmlNode * id_provider_node)
352 {
353         _INNER_FUNC_ENTER;
354
355         retvm_if(id_provider_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
356
357         char *code_str = __get_child_node_content(id_provider_node, "Code");
358         char *max_id_str = __get_child_node_content(id_provider_node, "MaxID");
359         char *page_size_str = __get_child_node_content(id_provider_node, "PageSize");
360
361         unsigned int code = atoi(code_str);
362         unsigned int id_max = atoi(max_id_str);
363         unsigned int log2_page_size = atoi(page_size_str);
364
365         util_id_provider_error_e error = util_create_id_persistent_provider(code, id_max, log2_page_size);
366         if (error == UTIL_ID_PROVIDER_OK) {
367                 _DEBUG_VERBOSE("Done sync_agent_create_id_persistent_provider : %d", error);
368         }
369
370         _INNER_FUNC_EXIT;
371
372         return SYNC_AGENT_INIT_SUCCESS;
373 }
374
375 static sync_agent_init_error_e __process_event(xmlNode * event_node)
376 {
377         _INNER_FUNC_ENTER;
378
379         retvm_if(event_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
380
381         char *event_key = __get_child_node_content(event_node, "Event-Key");
382         char *event_configfile = __get_child_node_content(event_node, "Event-Config");
383         xmlChar *run_eventHandler = (xmlChar *) __get_child_node_content(event_node, "RunEventHandler");
384
385         sync_agent_event_error_e event_err = event_set_event_spec_from_config_file(event_key, event_configfile);
386         if (event_err != SYNC_AGENT_EVENT_SUCCESS) {
387                 _DEBUG_ERROR("Fail Load event config");
388                 return SYNC_AGENT_INIT_SYNC_AGENT_EVENT_FAIL;
389         } else {
390                 _DEBUG_VERBOSE("Success Load event config");
391         }
392
393         if (__compare_uchar_with_char(run_eventHandler, "1")) {
394                 unsigned long int thread_id;
395                 event_err = sync_agent_run_event_handler(&thread_id);
396                 if (event_err != SYNC_AGENT_EVENT_SUCCESS) {
397                         _DEBUG_ERROR("Failed to sync_agent_run_event_handler()");
398                         return SYNC_AGENT_INIT_SYNC_AGENT_EVENT_FAIL;
399                 }
400         }
401
402         _INNER_FUNC_EXIT;
403
404         return SYNC_AGENT_INIT_SUCCESS;
405 }
406
407 static sync_agent_init_error_e __process_noti(xmlNode * event_node)
408 {
409         _INNER_FUNC_ENTER;
410
411         retvm_if(event_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
412
413         char *noti_key = __get_child_node_content(event_node, "Noti-Key");
414         char *noti_configfile = __get_child_node_content(event_node, "Noti-Config");
415
416         sync_agent_event_error_e event_err = event_set_noti_spec_from_config_file(noti_key, noti_configfile);
417         if (event_err != SYNC_AGENT_EVENT_SUCCESS) {
418                 _DEBUG_ERROR("Fail Load noti config : %s", noti_configfile);
419                 return SYNC_AGENT_INIT_SYNC_AGENT_EVENT_FAIL;
420         } else {
421                 _DEBUG_VERBOSE("Success Load noti config");
422         }
423
424         _INNER_FUNC_EXIT;
425
426         return SYNC_AGENT_INIT_SUCCESS;
427 }
428
429 static sync_agent_init_error_e __process_plugin(xmlNode * plugin_mgr_node)
430 {
431         _INNER_FUNC_ENTER;
432
433         retvm_if(plugin_mgr_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
434
435         xmlNode *domain_node = plugin_mgr_node->children;
436         while (domain_node != NULL) {
437                 if (domain_node->type == XML_ELEMENT_NODE) {
438                         const xmlChar *domain_name = xmlGetProp(domain_node, (xmlChar *) "name");
439                         const xmlChar *domain_count = xmlGetProp(domain_node, (xmlChar *) "count");
440                         int d_count = 0;
441                         if (domain_count != NULL) {
442                                 d_count = atoi((char *)domain_count);
443                         }
444                         _DEBUG_VERBOSE("Domain : %s ( count : %d ) =====================", domain_name, d_count);
445
446                         xmlNode *plugIn_node = domain_node->children;
447                         while (plugIn_node != NULL) {
448                                 if (plugIn_node->type == XML_ELEMENT_NODE) {
449                                         plugin_info_s plugIn_Info;
450
451                                         char *plugIn_ID = __get_child_node_content(plugIn_node, "ID");
452                                         char *plugIn_path = __get_child_node_content(plugIn_node, "Path");
453
454                                         plugIn_Info.plugin_id = atoi(plugIn_ID);
455
456                                         plugin_error_e plugIn_error;
457                                         plugIn_Info.plugin_handle = plugin_load_plugin(plugIn_path, &plugIn_error);
458                                         if (plugIn_error != PLUGIN_SUCCESS) {
459                                                 _DEBUG_ERROR("load_PlugIn Failed [%s]", plugIn_path);
460                                                 return SYNC_AGENT_INIT_PLUGIN_FAIL;
461                                         }
462
463                                         _DEBUG_VERBOSE("plugIn_ID : %d", plugIn_Info.plugin_id);
464                                         _DEBUG_VERBOSE("plugIn_Path : %s", plugIn_path);
465
466                                         if (__compare_uchar_with_char(domain_name, "DataConnector")) {
467                                                 plugin_data_connector_s plugIn_dc;
468                                                 char *dataConverter = __get_child_node_content(plugIn_node, "DataConverter-PlugIn");
469                                                 char *handleChangeNoti = __get_child_node_content(plugIn_node, "Handle-ChangeNoti");
470                                                 char *useMainLoop = __get_child_node_content(plugIn_node, "Use-MainLoop");
471
472                                                 plugIn_dc.plugin_info = plugIn_Info;
473                                                 plugIn_dc.data_converter_id = atoi(dataConverter);
474                                                 plugIn_dc.handle_change_noti = atoi(handleChangeNoti);
475                                                 plugIn_dc.use_main_loop = atoi(useMainLoop);
476                                                 plugIn_dc.func_set = plugin_get_data_connector_func_set(plugIn_Info.plugin_handle, &plugIn_error);
477
478                                                 plugin_register_plugin_data_connector(plugIn_dc);
479                                         } else if (__compare_uchar_with_char(domain_name, "DataConverter")) {
480                                                 plugin_data_converter_s plugIn_dataconverter;
481
482                                                 plugIn_dataconverter.plugin_info = plugIn_Info;
483                                                 plugIn_dataconverter.func_set = plugin_get_data_converter_func_set(plugIn_Info.plugin_handle, &plugIn_error);
484
485                                                 plugin_register_plugin_data_converter(plugIn_dataconverter);
486                                         } else if (__compare_uchar_with_char(domain_name, "Account")) {
487                                                 plugin_account_s plugIn_acc;
488
489                                                 plugIn_acc.plugin_info = plugIn_Info;
490                                                 plugIn_acc.func_set = plugin_get_plugin_account_func_set(plugIn_Info.plugin_handle, &plugIn_error);
491
492                                                 plugin_register_plugin_account(plugIn_acc);
493                                         } else if (__compare_uchar_with_char(domain_name, "NetworkAccess")) {
494                                                 plugin_network_access_s plugIn_na;
495
496                                                 plugIn_na.plugin_info = plugIn_Info;
497                                                 plugIn_na.func_set = plugin_get_network_access_func_set(plugIn_Info.plugin_handle, &plugIn_error);
498
499                                                 char *use_network = __get_child_node_content(plugIn_node, "UseNetwork");
500                                                 plugIn_na.use_network = atoi(use_network);
501
502                                                 plugin_register_plugin_network_access(plugIn_na);
503                                         } else if (__compare_uchar_with_char(domain_name, "DeviceInfo")) {
504                                                 plugin_device_info_s plugIn_device;
505
506                                                 plugIn_device.plugin_info = plugIn_Info;
507                                                 plugIn_device.func_set = plugin_get_device_info_func_set(plugIn_Info.plugin_handle, &plugIn_error);
508
509                                                 plugin_register_plugin_device_info(plugIn_device);
510
511                                                 char *depends_on = __get_child_node_content(plugIn_node, "DependsOn");
512                                                 if (depends_on != NULL) {
513                                                         /*
514                                                          * Setting dependant DeviceInfo interface
515                                                          */
516                                                         int plugIn_id = atoi(depends_on);
517                                                         if (plugin_is_exsist_device_info_plugin()) {
518                                                                 _DEBUG_INFO("Dependent DeviceInfo PlulgIn ID : %d", plugIn_id);
519
520                                                                 plugin_set_common_devinfo_function_cb func_point_set_common_devinfo_function = plugin_get_function_set_common_devinfo_function(plugIn_Info.plugin_id);
521                                                                 if (func_point_set_common_devinfo_function != 0) {
522                                                                         func_point_set_common_devinfo_function(sync_agent_get_devinfo);
523                                                                 } else {
524                                                                         _DEBUG_ERROR("func_point_set_func_get_device_info_common is NULL!!");
525                                                                 }
526                                                                 plugin_set_common_execute_dev_function_cb func_point_set_common_execute_dev_function = plugin_get_function_set_common_execute_dev_function(plugIn_Info.plugin_id);
527                                                                 if (func_point_set_common_execute_dev_function != 0) {
528                                                                         func_point_set_common_execute_dev_function(sync_agent_execute_dev_function);
529                                                                 } else {
530                                                                         _DEBUG_ERROR("func_point_set_func_execute_device_function_common is NULL!!");
531                                                                 }
532                                                         } else {
533                                                                 _DEBUG_ERROR("FAIL!! Depending DeviceInfo PlulgIn not existing!!");
534                                                         }
535                                                 }
536                                         } else if (__compare_uchar_with_char(domain_name, "PlatformMonitor")) {
537                                                 int r_count = 0;
538                                                 plugin_platform_monitor_s plugIn_pm;
539                                                 sync_agent_pm_register_data_s **reg_data = NULL;
540
541                                                 char *useMainLoop = __get_child_node_content(plugIn_node, "Use-MainLoop");
542                                                 plugIn_pm.use_main_loop = atoi(useMainLoop);
543
544                                                 xmlNode *cursor_node = plugIn_node->children;
545                                                 while (cursor_node != NULL) {
546                                                         if (__compare_uchar_with_char(cursor_node->name, "Register-Data")) {
547                                                                 char *reg_count = (char *)xmlGetProp(cursor_node, (xmlChar *) "count");
548                                                                 if (reg_count != NULL) {
549                                                                         r_count = atoi(reg_count);
550                                                                 }
551                                                                 _DEBUG_VERBOSE("Register-Data count : %d", r_count);
552
553                                                                 if (r_count > 0) {
554                                                                         _DEBUG_VERBOSE("exist Register-Data ( count : %d ) !!", r_count);
555                                                                         xmlNode *cursor_child_node = cursor_node->children;
556
557                                                                         reg_data = (sync_agent_pm_register_data_s **) calloc(r_count, sizeof(sync_agent_pm_register_data_s *));
558                                                                         if (reg_data == NULL) {
559                                                                                 _DEBUG_ERROR("calloc failed !!");
560                                                                                 return SYNC_AGENT_INIT_FAIL;
561                                                                         }
562
563                                                                         int i = 0;
564                                                                         while (cursor_child_node != NULL) {
565                                                                                 if (__compare_uchar_with_char(cursor_child_node->name, "Data")) {
566                                                                                         char *pkg_name = __get_child_node_content(cursor_child_node, "Pkg-Name");
567                                                                                         char *additional_data = __get_child_node_content(cursor_child_node, "Additional-Data");
568
569                                                                                         reg_data[i] = (sync_agent_pm_register_data_s *) calloc(1, sizeof(sync_agent_pm_register_data_s));
570                                                                                         if (reg_data[i] == NULL) {
571                                                                                                 _DEBUG_ERROR("calloc failed !!");
572                                                                                                 if (reg_data != NULL)
573                                                                                                         free(reg_data);
574                                                                                                 return SYNC_AGENT_INIT_FAIL;
575                                                                                         }
576
577                                                                                         if (pkg_name != NULL) {
578                                                                                                 reg_data[i]->pkg_name = strdup(pkg_name);
579                                                                                                 _DEBUG_VERBOSE("pkg_name : %s", reg_data[i]->pkg_name);
580                                                                                         } else {
581                                                                                                 _DEBUG_VERBOSE("pkg_name is NULL !!");
582                                                                                         }
583
584                                                                                         if (additional_data != NULL) {
585                                                                                                 reg_data[i]->additional_data = strdup(additional_data);
586                                                                                                 _DEBUG_VERBOSE("additional_data : %s", reg_data[i]->additional_data);
587                                                                                         } else {
588                                                                                                 _DEBUG_VERBOSE("additional_data is NULL !!");
589                                                                                         }
590
591                                                                                         i++;
592                                                                                 }
593                                                                                 cursor_child_node = cursor_child_node->next;
594                                                                         }
595                                                                 } else {
596                                                                         _DEBUG_VERBOSE("not exist Register-Data !!");
597                                                                 }
598
599                                                                 break;
600                                                         }
601                                                         cursor_node = cursor_node->next;
602                                                 }
603
604                                                 if ((r_count == 0) && (strstr(plugIn_path, "sysnoti-network-connection") != NULL)) {
605                                                         _DEBUG_VERBOSE("set network connection cb data !!");
606                                                         reg_data = (sync_agent_pm_register_data_s **) calloc(1, sizeof(sync_agent_pm_register_data_s *));
607                                                         if (reg_data == NULL) {
608                                                                 _DEBUG_ERROR("calloc failed !!");
609                                                                 _INNER_FUNC_EXIT;
610                                                                 return SYNC_AGENT_INIT_FAIL;
611                                                         }
612
613                                                         *reg_data = (sync_agent_pm_register_data_s *) calloc(1, sizeof(sync_agent_pm_register_data_s));
614                                                         if (*reg_data == NULL) {
615                                                                 _DEBUG_ERROR("calloc failed !!");
616                                                                 if (reg_data != NULL)
617                                                                         free(reg_data);
618                                                                 _INNER_FUNC_EXIT;
619                                                                 return SYNC_AGENT_INIT_FAIL;
620                                                         }
621
622                                                         sync_agent_user_callback_cb_plugin *usr_cb = (sync_agent_user_callback_cb_plugin *) calloc(2, sizeof(sync_agent_user_callback_cb_plugin));
623                                                         if (usr_cb == NULL) {
624                                                                 _DEBUG_ERROR("calloc failed !!");
625                                                                 if (*reg_data != NULL)
626                                                                         free(*reg_data);
627                                                                 if (reg_data != NULL)
628                                                                         free(reg_data);
629                                                                 _INNER_FUNC_EXIT;
630                                                                 return SYNC_AGENT_INIT_FAIL;
631                                                         }
632
633                                                         usr_cb[0] = na_handle_network_status;
634                                                         usr_cb[1] = na_handle_network_all_cancel;
635
636                                                         (*reg_data)->user_data = (void *)usr_cb;
637
638                                                         r_count = 1;
639                                                 }
640
641                                                 plugIn_pm.reg_data = reg_data;
642                                                 plugIn_pm.reg_data_count = r_count;
643                                                 plugIn_pm.plugin_info = plugIn_Info;
644                                                 plugIn_pm.func_set = plugin_get_platform_monitor_func_set(plugIn_Info.plugin_handle, &plugIn_error);
645
646                                                 plugin_register_plugin_platform_monitor(plugIn_pm);
647                                         } else if (__compare_uchar_with_char(domain_name, "MO")) {
648                                                 plugin_mo_s plugIn_mo;
649
650                                                 plugIn_mo.plugin_info = plugIn_Info;
651                                                 plugIn_mo.func_set = plugin_get_mo_func_set(plugIn_Info.plugin_handle, &plugIn_error);
652
653                                                 char *mo_type = __get_child_node_content(plugIn_node, "Type");
654                                                 if (mo_type != NULL) {
655                                                         if (!strcmp(mo_type, "DEVDETAIL")) {
656                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_DEVDETAIL;
657                                                         } else if (!strcmp(mo_type, "DEVINFO")) {
658                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_DEVINFO;
659                                                         } else if (!strcmp(mo_type, "FUMO")) {
660                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_FUMO;
661                                                         } else if (!strcmp(mo_type, "DMACC")) {
662                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_DMACC;
663                                                         } else if (!strcmp(mo_type, "LAWMO")) {
664                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_LAWMO;
665                                                         } else if (!strcmp(mo_type, "SCOMO")) {
666                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_SCOMO;
667                                                         } else {
668                                                                 plugIn_mo.mo_type = SYNC_AGENT_DM_MO_TYPE_NO_TYPE;
669                                                         }
670
671                                                         plugin_register_plugin_mo(plugIn_mo);
672                                                 }
673                                         } else if (__compare_uchar_with_char(domain_name, "DeviceManager")) {
674                                                 plugin_device_manager_s plugIn_device_manager;
675
676                                                 plugIn_device_manager.plugin_info = plugIn_Info;
677                                                 plugIn_device_manager.func_set = plugin_get_device_manager_func_set(plugIn_Info.plugin_handle, &plugIn_error);
678
679                                                 plugin_register_plugin_device_manager(plugIn_device_manager);
680                                         }
681                                 }
682
683                                 plugIn_node = plugIn_node->next;
684                         }
685                 }
686                 domain_node = domain_node->next;
687         }
688
689         _INNER_FUNC_EXIT;
690
691         return SYNC_AGENT_INIT_SUCCESS;
692 }
693
694 static sync_agent_deinit_error_e __process_plugin_deinit(xmlNode * plugin_mgr_node)
695 {
696         _INNER_FUNC_ENTER;
697
698         retvm_if(plugin_mgr_node == NULL, SYNC_AGENT_DEINIT_FAIL, "xmlNode is NULL !!");
699
700         plugin_error_e plugin_error = PLUGIN_SUCCESS;
701         xmlNode *domain_node = plugin_mgr_node->children;
702         while (domain_node != NULL) {
703                 if (domain_node->type == XML_ELEMENT_NODE) {
704                         const xmlChar *domain_name = xmlGetProp(domain_node, (xmlChar *) "name");
705                         const xmlChar *domain_count = xmlGetProp(domain_node, (xmlChar *) "count");
706                         int d_count = 0;
707                         if (domain_count != NULL) {
708                                 d_count = atoi((char *)domain_count);
709                         }
710                         _DEBUG_VERBOSE("Domain : %s ( count : %d ) =====================", domain_name, d_count);
711
712                         int i;
713                         int plugin_count = 0;
714                         if (__compare_uchar_with_char(domain_name, "DataConnector")) {
715                                 const plugin_data_connector_s *plugin_dc_repository = plugin_get_data_connector_plugin_repository(&plugin_count);
716
717                                 /* unload DataConnector's plug-in */
718                                 for (i = 0; i < plugin_count; i++) {
719                                         plugin_error = plugin_unload_plugin(plugin_dc_repository[i].plugin_info.plugin_handle);
720                                         if (plugin_error != PLUGIN_SUCCESS) {
721                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_dc_repository[i].plugin_info.plugin_id);
722                                         } else {
723                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_dc_repository[i].plugin_info.plugin_id);
724                                         }
725                                 }
726
727                                 /* clear DataConnector's plug-in repository */
728                                 plugin_clear_plugin_data_connector();
729                         } else if (__compare_uchar_with_char(domain_name, "DataConverter")) {
730 //                              int plugin_count = 0;
731                                 const plugin_data_converter_s *plugin_dc_repository = plugin_get_data_converter_plugin_repository(&plugin_count);
732
733                                 /* unload DataConnector's plug-in */
734                                 for (i = 0; i < plugin_count; i++) {
735                                         plugin_error = plugin_unload_plugin(plugin_dc_repository[i].plugin_info.plugin_handle);
736                                         if (plugin_error != PLUGIN_SUCCESS) {
737                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_dc_repository[i].plugin_info.plugin_id);
738                                         } else {
739                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_dc_repository[i].plugin_info.plugin_id);
740                                         }
741                                 }
742
743                                 /* clear DataConverter's plug-in repository */
744                                 plugin_clear_plugin_data_converter();
745                         } else if (__compare_uchar_with_char(domain_name, "Account")) {
746                                 const plugin_account_s plugin_acc_repository = plugin_get_account_plugin_repository();
747
748                                 /* unload Account's plug-in */
749                                 plugin_error = plugin_unload_plugin(plugin_acc_repository.plugin_info.plugin_handle);
750                                 if (plugin_error != PLUGIN_SUCCESS) {
751                                         _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_acc_repository.plugin_info.plugin_id);
752                                 } else {
753                                         _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_acc_repository.plugin_info.plugin_id);
754                                 }
755
756                                 /* clear Account's plug-in repository */
757                                 plugin_clear_plugin_account();
758                         } else if (__compare_uchar_with_char(domain_name, "NetworkAccess")) {
759 //                              int plugin_count = 0;
760                                 const plugin_network_access_s *plugin_na_repository = plugin_get_network_access_plugin_repository(&plugin_count);
761
762                                 /* unload NetworkAccess's plug-in */
763                                 for (i = 0; i < plugin_count; i++) {
764                                         plugin_error = plugin_unload_plugin(plugin_na_repository[i].plugin_info.plugin_handle);
765                                         if (plugin_error != PLUGIN_SUCCESS) {
766                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_na_repository[i].plugin_info.plugin_id);
767                                         } else {
768                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_na_repository[i].plugin_info.plugin_id);
769                                         }
770                                 }
771
772                                 /* clear NetworkAccess's plug-in repository */
773                                 plugin_clear_plugin_network_access();
774                         } else if (__compare_uchar_with_char(domain_name, "DeviceInfo")) {
775 //                              int plugin_count = 0;
776                                 const plugin_device_info_s *plugin_di_repository = plugin_get_device_info_plugin_repository(&plugin_count);
777
778                                 /* unload DeviceInfo's plug-in */
779                                 for (i = 0; i < plugin_count; i++) {
780                                         plugin_error = plugin_unload_plugin(plugin_di_repository[i].plugin_info.plugin_handle);
781                                         if (plugin_error != PLUGIN_SUCCESS) {
782                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_di_repository[i].plugin_info.plugin_id);
783                                         } else {
784                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_di_repository[i].plugin_info.plugin_id);
785                                         }
786                                 }
787
788                                 /* clear DeviceInfo's plug-in repository */
789                                 plugin_clear_plugin_device_info();
790                         } else if (__compare_uchar_with_char(domain_name, "PlatformMonitor")) {
791 //                              int plugin_count = 0;
792                                 const plugin_platform_monitor_s *plugin_pm_repository = plugin_get_platform_monitor_plugin_repository(&plugin_count);
793
794                                 /* unload PlatformMonitor's plug-in */
795                                 for (i = 0; i < plugin_count; i++) {
796                                         plugin_error = plugin_unload_plugin(plugin_pm_repository[i].plugin_info.plugin_handle);
797                                         if (plugin_error != PLUGIN_SUCCESS) {
798                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_pm_repository[i].plugin_info.plugin_id);
799                                         } else {
800                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_pm_repository[i].plugin_info.plugin_id);
801                                         }
802                                 }
803
804                         } else if (__compare_uchar_with_char(domain_name, "MO")) {
805 //                              int plugin_count = 0;
806                                 const plugin_mo_s *plugin_mo_repository = plugin_get_mo_plugin_repository(&plugin_count);
807
808                                 /* unload MO's plug-in */
809                                 for (i = 0; i < plugin_count; i++) {
810                                         plugin_error = plugin_unload_plugin(plugin_mo_repository[i].plugin_info.plugin_handle);
811                                         if (plugin_error != PLUGIN_SUCCESS) {
812                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_mo_repository[i].plugin_info.plugin_id);
813                                         } else {
814                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_mo_repository[i].plugin_info.plugin_id);
815                                         }
816                                 }
817
818                                 /* clear MO's plug-in repository */
819                                 plugin_clear_plugin_mo();
820                         } else if (__compare_uchar_with_char(domain_name, "DeviceManager")) {
821 //                              int plugin_count = 0;
822                                 const plugin_device_manager_s *plugin_dm_repository = plugin_get_device_manager_plugin_repository(&plugin_count);
823
824                                 /* unload DeviceManager's plug-in */
825                                 for (i = 0; i < plugin_count; i++) {
826                                         plugin_error = plugin_unload_plugin(plugin_dm_repository[i].plugin_info.plugin_handle);
827                                         if (plugin_error != PLUGIN_SUCCESS) {
828                                                 _DEBUG_ERROR("plugin_unload_plugin() - %s [ID : %d] failed !!", domain_name, plugin_dm_repository[i].plugin_info.plugin_id);
829                                         } else {
830                                                 _DEBUG_VERBOSE("plugin_unload_plugin() - %s [ID : %d] success !!", domain_name, plugin_dm_repository[i].plugin_info.plugin_id);
831                                         }
832                                 }
833
834                                 /* clear DeviceManager's plug-in repository */
835                                 plugin_clear_plugin_device_manager();
836                         }
837                 }
838                 domain_node = domain_node->next;
839         }
840
841         _INNER_FUNC_EXIT;
842
843         return SYNC_AGENT_INIT_SUCCESS;
844 }
845
846 static sync_agent_init_error_e __process_device_manage(xmlNode * device_manage_node)
847 {
848         _INNER_FUNC_ENTER;
849
850         retvm_if(device_manage_node == NULL, SYNC_AGENT_INIT_FAIL, "xmlNode is NULL !!");
851
852         sync_agent_dm_mo_error_e mo_err = dm_mo_alloc_modb_handler_mgr();
853         if (mo_err != SYNC_AGENT_DM_MO_SUCCESS) {
854                 _DEBUG_ERROR("Failed to dm_mo_alloc_modb_handler_mgr() : %d", mo_err);
855                 return SYNC_AGENT_INIT_DM_FAIL;
856         }
857
858         /*
859          * Setting MO PlugIn DeviceInfo
860          */
861         char *mo_db_path_str = __get_child_node_content(device_manage_node, "MO-DB-Path");
862
863 //      mo_err = dm_mo_open("/opt/dbspace/.momanager.db");
864         mo_err = dm_mo_open(mo_db_path_str);
865         if (mo_err != SYNC_AGENT_DM_MO_SUCCESS) {
866                 _DEBUG_ERROR("Failed to dm_mo_open() : %d", mo_err);
867                 return SYNC_AGENT_INIT_DM_FAIL;
868         }
869
870         dm_mo_create_mo_table_wraper();
871
872         char *dependent_devinfo_id_str = __get_child_node_content(device_manage_node, "DependentDevInfoID");
873         int dependent_devinfo_id = atoi(dependent_devinfo_id_str);
874
875         if (plugin_is_exsist_device_info_plugin()) {
876                 _DEBUG_VERBOSE("Dependent device Info PlulgIn ID : %d", dependent_devinfo_id);
877
878                 int *mo_plugIn_id_list = NULL;
879                 int mo_plugIn_cnt = plugin_get_mo_plugin_id_list(&mo_plugIn_id_list);
880
881                 _DEBUG_VERBOSE("Loaded MO PlugIn Count : %d", mo_plugIn_cnt);
882
883                 int i = 0;
884                 for (; i < mo_plugIn_cnt; i++) {
885                         plugin_set_func_get_device_info func_point_set_func_get_device_info = plugin_get_function_set_func_get_device_info(mo_plugIn_id_list[i]);
886                         if (func_point_set_func_get_device_info != 0) {
887                                 func_point_set_func_get_device_info(sync_agent_get_devinfo);
888                         } else {
889                                 _DEBUG_ERROR("func_point_set_func_get_device_info is NULL!!");
890                         }
891                 }
892
893                 free(mo_plugIn_id_list);
894         }
895
896         _INNER_FUNC_EXIT;
897
898         return SYNC_AGENT_INIT_SUCCESS;
899 }
900
901 static char *__get_child_node_content(xmlNode * parent_node, const char *node_name)
902 {
903         _INNER_FUNC_ENTER;
904
905         if (parent_node == NULL || node_name == NULL) {
906                 return NULL;
907         }
908
909         xmlNode *cursor_node = parent_node->children;
910
911         while (cursor_node != NULL) {
912                 if (cursor_node->type == XML_ELEMENT_NODE) {
913                         const xmlChar *compare_name = cursor_node->name;
914                         if (__compare_uchar_with_char(compare_name, node_name)) {
915                                 _INNER_FUNC_EXIT;
916                                 return (char *)xmlNodeGetContent(cursor_node);
917                         }
918                 }
919                 cursor_node = cursor_node->next;
920         }
921
922         return NULL;
923 }
924
925 static int __compare_uchar_with_char(const xmlChar * str, const char *str2)
926 {
927         _INNER_FUNC_ENTER;
928
929         if (str == NULL || str2 == NULL) {
930                 return 0;
931         }
932
933         _INNER_FUNC_EXIT;
934
935         return xmlStrEqual(str, (const xmlChar *)str2);
936 }