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 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 5 | #ifndef LIBMEMS_IIO_DEVICE_IMPL_H_ |
| 6 | #define LIBMEMS_IIO_DEVICE_IMPL_H_ |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 7 | |
| 8 | #include <iio.h> |
| 9 | |
| 10 | #include <map> |
| 11 | #include <memory> |
| 12 | #include <string> |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 13 | #include <vector> |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/optional.h> |
| 16 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 17 | #include "libmems/export.h" |
| 18 | #include "libmems/iio_device.h" |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 19 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 20 | namespace libmems { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 21 | |
| 22 | class IioChannelImpl; |
| 23 | class IioContext; |
| 24 | class IioContextImpl; |
| 25 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 26 | class LIBMEMS_EXPORT IioDeviceImpl : public IioDevice { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 27 | public: |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 28 | static base::Optional<int> GetIdFromString(const char* id_str); |
| 29 | static std::string GetStringFromId(int id); |
| 30 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 31 | // iio_device objects are kept alive by the IioContextImpl. |
| 32 | IioDeviceImpl(IioContextImpl* ctx, iio_device* dev); |
| 33 | ~IioDeviceImpl() override = default; |
| 34 | |
| 35 | IioContext* GetContext() const override; |
| 36 | |
| 37 | const char* GetName() const override; |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 38 | int GetId() const override; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 39 | |
| 40 | base::FilePath GetPath() const override; |
| 41 | |
| 42 | base::Optional<std::string> ReadStringAttribute( |
| 43 | const std::string& name) const override; |
| 44 | base::Optional<int64_t> ReadNumberAttribute( |
| 45 | const std::string& name) const override; |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 46 | base::Optional<double> ReadDoubleAttribute( |
| 47 | const std::string& name) const override; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 48 | |
| 49 | bool WriteStringAttribute(const std::string& name, |
| 50 | const std::string& value) override; |
| 51 | bool WriteNumberAttribute(const std::string& name, int64_t value) override; |
Enrico Granata | d2e57f4 | 2019-07-31 10:46:03 -0700 | [diff] [blame] | 52 | bool WriteDoubleAttribute(const std::string& name, double value) override; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 53 | |
| 54 | iio_device* GetUnderlyingIioDevice() const override; |
| 55 | |
| 56 | bool SetTrigger(IioDevice* trigger_device) override; |
| 57 | IioDevice* GetTrigger() override; |
| 58 | |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 59 | base::Optional<size_t> GetSampleSize() const override; |
| 60 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 61 | bool EnableBuffer(size_t num) override; |
| 62 | bool DisableBuffer() override; |
| 63 | bool IsBufferEnabled(size_t* num = nullptr) const override; |
| 64 | |
Harvey Yang | e8b301b | 2020-05-19 14:43:03 +0800 | [diff] [blame] | 65 | base::Optional<int32_t> GetBufferFd() override; |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 66 | base::Optional<IioSample> ReadSample() override; |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 67 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 68 | private: |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 69 | static void IioBufferDeleter(iio_buffer* buffer); |
| 70 | |
Harvey Yang | 5e6cb70 | 2020-01-15 10:57:44 +0800 | [diff] [blame] | 71 | void EnableAllChannels(); |
Harvey Yang | 75452e8 | 2019-10-02 16:57:01 +0800 | [diff] [blame] | 72 | bool CreateBuffer(); |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 73 | |
Harvey Yang | 34c54d5 | 2020-05-20 13:34:20 +0800 | [diff] [blame] | 74 | IioSample DeserializeSample(const uint8_t* src); |
| 75 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 76 | IioContextImpl* context_; // non-owned |
| 77 | iio_device* const device_; // non-owned |
Harvey Yang | c0d19d8 | 2019-07-01 12:17:34 +0800 | [diff] [blame] | 78 | |
| 79 | using ScopedBuffer = std::unique_ptr<iio_buffer, decltype(&IioBufferDeleter)>; |
| 80 | ScopedBuffer buffer_; |
| 81 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(IioDeviceImpl); |
| 83 | }; |
| 84 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 85 | } // namespace libmems |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 86 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 87 | #endif // LIBMEMS_IIO_DEVICE_IMPL_H_ |