1 /*
2  * Copyright (C) 2022 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 import static com.google.uwb.support.fira.FiraParams.MULTI_NODE_MODE_UNICAST;
18 import static com.google.uwb.support.fira.FiraParams.RANGING_DEVICE_ROLE_INITIATOR;
19 import static com.google.uwb.support.fira.FiraParams.RANGING_DEVICE_TYPE_CONTROLLER;
20 import static com.google.uwb.support.fira.FiraParams.RANGING_ROUND_USAGE_DS_TWR_DEFERRED_MODE;
21 
22 import static org.junit.Assert.assertEquals;
23 
24 import android.uwb.UwbAddress;
25 
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import androidx.test.filters.SmallTest;
28 
29 import com.google.uwb.support.fira.FiraOpenSessionParams;
30 import com.google.uwb.support.fira.FiraParams;
31 import com.google.uwb.support.oemextension.AdvertisePointedTarget;
32 import com.google.uwb.support.oemextension.DeviceStatus;
33 import com.google.uwb.support.oemextension.RangingReportMetadata;
34 import com.google.uwb.support.oemextension.SessionConfigParams;
35 import com.google.uwb.support.oemextension.SessionStatus;
36 
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 import java.util.Arrays;
41 
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public class OemExtensionTests {
45 
46     @Test
testDeviceState()47     public void testDeviceState() {
48         int state = 1;
49         String chipId = "TEST_CHIP_ID";
50 
51         DeviceStatus deviceState = new DeviceStatus.Builder()
52                 .setDeviceState(state)
53                 .setChipId(chipId)
54                 .build();
55 
56         assertEquals(deviceState.getDeviceState(), state);
57         assertEquals(deviceState.getChipId(), chipId);
58 
59         DeviceStatus fromBundle = DeviceStatus.fromBundle(deviceState.toBundle());
60 
61         assertEquals(fromBundle.getDeviceState(), state);
62         assertEquals(fromBundle.getChipId(), chipId);
63     }
64 
65     @Test
testSessionStatus()66     public void testSessionStatus() {
67         long sessionId = 1;
68         int state = 0;
69         int reasonCode = 0;
70         String appPackageName = "test_app";
71         int sessionToken = 1000;
72 
73         SessionStatus sessionStatus = new SessionStatus.Builder()
74                 .setSessionId(sessionId)
75                 .setState(state)
76                 .setReasonCode(reasonCode)
77                 .setAppPackageName(appPackageName)
78                 .setSessiontoken(sessionToken)
79                 .build();
80 
81         assertEquals(sessionStatus.getSessionId(), sessionId);
82         assertEquals(sessionStatus.getState(), state);
83         assertEquals(sessionStatus.getReasonCode(), reasonCode);
84         assertEquals(sessionStatus.getAppPackageName(), appPackageName);
85         assertEquals(sessionStatus.getSessionToken(), sessionToken);
86 
87         SessionStatus fromBundle = SessionStatus.fromBundle(sessionStatus.toBundle());
88 
89         assertEquals(fromBundle.getSessionId(), sessionId);
90         assertEquals(fromBundle.getState(), state);
91         assertEquals(fromBundle.getReasonCode(), reasonCode);
92         assertEquals(fromBundle.getAppPackageName(), appPackageName);
93         assertEquals(fromBundle.getSessionToken(), sessionToken);
94     }
95 
96     @Test
testRangingReportMetadata()97     public void testRangingReportMetadata() {
98         byte[] testRawDataNtf = {0x0a, 0x0b, 0x10, 0x20, 0x6f};
99         long sessionId = 3;
100         RangingReportMetadata rangingReportMetadata = new RangingReportMetadata.Builder()
101                 .setSessionId(sessionId)
102                 .setRawNtfData(testRawDataNtf)
103                 .build();
104 
105         assertEquals(rangingReportMetadata.getRawNtfData(), testRawDataNtf);
106 
107         RangingReportMetadata fromBundle = RangingReportMetadata
108                 .fromBundle(rangingReportMetadata.toBundle());
109 
110         assertEquals(fromBundle.getSessionId(), sessionId);
111         assertEquals(Arrays.toString(fromBundle.getRawNtfData()), Arrays.toString(testRawDataNtf));
112     }
113 
114     @Test
testAdvertisePointedTarget()115     public void testAdvertisePointedTarget() {
116         byte[] macAddress = {0x0a, 0x0b};
117         boolean advertisePointingResult = true;
118 
119         AdvertisePointedTarget advertisePointedTarget = new AdvertisePointedTarget.Builder()
120                 .setMacAddress(macAddress)
121                 .setAdvertisePointingResult(advertisePointingResult)
122                 .build();
123         assertEquals(advertisePointedTarget.getMacAddress().toBytes(), macAddress);
124         assertEquals(advertisePointedTarget.isAdvertisePointingResult(), advertisePointingResult);
125 
126         AdvertisePointedTarget fromBundle = AdvertisePointedTarget.fromBundle(
127                 advertisePointedTarget.toBundle());
128 
129         assertEquals(Arrays.toString(fromBundle.getMacAddress().toBytes()),
130                 Arrays.toString(macAddress));
131         assertEquals(fromBundle.isAdvertisePointingResult(), advertisePointingResult);
132     }
133 
134     @Test
testSessionConfigParams()135     public void testSessionConfigParams() {
136         long sessionId = 100;
137         int sessionToken = 50;
138         FiraOpenSessionParams firaOpenSessionParams = new FiraOpenSessionParams.Builder()
139                 .setProtocolVersion(FiraParams.PROTOCOL_VERSION_1_1)
140                 .setSessionId((int) sessionId)
141                 .setSessionType(FiraParams.SESSION_TYPE_RANGING)
142                 .setChannelNumber(FiraParams.UWB_CHANNEL_9)
143                 .setDeviceType(RANGING_DEVICE_TYPE_CONTROLLER)
144                 .setDeviceRole(RANGING_DEVICE_ROLE_INITIATOR)
145                 .setDeviceAddress(UwbAddress.fromBytes(new byte[]{0x4, 0x6}))
146                 .setDestAddressList(Arrays.asList(UwbAddress.fromBytes(new byte[]{0x4, 0x6})))
147                 .setMultiNodeMode(MULTI_NODE_MODE_UNICAST)
148                 .setRangingRoundUsage(RANGING_ROUND_USAGE_DS_TWR_DEFERRED_MODE)
149                 .setVendorId(new byte[]{0x8, 0x7})
150                 .setStaticStsIV(new byte[]{0x1, 0x2, 0x3, 0x4, 0x5, 0x6})
151                 .build();
152 
153         SessionConfigParams sessionConfigParams = new SessionConfigParams.Builder()
154                 .setSessionId(sessionId)
155                 .setSessiontoken(sessionToken)
156                 .setOpenSessionParamsBundle(firaOpenSessionParams.toBundle())
157                 .build();
158 
159         assertEquals(sessionConfigParams.getSessionId(), sessionId);
160         assertEquals(sessionConfigParams.getSessionToken(), sessionToken);
161 
162         SessionConfigParams fromBundle = SessionConfigParams.fromBundle(
163                 sessionConfigParams.toBundle());
164 
165         assertEquals(fromBundle.getSessionId(), sessionId);
166         assertEquals(fromBundle.getSessionToken(), sessionToken);
167         FiraOpenSessionParams params = FiraOpenSessionParams.fromBundle(
168                 fromBundle.getFiraOpenSessionParamsBundle());
169         assertEquals(params.getSessionId(), (int) sessionId);
170         assertEquals(params.getDeviceRole(), RANGING_DEVICE_ROLE_INITIATOR);
171         assertEquals(params.getRangingRoundUsage(), RANGING_ROUND_USAGE_DS_TWR_DEFERRED_MODE);
172     }
173 }
174