1 /* 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 19 namespace aidl::android::hardware::bluetooth::audio { 20 21 class A2dpBits { 22 const uint8_t* cdata_; 23 uint8_t* data_; 24 25 public: A2dpBits(const std::vector<uint8_t> & vector)26 A2dpBits(const std::vector<uint8_t>& vector) 27 : cdata_(vector.data()), data_(nullptr) {} 28 A2dpBits(std::vector<uint8_t> & vector)29 A2dpBits(std::vector<uint8_t>& vector) 30 : cdata_(vector.data()), data_(vector.data()) {} 31 32 struct Range { 33 const int first, len; RangeRange34 constexpr Range(int first, int last) 35 : first(first), len(last - first + 1) {} RangeRange36 constexpr Range(int index) : first(index), len(1) {} 37 }; 38 get(int bit)39 constexpr bool get(int bit) const { 40 return (cdata_[bit >> 3] >> (7 - (bit & 7))) & 1; 41 } 42 get(const Range & range)43 constexpr unsigned get(const Range& range) const { 44 unsigned v(0); 45 for (int i = 0; i < range.len; i++) 46 v |= get(range.first + i) << ((range.len - 1) - i); 47 return v; 48 } 49 50 constexpr void set(int bit, int value = 1) { 51 uint8_t m = 1 << (7 - (bit & 7)); 52 if (value) 53 data_[bit >> 3] |= m; 54 else 55 data_[bit >> 3] &= ~m; 56 } 57 set(const Range & range,int value)58 constexpr void set(const Range& range, int value) { 59 for (int i = 0; i < range.len; i++) 60 set(range.first + i, (value >> ((range.len - 1) - i)) & 1); 61 } 62 find_active_bit(const Range & range)63 constexpr int find_active_bit(const Range& range) const { 64 unsigned v = get(range); 65 int i = 0; 66 for (; i < range.len && ((v >> i) & 1) == 0; i++) 67 ; 68 return i < range.len && (v ^ (1 << i)) == 0 69 ? range.first + (range.len - 1) - i 70 : -1; 71 } 72 }; 73 74 } // namespace aidl::android::hardware::bluetooth::audio 75