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 android.annotation.IntDef; 20 21 import com.google.auto.value.AutoValue; 22 23 /** Header for OOB messages. */ 24 @AutoValue 25 public abstract class OobHeader { 26 27 @IntDef({ 28 OobVersion.CURRENT, 29 }) 30 public @interface OobVersion { 31 int CURRENT = 1; 32 } 33 34 private static final int SIZE_BYTES = 2; 35 parseBytes(byte[] payload)36 public static OobHeader parseBytes(byte[] payload) { 37 if (payload.length < SIZE_BYTES) { 38 throw new IllegalArgumentException( 39 String.format( 40 "Header is too short, expected at least %d bytes, got %d", 41 SIZE_BYTES, payload.length)); 42 } 43 44 int parseCursor = 0; 45 int version = payload[parseCursor++]; 46 MessageType messageType = MessageType.parseByte(payload[parseCursor++]); 47 return builder().setVersion(version).setMessageType(messageType).build(); 48 } 49 toBytes()50 public byte[] toBytes() { 51 byte[] payload = new byte[SIZE_BYTES]; 52 int parseCursor = 0; 53 payload[parseCursor++] = (byte) getVersion(); 54 payload[parseCursor++] = (byte) getMessageType().getValue(); 55 return payload; 56 } 57 getSize()58 public int getSize() { 59 return SIZE_BYTES; 60 } 61 62 /** Returns the version. */ getVersion()63 public abstract int getVersion(); 64 65 /** Returns the message type. */ getMessageType()66 public abstract MessageType getMessageType(); 67 68 /** Returns a builder for {@link OobHeader}. */ builder()69 public static Builder builder() { 70 return new AutoValue_OobHeader.Builder(); 71 } 72 73 /** Builder for {@link OobHeader}. */ 74 @AutoValue.Builder 75 public abstract static class Builder { setVersion(int version)76 public abstract Builder setVersion(int version); 77 setMessageType(MessageType messageType)78 public abstract Builder setMessageType(MessageType messageType); 79 build()80 public abstract OobHeader build(); 81 } 82 } 83