3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Texas Instruments, Inc.
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>
38 #include "deviceinfo.h"
41 struct btd_device *dev; /* Device reference */
42 GAttrib *attrib; /* GATT connection */
43 guint attioid; /* Att watcher id */
44 struct att_range *svc_range; /* DeviceInfo range */
45 GSList *chars; /* Characteristics */
48 static GSList *servers = NULL;
50 struct characteristic {
51 struct gatt_char attr; /* Characteristic */
52 struct deviceinfo *d; /* deviceinfo where the char belongs */
55 static void deviceinfo_free(gpointer user_data)
57 struct deviceinfo *d = user_data;
60 btd_device_remove_attio_callback(d->dev, d->attioid);
62 if (d->attrib != NULL)
63 g_attrib_unref(d->attrib);
65 g_slist_free_full(d->chars, g_free);
67 btd_device_unref(d->dev);
72 static gint cmp_device(gconstpointer a, gconstpointer b)
74 const struct deviceinfo *d = a;
75 const struct btd_device *dev = b;
83 static void read_pnpid_cb(guint8 status, const guint8 *pdu, guint16 len,
86 struct characteristic *ch = user_data;
87 uint8_t value[ATT_MAX_MTU];
91 error("Error reading PNP_ID value: %s", att_ecode2str(status));
95 if (!dec_read_resp(pdu, len, value, &vlen)) {
96 error("Error reading PNP_ID: Protocol error");
101 error("Error reading PNP_ID: Invalid pdu length received");
105 device_set_pnpid(ch->d->dev, value[0], att_get_u16(&value[1]),
106 att_get_u16(&value[3]), att_get_u16(&value[5]));
109 static void process_deviceinfo_char(struct characteristic *ch)
111 if (g_strcmp0(ch->attr.uuid, PNPID_UUID) == 0)
112 gatt_read_char(ch->d->attrib, ch->attr.value_handle, 0,
116 static void configure_deviceinfo_cb(GSList *characteristics, guint8 status,
119 struct deviceinfo *d = user_data;
123 error("Discover deviceinfo characteristics: %s",
124 att_ecode2str(status));
128 for (l = characteristics; l; l = l->next) {
129 struct gatt_char *c = l->data;
130 struct characteristic *ch;
132 ch = g_new0(struct characteristic, 1);
133 ch->attr.handle = c->handle;
134 ch->attr.properties = c->properties;
135 ch->attr.value_handle = c->value_handle;
136 memcpy(ch->attr.uuid, c->uuid, MAX_LEN_UUID_STR + 1);
139 d->chars = g_slist_append(d->chars, ch);
141 process_deviceinfo_char(ch);
144 static void attio_connected_cb(GAttrib *attrib, gpointer user_data)
146 struct deviceinfo *d = user_data;
148 d->attrib = g_attrib_ref(attrib);
150 gatt_discover_char(d->attrib, d->svc_range->start, d->svc_range->end,
151 NULL, configure_deviceinfo_cb, d);
154 static void attio_disconnected_cb(gpointer user_data)
156 struct deviceinfo *d = user_data;
158 g_attrib_unref(d->attrib);
162 int deviceinfo_register(struct btd_device *device, struct gatt_primary *prim)
164 struct deviceinfo *d;
166 d = g_new0(struct deviceinfo, 1);
167 d->dev = btd_device_ref(device);
168 d->svc_range = g_new0(struct att_range, 1);
169 d->svc_range->start = prim->range.start;
170 d->svc_range->end = prim->range.end;
172 servers = g_slist_prepend(servers, d);
174 d->attioid = btd_device_add_attio_callback(device, attio_connected_cb,
175 attio_disconnected_cb, d);
179 void deviceinfo_unregister(struct btd_device *device)
181 struct deviceinfo *d;
184 l = g_slist_find_custom(servers, device, cmp_device);
189 servers = g_slist_remove(servers, d);