~ubuntu-branches/debian/sid/hal/sid

« back to all changes in this revision

Viewing changes to hald/linux/probing/probe-net-bluetooth.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2007-10-23 12:33:58 UTC
  • Revision ID: james.westby@ubuntu.com-20071023123358-xaf8mjc5n84d5gtz
Tags: upstream-0.5.10
ImportĀ upstreamĀ versionĀ 0.5.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 * CVSID: $Id$
 
3
 *
 
4
 * probe-net-bluetooth.c : Probe bluetooth network devices
 
5
 *
 
6
 * Copyright (C) 2007 Luiz Augusto von Dentz, <luiz.dentz@indt.org.br>
 
7
 *
 
8
 * Licensed under the Academic Free License version 2.1
 
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 <string.h>
 
31
 
 
32
#include "../../logger.h"
 
33
#include "libhal/libhal.h"
 
34
 
 
35
#define BLUEZ_SERVICE "org.bluez"
 
36
#define BLUEZ_PATH "/org/bluez"
 
37
#define BLUEZ_MANAGER_IFACE "org.bluez.Manager"
 
38
#define BLUEZ_NET_PATH "/org/bluez/network"
 
39
#define BLUEZ_NET_MANAGER_IFACE "org.bluez.network.Manager"
 
40
#define BLUEZ_NET_CONNECTION_IFACE "org.bluez.network.Connection"
 
41
#define BLUEZ_NET_SERVER_IFACE "org.bluez.network.Server"
 
42
 
 
43
static void
 
44
get_properties (DBusConnection *conn, LibHalContext *ctx, const char *udi,
 
45
                                const char *id, const char *path)
 
46
{
 
47
        DBusMessage *msg;
 
48
        DBusMessage *reply = NULL;
 
49
        DBusMessageIter reply_iter;
 
50
        DBusMessageIter dict_iter;
 
51
        DBusError error;
 
52
 
 
53
        dbus_error_init (&error);
 
54
 
 
55
        msg = dbus_message_new_method_call (id, path,
 
56
                                                                                BLUEZ_NET_CONNECTION_IFACE,
 
57
                                                                                "GetInfo");
 
58
 
 
59
        if (msg == NULL)
 
60
                goto out;
 
61
 
 
62
        HAL_INFO (("%s.GetInfo()", BLUEZ_NET_CONNECTION_IFACE));
 
63
        reply = dbus_connection_send_with_reply_and_block (conn, msg, -1, &error);
 
64
 
 
65
        if (dbus_error_is_set (&error) || dbus_set_error_from_message (&error,
 
66
                reply)) {
 
67
                dbus_error_free (&error);
 
68
                goto out;
 
69
        }
 
70
 
 
71
        dbus_message_iter_init (reply, &reply_iter);
 
72
 
 
73
        if (dbus_message_iter_get_arg_type (&reply_iter) != DBUS_TYPE_ARRAY  &&
 
74
            dbus_message_iter_get_element_type (&reply_iter) != DBUS_TYPE_DICT_ENTRY) {
 
75
                goto out;
 
76
        }
 
77
 
 
78
        dbus_message_iter_recurse (&reply_iter, &dict_iter);
 
79
 
 
80
        while (dbus_message_iter_get_arg_type (&dict_iter) == DBUS_TYPE_DICT_ENTRY) {
 
81
                DBusMessageIter dict_entry_iter, var_iter;
 
82
                const char *key;
 
83
                char prop[32];
 
84
 
 
85
                dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
 
86
                dbus_message_iter_get_basic (&dict_entry_iter, &key);
 
87
 
 
88
                dbus_message_iter_next (&dict_entry_iter);
 
89
                dbus_message_iter_recurse (&dict_entry_iter, &var_iter);
 
90
 
 
91
                snprintf(prop, sizeof (prop), "net.bluetooth.%s", key);
 
92
 
 
93
                /* Make any property found annouced by hal */
 
94
                switch (dbus_message_iter_get_arg_type (&var_iter)) {
 
95
                case DBUS_TYPE_STRING:
 
96
                {
 
97
                        const char *value;
 
98
 
 
99
                        dbus_message_iter_get_basic (&var_iter, &value);
 
100
 
 
101
                        HAL_INFO (("reply: %s:%s", key, value));
 
102
 
 
103
                        libhal_device_set_property_string (ctx, udi, prop, value, &error);
 
104
                        break;
 
105
                }
 
106
                case DBUS_TYPE_INT32:
 
107
                {
 
108
                        dbus_int32_t value;
 
109
 
 
110
                        dbus_message_iter_get_basic (&var_iter, &value);
 
111
 
 
112
                        HAL_INFO (("reply: %s:%d", key, value));
 
113
 
 
114
                        libhal_device_set_property_int (ctx, udi, prop, value, &error);
 
115
                        break;
 
116
                }
 
117
                default:
 
118
                        break;
 
119
                }
 
120
                dbus_message_iter_next (&dict_iter);
 
121
        }
 
122
 
 
123
out:
 
124
        if (msg)
 
125
                dbus_message_unref (msg);
 
126
        if (reply)
 
127
                dbus_message_unref (reply);
 
128
        return;
 
129
}
 
130
 
 
131
int
 
132
main (int argc, char *argv[])
 
133
{
 
134
        char *udi;
 
135
        char *iface;
 
136
        char *id;
 
137
        const char *connection;
 
138
        char network[8] = "network";
 
139
        const char *pnetwork = network;
 
140
        LibHalContext *ctx = NULL;
 
141
        DBusConnection *conn;
 
142
        DBusMessage *msg = NULL;
 
143
        DBusMessage *reply = NULL;
 
144
        DBusError error;
 
145
 
 
146
        udi = getenv ("UDI");
 
147
        if (udi == NULL)
 
148
                goto out;
 
149
 
 
150
        dbus_error_init (&error);
 
151
        if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
 
152
                goto out;
 
153
 
 
154
        iface = libhal_device_get_property_string (ctx, udi, "net.interface", NULL);
 
155
 
 
156
        HAL_INFO (("Investigating '%s'", iface));
 
157
 
 
158
        if (iface == NULL)
 
159
                goto out;
 
160
 
 
161
        if ((conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error)) == NULL)
 
162
                goto out;
 
163
 
 
164
        msg = dbus_message_new_method_call (BLUEZ_SERVICE, BLUEZ_PATH, BLUEZ_MANAGER_IFACE, "ActivateService");
 
165
 
 
166
        if (msg == NULL)
 
167
                goto out;
 
168
 
 
169
        HAL_INFO (("%s.ActivateService('%s')", BLUEZ_MANAGER_IFACE, pnetwork));
 
170
        dbus_message_append_args (msg, DBUS_TYPE_STRING, &pnetwork,
 
171
                                                                DBUS_TYPE_INVALID);
 
172
        reply = dbus_connection_send_with_reply_and_block (conn, msg, -1, &error);
 
173
 
 
174
        if (dbus_error_is_set (&error) || dbus_set_error_from_message (&error, reply)) {
 
175
                dbus_error_free (&error);
 
176
                goto out;
 
177
        }
 
178
 
 
179
        dbus_message_unref (msg);
 
180
        msg = NULL;
 
181
 
 
182
        dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &id, DBUS_TYPE_INVALID);
 
183
        if (dbus_error_is_set (&error)) {
 
184
                dbus_error_free (&error);
 
185
                goto out;
 
186
        }
 
187
 
 
188
        dbus_message_unref (reply);
 
189
        reply = NULL;
 
190
 
 
191
        HAL_INFO (("Found Bluez Network service '%s'", id));
 
192
 
 
193
        msg = dbus_message_new_method_call (id, BLUEZ_NET_PATH, BLUEZ_NET_MANAGER_IFACE, "FindConnection");
 
194
 
 
195
        if (msg == NULL)
 
196
                goto out;
 
197
 
 
198
        HAL_INFO (("%s.FindConnection('%s')", BLUEZ_NET_MANAGER_IFACE, iface));
 
199
        dbus_message_append_args (msg, DBUS_TYPE_STRING, &iface,
 
200
                                                        DBUS_TYPE_INVALID);
 
201
        reply = dbus_connection_send_with_reply_and_block (conn, msg, -1, &error);
 
202
 
 
203
        if (dbus_error_is_set (&error) || dbus_set_error_from_message (&error,
 
204
                reply)) {
 
205
                dbus_error_free (&error);
 
206
                goto out;
 
207
        }
 
208
 
 
209
        dbus_message_unref (msg);
 
210
        msg = NULL;
 
211
 
 
212
        dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &connection,
 
213
                                                        DBUS_TYPE_INVALID);
 
214
        if (dbus_error_is_set (&error)) {
 
215
                dbus_error_free (&error);
 
216
                goto out;
 
217
        }
 
218
 
 
219
        get_properties (conn, ctx, udi, id, connection);
 
220
 
 
221
out:
 
222
        if (msg)
 
223
                dbus_message_unref (msg);
 
224
        if (reply)
 
225
                dbus_message_unref (reply);
 
226
        if (ctx != NULL) {
 
227
                dbus_error_init (&error);
 
228
                libhal_ctx_shutdown (ctx, &error);
 
229
                libhal_ctx_free (ctx);
 
230
        }
 
231
 
 
232
        return 0;
 
233
}