blob: 265e9ecd365ae6fd181381beeaab9bb0884dcece [file] [log] [blame]
Utkarsh Sanghic2be4262014-08-18 14:15:13 -07001// 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 Deymod5814952014-11-10 19:55:35 -08005#include "trunks/trunks_service.h"
6
Utkarsh Sanghic2be4262014-08-18 14:15:13 -07007#include <base/bind.h>
Jocelyn Bohr2e77a422015-06-04 18:24:17 -07008#include <chromeos/bind_lambda.h>
Utkarsh Sanghic2be4262014-08-18 14:15:13 -07009
10#include "trunks/dbus_interface.h"
Darren Krahn80c739e2014-12-22 17:05:24 -080011#include "trunks/dbus_interface.pb.h"
Utkarsh Sanghic2be4262014-08-18 14:15:13 -070012#include "trunks/error_codes.h"
Utkarsh Sanghic2be4262014-08-18 14:15:13 -070013
14namespace trunks {
15
Jocelyn Bohr2e77a422015-06-04 18:24:17 -070016using chromeos::dbus_utils::DBusMethodResponse;
17
18TrunksService::TrunksService(const scoped_refptr<dbus::Bus>& bus,
19 CommandTransceiver* transceiver)
20 : trunks_dbus_object_(nullptr, bus, dbus::ObjectPath(kTrunksServicePath)),
Darren Krahn80c739e2014-12-22 17:05:24 -080021 transceiver_(transceiver),
22 weak_factory_(this) {}
Utkarsh Sanghic2be4262014-08-18 14:15:13 -070023
Jocelyn Bohr2e77a422015-06-04 18:24:17 -070024void 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 Sanghic2be4262014-08-18 14:15:13 -070031}
32
Darren Krahn80c739e2014-12-22 17:05:24 -080033void TrunksService::HandleSendCommand(
Jocelyn Bohr2e77a422015-06-04 18:24:17 -070034 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 Krahn80c739e2014-12-22 17:05:24 -080049 LOG(ERROR) << "TrunksService: Invalid request.";
Jocelyn Bohr2e77a422015-06-04 18:24:17 -070050 callback(SharedResponsePointer(std::move(response_sender)),
51 CreateErrorResponse(SAPI_RC_BAD_PARAMETER));
Utkarsh Sanghiac7e5e22014-08-25 15:32:58 -070052 return;
53 }
Jocelyn Bohr2e77a422015-06-04 18:24:17 -070054 transceiver_->SendCommand(
55 request.command(),
56 base::Bind(callback, SharedResponsePointer(std::move(response_sender))));
Utkarsh Sanghic2be4262014-08-18 14:15:13 -070057}
58
59} // namespace trunks