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>
31 #include <sync_agent.h>
34 #include "common/dm_common.h"
35 #include "common/util/util.h"
36 #include "framework/task/oma_dm_task_request.h"
37 #include "ipc_agent.h"
39 #ifndef OMADM_AGENT_LOG
41 #define LOG_TAG "OMA_DM"
44 #define MC_SERVICE_OBJECT_PATH "/com/samsung/omadmagent" /* Don't use special character */
45 #define MC_SERVICE_DBUS "com.samsung.omadmagent"
46 #define MC_SERVICE_INTERFACE "com.samsung.omadmagent"
48 static DBusConnection *connection = NULL;
50 static void _unregistered_path(DBusConnection * connection, void *user_data)
52 /* connection was finalized */
58 static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
61 if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
63 _DEBUG_INFO("Recived the HelloFromSelf message\n");
65 reply = dbus_message_new_method_return(message);
67 _DEBUG_ERROR("no memory\n");
69 if (!dbus_connection_send(connection, reply, NULL))
70 _DEBUG_ERROR("no memory\n");
71 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
72 _DEBUG_INFO("Hello_Agent");
73 } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
74 _DEBUG_INFO("Goodbye_Agent");
75 FILE *file_exist = NULL;
76 file_exist = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
77 if (file_exist != NULL) {
78 _DEBUG_INFO("file oma-dm-agent-enabled exist!!");
85 _DEBUG_INFO("can't match message");
89 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
92 static DBusObjectPathVTable echo_vtable = {
98 static int _register_dbus_service(void)
105 dbus_error_init(&error);
106 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
107 if (connection == NULL) {
108 _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
109 dbus_error_free(&error);
113 if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
114 _DEBUG_ERROR("no memory\n");
118 if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
119 _DEBUG_ERROR("no memory\n");
123 if (d != (void *)0xdeadbeef) {
124 _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
128 result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
129 if (dbus_error_is_set(&error)) {
130 _DEBUG_ERROR("Error %s\n", error.message);
131 dbus_error_free(&error);
132 dbus_connection_unref(connection);
141 dbus_connection_unref(connection);
146 static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
152 _DEBUG_INFO("Got SIGTERM");
154 /* stop gmain loop */
155 sync_agent_stop_main_loop(0);
166 static int _system_command(const char *command)
170 int pid = 0, status = 0;
171 char *const environ[] = { NULL };
173 retvm_if((command) == NULL, -1, "command is NULL!!");
177 _DEBUG_INFO("ERROR : fork failed to create a process\n");
181 } else if (pid == 0) {
185 argv[2] = (char *)command;
188 execve("/bin/sh", argv, environ);
192 if (waitpid(pid, &status, 0) == -1) {
193 if (errno != EINTR) {
208 static void _confversion_check()
212 value = vconf_get_str("db/SyncML/oma-dm-service/DevManagement/ConfVersion");
215 if (strcmp(value, "") == 0) {
219 } else if(strncmp(value, "0.1", strlen("0.1")) == 0) {
220 _DEBUG_INFO("ConfVersion is same value existed!");
232 _DEBUG_INFO("ConfVersion is not same value existed !!");
233 int ret = _system_command("/usr/share/oma-dm-cfg/csc-setting/change_csc_value.sh");
235 _DEBUG_INFO("success");
243 int main(int argc, char *argv[])
248 if(strncmp(argv[1], "booting", strlen("booting")) == 0) {
249 FILE * pFile_fota_enabled = NULL;
250 FILE * pFile_lawmo_enabled = NULL;
251 pFile_fota_enabled = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
252 pFile_lawmo_enabled = fopen("/opt/data/fota/oma-dm/.lawmo_oma-dm-agent-enabled", "r");
253 if(pFile_fota_enabled == NULL && pFile_lawmo_enabled == NULL) {
257 if(pFile_fota_enabled != NULL) {
258 fclose(pFile_fota_enabled);
259 pFile_fota_enabled = NULL;
261 if(pFile_lawmo_enabled != NULL) {
262 fclose(pFile_lawmo_enabled);
263 pFile_lawmo_enabled = NULL;
272 // _DEBUG_INFO("[TIME] 1. main : %d msec\n", appcore_measure_time_from("APP_START_TIME"));
273 // appcore_measure_start();
275 sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
276 sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
277 struct sigaction sig_act;
279 if (sync_agent_daemonize() < 0) {
280 _DEBUG_ERROR("daemonize error");
283 // _DEBUG_INFO("[TIME] 2. main : %d msec\n", appcore_measure_time());
284 // appcore_measure_start();
286 _confversion_check();
287 _register_dbus_service();
289 init_error = sync_agent_init("/usr/share/oma-dm-cfg/fw-init/omadm_fw_config.xml");
290 if (init_error != SYNC_AGENT_INIT_SUCCESS) {
291 _DEBUG_ERROR("Failed to OMA DM sync_agent_init() : %d", init_error);
294 _DEBUG_INFO("done init_Framework - OMA DM");
297 /*for network connection */
298 DM_ERROR ret = DM_OK;
301 //network_connection_delay();
302 return_Ipc = set_IPC();
303 _DEBUG_INFO("return ipc : %d", return_Ipc);
308 _DEBUG_INFO("return init dm : %d", ret);
309 /* if (ret != DM_OK) {
313 ret = dm_init_task_request();
318 /* Block SIGPIPE signal (client may close socket abnormally) */
319 signal(SIGPIPE, SIG_IGN);
321 /* register signal handler. will be called by kill command */
322 sig_act.sa_handler = NULL;
323 sig_act.sa_sigaction = _agent_daemon_signal_handler;
324 sig_act.sa_flags = SA_SIGINFO;
325 sigemptyset(&sig_act.sa_mask);
326 sigaction(SIGTERM, &sig_act, NULL);
328 sync_agent_run_main_loop(0);
332 _DEBUG_ERROR("end_dm() failed !!");
337 dbus_connection_unref(connection);
341 deinit_error = sync_agent_deinit();
342 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
343 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
353 _DEBUG_ERROR("end error : %d\n", ret);
356 dbus_connection_unref(connection);
360 deinit_error = sync_agent_deinit();
361 if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
362 _DEBUG_ERROR("deinit fail : %d!!", deinit_error);