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
34 #include "lib/bluetooth.h"
38 #include "src/shared/util.h"
39 #include "src/shared/queue.h"
41 #include "attrib/gattrib.h"
42 #include "attrib/att.h"
43 #include "attrib/gatt.h"
45 #include "android/dis.h"
56 GAttrib *attrib; /* GATT connection */
57 struct gatt_primary *primary; /* Primary details */
60 struct queue *gatt_op;
63 struct characteristic {
64 struct gatt_char attr; /* Characteristic */
65 struct bt_dis *d; /* deviceinfo where the char belongs */
74 static void destroy_gatt_req(struct gatt_request *req)
76 queue_remove(req->dis->gatt_op, req);
77 bt_dis_unref(req->dis);
81 static void dis_free(struct bt_dis *dis)
86 queue_destroy(dis->gatt_op, (void *) destroy_gatt_req);
90 struct bt_dis *bt_dis_new(void *primary)
94 dis = g_try_new0(struct bt_dis, 1);
98 dis->gatt_op = queue_new();
105 dis->primary = g_memdup(primary, sizeof(*dis->primary));
107 return bt_dis_ref(dis);
110 struct bt_dis *bt_dis_ref(struct bt_dis *dis)
115 __sync_fetch_and_add(&dis->ref_count, 1);
120 void bt_dis_unref(struct bt_dis *dis)
125 if (__sync_sub_and_fetch(&dis->ref_count, 1))
131 static struct gatt_request *create_request(struct bt_dis *dis,
134 struct gatt_request *req;
136 req = new0(struct gatt_request, 1);
140 req->user_data = user_data;
141 req->dis = bt_dis_ref(dis);
146 static bool set_and_store_gatt_req(struct bt_dis *dis,
147 struct gatt_request *req,
151 return queue_push_head(dis->gatt_op, req);
154 static void read_pnpid_cb(guint8 status, const guint8 *pdu, guint16 len,
157 struct gatt_request *req = user_data;
158 struct bt_dis *dis = req->user_data;
159 uint8_t value[PNP_ID_SIZE];
162 destroy_gatt_req(req);
165 error("Error reading PNP_ID value: %s", att_ecode2str(status));
169 vlen = dec_read_resp(pdu, len, value, sizeof(value));
171 error("Error reading PNP_ID: Protocol error");
176 error("Error reading PNP_ID: Invalid pdu length received");
180 dis->source = value[0];
181 dis->vendor = get_le16(&value[1]);
182 dis->product = get_le16(&value[3]);
183 dis->version = get_le16(&value[5]);
185 DBG("source: 0x%02X vendor: 0x%04X product: 0x%04X version: 0x%04X",
186 dis->source, dis->vendor, dis->product, dis->version);
189 dis->notify(dis->source, dis->vendor, dis->product,
190 dis->version, dis->notify_data);
193 static void read_char(struct bt_dis *dis, GAttrib *attrib, uint16_t handle,
194 GAttribResultFunc func, gpointer user_data)
196 struct gatt_request *req;
199 req = create_request(dis, user_data);
203 id = gatt_read_char(attrib, handle, func, req);
205 if (set_and_store_gatt_req(dis, req, id))
208 error("dis: Could not read characteristic");
209 g_attrib_cancel(attrib, id);
213 static void discover_char(struct bt_dis *dis, GAttrib *attrib,
214 uint16_t start, uint16_t end,
215 bt_uuid_t *uuid, gatt_cb_t func,
218 struct gatt_request *req;
221 req = create_request(dis, user_data);
225 id = gatt_discover_char(attrib, start, end, uuid, func, req);
227 if (set_and_store_gatt_req(dis, req, id))
230 error("dis: Could not send discover characteristic");
231 g_attrib_cancel(attrib, id);
235 static void configure_deviceinfo_cb(uint8_t status, GSList *characteristics,
238 struct gatt_request *req = user_data;
239 struct bt_dis *d = req->user_data;
242 destroy_gatt_req(req);
245 error("Discover deviceinfo characteristics: %s",
246 att_ecode2str(status));
250 for (l = characteristics; l; l = l->next) {
251 struct gatt_char *c = l->data;
253 if (strcmp(c->uuid, PNPID_UUID) == 0) {
254 d->handle = c->value_handle;
255 read_char(d, d->attrib, d->handle, read_pnpid_cb, d);
261 bool bt_dis_attach(struct bt_dis *dis, void *attrib)
263 struct gatt_primary *primary = dis->primary;
265 if (dis->attrib || !primary)
268 dis->attrib = g_attrib_ref(attrib);
271 discover_char(dis, dis->attrib, primary->range.start,
272 primary->range.end, NULL,
273 configure_deviceinfo_cb, dis);
278 static void cancel_gatt_req(struct gatt_request *req)
280 if (g_attrib_cancel(req->dis->attrib, req->id))
281 destroy_gatt_req(req);
284 void bt_dis_detach(struct bt_dis *dis)
289 queue_foreach(dis->gatt_op, (void *) cancel_gatt_req, NULL);
290 g_attrib_unref(dis->attrib);
294 bool bt_dis_set_notification(struct bt_dis *dis, bt_dis_notify func,
301 dis->notify_data = user_data;