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