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