1 // Copyright 2014 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TRUNKS_COMMAND_TRANSCEIVER_H_ 6 #define TRUNKS_COMMAND_TRANSCEIVER_H_ 7 8 #include <string> 9 #include <utility> 10 11 #include "callback.h" 12 13 namespace trunks { 14 15 // CommandTransceiver is an interface that sends commands to a TPM device and 16 // receives responses. It can operate synchronously or asynchronously. 17 class CommandTransceiver { 18 public: 19 typedef trunks::OnceCallback<void(const std::string& response)> 20 ResponseCallback; 21 ~CommandTransceiver()22 virtual ~CommandTransceiver() {} 23 24 // Sends a TPM |command| asynchronously. When a |response| is received, 25 // |callback| will be called with the |response| data from the TPM. If a 26 // transmission error occurs |callback| will be called with a well-formed 27 // error |response|. 28 virtual void SendCommand(const std::string& command, 29 ResponseCallback callback) = 0; 30 31 // Sends a TPM |command| synchronously (i.e. waits for a response) and returns 32 // the response. If a transmission error occurs the response will be populated 33 // with a well-formed error response. 34 virtual std::string SendCommandAndWait(const std::string& command) = 0; 35 36 // Similar to the SendCommand, but we add an extra sender information. 37 // By default, it will fallback to the normal implementation. SendCommandWithSender(const std::string & command,uint64_t sender,ResponseCallback callback)38 virtual void SendCommandWithSender(const std::string& command, 39 uint64_t sender, 40 ResponseCallback callback) { 41 return SendCommand(command, std::move(callback)); 42 } 43 44 // Similar to the SendCommandAndWait, but we add an extra sender information. 45 // By default, it will fallback to the normal implementation. SendCommandWithSenderAndWait(const std::string & command,uint64_t sender)46 virtual std::string SendCommandWithSenderAndWait(const std::string& command, 47 uint64_t sender) { 48 return SendCommandAndWait(command); 49 } 50 51 // Initializes the actual interface, replaced by the derived classes, where 52 // needed. Init()53 virtual bool Init() { return true; } 54 }; 55 56 } // namespace trunks 57 58 #endif // TRUNKS_COMMAND_TRANSCEIVER_H_ 59