blob: d288f0c7563a0447d0bda6434ac938d33eca1aff [file] [log] [blame]
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -08001/******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
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 ******************************************************************************/
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080018#include <android-base/stringprintf.h>
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -080019#include <android/hardware/nfc/1.1/INfc.h>
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070020#include <base/command_line.h>
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080021#include <base/logging.h>
22#include <cutils/properties.h>
Martijn Coenene9e48e52016-09-06 16:05:25 +020023#include <hwbinder/ProcessState.h>
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -070024
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080025#include "NfcAdaptation.h"
Evan Chua24be4f2013-11-13 15:30:16 -050026#include "android_logmsg.h"
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080027#include "debug_nfcsnoop.h"
28#include "nfa_api.h"
Ruchi Kandoif4064152018-02-28 16:27:41 -080029#include "nfa_rw_api.h"
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080030#include "nfc_config.h"
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080031#include "nfc_int.h"
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080032
Martijn Coenene9e48e52016-09-06 16:05:25 +020033using android::OK;
34using android::sp;
35using android::status_t;
36
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080037using android::base::StringPrintf;
Martijn Coenene9e48e52016-09-06 16:05:25 +020038using android::hardware::ProcessState;
39using android::hardware::Return;
40using android::hardware::Void;
41using android::hardware::nfc::V1_0::INfc;
Ruchi Kandoif4064152018-02-28 16:27:41 -080042using android::hardware::nfc::V1_1::PresenceCheckAlgorithm;
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -080043using INfcV1_1 = android::hardware::nfc::V1_1::INfc;
Ruchi Kandoif4064152018-02-28 16:27:41 -080044using NfcVendorConfig = android::hardware::nfc::V1_1::NfcConfig;
Ruchi Kandoi267fdf52018-02-02 15:31:55 -080045using android::hardware::nfc::V1_1::INfcClientCallback;
Martijn Coenene9e48e52016-09-06 16:05:25 +020046using android::hardware::hidl_vec;
47
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080048extern bool nfc_debug_enabled;
49
Ruchi Kandoi7dab0e52017-08-03 13:09:49 -070050extern void GKI_shutdown();
Ruchi Kandoi7dab0e52017-08-03 13:09:49 -070051extern void verify_stack_non_volatile_store();
52extern void delete_stack_non_volatile_store(bool forceDelete);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080053
54NfcAdaptation* NfcAdaptation::mpInstance = NULL;
55ThreadMutex NfcAdaptation::sLock;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080056tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = NULL;
57tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = NULL;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070058ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
59ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
Martijn Coenene9e48e52016-09-06 16:05:25 +020060sp<INfc> NfcAdaptation::mHal;
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -080061sp<INfcV1_1> NfcAdaptation::mHal_1_1;
Martijn Coenene9e48e52016-09-06 16:05:25 +020062INfcClientCallback* NfcAdaptation::mCallback;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080063
Ruchi Kandoi6767aec2017-09-26 09:46:26 -070064bool nfc_debug_enabled = false;
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080065std::string nfc_storage_path;
Love Khannad7852c92017-06-02 19:55:05 +053066uint8_t appl_dta_mode_flag = 0x00;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -080067
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080068extern tNFA_DM_CFG nfa_dm_cfg;
69extern tNFA_PROPRIETARY_CFG nfa_proprietary_cfg;
70extern tNFA_HCI_CFG nfa_hci_cfg;
Ruchi Kandoi512ee632017-01-03 13:59:10 -080071extern uint8_t nfa_ee_max_ee_cfg;
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080072extern bool nfa_poll_bail_out_mode;
73
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080074// Whitelist for hosts allowed to create a pipe
75// See ADM_CREATE_PIPE command in the ETSI test specification
76// ETSI TS 102 622, section 6.1.3.1
77static std::vector<uint8_t> host_whitelist;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -070078
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080079namespace {
80void initializeGlobalDebugEnabledFlag() {
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080081 nfc_debug_enabled =
82 (NfcConfig::getUnsigned(NAME_NFC_DEBUG_ENABLED, 0) != 0) ? true : false;
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080083
84 char valueStr[PROPERTY_VALUE_MAX] = {0};
Andre Eisenbach6261a472017-12-05 16:56:25 -080085 int len = property_get("nfc.debug_enabled", valueStr, "");
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080086 if (len > 0) {
87 // let Android property override .conf variable
Andre Eisenbach6261a472017-12-05 16:56:25 -080088 unsigned debug_enabled = 0;
89 sscanf(valueStr, "%u", &debug_enabled);
90 nfc_debug_enabled = (debug_enabled == 0) ? false : true;
Andre Eisenbach8a4edf62017-11-20 14:51:11 -080091 }
92
93 DLOG_IF(INFO, nfc_debug_enabled)
94 << StringPrintf("%s: level=%u", __func__, nfc_debug_enabled);
95}
96} // namespace
97
Martijn Coenene9e48e52016-09-06 16:05:25 +020098class NfcClientCallback : public INfcClientCallback {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -080099 public:
100 NfcClientCallback(tHAL_NFC_CBACK* eventCallback,
101 tHAL_NFC_DATA_CBACK dataCallback) {
102 mEventCallback = eventCallback;
103 mDataCallback = dataCallback;
104 };
105 virtual ~NfcClientCallback() = default;
Ruchi Kandoi267fdf52018-02-02 15:31:55 -0800106 Return<void> sendEvent_1_1(
107 ::android::hardware::nfc::V1_1::NfcEvent event,
108 ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
109 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
110 return Void();
111 };
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800112 Return<void> sendEvent(
113 ::android::hardware::nfc::V1_0::NfcEvent event,
114 ::android::hardware::nfc::V1_0::NfcStatus event_status) override {
115 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)event_status);
116 return Void();
117 };
118 Return<void> sendData(
119 const ::android::hardware::nfc::V1_0::NfcData& data) override {
120 ::android::hardware::nfc::V1_0::NfcData copy = data;
121 mDataCallback(copy.size(), &copy[0]);
122 return Void();
123 };
124
125 private:
126 tHAL_NFC_CBACK* mEventCallback;
127 tHAL_NFC_DATA_CBACK* mDataCallback;
Martijn Coenene9e48e52016-09-06 16:05:25 +0200128};
129
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800130/*******************************************************************************
131**
132** Function: NfcAdaptation::NfcAdaptation()
133**
134** Description: class constructor
135**
136** Returns: none
137**
138*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800139NfcAdaptation::NfcAdaptation() {
140 memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800141}
142
143/*******************************************************************************
144**
145** Function: NfcAdaptation::~NfcAdaptation()
146**
147** Description: class destructor
148**
149** Returns: none
150**
151*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800152NfcAdaptation::~NfcAdaptation() { mpInstance = NULL; }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800153
154/*******************************************************************************
155**
156** Function: NfcAdaptation::GetInstance()
157**
158** Description: access class singleton
159**
160** Returns: pointer to the singleton object
161**
162*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800163NfcAdaptation& NfcAdaptation::GetInstance() {
164 AutoThreadMutex a(sLock);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800165
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -0800166 if (!mpInstance) {
167 mpInstance = new NfcAdaptation;
168 mpInstance->InitializeHalDeviceContext();
169 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800170 return *mpInstance;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800171}
172
Ruchi Kandoif4064152018-02-28 16:27:41 -0800173void NfcAdaptation::GetVendorConfigs(
174 std::map<std::string, ConfigValue>& configMap) {
175 if (mHal_1_1) {
176 mHal_1_1->getConfig([&configMap](NfcVendorConfig config) {
177 std::vector<uint8_t> nfaPropCfg = {
178 config.nfaProprietaryCfg.protocol18092Active,
179 config.nfaProprietaryCfg.protocolBPrime,
180 config.nfaProprietaryCfg.protocolDual,
181 config.nfaProprietaryCfg.protocol15693,
182 config.nfaProprietaryCfg.protocolKovio,
183 config.nfaProprietaryCfg.protocolMifare,
184 config.nfaProprietaryCfg.discoveryPollKovio,
185 config.nfaProprietaryCfg.discoveryPollBPrime,
186 config.nfaProprietaryCfg.discoveryListenBPrime};
187 configMap.emplace(NAME_NFA_PROPRIETARY_CFG, ConfigValue(nfaPropCfg));
188 configMap.emplace(NAME_NFA_POLL_BAIL_OUT_MODE,
189 ConfigValue(config.nfaPollBailOutMode ? 1 : 0));
190 configMap.emplace(NAME_DEFAULT_OFFHOST_ROUTE,
191 ConfigValue(config.defaultOffHostRoute));
192 configMap.emplace(NAME_DEFAULT_NFCF_ROUTE,
193 ConfigValue(config.defaultOffHostRouteFelica));
194 configMap.emplace(NAME_DEFAULT_SYS_CODE_ROUTE,
195 ConfigValue(config.defaultSystemCodeRoute));
Ruchi Kandoice478122018-04-02 17:37:57 -0700196 configMap.emplace(NAME_DEFAULT_SYS_CODE_PWR_STATE,
197 ConfigValue(config.defaultSystemCodePowerState));
Ruchi Kandoif4064152018-02-28 16:27:41 -0800198 configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
199 ConfigValue(config.offHostSIMPipeId));
200 configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
201 ConfigValue(config.offHostESEPipeId));
202 configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
203 ConfigValue(config.maxIsoDepTransceiveLength));
204 /* For Backwards compatibility */
205 if (config.presenceCheckAlgorithm ==
206 PresenceCheckAlgorithm::ISO_DEP_NAK) {
207 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
208 ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
209 } else {
210 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
211 ConfigValue((uint32_t)config.presenceCheckAlgorithm));
212 }
213 });
214 }
215}
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800216/*******************************************************************************
217**
218** Function: NfcAdaptation::Initialize()
219**
220** Description: class initializer
221**
222** Returns: none
223**
224*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800225void NfcAdaptation::Initialize() {
226 const char* func = "NfcAdaptation::Initialize";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700227 const char* argv[] = {"libnfc_nci"};
228 // Init log tag
229 base::CommandLine::Init(1, argv);
230
231 // Android already logs thread_id, proc_id, timestamp, so disable those.
232 logging::SetLogItems(false, false, false, false);
233
234 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800235
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800236 nfc_storage_path = NfcConfig::getString(NAME_NFA_STORAGE, "/data/nfc");
237
238 if (NfcConfig::hasKey(NAME_NFA_DM_CFG)) {
239 std::vector<uint8_t> dm_config = NfcConfig::getBytes(NAME_NFA_DM_CFG);
240 if (dm_config.size() > 0) nfa_dm_cfg.auto_detect_ndef = dm_config[0];
241 if (dm_config.size() > 1) nfa_dm_cfg.auto_read_ndef = dm_config[1];
242 if (dm_config.size() > 2) nfa_dm_cfg.auto_presence_check = dm_config[2];
243 if (dm_config.size() > 3) nfa_dm_cfg.presence_check_option = dm_config[3];
244 // NOTE: The timeout value is not configurable here because the endianess
245 // of a byte array is ambiguous and needlessly difficult to configure.
246 // If this value needs to be configgurable, a numeric config option should
247 // be used.
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800248 }
Andres Moralese4ecc7d2014-10-01 17:46:04 -0700249
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800250 if (NfcConfig::hasKey(NAME_NFA_MAX_EE_SUPPORTED)) {
251 nfa_ee_max_ee_cfg = NfcConfig::getUnsigned(NAME_NFA_MAX_EE_SUPPORTED);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700252 DLOG_IF(INFO, nfc_debug_enabled)
253 << StringPrintf("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d",
254 func, nfa_ee_max_ee_cfg);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800255 }
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800256
257 if (NfcConfig::hasKey(NAME_NFA_POLL_BAIL_OUT_MODE)) {
258 nfa_poll_bail_out_mode =
259 NfcConfig::getUnsigned(NAME_NFA_POLL_BAIL_OUT_MODE);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700260 DLOG_IF(INFO, nfc_debug_enabled)
261 << StringPrintf("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func,
262 nfa_poll_bail_out_mode);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800263 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700264
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800265 if (NfcConfig::hasKey(NAME_NFA_PROPRIETARY_CFG)) {
266 std::vector<uint8_t> p_config =
267 NfcConfig::getBytes(NAME_NFA_PROPRIETARY_CFG);
268 if (p_config.size() > 0)
269 nfa_proprietary_cfg.pro_protocol_18092_active = p_config[0];
270 if (p_config.size() > 1)
271 nfa_proprietary_cfg.pro_protocol_b_prime = p_config[1];
272 if (p_config.size() > 2)
273 nfa_proprietary_cfg.pro_protocol_dual = p_config[2];
274 if (p_config.size() > 3)
275 nfa_proprietary_cfg.pro_protocol_15693 = p_config[3];
276 if (p_config.size() > 4)
277 nfa_proprietary_cfg.pro_protocol_kovio = p_config[4];
278 if (p_config.size() > 5) nfa_proprietary_cfg.pro_protocol_mfc = p_config[5];
279 if (p_config.size() > 6)
280 nfa_proprietary_cfg.pro_discovery_kovio_poll = p_config[6];
281 if (p_config.size() > 7)
282 nfa_proprietary_cfg.pro_discovery_b_prime_poll = p_config[7];
283 if (p_config.size() > 8)
284 nfa_proprietary_cfg.pro_discovery_b_prime_listen = p_config[8];
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800285 }
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700286
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800287 // Configure whitelist of HCI host ID's
288 // See specification: ETSI TS 102 622, section 6.1.3.1
289 if (NfcConfig::hasKey(NAME_DEVICE_HOST_WHITE_LIST)) {
290 host_whitelist = NfcConfig::getBytes(NAME_DEVICE_HOST_WHITE_LIST);
291 nfa_hci_cfg.num_whitelist_host = host_whitelist.size();
292 nfa_hci_cfg.p_whitelist = &host_whitelist[0];
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800293 }
Evan Chub7cb5672014-02-07 15:01:21 -0500294
Andre Eisenbach8a4edf62017-11-20 14:51:11 -0800295 initializeGlobalDebugEnabledFlag();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800296
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800297 verify_stack_non_volatile_store();
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800298 if (NfcConfig::hasKey(NAME_PRESERVE_STORAGE) &&
299 NfcConfig::getUnsigned(NAME_PRESERVE_STORAGE) == 1) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700300 DLOG_IF(INFO, nfc_debug_enabled)
301 << StringPrintf("%s: preserve stack NV store", __func__);
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800302 } else {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800303 delete_stack_non_volatile_store(FALSE);
304 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700305
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800306 GKI_init();
307 GKI_enable();
308 GKI_create_task((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", 0, 0,
309 (pthread_cond_t*)NULL, NULL);
310 {
311 AutoThreadMutex guard(mCondVar);
312 GKI_create_task((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", 0, 0,
313 (pthread_cond_t*)NULL, NULL);
314 mCondVar.wait();
315 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800316
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700317 debug_nfcsnoop_init();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700318 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800319}
320
321/*******************************************************************************
322**
323** Function: NfcAdaptation::Finalize()
324**
325** Description: class finalizer
326**
327** Returns: none
328**
329*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800330void NfcAdaptation::Finalize() {
331 const char* func = "NfcAdaptation::Finalize";
332 AutoThreadMutex a(sLock);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800333
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700334 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800335 GKI_shutdown();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800336
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800337 NfcConfig::clear();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800338
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700339 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800340 delete this;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800341}
342
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -0800343void NfcAdaptation::FactoryReset() {
344 if (mHal_1_1 != nullptr) {
345 mHal_1_1->factoryReset();
346 }
347}
348
Ruchi Kandoi40da4372017-12-07 15:19:30 -0800349void NfcAdaptation::DeviceShutdown() {
350 if (mHal_1_1 != nullptr) {
351 mHal_1_1->closeForPowerOffCase();
352 }
353}
354
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800355/*******************************************************************************
356**
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700357** Function: NfcAdaptation::Dump
358**
359** Description: Native support for dumpsys function.
360**
361** Returns: None.
362**
363*******************************************************************************/
364void NfcAdaptation::Dump(int fd) { debug_nfcsnoop_dump(fd); }
365
366/*******************************************************************************
367**
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800368** Function: NfcAdaptation::signal()
369**
370** Description: signal the CondVar to release the thread that is waiting
371**
372** Returns: none
373**
374*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800375void NfcAdaptation::signal() { mCondVar.signal(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800376
377/*******************************************************************************
378**
379** Function: NfcAdaptation::NFCA_TASK()
380**
381** Description: NFCA_TASK runs the GKI main task
382**
383** Returns: none
384**
385*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700386uint32_t NfcAdaptation::NFCA_TASK(__attribute__((unused)) uint32_t arg) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800387 const char* func = "NfcAdaptation::NFCA_TASK";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700388 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800389 GKI_run(0);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700390 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800391 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800392}
393
394/*******************************************************************************
395**
396** Function: NfcAdaptation::Thread()
397**
398** Description: Creates work threads
399**
400** Returns: none
401**
402*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700403uint32_t NfcAdaptation::Thread(__attribute__((unused)) uint32_t arg) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800404 const char* func = "NfcAdaptation::Thread";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700405 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800406
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800407 {
408 ThreadCondVar CondVar;
409 AutoThreadMutex guard(CondVar);
410 GKI_create_task((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", 0, 0,
411 (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
412 CondVar.wait();
413 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800414
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800415 NfcAdaptation::GetInstance().signal();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800416
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800417 GKI_exit_task(GKI_get_taskid());
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700418 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800419 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800420}
421
422/*******************************************************************************
423**
424** Function: NfcAdaptation::GetHalEntryFuncs()
425**
426** Description: Get the set of HAL entry points.
427**
428** Returns: Functions pointers for HAL entry points.
429**
430*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800431tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs() { return &mHalEntryFuncs; }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800432
433/*******************************************************************************
434**
435** Function: NfcAdaptation::InitializeHalDeviceContext
436**
437** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
438**
439** Returns: None.
440**
441*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800442void NfcAdaptation::InitializeHalDeviceContext() {
443 const char* func = "NfcAdaptation::InitializeHalDeviceContext";
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800444
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800445 mHalEntryFuncs.initialize = HalInitialize;
446 mHalEntryFuncs.terminate = HalTerminate;
447 mHalEntryFuncs.open = HalOpen;
448 mHalEntryFuncs.close = HalClose;
449 mHalEntryFuncs.core_initialized = HalCoreInitialized;
450 mHalEntryFuncs.write = HalWrite;
451 mHalEntryFuncs.prediscover = HalPrediscover;
452 mHalEntryFuncs.control_granted = HalControlGranted;
453 mHalEntryFuncs.power_cycle = HalPowerCycle;
454 mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700455 LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -0800456 mHal = mHal_1_1 = INfcV1_1::getService();
457 if (mHal_1_1 == nullptr) {
458 mHal = INfc::getService();
459 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800460 LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!");
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700461 LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
462 mHal.get(),
463 (mHal->isRemote() ? "remote" : "local"));
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800464}
465
466/*******************************************************************************
467**
468** Function: NfcAdaptation::HalInitialize
469**
470** Description: Not implemented because this function is only needed
471** within the HAL.
472**
473** Returns: None.
474**
475*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800476void NfcAdaptation::HalInitialize() {
477 const char* func = "NfcAdaptation::HalInitialize";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700478 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800479}
480
481/*******************************************************************************
482**
483** Function: NfcAdaptation::HalTerminate
484**
485** Description: Not implemented because this function is only needed
486** within the HAL.
487**
488** Returns: None.
489**
490*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800491void NfcAdaptation::HalTerminate() {
492 const char* func = "NfcAdaptation::HalTerminate";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700493 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800494}
495
496/*******************************************************************************
497**
498** Function: NfcAdaptation::HalOpen
499**
500** Description: Turn on controller, download firmware.
501**
502** Returns: None.
503**
504*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800505void NfcAdaptation::HalOpen(tHAL_NFC_CBACK* p_hal_cback,
506 tHAL_NFC_DATA_CBACK* p_data_cback) {
507 const char* func = "NfcAdaptation::HalOpen";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700508 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800509 mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
Ruchi Kandoi267fdf52018-02-02 15:31:55 -0800510 if (mHal_1_1 != nullptr) {
511 mHal_1_1->open_1_1(mCallback);
512 } else {
513 mHal->open(mCallback);
514 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800515}
516
517/*******************************************************************************
518**
519** Function: NfcAdaptation::HalClose
520**
521** Description: Turn off controller.
522**
523** Returns: None.
524**
525*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800526void NfcAdaptation::HalClose() {
527 const char* func = "NfcAdaptation::HalClose";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700528 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800529 mHal->close();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800530}
531
532/*******************************************************************************
533**
534** Function: NfcAdaptation::HalDeviceContextCallback
535**
536** Description: Translate generic Android HAL's callback into Broadcom-specific
537** callback function.
538**
539** Returns: None.
540**
541*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800542void NfcAdaptation::HalDeviceContextCallback(nfc_event_t event,
543 nfc_status_t event_status) {
544 const char* func = "NfcAdaptation::HalDeviceContextCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700545 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: event=%u", func, event);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800546 if (mHalCallback) mHalCallback(event, (tHAL_NFC_STATUS)event_status);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800547}
548
549/*******************************************************************************
550**
551** Function: NfcAdaptation::HalDeviceContextDataCallback
552**
553** Description: Translate generic Android HAL's callback into Broadcom-specific
554** callback function.
555**
556** Returns: None.
557**
558*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800559void NfcAdaptation::HalDeviceContextDataCallback(uint16_t data_len,
560 uint8_t* p_data) {
561 const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700562 DLOG_IF(INFO, nfc_debug_enabled)
563 << StringPrintf("%s: len=%u", func, data_len);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800564 if (mHalDataCallback) mHalDataCallback(data_len, p_data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800565}
566
567/*******************************************************************************
568**
569** Function: NfcAdaptation::HalWrite
570**
571** Description: Write NCI message to the controller.
572**
573** Returns: None.
574**
575*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800576void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
577 const char* func = "NfcAdaptation::HalWrite";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700578 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800579 ::android::hardware::nfc::V1_0::NfcData data;
580 data.setToExternal(p_data, data_len);
581 mHal->write(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800582}
583
584/*******************************************************************************
585**
586** Function: NfcAdaptation::HalCoreInitialized
587**
588** Description: Adjust the configurable parameters in the controller.
589**
590** Returns: None.
591**
592*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800593void NfcAdaptation::HalCoreInitialized(uint16_t data_len,
594 uint8_t* p_core_init_rsp_params) {
595 const char* func = "NfcAdaptation::HalCoreInitialized";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700596 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800597 hidl_vec<uint8_t> data;
598 data.setToExternal(p_core_init_rsp_params, data_len);
Martijn Coenene9e48e52016-09-06 16:05:25 +0200599
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800600 mHal->coreInitialized(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800601}
602
603/*******************************************************************************
604**
605** Function: NfcAdaptation::HalPrediscover
606**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800607** Description: Perform any vendor-specific pre-discovery actions (if
608** needed) If any actions were performed TRUE will be returned,
609** and HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800610** completed.
611**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800612** Returns: TRUE if vendor-specific pre-discovery actions initialized
613** FALSE if no vendor-specific pre-discovery actions are
614** needed.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800615**
616*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800617bool NfcAdaptation::HalPrediscover() {
618 const char* func = "NfcAdaptation::HalPrediscover";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700619 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800620 bool retval = FALSE;
621 mHal->prediscover();
622 return retval;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800623}
624
625/*******************************************************************************
626**
627** Function: HAL_NfcControlGranted
628**
629** Description: Grant control to HAL control for sending NCI commands.
630** Call in response to HAL_REQUEST_CONTROL_EVT.
631** Must only be called when there are no NCI commands pending.
632** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
633** needs control of NCI.
634**
635** Returns: void
636**
637*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800638void NfcAdaptation::HalControlGranted() {
639 const char* func = "NfcAdaptation::HalControlGranted";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700640 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800641 mHal->controlGranted();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800642}
643
644/*******************************************************************************
645**
646** Function: NfcAdaptation::HalPowerCycle
647**
648** Description: Turn off and turn on the controller.
649**
650** Returns: None.
651**
652*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800653void NfcAdaptation::HalPowerCycle() {
654 const char* func = "NfcAdaptation::HalPowerCycle";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700655 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800656 mHal->powerCycle();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800657}
658
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400659/*******************************************************************************
660**
661** Function: NfcAdaptation::HalGetMaxNfcee
662**
663** Description: Turn off and turn on the controller.
664**
665** Returns: None.
666**
667*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800668uint8_t NfcAdaptation::HalGetMaxNfcee() {
669 const char* func = "NfcAdaptation::HalPowerCycle";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700670 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Evan Chua24be4f2013-11-13 15:30:16 -0500671
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800672 return nfa_ee_max_ee_cfg;
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400673}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700674
675/*******************************************************************************
676**
677** Function: NfcAdaptation::DownloadFirmware
678**
679** Description: Download firmware patch files.
680**
681** Returns: None.
682**
683*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800684void NfcAdaptation::DownloadFirmware() {
685 const char* func = "NfcAdaptation::DownloadFirmware";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700686 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800687 HalInitialize();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700688
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800689 mHalOpenCompletedEvent.lock();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700690 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try open HAL", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800691 HalOpen(HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
692 mHalOpenCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700693
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800694 mHalCloseCompletedEvent.lock();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700695 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try close HAL", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800696 HalClose();
697 mHalCloseCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700698
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800699 HalTerminate();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700700 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700701}
702
703/*******************************************************************************
704**
705** Function: NfcAdaptation::HalDownloadFirmwareCallback
706**
707** Description: Receive events from the HAL.
708**
709** Returns: None.
710**
711*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800712void NfcAdaptation::HalDownloadFirmwareCallback(nfc_event_t event,
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700713 __attribute__((unused))
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800714 nfc_status_t event_status) {
715 const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700716 DLOG_IF(INFO, nfc_debug_enabled)
717 << StringPrintf("%s: event=0x%X", func, event);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800718 switch (event) {
719 case HAL_NFC_OPEN_CPLT_EVT: {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700720 DLOG_IF(INFO, nfc_debug_enabled)
721 << StringPrintf("%s: HAL_NFC_OPEN_CPLT_EVT", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800722 mHalOpenCompletedEvent.signal();
723 break;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700724 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800725 case HAL_NFC_CLOSE_CPLT_EVT: {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700726 DLOG_IF(INFO, nfc_debug_enabled)
727 << StringPrintf("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800728 mHalCloseCompletedEvent.signal();
729 break;
730 }
731 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700732}
733
734/*******************************************************************************
735**
736** Function: NfcAdaptation::HalDownloadFirmwareDataCallback
737**
738** Description: Receive data events from the HAL.
739**
740** Returns: None.
741**
742*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700743void NfcAdaptation::HalDownloadFirmwareDataCallback(__attribute__((unused))
744 uint16_t data_len,
745 __attribute__((unused))
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800746 uint8_t* p_data) {}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700747
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800748/*******************************************************************************
749**
750** Function: ThreadMutex::ThreadMutex()
751**
752** Description: class constructor
753**
754** Returns: none
755**
756*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800757ThreadMutex::ThreadMutex() {
758 pthread_mutexattr_t mutexAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800759
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800760 pthread_mutexattr_init(&mutexAttr);
761 pthread_mutex_init(&mMutex, &mutexAttr);
762 pthread_mutexattr_destroy(&mutexAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800763}
764
765/*******************************************************************************
766**
767** Function: ThreadMutex::~ThreadMutex()
768**
769** Description: class destructor
770**
771** Returns: none
772**
773*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800774ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800775
776/*******************************************************************************
777**
778** Function: ThreadMutex::lock()
779**
780** Description: lock kthe mutex
781**
782** Returns: none
783**
784*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800785void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800786
787/*******************************************************************************
788**
789** Function: ThreadMutex::unblock()
790**
791** Description: unlock the mutex
792**
793** Returns: none
794**
795*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800796void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800797
798/*******************************************************************************
799**
800** Function: ThreadCondVar::ThreadCondVar()
801**
802** Description: class constructor
803**
804** Returns: none
805**
806*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800807ThreadCondVar::ThreadCondVar() {
808 pthread_condattr_t CondAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800809
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800810 pthread_condattr_init(&CondAttr);
811 pthread_cond_init(&mCondVar, &CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800812
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800813 pthread_condattr_destroy(&CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800814}
815
816/*******************************************************************************
817**
818** Function: ThreadCondVar::~ThreadCondVar()
819**
820** Description: class destructor
821**
822** Returns: none
823**
824*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800825ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800826
827/*******************************************************************************
828**
829** Function: ThreadCondVar::wait()
830**
831** Description: wait on the mCondVar
832**
833** Returns: none
834**
835*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800836void ThreadCondVar::wait() {
837 pthread_cond_wait(&mCondVar, *this);
838 pthread_mutex_unlock(*this);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800839}
840
841/*******************************************************************************
842**
843** Function: ThreadCondVar::signal()
844**
845** Description: signal the mCondVar
846**
847** Returns: none
848**
849*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800850void ThreadCondVar::signal() {
851 AutoThreadMutex a(*this);
852 pthread_cond_signal(&mCondVar);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800853}
854
855/*******************************************************************************
856**
857** Function: AutoThreadMutex::AutoThreadMutex()
858**
859** Description: class constructor, automatically lock the mutex
860**
861** Returns: none
862**
863*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800864AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800865
866/*******************************************************************************
867**
868** Function: AutoThreadMutex::~AutoThreadMutex()
869**
870** Description: class destructor, automatically unlock the mutex
871**
872** Returns: none
873**
874*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800875AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }