3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Nordic Semiconductor Inc.
6 * Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "lib/bluetooth.h"
40 #include "src/shared/util.h"
41 #include "src/shared/queue.h"
43 #include "attrib/att.h"
44 #include "attrib/gattrib.h"
45 #include "attrib/gatt.h"
47 #include "android/scpp.h"
49 #define SCAN_INTERVAL_WIN_UUID 0x2A4F
50 #define SCAN_REFRESH_UUID 0x2A31
52 #define SCAN_INTERVAL 0x0060
53 #define SCAN_WINDOW 0x0030
54 #define SERVER_REQUIRES_REFRESH 0x00
59 struct gatt_primary *primary;
63 uint16_t refresh_handle;
65 struct queue *gatt_op;
68 static void discover_char(struct bt_scpp *scpp, GAttrib *attrib,
69 uint16_t start, uint16_t end,
70 bt_uuid_t *uuid, gatt_cb_t func,
75 id = gatt_discover_char(attrib, start, end, uuid, func, user_data);
77 if (queue_push_head(scpp->gatt_op, UINT_TO_PTR(id)))
80 error("scpp: Could not discover characteristic");
81 g_attrib_cancel(attrib, id);
84 static void discover_desc(struct bt_scpp *scpp, GAttrib *attrib,
85 uint16_t start, uint16_t end, bt_uuid_t *uuid,
86 gatt_cb_t func, gpointer user_data)
90 id = gatt_discover_desc(attrib, start, end, uuid, func, user_data);
92 if (queue_push_head(scpp->gatt_op, UINT_TO_PTR(id)))
95 error("scpp: Could not discover descriptor");
96 g_attrib_cancel(attrib, id);
99 static void write_char(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle,
100 const uint8_t *value, size_t vlen,
101 GAttribResultFunc func,
106 id = gatt_write_char(attrib, handle, value, vlen, func, user_data);
108 if (queue_push_head(scan->gatt_op, UINT_TO_PTR(id)))
111 error("scpp: Could not read char");
112 g_attrib_cancel(attrib, id);
115 static void scpp_free(struct bt_scpp *scan)
117 bt_scpp_detach(scan);
119 g_free(scan->primary);
120 queue_destroy(scan->gatt_op, NULL); /* cleared in bt_scpp_detach */
124 struct bt_scpp *bt_scpp_new(void *primary)
126 struct bt_scpp *scan;
128 scan = g_try_new0(struct bt_scpp, 1);
132 scan->interval = SCAN_INTERVAL;
133 scan->window = SCAN_WINDOW;
135 scan->gatt_op = queue_new();
136 if (!scan->gatt_op) {
142 scan->primary = g_memdup(primary, sizeof(*scan->primary));
144 return bt_scpp_ref(scan);
147 struct bt_scpp *bt_scpp_ref(struct bt_scpp *scan)
152 __sync_fetch_and_add(&scan->ref_count, 1);
157 void bt_scpp_unref(struct bt_scpp *scan)
162 if (__sync_sub_and_fetch(&scan->ref_count, 1))
168 static void write_scan_params(GAttrib *attrib, uint16_t handle,
169 uint16_t interval, uint16_t window)
173 put_le16(interval, &value[0]);
174 put_le16(window, &value[2]);
176 gatt_write_cmd(attrib, handle, value, sizeof(value), NULL, NULL);
179 static void refresh_value_cb(const uint8_t *pdu, uint16_t len,
182 struct bt_scpp *scan = user_data;
184 DBG("Server requires refresh: %d", pdu[3]);
186 if (pdu[3] == SERVER_REQUIRES_REFRESH)
187 write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
191 static void ccc_written_cb(guint8 status, const guint8 *pdu,
192 guint16 plen, gpointer user_data)
194 struct bt_scpp *scan = user_data;
197 error("Write Scan Refresh CCC failed: %s",
198 att_ecode2str(status));
202 DBG("Scan Refresh: notification enabled");
204 scan->refresh_cb_id = g_attrib_register(scan->attrib,
205 ATT_OP_HANDLE_NOTIFY, scan->refresh_handle,
206 refresh_value_cb, scan, NULL);
209 static void write_ccc(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle,
214 put_le16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT, value);
216 write_char(scan, attrib, handle, value, sizeof(value), ccc_written_cb,
220 static void discover_descriptor_cb(uint8_t status, GSList *descs,
223 struct bt_scpp *scan = user_data;
224 struct gatt_desc *desc;
227 error("Discover descriptors failed: %s", att_ecode2str(status));
231 /* There will be only one descriptor on list and it will be CCC */
234 write_ccc(scan, scan->attrib, desc->handle, scan);
237 static void refresh_discovered_cb(uint8_t status, GSList *chars,
240 struct bt_scpp *scan = user_data;
241 struct gatt_char *chr;
246 error("Scan Refresh %s", att_ecode2str(status));
251 DBG("Scan Refresh not supported");
257 DBG("Scan Refresh handle: 0x%04x", chr->value_handle);
259 start = chr->value_handle + 1;
260 end = scan->primary->range.end;
265 scan->refresh_handle = chr->value_handle;
267 bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
269 discover_desc(scan, scan->attrib, start, end, &uuid,
270 discover_descriptor_cb, user_data);
273 static void iwin_discovered_cb(uint8_t status, GSList *chars, void *user_data)
275 struct bt_scpp *scan = user_data;
276 struct gatt_char *chr;
279 error("Discover Scan Interval Window: %s",
280 att_ecode2str(status));
285 scan->iwhandle = chr->value_handle;
287 DBG("Scan Interval Window handle: 0x%04x", scan->iwhandle);
289 write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
293 bool bt_scpp_attach(struct bt_scpp *scan, void *attrib)
295 bt_uuid_t iwin_uuid, refresh_uuid;
297 if (!scan || scan->attrib || !scan->primary)
300 scan->attrib = g_attrib_ref(attrib);
303 write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
306 bt_uuid16_create(&iwin_uuid, SCAN_INTERVAL_WIN_UUID);
307 discover_char(scan, scan->attrib, scan->primary->range.start,
308 scan->primary->range.end, &iwin_uuid,
309 iwin_discovered_cb, scan);
312 if (scan->refresh_handle)
313 scan->refresh_cb_id = g_attrib_register(scan->attrib,
314 ATT_OP_HANDLE_NOTIFY, scan->refresh_handle,
315 refresh_value_cb, scan, NULL);
317 bt_uuid16_create(&refresh_uuid, SCAN_REFRESH_UUID);
318 discover_char(scan, scan->attrib, scan->primary->range.start,
319 scan->primary->range.end, &refresh_uuid,
320 refresh_discovered_cb, scan);
326 static void cancel_gatt_req(void *data, void *user_data)
328 unsigned int id = PTR_TO_UINT(data);
329 struct bt_scpp *scan = user_data;
331 g_attrib_cancel(scan->attrib, id);
334 void bt_scpp_detach(struct bt_scpp *scan)
336 if (!scan || !scan->attrib)
339 if (scan->refresh_cb_id > 0) {
340 g_attrib_unregister(scan->attrib, scan->refresh_cb_id);
341 scan->refresh_cb_id = 0;
344 queue_foreach(scan->gatt_op, cancel_gatt_req, scan);
345 g_attrib_unref(scan->attrib);
349 bool bt_scpp_set_interval(struct bt_scpp *scan, uint16_t value)
354 /* TODO: Check valid range */
356 scan->interval = value;
361 bool bt_scpp_set_window(struct bt_scpp *scan, uint16_t value)
366 /* TODO: Check valid range */
368 scan->window = value;