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->DefineSpace(...);
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 DefineSpace(const DefineSpaceRequest& request,
49                   const DefineSpaceCallback& callback) override;
50  void DestroySpace(const DestroySpaceRequest& request,
51                    const DestroySpaceCallback& callback) override;
52  void WriteSpace(const WriteSpaceRequest& request,
53                  const WriteSpaceCallback& callback) override;
54  void ReadSpace(const ReadSpaceRequest& request,
55                 const ReadSpaceCallback& callback) override;
56  void LockSpace(const LockSpaceRequest& request,
57                 const LockSpaceCallback& callback) override;
58  void ListSpaces(const ListSpacesRequest& request,
59                  const ListSpacesCallback& callback) override;
60  void GetSpaceInfo(const GetSpaceInfoRequest& request,
61                    const GetSpaceInfoCallback& 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