1 /*
2 * Copyright (C) 2024 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 #include "NfceeManager.h"
18
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <errno.h>
22 #include <nativehelper/ScopedLocalRef.h>
23
24 #include "nfc_config.h"
25
26 using android::base::StringPrintf;
27
28 NfceeManager NfceeManager::sNfceeManager;
29
30 /*******************************************************************************
31 **
32 ** Function: NfceeManager
33 **
34 ** Description: Initialize member variables.
35 **
36 ** Returns: None
37 **
38 *******************************************************************************/
NfceeManager()39 NfceeManager::NfceeManager() : mNumEePresent(0) {
40 mActualNumEe = MAX_NUM_NFCEE;
41 eseName = "eSE";
42 uiccName = "SIM";
43 ndefNfceeName = "NDEF-NFCEE";
44 memset(&mNfceeData_t, 0, sizeof(mNfceeData));
45 }
46
47 /*******************************************************************************
48 **
49 ** Function: ~NfceeManager
50 **
51 ** Description: Release all resources.
52 **
53 ** Returns: None
54 **
55 *******************************************************************************/
~NfceeManager()56 NfceeManager::~NfceeManager() {}
57
58 /*******************************************************************************
59 **
60 ** Function: getInstance
61 **
62 ** Description: Get the singleton of this object.
63 **
64 ** Returns: Reference to this object.
65 **
66 *******************************************************************************/
getInstance()67 NfceeManager& NfceeManager::getInstance() { return sNfceeManager; }
68
69 /*******************************************************************************
70 **
71 ** Function: getActiveNfceeList
72 **
73 ** Description: Get the list of Activated NFCEE.
74 ** e: Java Virtual Machine.
75 **
76 ** Returns: List of Activated NFCEE.
77 **
78 *******************************************************************************/
getActiveNfceeList(JNIEnv * e)79 jobject NfceeManager::getActiveNfceeList(JNIEnv* e) {
80 ScopedLocalRef<jclass> hashMapClass(e, e->FindClass(mHashMapClassName));
81 jmethodID hashMapConstructor =
82 e->GetMethodID(hashMapClass.get(), "<init>", "()V");
83 jmethodID hashMapPut = e->GetMethodID(
84 hashMapClass.get(), "put",
85 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
86 jobject nfceeHashMaptObj =
87 e->NewObject(hashMapClass.get(), hashMapConstructor);
88 ScopedLocalRef<jclass> integerClass(e, e->FindClass("java/lang/Integer"));
89 jmethodID integerConstructor =
90 e->GetMethodID(integerClass.get(), "<init>", "(I)V");
91 if (!getNFCEeInfo()) return (nfceeHashMaptObj);
92
93 vector<uint8_t> eSERoute;
94 vector<uint8_t> uiccRoute;
95 map<uint8_t, std::string> nfceeMap;
96
97 if (NfcConfig::hasKey(NAME_OFFHOST_ROUTE_ESE)) {
98 eSERoute = NfcConfig::getBytes(NAME_OFFHOST_ROUTE_ESE);
99 }
100
101 if (NfcConfig::hasKey(NAME_OFFHOST_ROUTE_UICC)) {
102 uiccRoute = NfcConfig::getBytes(NAME_OFFHOST_ROUTE_UICC);
103 }
104
105 for (uint8_t i = 0; i < eSERoute.size(); ++i) {
106 nfceeMap[eSERoute[i]] = eseName + std::to_string(i + 1);
107 }
108
109 for (uint8_t i = 0; i < uiccRoute.size(); ++i) {
110 nfceeMap[uiccRoute[i]] = uiccName + std::to_string(i + 1);
111 }
112
113 if (NfcConfig::hasKey(NAME_T4T_NFCEE_ENABLE)) {
114 if (NfcConfig::getUnsigned(NAME_T4T_NFCEE_ENABLE)) {
115 uint8_t defaultNdefNfceeRoute =
116 NfcConfig::getUnsigned(NAME_DEFAULT_NDEF_NFCEE_ROUTE, 0x10);
117 nfceeMap[defaultNdefNfceeRoute] = ndefNfceeName;
118 }
119 }
120
121 for (int i = 0; i < mNfceeData_t.mNfceePresent; i++) {
122 uint8_t id = (mNfceeData_t.mNfceeID[i] & ~NFA_HANDLE_GROUP_EE);
123 uint8_t status = mNfceeData_t.mNfceeStatus[i];
124 if ((nfceeMap.find(id) != nfceeMap.end()) &&
125 (status == NFC_NFCEE_STATUS_ACTIVE)) {
126 jstring element = e->NewStringUTF(nfceeMap[id].c_str());
127 jobject jtechmask = e->NewObject(integerClass.get(), integerConstructor,
128 mNfceeData_t.mNfceeTechMask[i]);
129 e->CallObjectMethod(nfceeHashMaptObj, hashMapPut, element, jtechmask);
130 e->DeleteLocalRef(element);
131 }
132 }
133 return nfceeHashMaptObj;
134 }
135
136 /*******************************************************************************
137 **
138 ** Function: getNFCEeInfo
139 **
140 ** Description: Get latest information about execution
141 ** environments from stack.
142 ** Returns: True if at least 1 EE is available.
143 **
144 *******************************************************************************/
getNFCEeInfo()145 bool NfceeManager::getNFCEeInfo() {
146 static const char fn[] = "getNFCEeInfo";
147 LOG(INFO) << StringPrintf("%s: enter", fn);
148 tNFA_STATUS nfaStat = NFA_STATUS_FAILED;
149 mNumEePresent = 0x00;
150 memset(&mNfceeData_t, 0, sizeof(mNfceeData_t));
151
152 /* Reading latest NFCEE info in case it is updated */
153 if ((nfaStat = NFA_EeGetInfo(&mActualNumEe, mEeInfo)) != NFA_STATUS_OK) {
154 LOG(ERROR) << StringPrintf("%s: fail get info; error=0x%X", fn, nfaStat);
155 mActualNumEe = 0;
156 } else {
157 LOG(INFO) << StringPrintf("%s: num NFCEE discovered=%u", fn, mActualNumEe);
158 if (mActualNumEe != 0) {
159 for (uint8_t xx = 0; xx < mActualNumEe; xx++) {
160 tNFA_TECHNOLOGY_MASK eeTechnology = 0x00;
161 if (mEeInfo[xx].ee_interface[0] != NCI_NFCEE_INTERFACE_HCI_ACCESS)
162 mNumEePresent++;
163
164 mNfceeData_t.mNfceeID[xx] = mEeInfo[xx].ee_handle;
165 mNfceeData_t.mNfceeStatus[xx] = mEeInfo[xx].ee_status;
166 if (mEeInfo[xx].la_protocol) eeTechnology |= NFA_TECHNOLOGY_MASK_A;
167 if (mEeInfo[xx].lb_protocol) eeTechnology |= NFA_TECHNOLOGY_MASK_B;
168 if (mEeInfo[xx].lf_protocol) eeTechnology |= NFA_TECHNOLOGY_MASK_F;
169 mNfceeData_t.mNfceeTechMask[xx] = eeTechnology;
170 }
171 }
172 }
173 LOG(INFO) << StringPrintf("%s: exit; mActualNumEe=%d, mNumEePresent=%d", fn,
174 mActualNumEe, mNumEePresent);
175 mNfceeData_t.mNfceePresent = mNumEePresent;
176 return (mActualNumEe != 0);
177 }
178