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 /******************************************************************************
18  **
19  ** The original Work has been changed by NXP.
20  **
21  ** Licensed under the Apache License, Version 2.0 (the "License");
22  ** you may not use this file except in compliance with the License.
23  ** You may obtain a copy of the License at
24  **
25  ** http://www.apache.org/licenses/LICENSE-2.0
26  **
27  ** Unless required by applicable law or agreed to in writing, software
28  ** distributed under the License is distributed on an "AS IS" BASIS,
29  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  ** See the License for the specific language governing permissions and
31  ** limitations under the License.
32  **
33  ** Copyright 2020-2021 NXP
34  **
35  *********************************************************************************/
36 #pragma once
37 #include "ITransport.h"
38 #include <memory>
39 #include <vector>
40 
41 namespace keymint::javacard {
42 using std::shared_ptr;
43 using std::vector;
44 
45 class SocketTransport : public ITransport {
46 
47   public:
SocketTransport(const std::vector<uint8_t> & mAppletAID)48     SocketTransport(const std::vector<uint8_t>& mAppletAID) :
49         ITransport(mAppletAID),
50         mSocket(-1), socketStatus(false) {}
51     /**
52      * Creates a socket instance and connects to the provided server IP and port.
53      */
54     bool openConnection() override;
55     /**
56      * Sends data over socket and receives data back.
57      */
58     bool sendData(const vector<uint8_t>& inData, vector<uint8_t>& output) override;
59     /**
60      * Closes the connection.
61      */
62     bool closeConnection() override;
63     /**
64      * Returns the state of the connection status. Returns true if the connection is active,
65      * false if connection is broken.
66      */
67     bool isConnected() override;
68 
69   private:
70     bool readData(vector<uint8_t>& output);
71     int mSocket;
72     bool socketStatus;
73 };
74 }  // namespace keymint::javacard
75 
76