blob: d564968890e4963ac2db154245a86138d4ee1e69 [file] [log] [blame]
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef HPS_DAEMON_DBUS_ADAPTOR_H_
#define HPS_DAEMON_DBUS_ADAPTOR_H_
#include <array>
#include <memory>
#include <base/sequence_checker.h>
#include <base/timer/timer.h>
#include <hps/hps.h>
#include <hps/daemon/filters/filter.h>
#include <hps/daemon/filters/status_callback.h>
#include <hps/proto_bindings/hps_service.pb.h>
#include <dbus_adaptors/org.chromium.Hps.h>
namespace hps {
class DBusAdaptor : public org::chromium::HpsAdaptor,
public org::chromium::HpsInterface {
public:
DBusAdaptor(scoped_refptr<dbus::Bus> bus,
std::unique_ptr<HPS>,
uint32_t poll_time_ms);
DBusAdaptor(const DBusAdaptor&) = delete;
DBusAdaptor& operator=(const DBusAdaptor&) = delete;
void RegisterAsync(
const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
// Timer Callback used to poll hps hardware and debounce results.
void PollTask();
// Methods for HpsInterface
bool EnableHpsSense(brillo::ErrorPtr* error,
const hps::FeatureConfig& config) override;
bool DisableHpsSense(brillo::ErrorPtr* error) override;
bool GetResultHpsSense(brillo::ErrorPtr* error,
HpsResultProto* result) override;
bool EnableHpsNotify(brillo::ErrorPtr* error,
const hps::FeatureConfig& config) override;
bool DisableHpsNotify(brillo::ErrorPtr* error) override;
bool GetResultHpsNotify(brillo::ErrorPtr* error,
HpsResultProto* result) override;
private:
void BootIfNeeded();
void ShutDown();
bool CommitState();
bool EnableFeature(brillo::ErrorPtr* error,
const hps::FeatureConfig& config,
uint8_t feature,
StatusCallback callback);
bool DisableFeature(brillo::ErrorPtr* error, uint8_t feature);
bool GetFeatureResult(brillo::ErrorPtr* error,
HpsResultProto* result,
uint8_t feature);
class FeatureState {
public:
void Enable(const FeatureConfig&, StatusCallback);
void Disable();
void DidCommit();
void DidShutDown();
bool enabled() const { return enabled_; }
bool enabled_in_hps() const { return enabled_in_hps_; }
bool needs_commit() const { return enabled_ != enabled_in_hps_; }
Filter* filter() const {
DCHECK(enabled_);
return filter_.get();
}
private:
bool enabled_ = false; // Whether the user wants the feature on or off.
bool enabled_in_hps_ = false; // Whether the feature is on or off in HPS.
FeatureConfig config_;
std::unique_ptr<Filter> filter_;
StatusCallback callback_;
};
brillo::dbus_utils::DBusObject dbus_object_;
std::unique_ptr<HPS> hps_;
bool hps_booted_ = true;
const uint32_t poll_time_ms_;
base::RepeatingTimer poll_timer_;
std::array<FeatureState, kFeatures> features_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace hps
#endif // HPS_DAEMON_DBUS_ADAPTOR_H_