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 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 11 | #include "libmems/common_types.h" |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 12 | #include "libmems/iio_channel_impl.h" |
| 13 | #include "libmems/iio_device.h" |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 14 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 15 | namespace libmems { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 16 | |
| 17 | IioChannelImpl::IioChannelImpl(iio_channel* channel) : channel_(channel) { |
| 18 | CHECK(channel_); |
| 19 | } |
| 20 | |
| 21 | const char* IioChannelImpl::GetId() const { |
| 22 | return iio_channel_get_id(channel_); |
| 23 | } |
| 24 | |
| 25 | bool IioChannelImpl::IsEnabled() const { |
| 26 | return iio_channel_is_enabled(channel_); |
| 27 | } |
| 28 | |
| 29 | bool IioChannelImpl::SetEnabled(bool en) { |
| 30 | if (en) |
| 31 | iio_channel_enable(channel_); |
| 32 | else |
| 33 | iio_channel_disable(channel_); |
| 34 | |
| 35 | // this tool will not stick around listening to this channel, |
| 36 | // all it needs to do is leave the channel enabled for Chrome to use; |
| 37 | // so, we directly write to the scan elements instead of setting up |
| 38 | // a buffer and keeping it enabled while we run (which wouldn't be long |
| 39 | // enough anyway). we do not need to handle the non scan-element case for |
| 40 | // the channels we care about. |
| 41 | if (!iio_channel_is_scan_element(channel_)) |
| 42 | return true; |
| 43 | |
| 44 | std::string en_attrib_name = base::StringPrintf( |
| 45 | "scan_elements/%s_%s_en", |
| 46 | iio_channel_is_output(channel_) ? "out" : "in", GetId()); |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 47 | int error = iio_channel_attr_write_bool(channel_, en_attrib_name.c_str(), en); |
| 48 | if (error) { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 49 | LOG(WARNING) << "could not write to " << en_attrib_name |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 50 | << ", error: " << error; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
Harvey Yang | 96029cb | 2019-06-03 17:26:14 +0800 | [diff] [blame] | 57 | base::Optional<std::string> IioChannelImpl::ReadStringAttribute( |
| 58 | const std::string& name) const { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 59 | char data[kReadAttrBufferSize] = {0}; |
Harvey Yang | 96029cb | 2019-06-03 17:26:14 +0800 | [diff] [blame] | 60 | ssize_t len = |
| 61 | iio_channel_attr_read(channel_, name.c_str(), data, sizeof(data)); |
| 62 | if (len < 0) { |
| 63 | LOG(WARNING) << "Attempting to read attribute " << name |
| 64 | << " failed: " << len; |
| 65 | return base::nullopt; |
| 66 | } |
| 67 | return std::string(data, len); |
| 68 | } |
| 69 | |
| 70 | base::Optional<int64_t> IioChannelImpl::ReadNumberAttribute( |
| 71 | const std::string& name) const { |
| 72 | long long val = 0; // NOLINT(runtime/int) |
| 73 | int error = iio_channel_attr_read_longlong(channel_, name.c_str(), &val); |
| 74 | if (error) { |
| 75 | LOG(WARNING) << "Attempting to read attribute " << name |
| 76 | << " failed: " << error; |
| 77 | return base::nullopt; |
| 78 | } |
| 79 | return val; |
| 80 | } |
| 81 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 82 | base::Optional<int64_t> IioChannelImpl::Convert(const uint8_t* src) const { |
| 83 | const iio_data_format* format = iio_channel_get_data_format(channel_); |
| 84 | if (!format) { |
| 85 | LOG(WARNING) << "Cannot find format of channel: " << GetId(); |
| 86 | return base::nullopt; |
| 87 | } |
| 88 | |
| 89 | size_t len = format->length; |
| 90 | if (len == 0) |
| 91 | return 0; |
| 92 | |
| 93 | int64_t value; |
| 94 | iio_channel_convert(channel_, &value, src); |
| 95 | |
| 96 | if (format->is_signed && len < CHAR_BIT * sizeof(int64_t)) { |
| 97 | int64_t mask = 1LL << (len - 1); |
| 98 | |
| 99 | if (mask & value) { |
| 100 | // Doing sign extension |
| 101 | value |= (~0LL) << len; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return value; |
| 106 | } |
| 107 | |
| 108 | base::Optional<uint64_t> IioChannelImpl::Length() const { |
| 109 | const iio_data_format* format = iio_channel_get_data_format(channel_); |
| 110 | if (!format) { |
| 111 | LOG(WARNING) << "Cannot find format of channel: " << GetId(); |
| 112 | return base::nullopt; |
| 113 | } |
| 114 | |
| 115 | return format->length; |
| 116 | } |
| 117 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 118 | } // namespace libmems |