1 /*
2  * Copyright (C) 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 package com.android.server.uwb.data;
17 
18 import android.util.Log;
19 import android.uwb.UwbAddress;
20 
21 import com.android.modules.utils.build.SdkLevel;
22 import com.android.server.uwb.params.TlvUtil;
23 
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26 
27 public class UwbMulticastListUpdateStatus {
28     private static final String TAG = "UwbM*ListUpdateStatus";
29     private long mSessionId;
30     private int mRemainingSize;
31     private int mNumOfControlees;
32     private byte[] mControleeMacAddresses;
33     private long[] mSubSessionId;
34     private int[] mStatus;
35     private UwbAddress[] mControleeUwbAddresses;
36 
UwbMulticastListUpdateStatus(long sessionID, int remainingSize, int numOfControlees, byte[] controleeMacAddresses, long[] subSessionId, int[] status)37     public UwbMulticastListUpdateStatus(long sessionID, int remainingSize, int numOfControlees,
38             byte[] controleeMacAddresses, long[] subSessionId, int[] status) {
39         this.mSessionId = sessionID;
40         this.mRemainingSize = remainingSize;
41         this.mNumOfControlees = numOfControlees;
42         this.mControleeMacAddresses = controleeMacAddresses;
43         this.mSubSessionId = subSessionId;
44         this.mStatus = status;
45 
46         Log.d(TAG, "Controlee count: " + numOfControlees + " mac addresses: "
47                 + Arrays.toString(controleeMacAddresses));
48         if (controleeMacAddresses != null) {
49             // Precache mac addresses in a more usable and universal form.
50             mControleeUwbAddresses = getUwbAddresses(mControleeMacAddresses, mNumOfControlees,
51                     mControleeMacAddresses.length / mNumOfControlees);
52         }
53     }
54 
55     // Convert controlee addresses in byte array to array of UwbAddress.
getUwbAddresses(byte[] macAddresses, int numOfAddresses, int addressLength)56     public UwbAddress[] getUwbAddresses(byte[] macAddresses, int numOfAddresses,
57             int addressLength) {
58         UwbAddress[] uwbAddresses = new UwbAddress[numOfAddresses];
59         ByteBuffer buffer = ByteBuffer.allocate(macAddresses.length);
60         buffer.put(macAddresses);
61         buffer.flip();
62         for (int i = 0; i < numOfAddresses; i++) {
63             if (buffer.remaining() >= addressLength) {
64                 byte[] macAddress = new byte[addressLength];
65                 buffer.get(macAddress, 0, macAddress.length);
66                 uwbAddresses[i] = getComputedMacAddress(macAddress);
67             }
68         }
69         return uwbAddresses;
70     }
71 
getSessionId()72     public long getSessionId() {
73         return mSessionId;
74     }
75 
getRemainingSize()76     public int getRemainingSize() {
77         return mRemainingSize;
78     }
79 
getNumOfControlee()80     public int getNumOfControlee() {
81         return mNumOfControlees;
82     }
83 
84     // This should go obsolete as we shift to UwbAddresses.
getControleeMacAddresses()85     public byte[] getControleeMacAddresses() {
86         return mControleeMacAddresses;
87     }
88 
getSubSessionId()89     public long[] getSubSessionId() {
90         return mSubSessionId;
91     }
92 
getStatus()93     public int[] getStatus() {
94         return mStatus;
95     }
96 
getControleeUwbAddresses()97     public UwbAddress[] getControleeUwbAddresses() {
98         return mControleeUwbAddresses;
99     }
100 
101     @Override
toString()102     public String toString() {
103         return "UwbMulticastListUpdateEvent { "
104                 + " SessionID =" + mSessionId
105                 + ", RemainingSize =" + mRemainingSize
106                 + ", NumOfControlee =" + mNumOfControlees
107                 + ", MacAddress =" + Arrays.toString(mControleeMacAddresses)
108                 + ", SubSessionId =" + Arrays.toString(mSubSessionId)
109                 + ", Status =" + Arrays.toString(mStatus)
110                 + '}';
111     }
112 
getComputedMacAddress(byte[] address)113     private static UwbAddress getComputedMacAddress(byte[] address) {
114         if (!SdkLevel.isAtLeastU()) {
115             return UwbAddress.fromBytes(TlvUtil.getReverseBytes(address));
116         }
117         return UwbAddress.fromBytes(address);
118     }
119 }
120