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