blob: 0126f3ac51a548380e1ed3f2857f95cd8268588d [file] [log] [blame]
Enrico Granata60a818d2019-05-09 09:56:09 -07001// 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 Granata51cdb942019-06-18 16:40:17 -07005#ifndef LIBMEMS_IIO_DEVICE_IMPL_H_
6#define LIBMEMS_IIO_DEVICE_IMPL_H_
Enrico Granata60a818d2019-05-09 09:56:09 -07007
8#include <iio.h>
9
10#include <map>
11#include <memory>
12#include <string>
Harvey Yangc0d19d82019-07-01 12:17:34 +080013#include <vector>
Enrico Granata60a818d2019-05-09 09:56:09 -070014
15#include <base/optional.h>
16
Enrico Granata51cdb942019-06-18 16:40:17 -070017#include "libmems/export.h"
18#include "libmems/iio_device.h"
Enrico Granata60a818d2019-05-09 09:56:09 -070019
Enrico Granata51cdb942019-06-18 16:40:17 -070020namespace libmems {
Enrico Granata60a818d2019-05-09 09:56:09 -070021
22class IioChannelImpl;
23class IioContext;
24class IioContextImpl;
25
Enrico Granata51cdb942019-06-18 16:40:17 -070026class LIBMEMS_EXPORT IioDeviceImpl : public IioDevice {
Enrico Granata60a818d2019-05-09 09:56:09 -070027 public:
28 // iio_device objects are kept alive by the IioContextImpl.
29 IioDeviceImpl(IioContextImpl* ctx, iio_device* dev);
30 ~IioDeviceImpl() override = default;
31
32 IioContext* GetContext() const override;
33
34 const char* GetName() const override;
35 const char* GetId() const override;
36
37 base::FilePath GetPath() const override;
38
39 base::Optional<std::string> ReadStringAttribute(
40 const std::string& name) const override;
41 base::Optional<int64_t> ReadNumberAttribute(
42 const std::string& name) const override;
43
44 bool WriteStringAttribute(const std::string& name,
45 const std::string& value) override;
46 bool WriteNumberAttribute(const std::string& name, int64_t value) override;
47
48 iio_device* GetUnderlyingIioDevice() const override;
49
50 bool SetTrigger(IioDevice* trigger_device) override;
51 IioDevice* GetTrigger() override;
52
53 IioChannel* GetChannel(const std::string& name) override;
54
Harvey Yangc0d19d82019-07-01 12:17:34 +080055 base::Optional<size_t> GetSampleSize() const override;
56
Enrico Granata60a818d2019-05-09 09:56:09 -070057 bool EnableBuffer(size_t num) override;
58 bool DisableBuffer() override;
59 bool IsBufferEnabled(size_t* num = nullptr) const override;
60
Harvey Yangc0d19d82019-07-01 12:17:34 +080061 bool ReadEvents(uint32_t num_samples, std::vector<uint8_t>* events) override;
62
Enrico Granata60a818d2019-05-09 09:56:09 -070063 private:
Harvey Yangc0d19d82019-07-01 12:17:34 +080064 static void IioBufferDeleter(iio_buffer* buffer);
65
66 bool CreateBuffer(uint32_t num_samples);
67
Enrico Granata60a818d2019-05-09 09:56:09 -070068 IioContextImpl* context_; // non-owned
69 iio_device* const device_; // non-owned
Harvey Yangc0d19d82019-07-01 12:17:34 +080070
71 using ScopedBuffer = std::unique_ptr<iio_buffer, decltype(&IioBufferDeleter)>;
72 ScopedBuffer buffer_;
73
74 uint32_t buffer_size_;
75
Enrico Granata60a818d2019-05-09 09:56:09 -070076 std::map<std::string, std::unique_ptr<IioChannelImpl>> channels_;
77 DISALLOW_COPY_AND_ASSIGN(IioDeviceImpl);
78};
79
Enrico Granata51cdb942019-06-18 16:40:17 -070080} // namespace libmems
Enrico Granata60a818d2019-05-09 09:56:09 -070081
Enrico Granata51cdb942019-06-18 16:40:17 -070082#endif // LIBMEMS_IIO_DEVICE_IMPL_H_