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 byte technologyId = payload[parseCursor++]; 39 RangingTechnology rangingTechnology; 40 try { 41 rangingTechnology = RangingTechnology.TECHNOLOGIES.get(technologyId); 42 } catch (IndexOutOfBoundsException e) { 43 throw new IllegalArgumentException( 44 "Attempted to parse unknown technology with id" + technologyId); 45 } 46 int size = payload[parseCursor++]; 47 48 return builder().setRangingTechnology(rangingTechnology).setSize(size).build(); 49 } 50 toBytes()51 public byte[] toBytes() { 52 byte[] payload = new byte[SIZE_BYTES]; 53 int parseCursor = 0; 54 payload[parseCursor++] = getRangingTechnology().toByte(); 55 payload[parseCursor++] = (byte) getSize(); 56 return payload; 57 } 58 getHeaderSize()59 public int getHeaderSize() { 60 return SIZE_BYTES; 61 } 62 63 /** Returns the version. */ getRangingTechnology()64 public abstract RangingTechnology getRangingTechnology(); 65 66 /** Returns the message type. */ getSize()67 public abstract int getSize(); 68 69 /** Returns a builder for {@link TechnologyHeader}. */ builder()70 public static Builder builder() { 71 return new AutoValue_TechnologyHeader.Builder(); 72 } 73 74 /** Builder for {@link TechnologyHeader}. */ 75 @AutoValue.Builder 76 public abstract static class Builder { setSize(int size)77 public abstract Builder setSize(int size); 78 setRangingTechnology(RangingTechnology rangingTechnology)79 public abstract Builder setRangingTechnology(RangingTechnology rangingTechnology); 80 build()81 public abstract TechnologyHeader build(); 82 } 83 } 84