Add support for disconnecting networks first
[platform/upstream/connman.git] / src / dbus.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 "connman.h"
27
28 void connman_dbus_property_append_variant(DBusMessageIter *iter,
29                                         const char *key, int type, void *val)
30 {
31         DBusMessageIter value;
32         const char *signature;
33
34         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
35
36         switch (type) {
37         case DBUS_TYPE_BOOLEAN:
38                 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
39                 break;
40         case DBUS_TYPE_STRING:
41                 signature = DBUS_TYPE_STRING_AS_STRING;
42                 break;
43         case DBUS_TYPE_BYTE:
44                 signature = DBUS_TYPE_BYTE_AS_STRING;
45                 break;
46         case DBUS_TYPE_UINT16:
47                 signature = DBUS_TYPE_UINT16_AS_STRING;
48                 break;
49         case DBUS_TYPE_INT16:
50                 signature = DBUS_TYPE_INT16_AS_STRING;
51                 break;
52         case DBUS_TYPE_UINT32:
53                 signature = DBUS_TYPE_UINT32_AS_STRING;
54                 break;
55         case DBUS_TYPE_INT32:
56                 signature = DBUS_TYPE_INT32_AS_STRING;
57                 break;
58         case DBUS_TYPE_OBJECT_PATH:
59                 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
60                 break;
61         default:
62                 signature = DBUS_TYPE_VARIANT_AS_STRING;
63                 break;
64         }
65
66         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
67                                                         signature, &value);
68         dbus_message_iter_append_basic(&value, type, val);
69         dbus_message_iter_close_container(iter, &value);
70 }
71
72 void connman_dbus_dict_append_array(DBusMessageIter *dict,
73                                 const char *key, int type, void *val, int len)
74 {
75         DBusMessageIter entry, value, array;
76         const char *variant_sig, *array_sig;
77
78         switch (type) {
79         case DBUS_TYPE_BYTE:
80                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
81                 array_sig = DBUS_TYPE_BYTE_AS_STRING;
82                 break;
83         default:
84                 return;
85         }
86
87         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
88                                                                 NULL, &entry);
89
90         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
91
92         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
93                                                         variant_sig, &value);
94
95         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
96                                                         array_sig, &array);
97         dbus_message_iter_append_fixed_array(&array, type, val, len);
98         dbus_message_iter_close_container(&value, &array);
99
100         dbus_message_iter_close_container(&entry, &value);
101
102         dbus_message_iter_close_container(dict, &entry);
103 }
104
105 void connman_dbus_dict_append_variant(DBusMessageIter *dict,
106                                         const char *key, int type, void *val)
107 {
108         DBusMessageIter entry;
109
110         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
111                                                                 NULL, &entry);
112
113         connman_dbus_property_append_variant(&entry, key, type, val);
114
115         dbus_message_iter_close_container(dict, &entry);
116 }
117
118 static DBusConnection *connection = NULL;
119
120 DBusConnection *connman_dbus_get_connection(void)
121 {
122         if (connection == NULL)
123                 return NULL;
124
125         return dbus_connection_ref(connection);
126 }
127
128 int __connman_dbus_init(DBusConnection *conn)
129 {
130         connection = conn;
131
132         return 0;
133 }
134
135 void __connman_dbus_cleanup(void)
136 {
137         connection = NULL;
138 }