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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 97 | LOG(WARNING) << log_prefix_ << "Attempting to read string attribute " |
| 98 | << name << " failed: " << len; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 109 | LOG(WARNING) << log_prefix_ << "Attempting to read number attribute " |
| 110 | << name << " failed: " << error; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 121 | LOG(WARNING) << log_prefix_ << "Attempting to read double attribute " |
| 122 | << name << " failed: " << error; |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 133 | LOG(WARNING) << log_prefix_ << "Attempting to write string attribute " |
| 134 | << name << " failed: " << error; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 144 | LOG(WARNING) << log_prefix_ << "Attempting to write number attribute " |
| 145 | << name << " failed: " << error; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 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 | 3b87b1d | 2020-12-04 15:38:20 +0800 | [diff] [blame] | 155 | LOG(WARNING) << log_prefix_ << "Attempting to write double attribute " |
| 156 | << name << " failed: " << error; |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 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); |
Harvey Yang | 74a1876 | 2020-12-30 11:09:13 +0800 | [diff] [blame^] | 204 | if (error) |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 205 | return nullptr; |
Gwendal Grignou | 66393e2 | 2019-11-20 16:46:22 -0800 | [diff] [blame] | 206 | |
| 207 | if (trigger == nullptr) |
| 208 | return nullptr; |
| 209 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 210 | const char* id_str = iio_device_get_id(trigger); |
| 211 | auto id = IioDeviceTriggerImpl::GetIdFromString(id_str); |
| 212 | |
| 213 | IioDevice* trigger_device = nullptr; |
| 214 | if (id.has_value()) |
| 215 | trigger_device = GetContext()->GetTriggerById(id.value()); |
| 216 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 217 | if (trigger_device == nullptr) { |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 218 | LOG(WARNING) << log_prefix_ << "Has trigger device " << id_str |
| 219 | << ", which cannot be found in this context"; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 220 | } |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 221 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 222 | return trigger_device; |
| 223 | } |
| 224 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 225 | base::Optional<size_t> IioDeviceImpl::GetSampleSize() const { |
| 226 | ssize_t sample_size = iio_device_get_sample_size(device_); |
| 227 | if (sample_size < 0) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 228 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 229 | iio_strerror(errno, errMsg, sizeof(errMsg)); |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 230 | LOG(WARNING) << log_prefix_ << "Unable to get sample size: " << errMsg; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 231 | return base::nullopt; |
| 232 | } |
| 233 | |
| 234 | return static_cast<size_t>(sample_size); |
| 235 | } |
| 236 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 237 | bool IioDeviceImpl::EnableBuffer(size_t count) { |
| 238 | if (!WriteNumberAttribute("buffer/length", count)) |
| 239 | return false; |
| 240 | if (!WriteNumberAttribute("buffer/enable", 1)) |
| 241 | return false; |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | bool IioDeviceImpl::DisableBuffer() { |
| 247 | return WriteNumberAttribute("buffer/enable", 0); |
| 248 | } |
| 249 | |
| 250 | bool IioDeviceImpl::IsBufferEnabled(size_t* count) const { |
| 251 | bool enabled = (ReadNumberAttribute("buffer/enable").value_or(0) == 1); |
| 252 | if (enabled && count) |
| 253 | *count = ReadNumberAttribute("buffer/length").value_or(0); |
| 254 | |
| 255 | return enabled; |
| 256 | } |
| 257 | |
Harvey Yang | e8b301b | 2020-05-19 14:43:03 +0800 | [diff] [blame] | 258 | base::Optional<int32_t> IioDeviceImpl::GetBufferFd() { |
| 259 | if (!CreateBuffer()) |
| 260 | return base::nullopt; |
| 261 | |
| 262 | int32_t fd = iio_buffer_get_poll_fd(buffer_.get()); |
| 263 | if (fd < 0) { |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 264 | LOG(ERROR) << log_prefix_ << "Failed to get poll fd: " << fd; |
Harvey Yang | e8b301b | 2020-05-19 14:43:03 +0800 | [diff] [blame] | 265 | return base::nullopt; |
| 266 | } |
| 267 | |
| 268 | return fd; |
| 269 | } |
| 270 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 271 | base::Optional<IioDevice::IioSample> IioDeviceImpl::ReadSample() { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 272 | if (!CreateBuffer()) |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 273 | return base::nullopt; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 274 | |
| 275 | ssize_t ret = iio_buffer_refill(buffer_.get()); |
| 276 | if (ret < 0) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 277 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 278 | iio_strerror(-ret, errMsg, sizeof(errMsg)); |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 279 | LOG(ERROR) << log_prefix_ << "Unable to refill buffer: " << errMsg; |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 280 | buffer_.reset(); |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 281 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 282 | return base::nullopt; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | const auto buf_step = iio_buffer_step(buffer_.get()); |
| 286 | size_t sample_size = GetSampleSize().value_or(0); |
| 287 | |
| 288 | // There is something wrong when refilling the buffer. |
| 289 | if (buf_step != sample_size) { |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 290 | LOG(ERROR) << log_prefix_ |
| 291 | << "sample_size doesn't match in refill: " << buf_step |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 292 | << ", sample_size: " << sample_size; |
Harvey Yang | ae53dc8 | 2019-07-25 18:11:20 +0800 | [diff] [blame] | 293 | buffer_.reset(); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 294 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 295 | return base::nullopt; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | uint8_t* start = reinterpret_cast<uint8_t*>(iio_buffer_start(buffer_.get())); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 299 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 300 | return DeserializeSample(start); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 301 | } |
| 302 | |
Harvey Yang | 98ff1b3 | 2020-10-28 14:45:35 +0800 | [diff] [blame] | 303 | base::TimeDelta IioDeviceImpl::GetPeriodForObsoleteSamplesInMilliseconds() { |
| 304 | double min_freq, max_freq; |
| 305 | if (!GetMinMaxFrequency(&min_freq, &max_freq) || max_freq <= 0.0) |
| 306 | return kDefaultPeriodForObsoleteSamples; |
| 307 | |
| 308 | return base::TimeDelta::FromMilliseconds(1.0 / max_freq); |
| 309 | } |
| 310 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 311 | // static |
| 312 | void IioDeviceImpl::IioBufferDeleter(iio_buffer* buffer) { |
| 313 | iio_buffer_cancel(buffer); |
| 314 | iio_buffer_destroy(buffer); |
| 315 | } |
| 316 | |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 317 | bool IioDeviceImpl::CreateBuffer() { |
| 318 | if (buffer_ && |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 319 | iio_device_get_sample_size(device_) == iio_buffer_step(buffer_.get())) |
| 320 | return true; |
| 321 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 322 | buffer_.reset(); |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 323 | buffer_.reset(iio_device_create_buffer(device_, kNumSamples, false)); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 324 | |
| 325 | if (!buffer_) { |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 326 | char errMsg[kErrorBufferSize]; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 327 | iio_strerror(errno, errMsg, sizeof(errMsg)); |
Harvey Yang | 705e40f | 2020-11-19 15:42:44 +0800 | [diff] [blame] | 328 | LOG(ERROR) << log_prefix_ << "Unable to allocate buffer: " << errMsg; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 335 | IioDevice::IioSample IioDeviceImpl::DeserializeSample(const uint8_t* src) { |
Harvey Yang | 9f48d81 | 2020-06-30 16:24:30 +0800 | [diff] [blame] | 336 | IioSample sample; |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 337 | int64_t pos = 0; |
| 338 | |
Harvey Yang | 9f48d81 | 2020-06-30 16:24:30 +0800 | [diff] [blame] | 339 | auto channels = GetAllChannels(); |
| 340 | for (int32_t i = 0; i < channels.size(); ++i) { |
| 341 | IioChannelImpl* chn = dynamic_cast<IioChannelImpl*>(channels[i]); |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 342 | if (!chn->IsEnabled()) |
| 343 | continue; |
| 344 | |
| 345 | size_t len = chn->Length().value_or(0); |
| 346 | if (len == 0) |
| 347 | continue; |
| 348 | len /= CHAR_BIT; |
| 349 | |
| 350 | size_t space_in_block = sizeof(int64_t) - (pos % sizeof(int64_t)); |
| 351 | if (len > space_in_block) { |
| 352 | pos += space_in_block; |
| 353 | } |
| 354 | |
| 355 | base::Optional<int64_t> value = chn->Convert(src + pos); |
| 356 | pos += len; |
| 357 | |
| 358 | if (value.has_value()) |
Harvey Yang | 9f48d81 | 2020-06-30 16:24:30 +0800 | [diff] [blame] | 359 | sample[i] = value.value(); |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Harvey Yang | 9f48d81 | 2020-06-30 16:24:30 +0800 | [diff] [blame] | 362 | return sample; |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 365 | } // namespace libmems |