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, 2023-2024 NXP
34  **
35  *********************************************************************************/
36 #define LOG_TAG "HalToHalTransport"
37 
38 #include <vector>
39 #include <signal.h>
40 #include <android-base/logging.h>
41 #include <android-base/stringprintf.h>
42 #include <iomanip>
43 
44 #include <HalToHalTransport.h>
45 #include <EseTransportUtils.h>
46 #include <IntervalTimer.h>
47 
48 namespace keymint::javacard {
SessionTimerFunc(union sigval arg)49 void SessionTimerFunc(union sigval arg){
50      LOG(INFO) << "Session Timer expired !!";
51      HalToHalTransport *obj = (HalToHalTransport*)arg.sival_ptr;
52      if(obj != nullptr)
53        obj->closeConnection();
54 }
openConnection()55 bool HalToHalTransport::openConnection() {
56 	  return mAppletConnection.connectToSEService();
57 }
58 
sendData(const vector<uint8_t> & inData,vector<uint8_t> & output)59 bool HalToHalTransport::sendData(const vector<uint8_t>& inData, vector<uint8_t>& output) {
60     std::vector<uint8_t> cApdu(inData);
61 #ifdef INTERVAL_TIMER
62      LOGD_OMAPI("stop the timer");
63      mTimer.kill();
64 #endif
65      if (!isConnected()) {
66          if (!openConnection()) {
67              return false;
68          }
69      }
70      std::vector<uint8_t> selectResponse;
71      bool status = mAppletConnection.openChannelToApplet(selectResponse);
72      if (!status) {
73          LOG(ERROR) << " Failed to open Logical Channel ,response " << selectResponse;
74          output = std::move(selectResponse);
75          return false;
76      }
77     status = mAppletConnection.transmit(cApdu, output);
78     if (output.size() < 2 ||
79         (output.size() >= 2 && (output.at(output.size() - 2) == LOGICAL_CH_NOT_SUPPORTED_SW1 &&
80                                 output.at(output.size() - 1) == LOGICAL_CH_NOT_SUPPORTED_SW2))) {
81         LOGD_OMAPI("transmit failed ,close the channel");
82         mAppletConnection.close();
83         return false;
84     }
85 #ifdef INTERVAL_TIMER
86      int timeout = mAppletConnection.getSessionTimeout();
87      if(timeout == 0) {
88        closeConnection(); //close immediately
89      } else {
90        LOGD_OMAPI("Set the timer with timeout " << timeout << " ms");
91        mTimer.set(mAppletConnection.getSessionTimeout(), this, SessionTimerFunc);
92      }
93 #endif
94      return true;
95 }
96 
closeConnection()97 bool HalToHalTransport::closeConnection() {
98     return mAppletConnection.close();
99 }
100 
isConnected()101 bool HalToHalTransport::isConnected() {
102     return mAppletConnection.isServiceConnected();
103 }
104 } // namespace keymint::javacard
105