Enrico Granata | 60a818d | 2019-05-09 09:56:09 -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 | |
| 5 | #include <memory> |
| 6 | #include <string> |
| 7 | |
| 8 | #include <base/logging.h> |
| 9 | #include <base/strings/stringprintf.h> |
| 10 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame^] | 11 | #include "libmems/iio_channel_impl.h" |
| 12 | #include "libmems/iio_device.h" |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 13 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame^] | 14 | namespace libmems { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 15 | |
| 16 | IioChannelImpl::IioChannelImpl(iio_channel* channel) : channel_(channel) { |
| 17 | CHECK(channel_); |
| 18 | } |
| 19 | |
| 20 | const char* IioChannelImpl::GetId() const { |
| 21 | return iio_channel_get_id(channel_); |
| 22 | } |
| 23 | |
| 24 | bool IioChannelImpl::IsEnabled() const { |
| 25 | return iio_channel_is_enabled(channel_); |
| 26 | } |
| 27 | |
| 28 | bool IioChannelImpl::SetEnabled(bool en) { |
| 29 | if (en) |
| 30 | iio_channel_enable(channel_); |
| 31 | else |
| 32 | iio_channel_disable(channel_); |
| 33 | |
| 34 | // this tool will not stick around listening to this channel, |
| 35 | // all it needs to do is leave the channel enabled for Chrome to use; |
| 36 | // so, we directly write to the scan elements instead of setting up |
| 37 | // a buffer and keeping it enabled while we run (which wouldn't be long |
| 38 | // enough anyway). we do not need to handle the non scan-element case for |
| 39 | // the channels we care about. |
| 40 | if (!iio_channel_is_scan_element(channel_)) |
| 41 | return true; |
| 42 | |
| 43 | std::string en_attrib_name = base::StringPrintf( |
| 44 | "scan_elements/%s_%s_en", |
| 45 | iio_channel_is_output(channel_) ? "out" : "in", GetId()); |
| 46 | int ok = iio_channel_attr_write_bool(channel_, en_attrib_name.c_str(), en); |
| 47 | if (!ok) { |
| 48 | LOG(WARNING) << "could not write to " << en_attrib_name |
| 49 | << ", error: " << ok; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
Harvey Yang | 96029cb | 2019-06-03 17:26:14 +0800 | [diff] [blame] | 56 | base::Optional<std::string> IioChannelImpl::ReadStringAttribute( |
| 57 | const std::string& name) const { |
| 58 | char data[1024] = {0}; |
| 59 | ssize_t len = |
| 60 | iio_channel_attr_read(channel_, name.c_str(), data, sizeof(data)); |
| 61 | if (len < 0) { |
| 62 | LOG(WARNING) << "Attempting to read attribute " << name |
| 63 | << " failed: " << len; |
| 64 | return base::nullopt; |
| 65 | } |
| 66 | return std::string(data, len); |
| 67 | } |
| 68 | |
| 69 | base::Optional<int64_t> IioChannelImpl::ReadNumberAttribute( |
| 70 | const std::string& name) const { |
| 71 | long long val = 0; // NOLINT(runtime/int) |
| 72 | int error = iio_channel_attr_read_longlong(channel_, name.c_str(), &val); |
| 73 | if (error) { |
| 74 | LOG(WARNING) << "Attempting to read attribute " << name |
| 75 | << " failed: " << error; |
| 76 | return base::nullopt; |
| 77 | } |
| 78 | return val; |
| 79 | } |
| 80 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame^] | 81 | } // namespace libmems |