Home | History | Annotate | Download | only in client
      1 //
      2 // Copyright (C) 2015 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 "tpm_manager/client/tpm_ownership_dbus_proxy.h"
     18 
     19 #include <brillo/bind_lambda.h>
     20 #include <brillo/dbus/dbus_method_invoker.h>
     21 
     22 #include "tpm_manager/common/tpm_manager_constants.h"
     23 #include "tpm_manager/common/tpm_ownership_dbus_interface.h"
     24 
     25 namespace {
     26 
     27 // Use a two minute timeout because TPM operations can take a long time.
     28 const int kDBusTimeoutMS = 2 * 60 * 1000;
     29 
     30 }  // namespace
     31 
     32 namespace tpm_manager {
     33 
     34 TpmOwnershipDBusProxy::~TpmOwnershipDBusProxy() {
     35   if (bus_) {
     36     bus_->ShutdownAndBlock();
     37   }
     38 }
     39 
     40 bool TpmOwnershipDBusProxy::Initialize() {
     41   dbus::Bus::Options options;
     42   options.bus_type = dbus::Bus::SYSTEM;
     43   bus_ = new dbus::Bus(options);
     44   object_proxy_ = bus_->GetObjectProxy(
     45       tpm_manager::kTpmManagerServiceName,
     46       dbus::ObjectPath(tpm_manager::kTpmManagerServicePath));
     47   return (object_proxy_ != nullptr);
     48 }
     49 
     50 void TpmOwnershipDBusProxy::GetTpmStatus(
     51     const GetTpmStatusRequest& request,
     52     const GetTpmStatusCallback& callback) {
     53   CallMethod<GetTpmStatusReply>(tpm_manager::kGetTpmStatus, request, callback);
     54 }
     55 
     56 void TpmOwnershipDBusProxy::TakeOwnership(
     57     const TakeOwnershipRequest& request,
     58     const TakeOwnershipCallback& callback) {
     59   CallMethod<TakeOwnershipReply>(
     60       tpm_manager::kTakeOwnership, request, callback);
     61 }
     62 
     63 void TpmOwnershipDBusProxy::RemoveOwnerDependency(
     64     const RemoveOwnerDependencyRequest& request,
     65     const RemoveOwnerDependencyCallback& callback) {
     66   CallMethod<RemoveOwnerDependencyReply>(
     67       tpm_manager::kRemoveOwnerDependency, request, callback);
     68 }
     69 
     70 template<typename ReplyProtobufType,
     71          typename RequestProtobufType,
     72          typename CallbackType>
     73 void TpmOwnershipDBusProxy::CallMethod(const std::string& method_name,
     74                                        const RequestProtobufType& request,
     75                                        const CallbackType& callback) {
     76   auto on_error = [callback](brillo::Error* error) {
     77     ReplyProtobufType reply;
     78     reply.set_status(STATUS_NOT_AVAILABLE);
     79     callback.Run(reply);
     80   };
     81   brillo::dbus_utils::CallMethodWithTimeout(
     82       kDBusTimeoutMS,
     83       object_proxy_,
     84       tpm_manager::kTpmOwnershipInterface,
     85       method_name,
     86       callback,
     87       base::Bind(on_error),
     88       request);
     89 }
     90 
     91 }  // namespace tpm_manager
     92