1 // 2 // Copyright (C) 2014 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_TPM_HANDLE_H_ 18 #define TRUNKS_TPM_HANDLE_H_ 19 20 #include "trunks/command_transceiver.h" 21 22 #include <string> 23 24 #include "trunks/error_codes.h" 25 26 namespace trunks { 27 28 // Sends commands to a TPM device via a handle to /dev/tpm0. All commands are 29 // sent synchronously. The SendCommand method is supported but does not return 30 // until a response is received and the callback has been called. Command and 31 // response data are opaque to this class; it performs no validation. 32 // 33 // Example: 34 // TpmHandle handle; 35 // if (!handle.Init()) {...} 36 // std::string response = handle.SendCommandAndWait(command); 37 class TpmHandle : public CommandTransceiver { 38 public: 39 TpmHandle(); 40 ~TpmHandle() override; 41 42 // Initializes a TpmHandle instance. This method must be called successfully 43 // before any other method. Returns true on success. 44 bool Init() override; 45 46 // CommandTranceiver methods. 47 void SendCommand(const std::string& command, 48 const ResponseCallback& callback) override; 49 std::string SendCommandAndWait(const std::string& command) override; 50 51 private: 52 // Writes a |command| to /dev/tpm0 and reads the |response|. Returns 53 // TPM_RC_SUCCESS on success. 54 TPM_RC SendCommandInternal(const std::string& command, std::string* response); 55 56 int fd_; // A file descriptor for /dev/tpm0. 57 58 DISALLOW_COPY_AND_ASSIGN(TpmHandle); 59 }; 60 61 } // namespace trunks 62 63 #endif // TRUNKS_TPM_HANDLE_H_ 64