nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /****************************************************************************** |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
nxp61433 | af447a5 | 2020-02-12 16:58:04 +0530 | [diff] [blame^] | 15 | * Copyright 2018-2020 NXP |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 16 | * |
| 17 | ******************************************************************************/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 18 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | #include <base/logging.h> |
| 21 | #include "PowerSwitch.h" |
| 22 | #include "RoutingManager.h" |
| 23 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 24 | #include <nativehelper/ScopedUtfChars.h> |
| 25 | #include <nativehelper/ScopedLocalRef.h> |
| 26 | #include "JavaClassConstants.h" |
| 27 | #include "NfcJniUtil.h" |
| 28 | #include "config.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 29 | #include "SecureElement.h" |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame] | 30 | #include "NfcAdaptation.h" |
Suraj Uday Kotharkar | e12d10f | 2019-04-17 15:44:08 +0530 | [diff] [blame] | 31 | #include "NativeJniExtns.h" |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 32 | using android::base::StringPrintf; |
| 33 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 34 | namespace android |
| 35 | { |
Himanshu Singh | 0147ad3 | 2019-11-08 18:44:02 +0530 | [diff] [blame] | 36 | #define INVALID_LEN_SW1 0x64 |
| 37 | #define INVALID_LEN_SW2 0xFF |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 38 | static const int EE_ERROR_INIT = -3; |
Suraj Uday Kotharkar | 93673fe | 2019-07-12 16:25:46 +0530 | [diff] [blame] | 39 | extern bool nfcManager_isNfcActive(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 40 | /******************************************************************************* |
| 41 | ** |
| 42 | ** Function: nativeNfcSecureElement_doOpenSecureElementConnection |
| 43 | ** |
| 44 | ** Description: Connect to the secure element. |
| 45 | ** e: JVM environment. |
| 46 | ** o: Java object. |
| 47 | ** |
| 48 | ** Returns: Handle of secure element. values < 0 represent failure. |
| 49 | ** |
| 50 | *******************************************************************************/ |
| 51 | static jint nativeNfcSecureElement_doOpenSecureElementConnection (JNIEnv*, jobject) |
| 52 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 53 | LOG(INFO) << StringPrintf("%s: Enter; ", __func__); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 54 | bool stat = false; |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 55 | const int32_t recvBufferMaxSize = 1024; |
| 56 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 57 | int32_t recvBufferActualSize = 0; |
| 58 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 59 | jint secElemHandle = EE_ERROR_INIT; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 60 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 61 | SecureElement &se = SecureElement::getInstance(); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 62 | se.mModeSetNtfstatus = NFA_STATUS_FAILED; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 63 | |
Suraj Uday Kotharkar | e12d10f | 2019-04-17 15:44:08 +0530 | [diff] [blame] | 64 | NativeJniExtns::getInstance().notifyNfcEvent(__func__); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 65 | /* Tell the controller to power up to get ready for sec elem operations */ |
| 66 | PowerSwitch::getInstance ().setLevel (PowerSwitch::FULL_POWER); |
| 67 | PowerSwitch::getInstance ().setModeOn (PowerSwitch::SE_CONNECTED); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 68 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 69 | /* If controller is not routing AND there is no pipe connected, |
| 70 | then turn on the sec elem */ |
| 71 | stat = se.activate(SecureElement::ESE_ID); // It is to get the current activated handle. |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 72 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 73 | if((stat) && (nfcFL.eseFL._NCI_NFCEE_PWR_LINK_CMD)) |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 74 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 75 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON|se.COMM_LINK_ACTIVE); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 76 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 77 | if(status != NFA_STATUS_OK) |
| 78 | { |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 79 | LOG(INFO) << StringPrintf("%s: power link command failed", __func__); |
| 80 | stat =false; |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 81 | } |
| 82 | else |
| 83 | { |
nxpandroid | 82f7e9e | 2018-04-05 14:54:41 +0530 | [diff] [blame] | 84 | stat = se.SecEle_Modeset(se.NFCEE_ENABLE); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 85 | if(se.mModeSetNtfstatus != NFA_STATUS_OK) |
| 86 | { |
| 87 | stat = false; |
| 88 | LOG(INFO) << StringPrintf("%s: Mode set ntf STATUS_FAILED", __func__); |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 89 | |
hariprasad nalacheruvu | 7254b03 | 2018-11-13 15:43:47 +0530 | [diff] [blame] | 90 | SyncEventGuard guard (se.mEERecoveryComplete); |
| 91 | { |
| 92 | se.mEERecoveryComplete.wait(); |
| 93 | LOG(INFO) << StringPrintf("%s: Recovery complete", __func__); |
| 94 | } |
| 95 | if(se.mErrorRecovery) |
| 96 | { |
| 97 | stat = true; |
| 98 | } |
| 99 | } |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 100 | if(stat == true) |
| 101 | { |
hariprasad nalacheruvu | f032814 | 2018-11-07 14:03:02 +0530 | [diff] [blame] | 102 | se.mIsWiredModeOpen = true; |
Suraj Uday Kotharkar | 9e250fb | 2018-06-08 21:06:52 +0530 | [diff] [blame] | 103 | stat = se.apduGateReset(se.mActiveEeHandle, recvBuffer, &recvBufferActualSize); |
| 104 | if (stat) |
| 105 | { |
| 106 | secElemHandle = se.mActiveEeHandle; |
| 107 | } |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 108 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 109 | } |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 110 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 111 | /* if code fails to connect to the secure element, and nothing is active, then |
| 112 | * tell the controller to power down |
| 113 | */ |
| 114 | if ((!stat) && (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED))) |
| 115 | { |
| 116 | LOG(INFO) << StringPrintf("%s: stat fails; ", __func__); |
| 117 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
| 118 | se.deactivate(SecureElement::ESE_ID); |
| 119 | se.mIsWiredModeOpen = false; |
| 120 | stat = false; |
| 121 | } |
| 122 | LOG(INFO) << StringPrintf("%s: exit; return handle=0x%X", __func__, secElemHandle); |
| 123 | return secElemHandle; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | /******************************************************************************* |
| 128 | ** |
| 129 | ** Function: nativeNfcSecureElement_doDisconnectSecureElementConnection |
| 130 | ** |
| 131 | ** Description: Disconnect from the secure element. |
| 132 | ** e: JVM environment. |
| 133 | ** o: Java object. |
| 134 | ** handle: Handle of secure element. |
| 135 | ** |
| 136 | ** Returns: True if ok. |
| 137 | ** |
| 138 | *******************************************************************************/ |
| 139 | static jboolean nativeNfcSecureElement_doDisconnectSecureElementConnection (JNIEnv*, jobject, jint handle) |
| 140 | { |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 141 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%04x", __func__, handle); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 142 | bool stat = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 143 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 144 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 145 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 146 | if(!se.mIsWiredModeOpen) |
| 147 | return false; |
Sreenivas | 88ee6aa | 2018-05-14 13:17:57 +0530 | [diff] [blame] | 148 | /* release any pending transceive wait */ |
| 149 | se.releasePendingTransceive(); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 150 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 151 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON); |
| 152 | if(status != NFA_STATUS_OK) |
| 153 | { |
| 154 | LOG(INFO) << StringPrintf("%s: power link command failed", __func__); |
nxpandroid | 0f06fde | 2017-08-14 11:25:28 +0530 | [diff] [blame] | 155 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 156 | else |
| 157 | { |
Sreenivas | f83b876 | 2018-04-09 16:09:49 +0530 | [diff] [blame] | 158 | status = se.sendEvent(SecureElement::EVT_END_OF_APDU_TRANSFER); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 159 | if(status == NFA_STATUS_OK) |
Sreenivas | f83b876 | 2018-04-09 16:09:49 +0530 | [diff] [blame] | 160 | stat = true; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 161 | } |
Sreenivas | 88ee6aa | 2018-05-14 13:17:57 +0530 | [diff] [blame] | 162 | se.mIsWiredModeOpen = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 163 | |
nxpandroid | 5d64ce9 | 2016-11-18 19:48:53 +0530 | [diff] [blame] | 164 | /* if nothing is active after this, then tell the controller to power down */ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 165 | if (! PowerSwitch::getInstance ().setModeOff (PowerSwitch::SE_CONNECTED)) |
| 166 | PowerSwitch::getInstance ().setLevel (PowerSwitch::LOW_POWER); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 167 | LOG(INFO) << StringPrintf("%s: exit", __func__); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 168 | return stat ? JNI_TRUE : JNI_FALSE; |
| 169 | } |
| 170 | /******************************************************************************* |
| 171 | ** |
Suraj Uday Kotharkar | ce445d8 | 2019-04-19 10:30:46 +0530 | [diff] [blame] | 172 | ** Function: nativeNfcSecureElement_doResetForEseCosUpdate |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 173 | ** |
| 174 | ** Description: Reset the secure element. |
| 175 | ** e: JVM environment. |
| 176 | ** o: Java object. |
| 177 | ** handle: Handle of secure element. |
| 178 | ** |
| 179 | ** Returns: True if ok. |
| 180 | ** |
| 181 | *******************************************************************************/ |
Suraj Uday Kotharkar | ce445d8 | 2019-04-19 10:30:46 +0530 | [diff] [blame] | 182 | static jboolean nativeNfcSecureElement_doResetForEseCosUpdate(JNIEnv*, jobject, |
| 183 | jint handle) { |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame] | 184 | bool stat = false; |
| 185 | int ret = -1; |
| 186 | NfcAdaptation& theInstance = NfcAdaptation::GetInstance(); |
| 187 | tHAL_NFC_ENTRY* halFuncEntries = theInstance.GetHalEntryFuncs (); |
nxp61433 | af447a5 | 2020-02-12 16:58:04 +0530 | [diff] [blame^] | 188 | |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame] | 189 | LOG(INFO) << StringPrintf("%s: Entry", __func__); |
| 190 | if(NULL == halFuncEntries) { |
| 191 | LOG(INFO) << StringPrintf("%s: halFuncEntries is NULL", __func__); |
| 192 | } else { |
nxp61433 | af447a5 | 2020-02-12 16:58:04 +0530 | [diff] [blame^] | 193 | ret = theInstance.resetEse((uint64_t)NFA_ESE_HARD_RESET); |
| 194 | if(ret == 0) { |
| 195 | LOG(INFO) << StringPrintf("%s: reset IOCTL failed", __func__); |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame] | 196 | } else { |
| 197 | stat = true; |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 198 | } |
Suraj Uday Kotharkar | a09a2b7 | 2019-04-17 15:07:44 +0530 | [diff] [blame] | 199 | } |
| 200 | LOG(INFO) << StringPrintf("%s: exit", __func__); |
| 201 | return stat; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /******************************************************************************* |
| 205 | ** |
| 206 | ** Function: nativeNfcSecureElement_doGetAtr |
| 207 | ** |
| 208 | ** Description: GetAtr from the connected eSE. |
| 209 | ** e: JVM environment. |
| 210 | ** o: Java object. |
| 211 | ** handle: Handle of secure element. |
| 212 | ** |
| 213 | ** Returns: Buffer of received data. |
| 214 | ** |
| 215 | *******************************************************************************/ |
| 216 | static jbyteArray nativeNfcSecureElement_doGetAtr (JNIEnv* e, jobject, jint handle) |
| 217 | { |
| 218 | bool stat = false; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 219 | const int32_t recvBufferMaxSize = 1024; |
| 220 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 221 | int32_t recvBufferActualSize = 0; |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 222 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%04x", __func__, handle); |
| 223 | SecureElement &se = SecureElement::getInstance(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 224 | |
hariprasad nalacheruvu | ba06cec | 2018-06-06 17:55:23 +0530 | [diff] [blame] | 225 | stat = se.getAtr(recvBuffer, &recvBufferActualSize); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 226 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 227 | //copy results back to java |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 228 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 229 | if (result != NULL) { |
| 230 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
| 231 | } |
| 232 | |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 233 | LOG(INFO) << StringPrintf("%s: exit: recv len=%d", __func__, recvBufferActualSize); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 234 | |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | /******************************************************************************* |
| 239 | ** |
| 240 | ** Function: nativeNfcSecureElement_doTransceive |
| 241 | ** |
| 242 | ** Description: Send data to the secure element; retrieve response. |
| 243 | ** e: JVM environment. |
| 244 | ** o: Java object. |
| 245 | ** handle: Secure element's handle. |
| 246 | ** data: Data to send. |
| 247 | ** |
| 248 | ** Returns: Buffer of received data. |
| 249 | ** |
| 250 | *******************************************************************************/ |
| 251 | static jbyteArray nativeNfcSecureElement_doTransceive (JNIEnv* e, jobject, jint handle, jbyteArray data) |
| 252 | { |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 253 | const int32_t recvBufferMaxSize = 0x8800;//1024; 34k |
| 254 | uint8_t recvBuffer [recvBufferMaxSize]; |
| 255 | int32_t recvBufferActualSize = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 256 | ScopedByteArrayRW bytes(e, data); |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 257 | LOG(INFO) << StringPrintf("%s: enter; handle=0x%X; buf len=%zu", __func__, handle, bytes.size()); |
Himanshu Singh | 0147ad3 | 2019-11-08 18:44:02 +0530 | [diff] [blame] | 258 | if(bytes.size() > recvBufferMaxSize) { |
| 259 | LOG(ERROR) << StringPrintf("%s: datasize not supported", __func__); |
| 260 | uint8_t respBuf[] = {INVALID_LEN_SW1, INVALID_LEN_SW2}; |
| 261 | jbyteArray resp = e->NewByteArray(sizeof(respBuf)); |
| 262 | if (resp != NULL) { |
| 263 | e->SetByteArrayRegion(resp, 0, sizeof(respBuf), (jbyte *) respBuf); |
| 264 | } |
| 265 | return resp; |
| 266 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 267 | |
| 268 | SecureElement &se = SecureElement::getInstance(); |
| 269 | if(!se.mIsWiredModeOpen) |
| 270 | return NULL; |
| 271 | |
Sreenivas | 426138e | 2018-04-04 14:56:08 +0530 | [diff] [blame] | 272 | se.transceive(reinterpret_cast<uint8_t*>(&bytes[0]), bytes.size(), recvBuffer, recvBufferMaxSize, recvBufferActualSize, se.SmbTransceiveTimeOutVal); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 273 | |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 274 | //copy results back to java |
| 275 | jbyteArray result = e->NewByteArray(recvBufferActualSize); |
| 276 | if (result != NULL) |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 277 | { |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 278 | e->SetByteArrayRegion(result, 0, recvBufferActualSize, (jbyte *) recvBuffer); |
nxpandroid | 7d44e57 | 2016-08-01 19:11:04 +0530 | [diff] [blame] | 279 | } |
nxf24591 | dc0bc2c | 2018-02-21 17:33:08 +0530 | [diff] [blame] | 280 | |
| 281 | LOG(INFO) << StringPrintf("%s: exit: recv len=%d", __func__, recvBufferActualSize); |
nxpandroid | a9a68ba | 2016-01-14 21:12:17 +0530 | [diff] [blame] | 282 | return result; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 283 | } |
Suraj Uday Kotharkar | 93673fe | 2019-07-12 16:25:46 +0530 | [diff] [blame] | 284 | /******************************************************************************* |
| 285 | ** |
| 286 | ** Function: nfcManager_doactivateSeInterface |
| 287 | ** |
| 288 | ** Description: Activate SecureElement Interface |
| 289 | ** |
| 290 | ** Returns: Success/Failure |
| 291 | ** Success = 0x00 |
| 292 | ** Failure = 0x03 |
| 293 | ** |
| 294 | *******************************************************************************/ |
| 295 | static jint nfcManager_doactivateSeInterface(JNIEnv* e, jobject o) { |
| 296 | jint ret = NFA_STATUS_FAILED; |
| 297 | tNFA_STATUS status = NFA_STATUS_FAILED; |
| 298 | SecureElement& se = SecureElement::getInstance(); |
| 299 | se.mModeSetNtfstatus = NFA_STATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 300 | |
Suraj Uday Kotharkar | 93673fe | 2019-07-12 16:25:46 +0530 | [diff] [blame] | 301 | LOG(INFO) << StringPrintf("%s: enter", __func__); |
| 302 | |
| 303 | if (!nfcManager_isNfcActive() || (!nfcFL.eseFL._NCI_NFCEE_PWR_LINK_CMD)) { |
| 304 | LOG(INFO) << StringPrintf("%s: Not supported", __func__); |
| 305 | return ret; |
| 306 | } |
| 307 | if (se.mIsSeIntfActivated) { |
| 308 | LOG(INFO) << StringPrintf("%s: Already activated", __func__); |
| 309 | return NFA_STATUS_OK; |
| 310 | } |
| 311 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON | se.COMM_LINK_ACTIVE); |
| 312 | if (status == NFA_STATUS_OK) { |
| 313 | status = se.SecEle_Modeset(se.NFCEE_ENABLE); |
| 314 | if (se.mModeSetNtfstatus != NFA_STATUS_OK) { |
| 315 | LOG(INFO) << StringPrintf("%s: Mode set ntf STATUS_FAILED", __func__); |
| 316 | SyncEventGuard guard(se.mEERecoveryComplete); |
| 317 | if (se.mEERecoveryComplete.wait(NFC_CMD_TIMEOUT)) { |
| 318 | LOG(INFO) << StringPrintf("%s: Recovery complete", __func__); |
| 319 | ret = NFA_STATUS_OK; |
| 320 | } |
| 321 | } else { |
| 322 | ret = NFA_STATUS_OK; |
| 323 | } |
| 324 | } else { |
| 325 | LOG(INFO) << StringPrintf("%s: power link command failed", __func__); |
| 326 | } |
| 327 | |
| 328 | if (ret == NFA_STATUS_OK) se.mIsSeIntfActivated = true; |
| 329 | LOG(INFO) << StringPrintf("%s: Exit", __func__); |
| 330 | return ret; |
| 331 | } |
| 332 | /******************************************************************************* |
| 333 | ** |
| 334 | ** Function: nfcManager_dodeactivateSeInterface |
| 335 | ** |
| 336 | ** Description: Deactivate SecureElement Interface |
| 337 | ** |
| 338 | ** Returns: Success/Failure |
| 339 | ** Success = 0x00 |
| 340 | ** Failure = 0x03 |
| 341 | ** |
| 342 | *******************************************************************************/ |
| 343 | static jint nfcManager_dodeactivateSeInterface(JNIEnv* e, jobject o) { |
| 344 | jint ret = NFA_STATUS_FAILED; |
| 345 | tNFA_STATUS status = NFA_STATUS_FAILED; |
| 346 | SecureElement& se = SecureElement::getInstance(); |
| 347 | LOG(INFO) << StringPrintf("%s: enter", __func__); |
| 348 | |
| 349 | if (!nfcManager_isNfcActive() || (!nfcFL.eseFL._NCI_NFCEE_PWR_LINK_CMD)) { |
| 350 | LOG(INFO) << StringPrintf("%s: Not supported", __func__); |
| 351 | return ret; |
| 352 | } |
| 353 | if (!se.mIsSeIntfActivated) { |
| 354 | LOG(INFO) << StringPrintf("%s: Already Deactivated or call activate first", |
| 355 | __func__); |
| 356 | return NFA_STATUS_OK; |
| 357 | } |
| 358 | |
| 359 | status = se.setNfccPwrConfig(se.POWER_ALWAYS_ON); |
| 360 | if (status == NFA_STATUS_OK) { |
| 361 | LOG(INFO) << StringPrintf("%s: power link command success", __func__); |
| 362 | se.mIsSeIntfActivated = false; |
| 363 | ret = NFA_STATUS_OK; |
| 364 | } |
| 365 | LOG(INFO) << StringPrintf("%s: Exit, status =0x02%u", __func__, status); |
| 366 | return ret; |
| 367 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 368 | /***************************************************************************** |
| 369 | ** |
| 370 | ** Description: JNI functions |
| 371 | ** |
| 372 | *****************************************************************************/ |
Suraj Uday Kotharkar | ce445d8 | 2019-04-19 10:30:46 +0530 | [diff] [blame] | 373 | static JNINativeMethod gMethods[] = { |
| 374 | {"doNativeOpenSecureElementConnection", "()I", |
| 375 | (void*)nativeNfcSecureElement_doOpenSecureElementConnection}, |
| 376 | {"doNativeDisconnectSecureElementConnection", "(I)Z", |
| 377 | (void*)nativeNfcSecureElement_doDisconnectSecureElementConnection}, |
| 378 | {"doResetForEseCosUpdate", "(I)Z", |
| 379 | (void*)nativeNfcSecureElement_doResetForEseCosUpdate}, |
| 380 | {"doTransceive", "(I[B)[B", (void*)nativeNfcSecureElement_doTransceive}, |
| 381 | {"doNativeGetAtr", "(I)[B", (void*)nativeNfcSecureElement_doGetAtr}, |
Suraj Uday Kotharkar | 93673fe | 2019-07-12 16:25:46 +0530 | [diff] [blame] | 382 | {"doactivateSeInterface", "()I", (void*)nfcManager_doactivateSeInterface}, |
| 383 | {"dodeactivateSeInterface", "()I", |
| 384 | (void*)nfcManager_dodeactivateSeInterface}, |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 385 | }; |
| 386 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 387 | /******************************************************************************* |
| 388 | ** |
| 389 | ** Function: register_com_android_nfc_NativeNfcSecureElement |
| 390 | ** |
| 391 | ** Description: Regisgter JNI functions with Java Virtual Machine. |
| 392 | ** e: Environment of JVM. |
| 393 | ** |
| 394 | ** Returns: Status of registration. |
| 395 | ** |
| 396 | *******************************************************************************/ |
| 397 | int register_com_android_nfc_NativeNfcSecureElement(JNIEnv *e) |
| 398 | { |
| 399 | return jniRegisterNativeMethods(e, gNativeNfcSecureElementClassName, |
| 400 | gMethods, NELEM(gMethods)); |
| 401 | } |
| 402 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 403 | } // namespace android |