1 //
2 // Copyright (C) 2016 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 TRUNKS_TRUNKS_DBUS_SERVICE_H_
18 #define TRUNKS_TRUNKS_DBUS_SERVICE_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include <base/memory/weak_ptr.h>
24 #include <brillo/daemons/dbus_daemon.h>
25 #include <brillo/dbus/dbus_method_response.h>
26 #include <brillo/dbus/dbus_object.h>
27 
28 #include "trunks/command_transceiver.h"
29 #include "trunks/interface.pb.h"
30 
31 namespace trunks {
32 
33 // TrunksDBusService registers for and handles all incoming D-Bus messages for
34 // the trunksd system daemon.
35 //
36 // Example Usage:
37 //
38 // TrunksDBusService service;
39 // service.set_transceiver(&my_transceiver);
40 // service.Run();
41 class TrunksDBusService : public brillo::DBusServiceDaemon {
42  public:
43   TrunksDBusService();
44   ~TrunksDBusService() override = default;
45 
46   // The |transceiver| will be the target of all incoming TPM commands. This
47   // class does not take ownership of |transceiver|.
set_transceiver(CommandTransceiver * transceiver)48   void set_transceiver(CommandTransceiver* transceiver) {
49     transceiver_ = transceiver;
50   }
51 
52  protected:
53   // Exports D-Bus methods.
54   void RegisterDBusObjectsAsync(
55       brillo::dbus_utils::AsyncEventSequencer* sequencer) override;
56 
57  private:
58   // Handles calls to the 'SendCommand' method.
59   void HandleSendCommand(
60       std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<
61           const SendCommandResponse&>> response_sender,
62       const SendCommandRequest& request);
63 
GetWeakPtr()64   base::WeakPtr<TrunksDBusService> GetWeakPtr() {
65     return weak_factory_.GetWeakPtr();
66   }
67 
68   std::unique_ptr<brillo::dbus_utils::DBusObject> trunks_dbus_object_;
69   CommandTransceiver* transceiver_ = nullptr;
70 
71   // Declared last so weak pointers are invalidated first on destruction.
72   base::WeakPtrFactory<TrunksDBusService> weak_factory_{this};
73   DISALLOW_COPY_AND_ASSIGN(TrunksDBusService);
74 };
75 
76 }  // namespace trunks
77 
78 
79 #endif  // TRUNKS_TRUNKS_DBUS_SERVICE_H_
80