Lines Matching +full:volume +full:- +full:min
1 /* Bluetooth VOCS - Volume Offset Control Service - Client */
4 * Copyright (c) 2021-2022 Nordic Semiconductor ASA
6 * SPDX-License-Identifier: Apache-2.0
60 uint16_t handle = params->value_handle; in vocs_client_notify_handler()
78 if (handle == inst->state_handle) { in vocs_client_notify_handler()
79 if (length == sizeof(inst->state)) { in vocs_client_notify_handler()
80 memcpy(&inst->state, data, length); in vocs_client_notify_handler()
81 LOG_DBG("Inst %p: Offset %d, counter %u", inst, inst->state.offset, in vocs_client_notify_handler()
82 inst->state.change_counter); in vocs_client_notify_handler()
83 if (inst->cb && inst->cb->state) { in vocs_client_notify_handler()
84 inst->cb->state(&inst->vocs, 0, inst->state.offset); in vocs_client_notify_handler()
89 } else if (handle == inst->desc_handle) { in vocs_client_notify_handler()
90 char desc[MIN(BT_L2CAP_RX_MTU, BT_ATT_MAX_ATTRIBUTE_LEN) + 1]; in vocs_client_notify_handler()
94 if (length > sizeof(desc) - 1) { in vocs_client_notify_handler()
96 sizeof(desc) - 1); in vocs_client_notify_handler()
98 length = MIN(sizeof(desc) - 1, length); in vocs_client_notify_handler()
103 if (inst->cb && inst->cb->description) { in vocs_client_notify_handler()
104 inst->cb->description(&inst->vocs, 0, desc); in vocs_client_notify_handler()
106 } else if (handle == inst->location_handle) { in vocs_client_notify_handler()
107 if (length == sizeof(inst->location)) { in vocs_client_notify_handler()
108 memcpy(&inst->location, data, length); in vocs_client_notify_handler()
109 LOG_DBG("Inst %p: Location %u", inst, inst->location); in vocs_client_notify_handler()
110 if (inst->cb && inst->cb->location) { in vocs_client_notify_handler()
111 inst->cb->location(&inst->vocs, 0, inst->location); in vocs_client_notify_handler()
126 struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle); in vocs_client_read_offset_state_cb()
136 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in vocs_client_read_offset_state_cb()
141 if (length == sizeof(inst->state)) { in vocs_client_read_offset_state_cb()
142 memcpy(&inst->state, data, length); in vocs_client_read_offset_state_cb()
143 LOG_DBG("Offset %d, counter %u", inst->state.offset, in vocs_client_read_offset_state_cb()
144 inst->state.change_counter); in vocs_client_read_offset_state_cb()
147 sizeof(inst->state)); in vocs_client_read_offset_state_cb()
155 if (inst->cb && inst->cb->state) { in vocs_client_read_offset_state_cb()
156 inst->cb->state(&inst->vocs, cb_err, cb_err ? 0 : inst->state.offset); in vocs_client_read_offset_state_cb()
167 struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle); in vocs_client_read_location_cb()
177 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in vocs_client_read_location_cb()
182 if (length == sizeof(inst->location)) { in vocs_client_read_location_cb()
183 memcpy(&inst->location, data, length); in vocs_client_read_location_cb()
184 LOG_DBG("Location %u", inst->location); in vocs_client_read_location_cb()
187 sizeof(inst->location)); in vocs_client_read_location_cb()
195 if (inst->cb && inst->cb->location) { in vocs_client_read_location_cb()
196 inst->cb->location(&inst->vocs, cb_err, cb_err ? 0 : inst->location); in vocs_client_read_location_cb()
207 struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle); in internal_read_volume_offset_state_cb()
217 LOG_WRN("Volume offset state read failed: %d", err); in internal_read_volume_offset_state_cb()
220 if (length == sizeof(inst->state)) { in internal_read_volume_offset_state_cb()
223 memcpy(&inst->state, data, length); in internal_read_volume_offset_state_cb()
224 LOG_DBG("Offset %d, counter %u", inst->state.offset, in internal_read_volume_offset_state_cb()
225 inst->state.change_counter); in internal_read_volume_offset_state_cb()
228 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in internal_read_volume_offset_state_cb()
229 write_err = bt_vocs_client_state_set(inst, inst->cp.offset); in internal_read_volume_offset_state_cb()
235 sizeof(inst->state)); in internal_read_volume_offset_state_cb()
244 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in internal_read_volume_offset_state_cb()
246 if (inst->cb && inst->cb->set_offset) { in internal_read_volume_offset_state_cb()
247 inst->cb->set_offset(&inst->vocs, err); in internal_read_volume_offset_state_cb()
258 struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->handle); in vocs_client_write_vocs_cp_cb()
275 atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED)) { in vocs_client_write_vocs_cp_cb()
277 } else if (cb_err == BT_VOCS_ERR_INVALID_COUNTER && inst->state_handle) { in vocs_client_write_vocs_cp_cb()
278 LOG_DBG("Invalid change counter. Reading volume offset state from server."); in vocs_client_write_vocs_cp_cb()
280 inst->read_params.func = internal_read_volume_offset_state_cb; in vocs_client_write_vocs_cp_cb()
281 inst->read_params.handle_count = 1; in vocs_client_write_vocs_cp_cb()
282 inst->read_params.single.handle = inst->state_handle; in vocs_client_write_vocs_cp_cb()
284 atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED); in vocs_client_write_vocs_cp_cb()
286 cb_err = bt_gatt_read(conn, &inst->read_params); in vocs_client_write_vocs_cp_cb()
288 LOG_WRN("Could not read Volume offset state: %d", cb_err); in vocs_client_write_vocs_cp_cb()
295 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED); in vocs_client_write_vocs_cp_cb()
296 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in vocs_client_write_vocs_cp_cb()
298 if (inst->cb && inst->cb->set_offset) { in vocs_client_write_vocs_cp_cb()
299 inst->cb->set_offset(&inst->vocs, cb_err); in vocs_client_write_vocs_cp_cb()
308 struct bt_vocs_client *inst = lookup_vocs_by_handle(conn, params->single.handle); in vocs_client_read_output_desc_cb()
309 char desc[MIN(BT_L2CAP_RX_MTU, BT_ATT_MAX_ATTRIBUTE_LEN) + 1]; in vocs_client_read_output_desc_cb()
319 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in vocs_client_read_output_desc_cb()
327 if (length > sizeof(desc) - 1) { in vocs_client_read_output_desc_cb()
329 sizeof(desc) - 1); in vocs_client_read_output_desc_cb()
331 length = MIN(sizeof(desc) - 1, length); in vocs_client_read_output_desc_cb()
340 if (inst->cb && inst->cb->description) { in vocs_client_read_output_desc_cb()
341 inst->cb->description(&inst->vocs, cb_err, cb_err ? NULL : desc); in vocs_client_read_output_desc_cb()
349 return inst->state_handle && in valid_inst_discovered()
350 inst->control_handle && in valid_inst_discovered()
351 inst->location_handle && in valid_inst_discovered()
352 inst->desc_handle; in valid_inst_discovered()
362 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in vocs_discover_func()
365 if (inst->cb && inst->cb->discover) { in vocs_discover_func()
366 int err = valid_inst_discovered(inst) ? 0 : -ENOENT; in vocs_discover_func()
368 inst->cb->discover(&inst->vocs, err); in vocs_discover_func()
374 LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle); in vocs_discover_func()
376 if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) { in vocs_discover_func()
380 chrc = (struct bt_gatt_chrc *)attr->user_data; in vocs_discover_func()
381 if (inst->start_handle == 0) { in vocs_discover_func()
382 inst->start_handle = chrc->value_handle; in vocs_discover_func()
384 inst->end_handle = chrc->value_handle; in vocs_discover_func()
386 if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_STATE)) { in vocs_discover_func()
387 LOG_DBG("Volume offset state"); in vocs_discover_func()
388 inst->state_handle = chrc->value_handle; in vocs_discover_func()
389 sub_params = &inst->state_sub_params; in vocs_discover_func()
390 } else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_LOCATION)) { in vocs_discover_func()
392 inst->location_handle = chrc->value_handle; in vocs_discover_func()
393 if (chrc->properties & BT_GATT_CHRC_NOTIFY) { in vocs_discover_func()
394 sub_params = &inst->location_sub_params; in vocs_discover_func()
396 if (chrc->properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) { in vocs_discover_func()
397 atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE); in vocs_discover_func()
399 } else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_CONTROL)) { in vocs_discover_func()
401 inst->control_handle = chrc->value_handle; in vocs_discover_func()
402 } else if (!bt_uuid_cmp(chrc->uuid, BT_UUID_VOCS_DESCRIPTION)) { in vocs_discover_func()
404 inst->desc_handle = chrc->value_handle; in vocs_discover_func()
405 if (chrc->properties & BT_GATT_CHRC_NOTIFY) { in vocs_discover_func()
406 sub_params = &inst->desc_sub_params; in vocs_discover_func()
408 if (chrc->properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) { in vocs_discover_func()
409 atomic_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE); in vocs_discover_func()
416 sub_params->value = BT_GATT_CCC_NOTIFY; in vocs_discover_func()
417 sub_params->value_handle = chrc->value_handle; in vocs_discover_func()
422 sub_params->ccc_handle = attr->handle + 2; in vocs_discover_func()
423 sub_params->notify = vocs_client_notify_handler; in vocs_discover_func()
424 atomic_set_bit(sub_params->flags, BT_GATT_SUBSCRIBE_FLAG_VOLATILE); in vocs_discover_func()
427 if (err != 0 && err != -EALREADY) { in vocs_discover_func()
428 LOG_WRN("Could not subscribe to handle %u", sub_params->ccc_handle); in vocs_discover_func()
430 inst->cb->discover(&inst->vocs, err); in vocs_discover_func()
446 return -EINVAL; in bt_vocs_client_state_get()
449 CHECKIF(inst->conn == NULL) { in bt_vocs_client_state_get()
451 return -EINVAL; in bt_vocs_client_state_get()
454 if (!inst->state_handle) { in bt_vocs_client_state_get()
456 return -EINVAL; in bt_vocs_client_state_get()
459 if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_state_get()
461 return -EBUSY; in bt_vocs_client_state_get()
464 inst->read_params.func = vocs_client_read_offset_state_cb; in bt_vocs_client_state_get()
465 inst->read_params.handle_count = 1; in bt_vocs_client_state_get()
466 inst->read_params.single.handle = inst->state_handle; in bt_vocs_client_state_get()
467 inst->read_params.single.offset = 0U; in bt_vocs_client_state_get()
469 err = bt_gatt_read(inst->conn, &inst->read_params); in bt_vocs_client_state_get()
471 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in bt_vocs_client_state_get()
481 return -EINVAL; in bt_vocs_client_location_set()
484 CHECKIF(inst->conn == NULL) { in bt_vocs_client_location_set()
486 return -EINVAL; in bt_vocs_client_location_set()
491 return -EINVAL; in bt_vocs_client_location_set()
494 if (!inst->location_handle) { in bt_vocs_client_location_set()
496 return -EINVAL; in bt_vocs_client_location_set()
497 } else if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE)) { in bt_vocs_client_location_set()
499 return -EPERM; in bt_vocs_client_location_set()
500 } else if (atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_location_set()
502 return -EBUSY; in bt_vocs_client_location_set()
507 return bt_gatt_write_without_response(inst->conn, in bt_vocs_client_location_set()
508 inst->location_handle, in bt_vocs_client_location_set()
519 return -EINVAL; in bt_vocs_client_location_get()
522 CHECKIF(inst->conn == NULL) { in bt_vocs_client_location_get()
524 return -EINVAL; in bt_vocs_client_location_get()
527 if (!inst->location_handle) { in bt_vocs_client_location_get()
529 return -EINVAL; in bt_vocs_client_location_get()
530 } else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_location_get()
532 return -EBUSY; in bt_vocs_client_location_get()
535 inst->read_params.func = vocs_client_read_location_cb; in bt_vocs_client_location_get()
536 inst->read_params.handle_count = 1; in bt_vocs_client_location_get()
537 inst->read_params.single.handle = inst->location_handle; in bt_vocs_client_location_get()
538 inst->read_params.single.offset = 0U; in bt_vocs_client_location_get()
540 err = bt_gatt_read(inst->conn, &inst->read_params); in bt_vocs_client_location_get()
542 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in bt_vocs_client_location_get()
554 return -EINVAL; in bt_vocs_client_state_set()
557 CHECKIF(inst->conn == NULL) { in bt_vocs_client_state_set()
559 return -EINVAL; in bt_vocs_client_state_set()
564 return -EINVAL; in bt_vocs_client_state_set()
567 if (!inst->control_handle) { in bt_vocs_client_state_set()
569 return -EINVAL; in bt_vocs_client_state_set()
570 } else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_state_set()
572 return -EBUSY; in bt_vocs_client_state_set()
575 inst->cp.opcode = BT_VOCS_OPCODE_SET_OFFSET; in bt_vocs_client_state_set()
576 inst->cp.counter = inst->state.change_counter; in bt_vocs_client_state_set()
577 inst->cp.offset = offset; in bt_vocs_client_state_set()
579 inst->write_params.offset = 0; in bt_vocs_client_state_set()
580 inst->write_params.data = &inst->cp; in bt_vocs_client_state_set()
581 inst->write_params.length = sizeof(inst->cp); in bt_vocs_client_state_set()
582 inst->write_params.handle = inst->control_handle; in bt_vocs_client_state_set()
583 inst->write_params.func = vocs_client_write_vocs_cp_cb; in bt_vocs_client_state_set()
585 err = bt_gatt_write(inst->conn, &inst->write_params); in bt_vocs_client_state_set()
587 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in bt_vocs_client_state_set()
599 return -EINVAL; in bt_vocs_client_description_get()
602 CHECKIF(inst->conn == NULL) { in bt_vocs_client_description_get()
604 return -EINVAL; in bt_vocs_client_description_get()
607 if (!inst->desc_handle) { in bt_vocs_client_description_get()
609 return -EINVAL; in bt_vocs_client_description_get()
610 } else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_description_get()
612 return -EBUSY; in bt_vocs_client_description_get()
615 inst->read_params.func = vocs_client_read_output_desc_cb; in bt_vocs_client_description_get()
616 inst->read_params.handle_count = 1; in bt_vocs_client_description_get()
617 inst->read_params.single.handle = inst->desc_handle; in bt_vocs_client_description_get()
618 inst->read_params.single.offset = 0U; in bt_vocs_client_description_get()
620 err = bt_gatt_read(inst->conn, &inst->read_params); in bt_vocs_client_description_get()
622 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in bt_vocs_client_description_get()
633 return -EINVAL; in bt_vocs_client_description_set()
636 CHECKIF(inst->conn == NULL) { in bt_vocs_client_description_set()
638 return -EINVAL; in bt_vocs_client_description_set()
641 if (!inst->desc_handle) { in bt_vocs_client_description_set()
643 return -EINVAL; in bt_vocs_client_description_set()
644 } else if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE)) { in bt_vocs_client_description_set()
646 return -EPERM; in bt_vocs_client_description_set()
647 } else if (atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_client_description_set()
649 return -EBUSY; in bt_vocs_client_description_set()
654 return bt_gatt_write_without_response(inst->conn, in bt_vocs_client_description_set()
655 inst->desc_handle, in bt_vocs_client_description_set()
678 return -EINVAL; in bt_vocs_client_conn_get()
681 CHECKIF(!vocs->client_instance) { in bt_vocs_client_conn_get()
683 return -EINVAL; in bt_vocs_client_conn_get()
688 if (inst->conn == NULL) { in bt_vocs_client_conn_get()
691 return -ENOTCONN; in bt_vocs_client_conn_get()
694 *conn = inst->conn; in bt_vocs_client_conn_get()
700 memset(&inst->state, 0, sizeof(inst->state)); in vocs_client_reset()
701 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_LOC_WRITABLE); in vocs_client_reset()
702 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_DESC_WRITABLE); in vocs_client_reset()
703 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_CP_RETRIED); in vocs_client_reset()
705 inst->location = 0; in vocs_client_reset()
706 inst->start_handle = 0; in vocs_client_reset()
707 inst->end_handle = 0; in vocs_client_reset()
708 inst->state_handle = 0; in vocs_client_reset()
709 inst->location_handle = 0; in vocs_client_reset()
710 inst->control_handle = 0; in vocs_client_reset()
711 inst->desc_handle = 0; in vocs_client_reset()
713 if (inst->conn != NULL) { in vocs_client_reset()
714 struct bt_conn *conn = inst->conn; in vocs_client_reset()
717 inst->conn = NULL; in vocs_client_reset()
731 return -EINVAL; in bt_vocs_discover()
734 CHECKIF(!vocs->client_instance) { in bt_vocs_discover()
736 return -EINVAL; in bt_vocs_discover()
739 CHECKIF(param->end_handle < param->start_handle) { in bt_vocs_discover()
740 LOG_DBG("start_handle (%u) shall be less than end_handle (%u)", param->start_handle, in bt_vocs_discover()
741 param->end_handle); in bt_vocs_discover()
742 return -EINVAL; in bt_vocs_discover()
747 if (!atomic_test_bit(inst->flags, BT_VOCS_CLIENT_FLAG_ACTIVE)) { in bt_vocs_discover()
749 return -EINVAL; in bt_vocs_discover()
750 } else if (atomic_test_and_set_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY)) { in bt_vocs_discover()
752 return -EBUSY; in bt_vocs_discover()
757 inst->conn = bt_conn_ref(conn); in bt_vocs_discover()
758 inst->discover_params.start_handle = param->start_handle; in bt_vocs_discover()
759 inst->discover_params.end_handle = param->end_handle; in bt_vocs_discover()
760 inst->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; in bt_vocs_discover()
761 inst->discover_params.func = vocs_discover_func; in bt_vocs_discover()
763 err = bt_gatt_discover(conn, &inst->discover_params); in bt_vocs_discover()
766 atomic_clear_bit(inst->flags, BT_VOCS_CLIENT_FLAG_BUSY); in bt_vocs_discover()
781 CHECKIF(!vocs->client_instance) { in bt_vocs_client_cb_register()
788 inst->cb = cb; in bt_vocs_client_cb_register()