• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2014 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 #include "trunks/trunks_dbus_service.h"
18 
19 #include <base/bind.h>
20 #include <brillo/bind_lambda.h>
21 
22 #include "trunks/dbus_interface.h"
23 #include "trunks/error_codes.h"
24 #include "trunks/interface.pb.h"
25 
26 namespace trunks {
27 
28 using brillo::dbus_utils::AsyncEventSequencer;
29 using brillo::dbus_utils::DBusMethodResponse;
30 
TrunksDBusService()31 TrunksDBusService::TrunksDBusService()
32     : brillo::DBusServiceDaemon(trunks::kTrunksServiceName) {}
33 
RegisterDBusObjectsAsync(AsyncEventSequencer * sequencer)34 void TrunksDBusService::RegisterDBusObjectsAsync(
35     AsyncEventSequencer* sequencer) {
36   trunks_dbus_object_.reset(new brillo::dbus_utils::DBusObject(
37       nullptr, bus_, dbus::ObjectPath(kTrunksServicePath)));
38   brillo::dbus_utils::DBusInterface* dbus_interface =
39       trunks_dbus_object_->AddOrGetInterface(kTrunksInterface);
40   dbus_interface->AddMethodHandler(kSendCommand, base::Unretained(this),
41                                    &TrunksDBusService::HandleSendCommand);
42   trunks_dbus_object_->RegisterAsync(
43       sequencer->GetHandler("Failed to register D-Bus object.", true));
44 }
45 
HandleSendCommand(std::unique_ptr<DBusMethodResponse<const SendCommandResponse &>> response_sender,const SendCommandRequest & request)46 void TrunksDBusService::HandleSendCommand(
47     std::unique_ptr<DBusMethodResponse<const SendCommandResponse&>>
48         response_sender,
49     const SendCommandRequest& request) {
50   // Convert |response_sender| to a shared_ptr so |transceiver_| can safely
51   // copy the callback.
52   using SharedResponsePointer =
53       std::shared_ptr<DBusMethodResponse<const SendCommandResponse&>>;
54   // A callback that constructs the response protobuf and sends it.
55   auto callback = [](const SharedResponsePointer& response,
56                      const std::string& response_from_tpm) {
57     SendCommandResponse tpm_response_proto;
58     tpm_response_proto.set_response(response_from_tpm);
59     response->Return(tpm_response_proto);
60   };
61   if (!request.has_command() || request.command().empty()) {
62     LOG(ERROR) << "TrunksDBusService: Invalid request.";
63     callback(SharedResponsePointer(std::move(response_sender)),
64              CreateErrorResponse(SAPI_RC_BAD_PARAMETER));
65     return;
66   }
67   transceiver_->SendCommand(
68       request.command(),
69       base::Bind(callback, SharedResponsePointer(std::move(response_sender))));
70 }
71 
72 }  // namespace trunks
73