summaryrefslogtreecommitdiff
path: root/src/bluetooth-gatt.c
blob: b159fb0f20ec385e3ce9c0fe7a9d22c3d1d3c293 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdbool.h>
#include <dlog.h>
#include <bluetooth-api.h>

#include "bluetooth.h"
#include "bluetooth_private.h"

int bt_gatt_foreach_primary_services(const char *remote_address,
				bt_gatt_primary_service_cb callback,
				void *user_data)
{
	int i;
	int ret;
	bool foreach_call = true;
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_gatt_handle_info_t *prim_svc;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(callback);

	prim_svc = g_new0(bt_gatt_handle_info_t, 1);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	ret = _bt_get_error_code(bluetooth_gatt_get_primary_services(&addr_hex,
								prim_svc));
	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x) : Failed to run function",
					_bt_convert_error_to_string(ret), ret);
		g_free(prim_svc);
		return ret;
	}

	for (i = 0; i < prim_svc->count; i++) {
		if (prim_svc->handle[i] == NULL)
			continue;

		BT_DBG("handle: %s", prim_svc->handle[i]);

		if (foreach_call == true &&
		    !callback((bt_gatt_attribute_h)prim_svc->handle[i], user_data)) {
			foreach_call = false;
		}

		/* Application should clone the handle using API in callback */
		/* bt_gatt_clone_attribute_handle */
		g_free(prim_svc->handle[i]);
	}

	g_free(prim_svc->handle);
	g_free(prim_svc);

	return ret;
}

int bt_gatt_discover_characteristics(bt_gatt_attribute_h service,
				bt_gatt_characteristics_discovered_cb callback,
				void *user_data)
{
	int ret;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(service);
	BT_CHECK_INPUT_PARAMETER(callback);

	ret = _bt_get_error_code(bluetooth_gatt_discover_service_characteristics((const char *)service));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		_bt_set_cb(BT_EVENT_GATT_CHARACTERISTIC_DISCOVERED, callback, user_data);
	}

	return ret;
}

int bt_gatt_get_service_uuid(bt_gatt_attribute_h service, char **uuid)
{
	int i;
	int ret;
	bt_gatt_service_property_t property;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(service);

	memset(&property, 0x00, sizeof(bt_gatt_service_property_t));

	ret = _bt_get_error_code(bluetooth_gatt_get_service_property((const char *)service, &property));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		*uuid = g_strdup(property.uuid);

		if (property.handle_info.count != 0 && property.handle_info.handle) {
			for (i = 0; i < property.handle_info.count; i++) {
				g_free(property.handle_info.handle[i]);
			}
			g_free(property.handle_info.handle);
		}
	}

	return ret;
}

int bt_gatt_foreach_included_services(bt_gatt_attribute_h service,
				bt_gatt_included_service_cb callback,
				void *user_data)
{
	int i;
	int ret;
	bool foreach_call = true;
	bt_gatt_service_property_t property;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(service);
	BT_CHECK_INPUT_PARAMETER(callback);

	memset(&property, 0x00, sizeof(bt_gatt_service_property_t));

	ret = _bt_get_error_code(bluetooth_gatt_get_service_property((const char *)service, &property));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		if (property.handle_info.count == 0 ||
		     property.handle_info.handle == NULL) {
			return ret;
		}

		for (i = 0; i < property.handle_info.count; i++) {
			if (property.handle_info.handle[i] == NULL)
				continue;

			if (foreach_call == true &&
			    !callback((bt_gatt_attribute_h)property.handle_info.handle[i],
					user_data)) {
				foreach_call = false;
			}

			g_free(property.handle_info.handle[i]);
		}
		g_free(property.handle_info.handle);
	}
	return ret;
}

int bt_gatt_set_characteristic_changed_cb(bt_gatt_attribute_h service,
				bt_gatt_characteristic_changed_cb callback,
				void *user_data)
{
	int ret;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(service);
	BT_CHECK_INPUT_PARAMETER(callback);

	ret = _bt_get_error_code(bluetooth_gatt_watch_characteristics((const char *)service));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		_bt_set_cb(BT_EVENT_GATT_VALUE_CHANGED, callback, user_data);
	}

	return ret;
}

int bt_gatt_unset_characteristic_changed_cb(bt_gatt_attribute_h service)
{
	int ret;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(service);

	ret = _bt_get_error_code(bluetooth_gatt_unwatch_characteristics((const char *)service));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		_bt_unset_cb(BT_EVENT_GATT_VALUE_CHANGED);
	}

	return ret;
}

int bt_gatt_get_characteristic_declaration(bt_gatt_attribute_h characteristic,
				char **uuid, unsigned char **value,
				int *value_length)
{
	int ret;
	bt_gatt_char_property_t property;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(characteristic);

	memset(&property, 0x00, sizeof(bt_gatt_service_property_t));

	ret = _bt_get_error_code(bluetooth_gatt_get_characteristics_property((const char *)characteristic, &property));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	} else {
		if (property.uuid) {
			*uuid = g_strdup(property.uuid);
			g_free(property.uuid);
		}

		if (property.val && property.val_len != 0) {
			*value = g_memdup(property.val, property.val_len);
			*value_length = property.val_len;
			g_free(property.val);
		}

		g_free(property.name);

	}

	return ret;
}

int bt_gatt_set_characteristic_value(bt_gatt_attribute_h characteristic,
				const unsigned char *value,
				int value_length)
{
	int ret;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(characteristic);
	BT_CHECK_INPUT_PARAMETER(value);

	if (value_length <= 0)
		return BT_ERROR_INVALID_PARAMETER;

	ret = _bt_get_error_code(bluetooth_gatt_set_characteristics_value((const char *)characteristic,
							(const guint8 *)value, value_length));

	if (ret != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
	}

	return ret;
}

int bt_gatt_clone_attribute_handle(bt_gatt_attribute_h *clone,
				bt_gatt_attribute_h origin)
{
	int error = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(origin);

	*clone = g_strdup((char *)origin);

	return error;
}

int bt_gatt_destroy_attribute_handle(bt_gatt_attribute_h handle)
{
	int error = BT_ERROR_NONE;

	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(handle);

	g_free(handle);

	return error;
}