blob: 04af784a04d4d9ae8d5ee4006a63e525d960167d [file] [log] [blame]
Enrico Granata51cdb942019-06-18 16:40:17 -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
Enrico Granata9bb17522019-06-28 17:22:42 -07005#include "libmems/test_fakes.h"
Enrico Granata51cdb942019-06-18 16:40:17 -07006
7#include <base/logging.h>
8
9namespace libmems {
Enrico Granata9bb17522019-06-28 17:22:42 -070010namespace fakes {
Enrico Granata51cdb942019-06-18 16:40:17 -070011
Enrico Granata9bb17522019-06-28 17:22:42 -070012FakeIioChannel::FakeIioChannel(const std::string& id, bool enabled)
Enrico Granata51cdb942019-06-18 16:40:17 -070013 : id_(id), enabled_(enabled) {}
14
Enrico Granata9bb17522019-06-28 17:22:42 -070015bool FakeIioChannel::SetEnabled(bool en) {
Enrico Granata51cdb942019-06-18 16:40:17 -070016 enabled_ = en;
17 return true;
18}
19
Gwendal Grignou197d5162019-11-20 16:46:02 -080020template <typename T> base::Optional<T> FakeReadAttributes(
21 const std::string& name,
22 std::map<std::string, T> attributes) {
23 auto k = attributes.find(name);
24 if (k == attributes.end())
Enrico Granata064a25c2019-07-15 15:48:03 -070025 return base::nullopt;
26 return k->second;
27}
Gwendal Grignou197d5162019-11-20 16:46:02 -080028
29base::Optional<std::string> FakeIioChannel::ReadStringAttribute(
30 const std::string& name) const {
31 return FakeReadAttributes<>(name, text_attributes_);
32}
Enrico Granata064a25c2019-07-15 15:48:03 -070033base::Optional<int64_t> FakeIioChannel::ReadNumberAttribute(
34 const std::string& name) const {
Gwendal Grignou197d5162019-11-20 16:46:02 -080035 return FakeReadAttributes<>(name, numeric_attributes_);
Enrico Granata064a25c2019-07-15 15:48:03 -070036}
37
38void FakeIioChannel::WriteStringAttribute(const std::string& name,
39 const std::string& value) {
40 text_attributes_[name] = value;
41}
42void FakeIioChannel::WriteNumberAttribute(const std::string& name,
43 int64_t value) {
44 numeric_attributes_[name] = value;
45}
46
Enrico Granata9bb17522019-06-28 17:22:42 -070047FakeIioDevice::FakeIioDevice(FakeIioContext* ctx,
Enrico Granata51cdb942019-06-18 16:40:17 -070048 const std::string& name,
49 const std::string& id)
50 : IioDevice(), context_(ctx), name_(name), id_(id) {}
51
Enrico Granata9bb17522019-06-28 17:22:42 -070052base::Optional<std::string> FakeIioDevice::ReadStringAttribute(
Enrico Granata51cdb942019-06-18 16:40:17 -070053 const std::string& name) const {
Gwendal Grignou197d5162019-11-20 16:46:02 -080054 return FakeReadAttributes<>(name, text_attributes_);
Enrico Granata51cdb942019-06-18 16:40:17 -070055}
Enrico Granata9bb17522019-06-28 17:22:42 -070056base::Optional<int64_t> FakeIioDevice::ReadNumberAttribute(
Enrico Granata51cdb942019-06-18 16:40:17 -070057 const std::string& name) const {
Gwendal Grignou197d5162019-11-20 16:46:02 -080058 return FakeReadAttributes<>(name, numeric_attributes_);
Enrico Granata51cdb942019-06-18 16:40:17 -070059}
Enrico Granatad2e57f42019-07-31 10:46:03 -070060base::Optional<double> FakeIioDevice::ReadDoubleAttribute(
61 const std::string& name) const {
Gwendal Grignou197d5162019-11-20 16:46:02 -080062 return FakeReadAttributes<>(name, double_attributes_);
Enrico Granatad2e57f42019-07-31 10:46:03 -070063}
Enrico Granata51cdb942019-06-18 16:40:17 -070064
Enrico Granata9bb17522019-06-28 17:22:42 -070065bool FakeIioDevice::WriteStringAttribute(const std::string& name,
Enrico Granata51cdb942019-06-18 16:40:17 -070066 const std::string& value) {
67 text_attributes_[name] = value;
68 return true;
69}
Enrico Granata9bb17522019-06-28 17:22:42 -070070bool FakeIioDevice::WriteNumberAttribute(const std::string& name,
Enrico Granata51cdb942019-06-18 16:40:17 -070071 int64_t value) {
72 numeric_attributes_[name] = value;
73 return true;
74}
Enrico Granatad2e57f42019-07-31 10:46:03 -070075bool FakeIioDevice::WriteDoubleAttribute(const std::string& name,
76 double value) {
77 double_attributes_[name] = value;
78 return true;
79}
Enrico Granata51cdb942019-06-18 16:40:17 -070080
Enrico Granata9bb17522019-06-28 17:22:42 -070081bool FakeIioDevice::SetTrigger(IioDevice* trigger) {
Enrico Granata51cdb942019-06-18 16:40:17 -070082 trigger_ = trigger;
83 return true;
84}
85
Enrico Granata9bb17522019-06-28 17:22:42 -070086IioChannel* FakeIioDevice::GetChannel(const std::string& id) {
Enrico Granata51cdb942019-06-18 16:40:17 -070087 auto k = channels_.find(id);
88 if (k == channels_.end())
89 return nullptr;
90 return k->second;
91}
92
Enrico Granata9bb17522019-06-28 17:22:42 -070093bool FakeIioDevice::EnableBuffer(size_t n) {
Enrico Granata51cdb942019-06-18 16:40:17 -070094 buffer_length_ = n;
95 buffer_enabled_ = true;
96 return true;
97}
Enrico Granata9bb17522019-06-28 17:22:42 -070098bool FakeIioDevice::DisableBuffer() {
Enrico Granata51cdb942019-06-18 16:40:17 -070099 buffer_enabled_ = false;
100 return true;
101}
Enrico Granata9bb17522019-06-28 17:22:42 -0700102bool FakeIioDevice::IsBufferEnabled(size_t* n) const {
Enrico Granata51cdb942019-06-18 16:40:17 -0700103 if (n && buffer_enabled_)
104 *n = buffer_length_;
105 return buffer_enabled_;
106}
107
Enrico Granata9bb17522019-06-28 17:22:42 -0700108void FakeIioContext::AddDevice(FakeIioDevice* device) {
Enrico Granata51cdb942019-06-18 16:40:17 -0700109 CHECK(device);
110 devices_.emplace(device->GetName(), device);
111 devices_.emplace(device->GetId(), device);
112}
113
Enrico Granata9bb17522019-06-28 17:22:42 -0700114IioDevice* FakeIioContext::GetDevice(const std::string& name) {
Enrico Granata51cdb942019-06-18 16:40:17 -0700115 auto k = devices_.find(name);
116 return (k == devices_.end()) ? nullptr : k->second;
117}
118
Enrico Granata9bb17522019-06-28 17:22:42 -0700119} // namespace fakes
Enrico Granata51cdb942019-06-18 16:40:17 -0700120} // namespace libmems