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_SERVER_DBUS_SERVICE_H_
18#define TPM_MANAGER_SERVER_DBUS_SERVICE_H_
19
20#include <memory>
21
22#include <brillo/daemons/dbus_daemon.h>
23#include <brillo/dbus/dbus_method_response.h>
24#include <brillo/dbus/dbus_object.h>
25#include <dbus/bus.h>
26
27#include "tpm_manager/common/tpm_nvram_interface.h"
28#include "tpm_manager/common/tpm_ownership_interface.h"
29
30namespace tpm_manager {
31
32using brillo::dbus_utils::DBusMethodResponse;
33
34// Handles D-Bus communication with the TpmManager daemon.
35class DBusService : public brillo::DBusServiceDaemon {
36 public:
37  // Does not take ownership of |nvram_service| or |ownership_service|. The
38  // services provided must be initialized, and must remain valid for the
39  // lifetime of this instance.
40  DBusService(TpmNvramInterface* nvram_service,
41              TpmOwnershipInterface* ownership_service);
42  // Used to inject a mock bus.
43  DBusService(scoped_refptr<dbus::Bus> bus,
44              TpmNvramInterface* nvram_service,
45              TpmOwnershipInterface* ownership_service);
46  virtual ~DBusService() = default;
47
48  // Registers objects exported by this service.
49  void RegisterDBusObjectsAsync(
50      brillo::dbus_utils::AsyncEventSequencer* sequencer) override;
51
52 private:
53  friend class DBusServiceTest;
54
55  template <typename RequestProtobufType,
56            typename ReplyProtobufType,
57            typename TpmInterface>
58  using HandlerFunction = void (TpmInterface::*)(
59      const RequestProtobufType&,
60      const base::Callback<void(const ReplyProtobufType&)>&);
61
62  // Templates to handle D-Bus calls.
63  template <typename RequestProtobufType,
64            typename ReplyProtobufType,
65            DBusService::HandlerFunction<RequestProtobufType,
66                                         ReplyProtobufType,
67                                         TpmNvramInterface> func>
68  void HandleNvramDBusMethod(
69      std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response,
70      const RequestProtobufType& request);
71
72  template <typename RequestProtobufType,
73            typename ReplyProtobufType,
74            DBusService::HandlerFunction<RequestProtobufType,
75                                         ReplyProtobufType,
76                                         TpmOwnershipInterface> func>
77  void HandleOwnershipDBusMethod(
78      std::unique_ptr<DBusMethodResponse<const ReplyProtobufType&>> response,
79      const RequestProtobufType& request);
80
81  std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
82  TpmNvramInterface* nvram_service_;
83  TpmOwnershipInterface* ownership_service_;
84  DISALLOW_COPY_AND_ASSIGN(DBusService);
85};
86
87}  // namespace tpm_manager
88
89#endif  // TPM_MANAGER_SERVER_DBUS_SERVICE_H_
90