File: fake_sensor_device.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,353 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_
#define CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_

#include <map>
#include <optional>
#include <string>
#include <vector>

#include "base/component_export.h"
#include "base/sequence_checker.h"
#include "chromeos/components/sensors/mojom/sensor.mojom.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace chromeos {
namespace sensors {

class FakeSensorDevice final : public mojom::SensorDevice {
 public:
  struct ChannelData {
    ChannelData();
    ChannelData(const ChannelData&);
    ChannelData& operator=(const ChannelData&);
    ~ChannelData();

    std::string id;
    std::map<std::string, std::string> attrs;
    int64_t sample_data;
  };

  explicit FakeSensorDevice(const std::vector<ChannelData>& channels);
  FakeSensorDevice(const FakeSensorDevice&) = delete;
  FakeSensorDevice& operator=(const FakeSensorDevice&) = delete;
  ~FakeSensorDevice() override;

  mojo::ReceiverId AddReceiver(
      mojo::PendingReceiver<mojom::SensorDevice> pending_receiver);
  void RemoveReceiver(mojo::ReceiverId id);
  void RemoveReceiverWithReason(mojo::ReceiverId id,
                                mojom::SensorDeviceDisconnectReason reason,
                                const std::string& description);

  void ClearReceivers();
  void ClearReceiversWithReason(mojom::SensorDeviceDisconnectReason reason,
                                const std::string& description);
  bool HasReceivers() const;
  size_t SizeOfReceivers() const;

  void SetAttribute(const std::string& attr_name,
                    const std::string& attr_value);

  void ResetObserverRemote(mojo::ReceiverId id);
  void ResetObserverRemoteWithReason(mojo::ReceiverId id,
                                     mojom::SensorDeviceDisconnectReason reason,
                                     const std::string& description);

  // Unlike SetChannelsEnabled() below, SetChannelsEnabledWithId() is used
  // without a mojo pipe, instead, with the mojo::ReceiverId from AddReceiver().
  // It lets the tests manually enable or disable channels to simulate some
  // unexpected behaviors of iioservice, such as channels being unavailable
  // suddenly.
  void SetChannelsEnabledWithId(mojo::ReceiverId id,
                                const std::vector<int32_t>& iio_chn_indices,
                                bool en);

  // Implementation of mojom::SensorDevice.
  void SetTimeout(uint32_t timeout) override {}
  void GetAttributes(const std::vector<std::string>& attr_names,
                     GetAttributesCallback callback) override;
  void SetFrequency(double frequency, SetFrequencyCallback callback) override;
  void StartReadingSamples(
      mojo::PendingRemote<mojom::SensorDeviceSamplesObserver> observer)
      override;
  void StopReadingSamples() override;
  void GetAllChannelIds(GetAllChannelIdsCallback callback) override;
  void SetChannelsEnabled(const std::vector<int32_t>& iio_chn_indices,
                          bool en,
                          SetChannelsEnabledCallback callback) override;
  void GetChannelsEnabled(const std::vector<int32_t>& iio_chn_indices,
                          GetChannelsEnabledCallback callback) override;
  void GetChannelsAttributes(const std::vector<int32_t>& iio_chn_indices,
                             const std::string& attr_name,
                             GetChannelsAttributesCallback callback) override;

 private:
  struct ClientData {
    ClientData();
    ~ClientData();

    std::optional<double> frequency;
    std::vector<bool> channels_enabled;
    mojo::Remote<mojom::SensorDeviceSamplesObserver> observer;
  };

  void SendSampleIfReady(ClientData& client);

  std::map<std::string, std::string> attributes_;
  const std::vector<ChannelData> channels_;

  mojo::ReceiverSet<mojom::SensorDevice> receiver_set_;

  // First is the client's id from |receiver_set_|, second is the client's
  // states and observer remote.
  std::map<mojo::ReceiverId, ClientData> clients_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace sensors
}  // namespace chromeos

#endif  // CHROMEOS_COMPONENTS_SENSORS_FAKE_SENSOR_DEVICE_H_