• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.uwb.support.oemextension;
18 
19 import android.os.PersistableBundle;
20 import android.uwb.UwbManager;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.google.uwb.support.base.RequiredParam;
25 
26 /**
27  * RF test notification for oem extension callback
28 *
29 * <p> This is passed as a bundle to oem extension API
30 * {@link UwbManager.UwbOemExtensionCallback#onRfTestNotificationReceived(PersistableBundle)}.
31 */
32 public class RfTestNotification {
33     private static final int BUNDLE_VERSION_1 = 1;
34     private static final int BUNDLE_VERSION_CURRENT = BUNDLE_VERSION_1;
35 
36     private final int mRfTestOperationType;
37     private final byte[] mRfTestNtfData;
38     public static final String KEY_BUNDLE_VERSION = "bundle_version";
39     public static final String RF_TEST_NTF_DATA = "rf_test_ntf_data";
40     private static final String KEY_RF_OPERATION_TYPE = "rf_operation_type";
41 
getBundleVersion()42     public static int getBundleVersion() {
43         return BUNDLE_VERSION_CURRENT;
44     }
45 
getRfTestNtfData()46     public byte[] getRfTestNtfData() {
47         return mRfTestNtfData;
48     }
49 
50     /*
51      * RfTestParams.RfTestOperationType defined as part of RfTest support lib
52      * Possible values are:
53      * - TEST_PERIODIC_TX = 0
54      * - TEST_PER_RX = 1
55      * - TEST_RX = 2
56      * - TEST_LOOPBACK = 3
57      * - TEST_SS_TWR = 4
58      * - TEST_SR_RX = 5
59      *
60      * @return The RF test operation type.
61      */
getRfTestOperationType()62     public int getRfTestOperationType() {
63         return mRfTestOperationType;
64     }
65 
RfTestNotification(int rfTestOperationType, byte[] rfTestNtfData)66     private RfTestNotification(int rfTestOperationType, byte[] rfTestNtfData) {
67         mRfTestOperationType = rfTestOperationType;
68         mRfTestNtfData = rfTestNtfData;
69     }
70 
71     @Nullable
byteArrayToIntArray(@ullable byte[] bytes)72     private static int[] byteArrayToIntArray(@Nullable byte[] bytes) {
73         if (bytes == null) {
74             return null;
75         }
76         int[] values = new int[bytes.length];
77         for (int i = 0; i < values.length; i++) {
78             values[i] = (bytes[i]);
79         }
80         return values;
81     }
82 
83     @Nullable
intArrayToByteArray(@ullable int[] values)84     private static byte[] intArrayToByteArray(@Nullable int[] values) {
85         if (values == null) {
86             return null;
87         }
88         byte[] bytes = new byte[values.length];
89         for (int i = 0; i < values.length; i++) {
90             bytes[i] = (byte) values[i];
91         }
92         return bytes;
93     }
94 
toBundle()95     public PersistableBundle toBundle() {
96         PersistableBundle bundle = new PersistableBundle();
97         bundle.putInt(KEY_BUNDLE_VERSION, getBundleVersion());
98         bundle.putIntArray(RF_TEST_NTF_DATA, byteArrayToIntArray(mRfTestNtfData));
99         bundle.putInt(KEY_RF_OPERATION_TYPE, mRfTestOperationType);
100         return bundle;
101     }
102 
fromBundle(PersistableBundle bundle)103     public static RfTestNotification fromBundle(PersistableBundle bundle) {
104         switch (bundle.getInt(KEY_BUNDLE_VERSION)) {
105             case BUNDLE_VERSION_1:
106                 return parseVersion1(bundle);
107             default:
108                 throw new IllegalArgumentException("Invalid bundle version");
109         }
110     }
111 
parseVersion1(PersistableBundle bundle)112     private static RfTestNotification parseVersion1(PersistableBundle bundle) {
113         return new RfTestNotification.Builder()
114                 .setRfTestOperationType(bundle.getInt(KEY_RF_OPERATION_TYPE))
115                 .setRfTestNtfData(intArrayToByteArray(bundle.getIntArray(RF_TEST_NTF_DATA)))
116                 .build();
117     }
118 
119     /** Builder */
120     public static class Builder {
121         private RequiredParam<Integer> mRfTestOperationType = new RequiredParam<Integer>();
122         private byte[] mRfTestNtfData = null;
123 
setRfTestNtfData(byte[] rfTestNtfData)124         public RfTestNotification.Builder setRfTestNtfData(byte[] rfTestNtfData) {
125             mRfTestNtfData = rfTestNtfData;
126             return this;
127         }
128 
setRfTestOperationType(int rfTestOperationType)129         public Builder setRfTestOperationType(int rfTestOperationType) {
130             mRfTestOperationType.set(rfTestOperationType);
131             return this;
132         }
133 
build()134         public RfTestNotification build() {
135             return new RfTestNotification(mRfTestOperationType.get(), mRfTestNtfData);
136         }
137     }
138 }
139