3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <libxml/parser.h>
23 #include <dbus/dbus-glib.h>
24 #include <dbus/dbus.h>
25 //#include <appcore-common.h>
28 #include <sync_agent.h>
31 #include "common/dm_common.h"
32 #include "common/util/util.h"
33 #include "framework/task/oma_dm_task_request.h"
34 #include "ipc_agent.h"
36 #ifndef OMADM_AGENT_LOG
38 #define LOG_TAG "OMA_DM"
41 #define MC_SERVICE_OBJECT_PATH "/com/samsung/omadmagent" /* Don't use special character */
42 #define MC_SERVICE_DBUS "com.samsung.omadmagent"
43 #define MC_SERVICE_INTERFACE "com.samsung.omadmagent"
45 static DBusConnection *connection = NULL;
47 static void _unregistered_path(DBusConnection * connection, void *user_data)
49 /* connection was finalized */
55 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
58 if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
60 _DEBUG_INFO("Recived the HelloFromSelf message\n");
62 reply = dbus_message_new_method_return(message);
64 _DEBUG_ERROR("no memory\n");
66 if (!dbus_connection_send(connection, reply, NULL))
67 _DEBUG_ERROR("no memory\n");
68 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
69 _DEBUG_INFO("Hello_Agent");
70 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
71 _DEBUG_INFO("Goodbye_Agent");
72 FILE *file_exist = NULL;
73 file_exist = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
74 if (file_exist != NULL) {
75 _DEBUG_INFO("file oma-dm-agent-enabled exist!!");
82 _DEBUG_INFO("can't match message");
86 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
89 static DBusObjectPathVTable echo_vtable = {
95 static int _register_dbus_service(void)
102 dbus_error_init(&error);
103 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
104 if (connection == NULL) {
105 _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
106 dbus_error_free(&error);
110 if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
111 _DEBUG_ERROR("no memory\n");
115 if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
116 _DEBUG_ERROR("no memory\n");
120 if (d != (void *)0xdeadbeef) {
121 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
125 result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
126 if (dbus_error_is_set(&error)) {
127 _DEBUG_ERROR("Error %s\n", error.message);
128 dbus_error_free(&error);
129 dbus_connection_unref(connection);
138 dbus_connection_unref(connection);
143 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
149 _DEBUG_INFO("Got SIGTERM");
151 /* stop gmain loop */
152 sync_agent_stop_main_loop(0);
166 // _DEBUG_INFO("[TIME] 1. main : %d msec\n", appcore_measure_time_from("APP_START_TIME"));
167 // appcore_measure_start();
169 sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
170 sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
171 struct sigaction sig_act;
173 if (sync_agent_daemonize() < 0) {
174 _DEBUG_ERROR("daemonize error");
177 // _DEBUG_INFO("[TIME] 2. main : %d msec\n", appcore_measure_time());
178 // appcore_measure_start();
181 _register_dbus_service();
183 init_error = sync_agent_init("/usr/share/oma-dm-cfg/fw-init/omadm_fw_config.xml");
184 if (init_error != SYNC_AGENT_INIT_SUCCESS) {
185 _DEBUG_ERROR("Failed to OMA DM sync_agent_init() : %d", init_error);
188 _DEBUG_INFO("done init_Framework - OMA DM");
191 /*for network connection */
192 DM_ERROR ret = DM_OK;
195 //network_connection_delay();
196 return_Ipc = set_IPC();
197 _DEBUG_INFO("return ipc : %d", return_Ipc);
202 _DEBUG_INFO("return init dm : %d", ret);
203 /* if (ret != DM_OK) {
207 ret = dm_init_task_request();
212 /* Block SIGPIPE signal (client may close socket abnormally) */
213 signal(SIGPIPE, SIG_IGN);
215 /* register signal handler. will be called by kill command */
216 sig_act.sa_handler = NULL;
217 sig_act.sa_sigaction = _agent_daemon_signal_handler;
218 sig_act.sa_flags = SA_SIGINFO;
219 sigemptyset(&sig_act.sa_mask);
220 sigaction(SIGTERM, &sig_act, NULL);
222 sync_agent_run_main_loop(0);
226 _DEBUG_ERROR("end_dm() failed !!");
231 dbus_connection_unref(connection);
235 deinit_error = sync_agent_deinit();
236 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
237 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
247 _DEBUG_ERROR("end error : %d\n", ret);
250 dbus_connection_unref(connection);
254 deinit_error = sync_agent_deinit();
255 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
256 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);