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_SIMULATOR_HANDLE_H_
18 #define TRUNKS_TPM_SIMULATOR_HANDLE_H_
19 
20 #include "trunks/command_transceiver.h"
21 
22 #include <string>
23 #include <vector>
24 
25 #include "trunks/error_codes.h"
26 
27 namespace trunks {
28 
29 // Sends command requests to an in-process software TPM. All commands are
30 // sent synchronously. The SendCommand method is supported but does not return
31 // until a response is received and the callback has been called. Command and
32 // response data are opaque to this class; it performs no validation.
33 //
34 // Example:
35 //   TpmSimulatorHandle handle;
36 //   if (!handle.Init()) {...}
37 //   std::string response = handle.SendCommandAndWait(command);
38 class TpmSimulatorHandle : public CommandTransceiver {
39  public:
40   TpmSimulatorHandle();
41   ~TpmSimulatorHandle() override;
42 
43   // Initializes a TpmSimulatorHandle instance. This method must be called
44   // successfully before any other method. Returns true on success.
45   bool Init() override;
46 
47   // CommandTranceiver methods.
48   void SendCommand(const std::string& command,
49                    const ResponseCallback& callback) override;
50   std::string SendCommandAndWait(const std::string& command) override;
51 
52  private:
53   std::vector<unsigned char> command_buffer;
54   DISALLOW_COPY_AND_ASSIGN(TpmSimulatorHandle);
55 };
56 
57 }  // namespace trunks
58 
59 #endif  // TRUNKS_TPM_SIMULATOR_HANDLE_H_
60