1 /* 2 ** 3 ** Copyright 2020, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 #pragma once 18 19 #include <memory> 20 #include <vector> 21 22 #include "ITransport.h" 23 24 namespace keymint::javacard { 25 using std::shared_ptr; 26 using std::vector; 27 28 class SocketTransport : public ITransport { 29 30 public: SocketTransport()31 SocketTransport() : mSocket(-1), socketStatus(false) {} 32 /** 33 * Creates a socket instance and connects to the provided server IP and port. 34 */ 35 keymaster_error_t openConnection() override; 36 /** 37 * Sends data over socket and receives data back. 38 */ 39 keymaster_error_t sendData(const vector<uint8_t>& inData, vector<uint8_t>& output) override; 40 /** 41 * Closes the connection. 42 */ 43 keymaster_error_t closeConnection() override; 44 /** 45 * Returns the state of the connection status. Returns true if the connection is active, 46 * false if connection is broken. 47 */ 48 bool isConnected() override; 49 50 private: 51 bool readData(vector<uint8_t>& output); 52 int mSocket; 53 bool socketStatus; 54 }; 55 } // namespace keymint::javacard 56