3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Texas Instruments Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <bluetooth/uuid.h>
31 #include <dbus/dbus.h>
35 #include "att-database.h"
39 #include "gatt-service.h"
40 #include "attrib-server.h"
43 #include "dbus-common.h"
47 #define BLUEZ_SERVICE "org.bluez"
49 struct link_loss_adapter {
50 struct btd_adapter *adapter;
51 uint16_t alert_lvl_value_handle;
53 GSList *connected_devices;
56 struct connected_device {
57 struct btd_device *device;
58 struct link_loss_adapter *adapter;
64 static GSList *link_loss_adapters;
66 static int lldevice_cmp(gconstpointer a, gconstpointer b)
68 const struct connected_device *llcondev = a;
69 const struct btd_device *device = b;
71 if (llcondev->device == device)
77 static struct connected_device *
78 find_connected_device(struct link_loss_adapter *la, struct btd_device *device)
80 GSList *l = g_slist_find_custom(la->connected_devices, device,
88 static int lladapter_cmp(gconstpointer a, gconstpointer b)
90 const struct link_loss_adapter *lladapter = a;
91 const struct btd_adapter *adapter = b;
93 if (lladapter->adapter == adapter)
99 static struct link_loss_adapter *
100 find_link_loss_adapter(struct btd_adapter *adapter)
102 GSList *l = g_slist_find_custom(link_loss_adapters, adapter,
110 const char *link_loss_get_alert_level(struct btd_device *device)
112 struct link_loss_adapter *lladapter;
113 struct connected_device *condev;
116 return get_alert_level_string(NO_ALERT);
118 lladapter = find_link_loss_adapter(device_get_adapter(device));
120 return get_alert_level_string(NO_ALERT);
122 condev = find_connected_device(lladapter, device);
124 return get_alert_level_string(NO_ALERT);
126 return get_alert_level_string(condev->alert_level);
129 static void link_loss_emit_alert_signal(struct connected_device *condev)
131 struct link_loss_adapter *adapter = condev->adapter;
132 const char *alert_level_str, *path;
137 path = device_get_path(condev->device);
138 alert_level_str = get_alert_level_string(condev->alert_level);
140 DBG("alert %s remote %s", alert_level_str, path);
142 emit_property_changed(adapter->conn, path,
143 PROXIMITY_REPORTER_INTERFACE, "LinkLossAlertLevel",
144 DBUS_TYPE_STRING, &alert_level_str);
147 static uint8_t link_loss_alert_lvl_read(struct attribute *a,
148 struct btd_device *device, gpointer user_data)
150 struct link_loss_adapter *la = user_data;
151 struct connected_device *condev;
152 uint8_t alert_level = NO_ALERT;
157 condev = find_connected_device(la, device);
161 alert_level = condev->alert_level;
164 DBG("return alert level %d for dev %p", alert_level, device);
166 /* update the alert level according to the requesting device */
167 attrib_db_update(la->adapter, a->handle, NULL, &alert_level,
168 sizeof(alert_level), NULL);
173 /* condev can be NULL */
174 static void link_loss_remove_condev(struct connected_device *condev)
176 struct link_loss_adapter *la;
181 la = condev->adapter;
183 if (condev->callback_id && condev->device)
184 btd_device_remove_attio_callback(condev->device,
185 condev->callback_id);
187 if (condev->local_disc_id && condev->device)
188 device_remove_disconnect_watch(condev->device,
189 condev->local_disc_id);
192 btd_device_unref(condev->device);
194 la->connected_devices = g_slist_remove(la->connected_devices, condev);
198 static void link_loss_disc_cb(gpointer user_data)
200 struct connected_device *condev = user_data;
202 DBG("alert loss disconnect device %p", condev->device);
204 /* if an alert-level is set, emit a signal */
205 if (condev->alert_level != NO_ALERT)
206 link_loss_emit_alert_signal(condev);
208 /* we are open for more changes now */
209 link_loss_remove_condev(condev);
212 static void link_loss_local_disc(struct btd_device *device,
213 gboolean removal, void *user_data)
215 struct connected_device *condev = user_data;
217 /* no need to alert on this device - we requested disconnection */
218 link_loss_remove_condev(condev);
220 DBG("alert level zeroed for locally disconnecting dev %p", device);
223 static uint8_t link_loss_alert_lvl_write(struct attribute *a,
224 struct btd_device *device, gpointer user_data)
227 struct link_loss_adapter *la = user_data;
228 struct connected_device *condev = NULL;
233 /* condev might remain NULL here if nothing is found */
234 condev = find_connected_device(la, device);
237 DBG("Illegal alert level length");
242 if (value != NO_ALERT && value != MILD_ALERT && value != HIGH_ALERT) {
243 DBG("Illegal alert value");
247 /* Register a disconnect cb if the alert level is non-zero */
248 if (value != NO_ALERT && !condev) {
249 condev = g_new0(struct connected_device, 1);
250 condev->device = btd_device_ref(device);
251 condev->adapter = la;
252 condev->callback_id = btd_device_add_attio_callback(device,
253 NULL, link_loss_disc_cb, condev);
254 condev->local_disc_id = device_add_disconnect_watch(device,
255 link_loss_local_disc, condev, NULL);
257 la->connected_devices = g_slist_append(la->connected_devices,
259 } else if (value == NO_ALERT && condev) {
260 link_loss_remove_condev(condev);
264 DBG("alert level set to %d by device %p", value, device);
267 condev->alert_level = value;
272 error("Set link loss alert level for dev %p", device);
273 /* reset alert level on erroneous devices */
274 link_loss_remove_condev(condev);
278 void link_loss_register(struct btd_adapter *adapter, DBusConnection *conn)
282 struct link_loss_adapter *lladapter;
284 bt_uuid16_create(&uuid, LINK_LOSS_SVC_UUID);
286 lladapter = g_new0(struct link_loss_adapter, 1);
287 lladapter->adapter = adapter;
288 lladapter->conn = dbus_connection_ref(conn);
290 link_loss_adapters = g_slist_append(link_loss_adapters, lladapter);
292 /* Link Loss Service */
293 svc_added = gatt_service_add(adapter,
294 GATT_PRIM_SVC_UUID, &uuid,
295 /* Alert level characteristic */
296 GATT_OPT_CHR_UUID, ALERT_LEVEL_CHR_UUID,
298 ATT_CHAR_PROPER_READ | ATT_CHAR_PROPER_WRITE,
299 GATT_OPT_CHR_VALUE_CB, ATTRIB_READ,
300 link_loss_alert_lvl_read, lladapter,
301 GATT_OPT_CHR_VALUE_CB, ATTRIB_WRITE,
302 link_loss_alert_lvl_write, lladapter,
303 GATT_OPT_CHR_VALUE_GET_HANDLE,
304 &lladapter->alert_lvl_value_handle,
310 DBG("Link Loss service added");
314 error("Error adding Link Loss service");
315 link_loss_unregister(adapter);
318 static void remove_condev_list_item(gpointer data, gpointer user_data)
320 struct connected_device *condev = data;
322 link_loss_remove_condev(condev);
325 void link_loss_unregister(struct btd_adapter *adapter)
327 struct link_loss_adapter *lladapter;
328 lladapter = find_link_loss_adapter(adapter);
332 g_slist_foreach(lladapter->connected_devices, remove_condev_list_item,
334 dbus_connection_unref(lladapter->conn);
336 link_loss_adapters = g_slist_remove(link_loss_adapters, lladapter);