blob: b11f4c3c0d9c4f5e012d2d966fc779552f1b799f [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
Enrico Granata064a25c2019-07-15 15:48:03 -070020base::Optional<std::string> FakeIioChannel::ReadStringAttribute(
21 const std::string& name) const {
22 auto k = text_attributes_.find(name);
23 if (k == text_attributes_.end())
24 return base::nullopt;
25 return k->second;
26}
27base::Optional<int64_t> FakeIioChannel::ReadNumberAttribute(
28 const std::string& name) const {
29 auto k = numeric_attributes_.find(name);
30 if (k == numeric_attributes_.end())
31 return base::nullopt;
32 return k->second;
33}
34
35void FakeIioChannel::WriteStringAttribute(const std::string& name,
36 const std::string& value) {
37 text_attributes_[name] = value;
38}
39void FakeIioChannel::WriteNumberAttribute(const std::string& name,
40 int64_t value) {
41 numeric_attributes_[name] = value;
42}
43
Enrico Granata9bb17522019-06-28 17:22:42 -070044FakeIioDevice::FakeIioDevice(FakeIioContext* ctx,
Enrico Granata51cdb942019-06-18 16:40:17 -070045 const std::string& name,
46 const std::string& id)
47 : IioDevice(), context_(ctx), name_(name), id_(id) {}
48
Enrico Granata9bb17522019-06-28 17:22:42 -070049base::Optional<std::string> FakeIioDevice::ReadStringAttribute(
Enrico Granata51cdb942019-06-18 16:40:17 -070050 const std::string& name) const {
51 auto k = text_attributes_.find(name);
52 if (k == text_attributes_.end())
53 return base::nullopt;
54 return k->second;
55}
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 {
58 auto k = numeric_attributes_.find(name);
59 if (k == numeric_attributes_.end())
60 return base::nullopt;
61 return k->second;
62}
Enrico Granatad2e57f42019-07-31 10:46:03 -070063base::Optional<double> FakeIioDevice::ReadDoubleAttribute(
64 const std::string& name) const {
65 auto k = double_attributes_.find(name);
66 if (k == double_attributes_.end())
67 return base::nullopt;
68 return k->second;
69}
Enrico Granata51cdb942019-06-18 16:40:17 -070070
Enrico Granata9bb17522019-06-28 17:22:42 -070071bool FakeIioDevice::WriteStringAttribute(const std::string& name,
Enrico Granata51cdb942019-06-18 16:40:17 -070072 const std::string& value) {
73 text_attributes_[name] = value;
74 return true;
75}
Enrico Granata9bb17522019-06-28 17:22:42 -070076bool FakeIioDevice::WriteNumberAttribute(const std::string& name,
Enrico Granata51cdb942019-06-18 16:40:17 -070077 int64_t value) {
78 numeric_attributes_[name] = value;
79 return true;
80}
Enrico Granatad2e57f42019-07-31 10:46:03 -070081bool FakeIioDevice::WriteDoubleAttribute(const std::string& name,
82 double value) {
83 double_attributes_[name] = value;
84 return true;
85}
Enrico Granata51cdb942019-06-18 16:40:17 -070086
Enrico Granata9bb17522019-06-28 17:22:42 -070087bool FakeIioDevice::SetTrigger(IioDevice* trigger) {
Enrico Granata51cdb942019-06-18 16:40:17 -070088 trigger_ = trigger;
89 return true;
90}
91
Enrico Granata9bb17522019-06-28 17:22:42 -070092IioChannel* FakeIioDevice::GetChannel(const std::string& id) {
Enrico Granata51cdb942019-06-18 16:40:17 -070093 auto k = channels_.find(id);
94 if (k == channels_.end())
95 return nullptr;
96 return k->second;
97}
98
Enrico Granata9bb17522019-06-28 17:22:42 -070099bool FakeIioDevice::EnableBuffer(size_t n) {
Enrico Granata51cdb942019-06-18 16:40:17 -0700100 buffer_length_ = n;
101 buffer_enabled_ = true;
102 return true;
103}
Enrico Granata9bb17522019-06-28 17:22:42 -0700104bool FakeIioDevice::DisableBuffer() {
Enrico Granata51cdb942019-06-18 16:40:17 -0700105 buffer_enabled_ = false;
106 return true;
107}
Enrico Granata9bb17522019-06-28 17:22:42 -0700108bool FakeIioDevice::IsBufferEnabled(size_t* n) const {
Enrico Granata51cdb942019-06-18 16:40:17 -0700109 if (n && buffer_enabled_)
110 *n = buffer_length_;
111 return buffer_enabled_;
112}
113
Enrico Granata9bb17522019-06-28 17:22:42 -0700114void FakeIioContext::AddDevice(FakeIioDevice* device) {
Enrico Granata51cdb942019-06-18 16:40:17 -0700115 CHECK(device);
116 devices_.emplace(device->GetName(), device);
117 devices_.emplace(device->GetId(), device);
118}
119
Enrico Granata9bb17522019-06-28 17:22:42 -0700120IioDevice* FakeIioContext::GetDevice(const std::string& name) {
Enrico Granata51cdb942019-06-18 16:40:17 -0700121 auto k = devices_.find(name);
122 return (k == devices_.end()) ? nullptr : k->second;
123}
124
Enrico Granata9bb17522019-06-28 17:22:42 -0700125} // namespace fakes
Enrico Granata51cdb942019-06-18 16:40:17 -0700126} // namespace libmems