1 /*
2  * Copyright (C) 2024 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 package com.android.ranging.generic;
18 
19 import com.google.common.collect.ImmutableList;
20 
21 import java.util.BitSet;
22 import java.util.List;
23 
24 /** Enum representing an individual ranging technology. */
25 public enum RangingTechnology {
26     UWB(0), // Ultra-Wide Band
27     CS(1); // Channel Sounding, formerly known as HADM
28 
29     private final int value;
30 
RangingTechnology(int value)31     RangingTechnology(int value) {
32         this.value = value;
33     }
34 
getValue()35     public int getValue() {
36         return value;
37     }
38 
toByte()39     public byte toByte() {
40         return (byte) (1 << value);
41     }
42 
parseByte(byte technologiesByte)43     public static ImmutableList<RangingTechnology> parseByte(byte technologiesByte) {
44         BitSet bitset = BitSet.valueOf(new byte[]{technologiesByte});
45         ImmutableList.Builder<RangingTechnology> technologies = ImmutableList.builder();
46         for (RangingTechnology technology : RangingTechnology.values()) {
47             if (bitset.get(technology.value)) {
48                 technologies.add(technology);
49             }
50         }
51         return technologies.build();
52     }
53 
toBitmap(List<RangingTechnology> technologies)54     public static byte toBitmap(List<RangingTechnology> technologies) {
55         if (technologies.isEmpty()) {
56             return 0x0;
57         }
58         BitSet bitset = new BitSet();
59         for (RangingTechnology technology : technologies) {
60             bitset.set(technology.value);
61         }
62         return bitset.toByteArray()[0];
63     }
64 }