Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / src / dbus-common.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2010  Nokia Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *  Copyright (C) 2005-2007  Johan Hedberg <johan.hedberg@nokia.com>
8  *
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdint.h>
32
33 #include <glib.h>
34 #include <dbus/dbus.h>
35
36 #include "gdbus/gdbus.h"
37
38 #include "log.h"
39
40 #include "dbus-common.h"
41
42 static DBusConnection *connection = NULL;
43
44 static void append_variant(DBusMessageIter *iter, int type, void *val)
45 {
46         DBusMessageIter value;
47         char sig[2] = { type, '\0' };
48
49         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, sig, &value);
50
51         dbus_message_iter_append_basic(&value, type, val);
52
53         dbus_message_iter_close_container(iter, &value);
54 }
55
56 static void append_array_variant(DBusMessageIter *iter, int type, void *val,
57                                                         int n_elements)
58 {
59         DBusMessageIter variant, array;
60         char type_sig[2] = { type, '\0' };
61         char array_sig[3] = { DBUS_TYPE_ARRAY, type, '\0' };
62
63         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
64                                                 array_sig, &variant);
65
66         dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
67                                                 type_sig, &array);
68
69         if (dbus_type_is_fixed(type) == TRUE) {
70                 dbus_message_iter_append_fixed_array(&array, type, val,
71                                                         n_elements);
72         } else if (type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) {
73                 const char ***str_array = val;
74                 int i;
75
76                 for (i = 0; i < n_elements; i++)
77                         dbus_message_iter_append_basic(&array, type,
78                                                         &((*str_array)[i]));
79         }
80
81         dbus_message_iter_close_container(&variant, &array);
82
83         dbus_message_iter_close_container(iter, &variant);
84 }
85
86 void dict_append_basic(DBusMessageIter *dict, int key_type, const void *key,
87                                                 int type, void *val)
88 {
89         DBusMessageIter entry;
90
91         if (type == DBUS_TYPE_STRING) {
92                 const char *str = *((const char **) val);
93                 if (str == NULL)
94                         return;
95         }
96
97         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
98                                                         NULL, &entry);
99
100         dbus_message_iter_append_basic(&entry, key_type, key);
101
102         append_variant(&entry, type, val);
103
104         dbus_message_iter_close_container(dict, &entry);
105
106 }
107
108 void dict_append_entry(DBusMessageIter *dict,
109                         const char *key, int type, void *val)
110 {
111         dict_append_basic(dict, DBUS_TYPE_STRING, &key, type, val);
112 }
113
114 void dict_append_basic_array(DBusMessageIter *dict, int key_type,
115                                         const void *key, int type, void *val,
116                                         int n_elements)
117 {
118         DBusMessageIter entry;
119
120         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
121                                                 NULL, &entry);
122
123         dbus_message_iter_append_basic(&entry, key_type, key);
124
125         append_array_variant(&entry, type, val, n_elements);
126
127         dbus_message_iter_close_container(dict, &entry);
128 }
129
130 void dict_append_array(DBusMessageIter *dict, const char *key, int type,
131                         void *val, int n_elements)
132 {
133         dict_append_basic_array(dict, DBUS_TYPE_STRING, &key, type, val,
134                                                                 n_elements);
135 }
136
137 void set_dbus_connection(DBusConnection *conn)
138 {
139         connection = conn;
140 }
141
142 DBusConnection *btd_get_dbus_connection(void)
143 {
144         return connection;
145 }
146
147 const char *class_to_icon(uint32_t class)
148 {
149         switch ((class & 0x1f00) >> 8) {
150         case 0x01:
151                 return "computer";
152         case 0x02:
153                 switch ((class & 0xfc) >> 2) {
154                 case 0x01:
155                 case 0x02:
156                 case 0x03:
157                 case 0x05:
158                         return "phone";
159                 case 0x04:
160                         return "modem";
161                 }
162                 break;
163         case 0x03:
164                 return "network-wireless";
165         case 0x04:
166                 switch ((class & 0xfc) >> 2) {
167                 case 0x01:
168                 case 0x02:
169                         return "audio-card";    /* Headset */
170                 case 0x06:
171                         return "audio-card";    /* Headphone */
172                 case 0x0b: /* VCR */
173                 case 0x0c: /* Video Camera */
174                 case 0x0d: /* Camcorder */
175                         return "camera-video";
176                 default:
177                         return "audio-card";    /* Other audio device */
178                 }
179                 break;
180         case 0x05:
181                 switch ((class & 0xc0) >> 6) {
182                 case 0x00:
183                         switch ((class & 0x1e) >> 2) {
184                         case 0x01:
185                         case 0x02:
186                                 return "input-gaming";
187                         }
188                         break;
189                 case 0x01:
190                         return "input-keyboard";
191                 case 0x02:
192                         switch ((class & 0x1e) >> 2) {
193                         case 0x05:
194                                 return "input-tablet";
195                         default:
196                                 return "input-mouse";
197                         }
198                 }
199                 break;
200         case 0x06:
201                 if (class & 0x80)
202                         return "printer";
203                 if (class & 0x20)
204                         return "camera-photo";
205                 break;
206         }
207
208         return NULL;
209 }
210
211 const char *gap_appearance_to_icon(uint16_t appearance)
212 {
213         switch ((appearance & 0xffc0) >> 6) {
214         case 0x00:
215                 return "unknown";
216         case 0x01:
217                 return "phone";
218         case 0x02:
219                 return "computer";
220         case 0x05:
221                 return "video-display";
222         case 0x0a:
223                 return "multimedia-player";
224         case 0x0b:
225                 return "scanner";
226         case 0x0f: /* HID Generic */
227                 switch (appearance & 0x3f) {
228                 case 0x01:
229                         return "input-keyboard";
230                 case 0x02:
231                         return "input-mouse";
232                 case 0x03:
233                 case 0x04:
234                         return "input-gaming";
235                 case 0x05:
236                         return "input-tablet";
237                 case 0x08:
238                         return "scanner";
239                 }
240                 break;
241         }
242
243         return NULL;
244 }