• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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.nearby.common.bluetooth.fastpair;
18 
19 import static com.google.common.io.BaseEncoding.base16;
20 
21 import com.google.common.primitives.Bytes;
22 import com.google.errorprone.annotations.FormatMethod;
23 import com.google.errorprone.annotations.FormatString;
24 
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 
29 /**
30  * A length, type, value (LTV) data block.
31  */
32 public class Ltv {
33 
34     private static final int SIZE_OF_LEN_TYPE = 2;
35 
36     final byte mType;
37     final byte[] mValue;
38 
39     /**
40      * Thrown if there's an error during {@link #parse}.
41      */
42     public static class ParseException extends Exception {
43 
44         @FormatMethod
ParseException(@ormatString String format, Object... objects)45         private ParseException(@FormatString String format, Object... objects) {
46             super(String.format(format, objects));
47         }
48     }
49 
50     /**
51      * Constructor.
52      */
Ltv(byte type, byte... value)53     public Ltv(byte type, byte... value) {
54         this.mType = type;
55         this.mValue = value;
56     }
57 
58     /**
59      * Parses a list of LTV blocks out of the input byte block.
60      */
parse(byte[] bytes)61     static List<Ltv> parse(byte[] bytes) throws ParseException {
62         List<Ltv> ltvs = new ArrayList<>();
63         // The "+ 2" is for the length and type bytes.
64         for (int valueLength, i = 0; i < bytes.length; i += SIZE_OF_LEN_TYPE + valueLength) {
65             // - 1 since the length in the packet includes the type byte.
66             valueLength = bytes[i] - 1;
67             if (valueLength < 0 || bytes.length < i + SIZE_OF_LEN_TYPE + valueLength) {
68                 throw new ParseException(
69                         "Wrong length=%d at index=%d in LTVs=%s", bytes[i], i,
70                         base16().encode(bytes));
71             }
72             ltvs.add(new Ltv(bytes[i + 1], Arrays.copyOfRange(bytes, i + SIZE_OF_LEN_TYPE,
73                     i + SIZE_OF_LEN_TYPE + valueLength)));
74         }
75         return ltvs;
76     }
77 
78     /**
79      * Returns an LTV block, where length is mValue.length + 1 (for the type byte).
80      */
getBytes()81     public byte[] getBytes() {
82         return Bytes.concat(new byte[]{(byte) (mValue.length + 1), mType}, mValue);
83     }
84 }
85