Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [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 | #include <string> |
| 5 | |
| 6 | #include <base/files/file_util.h> |
| 7 | #include <base/logging.h> |
| 8 | #include <base/strings/stringprintf.h> |
| 9 | |
| 10 | #include "libmems/common_types.h" |
| 11 | #include "libmems/iio_channel.h" |
| 12 | #include "libmems/iio_context_impl.h" |
| 13 | #include "libmems/iio_device_trigger_impl.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | constexpr char kAddTrigger[] = "add_trigger"; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | namespace libmems { |
| 22 | |
| 23 | base::Optional<int> IioDeviceTriggerImpl::GetIdFromString(const char* id_str) { |
| 24 | size_t id_len = strlen(id_str); |
| 25 | if (id_len == strlen(kIioSysfsTrigger) && |
| 26 | strncmp(id_str, kIioSysfsTrigger, id_len) == 0) { |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | return IioDevice::GetIdAfterPrefix(id_str, kTriggerIdPrefix); |
| 31 | } |
| 32 | |
| 33 | std::string IioDeviceTriggerImpl::GetStringFromId(int id) { |
| 34 | if (id == -1) |
| 35 | return std::string(kIioSysfsTrigger); |
| 36 | return base::StringPrintf("trigger%d", id); |
| 37 | } |
| 38 | |
| 39 | IioDeviceTriggerImpl::IioDeviceTriggerImpl(IioContextImpl* ctx, iio_device* dev) |
| 40 | : IioDevice(), context_(ctx), trigger_(dev) { |
| 41 | CHECK(context_); |
| 42 | CHECK(trigger_); |
| 43 | } |
| 44 | |
| 45 | IioContext* IioDeviceTriggerImpl::GetContext() const { |
| 46 | return context_; |
| 47 | } |
| 48 | |
| 49 | const char* IioDeviceTriggerImpl::GetName() const { |
| 50 | return iio_device_get_name(trigger_); |
| 51 | } |
| 52 | |
| 53 | int IioDeviceTriggerImpl::GetId() const { |
| 54 | const char* id_str = iio_device_get_id(trigger_); |
| 55 | |
| 56 | auto id = GetIdFromString(id_str); |
| 57 | DCHECK(id.has_value()); |
| 58 | return id.value(); |
| 59 | } |
| 60 | |
| 61 | base::FilePath IioDeviceTriggerImpl::GetPath() const { |
| 62 | int id = GetId(); |
| 63 | std::string id_str = std::string(kIioSysfsTrigger); |
| 64 | if (id >= 0) |
| 65 | id_str = GetStringFromId(id); |
| 66 | |
Harvey Yang | 36779e9 | 2020-08-26 10:46:15 +0800 | [diff] [blame] | 67 | auto path = base::FilePath(kSysDevString).Append(id_str); |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 68 | CHECK(base::DirectoryExists(path)); |
| 69 | return path; |
| 70 | } |
| 71 | |
| 72 | base::Optional<std::string> IioDeviceTriggerImpl::ReadStringAttribute( |
| 73 | const std::string& name) const { |
| 74 | char data[kReadAttrBufferSize] = {0}; |
| 75 | ssize_t len = |
| 76 | iio_device_attr_read(trigger_, name.c_str(), data, sizeof(data)); |
| 77 | if (len < 0) { |
| 78 | LOG(WARNING) << "Attempting to read attribute " << name |
| 79 | << " failed: " << len; |
| 80 | return base::nullopt; |
| 81 | } |
| 82 | return std::string(data, len); |
| 83 | } |
| 84 | |
| 85 | base::Optional<int64_t> IioDeviceTriggerImpl::ReadNumberAttribute( |
| 86 | const std::string& name) const { |
| 87 | long long val = 0; // NOLINT(runtime/int) |
| 88 | int error = iio_device_attr_read_longlong(trigger_, name.c_str(), &val); |
| 89 | if (error) { |
| 90 | LOG(WARNING) << "Attempting to read attribute " << name |
| 91 | << " failed: " << error; |
| 92 | return base::nullopt; |
| 93 | } |
| 94 | return val; |
| 95 | } |
| 96 | |
Harvey Yang | 0c0039a | 2020-12-02 14:45:59 +0800 | [diff] [blame] | 97 | base::Optional<double> IioDeviceTriggerImpl::ReadDoubleAttribute( |
| 98 | const std::string& name) const { |
| 99 | double val = 0; |
| 100 | int error = iio_device_attr_read_double(trigger_, name.c_str(), &val); |
| 101 | if (error) { |
| 102 | LOG(WARNING) << "Attempting to read attribute " << name |
| 103 | << " failed: " << error; |
| 104 | return base::nullopt; |
| 105 | } |
| 106 | return val; |
| 107 | } |
| 108 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 109 | bool IioDeviceTriggerImpl::WriteNumberAttribute(const std::string& name, |
| 110 | int64_t value) { |
| 111 | int id = GetId(); |
| 112 | if ((id == -1 && name.compare(kAddTrigger) != 0) || |
Harvey Yang | 0c0039a | 2020-12-02 14:45:59 +0800 | [diff] [blame] | 113 | (id != -1 && name.compare(kSamplingFrequencyAttr) != 0)) { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 114 | return false; |
Harvey Yang | 0c0039a | 2020-12-02 14:45:59 +0800 | [diff] [blame] | 115 | } |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 116 | |
| 117 | int error = iio_device_attr_write_longlong(trigger_, name.c_str(), value); |
| 118 | if (error) { |
| 119 | LOG(WARNING) << "Attempting to write attribute " << name |
| 120 | << " failed: " << error; |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
Harvey Yang | 0c0039a | 2020-12-02 14:45:59 +0800 | [diff] [blame] | 126 | bool IioDeviceTriggerImpl::WriteDoubleAttribute(const std::string& name, |
| 127 | double value) { |
| 128 | int id = GetId(); |
| 129 | if ((id == -1 && name.compare(kAddTrigger) != 0) || |
| 130 | (id != -1 && name.compare(kSamplingFrequencyAttr) != 0)) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | int error = iio_device_attr_write_double(trigger_, name.c_str(), value); |
| 135 | if (error) { |
| 136 | LOG(WARNING) << "Attempting to write attribute " << name |
| 137 | << " failed: " << error; |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 143 | } // namespace libmems |