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> |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 7 | #include <vector> |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 8 | |
| 9 | #include <base/files/file_util.h> |
| 10 | #include <base/logging.h> |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 12 | |
Harvey Yang | 6698c86 | 2019-09-16 17:24:38 +0800 | [diff] [blame] | 13 | #include "libmems/common_types.h" |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 14 | #include "libmems/iio_channel_impl.h" |
| 15 | #include "libmems/iio_context_impl.h" |
| 16 | #include "libmems/iio_device_impl.h" |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 17 | #include "libmems/iio_device_trigger_impl.h" |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 18 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 19 | #define ERROR_BUFFER_SIZE 256 |
| 20 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 21 | namespace libmems { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 22 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 23 | namespace { |
| 24 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 25 | constexpr int kNumSamples = 1; |
| 26 | |
| 27 | }; // namespace |
| 28 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 29 | base::Optional<int> IioDeviceImpl::GetIdFromString(const char* id_str) { |
| 30 | return IioDevice::GetIdAfterPrefix(id_str, kDeviceIdPrefix); |
| 31 | } |
| 32 | |
| 33 | std::string IioDeviceImpl::GetStringFromId(int id) { |
| 34 | return base::StringPrintf("%s%d", kDeviceIdPrefix, id); |
| 35 | } |
| 36 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 37 | IioDeviceImpl::IioDeviceImpl(IioContextImpl* ctx, iio_device* dev) |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 38 | : IioDevice(), |
| 39 | context_(ctx), |
| 40 | device_(dev), |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 41 | buffer_(nullptr, IioBufferDeleter) { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 42 | CHECK(context_); |
| 43 | CHECK(device_); |
Harvey Yang | 5e6cb70 | 2020-01-15 10:57:44 +0800 | [diff] [blame^] | 44 | |
| 45 | EnableAllChannels(); |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | IioContext* IioDeviceImpl::GetContext() const { |
| 49 | return context_; |
| 50 | } |
| 51 | |
| 52 | const char* IioDeviceImpl::GetName() const { |
| 53 | return iio_device_get_name(device_); |
| 54 | } |
| 55 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 56 | int IioDeviceImpl::GetId() const { |
| 57 | const char* id_str = iio_device_get_id(device_); |
| 58 | |
| 59 | auto id = GetIdFromString(id_str); |
| 60 | DCHECK(id.has_value()); |
| 61 | return id.value(); |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | base::FilePath IioDeviceImpl::GetPath() const { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 65 | std::string id_str = GetStringFromId(GetId()); |
| 66 | auto path = base::FilePath("/sys/bus/iio/devices").Append(id_str); |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 67 | CHECK(base::DirectoryExists(path)); |
| 68 | return path; |
| 69 | } |
| 70 | |
| 71 | base::Optional<std::string> IioDeviceImpl::ReadStringAttribute( |
| 72 | const std::string& name) const { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 73 | char data[kReadAttrBufferSize] = {0}; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 74 | ssize_t len = iio_device_attr_read(device_, name.c_str(), data, sizeof(data)); |
| 75 | if (len < 0) { |
| 76 | LOG(WARNING) << "Attempting to read attribute " << name |
| 77 | << " failed: " << len; |
| 78 | return base::nullopt; |
| 79 | } |
| 80 | return std::string(data, len); |
| 81 | } |
| 82 | |
| 83 | base::Optional<int64_t> IioDeviceImpl::ReadNumberAttribute( |
| 84 | const std::string& name) const { |
| 85 | long long val = 0; // NOLINT(runtime/int) |
| 86 | int error = iio_device_attr_read_longlong(device_, name.c_str(), &val); |
| 87 | if (error) { |
| 88 | LOG(WARNING) << "Attempting to read attribute " << name |
| 89 | << " failed: " << error; |
| 90 | return base::nullopt; |
| 91 | } |
| 92 | return val; |
| 93 | } |
| 94 | |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 95 | base::Optional<double> IioDeviceImpl::ReadDoubleAttribute( |
| 96 | const std::string& name) const { |
| 97 | double val = 0; |
| 98 | int error = iio_device_attr_read_double(device_, name.c_str(), &val); |
| 99 | if (error) { |
| 100 | LOG(WARNING) << "Attempting to read attribute " << name |
| 101 | << " failed: " << error; |
| 102 | return base::nullopt; |
| 103 | } |
| 104 | return val; |
| 105 | } |
| 106 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 107 | bool IioDeviceImpl::WriteStringAttribute(const std::string& name, |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 108 | const std::string& value) { |
| 109 | int error = iio_device_attr_write_raw(device_, name.c_str(), value.data(), |
| 110 | value.size()); |
| 111 | if (error < 0) { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 112 | LOG(WARNING) << "Attempting to write attribute " << name |
| 113 | << " failed: " << error; |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 118 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 119 | bool IioDeviceImpl::WriteNumberAttribute(const std::string& name, |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 120 | int64_t value) { |
| 121 | int error = iio_device_attr_write_longlong(device_, name.c_str(), value); |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 122 | if (error) { |
| 123 | LOG(WARNING) << "Attempting to write attribute " << name |
| 124 | << " failed: " << error; |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 130 | bool IioDeviceImpl::WriteDoubleAttribute(const std::string& name, |
| 131 | double value) { |
| 132 | int error = iio_device_attr_write_double(device_, name.c_str(), value); |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 133 | if (error) { |
| 134 | LOG(WARNING) << "Attempting to write attribute " << name |
| 135 | << " failed: " << error; |
| 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 141 | iio_device* IioDeviceImpl::GetUnderlyingIioDevice() const { |
| 142 | return device_; |
| 143 | } |
| 144 | |
| 145 | bool IioDeviceImpl::SetTrigger(IioDevice* trigger_device) { |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 146 | // Reset the old - if any - and then add the new trigger. |
| 147 | int error = iio_device_set_trigger(device_, NULL); |
| 148 | if (error) { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 149 | LOG(WARNING) << "Unable to clean trigger of device " << GetId() |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 150 | << ", error: " << error; |
| 151 | return false; |
| 152 | } |
| 153 | if (trigger_device == nullptr) |
| 154 | return true; |
| 155 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 156 | const iio_device* impl_device = nullptr; |
| 157 | int id = trigger_device->GetId(); |
| 158 | if (id == -2) { |
| 159 | impl_device = iio_context_find_device(GetContext()->GetCurrentContext(), |
| 160 | kIioSysfsTrigger); |
| 161 | } else { |
| 162 | std::string id_str = IioDeviceTriggerImpl::GetStringFromId(id); |
| 163 | impl_device = iio_context_find_device(GetContext()->GetCurrentContext(), |
| 164 | id_str.c_str()); |
| 165 | } |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 166 | if (!impl_device) { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 167 | LOG(WARNING) << "cannot find device " << id << " in the current context"; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 170 | |
| 171 | error = iio_device_set_trigger(device_, impl_device); |
| 172 | if (error) { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 173 | LOG(WARNING) << "Unable to set trigger for device " << GetId() |
| 174 | << " to be device " << trigger_device->GetId() |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 175 | << ", error: " << error; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | IioDevice* IioDeviceImpl::GetTrigger() { |
| 182 | const iio_device* trigger; |
| 183 | int error = iio_device_get_trigger(device_, &trigger); |
| 184 | if (error) { |
| 185 | LOG(WARNING) << "Unable to get trigger for device " << GetId(); |
| 186 | return nullptr; |
| 187 | } |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 188 | |
| 189 | if (trigger == nullptr) |
| 190 | return nullptr; |
| 191 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 192 | const char* id_str = iio_device_get_id(trigger); |
| 193 | auto id = IioDeviceTriggerImpl::GetIdFromString(id_str); |
| 194 | |
| 195 | IioDevice* trigger_device = nullptr; |
| 196 | if (id.has_value()) |
| 197 | trigger_device = GetContext()->GetTriggerById(id.value()); |
| 198 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 199 | if (trigger_device == nullptr) { |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 200 | LOG(WARNING) << GetId() << " has trigger device " << id_str |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 201 | << "which cannot be found in this context"; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 202 | } |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 203 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 204 | return trigger_device; |
| 205 | } |
| 206 | |
Harvey Yang | b0c3096 | 2019-09-17 15:00:25 +0800 | [diff] [blame] | 207 | std::vector<IioChannel*> IioDeviceImpl::GetAllChannels() { |
| 208 | std::vector<IioChannel*> channels; |
| 209 | uint32_t chn_count = iio_device_get_channels_count(device_); |
| 210 | |
| 211 | for (int i = 0; i < chn_count; ++i) { |
| 212 | iio_channel* channel = iio_device_get_channel(device_, i); |
| 213 | if (channel == nullptr) { |
| 214 | LOG(WARNING) << "Unable to get " << i |
| 215 | << "th channel from device: " << GetId(); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | channels.push_back(GetChannel(iio_channel_get_id(channel))); |
| 220 | } |
| 221 | |
| 222 | return channels; |
| 223 | } |
| 224 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 225 | IioChannel* IioDeviceImpl::GetChannel(const std::string& name) { |
| 226 | auto k = channels_.find(name); |
| 227 | if (k != channels_.end()) |
| 228 | return k->second.get(); |
| 229 | iio_channel* channel = iio_device_find_channel(device_, name.c_str(), true); |
| 230 | if (channel == nullptr) |
| 231 | channel = iio_device_find_channel(device_, name.c_str(), false); |
| 232 | if (channel == nullptr) |
| 233 | return nullptr; |
| 234 | channels_.emplace(name, std::make_unique<IioChannelImpl>(channel)); |
| 235 | return channels_[name].get(); |
| 236 | } |
| 237 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 238 | base::Optional<size_t> IioDeviceImpl::GetSampleSize() const { |
| 239 | ssize_t sample_size = iio_device_get_sample_size(device_); |
| 240 | if (sample_size < 0) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 241 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 242 | iio_strerror(errno, errMsg, sizeof(errMsg)); |
| 243 | LOG(WARNING) << "Unable to get sample size: " << errMsg; |
| 244 | return base::nullopt; |
| 245 | } |
| 246 | |
| 247 | return static_cast<size_t>(sample_size); |
| 248 | } |
| 249 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 250 | bool IioDeviceImpl::EnableBuffer(size_t count) { |
| 251 | if (!WriteNumberAttribute("buffer/length", count)) |
| 252 | return false; |
| 253 | if (!WriteNumberAttribute("buffer/enable", 1)) |
| 254 | return false; |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | bool IioDeviceImpl::DisableBuffer() { |
| 260 | return WriteNumberAttribute("buffer/enable", 0); |
| 261 | } |
| 262 | |
| 263 | bool IioDeviceImpl::IsBufferEnabled(size_t* count) const { |
| 264 | bool enabled = (ReadNumberAttribute("buffer/enable").value_or(0) == 1); |
| 265 | if (enabled && count) |
| 266 | *count = ReadNumberAttribute("buffer/length").value_or(0); |
| 267 | |
| 268 | return enabled; |
| 269 | } |
| 270 | |
Harvey Yang | e8b301b | 2020-05-19 14:43:03 +0800 | [diff] [blame] | 271 | base::Optional<int32_t> IioDeviceImpl::GetBufferFd() { |
| 272 | if (!CreateBuffer()) |
| 273 | return base::nullopt; |
| 274 | |
| 275 | int32_t fd = iio_buffer_get_poll_fd(buffer_.get()); |
| 276 | if (fd < 0) { |
| 277 | LOG(ERROR) << "Failed to get poll fd: " << fd; |
| 278 | return base::nullopt; |
| 279 | } |
| 280 | |
| 281 | return fd; |
| 282 | } |
| 283 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 284 | bool IioDeviceImpl::ReadEvent(std::vector<uint8_t>* event) { |
| 285 | if (!CreateBuffer()) |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 286 | return false; |
| 287 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 288 | event->clear(); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 289 | |
| 290 | ssize_t ret = iio_buffer_refill(buffer_.get()); |
| 291 | if (ret < 0) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 292 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 293 | iio_strerror(-ret, errMsg, sizeof(errMsg)); |
| 294 | LOG(ERROR) << "Unable to refill buffer: " << errMsg; |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 295 | buffer_.reset(); |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 296 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | const auto buf_step = iio_buffer_step(buffer_.get()); |
| 301 | size_t sample_size = GetSampleSize().value_or(0); |
| 302 | |
| 303 | // There is something wrong when refilling the buffer. |
| 304 | if (buf_step != sample_size) { |
| 305 | LOG(ERROR) << "sample_size doesn't match in refill: " << buf_step |
| 306 | << ", sample_size: " << sample_size; |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 307 | buffer_.reset(); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 308 | |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | uint8_t* start = reinterpret_cast<uint8_t*>(iio_buffer_start(buffer_.get())); |
| 313 | size_t len = reinterpret_cast<intptr_t>(iio_buffer_end(buffer_.get())) - |
| 314 | reinterpret_cast<intptr_t>(start); |
| 315 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 316 | event->reserve(len); |
| 317 | event->insert(event->begin(), start, start + len); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | // static |
| 323 | void IioDeviceImpl::IioBufferDeleter(iio_buffer* buffer) { |
| 324 | iio_buffer_cancel(buffer); |
| 325 | iio_buffer_destroy(buffer); |
| 326 | } |
| 327 | |
Harvey Yang | 5e6cb70 | 2020-01-15 10:57:44 +0800 | [diff] [blame^] | 328 | void IioDeviceImpl::EnableAllChannels() { |
| 329 | for (IioChannel* chn : GetAllChannels()) { |
| 330 | if (!chn->SetEnabledAndCheck(true)) |
| 331 | LOG(ERROR) << "Failed to enable channel: " << chn->GetId(); |
| 332 | } |
| 333 | } |
| 334 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 335 | bool IioDeviceImpl::CreateBuffer() { |
| 336 | if (buffer_ && |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 337 | iio_device_get_sample_size(device_) == iio_buffer_step(buffer_.get())) |
| 338 | return true; |
| 339 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 340 | buffer_.reset(); |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 341 | buffer_.reset(iio_device_create_buffer(device_, kNumSamples, false)); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 342 | |
| 343 | if (!buffer_) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 344 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 345 | iio_strerror(errno, errMsg, sizeof(errMsg)); |
| 346 | LOG(ERROR) << "Unable to allocate buffer: " << errMsg; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | return true; |
| 351 | } |
| 352 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 353 | } // namespace libmems |