-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_fakes.cc
370 lines (299 loc) · 9.46 KB
/
test_fakes.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "libmems/test_fakes.h"
#include <sys/eventfd.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include "base/posix/eintr_wrapper.h"
#include <base/stl_util.h>
#include "libmems/common_types.h"
namespace libmems {
namespace fakes {
FakeIioChannel::FakeIioChannel(const std::string& id, bool enabled)
: id_(id), enabled_(enabled) {}
void FakeIioChannel::SetEnabled(bool en) {
enabled_ = en;
}
bool FakeIioChannel::SetScanElementsEnabled(bool en) {
scan_elements_enabled_ = en;
return true;
}
template <typename T>
base::Optional<T> FakeReadAttributes(const std::string& name,
std::map<std::string, T> attributes) {
auto k = attributes.find(name);
if (k == attributes.end())
return base::nullopt;
return k->second;
}
base::Optional<std::string> FakeIioChannel::ReadStringAttribute(
const std::string& name) const {
return FakeReadAttributes<>(name, text_attributes_);
}
base::Optional<int64_t> FakeIioChannel::ReadNumberAttribute(
const std::string& name) const {
return FakeReadAttributes<>(name, numeric_attributes_);
}
base::Optional<double> FakeIioChannel::ReadDoubleAttribute(
const std::string& name) const {
return FakeReadAttributes<>(name, double_attributes_);
}
bool FakeIioChannel::WriteStringAttribute(const std::string& name,
const std::string& value) {
text_attributes_[name] = value;
return true;
}
bool FakeIioChannel::WriteNumberAttribute(const std::string& name,
int64_t value) {
numeric_attributes_[name] = value;
return true;
}
bool FakeIioChannel::WriteDoubleAttribute(const std::string& name,
double value) {
double_attributes_[name] = value;
return true;
}
base::Optional<int64_t> FakeIioChannel::GetData(int index) {
if (!enabled_ || index < 0 || index >= base::size(kFakeAccelSamples))
return base::nullopt;
auto raw = ReadNumberAttribute(kRawAttr);
if (raw.has_value())
return raw;
for (int i = 0; i < base::size(kFakeAccelChns); ++i) {
if (id_.compare(kFakeAccelChns[i]) == 0)
return kFakeAccelSamples[index][i];
}
return base::nullopt;
}
FakeIioDevice::FakeIioDevice(FakeIioContext* ctx,
const std::string& name,
int id)
: IioDevice(), context_(ctx), name_(name), id_(id) {}
base::FilePath FakeIioDevice::GetPath() const {
std::string id_str(kDeviceIdPrefix);
id_str.append(std::to_string(GetId()));
return base::FilePath(kSysDevString).Append(id_str);
}
base::Optional<std::string> FakeIioDevice::ReadStringAttribute(
const std::string& name) const {
if (name.compare(kDeviceName) == 0)
return name_;
return FakeReadAttributes<>(name, text_attributes_);
}
base::Optional<int64_t> FakeIioDevice::ReadNumberAttribute(
const std::string& name) const {
return FakeReadAttributes<>(name, numeric_attributes_);
}
base::Optional<double> FakeIioDevice::ReadDoubleAttribute(
const std::string& name) const {
return FakeReadAttributes<>(name, double_attributes_);
}
bool FakeIioDevice::WriteStringAttribute(const std::string& name,
const std::string& value) {
text_attributes_[name] = value;
return true;
}
bool FakeIioDevice::WriteNumberAttribute(const std::string& name,
int64_t value) {
numeric_attributes_[name] = value;
return true;
}
bool FakeIioDevice::WriteDoubleAttribute(const std::string& name,
double value) {
double_attributes_[name] = value;
return true;
}
bool FakeIioDevice::SetTrigger(IioDevice* trigger) {
trigger_ = trigger;
return true;
}
bool FakeIioDevice::EnableBuffer(size_t n) {
buffer_length_ = n;
buffer_enabled_ = true;
return true;
}
bool FakeIioDevice::DisableBuffer() {
buffer_enabled_ = false;
return true;
}
bool FakeIioDevice::IsBufferEnabled(size_t* n) const {
if (n && buffer_enabled_)
*n = buffer_length_;
return buffer_enabled_;
}
base::Optional<int32_t> FakeIioDevice::GetBufferFd() {
if (disabled_fd_)
return base::nullopt;
if (!CreateBuffer())
return base::nullopt;
return sample_fd_.get();
}
base::Optional<IioDevice::IioSample> FakeIioDevice::ReadSample() {
if (is_paused_ || disabled_fd_)
return base::nullopt;
if (!failed_read_queue_.empty()) {
CHECK_GE(failed_read_queue_.top(), sample_index_);
if (failed_read_queue_.top() == sample_index_) {
failed_read_queue_.pop();
return base::nullopt;
}
}
if (!CreateBuffer())
return base::nullopt;
if (!ReadByte())
return base::nullopt;
base::Optional<double> freq_opt = ReadDoubleAttribute(kSamplingFrequencyAttr);
if (!freq_opt.has_value()) {
LOG(ERROR) << "sampling_frequency not set";
return base::nullopt;
}
double frequency = freq_opt.value();
if (frequency <= 0.0) {
LOG(ERROR) << "Invalid frequency: " << frequency;
return base::nullopt;
}
IioDevice::IioSample sample;
auto channels = GetAllChannels();
for (int32_t i = 0; i < channels.size(); ++i) {
FakeIioChannel* chn = dynamic_cast<FakeIioChannel*>(channels[i]);
auto value = chn->GetData(sample_index_);
if (!value.has_value()) {
LOG(ERROR) << "Channel: " << channels_[i].chn_id << " has no sample";
return base::nullopt;
}
sample[i] = value.value();
}
sample_index_ += 1;
if (sample_index_ < base::size(kFakeAccelSamples)) {
if (pause_index_.has_value() && sample_index_ == pause_index_.value())
SetPause();
else if (!WriteByte())
return base::nullopt;
}
return sample;
}
base::TimeDelta FakeIioDevice::GetPeriodForObsoleteSamplesInMilliseconds() {
return base::TimeDelta::FromMilliseconds(0.0);
}
void FakeIioDevice::DisableFd() {
disabled_fd_ = true;
if (readable_fd_)
CHECK(ReadByte());
}
void FakeIioDevice::AddFailedReadAtKthSample(int k) {
CHECK_GE(k, sample_index_);
failed_read_queue_.push(k);
}
void FakeIioDevice::SetPauseCallbackAtKthSamples(
int k, base::OnceCallback<void()> callback) {
CHECK_GE(k, sample_index_);
CHECK_LE(k, base::size(kFakeAccelSamples));
CHECK(!pause_index_.has_value()); // pause callback hasn't been set
pause_index_ = k;
pause_callback_ = std::move(callback);
if (pause_index_.value() != sample_index_)
return;
SetPause();
}
void FakeIioDevice::ResumeReadingSamples() {
CHECK(is_paused_);
is_paused_ = false;
if (sample_fd_.is_valid() && !readable_fd_)
CHECK(WriteByte());
}
bool FakeIioDevice::CreateBuffer() {
CHECK(!disabled_fd_);
if (sample_fd_.is_valid())
return true;
int fd = eventfd(0, 0);
CHECK_GE(fd, 0);
sample_fd_.reset(fd);
if (sample_index_ >= base::size(kFakeAccelSamples) || is_paused_)
return true;
if (!WriteByte()) {
ClosePipe();
return false;
}
return true;
}
bool FakeIioDevice::WriteByte() {
if (!sample_fd_.is_valid())
return false;
CHECK(!readable_fd_);
uint64_t val = 1;
CHECK_EQ(write(sample_fd_.get(), &val, sizeof(uint64_t)), sizeof(uint64_t));
readable_fd_ = true;
return true;
}
bool FakeIioDevice::ReadByte() {
if (!sample_fd_.is_valid())
return false;
CHECK(readable_fd_);
int64_t val = 1;
CHECK_EQ(read(sample_fd_.get(), &val, sizeof(uint64_t)), sizeof(uint64_t));
readable_fd_ = false;
return true;
}
void FakeIioDevice::ClosePipe() {
sample_fd_.reset();
}
void FakeIioDevice::SetPause() {
is_paused_ = true;
pause_index_.reset();
std::move(pause_callback_).Run();
if (readable_fd_)
CHECK(ReadByte());
}
void FakeIioContext::AddDevice(std::unique_ptr<FakeIioDevice> device) {
CHECK(device.get());
devices_.emplace(device->GetId(), std::move(device));
}
void FakeIioContext::AddTrigger(std::unique_ptr<FakeIioDevice> trigger) {
CHECK(trigger.get());
triggers_.emplace(trigger->GetId(), std::move(trigger));
}
std::vector<IioDevice*> FakeIioContext::GetDevicesByName(
const std::string& name) {
return GetFakeByName(name, devices_);
}
IioDevice* FakeIioContext::GetDeviceById(int id) {
return GetFakeById(id, devices_);
}
std::vector<IioDevice*> FakeIioContext::GetAllDevices() {
return GetFakeAll(devices_);
}
std::vector<IioDevice*> FakeIioContext::GetTriggersByName(
const std::string& name) {
return GetFakeByName(name, triggers_);
}
IioDevice* FakeIioContext::GetTriggerById(int id) {
return GetFakeById(id, triggers_);
}
std::vector<IioDevice*> FakeIioContext::GetAllTriggers() {
return GetFakeAll(triggers_);
}
IioDevice* FakeIioContext::GetFakeById(
int id, const std::map<int, std::unique_ptr<FakeIioDevice>>& devices_map) {
auto k = devices_map.find(id);
return (k == devices_map.end()) ? nullptr : k->second.get();
}
std::vector<IioDevice*> FakeIioContext::GetFakeByName(
const std::string& name,
const std::map<int, std::unique_ptr<FakeIioDevice>>& devices_map) {
std::vector<IioDevice*> devices;
for (auto const& it : devices_map) {
if (name.compare(it.second->GetName()) == 0)
devices.push_back(it.second.get());
}
return devices;
}
std::vector<IioDevice*> FakeIioContext::GetFakeAll(
const std::map<int, std::unique_ptr<FakeIioDevice>>& devices_map) {
std::vector<IioDevice*> devices;
for (auto const& it : devices_map)
devices.push_back(it.second.get());
return devices;
}
} // namespace fakes
} // namespace libmems