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