1//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_
18#define TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_
19
20#include "tpm_manager/common/tpm_nvram_interface.h"
21
22#include <string>
23
24#include <base/macros.h>
25#include <base/memory/ref_counted.h>
26#include <dbus/bus.h>
27#include <dbus/object_proxy.h>
28
29#include "tpm_manager/common/export.h"
30
31namespace tpm_manager {
32
33// An implementation of TpmNvramInterface that forwards requests to
34// tpm_managerd over D-Bus.
35// Usage:
36// std::unique_ptr<TpmNvramInterface> tpm_manager = new TpmNvramDBusProxy();
37// tpm_manager->DefineNvram(...);
38class TPM_MANAGER_EXPORT TpmNvramDBusProxy : public TpmNvramInterface {
39 public:
40  TpmNvramDBusProxy() = default;
41  virtual ~TpmNvramDBusProxy();
42
43  // Performs initialization tasks. This method must be called before calling
44  // any other method in this class. Returns true on success.
45  bool Initialize();
46
47  // TpmNvramInterface methods.
48  void DefineNvram(const DefineNvramRequest& request,
49                   const DefineNvramCallback& callback) override;
50  void DestroyNvram(const DestroyNvramRequest& request,
51                    const DestroyNvramCallback& callback) override;
52  void WriteNvram(const WriteNvramRequest& request,
53                  const WriteNvramCallback& callback) override;
54  void ReadNvram(const ReadNvramRequest& request,
55                 const ReadNvramCallback& callback) override;
56  void IsNvramDefined(const IsNvramDefinedRequest& request,
57                      const IsNvramDefinedCallback& callback) override;
58  void IsNvramLocked(const IsNvramLockedRequest& request,
59                     const IsNvramLockedCallback& callback) override;
60  void GetNvramSize(const GetNvramSizeRequest& request,
61                    const GetNvramSizeCallback& callback) override;
62
63  void set_object_proxy(dbus::ObjectProxy* object_proxy) {
64    object_proxy_ = object_proxy;
65  }
66
67 private:
68  // Template method to call a given |method_name| remotely via dbus.
69  template<typename ReplyProtobufType,
70           typename RequestProtobufType,
71           typename CallbackType>
72  void CallMethod(const std::string& method_name,
73                  const RequestProtobufType& request,
74                  const CallbackType& callback);
75
76  scoped_refptr<dbus::Bus> bus_;
77  dbus::ObjectProxy* object_proxy_;
78  DISALLOW_COPY_AND_ASSIGN(TpmNvramDBusProxy);
79};
80
81}  // namespace tpm_manager
82
83#endif  // TPM_MANAGER_CLIENT_TPM_NVRAM_DBUS_PROXY_H_
84