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
17 #include "chre/pal/util/wifi_pal_convert.h"
18 #include "chre_api/chre/wifi.h"
19
20 #include <cinttypes>
21
22 #include "chre/util/macros.h"
23 #include "gtest/gtest.h"
24
25 /************************************************
26 * Private functions
27 ***********************************************/
28 namespace {
29
validateLciConvert(const uint8_t * lci,size_t len,const struct chreWifiRangingResult & expectedResult)30 void validateLciConvert(const uint8_t *lci, size_t len,
31 const struct chreWifiRangingResult &expectedResult) {
32 struct chreWifiRangingResult result;
33 ASSERT_TRUE(chreWifiLciFromIe(lci, len, &result));
34 EXPECT_EQ(result.lci.latitude, expectedResult.lci.latitude);
35 EXPECT_EQ(result.lci.longitude, expectedResult.lci.longitude);
36 EXPECT_EQ(result.lci.altitude, expectedResult.lci.altitude);
37 EXPECT_EQ(result.lci.latitudeUncertainty,
38 expectedResult.lci.latitudeUncertainty);
39 EXPECT_EQ(result.lci.longitudeUncertainty,
40 expectedResult.lci.longitudeUncertainty);
41 EXPECT_EQ(result.lci.altitudeType, expectedResult.lci.altitudeType);
42 EXPECT_EQ(result.lci.altitudeUncertainty,
43 expectedResult.lci.altitudeUncertainty);
44 EXPECT_EQ(result.flags, CHRE_WIFI_RTT_RESULT_HAS_LCI);
45 }
46
47 } // anonymous namespace
48
49 /************************************************
50 * Tests
51 ***********************************************/
TEST(WifiPalConvert,SimpleConvertTest)52 TEST(WifiPalConvert, SimpleConvertTest) {
53 // Example taken from IEEE P802.11-REVmc/D8.0, section 9.4.2.22.10
54 uint8_t lci[CHRE_LCI_IE_HEADER_LEN_BYTES +
55 CHRE_LCI_SUBELEMENT_HEADER_LEN_BYTES +
56 CHRE_LCI_SUBELEMENT_DATA_LEN_BYTES] = {
57 0x01, 0x0, 0x08, 0x0, 0x10, 0x52, 0x83, 0x4d, 0x12, 0xef, 0xd2,
58 0xb0, 0x8b, 0x9b, 0x4b, 0xf1, 0xcc, 0x2c, 0x00, 0x00, 0x41};
59
60 struct chreWifiRangingResult expectedResult;
61 expectedResult.lci = {
62 .latitude = -1136052723, // -33.857 deg
63 .longitude = 5073940163, // 151.2152 deg
64 .altitude = 2867, // 11.2 m
65 .latitudeUncertainty = 18,
66 .longitudeUncertainty = 18,
67 .altitudeType = 1, // CHRE_WIFI_LCI_ALTITUDE_TYPE_METERS
68 .altitudeUncertainty = 15,
69 };
70
71 validateLciConvert(lci, ARRAY_SIZE(lci), expectedResult);
72 }
73
TEST(WifiPalConvert,ExtraDataTest)74 TEST(WifiPalConvert, ExtraDataTest) {
75 uint8_t lci[CHRE_LCI_IE_HEADER_LEN_BYTES +
76 CHRE_LCI_SUBELEMENT_HEADER_LEN_BYTES +
77 CHRE_LCI_SUBELEMENT_DATA_LEN_BYTES + 2] = {
78 0x01, 0x0, 0x08, 0x0, 0x10, 0x52, 0x83, 0x4d, 0x12, 0xef, 0xd2, 0xb0,
79 0x8b, 0x9b, 0x4b, 0xf1, 0xcc, 0x2c, 0x00, 0x00, 0x41, 0x00, 0x00};
80
81 struct chreWifiRangingResult expectedResult;
82 expectedResult.lci = {
83 .latitude = -1136052723, // -33.857 deg
84 .longitude = 5073940163, // 151.2152 deg
85 .altitude = 2867, // 11.2 m
86 .latitudeUncertainty = 18,
87 .longitudeUncertainty = 18,
88 .altitudeType = 1, // CHRE_WIFI_LCI_ALTITUDE_TYPE_METERS
89 .altitudeUncertainty = 15,
90 };
91
92 validateLciConvert(lci, ARRAY_SIZE(lci), expectedResult);
93 }
94
TEST(WifiPalConvert,NoLciTest)95 TEST(WifiPalConvert, NoLciTest) {
96 uint8_t lci[CHRE_LCI_IE_HEADER_LEN_BYTES +
97 CHRE_LCI_SUBELEMENT_HEADER_LEN_BYTES] = {0x01, 0x0, 0x08, 0x0,
98 0x0};
99
100 struct chreWifiRangingResult result;
101 ASSERT_TRUE(chreWifiLciFromIe(lci, ARRAY_SIZE(lci), &result));
102 EXPECT_EQ(result.flags, 0);
103 }
104
TEST(WifiPalConvert,InvalidLciTest)105 TEST(WifiPalConvert, InvalidLciTest) {
106 uint8_t lci[CHRE_LCI_IE_HEADER_LEN_BYTES] = {0x01, 0x0, 0x08};
107
108 struct chreWifiRangingResult result;
109 EXPECT_FALSE(chreWifiLciFromIe(lci, ARRAY_SIZE(lci), &result));
110 }
111