1 /* 2 * Copyright (C) 2022 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "broadcast_audio_scan_service_server.c" 39 40 #include <stdio.h> 41 42 #include "ble/att_db.h" 43 #include "ble/att_server.h" 44 #include "bluetooth_gatt.h" 45 #include "btstack_debug.h" 46 #include "btstack_defines.h" 47 #include "btstack_event.h" 48 #include "btstack_util.h" 49 50 #include "le-audio/gatt-service/broadcast_audio_scan_service_util.h" 51 #include "le-audio/le_audio.h" 52 #include "le-audio/le_audio_util.h" 53 54 #ifdef ENABLE_TESTING_SUPPORT 55 #include <stdio.h> 56 #endif 57 58 // offset gives position into fully serialized bass record 59 uint16_t bass_util_copy_source_common_data_to_buffer(const bass_source_data_t * data, uint16_t *source_offset, uint16_t buffer_offset, uint8_t * buffer, uint16_t buffer_size){ 60 uint16_t stored_bytes = 0; 61 uint8_t field_data[16]; 62 63 field_data[0] = (uint8_t)data->address_type; 64 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 1, *source_offset, buffer, buffer_size, buffer_offset); 65 (*source_offset)++; 66 67 reverse_bd_addr(data->address, &field_data[0]); 68 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 6, *source_offset, buffer, buffer_size, buffer_offset); 69 (*source_offset) += 6; 70 71 field_data[0] = data->adv_sid; 72 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 1, *source_offset, buffer, buffer_size, buffer_offset); 73 (*source_offset)++; 74 75 little_endian_store_24(field_data, 0, data->broadcast_id); 76 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 3, *source_offset, buffer, buffer_size, buffer_offset); 77 (*source_offset) += 3; 78 79 return stored_bytes; 80 } 81 82 // offset gives position into fully serialized bass record 83 uint16_t 84 bass_util_store_source_subgroups_into_buffer(const bass_source_data_t *data, bool use_state_fields, uint16_t *source_offset, 85 uint16_t buffer_offset, uint8_t *buffer, uint16_t buffer_size) { 86 uint16_t stored_bytes = 0; 87 uint8_t field_data[16]; 88 89 field_data[0] = (uint8_t)data->subgroups_num; 90 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 1, *source_offset, buffer, buffer_size, buffer_offset); 91 (*source_offset)++; 92 93 uint8_t i; 94 for (i = 0; i < data->subgroups_num; i++){ 95 bass_subgroup_t subgroup = data->subgroups[i]; 96 97 if (use_state_fields) { 98 little_endian_store_32(field_data, 0, subgroup.bis_sync_state); 99 } else { 100 little_endian_store_32(field_data, 0, subgroup.bis_sync); 101 } 102 stored_bytes += le_audio_virtual_memcpy_helper(field_data, 4, *source_offset, buffer, buffer_size, buffer_offset); 103 (*source_offset) += 4; 104 stored_bytes += le_audio_virtual_memcpy_metadata(&subgroup.metadata, subgroup.metadata_length, source_offset, buffer, buffer_size, buffer_offset); 105 } 106 return stored_bytes; 107 } 108 109 bool bass_util_pa_sync_state_and_subgroups_in_valid_range(uint8_t *buffer, uint16_t buffer_size){ 110 uint8_t pos = 0; 111 // pa_sync_state 112 uint8_t pa_sync_state = buffer[pos++]; 113 if (pa_sync_state >= (uint8_t)LE_AUDIO_PA_SYNC_STATE_RFU){ 114 log_info("Unexpected pa_sync_state 0x%02X", pa_sync_state); 115 return false; 116 } 117 118 // pa_interval 119 pos += 2; 120 uint8_t num_subgroups = buffer[pos++]; 121 if (num_subgroups > BASS_SUBGROUPS_MAX_NUM){ 122 log_info("Number of subgroups %u exceedes maximum %u", num_subgroups, BASS_SUBGROUPS_MAX_NUM); 123 return false; 124 } 125 126 // If a BIS_Sync parameter value is not 0xFFFFFFFF for a subgroup, 127 // and if a BIS_index value written by a client is set to a value of 0b1 in more than one subgroup, 128 // the server shall ignore the operation. 129 uint8_t i; 130 uint32_t mask_total = 0; 131 for (i = 0; i < num_subgroups; i++){ 132 // bis_sync 133 uint32_t bis_sync = little_endian_read_32(buffer, pos); 134 pos += 4; 135 136 if (bis_sync != 0xFFFFFFFF){ 137 uint32_t mask_add = mask_total + ~bis_sync; 138 uint32_t mask_or = mask_total | ~bis_sync; 139 if (mask_add != mask_or){ 140 // not disjunct 141 return false; 142 } 143 mask_total = mask_add; 144 } 145 146 // check if we can read metadata_length 147 if (pos >= buffer_size){ 148 log_info("Metadata length not specified, subgroup %u", i); 149 return false; 150 } 151 152 uint8_t metadata_length = buffer[pos++]; 153 154 if ((buffer_size - pos) < metadata_length){ 155 log_info("Metadata length %u exceedes remaining buffer %u", metadata_length, buffer_size - pos); 156 return false; 157 } 158 // metadata 159 pos += metadata_length; 160 } 161 162 return (pos == buffer_size); 163 } 164 165 bool bass_util_add_source_buffer_in_valid_range(uint8_t *buffer, uint16_t buffer_size){ 166 if (buffer_size < 15){ 167 log_info("Add Source opcode, buffer too small"); 168 return false; 169 } 170 171 uint8_t pos = 0; 172 // addr type 173 uint8_t adv_type = buffer[pos++]; 174 if (adv_type > (uint8_t)BD_ADDR_TYPE_LE_RANDOM){ 175 log_info("Unexpected adv_type 0x%02X", adv_type); 176 return false; 177 } 178 179 // address 180 pos += 6; 181 182 // advertising_sid Range: 0x00-0x0F 183 uint8_t advertising_sid = buffer[pos++]; 184 if (advertising_sid > 0x0F){ 185 log_info("Advertising sid out of range 0x%02X", advertising_sid); 186 return false; 187 } 188 189 // broadcast_id 190 pos += 3; 191 return bass_util_pa_sync_state_and_subgroups_in_valid_range(buffer+pos, buffer_size-pos); 192 } 193 194 void 195 bass_util_get_pa_info_and_subgroups_from_buffer(uint8_t *buffer, uint16_t buffer_size, bass_source_data_t *source_data, 196 bool is_broadcast_receive_state) { 197 UNUSED(buffer_size); 198 uint8_t pos = 0; 199 // for Broadcast Receive state, we have BIG_Encryption + Bad_Code, while for Add/Modify we have PA_Interval 200 if (is_broadcast_receive_state){ 201 source_data->pa_sync_state = (le_audio_pa_sync_state_t)buffer[pos++]; 202 le_audio_big_encryption_t big_encryption = (le_audio_big_encryption_t) buffer[pos++]; 203 if (big_encryption == LE_AUDIO_BIG_ENCRYPTION_BAD_CODE){ 204 pos += 16; 205 } 206 } else { 207 source_data->pa_sync = (le_audio_pa_sync_t)buffer[pos++]; 208 source_data->pa_interval = little_endian_read_16(buffer, pos); 209 pos += 2; 210 } 211 212 source_data->subgroups_num = buffer[pos++]; 213 uint8_t i; 214 for (i = 0; i < source_data->subgroups_num; i++){ 215 // bis_sync / state 216 if (is_broadcast_receive_state){ 217 source_data->subgroups[i].bis_sync_state = little_endian_read_32(buffer, pos); 218 } else { 219 source_data->subgroups[i].bis_sync = little_endian_read_32(buffer, pos); 220 } 221 pos += 4; 222 223 uint8_t metadata_bytes_read = le_audio_metadata_parse_tlv(&buffer[pos], buffer_size - pos, &source_data->subgroups[i].metadata); 224 source_data->subgroups[i].metadata_length = metadata_bytes_read - 1; 225 pos += metadata_bytes_read; 226 } 227 } 228 229 void bass_util_get_source_from_buffer(uint8_t *buffer, uint16_t buffer_size, bass_source_data_t *source_data, 230 bool is_broadcast_receive_state) { 231 UNUSED(buffer_size); 232 uint8_t pos = 0; 233 234 source_data->address_type = (bd_addr_type_t)buffer[pos++]; 235 236 reverse_bd_addr(&buffer[pos], source_data->address); 237 pos += 6; 238 239 source_data->adv_sid = buffer[pos++]; 240 241 source_data->broadcast_id = little_endian_read_24(buffer, pos); 242 pos += 3; 243 244 bass_util_get_pa_info_and_subgroups_from_buffer(buffer + pos, buffer_size - pos, source_data, is_broadcast_receive_state); 245 }