Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | d581495 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "trunks/trunks_service.h" |
| 6 | |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 8 | #include <chromeos/bind_lambda.h> |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 9 | |
| 10 | #include "trunks/dbus_interface.h" |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 11 | #include "trunks/dbus_interface.pb.h" |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 12 | #include "trunks/error_codes.h" |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 13 | |
| 14 | namespace trunks { |
| 15 | |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 16 | using chromeos::dbus_utils::DBusMethodResponse; |
| 17 | |
| 18 | TrunksService::TrunksService(const scoped_refptr<dbus::Bus>& bus, |
| 19 | CommandTransceiver* transceiver) |
| 20 | : trunks_dbus_object_(nullptr, bus, dbus::ObjectPath(kTrunksServicePath)), |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 21 | transceiver_(transceiver), |
| 22 | weak_factory_(this) {} |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 23 | |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 24 | void TrunksService::Register(const CompletionAction& callback) { |
| 25 | chromeos::dbus_utils::DBusInterface* dbus_interface = |
| 26 | trunks_dbus_object_.AddOrGetInterface(kTrunksInterface); |
| 27 | dbus_interface->AddMethodHandler(kSendCommand, |
| 28 | base::Unretained(this), |
| 29 | &TrunksService::HandleSendCommand); |
| 30 | trunks_dbus_object_.RegisterAsync(callback); |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 33 | void TrunksService::HandleSendCommand( |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 34 | std::unique_ptr<DBusMethodResponse< |
| 35 | const SendCommandResponse&>> response_sender, |
| 36 | const SendCommandRequest& request) { |
| 37 | // Convert |response_sender| to a shared_ptr so |transceiver_| can safely |
| 38 | // copy the callback. |
| 39 | using SharedResponsePointer = std::shared_ptr< |
| 40 | DBusMethodResponse<const SendCommandResponse&>>; |
| 41 | // A callback that constructs the response protobuf and sends it. |
| 42 | auto callback = [](const SharedResponsePointer& response, |
| 43 | const std::string& response_from_tpm) { |
| 44 | SendCommandResponse tpm_response_proto; |
| 45 | tpm_response_proto.set_response(response_from_tpm); |
| 46 | response->Return(tpm_response_proto); |
| 47 | }; |
| 48 | if (!request.has_command() || request.command().empty()) { |
Darren Krahn | 80c739e | 2014-12-22 17:05:24 -0800 | [diff] [blame] | 49 | LOG(ERROR) << "TrunksService: Invalid request."; |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 50 | callback(SharedResponsePointer(std::move(response_sender)), |
| 51 | CreateErrorResponse(SAPI_RC_BAD_PARAMETER)); |
Utkarsh Sanghi | ac7e5e2 | 2014-08-25 15:32:58 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
Jocelyn Bohr | 2e77a42 | 2015-06-04 18:24:17 -0700 | [diff] [blame] | 54 | transceiver_->SendCommand( |
| 55 | request.command(), |
| 56 | base::Bind(callback, SharedResponsePointer(std::move(response_sender)))); |
Utkarsh Sanghi | c2be426 | 2014-08-18 14:15:13 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace trunks |