1 /*  Bluetooth VOCS - Volume Offset Control Service - Client */
2 
3 /*
4  * Copyright (c) 2021-2022 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <errno.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <string.h>
14 
15 #include <zephyr/autoconf.h>
16 #include <zephyr/bluetooth/att.h>
17 #include <zephyr/bluetooth/audio/audio.h>
18 #include <zephyr/bluetooth/audio/vocs.h>
19 #include <zephyr/bluetooth/bluetooth.h>
20 #include <zephyr/bluetooth/l2cap.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/gatt.h>
23 #include <zephyr/bluetooth/uuid.h>
24 #include <zephyr/device.h>
25 #include <zephyr/init.h>
26 #include <zephyr/kernel.h>
27 #include <zephyr/logging/log.h>
28 #include <zephyr/sys/__assert.h>
29 #include <zephyr/sys/atomic.h>
30 #include <zephyr/sys/check.h>
31 #include <zephyr/sys/util.h>
32 #include <zephyr/types.h>
33 
34 #include "vocs_internal.h"
35 
36 LOG_MODULE_REGISTER(bt_vocs_client, CONFIG_BT_VOCS_CLIENT_LOG_LEVEL);
37 
38 static struct bt_vocs_client insts[CONFIG_BT_MAX_CONN * CONFIG_BT_VOCS_CLIENT_MAX_INSTANCE_COUNT];
39 
lookup_vocs_by_handle(struct bt_conn * conn,uint16_t handle)40 static struct bt_vocs_client *lookup_vocs_by_handle(struct bt_conn *conn, uint16_t handle)
41 {
42 	__ASSERT(handle != 0, "Handle cannot be 0");
43 	__ASSERT(conn, "Conn cannot be NULL");
44 
45 	for (int i = 0; i < ARRAY_SIZE(insts); i++) {
46 		if (insts[i].conn == conn &&
47 		    atomic_test_bit(insts[i].flags, BT_VOCS_CLIENT_FLAG_ACTIVE) &&
48 		    insts[i].start_handle <= handle && insts[i].end_handle >= handle) {
49 			return &insts[i];
50 		}
51 	}
52 
53 	LOG_DBG("Could not find VOCS instance with handle 0x%04x", handle);
54 	return NULL;
55 }
56 
vocs_client_notify_handler(struct bt_conn * conn,struct bt_gatt_subscribe_params * params,const void * data,uint16_t length)57 uint8_t vocs_client_notify_handler(struct bt_conn *conn, struct bt_gatt_subscribe_params *params,
58 				   const void *data, uint16_t length)
59 {
60 	uint16_t handle = params->value_handle;
61 	struct bt_vocs_client *inst;
62 
63 	if (conn == NULL) {
64 		return BT_GATT_ITER_CONTINUE;
65 	}
66 
67 	inst = lookup_vocs_by_handle(conn, handle);
68 
69 	if (!inst) {
70 		LOG_DBG("Instance not found");
71 		return BT_GATT_ITER_STOP;
72 	}
73 
74 	if (!data || !length) {
75 		return BT_GATT_ITER_CONTINUE;
76 	}
77 
78 	if (handle == inst->state_handle) {
79 		if (length == sizeof(inst->state)) {
80 			memcpy(&inst->state, data, length);
81 			LOG_DBG("Inst %p: Offset %d, counter %u", inst, inst->state.offset,
82 				inst->state.change_counter);
83 			if (inst->cb && inst->cb->state) {
84 				inst->cb->state(&inst->vocs, 0, inst->state.offset);
85 			}
86 		} else {
87 			LOG_DBG("Invalid state length %u", length);
88 		}
89 	} else if (handle == inst->desc_handle) {
90 		char desc[MIN(BT_L2CAP_RX_MTU, BT_ATT_MAX_ATTRIBUTE_LEN) + 1];
91 
92 		/* Truncate if too large */
93 
94 		if (length > sizeof(desc) - 1) {
95 			LOG_DBG("Description truncated from %u to %zu octets", length,
96 				sizeof(desc) - 1);
97 		}
98 		length = MIN(sizeof(desc) - 1, length);
99 
100 		memcpy(desc, data, length);
101 		desc[length] = '\0';
102 		LOG_DBG("Inst %p: Output description: %s", inst, desc);
103 		if (inst->cb && inst->cb->description) {
104 			inst->cb->description(&inst->vocs, 0, desc);
105 		}
106 	} else if (handle == inst->location_handle) {
107 		if (length == sizeof(inst->location)) {
108 			memcpy(&inst->location, data, length);
109 			LOG_DBG("Inst %p: Location %u", inst, inst->location);
110 			if (inst->cb && inst->cb->location) {
111 				inst->cb->location(&inst->vocs, 0, inst->location);
112 			}
113 		} else {
114 			LOG_DBG("Invalid location length %u", length);
115 		}
116 	}
117 
118 	return BT_GATT_ITER_CONTINUE;
119 }
120 
vocs_client_read_offset_state_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)121 static uint8_t vocs_client_read_offset_state_cb(struct bt_conn *conn, uint8_t err,
122 						struct bt_gatt_read_params *params,
123 						const void *data, uint16_t length)
124 {
125 	int cb_err = err;
126 	struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle);
127 
128 	memset(params, 0, sizeof(*params));
129 
130 	if (!inst) {
131 		LOG_DBG("Instance not found");
132 		return BT_GATT_ITER_STOP;
133 	}
134 
135 	LOG_DBG("Inst %p: err: 0x%02X", inst, err);
136 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
137 
138 	if (cb_err) {
139 		LOG_DBG("Offset state read failed: %d", err);
140 	} else if (data) {
141 		if (length == sizeof(inst->state)) {
142 			memcpy(&inst->state, data, length);
143 			LOG_DBG("Offset %d, counter %u", inst->state.offset,
144 				inst->state.change_counter);
145 		} else {
146 			LOG_DBG("Invalid length %u (expected %zu)", length,
147 				sizeof(inst->state));
148 			cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
149 		}
150 	} else {
151 		LOG_DBG("Invalid state");
152 		cb_err = BT_ATT_ERR_UNLIKELY;
153 	}
154 
155 	if (inst->cb && inst->cb->state) {
156 		inst->cb->state(&inst->vocs, cb_err, cb_err ? 0 : inst->state.offset);
157 	}
158 
159 	return BT_GATT_ITER_STOP;
160 }
161 
vocs_client_read_location_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)162 static uint8_t vocs_client_read_location_cb(struct bt_conn *conn, uint8_t err,
163 					    struct bt_gatt_read_params *params,
164 					    const void *data, uint16_t length)
165 {
166 	int cb_err = err;
167 	struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle);
168 
169 	memset(params, 0, sizeof(*params));
170 
171 	if (!inst) {
172 		LOG_DBG("Instance not found");
173 		return BT_GATT_ITER_STOP;
174 	}
175 
176 	LOG_DBG("Inst %p: err: 0x%02X", inst, err);
177 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
178 
179 	if (cb_err) {
180 		LOG_DBG("Offset state read failed: %d", err);
181 	} else if (data) {
182 		if (length == sizeof(inst->location)) {
183 			memcpy(&inst->location, data, length);
184 			LOG_DBG("Location %u", inst->location);
185 		} else {
186 			LOG_DBG("Invalid length %u (expected %zu)", length,
187 				sizeof(inst->location));
188 			cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
189 		}
190 	} else {
191 		LOG_DBG("Invalid location");
192 		cb_err = BT_ATT_ERR_UNLIKELY;
193 	}
194 
195 	if (inst->cb && inst->cb->location) {
196 		inst->cb->location(&inst->vocs, cb_err, cb_err ? 0 : inst->location);
197 	}
198 
199 	return BT_GATT_ITER_STOP;
200 }
201 
internal_read_volume_offset_state_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)202 static uint8_t internal_read_volume_offset_state_cb(struct bt_conn *conn, uint8_t err,
203 						    struct bt_gatt_read_params *params,
204 						    const void *data, uint16_t length)
205 {
206 	int cb_err = err;
207 	struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle);
208 
209 	memset(params, 0, sizeof(*params));
210 
211 	if (!inst) {
212 		LOG_ERR("Instance not found");
213 		return BT_GATT_ITER_STOP;
214 	}
215 
216 	if (err) {
217 		LOG_WRN("Volume offset state read failed: %d", err);
218 		cb_err = BT_ATT_ERR_UNLIKELY;
219 	} else if (data) {
220 		if (length == sizeof(inst->state)) {
221 			int write_err;
222 
223 			memcpy(&inst->state, data, length);
224 			LOG_DBG("Offset %d, counter %u", inst->state.offset,
225 				inst->state.change_counter);
226 
227 			/* clear busy flag to reuse function */
228 			atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
229 			write_err = bt_vocs_client_state_set(inst, inst->cp.offset);
230 			if (write_err) {
231 				cb_err = BT_ATT_ERR_UNLIKELY;
232 			}
233 		} else {
234 			LOG_DBG("Invalid length %u (expected %zu)", length,
235 				sizeof(inst->state));
236 			cb_err = BT_ATT_ERR_UNLIKELY;
237 		}
238 	} else {
239 		LOG_DBG("Invalid (empty) offset state read");
240 		cb_err = BT_ATT_ERR_UNLIKELY;
241 	}
242 
243 	if (cb_err) {
244 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
245 
246 		if (inst->cb && inst->cb->set_offset) {
247 			inst->cb->set_offset(&inst->vocs, err);
248 		}
249 	}
250 
251 	return BT_GATT_ITER_STOP;
252 }
253 
vocs_client_write_vocs_cp_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_write_params * params)254 static void vocs_client_write_vocs_cp_cb(struct bt_conn *conn, uint8_t err,
255 					 struct bt_gatt_write_params *params)
256 {
257 	int cb_err = err;
258 	struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->handle);
259 
260 	memset(params, 0, sizeof(*params));
261 
262 	if (!inst) {
263 		LOG_DBG("Instance not found");
264 		return;
265 	}
266 
267 	LOG_DBG("Inst %p: err: 0x%02X", inst, err);
268 
269 	/* If the change counter is out of data when a write was attempted from the application,
270 	 * we automatically initiate a read to get the newest state and try again. Once the
271 	 * change counter has been read, we restart the applications write request. If it fails
272 	 * the second time, we return an error to the application.
273 	 */
274 	if (cb_err == BT_VOCS_ERR_INVALID_COUNTER &&
275 	    atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED)) {
276 		cb_err = BT_ATT_ERR_UNLIKELY;
277 	} else if (cb_err == BT_VOCS_ERR_INVALID_COUNTER && inst->state_handle) {
278 		LOG_DBG("Invalid change counter. Reading volume offset state from server.");
279 
280 		inst->read_params.func = internal_read_volume_offset_state_cb;
281 		inst->read_params.handle_count = 1;
282 		inst->read_params.single.handle = inst->state_handle;
283 
284 		atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED);
285 
286 		cb_err = bt_gatt_read(conn, &inst->read_params);
287 		if (cb_err) {
288 			LOG_WRN("Could not read Volume offset state: %d", cb_err);
289 		} else {
290 			/* Wait for read callback */
291 			return;
292 		}
293 	}
294 
295 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED);
296 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
297 
298 	if (inst->cb && inst->cb->set_offset) {
299 		inst->cb->set_offset(&inst->vocs, cb_err);
300 	}
301 }
302 
vocs_client_read_output_desc_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_read_params * params,const void * data,uint16_t length)303 static uint8_t vocs_client_read_output_desc_cb(struct bt_conn *conn, uint8_t err,
304 					       struct bt_gatt_read_params *params,
305 					       const void *data, uint16_t length)
306 {
307 	int cb_err = err;
308 	struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle);
309 	char desc[MIN(BT_L2CAP_RX_MTU, BT_ATT_MAX_ATTRIBUTE_LEN) + 1];
310 
311 	memset(params, 0, sizeof(*params));
312 
313 	if (!inst) {
314 		LOG_DBG("Instance not found");
315 		return BT_GATT_ITER_STOP;
316 	}
317 
318 	LOG_DBG("Inst %p: err: 0x%02X", inst, err);
319 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
320 
321 	if (cb_err) {
322 		LOG_DBG("Description read failed: %d", err);
323 	} else {
324 		if (data) {
325 			LOG_HEXDUMP_DBG(data, length, "Output description read");
326 
327 			if (length > sizeof(desc) - 1) {
328 				LOG_DBG("Description truncated from %u to %zu octets", length,
329 					sizeof(desc) - 1);
330 			}
331 			length = MIN(sizeof(desc) - 1, length);
332 
333 			/* TODO: Handle long reads */
334 			memcpy(desc, data, length);
335 		}
336 		desc[length] = '\0';
337 		LOG_DBG("Output description: %s", desc);
338 	}
339 
340 	if (inst->cb && inst->cb->description) {
341 		inst->cb->description(&inst->vocs, cb_err, cb_err ? NULL : desc);
342 	}
343 
344 	return BT_GATT_ITER_STOP;
345 }
346 
valid_inst_discovered(struct bt_vocs_client * inst)347 static bool valid_inst_discovered(struct bt_vocs_client *inst)
348 {
349 	return inst->state_handle &&
350 		inst->control_handle &&
351 		inst->location_handle &&
352 		inst->desc_handle;
353 }
354 
vocs_discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)355 static uint8_t vocs_discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
356 				  struct bt_gatt_discover_params *params)
357 {
358 	struct bt_vocs_client *inst = CONTAINER_OF(params, struct bt_vocs_client, discover_params);
359 
360 	if (!attr) {
361 		LOG_DBG("Discovery complete for VOCS %p", inst);
362 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
363 		(void)memset(params, 0, sizeof(*params));
364 
365 		if (inst->cb && inst->cb->discover) {
366 			int err = valid_inst_discovered(inst) ? 0 : -ENOENT;
367 
368 			inst->cb->discover(&inst->vocs, err);
369 		}
370 
371 		return BT_GATT_ITER_STOP;
372 	}
373 
374 	LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
375 
376 	if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
377 		struct bt_gatt_subscribe_params *sub_params = NULL;
378 		struct bt_gatt_chrc *chrc;
379 
380 		chrc = (struct bt_gatt_chrc *)attr->user_data;
381 		if (inst->start_handle == 0) {
382 			inst->start_handle = chrc->value_handle;
383 		}
384 		inst->end_handle = chrc->value_handle;
385 
386 		if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_STATE)) {
387 			LOG_DBG("Volume offset state");
388 			inst->state_handle = chrc->value_handle;
389 			sub_params = &inst->state_sub_params;
390 		} else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_LOCATION)) {
391 			LOG_DBG("Location");
392 			inst->location_handle = chrc->value_handle;
393 			if (chrc->properties & BT_GATT_CHRC_NOTIFY) {
394 				sub_params = &inst->location_sub_params;
395 			}
396 			if (chrc->properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) {
397 				atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE);
398 			}
399 		} else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_CONTROL)) {
400 			LOG_DBG("Control point");
401 			inst->control_handle = chrc->value_handle;
402 		} else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_DESCRIPTION)) {
403 			LOG_DBG("Description");
404 			inst->desc_handle = chrc->value_handle;
405 			if (chrc->properties & BT_GATT_CHRC_NOTIFY) {
406 				sub_params = &inst->desc_sub_params;
407 			}
408 			if (chrc->properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) {
409 				atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE);
410 			}
411 		}
412 
413 		if (sub_params) {
414 			int err;
415 
416 			sub_params->value = BT_GATT_CCC_NOTIFY;
417 			sub_params->value_handle = chrc->value_handle;
418 			/*
419 			 * TODO: Don't assume that CCC is at handle + 2;
420 			 * do proper discovery;
421 			 */
422 			sub_params->ccc_handle = attr->handle + 2;
423 			sub_params->notify = vocs_client_notify_handler;
424 			atomic_set_bit(sub_params->flags, BT_GATT_SUBSCRIBE_FLAG_VOLATILE);
425 
426 			err = bt_gatt_subscribe(conn, sub_params);
427 			if (err != 0 && err != -EALREADY) {
428 				LOG_WRN("Could not subscribe to handle %u", sub_params->ccc_handle);
429 
430 				inst->cb->discover(&inst->vocs, err);
431 
432 				return BT_GATT_ITER_STOP;
433 			}
434 		}
435 	}
436 
437 	return BT_GATT_ITER_CONTINUE;
438 }
439 
bt_vocs_client_state_get(struct bt_vocs_client * inst)440 int bt_vocs_client_state_get(struct bt_vocs_client *inst)
441 {
442 	int err;
443 
444 	CHECKIF(!inst) {
445 		LOG_DBG("NULL instance");
446 		return -EINVAL;
447 	}
448 
449 	CHECKIF(inst->conn == NULL) {
450 		LOG_DBG("NULL conn");
451 		return -EINVAL;
452 	}
453 
454 	if (!inst->state_handle) {
455 		LOG_DBG("Handle not set");
456 		return -EINVAL;
457 	}
458 
459 	if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
460 		LOG_DBG("Instance is busy");
461 		return -EBUSY;
462 	}
463 
464 	inst->read_params.func = vocs_client_read_offset_state_cb;
465 	inst->read_params.handle_count = 1;
466 	inst->read_params.single.handle = inst->state_handle;
467 	inst->read_params.single.offset = 0U;
468 
469 	err = bt_gatt_read(inst->conn, &inst->read_params);
470 	if (err != 0) {
471 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
472 	}
473 
474 	return err;
475 }
476 
bt_vocs_client_location_set(struct bt_vocs_client * inst,uint32_t location)477 int bt_vocs_client_location_set(struct bt_vocs_client *inst, uint32_t location)
478 {
479 	CHECKIF(!inst) {
480 		LOG_DBG("NULL instance");
481 		return -EINVAL;
482 	}
483 
484 	CHECKIF(inst->conn == NULL) {
485 		LOG_DBG("NULL conn");
486 		return -EINVAL;
487 	}
488 
489 	CHECKIF(location > BT_AUDIO_LOCATION_ANY) {
490 		LOG_DBG("Invalid location 0x%08X", location);
491 		return -EINVAL;
492 	}
493 
494 	if (!inst->location_handle) {
495 		LOG_DBG("Handle not set");
496 		return -EINVAL;
497 	} else if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE)) {
498 		LOG_DBG("Location is not writable on peer service instance");
499 		return -EPERM;
500 	} else if (atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
501 		LOG_DBG("Instance is busy");
502 		return -EBUSY;
503 	}
504 
505 	/* When using write without response we do not set the busy flag */
506 
507 	return bt_gatt_write_without_response(inst->conn,
508 					      inst->location_handle,
509 					      &location, sizeof(location),
510 					      false);
511 }
512 
bt_vocs_client_location_get(struct bt_vocs_client * inst)513 int bt_vocs_client_location_get(struct bt_vocs_client *inst)
514 {
515 	int err;
516 
517 	CHECKIF(!inst) {
518 		LOG_DBG("NULL instance");
519 		return -EINVAL;
520 	}
521 
522 	CHECKIF(inst->conn == NULL) {
523 		LOG_DBG("NULL conn");
524 		return -EINVAL;
525 	}
526 
527 	if (!inst->location_handle) {
528 		LOG_DBG("Handle not set");
529 		return -EINVAL;
530 	} else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
531 		LOG_DBG("Instance is busy");
532 		return -EBUSY;
533 	}
534 
535 	inst->read_params.func = vocs_client_read_location_cb;
536 	inst->read_params.handle_count = 1;
537 	inst->read_params.single.handle = inst->location_handle;
538 	inst->read_params.single.offset = 0U;
539 
540 	err = bt_gatt_read(inst->conn, &inst->read_params);
541 	if (err != 0) {
542 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
543 	}
544 
545 	return err;
546 }
547 
bt_vocs_client_state_set(struct bt_vocs_client * inst,int16_t offset)548 int bt_vocs_client_state_set(struct bt_vocs_client *inst, int16_t offset)
549 {
550 	int err;
551 
552 	CHECKIF(!inst) {
553 		LOG_DBG("NULL instance");
554 		return -EINVAL;
555 	}
556 
557 	CHECKIF(inst->conn == NULL) {
558 		LOG_DBG("NULL conn");
559 		return -EINVAL;
560 	}
561 
562 	CHECKIF(!IN_RANGE(offset, BT_VOCS_MIN_OFFSET, BT_VOCS_MAX_OFFSET)) {
563 		LOG_DBG("Invalid offset: %d", offset);
564 		return -EINVAL;
565 	}
566 
567 	if (!inst->control_handle) {
568 		LOG_DBG("Handle not set");
569 		return -EINVAL;
570 	} else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
571 		LOG_DBG("Instance is busy");
572 		return -EBUSY;
573 	}
574 
575 	inst->cp.opcode = BT_VOCS_OPCODE_SET_OFFSET;
576 	inst->cp.counter = inst->state.change_counter;
577 	inst->cp.offset = offset;
578 
579 	inst->write_params.offset = 0;
580 	inst->write_params.data = &inst->cp;
581 	inst->write_params.length = sizeof(inst->cp);
582 	inst->write_params.handle = inst->control_handle;
583 	inst->write_params.func = vocs_client_write_vocs_cp_cb;
584 
585 	err = bt_gatt_write(inst->conn, &inst->write_params);
586 	if (err != 0) {
587 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
588 	}
589 
590 	return err;
591 }
592 
bt_vocs_client_description_get(struct bt_vocs_client * inst)593 int bt_vocs_client_description_get(struct bt_vocs_client *inst)
594 {
595 	int err;
596 
597 	CHECKIF(!inst) {
598 		LOG_DBG("NULL instance");
599 		return -EINVAL;
600 	}
601 
602 	CHECKIF(inst->conn == NULL) {
603 		LOG_DBG("NULL conn");
604 		return -EINVAL;
605 	}
606 
607 	if (!inst->desc_handle) {
608 		LOG_DBG("Handle not set");
609 		return -EINVAL;
610 	} else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
611 		LOG_DBG("Instance is busy");
612 		return -EBUSY;
613 	}
614 
615 	inst->read_params.func = vocs_client_read_output_desc_cb;
616 	inst->read_params.handle_count = 1;
617 	inst->read_params.single.handle = inst->desc_handle;
618 	inst->read_params.single.offset = 0U;
619 
620 	err = bt_gatt_read(inst->conn, &inst->read_params);
621 	if (err != 0) {
622 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
623 	}
624 
625 	return err;
626 }
627 
bt_vocs_client_description_set(struct bt_vocs_client * inst,const char * description)628 int bt_vocs_client_description_set(struct bt_vocs_client *inst,
629 				   const char *description)
630 {
631 	CHECKIF(!inst) {
632 		LOG_DBG("NULL instance");
633 		return -EINVAL;
634 	}
635 
636 	CHECKIF(inst->conn == NULL) {
637 		LOG_DBG("NULL conn");
638 		return -EINVAL;
639 	}
640 
641 	if (!inst->desc_handle) {
642 		LOG_DBG("Handle not set");
643 		return -EINVAL;
644 	} else if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE)) {
645 		LOG_DBG("Description is not writable on peer service instance");
646 		return -EPERM;
647 	} else if (atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
648 		LOG_DBG("Instance is busy");
649 		return -EBUSY;
650 	}
651 
652 	/* When using write without response we do not set the busy flag */
653 
654 	return bt_gatt_write_without_response(inst->conn,
655 					      inst->desc_handle,
656 					      description,
657 					      strlen(description), false);
658 }
659 
bt_vocs_client_free_instance_get(void)660 struct bt_vocs *bt_vocs_client_free_instance_get(void)
661 {
662 	for (int i = 0; i < ARRAY_SIZE(insts); i++) {
663 		if (!atomic_test_and_set_bit(insts[i].flags, BT_VOCS_CLIENT_FLAG_ACTIVE)) {
664 			insts[i].vocs.client_instance = true;
665 			return &insts[i].vocs;
666 		}
667 	}
668 
669 	return NULL;
670 }
671 
bt_vocs_client_conn_get(const struct bt_vocs * vocs,struct bt_conn ** conn)672 int bt_vocs_client_conn_get(const struct bt_vocs *vocs, struct bt_conn **conn)
673 {
674 	struct bt_vocs_client *inst;
675 
676 	CHECKIF(vocs == NULL) {
677 		LOG_DBG("NULL vocs pointer");
678 		return -EINVAL;
679 	}
680 
681 	CHECKIF(!vocs->client_instance) {
682 		LOG_DBG("vocs pointer shall be client instance");
683 		return -EINVAL;
684 	}
685 
686 	inst = CONTAINER_OF(vocs, struct bt_vocs_client, vocs);
687 
688 	if (inst->conn == NULL) {
689 		LOG_DBG("vocs pointer not associated with a connection. "
690 		       "Do discovery first");
691 		return -ENOTCONN;
692 	}
693 
694 	*conn = inst->conn;
695 	return 0;
696 }
697 
vocs_client_reset(struct bt_vocs_client * inst)698 static void vocs_client_reset(struct bt_vocs_client *inst)
699 {
700 	memset(&inst->state, 0, sizeof(inst->state));
701 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE);
702 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE);
703 	atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED);
704 
705 	inst->location = 0;
706 	inst->start_handle = 0;
707 	inst->end_handle = 0;
708 	inst->state_handle = 0;
709 	inst->location_handle = 0;
710 	inst->control_handle = 0;
711 	inst->desc_handle = 0;
712 
713 	if (inst->conn != NULL) {
714 		struct bt_conn *conn = inst->conn;
715 
716 		bt_conn_unref(conn);
717 		inst->conn = NULL;
718 	}
719 }
720 
bt_vocs_discover(struct bt_conn * conn,struct bt_vocs * vocs,const struct bt_vocs_discover_param * param)721 int bt_vocs_discover(struct bt_conn *conn, struct bt_vocs *vocs,
722 		     const struct bt_vocs_discover_param *param)
723 {
724 	struct bt_vocs_client *inst;
725 	int err = 0;
726 
727 	CHECKIF(!vocs || !conn || !param) {
728 		LOG_DBG("%s cannot be NULL", vocs == NULL   ? "vocs"
729 					     : conn == NULL ? "conn"
730 							    : "param");
731 		return -EINVAL;
732 	}
733 
734 	CHECKIF(!vocs->client_instance) {
735 		LOG_DBG("vocs pointer shall be client instance");
736 		return -EINVAL;
737 	}
738 
739 	CHECKIF(param->end_handle < param->start_handle) {
740 		LOG_DBG("start_handle (%u) shall be less than end_handle (%u)", param->start_handle,
741 			param->end_handle);
742 		return -EINVAL;
743 	}
744 
745 	inst = CONTAINER_OF(vocs, struct bt_vocs_client, vocs);
746 
747 	if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_ACTIVE)) {
748 		LOG_DBG("Inactive instance");
749 		return -EINVAL;
750 	} else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) {
751 		LOG_DBG("Instance is busy");
752 		return -EBUSY;
753 	}
754 
755 	vocs_client_reset(inst);
756 
757 	inst->conn = bt_conn_ref(conn);
758 	inst->discover_params.start_handle = param->start_handle;
759 	inst->discover_params.end_handle = param->end_handle;
760 	inst->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
761 	inst->discover_params.func = vocs_discover_func;
762 
763 	err = bt_gatt_discover(conn, &inst->discover_params);
764 	if (err != 0) {
765 		LOG_DBG("Discover failed (err %d)", err);
766 		atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY);
767 	}
768 
769 	return err;
770 }
771 
bt_vocs_client_cb_register(struct bt_vocs * vocs,struct bt_vocs_cb * cb)772 void bt_vocs_client_cb_register(struct bt_vocs *vocs, struct bt_vocs_cb *cb)
773 {
774 	struct bt_vocs_client *inst;
775 
776 	CHECKIF(!vocs) {
777 		LOG_DBG("inst cannot be NULL");
778 		return;
779 	}
780 
781 	CHECKIF(!vocs->client_instance) {
782 		LOG_DBG("vocs pointer shall be client instance");
783 		return;
784 	}
785 
786 	inst = CONTAINER_OF(vocs, struct bt_vocs_client, vocs);
787 
788 	inst->cb = cb;
789 }
790