1c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//
2c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// Copyright (C) 2015 The Android Open Source Project
3c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//
4c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// Licensed under the Apache License, Version 2.0 (the "License");
5c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// you may not use this file except in compliance with the License.
6c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// You may obtain a copy of the License at
7c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//
8c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//      http://www.apache.org/licenses/LICENSE-2.0
9c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//
10c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// Unless required by applicable law or agreed to in writing, software
11c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// distributed under the License is distributed on an "AS IS" BASIS,
12c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// See the License for the specific language governing permissions and
14c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// limitations under the License.
15c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi//
16c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
17c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi#include "tpm_manager/client/tpm_ownership_dbus_proxy.h"
18c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
19c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi#include <brillo/bind_lambda.h>
20c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi#include <brillo/dbus/dbus_method_invoker.h>
21c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
22c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi#include "tpm_manager/common/tpm_manager_constants.h"
23c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi#include "tpm_manager/common/tpm_ownership_dbus_interface.h"
24c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
25c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghinamespace {
26c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
27c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi// Use a two minute timeout because TPM operations can take a long time.
28c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghiconst int kDBusTimeoutMS = 2 * 60 * 1000;
29c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
30c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}  // namespace
31c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
32c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghinamespace tpm_manager {
33c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
34c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh SanghiTpmOwnershipDBusProxy::~TpmOwnershipDBusProxy() {
35c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  if (bus_) {
36c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    bus_->ShutdownAndBlock();
37c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  }
38c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}
39c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
40c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghibool TpmOwnershipDBusProxy::Initialize() {
41c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  dbus::Bus::Options options;
42c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  options.bus_type = dbus::Bus::SYSTEM;
43c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  bus_ = new dbus::Bus(options);
44c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  object_proxy_ = bus_->GetObjectProxy(
45c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi      tpm_manager::kTpmManagerServiceName,
46c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi      dbus::ObjectPath(tpm_manager::kTpmManagerServicePath));
47c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  return (object_proxy_ != nullptr);
48c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}
49c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
504dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahnvoid TpmOwnershipDBusProxy::GetTpmStatus(const GetTpmStatusRequest& request,
514dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn                                         const GetTpmStatusCallback& callback) {
52c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  CallMethod<GetTpmStatusReply>(tpm_manager::kGetTpmStatus, request, callback);
53c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}
54c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
55c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghivoid TpmOwnershipDBusProxy::TakeOwnership(
56c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    const TakeOwnershipRequest& request,
57c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    const TakeOwnershipCallback& callback) {
584dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn  CallMethod<TakeOwnershipReply>(tpm_manager::kTakeOwnership, request,
594dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn                                 callback);
60c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}
61c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
62bbdb1b1f93626766baf7e293c7a9e532933531c3Utkarsh Sanghivoid TpmOwnershipDBusProxy::RemoveOwnerDependency(
63bbdb1b1f93626766baf7e293c7a9e532933531c3Utkarsh Sanghi    const RemoveOwnerDependencyRequest& request,
64bbdb1b1f93626766baf7e293c7a9e532933531c3Utkarsh Sanghi    const RemoveOwnerDependencyCallback& callback) {
654dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn  CallMethod<RemoveOwnerDependencyReply>(tpm_manager::kRemoveOwnerDependency,
664dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn                                         request, callback);
67bbdb1b1f93626766baf7e293c7a9e532933531c3Utkarsh Sanghi}
68bbdb1b1f93626766baf7e293c7a9e532933531c3Utkarsh Sanghi
694dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahntemplate <typename ReplyProtobufType,
704dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn          typename RequestProtobufType,
714dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn          typename CallbackType>
72c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghivoid TpmOwnershipDBusProxy::CallMethod(const std::string& method_name,
73c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi                                       const RequestProtobufType& request,
74c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi                                       const CallbackType& callback) {
75c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  auto on_error = [callback](brillo::Error* error) {
76c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    ReplyProtobufType reply;
77c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    reply.set_status(STATUS_NOT_AVAILABLE);
78c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi    callback.Run(reply);
79c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  };
80c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi  brillo::dbus_utils::CallMethodWithTimeout(
814dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn      kDBusTimeoutMS, object_proxy_, tpm_manager::kTpmOwnershipInterface,
824dc4629c415e7ca90ff146d7bb75b5646ecd8b17Darren Krahn      method_name, callback, base::Bind(on_error), request);
83c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}
84c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi
85c9429017d51d78d516ac40531ce83fe35c48524cUtkarsh Sanghi}  // namespace tpm_manager
86