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