blob: 8f039e2baf918ebb0cb2cebaece843c970521c5e [file] [log] [blame]
Enrico Granata60b1cbc2019-05-20 11:06:46 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <map>
6#include <memory>
7#include <string>
8
9#include <gtest/gtest.h>
10
Harvey Yang1d50fff2020-10-27 12:24:38 +080011#include <libmems/common_types.h>
Enrico Granata51cdb942019-06-18 16:40:17 -070012#include <libmems/iio_context.h>
13#include <libmems/iio_device.h>
Enrico Granata9bb17522019-06-28 17:22:42 -070014#include <libmems/test_fakes.h>
Enrico Granata60b1cbc2019-05-20 11:06:46 -070015#include "mems_setup/configuration.h"
16#include "mems_setup/delegate.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070017#include "mems_setup/sensor_location.h"
Enrico Granata9bb17522019-06-28 17:22:42 -070018#include "mems_setup/test_fakes.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070019#include "mems_setup/test_helper.h"
Enrico Granata60b1cbc2019-05-20 11:06:46 -070020
Enrico Granata60b1cbc2019-05-20 11:06:46 -070021using mems_setup::testing::SensorTestBase;
22
23namespace mems_setup {
24
25namespace {
26
Harvey Yang34f464c2020-01-08 17:43:46 +080027static gid_t kIioserviceGroupId = 777;
28
Enrico Granata60b1cbc2019-05-20 11:06:46 -070029class GyroscopeTest : public SensorTestBase {
30 public:
Harvey Yang73394622020-02-12 10:57:57 +080031 GyroscopeTest() : SensorTestBase("cros-ec-gyro", 2) {
Harvey Yang34f464c2020-01-08 17:43:46 +080032 mock_delegate_->AddGroup(Configuration::GetGroupNameForSysfs(),
33 kIioserviceGroupId);
34 }
Enrico Granata60b1cbc2019-05-20 11:06:46 -070035};
36
Harvey Yang1d50fff2020-10-27 12:24:38 +080037#if USE_IIOSERVICE
38TEST_F(GyroscopeTest, FrequencyReset) {
39 SetSingleSensor(kBaseSensorLocation);
40 ConfigureVpd({{"in_anglvel_x_base_calibbias", "100"}});
41
42 EXPECT_TRUE(GetConfiguration()->Configure());
43
44 auto frequency_opt =
45 mock_device_->ReadDoubleAttribute(libmems::kSamplingFrequencyAttr);
46 EXPECT_TRUE(frequency_opt.has_value());
47 EXPECT_EQ(frequency_opt.value(), 0.0);
48}
49#endif // USE_IIOSERVICE
50
Enrico Granata60b1cbc2019-05-20 11:06:46 -070051TEST_F(GyroscopeTest, MissingVpd) {
52 SetSingleSensor(kBaseSensorLocation);
53 ConfigureVpd({{"in_anglvel_x_base_calibbias", "100"}});
54
55 EXPECT_TRUE(GetConfiguration()->Configure());
56
Harvey Yangfc9178f2020-03-16 14:26:53 +080057 EXPECT_TRUE(mock_device_->GetChannel("anglvel_x")
58 ->ReadNumberAttribute("calibbias")
59 .has_value());
60 EXPECT_EQ(100, mock_device_->GetChannel("anglvel_x")
61 ->ReadNumberAttribute("calibbias")
62 .value());
63 EXPECT_FALSE(mock_device_->GetChannel("anglvel_y")
64 ->ReadNumberAttribute("calibbias")
65 .has_value());
66 EXPECT_FALSE(mock_device_->GetChannel("anglvel_z")
67 ->ReadNumberAttribute("calibbias")
68 .has_value());
Enrico Granata60b1cbc2019-05-20 11:06:46 -070069}
70
71TEST_F(GyroscopeTest, NotNumericVpd) {
72 SetSingleSensor(kBaseSensorLocation);
73 ConfigureVpd({{"in_anglvel_x_base_calibbias", "blah"},
74 {"in_anglvel_y_base_calibbias", "104"}});
75
76 EXPECT_TRUE(GetConfiguration()->Configure());
77
Harvey Yangfc9178f2020-03-16 14:26:53 +080078 EXPECT_FALSE(mock_device_->GetChannel("anglvel_x")
79 ->ReadNumberAttribute("calibbias")
80 .has_value());
81 EXPECT_TRUE(mock_device_->GetChannel("anglvel_y")
82 ->ReadNumberAttribute("calibbias")
83 .has_value());
84 EXPECT_EQ(104, mock_device_->GetChannel("anglvel_y")
85 ->ReadNumberAttribute("calibbias")
86 .value());
87 EXPECT_FALSE(mock_device_->GetChannel("anglvel_z")
88 ->ReadNumberAttribute("calibbias")
89 .has_value());
Enrico Granata60b1cbc2019-05-20 11:06:46 -070090}
91
92TEST_F(GyroscopeTest, VpdOutOfRange) {
93 SetSingleSensor(kBaseSensorLocation);
Gwendal Grignou4460a0d2020-01-06 14:07:00 -080094 ConfigureVpd({{"in_anglvel_x_base_calibbias", "104"},
95 {"in_anglvel_y_base_calibbias", "123456789"},
Enrico Granata60b1cbc2019-05-20 11:06:46 -070096 {"in_anglvel_z_base_calibbias", "85"}});
97
98 EXPECT_TRUE(GetConfiguration()->Configure());
99
Harvey Yangfc9178f2020-03-16 14:26:53 +0800100 EXPECT_FALSE(mock_device_->GetChannel("anglvel_x")
101 ->ReadNumberAttribute("calibbias")
102 .has_value());
103 EXPECT_FALSE(mock_device_->GetChannel("anglvel_y")
104 ->ReadNumberAttribute("calibbias")
105 .has_value());
106 EXPECT_FALSE(mock_device_->GetChannel("anglvel_z")
107 ->ReadNumberAttribute("calibbias")
108 .has_value());
Enrico Granata60b1cbc2019-05-20 11:06:46 -0700109}
110
111TEST_F(GyroscopeTest, NotLoadingTriggerModule) {
112 SetSingleSensor(kBaseSensorLocation);
113 ConfigureVpd({{"in_anglvel_x_base_calibbias", "50"},
114 {"in_anglvel_y_base_calibbias", "104"},
115 {"in_anglvel_z_base_calibbias", "85"}});
116
117 EXPECT_TRUE(GetConfiguration()->Configure());
118
119 EXPECT_EQ(0, mock_delegate_->GetNumModulesProbed());
120}
121
122TEST_F(GyroscopeTest, MultipleSensorDevice) {
Harvey Yangfc9178f2020-03-16 14:26:53 +0800123 SetSharedSensor();
Enrico Granata60b1cbc2019-05-20 11:06:46 -0700124 ConfigureVpd({{"in_anglvel_x_base_calibbias", "50"},
125 {"in_anglvel_y_base_calibbias", "104"},
126 {"in_anglvel_z_base_calibbias", "85"},
127 {"in_anglvel_y_lid_calibbias", "27"}});
128
129 EXPECT_TRUE(GetConfiguration()->Configure());
130
Harvey Yangfc9178f2020-03-16 14:26:53 +0800131 EXPECT_TRUE(mock_device_->GetChannel("anglvel_x_base")
132 ->ReadNumberAttribute("calibbias")
133 .has_value());
134 EXPECT_TRUE(mock_device_->GetChannel("anglvel_y_base")
135 ->ReadNumberAttribute("calibbias")
136 .has_value());
137 EXPECT_TRUE(mock_device_->GetChannel("anglvel_z_base")
138 ->ReadNumberAttribute("calibbias")
139 .has_value());
Enrico Granata60b1cbc2019-05-20 11:06:46 -0700140
Harvey Yangfc9178f2020-03-16 14:26:53 +0800141 EXPECT_EQ(50, mock_device_->GetChannel("anglvel_x_base")
142 ->ReadNumberAttribute("calibbias")
143 .value());
144 EXPECT_EQ(104, mock_device_->GetChannel("anglvel_y_base")
145 ->ReadNumberAttribute("calibbias")
146 .value());
147 EXPECT_EQ(85, mock_device_->GetChannel("anglvel_z_base")
148 ->ReadNumberAttribute("calibbias")
149 .value());
150
151 EXPECT_FALSE(mock_device_->GetChannel("anglvel_x_lid")
152 ->ReadNumberAttribute("calibbias")
Enrico Granata60b1cbc2019-05-20 11:06:46 -0700153 .has_value());
Harvey Yangfc9178f2020-03-16 14:26:53 +0800154 EXPECT_TRUE(mock_device_->GetChannel("anglvel_y_lid")
155 ->ReadNumberAttribute("calibbias")
156 .has_value());
157 EXPECT_EQ(27, mock_device_->GetChannel("anglvel_y_lid")
158 ->ReadNumberAttribute("calibbias")
159 .value());
160 EXPECT_FALSE(mock_device_->GetChannel("anglvel_z_lid")
161 ->ReadNumberAttribute("calibbias")
Enrico Granata60b1cbc2019-05-20 11:06:46 -0700162 .has_value());
163}
164
165} // namespace
166
167} // namespace mems_setup