1 /*
2  * Copyright (C) 2018 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 #include "HciEventManager.h"
17 
18 #include <android-base/logging.h>
19 #include <android-base/stringprintf.h>
20 #include <log/log.h>
21 #include <nativehelper/ScopedLocalRef.h>
22 
23 #include "JavaClassConstants.h"
24 #include "NfcJniUtil.h"
25 #include "nfc_config.h"
26 
27 const char* APP_NAME = "NfcNci";
28 uint8_t HciEventManager::sEsePipe;
29 uint8_t HciEventManager::sSimPipe;
30 
31 using android::base::StringPrintf;
32 
HciEventManager()33 HciEventManager::HciEventManager() : mNativeData(nullptr) {}
34 
getInstance()35 HciEventManager& HciEventManager::getInstance() {
36   static HciEventManager sHciEventManager;
37   return sHciEventManager;
38 }
39 
initialize(nfc_jni_native_data * native)40 void HciEventManager::initialize(nfc_jni_native_data* native) {
41   mNativeData = native;
42   tNFA_STATUS nfaStat = NFA_HciRegister(const_cast<char*>(APP_NAME),
43                                         (tNFA_HCI_CBACK*)&nfaHciCallback, true);
44   if (nfaStat != NFA_STATUS_OK) {
45     LOG(ERROR) << "HCI registration failed; status=" << nfaStat;
46   }
47   sEsePipe = NfcConfig::getUnsigned(NAME_OFF_HOST_ESE_PIPE_ID, 0x16);
48   sSimPipe = NfcConfig::getUnsigned(NAME_OFF_HOST_SIM_PIPE_ID, 0x0A);
49 }
50 
notifyTransactionListenersOfAid(std::vector<uint8_t> aid,std::vector<uint8_t> data,std::string evtSrc)51 void HciEventManager::notifyTransactionListenersOfAid(std::vector<uint8_t> aid,
52                                                       std::vector<uint8_t> data,
53                                                       std::string evtSrc) {
54   if (aid.empty()) {
55     return;
56   }
57 
58   JNIEnv* e = NULL;
59   ScopedAttach attach(mNativeData->vm, &e);
60   CHECK(e);
61 
62   ScopedLocalRef<jobject> aidJavaArray(e, e->NewByteArray(aid.size()));
63   CHECK(aidJavaArray.get());
64   e->SetByteArrayRegion((jbyteArray)aidJavaArray.get(), 0, aid.size(),
65                         (jbyte*)&aid[0]);
66   CHECK(!e->ExceptionCheck());
67 
68   ScopedLocalRef<jobject> srcJavaString(e, e->NewStringUTF(evtSrc.c_str()));
69   CHECK(srcJavaString.get());
70 
71   if (data.size() > 0) {
72     ScopedLocalRef<jobject> dataJavaArray(e, e->NewByteArray(data.size()));
73     CHECK(dataJavaArray.get());
74     e->SetByteArrayRegion((jbyteArray)dataJavaArray.get(), 0, data.size(),
75                           (jbyte*)&data[0]);
76     CHECK(!e->ExceptionCheck());
77     e->CallVoidMethod(mNativeData->manager,
78                       android::gCachedNfcManagerNotifyTransactionListeners,
79                       aidJavaArray.get(), dataJavaArray.get(),
80                       srcJavaString.get());
81   } else {
82     e->CallVoidMethod(mNativeData->manager,
83                       android::gCachedNfcManagerNotifyTransactionListeners,
84                       aidJavaArray.get(), NULL, srcJavaString.get());
85   }
86 }
87 
88 /**
89  * BerTlv has the following format:
90  *
91  * byte1 byte2 byte3 byte4 byte5 byte6
92  * 00-7F   -    -     -     -     -
93  * 81    00-FF  -     -     -     -
94  * 82    0000-FFFF    -     -     -
95  * 83      000000-FFFFFF    -     -
96  * 84      00000000-FFFFFFFF      -
97  */
getDataFromBerTlv(std::vector<uint8_t> berTlv)98 std::vector<uint8_t> HciEventManager::getDataFromBerTlv(
99     std::vector<uint8_t> berTlv) {
100   if (berTlv.empty()) {
101     return std::vector<uint8_t>();
102   }
103   size_t lengthTag = berTlv[0];
104   LOG(DEBUG) << "decodeBerTlv: berTlv[0]=" << berTlv[0];
105 
106   /* As per ISO/IEC 7816, read the first byte to determine the length and
107    * the start index accordingly
108    */
109   if (lengthTag < 0x80 && berTlv.size() == (lengthTag + 1)) {
110     return std::vector<uint8_t>(berTlv.begin() + 1, berTlv.end());
111   } else if (lengthTag == 0x81 && berTlv.size() > 2) {
112     size_t length = berTlv[1];
113     if ((length + 2) == berTlv.size()) {
114       return std::vector<uint8_t>(berTlv.begin() + 2, berTlv.end());
115     }
116   } else if (lengthTag == 0x82 && berTlv.size() > 3) {
117     size_t length = ((berTlv[1] << 8) | berTlv[2]);
118     if ((length + 3) == berTlv.size()) {
119       return std::vector<uint8_t>(berTlv.begin() + 3, berTlv.end());
120     }
121   } else if (lengthTag == 0x83 && berTlv.size() > 4) {
122     size_t length = (berTlv[1] << 16) | (berTlv[2] << 8) | berTlv[3];
123     if ((length + 4) == berTlv.size()) {
124       return std::vector<uint8_t>(berTlv.begin() + 4, berTlv.end());
125     }
126   } else if (lengthTag == 0x84 && berTlv.size() > 5) {
127     size_t length =
128         (berTlv[1] << 24) | (berTlv[2] << 16) | (berTlv[3] << 8) | berTlv[4];
129     if ((length + 5) == berTlv.size()) {
130       return std::vector<uint8_t>(berTlv.begin() + 5, berTlv.end());
131     }
132   }
133   LOG(ERROR) << "Error in TLV length encoding!";
134   return std::vector<uint8_t>();
135 }
136 
nfaHciCallback(tNFA_HCI_EVT event,tNFA_HCI_EVT_DATA * eventData)137 void HciEventManager::nfaHciCallback(tNFA_HCI_EVT event,
138                                      tNFA_HCI_EVT_DATA* eventData) {
139   if (eventData == nullptr) {
140     return;
141   }
142 
143   LOG(DEBUG) << StringPrintf(
144       "event=%d code=%d pipe=%d len=%d", event, eventData->rcvd_evt.evt_code,
145       eventData->rcvd_evt.pipe, eventData->rcvd_evt.evt_len);
146 
147   std::string evtSrc;
148   if (eventData->rcvd_evt.pipe == sEsePipe) {
149     evtSrc = "eSE1";
150   } else if (eventData->rcvd_evt.pipe == sSimPipe) {
151     evtSrc = "SIM1";
152   } else {
153     LOG(WARNING) << "Incorrect Pipe Id";
154     return;
155   }
156 
157   // Check the event and check if it contains the AID
158   uint8_t* event_buff = eventData->rcvd_evt.p_evt_buf;
159   uint32_t event_buff_len = eventData->rcvd_evt.evt_len;
160   if (event != NFA_HCI_EVENT_RCVD_EVT ||
161       eventData->rcvd_evt.evt_code != NFA_HCI_EVT_TRANSACTION ||
162       event_buff_len <= 3 || event_buff == nullptr || event_buff[0] != 0x81) {
163     LOG(WARNING) << "Invalid event";
164     return;
165   }
166 
167   uint32_t aid_len = event_buff[1];
168   if (aid_len >= (event_buff_len - 1)) {
169     android_errorWriteLog(0x534e4554, "181346545");
170     LOG(ERROR) << StringPrintf("error: aidlen(%d) is too big", aid_len);
171     return;
172   }
173 
174   std::vector<uint8_t> aid(event_buff + 2, event_buff + aid_len + 2);
175   int32_t berTlvStart = aid_len + 2 + 1;
176   int32_t berTlvLen = event_buff_len - berTlvStart;
177   std::vector<uint8_t> data;
178   if (berTlvLen > 0 && event_buff[2 + aid_len] == 0x82) {
179     std::vector<uint8_t> berTlv(event_buff + berTlvStart,
180                                 event_buff + event_buff_len);
181     // BERTLV decoding here, to support extended data length for params.
182     data = getInstance().getDataFromBerTlv(berTlv);
183   }
184 
185   getInstance().notifyTransactionListenersOfAid(aid, data, evtSrc);
186 }
187 
finalize()188 void HciEventManager::finalize() { mNativeData = NULL; }
189