summaryrefslogtreecommitdiff
path: root/src/bt-device.c
blob: 2ba1172f9ced493c6da0b9edc7dd4c2e5cdaad3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *
 *  bluez-tools - a set of tools to manage bluetooth devices for linux
 *
 *  Copyright (C) 2010  Alexander Orlenko <zxteam@gmail.com>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <glib.h>

#include "lib/dbus-common.h"
#include "lib/helpers.h"
#include "lib/adapter.h"
#include "lib/device.h"
#include "lib/agent.h"

static gchar *adapter_arg = NULL;
static gboolean list_arg = FALSE;
static gchar *connect_arg = NULL;
static gchar *remove_arg = NULL;
static gchar *info_arg = NULL;
static gchar *services_arg = NULL;
static gboolean set_arg = FALSE;
static gchar *set_device_arg = NULL;
static gchar *set_name_arg = NULL;
static gchar *set_value_arg = NULL;

static GOptionEntry entries[] = {
	{ "adapter", 'a', 0, G_OPTION_ARG_STRING, &adapter_arg, "Adapter name or MAC", "adapter#id"},
	{ "list", 'l', 0, G_OPTION_ARG_NONE, &list_arg, "List added devices", NULL},
	{ "connect", 'c', 0, G_OPTION_ARG_STRING, &connect_arg, "Connect to a device", "device#id"},
	{ "remove", 'r', 0, G_OPTION_ARG_STRING, &remove_arg, "Remove device", "device#id"},
	{ "info", 'i', 0, G_OPTION_ARG_STRING, &info_arg, "Get info about device", "device#id"},
	{ "services", 's', 0, G_OPTION_ARG_STRING, &services_arg, "Discover device services", "device#id"},
	{ "set", 0, 0, G_OPTION_ARG_NONE, &set_arg, "Set property", NULL},
	{ NULL}
};

int main(int argc, char *argv[])
{
	GError *error = NULL;
	GOptionContext *context;

	g_type_init();

	context = g_option_context_new("[--set device#id Name Value] - a bluetooth device manager");
	g_option_context_add_main_entries(context, entries, NULL);
	g_option_context_set_summary(context, "summary");
	g_option_context_set_description(context, "desc");

	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		g_print("%s: %s\n", g_get_prgname(), error->message);
		g_print("Try `%s --help` for more information.\n", g_get_prgname());
		exit(EXIT_FAILURE);
	} else if (!list_arg && !connect_arg && !remove_arg && !info_arg && !services_arg && !set_arg) {
		g_print("%s", g_option_context_get_help(context, FALSE, NULL));
		exit(EXIT_FAILURE);
	} else if (set_arg && argc != 4) {
		g_print("%s: Invalid arguments for --set\n", g_get_prgname());
		g_print("Try `%s --help` for more information.\n", g_get_prgname());
		exit(EXIT_FAILURE);
	}

	g_option_context_free(context);

	if (!dbus_connect(&error)) {
		g_printerr("Couldn't connect to dbus: %s", error->message);
		exit(EXIT_FAILURE);
	}

	Adapter *adapter = find_adapter(adapter_arg, &error);
	exit_if_error(error);

	if (list_arg) {
		const GPtrArray *devices_list = adapter_get_devices(adapter);
		g_assert(devices_list != NULL);

		g_print("Added devices:\n");
		if (devices_list->len == 0) {
			g_print("no devices found\n");
		}

		for (int i = 0; i < devices_list->len; i++) {
			const gchar *device_path = g_ptr_array_index(devices_list, i);
			Device *device = g_object_new(DEVICE_TYPE, "DBusObjectPath", device_path, NULL);
			g_print("%s (%s)\n", device_get_name(device), device_get_address(device));
			g_object_unref(device);
		}
	} else if (connect_arg) {
		g_print("Connecting to: %s\n", connect_arg);
		Agent *agent = g_object_new(AGENT_TYPE, NULL);
		adapter_create_paired_device(adapter, connect_arg, AGENT_DBUS_PATH, "DisplayYesNo", &error);
		exit_if_error(error);
		g_object_unref(agent);
	} else if (remove_arg) {
		Device *device = find_device(adapter, remove_arg, &error);
		exit_if_error(error);

		adapter_remove_device(adapter, device_get_dbus_object_path(device), &error);
		exit_if_error(error);

		g_print("Done\n");
		g_object_unref(device);
	} else if (info_arg) {
		Device *device = find_device(adapter, info_arg, &error);
		exit_if_error(error);

		g_print("[%s]\n", device_get_address(device));
		g_print("  Name: %s\n", device_get_name(device));
		g_print("  Alias: %s\n", device_get_alias(device));
		g_print("  Address: %s\n", device_get_address(device));
		g_print("  Class: %x\n", device_get_class(device));

		g_object_unref(device);
	} else if (services_arg) {
		Device *device = find_device(adapter, services_arg, &error);
		exit_if_error(error);

	} else if (set_arg) {
		Device *device = find_device(adapter, set_device_arg, &error);
		exit_if_error(error);

	}

	g_object_unref(adapter);

	exit(EXIT_SUCCESS);
}