1 /******************************************************************************
2  *
3  *  Copyright 2018 NXP
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  ******************************************************************************/
18 
19 #ifndef ANDROID_HARDWARE_NFC_V1_2_NFC_H
20 #define ANDROID_HARDWARE_NFC_V1_2_NFC_H
21 
22 #include <android/hardware/nfc/1.2/INfc.h>
23 #include <android/hardware/nfc/1.2/types.h>
24 #include <hidl/MQDescriptor.h>
25 #include <hidl/Status.h>
26 #include <log/log.h>
27 #include <pthread.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace nfc {
32 namespace V1_2 {
33 namespace implementation {
34 
35 using ::android::hidl::base::V1_0::IBase;
36 using ::android::hardware::nfc::V1_2::INfc;
37 using ::android::hardware::hidl_array;
38 using ::android::hardware::hidl_memory;
39 using ::android::hardware::hidl_string;
40 using ::android::hardware::hidl_vec;
41 using ::android::hardware::Return;
42 using ::android::hardware::Void;
43 using ::android::sp;
44 struct Nfc : public V1_2::INfc, public hidl_death_recipient {
45  public:
46   // Methods from ::android::hardware::nfc::V1_0::INfc follow.
47   Return<V1_0::NfcStatus> open(
48       const sp<V1_0::INfcClientCallback>& clientCallback) override;
49   Return<V1_0::NfcStatus> open_1_1(
50       const sp<V1_1::INfcClientCallback>& clientCallback) override;
51   Return<uint32_t> write(const hidl_vec<uint8_t>& data) override;
52   Return<V1_0::NfcStatus> coreInitialized(
53       const hidl_vec<uint8_t>& data) override;
54   Return<V1_0::NfcStatus> prediscover() override;
55   Return<V1_0::NfcStatus> close() override;
56   Return<V1_0::NfcStatus> controlGranted() override;
57   Return<V1_0::NfcStatus> powerCycle() override;
58 
59   // Methods from ::android::hardware::nfc::V1_1::INfc follow.
60   Return<void> factoryReset();
61   Return<V1_0::NfcStatus> closeForPowerOffCase();
62   Return<void> getConfig(getConfig_cb config);
63 
64   Return<void> getConfig_1_2(getConfig_1_2_cb config);
65 
66   // Methods from ::android::hidl::base::V1_0::IBase follow.
67 
eventCallbackNfc68   static void eventCallback(uint8_t event, uint8_t status) {
69     if (mCallbackV1_1 != nullptr) {
70       auto ret = mCallbackV1_1->sendEvent_1_1((V1_1::NfcEvent)event,
71                                               (V1_0::NfcStatus)status);
72       if (!ret.isOk()) {
73         ALOGW("failed to send event!!!");
74       }
75     } else if (mCallbackV1_0 != nullptr) {
76       auto ret = mCallbackV1_0->sendEvent((V1_0::NfcEvent)event,
77                                           (V1_0::NfcStatus)status);
78       if (!ret.isOk()) {
79         ALOGE("failed to send event!!!");
80       }
81     }
82   }
83 
dataCallbackNfc84   static void dataCallback(uint16_t data_len, uint8_t* p_data) {
85     hidl_vec<uint8_t> data;
86     data.setToExternal(p_data, data_len);
87     if (mCallbackV1_1 != nullptr) {
88       auto ret = mCallbackV1_1->sendData(data);
89       if (!ret.isOk()) {
90         ALOGW("failed to send data!!!");
91       }
92     } else if (mCallbackV1_0 != nullptr) {
93       auto ret = mCallbackV1_0->sendData(data);
94       if (!ret.isOk()) {
95         ALOGE("failed to send data!!!");
96       }
97     }
98   }
99 
serviceDiedNfc100   virtual void serviceDied(uint64_t cookie, const wp<IBase>& /*who*/) {
101     pthread_mutex_lock(&mLockOpenClose);
102     ALOGE("serviceDied!!! %llu, %llu, %s, %s",
103           (unsigned long long)cookie,
104           (unsigned long long)mOpenCount,
105           (mCallbackV1_0 == nullptr ? "null" : "defined"),
106           (mCallbackV1_1 == nullptr ? "null" : "defined"));
107     if (cookie == mOpenCount) {
108       if (mCallbackV1_1 != nullptr) {
109         mCallbackV1_1->unlinkToDeath(this);
110         mCallbackV1_1 = nullptr;
111       }
112       if (mCallbackV1_0 != nullptr) {
113         mCallbackV1_0->unlinkToDeath(this);
114         mCallbackV1_0 = nullptr;
115       }
116       pthread_mutex_unlock(&mLockOpenClose);
117       close();
118     } else {
119       pthread_mutex_unlock(&mLockOpenClose);
120     }
121   }
122 
123  private:
124   static sp<V1_1::INfcClientCallback> mCallbackV1_1;
125   static sp<V1_0::INfcClientCallback> mCallbackV1_0;
126   pthread_mutex_t mLockOpenClose = PTHREAD_MUTEX_INITIALIZER;
127   uint64_t mOpenCount = 0;
128 };
129 
130 }  // namespace implementation
131 }  // namespace V1_2
132 }  // namespace nfc
133 }  // namespace hardware
134 }  // namespace android
135 
136 #endif  // ANDROID_HARDWARE_NFC_V1_2_NFC_H
137