1 /*
2  * Copyright 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.server.ranging.oob;
18 
19 import com.android.server.ranging.RangingTechnology;
20 
21 import com.google.auto.value.AutoValue;
22 
23 /** Header for individual technology capability. */
24 @AutoValue
25 public abstract class TechnologyHeader {
26 
27     public static final int SIZE_BYTES = 2;
28 
parseBytes(byte[] payload)29     public static TechnologyHeader parseBytes(byte[] payload) {
30         if (payload.length < SIZE_BYTES) {
31             throw new IllegalArgumentException(
32                     String.format(
33                             "Header is too short, expected at least %d bytes, got %d",
34                             SIZE_BYTES, payload.length));
35         }
36 
37         int parseCursor = 0;
38         RangingTechnology rangingTechnology =
39                 RangingTechnology.TECHNOLOGIES.get(payload[parseCursor++]);
40         int size = payload[parseCursor++];
41 
42         return builder().setRangingTechnology(rangingTechnology).setSize(size).build();
43     }
44 
toBytes()45     public byte[] toBytes() {
46         byte[] payload = new byte[SIZE_BYTES];
47         int parseCursor = 0;
48         payload[parseCursor++] = getRangingTechnology().toByte();
49         payload[parseCursor++] = (byte) getSize();
50         return payload;
51     }
52 
getHeaderSize()53     public int getHeaderSize() {
54         return SIZE_BYTES;
55     }
56 
57     /** Returns the version. */
getRangingTechnology()58     public abstract RangingTechnology getRangingTechnology();
59 
60     /** Returns the message type. */
getSize()61     public abstract int getSize();
62 
63     /** Returns a builder for {@link TechnologyHeader}. */
builder()64     public static Builder builder() {
65         return new AutoValue_TechnologyHeader.Builder();
66     }
67 
68     /** Builder for {@link TechnologyHeader}. */
69     @AutoValue.Builder
70     public abstract static class Builder {
setSize(int size)71         public abstract Builder setSize(int size);
72 
setRangingTechnology(RangingTechnology rangingTechnology)73         public abstract Builder setRangingTechnology(RangingTechnology rangingTechnology);
74 
build()75         public abstract TechnologyHeader build();
76     }
77 }
78