blob: 528bdd781ffe3167cb2ffcba136f0d6bb6f96300 [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));
Love Khannab69e2052018-04-06 22:41:39 +0530192 configMap.emplace(NAME_DEFAULT_ROUTE, ConfigValue(config.defaultRoute));
Ruchi Kandoif4064152018-02-28 16:27:41 -0800193 configMap.emplace(NAME_DEFAULT_NFCF_ROUTE,
194 ConfigValue(config.defaultOffHostRouteFelica));
195 configMap.emplace(NAME_DEFAULT_SYS_CODE_ROUTE,
196 ConfigValue(config.defaultSystemCodeRoute));
Ruchi Kandoice478122018-04-02 17:37:57 -0700197 configMap.emplace(NAME_DEFAULT_SYS_CODE_PWR_STATE,
198 ConfigValue(config.defaultSystemCodePowerState));
Ruchi Kandoif4064152018-02-28 16:27:41 -0800199 configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
200 ConfigValue(config.offHostSIMPipeId));
201 configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
202 ConfigValue(config.offHostESEPipeId));
203 configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
204 ConfigValue(config.maxIsoDepTransceiveLength));
Love Khannab69e2052018-04-06 22:41:39 +0530205 if (config.hostWhitelist.size() != 0) {
206 configMap.emplace(NAME_DEVICE_HOST_WHITE_LIST,
207 ConfigValue(config.hostWhitelist));
208 }
Ruchi Kandoif4064152018-02-28 16:27:41 -0800209 /* For Backwards compatibility */
210 if (config.presenceCheckAlgorithm ==
211 PresenceCheckAlgorithm::ISO_DEP_NAK) {
212 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
213 ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
214 } else {
215 configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
216 ConfigValue((uint32_t)config.presenceCheckAlgorithm));
217 }
218 });
219 }
220}
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800221/*******************************************************************************
222**
223** Function: NfcAdaptation::Initialize()
224**
225** Description: class initializer
226**
227** Returns: none
228**
229*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800230void NfcAdaptation::Initialize() {
231 const char* func = "NfcAdaptation::Initialize";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700232 const char* argv[] = {"libnfc_nci"};
233 // Init log tag
234 base::CommandLine::Init(1, argv);
235
236 // Android already logs thread_id, proc_id, timestamp, so disable those.
237 logging::SetLogItems(false, false, false, false);
238
239 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800240
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800241 nfc_storage_path = NfcConfig::getString(NAME_NFA_STORAGE, "/data/nfc");
242
243 if (NfcConfig::hasKey(NAME_NFA_DM_CFG)) {
244 std::vector<uint8_t> dm_config = NfcConfig::getBytes(NAME_NFA_DM_CFG);
245 if (dm_config.size() > 0) nfa_dm_cfg.auto_detect_ndef = dm_config[0];
246 if (dm_config.size() > 1) nfa_dm_cfg.auto_read_ndef = dm_config[1];
247 if (dm_config.size() > 2) nfa_dm_cfg.auto_presence_check = dm_config[2];
248 if (dm_config.size() > 3) nfa_dm_cfg.presence_check_option = dm_config[3];
249 // NOTE: The timeout value is not configurable here because the endianess
250 // of a byte array is ambiguous and needlessly difficult to configure.
251 // If this value needs to be configgurable, a numeric config option should
252 // be used.
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800253 }
Andres Moralese4ecc7d2014-10-01 17:46:04 -0700254
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800255 if (NfcConfig::hasKey(NAME_NFA_MAX_EE_SUPPORTED)) {
256 nfa_ee_max_ee_cfg = NfcConfig::getUnsigned(NAME_NFA_MAX_EE_SUPPORTED);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700257 DLOG_IF(INFO, nfc_debug_enabled)
258 << StringPrintf("%s: Overriding NFA_EE_MAX_EE_SUPPORTED to use %d",
259 func, nfa_ee_max_ee_cfg);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800260 }
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800261
262 if (NfcConfig::hasKey(NAME_NFA_POLL_BAIL_OUT_MODE)) {
263 nfa_poll_bail_out_mode =
264 NfcConfig::getUnsigned(NAME_NFA_POLL_BAIL_OUT_MODE);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700265 DLOG_IF(INFO, nfc_debug_enabled)
266 << StringPrintf("%s: Overriding NFA_POLL_BAIL_OUT_MODE to use %d", func,
267 nfa_poll_bail_out_mode);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800268 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700269
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800270 if (NfcConfig::hasKey(NAME_NFA_PROPRIETARY_CFG)) {
271 std::vector<uint8_t> p_config =
272 NfcConfig::getBytes(NAME_NFA_PROPRIETARY_CFG);
273 if (p_config.size() > 0)
274 nfa_proprietary_cfg.pro_protocol_18092_active = p_config[0];
275 if (p_config.size() > 1)
276 nfa_proprietary_cfg.pro_protocol_b_prime = p_config[1];
277 if (p_config.size() > 2)
278 nfa_proprietary_cfg.pro_protocol_dual = p_config[2];
279 if (p_config.size() > 3)
280 nfa_proprietary_cfg.pro_protocol_15693 = p_config[3];
281 if (p_config.size() > 4)
282 nfa_proprietary_cfg.pro_protocol_kovio = p_config[4];
283 if (p_config.size() > 5) nfa_proprietary_cfg.pro_protocol_mfc = p_config[5];
284 if (p_config.size() > 6)
285 nfa_proprietary_cfg.pro_discovery_kovio_poll = p_config[6];
286 if (p_config.size() > 7)
287 nfa_proprietary_cfg.pro_discovery_b_prime_poll = p_config[7];
288 if (p_config.size() > 8)
289 nfa_proprietary_cfg.pro_discovery_b_prime_listen = p_config[8];
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800290 }
Jizhou Liaoacf1ec02015-06-30 10:25:41 -0700291
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800292 // Configure whitelist of HCI host ID's
293 // See specification: ETSI TS 102 622, section 6.1.3.1
294 if (NfcConfig::hasKey(NAME_DEVICE_HOST_WHITE_LIST)) {
295 host_whitelist = NfcConfig::getBytes(NAME_DEVICE_HOST_WHITE_LIST);
296 nfa_hci_cfg.num_whitelist_host = host_whitelist.size();
297 nfa_hci_cfg.p_whitelist = &host_whitelist[0];
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800298 }
Evan Chub7cb5672014-02-07 15:01:21 -0500299
Andre Eisenbach8a4edf62017-11-20 14:51:11 -0800300 initializeGlobalDebugEnabledFlag();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800301
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800302 verify_stack_non_volatile_store();
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800303 if (NfcConfig::hasKey(NAME_PRESERVE_STORAGE) &&
304 NfcConfig::getUnsigned(NAME_PRESERVE_STORAGE) == 1) {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700305 DLOG_IF(INFO, nfc_debug_enabled)
306 << StringPrintf("%s: preserve stack NV store", __func__);
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800307 } else {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800308 delete_stack_non_volatile_store(FALSE);
309 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700310
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800311 GKI_init();
312 GKI_enable();
313 GKI_create_task((TASKPTR)NFCA_TASK, BTU_TASK, (int8_t*)"NFCA_TASK", 0, 0,
314 (pthread_cond_t*)NULL, NULL);
315 {
316 AutoThreadMutex guard(mCondVar);
317 GKI_create_task((TASKPTR)Thread, MMI_TASK, (int8_t*)"NFCA_THREAD", 0, 0,
318 (pthread_cond_t*)NULL, NULL);
319 mCondVar.wait();
320 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800321
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700322 debug_nfcsnoop_init();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700323 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800324}
325
326/*******************************************************************************
327**
328** Function: NfcAdaptation::Finalize()
329**
330** Description: class finalizer
331**
332** Returns: none
333**
334*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800335void NfcAdaptation::Finalize() {
336 const char* func = "NfcAdaptation::Finalize";
337 AutoThreadMutex a(sLock);
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: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800340 GKI_shutdown();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800341
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800342 NfcConfig::clear();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800343
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700344 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800345 delete this;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800346}
347
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -0800348void NfcAdaptation::FactoryReset() {
349 if (mHal_1_1 != nullptr) {
350 mHal_1_1->factoryReset();
351 }
352}
353
Ruchi Kandoi40da4372017-12-07 15:19:30 -0800354void NfcAdaptation::DeviceShutdown() {
355 if (mHal_1_1 != nullptr) {
356 mHal_1_1->closeForPowerOffCase();
357 }
358}
359
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800360/*******************************************************************************
361**
Ruchi Kandoic0cd1ab2017-04-17 17:23:56 -0700362** Function: NfcAdaptation::Dump
363**
364** Description: Native support for dumpsys function.
365**
366** Returns: None.
367**
368*******************************************************************************/
369void NfcAdaptation::Dump(int fd) { debug_nfcsnoop_dump(fd); }
370
371/*******************************************************************************
372**
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800373** Function: NfcAdaptation::signal()
374**
375** Description: signal the CondVar to release the thread that is waiting
376**
377** Returns: none
378**
379*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800380void NfcAdaptation::signal() { mCondVar.signal(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800381
382/*******************************************************************************
383**
384** Function: NfcAdaptation::NFCA_TASK()
385**
386** Description: NFCA_TASK runs the GKI main task
387**
388** Returns: none
389**
390*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700391uint32_t NfcAdaptation::NFCA_TASK(__attribute__((unused)) uint32_t arg) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800392 const char* func = "NfcAdaptation::NFCA_TASK";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700393 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800394 GKI_run(0);
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700395 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800396 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800397}
398
399/*******************************************************************************
400**
401** Function: NfcAdaptation::Thread()
402**
403** Description: Creates work threads
404**
405** Returns: none
406**
407*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700408uint32_t NfcAdaptation::Thread(__attribute__((unused)) uint32_t arg) {
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800409 const char* func = "NfcAdaptation::Thread";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700410 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800411
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800412 {
413 ThreadCondVar CondVar;
414 AutoThreadMutex guard(CondVar);
415 GKI_create_task((TASKPTR)nfc_task, NFC_TASK, (int8_t*)"NFC_TASK", 0, 0,
416 (pthread_cond_t*)CondVar, (pthread_mutex_t*)CondVar);
417 CondVar.wait();
418 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800419
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800420 NfcAdaptation::GetInstance().signal();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800421
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800422 GKI_exit_task(GKI_get_taskid());
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700423 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800424 return 0;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800425}
426
427/*******************************************************************************
428**
429** Function: NfcAdaptation::GetHalEntryFuncs()
430**
431** Description: Get the set of HAL entry points.
432**
433** Returns: Functions pointers for HAL entry points.
434**
435*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800436tHAL_NFC_ENTRY* NfcAdaptation::GetHalEntryFuncs() { return &mHalEntryFuncs; }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800437
438/*******************************************************************************
439**
440** Function: NfcAdaptation::InitializeHalDeviceContext
441**
442** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
443**
444** Returns: None.
445**
446*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800447void NfcAdaptation::InitializeHalDeviceContext() {
448 const char* func = "NfcAdaptation::InitializeHalDeviceContext";
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800449
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800450 mHalEntryFuncs.initialize = HalInitialize;
451 mHalEntryFuncs.terminate = HalTerminate;
452 mHalEntryFuncs.open = HalOpen;
453 mHalEntryFuncs.close = HalClose;
454 mHalEntryFuncs.core_initialized = HalCoreInitialized;
455 mHalEntryFuncs.write = HalWrite;
456 mHalEntryFuncs.prediscover = HalPrediscover;
457 mHalEntryFuncs.control_granted = HalControlGranted;
458 mHalEntryFuncs.power_cycle = HalPowerCycle;
459 mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700460 LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
Ruchi Kandoi5815b5c2017-12-07 09:30:01 -0800461 mHal = mHal_1_1 = INfcV1_1::getService();
462 if (mHal_1_1 == nullptr) {
463 mHal = INfc::getService();
464 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800465 LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!");
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700466 LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
467 mHal.get(),
468 (mHal->isRemote() ? "remote" : "local"));
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800469}
470
471/*******************************************************************************
472**
473** Function: NfcAdaptation::HalInitialize
474**
475** Description: Not implemented because this function is only needed
476** within the HAL.
477**
478** Returns: None.
479**
480*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800481void NfcAdaptation::HalInitialize() {
482 const char* func = "NfcAdaptation::HalInitialize";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700483 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800484}
485
486/*******************************************************************************
487**
488** Function: NfcAdaptation::HalTerminate
489**
490** Description: Not implemented because this function is only needed
491** within the HAL.
492**
493** Returns: None.
494**
495*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800496void NfcAdaptation::HalTerminate() {
497 const char* func = "NfcAdaptation::HalTerminate";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700498 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800499}
500
501/*******************************************************************************
502**
503** Function: NfcAdaptation::HalOpen
504**
505** Description: Turn on controller, download firmware.
506**
507** Returns: None.
508**
509*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800510void NfcAdaptation::HalOpen(tHAL_NFC_CBACK* p_hal_cback,
511 tHAL_NFC_DATA_CBACK* p_data_cback) {
512 const char* func = "NfcAdaptation::HalOpen";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700513 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800514 mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
Ruchi Kandoi267fdf52018-02-02 15:31:55 -0800515 if (mHal_1_1 != nullptr) {
516 mHal_1_1->open_1_1(mCallback);
517 } else {
518 mHal->open(mCallback);
519 }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800520}
521
522/*******************************************************************************
523**
524** Function: NfcAdaptation::HalClose
525**
526** Description: Turn off controller.
527**
528** Returns: None.
529**
530*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800531void NfcAdaptation::HalClose() {
532 const char* func = "NfcAdaptation::HalClose";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700533 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800534 mHal->close();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800535}
536
537/*******************************************************************************
538**
539** Function: NfcAdaptation::HalDeviceContextCallback
540**
541** Description: Translate generic Android HAL's callback into Broadcom-specific
542** callback function.
543**
544** Returns: None.
545**
546*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800547void NfcAdaptation::HalDeviceContextCallback(nfc_event_t event,
548 nfc_status_t event_status) {
549 const char* func = "NfcAdaptation::HalDeviceContextCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700550 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: event=%u", func, event);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800551 if (mHalCallback) mHalCallback(event, (tHAL_NFC_STATUS)event_status);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800552}
553
554/*******************************************************************************
555**
556** Function: NfcAdaptation::HalDeviceContextDataCallback
557**
558** Description: Translate generic Android HAL's callback into Broadcom-specific
559** callback function.
560**
561** Returns: None.
562**
563*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800564void NfcAdaptation::HalDeviceContextDataCallback(uint16_t data_len,
565 uint8_t* p_data) {
566 const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700567 DLOG_IF(INFO, nfc_debug_enabled)
568 << StringPrintf("%s: len=%u", func, data_len);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800569 if (mHalDataCallback) mHalDataCallback(data_len, p_data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800570}
571
572/*******************************************************************************
573**
574** Function: NfcAdaptation::HalWrite
575**
576** Description: Write NCI message to the controller.
577**
578** Returns: None.
579**
580*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800581void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
582 const char* func = "NfcAdaptation::HalWrite";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700583 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800584 ::android::hardware::nfc::V1_0::NfcData data;
585 data.setToExternal(p_data, data_len);
586 mHal->write(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800587}
588
589/*******************************************************************************
590**
591** Function: NfcAdaptation::HalCoreInitialized
592**
593** Description: Adjust the configurable parameters in the controller.
594**
595** Returns: None.
596**
597*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800598void NfcAdaptation::HalCoreInitialized(uint16_t data_len,
599 uint8_t* p_core_init_rsp_params) {
600 const char* func = "NfcAdaptation::HalCoreInitialized";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700601 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800602 hidl_vec<uint8_t> data;
603 data.setToExternal(p_core_init_rsp_params, data_len);
Martijn Coenene9e48e52016-09-06 16:05:25 +0200604
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800605 mHal->coreInitialized(data);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800606}
607
608/*******************************************************************************
609**
610** Function: NfcAdaptation::HalPrediscover
611**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800612** Description: Perform any vendor-specific pre-discovery actions (if
613** needed) If any actions were performed TRUE will be returned,
614** and HAL_PRE_DISCOVER_CPLT_EVT will notify when actions are
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800615** completed.
616**
Ruchi Kandoi552f2b72017-01-28 16:22:55 -0800617** Returns: TRUE if vendor-specific pre-discovery actions initialized
618** FALSE if no vendor-specific pre-discovery actions are
619** needed.
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800620**
621*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800622bool NfcAdaptation::HalPrediscover() {
623 const char* func = "NfcAdaptation::HalPrediscover";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700624 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800625 bool retval = FALSE;
626 mHal->prediscover();
627 return retval;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800628}
629
630/*******************************************************************************
631**
632** Function: HAL_NfcControlGranted
633**
634** Description: Grant control to HAL control for sending NCI commands.
635** Call in response to HAL_REQUEST_CONTROL_EVT.
636** Must only be called when there are no NCI commands pending.
637** HAL_RELEASE_CONTROL_EVT will notify when HAL no longer
638** needs control of NCI.
639**
640** Returns: void
641**
642*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800643void NfcAdaptation::HalControlGranted() {
644 const char* func = "NfcAdaptation::HalControlGranted";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700645 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800646 mHal->controlGranted();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800647}
648
649/*******************************************************************************
650**
651** Function: NfcAdaptation::HalPowerCycle
652**
653** Description: Turn off and turn on the controller.
654**
655** Returns: None.
656**
657*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800658void NfcAdaptation::HalPowerCycle() {
659 const char* func = "NfcAdaptation::HalPowerCycle";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700660 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800661 mHal->powerCycle();
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800662}
663
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400664/*******************************************************************************
665**
666** Function: NfcAdaptation::HalGetMaxNfcee
667**
668** Description: Turn off and turn on the controller.
669**
670** Returns: None.
671**
672*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800673uint8_t NfcAdaptation::HalGetMaxNfcee() {
674 const char* func = "NfcAdaptation::HalPowerCycle";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700675 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
Evan Chua24be4f2013-11-13 15:30:16 -0500676
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800677 return nfa_ee_max_ee_cfg;
Paul Chaissonb20cbf32013-07-12 13:25:09 -0400678}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700679
680/*******************************************************************************
681**
682** Function: NfcAdaptation::DownloadFirmware
683**
684** Description: Download firmware patch files.
685**
686** Returns: None.
687**
688*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800689void NfcAdaptation::DownloadFirmware() {
690 const char* func = "NfcAdaptation::DownloadFirmware";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700691 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800692 HalInitialize();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700693
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800694 mHalOpenCompletedEvent.lock();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700695 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try open HAL", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800696 HalOpen(HalDownloadFirmwareCallback, HalDownloadFirmwareDataCallback);
697 mHalOpenCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700698
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800699 mHalCloseCompletedEvent.lock();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700700 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: try close HAL", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800701 HalClose();
702 mHalCloseCompletedEvent.wait();
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700703
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800704 HalTerminate();
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700705 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700706}
707
708/*******************************************************************************
709**
710** Function: NfcAdaptation::HalDownloadFirmwareCallback
711**
712** Description: Receive events from the HAL.
713**
714** Returns: None.
715**
716*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800717void NfcAdaptation::HalDownloadFirmwareCallback(nfc_event_t event,
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700718 __attribute__((unused))
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800719 nfc_status_t event_status) {
720 const char* func = "NfcAdaptation::HalDownloadFirmwareCallback";
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700721 DLOG_IF(INFO, nfc_debug_enabled)
722 << StringPrintf("%s: event=0x%X", func, event);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800723 switch (event) {
724 case HAL_NFC_OPEN_CPLT_EVT: {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700725 DLOG_IF(INFO, nfc_debug_enabled)
726 << StringPrintf("%s: HAL_NFC_OPEN_CPLT_EVT", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800727 mHalOpenCompletedEvent.signal();
728 break;
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700729 }
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800730 case HAL_NFC_CLOSE_CPLT_EVT: {
Ruchi Kandoi6767aec2017-09-26 09:46:26 -0700731 DLOG_IF(INFO, nfc_debug_enabled)
732 << StringPrintf("%s: HAL_NFC_CLOSE_CPLT_EVT", func);
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800733 mHalCloseCompletedEvent.signal();
734 break;
735 }
736 }
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700737}
738
739/*******************************************************************************
740**
741** Function: NfcAdaptation::HalDownloadFirmwareDataCallback
742**
743** Description: Receive data events from the HAL.
744**
745** Returns: None.
746**
747*******************************************************************************/
Ruchi Kandoi9ba6d242017-10-13 16:07:01 -0700748void NfcAdaptation::HalDownloadFirmwareDataCallback(__attribute__((unused))
749 uint16_t data_len,
750 __attribute__((unused))
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800751 uint8_t* p_data) {}
Martijn Coenen5c65c3a2013-03-27 13:23:36 -0700752
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800753/*******************************************************************************
754**
755** Function: ThreadMutex::ThreadMutex()
756**
757** Description: class constructor
758**
759** Returns: none
760**
761*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800762ThreadMutex::ThreadMutex() {
763 pthread_mutexattr_t mutexAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800764
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800765 pthread_mutexattr_init(&mutexAttr);
766 pthread_mutex_init(&mMutex, &mutexAttr);
767 pthread_mutexattr_destroy(&mutexAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800768}
769
770/*******************************************************************************
771**
772** Function: ThreadMutex::~ThreadMutex()
773**
774** Description: class destructor
775**
776** Returns: none
777**
778*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800779ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800780
781/*******************************************************************************
782**
783** Function: ThreadMutex::lock()
784**
785** Description: lock kthe mutex
786**
787** Returns: none
788**
789*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800790void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800791
792/*******************************************************************************
793**
794** Function: ThreadMutex::unblock()
795**
796** Description: unlock the mutex
797**
798** Returns: none
799**
800*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800801void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800802
803/*******************************************************************************
804**
805** Function: ThreadCondVar::ThreadCondVar()
806**
807** Description: class constructor
808**
809** Returns: none
810**
811*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800812ThreadCondVar::ThreadCondVar() {
813 pthread_condattr_t CondAttr;
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800814
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800815 pthread_condattr_init(&CondAttr);
816 pthread_cond_init(&mCondVar, &CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800817
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800818 pthread_condattr_destroy(&CondAttr);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800819}
820
821/*******************************************************************************
822**
823** Function: ThreadCondVar::~ThreadCondVar()
824**
825** Description: class destructor
826**
827** Returns: none
828**
829*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800830ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800831
832/*******************************************************************************
833**
834** Function: ThreadCondVar::wait()
835**
836** Description: wait on the mCondVar
837**
838** Returns: none
839**
840*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800841void ThreadCondVar::wait() {
842 pthread_cond_wait(&mCondVar, *this);
843 pthread_mutex_unlock(*this);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800844}
845
846/*******************************************************************************
847**
848** Function: ThreadCondVar::signal()
849**
850** Description: signal the mCondVar
851**
852** Returns: none
853**
854*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800855void ThreadCondVar::signal() {
856 AutoThreadMutex a(*this);
857 pthread_cond_signal(&mCondVar);
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800858}
859
860/*******************************************************************************
861**
862** Function: AutoThreadMutex::AutoThreadMutex()
863**
864** Description: class constructor, automatically lock the mutex
865**
866** Returns: none
867**
868*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800869AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
The Android Open Source Projecte9df6ba2012-12-13 14:55:37 -0800870
871/*******************************************************************************
872**
873** Function: AutoThreadMutex::~AutoThreadMutex()
874**
875** Description: class destructor, automatically unlock the mutex
876**
877** Returns: none
878**
879*******************************************************************************/
Ruchi Kandoi6fca02d2017-01-30 14:28:16 -0800880AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }