blob: f467c9588f72a86708607eeca26d21a33d90c3bd [file] [log] [blame]
Antoine SOULIER4e34d052023-09-29 19:10:07 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19namespace aidl::android::hardware::bluetooth::audio {
20
21class A2dpBits {
22 const uint8_t* cdata_;
23 uint8_t* data_;
24
25 public:
26 A2dpBits(const std::vector<uint8_t>& vector) : cdata_(vector.data()) {}
27
28 A2dpBits(std::vector<uint8_t>& vector)
29 : cdata_(vector.data()), data_(vector.data()) {}
30
31 struct Range {
32 const int first, len;
33 constexpr Range(int first, int last)
34 : first(first), len(last - first + 1) {}
35 constexpr Range(int index) : first(index), len(1) {}
36 };
37
38 constexpr bool get(int bit) const {
39 return (cdata_[bit >> 3] >> (7 - (bit & 7))) & 1;
40 }
41
42 constexpr unsigned get(const Range& range) const {
43 unsigned v(0);
44 for (int i = 0; i < range.len; i++)
45 v |= get(range.first + i) << ((range.len - 1) - i);
46 return v;
47 }
48
49 constexpr void set(int bit, int value = 1) {
50 uint8_t m = 1 << (7 - (bit & 7));
51 if (value)
52 data_[bit >> 3] |= m;
53 else
54 data_[bit >> 3] &= ~m;
55 }
56
57 constexpr void set(const Range& range, int value) {
58 for (int i = 0; i < range.len; i++)
59 set(range.first + i, (value >> ((range.len - 1) - i)) & 1);
60 }
61
62 constexpr int find_active_bit(const Range& range) const {
63 unsigned v = get(range);
64 int i = 0;
65 for (; i < range.len && ((v >> i) & 1) == 0; i++)
66 ;
67 return i < range.len && (v ^ (1 << i)) == 0
68 ? range.first + (range.len - 1) - i
69 : -1;
70 }
71};
72
73} // namespace aidl::android::hardware::bluetooth::audio