1// 2// Copyright (C) 2016 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#ifndef TRUNKS_TRUNKS_BINDER_SERVICE_H_ 18#define TRUNKS_TRUNKS_BINDER_SERVICE_H_ 19 20#include <base/memory/weak_ptr.h> 21#include <brillo/binder_watcher.h> 22#include <brillo/daemons/daemon.h> 23 24#include "android/trunks/BnTrunks.h" 25#include "trunks/command_transceiver.h" 26 27namespace trunks { 28 29// TrunksBinderService registers for and handles all incoming binder calls for 30// the trunksd system daemon. 31// 32// Example Usage: 33// 34// TrunksBinderService service; 35// service.set_transceiver(&my_transceiver); 36// service.Run(); 37class TrunksBinderService : public brillo::Daemon { 38 public: 39 TrunksBinderService() = default; 40 ~TrunksBinderService() override = default; 41 42 // The |transceiver| will be the target of all incoming TPM commands. This 43 // class does not take ownership of |transceiver|. 44 void set_transceiver(CommandTransceiver* transceiver) { 45 transceiver_ = transceiver; 46 } 47 48 protected: 49 int OnInit() override; 50 51 private: 52 friend class BinderServiceInternal; 53 class BinderServiceInternal : public android::trunks::BnTrunks { 54 public: 55 explicit BinderServiceInternal(TrunksBinderService* service); 56 ~BinderServiceInternal() override = default; 57 58 // ITrunks interface. 59 android::binder::Status SendCommand( 60 const std::vector<uint8_t>& command, 61 const android::sp<android::trunks::ITrunksClient>& client) override; 62 android::binder::Status SendCommandAndWait( 63 const std::vector<uint8_t>& command, 64 std::vector<uint8_t>* response) override; 65 66 private: 67 void OnResponse(const android::sp<android::trunks::ITrunksClient>& client, 68 const std::string& response); 69 70 base::WeakPtr<BinderServiceInternal> GetWeakPtr() { 71 return weak_factory_.GetWeakPtr(); 72 } 73 74 TrunksBinderService* service_; 75 76 // Declared last so weak pointers are invalidated first on destruction. 77 base::WeakPtrFactory<BinderServiceInternal> weak_factory_{this}; 78 }; 79 80 CommandTransceiver* transceiver_ = nullptr; 81 brillo::BinderWatcher watcher_; 82 android::sp<BinderServiceInternal> binder_; 83 84 DISALLOW_COPY_AND_ASSIGN(TrunksBinderService); 85}; 86 87} // namespace trunks 88 89#endif // TRUNKS_TRUNKS_BINDER_SERVICE_H_ 90