summaryrefslogtreecommitdiff
path: root/system/bta/le_audio/client_parser.cc
blob: e7d64613cf06d4ed41d03f94d3347b13a966e197 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 * Copyright 2019 HIMSA II K/S - www.himsa.com. Represented by EHIMA -
 * www.ehima.com
 *
 * 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.
 */

/*
 *  This module contains API of the audio stream control protocol.
 */

#include "client_parser.h"

#include <base/strings/string_number_conversions.h>
#include <endian.h>
#include <hardware/bt_common_types.h>

#include <map>
#include <memory>
#include <numeric>

#include "bta_le_audio_api.h"
#include "gap_api.h"
#include "gatt_api.h"
#include "gd/common/strings.h"
#include "le_audio_types.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"

using le_audio::types::acs_ac_record;

namespace le_audio {
namespace client_parser {
namespace ascs {
static std::map<uint8_t, std::string> ase_state_map_string = {
    {kAseStateIdle, "Idle"},
    {kAseStateCodecConfigured, "Codec Configured"},
    {kAseStateQosConfigured, "QoS Configured"},
    {kAseStateEnabling, "Enabling"},
    {kAseStateStreaming, "Streaming"},
    {kAseStateDisabling, "Disabling"},
    {kAseStateReleasing, "Releasing"},
};

static std::map<uint8_t, std::string> ctp_opcode_map_string = {
    {kCtpOpcodeCodecConfiguration, "Config Codec"},
    {kCtpOpcodeQosConfiguration, "Config QoS"},
    {kCtpOpcodeEnable, "Enable"},
    {kCtpOpcodeReceiverStartReady, "Receiver Start Ready"},
    {kCtpOpcodeDisable, "Disable"},
    {kCtpOpcodeReceiverStopReady, "Receiver Stop Ready"},
    {kCtpOpcodeUpdateMetadata, "Update Metadata"},
    {kCtpOpcodeRelease, "Release"},
};

static std::map<uint8_t, std::string> ctp_reason_map_string = {
    {kCtpResponseNoReason, ""},
    {kCtpResponseCodecId, "Codec ID"},
    {kCtpResponseCodecSpecificConfiguration, "Codec specific configuration"},
    {kCtpResponseSduInterval, "SDU interval"},
    {kCtpResponseFraming, "Framing"},
    {kCtpResponsePhy, "PHY"},
    {kCtpResponseMaximumSduSize, "Maximum SDU size"},
    {kCtpResponseRetransmissionNumber, "Retransmission number"},
    {kCtpResponseMaxTransportLatency, "Max Transport latency"},
    {kCtpResponsePresentationDelay, "Presentation delay"},
    {kCtpResponseInvalidAseCisMapping, "Invalid ASE CIS mapping"},
};

static std::map<uint8_t, std::string> ctp_response_code_map_string = {
    {kCtpResponseCodeSuccess, "Success"},
    {kCtpResponseCodeUnsupportedOpcode, "Unsupported Opcode"},
    {kCtpResponseCodeInvalidLength, "Invalid Length"},
    {kCtpResponseCodeInvalidAseId, "Invalid ASE ID"},
    {kCtpResponseCodeInvalidAseStateMachineTransition,
     "Invalid ASE State Machine Transition"},
    {kCtpResponseCodeInvalidAseDirection, "Invalid ASE Direction"},
    {kCtpResponseCodeUnsupportedAudioCapabilities,
     "Unsupported Audio Capabilities"},
    {kCtpResponseCodeUnsupportedConfigurationParameterValue,
     "Unsupported Configuration Parameter Value"},
    {kCtpResponseCodeRejectedConfigurationParameterValue,
     "Rejected Configuration Parameter Value"},
    {kCtpResponseCodeInvalidConfigurationParameterValue,
     "Invalid Configuration Parameter Value"},
    {kCtpResponseCodeUnsupportedMetadata, "Unsupported Metadata"},
    {kCtpResponseCodeRejectedMetadata, "Rejected Metadata"},
    {kCtpResponseCodeInvalidMetadata, "Invalid Metadata"},
    {kCtpResponseCodeInsufficientResources, "Insufficient Resources"},
    {kCtpResponseCodeUnspecifiedError, "Unspecified Error"},
};

bool ParseAseStatusHeader(ase_rsp_hdr& arh, uint16_t len,
                          const uint8_t* value) {
  if (len < kAseRspHdrMinLen) {
    LOG(ERROR) << __func__
               << ", wrong len of ASE char (header): " << static_cast<int>(len);

    return false;
  }

  STREAM_TO_UINT8(arh.id, value);
  STREAM_TO_UINT8(arh.state, value);

  LOG(INFO) << "ASE status: "
            << "\tASE id: " << loghex(arh.id)
            << "\tASE state: " << ase_state_map_string[arh.state] << " ("
            << loghex(arh.state) << ")";

  return true;
}

bool ParseAseStatusCodecConfiguredStateParams(
    struct ase_codec_configured_state_params& rsp, uint16_t len,
    const uint8_t* value) {
  uint8_t codec_spec_conf_len;

  if (len < kAseStatusCodecConfMinLen) {
    LOG(ERROR) << "Wrong len of codec conf status (Codec conf header)";
    return false;
  }

  STREAM_TO_UINT8(rsp.framing, value);
  STREAM_TO_UINT8(rsp.preferred_phy, value);
  STREAM_TO_UINT8(rsp.preferred_retrans_nb, value);
  STREAM_TO_UINT16(rsp.max_transport_latency, value);
  STREAM_TO_UINT24(rsp.pres_delay_min, value);
  STREAM_TO_UINT24(rsp.pres_delay_max, value);
  STREAM_TO_UINT24(rsp.preferred_pres_delay_min, value);
  STREAM_TO_UINT24(rsp.preferred_pres_delay_max, value);
  STREAM_TO_UINT8(rsp.codec_id.coding_format, value);
  STREAM_TO_UINT16(rsp.codec_id.vendor_company_id, value);
  STREAM_TO_UINT16(rsp.codec_id.vendor_codec_id, value);
  STREAM_TO_UINT8(codec_spec_conf_len, value);

  len -= kAseStatusCodecConfMinLen;

  if (len != codec_spec_conf_len) {
    LOG(ERROR) << "Wrong len of codec conf status (Codec spec conf)";
    return false;
  }
  if (codec_spec_conf_len)
    rsp.codec_spec_conf =
        std::vector<uint8_t>(value, value + codec_spec_conf_len);

  LOG(INFO) << __func__ << ", Codec configuration"
            << "\n\tFraming: " << loghex(rsp.framing)
            << "\n\tPreferred PHY: " << loghex(rsp.preferred_phy)
            << "\n\tPreferred retransmission number: "
            << loghex(rsp.preferred_retrans_nb) << "\n\tMax transport latency: "
            << loghex(rsp.max_transport_latency)
            << "\n\tPresence delay min: " << loghex(rsp.pres_delay_min)
            << "\n\tPresence delay max: " << loghex(rsp.pres_delay_max)
            << "\n\tPreferredPresentationDelayMin: "
            << loghex(rsp.preferred_pres_delay_min)
            << "\n\tPreferredPresentationDelayMax: "
            << loghex(rsp.preferred_pres_delay_max)
            << "\n\tCoding format: " << loghex(rsp.codec_id.coding_format)
            << "\n\tVendor codec company ID: "
            << loghex(rsp.codec_id.vendor_company_id)
            << "\n\tVendor codec ID: " << loghex(rsp.codec_id.vendor_codec_id)
            << "\n\tCodec specific conf len: " << (int)codec_spec_conf_len
            << "\n\tCodec specific conf: "
            << base::HexEncode(rsp.codec_spec_conf.data(),
                               rsp.codec_spec_conf.size());

  return true;
}

bool ParseAseStatusQosConfiguredStateParams(
    struct ase_qos_configured_state_params& rsp, uint16_t len,
    const uint8_t* value) {
  if (len != kAseStatusCodecQosConfMinLen) {
    LOG(ERROR) << "Wrong len of ASE characteristic (QOS conf header)";
    return false;
  }

  STREAM_TO_UINT8(rsp.cig_id, value);
  STREAM_TO_UINT8(rsp.cis_id, value);
  STREAM_TO_UINT24(rsp.sdu_interval, value);
  STREAM_TO_UINT8(rsp.framing, value);
  STREAM_TO_UINT8(rsp.phy, value);
  STREAM_TO_UINT16(rsp.max_sdu, value);
  STREAM_TO_UINT8(rsp.retrans_nb, value);
  STREAM_TO_UINT16(rsp.max_transport_latency, value);
  STREAM_TO_UINT24(rsp.pres_delay, value);

  LOG(INFO) << __func__ << ", Codec QoS Configured"
            << "\n\tCIG: " << loghex(rsp.cig_id)
            << "\n\tCIS: " << loghex(rsp.cis_id)
            << "\n\tSDU interval: " << loghex(rsp.sdu_interval)
            << "\n\tFraming: " << loghex(rsp.framing)
            << "\n\tPHY: " << loghex(rsp.phy)
            << "\n\tMax SDU: " << loghex(rsp.max_sdu)
            << "\n\tRetransmission number: " << loghex(rsp.retrans_nb)
            << "\n\tMax transport latency: "
            << loghex(rsp.max_transport_latency)
            << "\n\tPresentation delay: " << loghex(rsp.pres_delay);

  return true;
}

bool ParseAseStatusTransientStateParams(struct ase_transient_state_params& rsp,
                                        uint16_t len, const uint8_t* value) {
  uint8_t metadata_len;

  if (len < kAseStatusTransMinLen) {
    LOG(ERROR) << "Wrong len of ASE characteristic (metadata)";
    return false;
  }

  STREAM_TO_UINT8(rsp.cig_id, value);
  STREAM_TO_UINT8(rsp.cis_id, value);
  STREAM_TO_UINT8(metadata_len, value);
  len -= kAseStatusTransMinLen;

  if (len != metadata_len) {
    LOG(ERROR) << "Wrong len of ASE characteristic (metadata)";
    return false;
  }

  if (metadata_len > 0)
    rsp.metadata = std::vector<uint8_t>(value, value + metadata_len);

  LOG(INFO) << __func__ << ", Status enabling/streaming/disabling"
            << "\n\tCIG: " << loghex(rsp.cig_id)
            << "\n\tCIS: " << loghex(rsp.cis_id) << "\n\tMetadata: "
            << base::HexEncode(rsp.metadata.data(), rsp.metadata.size());

  return true;
}

bool ParseAseCtpNotification(struct ctp_ntf& ntf, uint16_t len,
                             const uint8_t* value) {
  uint8_t num_entries;

  if (len < kCtpNtfMinLen) {
    LOG(ERROR) << "Wrong len of ASE control point notification: " << (int)len;
    return false;
  }

  STREAM_TO_UINT8(ntf.op, value);
  STREAM_TO_UINT8(num_entries, value);

  if (len != kCtpNtfMinLen + (num_entries * kCtpAseEntryMinLen)) {
    LOG(ERROR) << "Wrong len of ASE control point notification (ASE IDs)";
    return false;
  }

  for (int i = 0; i < num_entries; i++) {
    struct ctp_ase_entry entry;

    STREAM_TO_UINT8(entry.ase_id, value);
    STREAM_TO_UINT8(entry.response_code, value);
    STREAM_TO_UINT8(entry.reason, value);

    ntf.entries.push_back(std::move(entry));
  }

  LOG(INFO) << __func__ << ", Control point notification"
            << "\n\tOpcode: " << ctp_opcode_map_string[ntf.op] << " ("
            << loghex(ntf.op) << ")"
            << "\n\tNum ASE IDs: " << (int)num_entries;
  for (size_t i = 0; i < num_entries; i++)
    LOG(INFO) << "\n\tASE ID[" << loghex(ntf.entries[i].ase_id)
              << "] response: "
              << ctp_response_code_map_string[ntf.entries[i].response_code]
              << " (" << loghex(ntf.entries[i].response_code) << ")"
              << " reason: " << ctp_reason_map_string[ntf.entries[i].reason]
              << " (" << loghex(ntf.entries[i].reason) << ")";

  return true;
}

bool PrepareAseCtpCodecConfig(const std::vector<struct ctp_codec_conf>& confs,
                              std::vector<uint8_t>& value) {
  if (confs.size() == 0) return false;

  std::string conf_ents_str;
  size_t msg_len = std::accumulate(
      confs.begin(), confs.end(),
      confs.size() * kCtpCodecConfMinLen + kAseNumSize + kCtpOpSize,
      [&conf_ents_str](size_t cur_len, auto const& conf) {
        auto ltv_map = conf.codec_config.GetAsLtvMap();
        for (const auto& [type, value] : ltv_map.Values()) {
          conf_ents_str +=
              "\ttype: " + std::to_string(type) +
              "\tlen: " + std::to_string(value.size()) +
              "\tdata: " + base::HexEncode(value.data(), value.size()) + "\n";
        };

        return cur_len + ltv_map.RawPacketSize();
      });
  value.resize(msg_len);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeCodecConfiguration);

  UINT8_TO_STREAM(msg, confs.size());
  for (const struct ctp_codec_conf& conf : confs) {
    UINT8_TO_STREAM(msg, conf.ase_id);
    UINT8_TO_STREAM(msg, conf.target_latency);
    UINT8_TO_STREAM(msg, conf.target_phy);
    UINT8_TO_STREAM(msg, conf.codec_id.coding_format);
    UINT16_TO_STREAM(msg, conf.codec_id.vendor_company_id);
    UINT16_TO_STREAM(msg, conf.codec_id.vendor_codec_id);

    auto ltv_map = conf.codec_config.GetAsLtvMap();
    auto codec_spec_conf_len = ltv_map.RawPacketSize();

    UINT8_TO_STREAM(msg, codec_spec_conf_len);
    msg = ltv_map.RawPacket(msg);

    LOG(INFO) << __func__ << ", Codec configuration"
              << "\n\tAse id: " << loghex(conf.ase_id)
              << "\n\tTarget latency: " << loghex(conf.target_latency)
              << "\n\tTarget PHY: " << loghex(conf.target_phy)
              << "\n\tCoding format: " << loghex(conf.codec_id.coding_format)
              << "\n\tVendor codec company ID: "
              << loghex(conf.codec_id.vendor_company_id)
              << "\n\tVendor codec ID: "
              << loghex(conf.codec_id.vendor_codec_id)
              << "\n\tCodec config len: "
              << static_cast<int>(codec_spec_conf_len)
              << "\n\tCodec spec conf: "
              << "\n"
              << conf_ents_str;
  }

  return true;
}

bool PrepareAseCtpConfigQos(const std::vector<struct ctp_qos_conf>& confs,
                            std::vector<uint8_t>& value) {
  if (confs.size() == 0) return false;
  value.resize(confs.size() * kCtpQosConfMinLen + kAseNumSize + kCtpOpSize);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeQosConfiguration);
  UINT8_TO_STREAM(msg, confs.size());

  for (const struct ctp_qos_conf& conf : confs) {
    UINT8_TO_STREAM(msg, conf.ase_id);
    UINT8_TO_STREAM(msg, conf.cig);
    UINT8_TO_STREAM(msg, conf.cis);
    UINT24_TO_STREAM(msg, conf.sdu_interval);
    UINT8_TO_STREAM(msg, conf.framing);
    UINT8_TO_STREAM(msg, conf.phy);
    UINT16_TO_STREAM(msg, conf.max_sdu);
    UINT8_TO_STREAM(msg, conf.retrans_nb);
    UINT16_TO_STREAM(msg, conf.max_transport_latency);
    UINT24_TO_STREAM(msg, conf.pres_delay);

    LOG(INFO) << __func__ << ", QoS configuration"
              << "\n\tAse id: " << loghex(conf.ase_id)
              << "\n\tcig: " << loghex(conf.cig)
              << "\n\tCis: " << loghex(conf.cis)
              << "\n\tSDU interval: " << loghex(conf.sdu_interval)
              << "\n\tFraming: " << loghex(conf.framing)
              << "\n\tPhy: " << loghex(conf.phy)
              << "\n\tMax sdu size: " << loghex(conf.max_sdu)
              << "\n\tRetrans nb: " << loghex(conf.retrans_nb)
              << "\n\tMax Transport latency: "
              << loghex(conf.max_transport_latency)
              << "\n\tPres delay: " << loghex(conf.pres_delay);
  }

  return true;
}

bool PrepareAseCtpEnable(const std::vector<struct ctp_enable>& confs,
                         std::vector<uint8_t>& value) {
  if (confs.size() == 0) return false;

  if (confs.size() > UINT8_MAX) {
    LOG_ERROR(" To many ASEs to update metadata");
    return false;
  }

  uint16_t msg_len = confs.size() * kCtpEnableMinLen + kAseNumSize + kCtpOpSize;
  for (auto& conf : confs) {
    if (msg_len > GATT_MAX_ATTR_LEN) {
      LOG_ERROR(" Message length above GATT maximum");
      return false;
    }
    if (conf.metadata.size() > UINT8_MAX) {
      LOG_ERROR(" ase[%d] metadata length is invalid", conf.ase_id);
      return false;
    }

    msg_len += conf.metadata.size();
  }
  value.resize(msg_len);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeEnable);
  UINT8_TO_STREAM(msg, confs.size());

  for (const struct ctp_enable& conf : confs) {
    UINT8_TO_STREAM(msg, conf.ase_id);
    UINT8_TO_STREAM(msg, conf.metadata.size());
    ARRAY_TO_STREAM(msg, conf.metadata.data(),
                    static_cast<int>(conf.metadata.size()));

    LOG(INFO) << __func__ << ", Enable"
              << "\n\tAse id: " << loghex(conf.ase_id) << "\n\tMetadata: "
              << base::HexEncode(conf.metadata.data(), conf.metadata.size());
  }

  return true;
}

bool PrepareAseCtpAudioReceiverStartReady(const std::vector<uint8_t>& ase_ids,
                                          std::vector<uint8_t>& value) {
  if (ase_ids.size() == 0) return false;
  value.resize(ase_ids.size() * kAseIdSize + kAseNumSize + kCtpOpSize);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeReceiverStartReady);
  UINT8_TO_STREAM(msg, ase_ids.size());

  for (const uint8_t& id : ase_ids) {
    UINT8_TO_STREAM(msg, id);

    LOG(INFO) << __func__ << ", ReceiverStartReady"
              << "\n\tAse id: " << loghex(id);
  }

  return true;
}

bool PrepareAseCtpDisable(const std::vector<uint8_t>& ase_ids,
                          std::vector<uint8_t>& value) {
  if (ase_ids.size() == 0) return false;
  value.resize(ase_ids.size() * kAseIdSize + kAseNumSize + kCtpOpSize);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeDisable);
  UINT8_TO_STREAM(msg, ase_ids.size());

  for (const uint8_t& id : ase_ids) {
    UINT8_TO_STREAM(msg, id);

    LOG(INFO) << __func__ << ", Disable"
              << "\n\tAse id: " << loghex(id);
  }

  return true;
}

bool PrepareAseCtpAudioReceiverStopReady(const std::vector<uint8_t>& ase_ids,
                                         std::vector<uint8_t>& value) {
  if (ase_ids.size() == 0) return false;
  value.resize(ase_ids.size() * kAseIdSize + kAseNumSize + kCtpOpSize);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeReceiverStopReady);
  UINT8_TO_STREAM(msg, ase_ids.size());

  for (const uint8_t& ase_id : ase_ids) {
    UINT8_TO_STREAM(msg, ase_id);

    LOG(INFO) << __func__ << ", ReceiverStopReady"
              << "\n\tAse id: " << loghex(ase_id);
  }

  return true;
}

bool PrepareAseCtpUpdateMetadata(
    const std::vector<struct ctp_update_metadata>& confs,
    std::vector<uint8_t>& value) {
  if (confs.size() == 0) return false;

  if (confs.size() > UINT8_MAX) {
    LOG_ERROR(" To many ASEs to update metadata");
    return false;
  }

  uint16_t msg_len =
      confs.size() * kCtpUpdateMetadataMinLen + kAseNumSize + kCtpOpSize;
  for (auto& conf : confs) {
    if (msg_len > GATT_MAX_ATTR_LEN) {
      LOG_ERROR(" Message length above GATT maximum");
      return false;
    }
    if (conf.metadata.size() > UINT8_MAX) {
      LOG_ERROR(" ase[%d] metadata length is invalid", conf.ase_id);
      return false;
    }

    msg_len += conf.metadata.size();
  }
  value.resize(msg_len);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeUpdateMetadata);
  UINT8_TO_STREAM(msg, confs.size());

  for (const struct ctp_update_metadata& conf : confs) {
    UINT8_TO_STREAM(msg, conf.ase_id);
    UINT8_TO_STREAM(msg, conf.metadata.size());
    ARRAY_TO_STREAM(msg, conf.metadata.data(),
                    static_cast<int>(conf.metadata.size()));

    LOG(INFO) << __func__ << ", Update Metadata"
              << "\n\tAse id: " << loghex(conf.ase_id) << "\n\tMetadata: "
              << base::HexEncode(conf.metadata.data(), conf.metadata.size());
  }

  return true;
}

bool PrepareAseCtpRelease(const std::vector<uint8_t>& ase_ids,
                          std::vector<uint8_t>& value) {
  if (ase_ids.size() == 0) return true;
  value.resize(ase_ids.size() * kAseIdSize + kAseNumSize + kCtpOpSize);

  uint8_t* msg = value.data();
  UINT8_TO_STREAM(msg, kCtpOpcodeRelease);
  UINT8_TO_STREAM(msg, ase_ids.size());

  for (const uint8_t& ase_id : ase_ids) {
    UINT8_TO_STREAM(msg, ase_id);

    LOG(INFO) << __func__ << ", Release"
              << "\n\tAse id: " << loghex(ase_id);
  }

  return true;
}
}  // namespace ascs

namespace pacs {

bool ParsePac(std::vector<struct acs_ac_record>& pac_recs, uint16_t len,
              const uint8_t* value) {
  if (len < kAcsPacDiscoverRspMinLen) {
    LOG(ERROR) << "Wrong len of PAC characteristic";
    return false;
  }

  uint8_t pac_rec_nb;
  STREAM_TO_UINT8(pac_rec_nb, value);
  len -= kAcsPacDiscoverRspMinLen;

  pac_recs.reserve(pac_rec_nb);
  for (int i = 0; i < pac_rec_nb; i++) {
    struct acs_ac_record rec;
    uint8_t codec_spec_cap_len, metadata_len;

    if (len < kAcsPacRecordMinLen) {
      LOG(ERROR) << "Wrong len of PAC record";
      pac_recs.clear();
      return false;
    }

    STREAM_TO_UINT8(rec.codec_id.coding_format, value);
    STREAM_TO_UINT16(rec.codec_id.vendor_company_id, value);
    STREAM_TO_UINT16(rec.codec_id.vendor_codec_id, value);
    STREAM_TO_UINT8(codec_spec_cap_len, value);
    len -= kAcsPacRecordMinLen - kAcsPacMetadataLenLen;

    if (len < codec_spec_cap_len + kAcsPacMetadataLenLen) {
      LOG(ERROR) << "Wrong len of PAC record (codec specific capabilities)";
      pac_recs.clear();
      return false;
    }

    bool parsed;
    rec.codec_spec_caps =
        types::LeAudioLtvMap::Parse(value, codec_spec_cap_len, parsed);
    if (!parsed) return false;

    value += codec_spec_cap_len;
    len -= codec_spec_cap_len;

    STREAM_TO_UINT8(metadata_len, value);
    len -= kAcsPacMetadataLenLen;

    if (len < metadata_len) {
      LOG(ERROR) << "Wrong len of PAC record (metadata)";
      pac_recs.clear();
      return false;
    }

    rec.metadata = std::vector<uint8_t>(value, value + metadata_len);
    value += metadata_len;
    len -= metadata_len;

    pac_recs.push_back(std::move(rec));
  }

  return true;
}

bool ParseAudioLocations(types::AudioLocations& audio_locations, uint16_t len,
                         const uint8_t* value) {
  if (len != kAudioLocationsRspMinLen) {
    LOG(ERROR) << "Wrong len of Audio Location characteristic";
    return false;
  }

  STREAM_TO_UINT32(audio_locations, value);

  LOG(INFO) << "Audio locations: " << audio_locations.to_string();

  return true;
}

bool ParseSupportedAudioContexts(struct acs_supported_audio_contexts& contexts,
                                 uint16_t len, const uint8_t* value) {
  if (len != kAseAudioSuppContRspMinLen) {
    LOG(ERROR) << "Wrong len of Audio Supported Context characteristic";
    return false;
  }

  STREAM_TO_UINT16(contexts.snk_supp_cont, value);
  STREAM_TO_UINT16(contexts.src_supp_cont, value);

  LOG(INFO) << "Supported Audio Contexts: "
            << "\n\tSupported Sink Contexts: "
            << contexts.snk_supp_cont.to_string()
            << "\n\tSupported Source Contexts: "
            << contexts.src_supp_cont.to_string();

  return true;
}

bool ParseAvailableAudioContexts(struct acs_available_audio_contexts& contexts,
                                 uint16_t len, const uint8_t* value) {
  if (len != kAseAudioAvailRspMinLen) {
    LOG(ERROR) << "Wrong len of Audio Availability characteristic";
    return false;
  }

  STREAM_TO_UINT16(contexts.snk_avail_cont, value);
  STREAM_TO_UINT16(contexts.src_avail_cont, value);

  LOG(INFO) << "Available Audio Contexts: "
            << "\n\tAvailable Sink Contexts: "
            << contexts.snk_avail_cont.to_string()
            << "\n\tAvailable Source Contexts: "
            << contexts.src_avail_cont.to_string();

  return true;
}
}  // namespace pacs

}  // namespace client_parser
}  // namespace le_audio