3
3
* BlueZ - Bluetooth protocol stack for Linux
5
5
* Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
6
* Copyright (C) 2010 Giovanni Campagna <scampa.giovanni@gmail.com>
8
9
* This library is free software; you can redistribute it and/or
91
102
DBusGProxy *manager;
92
103
GtkTreeStore *store;
93
char *default_adapter;
94
gboolean default_adapter_powered;
104
GtkTreeRowReference *default_adapter;
99
109
PROP_DEFAULT_ADAPTER,
100
110
PROP_DEFAULT_ADAPTER_POWERED,
111
PROP_DEFAULT_ADAPTER_DISCOVERABLE,
103
114
G_DEFINE_TYPE(BluetoothClient, bluetooth_client, G_TYPE_OBJECT)
106
117
* bluetooth_type_to_string:
107
118
* @type: a #BluetoothType
109
* Return value: the string representation of the @type passed. Do not free the return value.
120
* Returns the string representation of the @type passed. Do not free the return value.
122
* Return value: a string.
111
124
const gchar *bluetooth_type_to_string(BluetoothType type)
114
127
case BLUETOOTH_TYPE_ANY:
115
return N_("All types");
128
return _("All types");
116
129
case BLUETOOTH_TYPE_PHONE:
118
131
case BLUETOOTH_TYPE_MODEM:
120
133
case BLUETOOTH_TYPE_COMPUTER:
121
return N_("Computer");
134
return _("Computer");
122
135
case BLUETOOTH_TYPE_NETWORK:
123
return N_("Network");
124
137
case BLUETOOTH_TYPE_HEADSET:
125
138
/* translators: a hands-free headset, a combination of a single speaker with a microphone */
126
return N_("Headset");
127
140
case BLUETOOTH_TYPE_HEADPHONES:
128
return N_("Headphones");
141
return _("Headphones");
129
142
case BLUETOOTH_TYPE_OTHER_AUDIO:
130
return N_("Audio device");
143
return _("Audio device");
131
144
case BLUETOOTH_TYPE_KEYBOARD:
132
return N_("Keyboard");
145
return _("Keyboard");
133
146
case BLUETOOTH_TYPE_MOUSE:
135
148
case BLUETOOTH_TYPE_CAMERA:
137
150
case BLUETOOTH_TYPE_PRINTER:
138
return N_("Printer");
139
152
case BLUETOOTH_TYPE_JOYPAD:
141
154
case BLUETOOTH_TYPE_TABLET:
156
case BLUETOOTH_TYPE_VIDEO:
157
return _("Video device");
144
return N_("Unknown");
149
164
* bluetooth_verify_address:
150
165
* @bdaddr: a string representing a Bluetooth address
152
* Return value: Whether the string is a valid Bluetooth address. This does not contact the device in any way.
167
* Returns whether the string is a valid Bluetooth address. This does not contact the device in any way.
169
* Return value: %TRUE if the address is valid, %FALSE if not.
155
172
bluetooth_verify_address (const char *bdaddr)
209
226
return BLUETOOTH_TYPE_HEADSET;
211
228
return BLUETOOTH_TYPE_HEADPHONES;
230
case 0x0c: /* Video Camera */
231
case 0x0d: /* Camcorder */
232
return BLUETOOTH_TYPE_VIDEO;
213
234
return BLUETOOTH_TYPE_OTHER_AUDIO;
555
578
* bluetooth_uuid_to_string:
556
579
* @uuid: a string representing a Bluetooth UUID
558
* Return value: a string representing a human-readable (but not usable for display to users) version of the @uuid. Do not free the return value.
581
* Returns a string representing a human-readable (but not usable for display to users) version of the @uuid. Do not free the return value.
583
* Return value: a string.
561
586
bluetooth_uuid_to_string (const char *uuid)
881
906
BLUETOOTH_COLUMN_POWERED, powered, -1);
882
907
gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
883
908
BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
884
if (is_default != FALSE && powered != priv->default_adapter_powered) {
885
priv->default_adapter_powered = powered;
909
if (is_default != FALSE)
886
910
g_object_notify (G_OBJECT (client), "default-adapter-powered");
888
912
} else if (g_str_equal(property, "Discoverable") == TRUE) {
889
913
gboolean discoverable = g_value_get_boolean(value);
891
916
gtk_tree_store_set(priv->store, &iter,
892
BLUETOOTH_COLUMN_DISCOVERABLE, discoverable, -1);
917
BLUETOOTH_COLUMN_DISCOVERABLE, discoverable, -1);
918
gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
919
BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
920
if (is_default != FALSE)
921
g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
1007
1036
while (cont == TRUE) {
1008
1037
DBusGProxy *adapter;
1009
1038
const char *adapter_path;
1039
gboolean found, was_default;
1012
1041
gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
1013
BLUETOOTH_COLUMN_PROXY, &adapter, -1);
1042
BLUETOOTH_COLUMN_PROXY, &adapter,
1043
BLUETOOTH_COLUMN_DEFAULT, &was_default, -1);
1015
1045
adapter_path = dbus_g_proxy_get_path(adapter);
1029
1059
g_object_unref(adapter);
1031
1061
if (found == TRUE) {
1032
cont = gtk_tree_store_remove(priv->store, &iter);
1063
gtk_tree_row_reference_free (priv->default_adapter);
1064
priv->default_adapter = NULL;
1065
g_object_notify (G_OBJECT (client), "default-adapter");
1066
g_object_notify (G_OBJECT (client), "default-adapter-powered");
1067
g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
1069
gtk_tree_store_remove(priv->store, &iter);
1036
1073
cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(priv->store),
1040
/* No adapters left in the tree? */
1041
if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL(priv->store), NULL) == 0) {
1042
g_free(priv->default_adapter);
1043
priv->default_adapter = NULL;
1044
priv->default_adapter_powered = FALSE;
1045
g_object_notify (G_OBJECT (client), "default-adapter");
1046
g_object_notify (G_OBJECT (client), "default-adapter-powered");
1050
1078
static void default_adapter_changed(DBusGProxy *manager,
1060
1088
cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1090
if (priv->default_adapter) {
1091
gtk_tree_row_reference_free (priv->default_adapter);
1092
priv->default_adapter = NULL;
1063
1095
while (cont == TRUE) {
1064
1096
DBusGProxy *adapter;
1076
1108
g_object_unref(adapter);
1079
priv->default_adapter_powered = powered;
1110
if (found != FALSE) {
1111
GtkTreePath *tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->store), &iter);
1112
priv->default_adapter = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->store), tree_path);
1113
gtk_tree_path_free (tree_path);
1081
1116
gtk_tree_store_set(priv->store, &iter,
1082
1117
BLUETOOTH_COLUMN_DEFAULT, found, -1);
1088
1123
/* Record the new default adapter */
1089
g_free(priv->default_adapter);
1090
priv->default_adapter = g_strdup(path);
1091
1124
g_object_notify (G_OBJECT (client), "default-adapter");
1092
1125
g_object_notify (G_OBJECT (client), "default-adapter-powered");
1126
g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
1095
1129
static void name_owner_changed(DBusGProxy *dbus, const char *name,
1106
1140
DBG("client %p name %s", client, name);
1142
if (priv->default_adapter) {
1143
gtk_tree_row_reference_free (priv->default_adapter);
1144
priv->default_adapter = NULL;
1108
1147
cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1215
_bluetooth_client_get_default_adapter_path (BluetoothClient *self)
1217
DBusGProxy *adapter = bluetooth_client_get_default_adapter (self);
1219
if (adapter != NULL) {
1220
const char *ret = dbus_g_proxy_get_path (adapter);
1221
g_object_unref (adapter);
1228
_bluetooth_client_get_default_adapter_powered (BluetoothClient *self)
1230
BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE (self);
1235
if (priv->default_adapter == NULL)
1238
path = gtk_tree_row_reference_get_path (priv->default_adapter);
1239
gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
1240
gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter, BLUETOOTH_COLUMN_POWERED, &ret, -1);
1176
1246
bluetooth_client_get_property (GObject *object,
1177
1247
guint property_id,
1179
1249
GParamSpec *pspec)
1181
BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE(object);
1251
BluetoothClient *self = BLUETOOTH_CLIENT (object);
1183
1253
switch (property_id) {
1184
1254
case PROP_DEFAULT_ADAPTER:
1185
g_value_set_string (value, priv->default_adapter);
1255
g_value_set_string (value, _bluetooth_client_get_default_adapter_path (self));
1187
1257
case PROP_DEFAULT_ADAPTER_POWERED:
1188
g_value_set_boolean (value, priv->default_adapter_powered);
1258
g_value_set_boolean (value, _bluetooth_client_get_default_adapter_powered (self));
1260
case PROP_DEFAULT_ADAPTER_DISCOVERABLE:
1261
g_value_set_boolean (value, bluetooth_client_get_discoverable (self));
1264
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
1270
bluetooth_client_set_property (GObject *object,
1272
const GValue *value,
1275
BluetoothClient *self = BLUETOOTH_CLIENT (object);
1277
switch (property_id) {
1278
case PROP_DEFAULT_ADAPTER_DISCOVERABLE:
1279
bluetooth_client_set_discoverable (self, g_value_get_boolean (value), 0);
1191
1282
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
1214
1305
g_object_unref(priv->store);
1216
g_free (priv->default_adapter);
1217
priv->default_adapter = NULL;
1307
if (priv->default_adapter)
1308
gtk_tree_row_reference_free (priv->default_adapter);
1219
1310
G_OBJECT_CLASS(bluetooth_client_parent_class)->finalize(client);
1231
1322
object_class->finalize = bluetooth_client_finalize;
1232
1323
object_class->get_property = bluetooth_client_get_property;
1324
object_class->set_property = bluetooth_client_set_property;
1235
1326
g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER,
1236
1327
g_param_spec_string ("default-adapter", NULL, NULL,
1238
1329
g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER_POWERED,
1239
1330
g_param_spec_boolean ("default-adapter-powered", NULL, NULL,
1240
1331
FALSE, G_PARAM_READABLE));
1332
g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER_DISCOVERABLE,
1333
g_param_spec_boolean ("default-adapter-discoverable", NULL, NULL,
1334
FALSE, G_PARAM_READWRITE));
1242
1336
dbus_g_object_register_marshaller(marshal_VOID__STRING_BOXED,
1243
1337
G_TYPE_NONE, G_TYPE_STRING,
1256
1350
* bluetooth_client_new:
1258
* Return value: a reference to the #BluetoothClient singleton. Unref the object when done.
1352
* Returns a reference to the #BluetoothClient singleton. Use #g_object_unref() the object when done.
1354
* Return value: (transfer full): a #BluetoothClient object.
1260
1356
BluetoothClient *bluetooth_client_new(void)
1277
1373
* bluetooth_client_get_model:
1278
1374
* @client: a #BluetoothClient object
1280
* Return value: an unfiltered #GtkTreeModel representing the adapter and devices available on the system.
1376
* Returns an unfiltered #GtkTreeModel representing the adapter and devices available on the system.
1378
* Return value: (transfer full): a #GtkTreeModel object.
1282
1380
GtkTreeModel *bluetooth_client_get_model (BluetoothClient *client)
1301
1399
* @data: user data to pass to gtk_tree_model_filter_set_visible_func()
1302
1400
* @destroy: a destroy function for gtk_tree_model_filter_set_visible_func()
1304
* Return value: a #GtkTreeModel of devices filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
1402
* Returns a #GtkTreeModel of devices filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
1404
* Return value: (transfer full): a #GtkTreeModel object.
1306
1406
GtkTreeModel *bluetooth_client_get_filter_model (BluetoothClient *client,
1307
1407
GtkTreeModelFilterVisibleFunc func,
1346
1446
* bluetooth_client_get_adapter_model:
1347
1447
* @client: a #BluetoothClient object
1349
* Return value: a filtered #GtkTreeModel with only adapters present.
1449
* Returns a filtered #GtkTreeModel with only adapters present.
1451
* Return value: (transfer full): a #GtkTreeModel object.
1351
1453
GtkTreeModel *bluetooth_client_get_adapter_model (BluetoothClient *client)
1361
1463
* @client: a #BluetoothClient object
1362
1464
* @adapter: a #DBusGProxy of the adapter object, or %NULL to get the default adapter.
1364
* Return value: a filtered #GtkTreeModel with only devices belonging to the selected adapter listed. Note that the model will follow a specific adapter, and will not follow the default-adapter when %NULL is passed.
1466
* Returns a filtered #GtkTreeModel with only devices belonging to the selected adapter listed. Note that the model will follow a specific adapter, and will not follow the default-adapter when %NULL is passed.
1468
* Return value: (transfer full): a #GtkTreeModel object.
1366
1470
GtkTreeModel *bluetooth_client_get_device_model (BluetoothClient *client,
1367
1471
DBusGProxy *adapter)
1419
1523
* bluetooth_client_get_device_filter_model:
1420
1524
* @client: a #BluetoothClient object
1525
* @adapter: a #DBusGProxy representing a particular adapter, or %NULL for the default adapter.
1421
1526
* @func: a #GtkTreeModelFilterVisibleFunc
1422
1527
* @data: user data to pass to gtk_tree_model_filter_set_visible_func()
1423
1528
* @destroy: a destroy function for gtk_tree_model_filter_set_visible_func()
1425
* Return value: a #GtkTreeModel of adapters filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
1530
* Returns a #GtkTreeModel of adapters filtered using the @func, @data and
1531
* @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
1533
* Return value: (transfer full): a #GtkTreeModel object.
1427
1535
GtkTreeModel *bluetooth_client_get_device_filter_model(BluetoothClient *client,
1428
1536
DBusGProxy *adapter, GtkTreeModelFilterVisibleFunc func,
1446
1554
* bluetooth_client_get_default_adapter:
1447
1555
* @client: a #BluetoothClient object
1449
* Return value: a #DBusGProxy object representing the default adapter, or %NULL if no adapters are present.
1557
* Returns a #DBusGProxy object representing the default adapter, or %NULL if no adapters are present.
1559
* Return value: (transfer full): a #DBusGProxy object.
1451
1561
DBusGProxy *bluetooth_client_get_default_adapter(BluetoothClient *client)
1453
1563
BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE(client);
1454
1565
GtkTreeIter iter;
1566
DBusGProxy *adapter;
1457
1568
g_return_val_if_fail (BLUETOOTH_IS_CLIENT (client), NULL);
1459
1570
DBG("client %p", client);
1461
cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1464
while (cont == TRUE) {
1465
DBusGProxy *adapter;
1466
gboolean is_default;
1468
gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
1469
BLUETOOTH_COLUMN_PROXY, &adapter,
1470
BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
1472
if (is_default == TRUE)
1475
g_object_unref(adapter);
1477
cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(priv->store),
1572
if (priv->default_adapter == NULL)
1575
path = gtk_tree_row_reference_get_path (priv->default_adapter);
1576
gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
1577
gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
1578
BLUETOOTH_COLUMN_PROXY, &adapter, -1);
1638
* bluetooth_client_get_discoverable:
1639
* @client: a #BluetoothClient
1641
* Gets the default adapter's discoverable status, cached in the adapter model.
1643
* Returns: the discoverable status, or FALSE if no default adapter exists
1646
bluetooth_client_get_discoverable (BluetoothClient *client)
1648
BluetoothClientPrivate *priv;
1653
g_return_val_if_fail (BLUETOOTH_IS_CLIENT (client), FALSE);
1655
priv = BLUETOOTH_CLIENT_GET_PRIVATE (client);
1656
if (priv->default_adapter == NULL)
1659
path = gtk_tree_row_reference_get_path (priv->default_adapter);
1660
gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
1661
gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
1662
BLUETOOTH_COLUMN_DISCOVERABLE, &ret, -1);
1539
1668
* bluetooth_client_set_discoverable:
1540
1669
* @client: a #BluetoothClient object
1541
1670
* @discoverable: whether the device should be discoverable
1671
* @timeout: timeout in seconds for making undiscoverable, or 0 for never.
1543
1673
* Sets the default adapter's discoverable status.
1564
1696
g_value_init (&disco, G_TYPE_BOOLEAN);
1565
g_value_init (&timeout, G_TYPE_UINT);
1697
g_value_init (&timeoutv, G_TYPE_UINT);
1567
1699
g_value_set_boolean (&disco, discoverable);
1568
g_value_set_uint (&timeout, 0);
1570
ret = dbus_g_proxy_call (adapter, "SetProperty", NULL,
1700
g_value_set_uint (&timeoutv, timeout);
1703
ret = dbus_g_proxy_call (adapter, "SetProperty", &error,
1704
G_TYPE_STRING, "DiscoverableTimeout",
1705
G_TYPE_VALUE, &timeoutv,
1706
G_TYPE_INVALID, G_TYPE_INVALID);
1711
ret = dbus_g_proxy_call (adapter, "SetProperty", &error,
1571
1712
G_TYPE_STRING, "Discoverable",
1572
1713
G_TYPE_VALUE, &disco,
1573
1714
G_TYPE_INVALID, G_TYPE_INVALID);
1577
ret = dbus_g_proxy_call (adapter, "SetProperty", NULL,
1578
G_TYPE_STRING, "DiscoverableTimeout",
1579
G_TYPE_VALUE, &timeout,
1580
G_TYPE_INVALID, G_TYPE_INVALID);
1583
1718
g_value_unset (&disco);
1584
g_value_unset (&timeout);
1719
g_value_unset (&timeoutv);
1586
1721
g_object_unref(adapter);
1724
g_warning ("Cannot set discoverable: %s", error->message);
1725
g_error_free (error);
1743
1884
g_object_unref(proxy);
1888
* bluetooth_client_connect_service:
1890
* @client: a #BluetoothClient
1891
* @device: the DBUS path on which to operate
1892
* @func: (scope async): a callback to call when the connection is complete
1895
* Returns: TRUE if the operation was started successfully, FALSE otherwise
1746
1897
gboolean bluetooth_client_connect_service(BluetoothClient *client,
1747
1898
const char *device,
1748
1899
BluetoothClientConnectFunc func,
2029
* bluetooth_client_disconnect_service:
2031
* @client: a #BluetoothClient
2032
* @device: the DBUS path on which to operate
2033
* @func: (scope async): a callback to call when the disconnection is complete
2036
* Returns: TRUE if the operation was started successfully, FALSE otherwise
1877
2038
gboolean bluetooth_client_disconnect_service (BluetoothClient *client,
1878
2039
const char *device,
1879
2040
BluetoothClientConnectFunc func,
1939
2100
services_foreach (const char *service, gpointer _value, GString *str)
1941
gboolean value = GPOINTER_TO_INT (_value);
1942
g_string_append_printf (str, "%s (%s) ", service, value ? "connected" : "not connected");
2104
BluetoothStatus status = GPOINTER_TO_INT (_value);
2106
eclass = g_type_class_ref (BLUETOOTH_TYPE_STATUS);
2107
ev = g_enum_get_value (eclass, status);
2109
g_warning ("Unknown status value %d", status);
2111
g_string_append_printf (str, "%s (%s) ", service, ev ? ev->value_nick : "unknown");
2112
g_type_class_unref (eclass);