summaryrefslogtreecommitdiff
path: root/src/agent/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/main.c')
-rwxr-xr-xsrc/agent/main.c264
1 files changed, 264 insertions, 0 deletions
diff --git a/src/agent/main.c b/src/agent/main.c
new file mode 100755
index 0000000..256840b
--- /dev/null
+++ b/src/agent/main.c
@@ -0,0 +1,264 @@
+/*
+ * 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 <unistd.h>
+#include <libxml/parser.h>
+#include <signal.h>
+
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+//#include <appcore-common.h>
+
+/*sync-agent*/
+#include <sync_agent.h>
+
+/*dm-agent*/
+#include "common/dm_common.h"
+#include "common/util/util.h"
+#include "framework/task/oma_dm_task_request.h"
+#include "ipc_agent.h"
+
+#ifndef OMADM_AGENT_LOG
+#undef LOG_TAG
+#define LOG_TAG "OMA_DM"
+#endif
+
+#define MC_SERVICE_OBJECT_PATH "/com/samsung/omadmagent" /* Don't use special character */
+#define MC_SERVICE_DBUS "com.samsung.omadmagent"
+#define MC_SERVICE_INTERFACE "com.samsung.omadmagent"
+
+static DBusConnection *connection = NULL;
+
+static void _unregistered_path(DBusConnection * connection, void *user_data)
+{
+ /* connection was finalized */
+ _INNER_FUNC_ENTER;
+
+ _INNER_FUNC_EXIT;
+}
+
+static DBusHandlerResult _message_path(DBusConnection * connection, DBusMessage * message, void *user_data)
+{
+ _INNER_FUNC_ENTER;
+ if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Echo")) {
+ DBusMessage *reply;
+ _DEBUG_INFO("Recived the HelloFromSelf message\n");
+
+ reply = dbus_message_new_method_return(message);
+ if (reply == NULL)
+ _DEBUG_ERROR("no memory\n");
+
+ if (!dbus_connection_send(connection, reply, NULL))
+ _DEBUG_ERROR("no memory\n");
+ } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Hello_Agent")) {
+ _DEBUG_INFO("Hello_Agent");
+ } else if (dbus_message_is_method_call(message, MC_SERVICE_DBUS, "Goodbye_Agent")) {
+ _DEBUG_INFO("Goodbye_Agent");
+ FILE *file_exist = NULL;
+ file_exist = fopen("/opt/data/fota/oma-dm/.oma-dm-agent-enabled", "r");
+ if (file_exist != NULL) {
+ _DEBUG_INFO("file oma-dm-agent-enabled exist!!");
+ fclose(file_exist);
+ } else {
+ _INNER_FUNC_EXIT;
+ exit(1);
+ }
+ } else {
+ _DEBUG_INFO("can't match message");
+ }
+
+ _INNER_FUNC_EXIT;
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static DBusObjectPathVTable echo_vtable = {
+ _unregistered_path,
+ _message_path,
+ NULL,
+};
+
+static int _register_dbus_service(void)
+{
+ _INNER_FUNC_ENTER;
+ int result;
+ DBusError error;
+ void *d;
+
+ dbus_error_init(&error);
+ connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+ if (connection == NULL) {
+ _DEBUG_ERROR("*** Failed to open connection to activating message bus: %s\n", error.message);
+ dbus_error_free(&error);
+ return 1;
+ }
+
+ if (!dbus_connection_register_object_path(connection, MC_SERVICE_OBJECT_PATH, &echo_vtable, (void *)0xdeadbeef)) {
+ _DEBUG_ERROR("no memory\n");
+ goto failed;
+ }
+
+ if (!dbus_connection_get_object_path_data(connection, MC_SERVICE_OBJECT_PATH, &d)) {
+ _DEBUG_ERROR("no memory\n");
+ goto failed;
+ }
+
+ if (d != (void *)0xdeadbeef) {
+ _DEBUG_ERROR("dbus_connection_get_object_path_data() doesn't seem to work right\n");
+ goto failed;
+ }
+
+ result = dbus_bus_request_name(connection, MC_SERVICE_DBUS, 0, &error);
+ if (dbus_error_is_set(&error)) {
+ _DEBUG_ERROR("Error %s\n", error.message);
+ dbus_error_free(&error);
+ dbus_connection_unref(connection);
+ _INNER_FUNC_EXIT;
+ exit(1);
+ }
+
+ _INNER_FUNC_EXIT;
+ return 0;
+
+ failed:
+ dbus_connection_unref(connection);
+ _INNER_FUNC_EXIT;
+ return 1;
+}
+
+static void _agent_daemon_signal_handler(int signo, siginfo_t * info, void *p_context)
+{
+ _EXTERN_FUNC_ENTER;
+
+ switch (signo) {
+ case SIGTERM:
+ _DEBUG_INFO("Got SIGTERM");
+
+ /* stop gmain loop */
+ sync_agent_stop_main_loop(0);
+
+ break;
+
+ default:
+ break;
+ }
+
+ _EXTERN_FUNC_EXIT;
+}
+
+int main()
+{
+ _EXTERN_FUNC_ENTER;
+// _DEBUG_INFO("[TIME] 1. main : %d msec\n", appcore_measure_time_from("APP_START_TIME"));
+// appcore_measure_start();
+
+ sync_agent_deinit_error_e deinit_error = SYNC_AGENT_DEINIT_SUCCESS;
+ sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
+ struct sigaction sig_act;
+
+ if (sync_agent_daemonize() < 0) {
+ _DEBUG_ERROR("daemonize error");
+ return -1;
+ }
+// _DEBUG_INFO("[TIME] 2. main : %d msec\n", appcore_measure_time());
+// appcore_measure_start();
+ xmlInitParser();
+
+ _register_dbus_service();
+
+ init_error = sync_agent_init("/usr/share/oma-dm-cfg/fw-init/omadm_fw_config.xml");
+ if (init_error != SYNC_AGENT_INIT_SUCCESS) {
+ _DEBUG_ERROR("Failed to OMA DM sync_agent_init() : %d", init_error);
+ return -1;
+ } else {
+ _DEBUG_INFO("done init_Framework - OMA DM");
+ }
+
+ /*for network connection */
+ DM_ERROR ret = DM_OK;
+ int return_Ipc = 0;
+ //sleep(4);
+ //network_connection_delay();
+ return_Ipc = set_IPC();
+ _DEBUG_INFO("return ipc : %d", return_Ipc);
+
+ check_csc();
+
+ ret = init_dm();
+ _DEBUG_INFO("return init dm : %d", ret);
+/* if (ret != DM_OK) {
+ goto error;
+ }*/
+
+ ret = dm_init_task_request();
+ if (ret != DM_OK) {
+ goto error;
+ }
+
+ /* Block SIGPIPE signal (client may close socket abnormally) */
+ signal(SIGPIPE, SIG_IGN);
+
+ /* register signal handler. will be called by kill command */
+ sig_act.sa_handler = NULL;
+ sig_act.sa_sigaction = _agent_daemon_signal_handler;
+ sig_act.sa_flags = SA_SIGINFO;
+ sigemptyset(&sig_act.sa_mask);
+ sigaction(SIGTERM, &sig_act, NULL);
+
+ sync_agent_run_main_loop(0);
+
+ ret = end_dm();
+ if (ret != DM_OK) {
+ _DEBUG_ERROR("end_dm() failed !!");
+ goto error;
+ }
+
+ if (connection) {
+ dbus_connection_unref(connection);
+ connection = NULL;
+ }
+
+ deinit_error = sync_agent_deinit();
+ if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
+ _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
+ }
+
+ xmlCleanupParser();
+
+ _EXTERN_FUNC_EXIT;
+
+ return 1;
+
+ error:
+ _DEBUG_ERROR("end error : %d\n", ret);
+
+ if (connection) {
+ dbus_connection_unref(connection);
+ connection = NULL;
+ }
+
+ deinit_error = sync_agent_deinit();
+ if (deinit_error != SYNC_AGENT_DEINIT_SUCCESS) {
+ _DEBUG_ERROR("deinit fail : %d!!", deinit_error);
+ }
+
+ xmlCleanupParser();
+
+ _EXTERN_FUNC_EXIT;
+
+ return 0;
+}