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