Add first steps of generic task framework
[platform/upstream/connman.git] / src / task.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <glib.h>
27
28 #include "connman.h"
29
30 struct connman_task {
31         char *path;
32         pid_t pid;
33 };
34
35 static GHashTable *task_hash = NULL;
36
37 static volatile gint task_counter;
38
39 static void free_task(gpointer data)
40 {
41         struct connman_task *task = data;
42
43         DBG("task %p", task);
44
45         g_free(task->path);
46         g_free(task);
47 }
48
49 struct connman_task *connman_task_create(void)
50 {
51         struct connman_task *task;
52         gint counter;
53
54         DBG("");
55
56         task = g_try_new0(struct connman_task, 1);
57         if (task == NULL)
58                 return NULL;
59
60         counter = g_atomic_int_exchange_and_add(&task_counter, 1);
61
62         task->path = g_strdup_printf("/task/%d", counter);
63         task->pid = -1;
64
65         DBG("task %p", task);
66
67         g_hash_table_insert(task_hash, task->path, task);
68
69         return task;
70 }
71
72 void connman_task_destroy(struct connman_task *task)
73 {
74         DBG("task %p", task);
75
76         g_hash_table_remove(task_hash, task->path);
77 }
78
79 static DBusHandlerResult task_filter(DBusConnection *connection,
80                                         DBusMessage *message, void *user_data)
81 {
82         struct connman_task *task;
83         const char *path;
84
85         if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
86                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
87
88         if (dbus_message_has_interface(message,
89                                         CONNMAN_TASK_INTERFACE) == FALSE)
90                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
91
92         path = dbus_message_get_path(message);
93         if (path == NULL)
94                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
95
96         task = g_hash_table_lookup(task_hash, path);
97         if (task == NULL)
98                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
99
100         return DBUS_HANDLER_RESULT_HANDLED;
101 }
102
103 static const char *task_rule = "type=method_call"
104                                         ",interface=" CONNMAN_TASK_INTERFACE;
105
106 static DBusConnection *connection;
107
108 int __connman_task_init(void)
109 {
110         DBG("");
111
112         connection = connman_dbus_get_connection();
113
114         dbus_connection_add_filter(connection, task_filter, NULL, NULL);
115
116         g_atomic_int_set(&task_counter, 0);
117
118         task_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
119                                                         NULL, free_task);
120
121         dbus_bus_add_match(connection, task_rule, NULL);
122         dbus_connection_flush(connection);
123
124         return 0;
125 }
126
127 void __connman_task_cleanup(void)
128 {
129         DBG("");
130
131         dbus_bus_remove_match(connection, task_rule, NULL);
132         dbus_connection_flush(connection);
133
134         g_hash_table_destroy(task_hash);
135         task_hash = NULL;
136
137         dbus_connection_remove_filter(connection, task_filter, NULL);
138
139         dbus_connection_unref(connection);
140 }