summaryrefslogtreecommitdiff
path: root/src/bluetooth-pbap.c
blob: d6b5af150a70f1261745f309f7d9ed3c55dc2a1c (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 <glib.h>
#include <dlog.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <bluetooth-api.h>

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

#define BT_CHECK_PBAP_CLIENT_SUPPORT() \
{ \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_PBAP_CLIENT); \
}
/* LCOV_EXCL_START */
int bt_pbap_client_initialize(void)
{
	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	int error_code = BT_ERROR_NONE;
	error_code = _bt_get_error_code(bluetooth_pbap_init());
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_pbap_client_deinitialize(void)
{
	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	int error_code = BT_ERROR_NONE;
	error_code = _bt_get_error_code(bluetooth_pbap_deinit());
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_pbap_client_set_connection_state_changed_cb(bt_pbap_connection_state_changed_cb callback, void *user_data)
{
	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_PBAP_CONNECTION_STATUS, callback, user_data);
	return BT_ERROR_NONE;

}
int bt_pbap_client_unset_connection_state_changed_cb(void)
{
	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	if (_bt_check_cb(BT_EVENT_PBAP_CONNECTION_STATUS) == true)
		_bt_unset_cb(BT_EVENT_PBAP_CONNECTION_STATUS);
	return BT_ERROR_NONE;
}

int bt_pbap_client_connect(const char *address)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);

	_bt_convert_address_to_hex(&addr_hex, address);

	error_code = _bt_get_error_code(bluetooth_pbap_connect(&addr_hex));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_pbap_client_disconnect(const char *address)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);

	_bt_convert_address_to_hex(&addr_hex, address);

	error_code = _bt_get_error_code(bluetooth_pbap_disconnect(&addr_hex));
	if (error_code != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);

	return error_code;
}

int bt_pbap_client_is_connected(const char *address, bool *connected_status)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	int ret;
	gboolean is_connected = FALSE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);

	_bt_convert_address_to_hex(&addr_hex, address);

	ret = _bt_get_error_code(bluetooth_is_device_connected(&addr_hex,
								BLUETOOTH_PBAP_SERVICE, &is_connected));
	*connected_status = is_connected ? true : false;

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

	return ret;

}

int bt_pbap_client_get_phone_book_size(const char *address, bt_pbap_address_book_source_e source,
		bt_pbap_folder_type_e folder_type, bt_pbap_phone_book_size_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_pbap_folder_t folder = { 0, };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);
	BT_CHECK_INPUT_PARAMETER(callback);

	folder.addressbook = source;
	folder.folder_type = folder_type;
	_bt_convert_address_to_hex(&addr_hex, address);
	_bt_set_cb(BT_EVENT_PBAP_PHONEBOOK_SIZE, callback, user_data);
	error_code = _bt_get_error_code(bluetooth_pbap_get_phonebook_size(&addr_hex, &folder));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
		_bt_unset_cb(BT_EVENT_PBAP_PHONEBOOK_SIZE);
	}

	return error_code;
}

int bt_pbap_client_get_phone_book(const char *address, bt_pbap_address_book_source_e source,
		bt_pbap_folder_type_e folder_type, bt_pbap_vcard_format_e format,
		bt_pbap_sort_order_e order, unsigned short offset,
		unsigned short max_list_count, unsigned int fields,
		bt_pbap_phone_book_received_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_pbap_pull_parameters_t app_param = { 0, };
	bt_pbap_folder_t folder = { 0, };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);
	BT_CHECK_INPUT_PARAMETER(callback);

	/* To get size of phonebook, a separate API is provided
	 * Hence, passing max_list_count as 0 is restricted. */
	if (max_list_count <= 0)
		return BT_ERROR_INVALID_PARAMETER;

	/* Maximum value of maxlistcount is 65535 */
	if (max_list_count > 65535)
		max_list_count = 65535;

	app_param.format = format;
	app_param.order = order;
	app_param.offset = offset;
	app_param.maxlist = max_list_count;
	app_param.fields = fields;

	folder.addressbook = source;
	folder.folder_type = folder_type;

	_bt_convert_address_to_hex(&addr_hex, address);
	_bt_set_cb(BT_EVENT_PBAP_PHONEBOOK_PULL, callback, user_data);
	error_code = _bt_get_error_code(bluetooth_pbap_get_phonebook(&addr_hex, &folder, &app_param));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
		_bt_unset_cb(BT_EVENT_PBAP_PHONEBOOK_PULL);
	}

	return error_code;
}

int bt_pbap_client_get_list(const char *address, bt_pbap_address_book_source_e source,
		bt_pbap_folder_type_e folder_type, bt_pbap_sort_order_e order,
		unsigned short offset, unsigned short max_list_count,
		bt_pbap_list_vcards_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_pbap_list_parameters_t app_param = { 0, };
	bt_pbap_folder_t folder = { 0, };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);
	BT_CHECK_INPUT_PARAMETER(callback);

	_bt_convert_address_to_hex(&addr_hex, address);
	app_param.order = order;
	app_param.offset = offset;
	app_param.maxlist = max_list_count;

	folder.addressbook = source;
	folder.folder_type = folder_type;

	_bt_set_cb(BT_EVENT_PBAP_VCARD_LIST, callback, user_data);
	error_code = _bt_get_error_code(bluetooth_pbap_get_list(&addr_hex, &folder, &app_param));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
		_bt_unset_cb(BT_EVENT_PBAP_VCARD_LIST);
	}

	return error_code;
}

int bt_pbap_client_pull_vcard(const char *address, bt_pbap_address_book_source_e source,
		bt_pbap_folder_type_e folder_type, int index,
		bt_pbap_vcard_format_e format, unsigned int fields,
		bt_pbap_phone_book_received_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_pbap_pull_vcard_parameters_t app_param = { 0, };
	bt_pbap_folder_t folder = { 0, };

	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);
	BT_CHECK_INPUT_PARAMETER(callback);

	_bt_convert_address_to_hex(&addr_hex, address);
	app_param.format = format;
	app_param.fields = fields;
	app_param.index = index;

	folder.addressbook = source;
	folder.folder_type = folder_type;

	_bt_set_cb(BT_EVENT_PBAP_VCARD_PULL, callback, user_data);
	error_code = _bt_get_error_code(bluetooth_pbap_pull_vcard(&addr_hex, &folder, &app_param));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
		_bt_unset_cb(BT_EVENT_PBAP_VCARD_PULL);
	}

	return error_code;
}

int bt_pbap_client_search_phone_book(const char *address,
		bt_pbap_address_book_source_e source, bt_pbap_folder_type_e folder_type,
		bt_pbap_search_field_e search_attribute, const char *search_value,
		bt_pbap_sort_order_e order,
		unsigned short offset, unsigned short max_list_count,
		bt_pbap_list_vcards_cb callback, void *user_data)
{
	bluetooth_device_address_t addr_hex = { {0,} };
	bt_pbap_folder_t folder = { 0, };
	bt_pbap_search_parameters_t app_param = { 0, };
	int error_code = BT_ERROR_NONE;

	BT_CHECK_PBAP_CLIENT_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(address);
	BT_CHECK_INPUT_PARAMETER(callback);

	_bt_convert_address_to_hex(&addr_hex, address);
	folder.addressbook = source;
	folder.folder_type = folder_type;

	app_param.order = order;
	app_param.offset = offset;
	app_param.maxlist = max_list_count;
	app_param.search_attribute = search_attribute;
	strncpy(app_param.search_value, search_value,
			BLUETOOTH_PBAP_MAX_SEARCH_VALUE_LENGTH - 1);

	_bt_set_cb(BT_EVENT_PBAP_PHONEBOOK_SEARCH, callback, user_data);
	error_code = _bt_get_error_code(bluetooth_pbap_phonebook_search(&addr_hex, &folder, &app_param));
	if (error_code != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
		_bt_unset_cb(BT_EVENT_PBAP_PHONEBOOK_SEARCH);
	}

	return error_code;
}
/* LCOV_EXCL_STOP */