File: cec_service_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 5,106 bytes parent folder | download | duplicates (7)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/cec_service/cec_service_client.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "chromeos/ash/components/dbus/cec_service/fake_cec_service_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

namespace {

CecServiceClient* g_instance = nullptr;

// Translates a power state from a D-Bus response to the types exposed by
// CecServiceClient.
CecServiceClient::PowerState ConvertDBusPowerState(int32_t power_state) {
  switch (power_state) {
    case cecservice::kTvPowerStatusError:
      return CecServiceClient::PowerState::kError;
    case cecservice::kTvPowerStatusAdapterNotConfigured:
      return CecServiceClient::PowerState::kAdapterNotConfigured;
    case cecservice::kTvPowerStatusNoTv:
      return CecServiceClient::PowerState::kNoDevice;
    case cecservice::kTvPowerStatusOn:
      return CecServiceClient::PowerState::kOn;
    case cecservice::kTvPowerStatusStandBy:
      return CecServiceClient::PowerState::kStandBy;
    case cecservice::kTvPowerStatusToOn:
      return CecServiceClient::PowerState::kTransitioningToOn;
    case cecservice::kTvPowerStatusToStandBy:
      return CecServiceClient::PowerState::kTransitioningToStandBy;
    case cecservice::kTvPowerStatusUnknown:
      return CecServiceClient::PowerState::kUnknown;
    default:
      NOTREACHED() << "Received unknown state " << power_state;
  }
}

void OnGetTvsPowerStatus(CecServiceClient::PowerStateCallback callback,
                         dbus::Response* response) {
  if (!response) {
    LOG(ERROR) << "QueryDisplayCecPowerState call failed, no response";
    std::move(callback).Run({});
    return;
  }

  dbus::MessageReader reader(response);

  dbus::MessageReader array_reader(nullptr);
  if (!reader.PopArray(&array_reader)) {
    LOG(ERROR) << "Unable to read back array for QueryDisplayCecPowerState";
    std::move(callback).Run({});
    return;
  }

  std::vector<CecServiceClient::PowerState> result;
  while (array_reader.HasMoreData()) {
    int32_t low_level_power_state = -1;
    if (!array_reader.PopInt32(&low_level_power_state)) {
      LOG(ERROR) << "Unable to pop state for QueryDisplayCecPowerState";
      std::move(callback).Run({});
      return;
    }

    result.push_back(ConvertDBusPowerState(low_level_power_state));
  }

  std::move(callback).Run(result);
}

// Real implementation of CecServiceClient.
class CecServiceClientImpl : public CecServiceClient {
 public:
  CecServiceClientImpl() = default;

  CecServiceClientImpl(const CecServiceClientImpl&) = delete;
  CecServiceClientImpl& operator=(const CecServiceClientImpl&) = delete;

  ~CecServiceClientImpl() override = default;

  void SendStandBy() override {
    dbus::MethodCall method_call(cecservice::kCecServiceInterface,
                                 cecservice::kSendStandByToAllDevicesMethod);
    cec_service_proxy_->CallMethod(&method_call,
                                   dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                                   base::DoNothing());
  }

  void SendWakeUp() override {
    dbus::MethodCall method_call(cecservice::kCecServiceInterface,
                                 cecservice::kSendWakeUpToAllDevicesMethod);
    cec_service_proxy_->CallMethod(&method_call,
                                   dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                                   base::DoNothing());
  }

  void QueryDisplayCecPowerState(PowerStateCallback callback) override {
    dbus::MethodCall query_method(cecservice::kCecServiceInterface,
                                  cecservice::kGetTvsPowerStatus);
    cec_service_proxy_->CallMethod(
        &query_method, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::BindOnce(&OnGetTvsPowerStatus, std::move(callback)));
  }

  void Init(dbus::Bus* bus) override {
    cec_service_proxy_ =
        bus->GetObjectProxy(cecservice::kCecServiceName,
                            dbus::ObjectPath(cecservice::kCecServicePath));
  }

 private:
  scoped_refptr<dbus::ObjectProxy> cec_service_proxy_;
};

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// CecServiceClient

// static
CecServiceClient* CecServiceClient::Get() {
  return g_instance;
}

// static
void CecServiceClient::Initialize(dbus::Bus* bus) {
  CHECK(bus);
  (new CecServiceClientImpl())->Init(bus);
}

// static
void CecServiceClient::InitializeFake() {
  (new FakeCecServiceClient())->Init(nullptr);
}

// static
void CecServiceClient::Shutdown() {
  CHECK(g_instance);
  delete g_instance;
}

CecServiceClient::CecServiceClient() {
  CHECK(!g_instance);
  g_instance = this;
}

CecServiceClient::~CecServiceClient() {
  CHECK_EQ(g_instance, this);
  g_instance = nullptr;
}

}  // namespace ash