summaryrefslogtreecommitdiff
path: root/android/bas.c
blob: 7342895f70e703e99ab1048573e617b6ad4e00a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
 *
 *
 *  This library is free software; you can rebastribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is bastributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>
#include <errno.h>

#include <glib.h>

#include "src/log.h"

#include "lib/uuid.h"
#include "src/shared/util.h"

#include "attrib/gattrib.h"
#include "attrib/att.h"
#include "attrib/gatt.h"

#include "android/bas.h"

#define ATT_NOTIFICATION_HEADER_SIZE 3

struct bt_bas {
	int ref_count;
	GAttrib *attrib;
	struct gatt_primary *primary;
	uint16_t handle;
	uint16_t ccc_handle;
	guint id;
};

static void bas_free(struct bt_bas *bas)
{
	bt_bas_detach(bas);

	g_free(bas->primary);
	g_free(bas);
}

struct bt_bas *bt_bas_new(void *primary)
{
	struct bt_bas *bas;

	bas = g_try_new0(struct bt_bas, 1);
	if (!bas)
		return NULL;

	if (primary)
		bas->primary = g_memdup(primary, sizeof(*bas->primary));

	return bt_bas_ref(bas);
}

struct bt_bas *bt_bas_ref(struct bt_bas *bas)
{
	if (!bas)
		return NULL;

	__sync_fetch_and_add(&bas->ref_count, 1);

	return bas;
}

void bt_bas_unref(struct bt_bas *bas)
{
	if (!bas)
		return;

	if (__sync_sub_and_fetch(&bas->ref_count, 1))
		return;

	bas_free(bas);
}

static void value_cb(const guint8 *pdu, guint16 len, gpointer user_data)
{
	DBG("Battery Level at %u", pdu[ATT_NOTIFICATION_HEADER_SIZE]);
}

static void ccc_written_cb(guint8 status, const guint8 *pdu,
					guint16 plen, gpointer user_data)
{
	struct bt_bas *bas = user_data;

	if (status != 0) {
		error("Write Scan Refresh CCC failed: %s",
						att_ecode2str(status));
		return;
	}

	DBG("Battery Level: notification enabled");

	bas->id = g_attrib_register(bas->attrib, ATT_OP_HANDLE_NOTIFY,
					bas->handle, value_cb, bas, NULL);
}

static void write_ccc(GAttrib *attrib, uint16_t handle, void *user_data)
{
	uint8_t value[2];

	put_le16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT, value);

	gatt_write_char(attrib, handle, value, sizeof(value), ccc_written_cb,
								user_data);
}


static void ccc_read_cb(guint8 status, const guint8 *pdu, guint16 len,
							gpointer user_data)
{
	struct bt_bas *bas = user_data;

	if (status != 0) {
		error("Error reading CCC value: %s", att_ecode2str(status));
		return;
	}

	write_ccc(bas->attrib, bas->ccc_handle, bas);
}

static void discover_descriptor_cb(uint8_t status, GSList *descs,
								void *user_data)
{
	struct bt_bas *bas = user_data;
	struct gatt_desc *desc;

	if (status != 0) {
		error("Discover descriptors failed: %s", att_ecode2str(status));
		return;
	}

	/* There will be only one descriptor on list and it will be CCC */
	desc = descs->data;
	bas->ccc_handle = desc->handle;

	gatt_read_char(bas->attrib, desc->handle, ccc_read_cb, bas);
}

static void bas_discovered_cb(uint8_t status, GSList *chars, void *user_data)
{
	struct bt_bas *bas = user_data;
	struct gatt_char *chr;
	uint16_t start, end;
	bt_uuid_t uuid;

	if (status) {
		error("Battery: %s", att_ecode2str(status));
		return;
	}

	chr = chars->data;
	bas->handle = chr->value_handle;

	DBG("Battery handle: 0x%04x", bas->handle);

	start = chr->value_handle + 1;
	end = bas->primary->range.end;

	bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);

	gatt_discover_desc(bas->attrib, start, end, &uuid,
						discover_descriptor_cb, bas);
}

bool bt_bas_attach(struct bt_bas *bas, void *attrib)
{
	if (!bas || bas->attrib || !bas->primary)
		return false;

	bas->attrib = g_attrib_ref(attrib);

	if (bas->handle > 0)
		return true;

	gatt_discover_char(bas->attrib, bas->primary->range.start,
					bas->primary->range.end, NULL,
					bas_discovered_cb, bas);

	return true;
}

void bt_bas_detach(struct bt_bas *bas)
{
	if (!bas || !bas->attrib)
		return;

	if (bas->id > 0) {
		g_attrib_unregister(bas->attrib, bas->id);
		bas->id = 0;
	}

	g_attrib_unref(bas->attrib);
	bas->attrib = NULL;
}