3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <dbus/dbus.h>
36 #define BLUEZ_SERVICE "org.bluez"
38 #define MANAGER_PATH "/"
39 #define MANAGER_INTF BLUEZ_SERVICE ".Manager"
40 #define ADAPTER_INTF BLUEZ_SERVICE ".Adapter"
42 static char *get_adapter(DBusConnection *conn)
44 DBusMessage *message, *reply;
49 message = dbus_message_new_method_call(BLUEZ_SERVICE, MANAGER_PATH,
50 MANAGER_INTF, "DefaultAdapter");
54 dbus_error_init(&error);
56 reply = dbus_connection_send_with_reply_and_block(conn,
59 dbus_message_unref(message);
62 if (dbus_error_is_set(&error) == TRUE) {
63 fprintf(stderr, "%s\n", error.message);
64 dbus_error_free(&error);
66 fprintf(stderr, "Failed to set property\n");
70 if (dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, &path,
71 DBUS_TYPE_INVALID) == FALSE)
74 printf("Using default adapter %s\n", path);
76 result = strdup(path);
79 dbus_message_unref(reply);
84 static char *find_device(DBusConnection *conn, const char *adapter,
87 DBusMessage *message, *reply;
92 message = dbus_message_new_method_call(BLUEZ_SERVICE, adapter,
93 ADAPTER_INTF, "FindDevice");
97 dbus_message_append_args(message, DBUS_TYPE_STRING, &address,
100 dbus_error_init(&error);
102 reply = dbus_connection_send_with_reply_and_block(conn,
103 message, -1, &error);
105 dbus_message_unref(message);
108 if (dbus_error_is_set(&error) == TRUE) {
109 fprintf(stderr, "%s\n", error.message);
110 dbus_error_free(&error);
112 fprintf(stderr, "Failed to set property\n");
116 if (dbus_message_get_args(reply, NULL, DBUS_TYPE_OBJECT_PATH, &path,
117 DBUS_TYPE_INVALID) == FALSE)
120 printf("Using device %s for address %s\n", path, address);
122 result = strdup(path);
125 dbus_message_unref(reply);
130 static int remove_device(DBusConnection *conn, const char *adapter,
133 DBusMessage *message, *reply;
136 message = dbus_message_new_method_call(BLUEZ_SERVICE, adapter,
137 ADAPTER_INTF, "RemoveDevice");
141 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &device,
144 dbus_error_init(&error);
146 reply = dbus_connection_send_with_reply_and_block(conn,
147 message, -1, &error);
149 dbus_message_unref(message);
152 if (dbus_error_is_set(&error) == TRUE) {
153 fprintf(stderr, "%s\n", error.message);
154 dbus_error_free(&error);
156 fprintf(stderr, "Failed to set property\n");
160 dbus_message_unref(reply);
162 printf("Removed device %s\n", device);
167 static int set_property(DBusConnection *conn, const char *adapter,
168 const char *key, int type, void *val)
170 DBusMessage *message, *reply;
171 DBusMessageIter array, value;
173 const char *signature;
175 message = dbus_message_new_method_call(BLUEZ_SERVICE, adapter,
176 ADAPTER_INTF, "SetProperty");
181 case DBUS_TYPE_BOOLEAN:
182 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
184 case DBUS_TYPE_UINT32:
185 signature = DBUS_TYPE_UINT32_AS_STRING;
191 dbus_message_iter_init_append(message, &array);
193 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &key);
195 dbus_message_iter_open_container(&array, DBUS_TYPE_VARIANT,
197 dbus_message_iter_append_basic(&value, type, val);
198 dbus_message_iter_close_container(&array, &value);
200 dbus_error_init(&error);
202 reply = dbus_connection_send_with_reply_and_block(conn,
203 message, -1, &error);
205 dbus_message_unref(message);
208 if (dbus_error_is_set(&error) == TRUE) {
209 fprintf(stderr, "%s\n", error.message);
210 dbus_error_free(&error);
212 fprintf(stderr, "Failed to set property\n");
216 dbus_message_unref(reply);
218 printf("Set property %s for %s\n", key, adapter);
223 static void usage(void)
225 printf("gaptest - GAP testing\n"
227 printf("\tgaptest [options]\n");
229 "\t-T <timeout> Set timeout\n"
230 "\t-P <powered> Set powered\n"
231 "\t-D <discoverable> Set discoverable\n"
232 "\t-B <pairable> Set pairable\n"
233 "\t-C <address> Create device\n"
234 "\t-R <address> Remove device\n");
237 int main(int argc, char *argv[])
239 DBusConnection *conn;
240 char *adapter, *device;
241 const char *create = NULL, *remove = NULL;
242 int opt, timeout = -1, powered = -1, discoverable = -1, pairable = -1;
244 while ((opt = getopt(argc, argv, "T:P:D:B:C:R:h")) != EOF) {
247 timeout = atoi(optarg);
250 powered = atoi(optarg);
253 discoverable = atoi(optarg);
256 pairable = atoi(optarg);
273 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
275 fprintf(stderr, "Can't get on system bus\n");
279 adapter = get_adapter(conn);
281 fprintf(stderr, "Can't get default adapter\n");
286 set_property(conn, adapter, "Powered",
287 DBUS_TYPE_BOOLEAN, &powered);
290 if (discoverable >= 0) {
291 set_property(conn, adapter, "Discoverable",
292 DBUS_TYPE_BOOLEAN, &discoverable);
295 set_property(conn, adapter, "DiscoverableTimeout",
296 DBUS_TYPE_UINT32, &timeout);
300 set_property(conn, adapter, "Pairable",
301 DBUS_TYPE_BOOLEAN, &pairable);
304 set_property(conn, adapter, "PairableTimeout",
305 DBUS_TYPE_UINT32, &timeout);
309 device = find_device(conn, adapter, create);
311 fprintf(stderr, "Can't find device\n");
319 device = find_device(conn, adapter, remove);
321 fprintf(stderr, "Can't find device\n");
325 remove_device(conn, adapter, device);
332 dbus_connection_unref(conn);