• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef NFC_CONTROLLER_H
16 #define NFC_CONTROLLER_H
17 
18 #include "ndef_msg_callback_stub.h"
19 #include "nfc_controller_callback_stub.h"
20 #include "nfc_sdk_common.h"
21 #include "infc_controller_callback.h"
22 
23 #ifdef VENDOR_APPLICATIONS_ENABLED
24 #include "iquery_app_info_callback.h"
25 #include "on_card_emulation_notify_cb_stub.h"
26 #include "query_app_info_callback_stub.h"
27 #endif
28 
29 namespace OHOS {
30 namespace NFC {
31 namespace KITS {
32 class NfcController final {
33 public:
34     explicit NfcController();
35     ~NfcController();
36 
37     /**
38      * @Description Get an object of nfc controller.
39      * @param void
40      * @return an object of nfc controller
41      */
42     static NfcController &GetInstance();
43     /**
44      * @Description Turn on Nfc of the device.
45      * @param void
46      * @return Errorcode of turn on nfc. if return 0, means successful.
47      */
48     int TurnOn();
49     /**
50      * @Description Turn off Nfc of the device.
51      * @param void
52      * @return Errorcode of turn off nfc. if return 0, means successful.
53      */
54     int TurnOff();
55     /**
56      * @Description Get nfc state of device.
57      * @param void
58      * @return nfc state.
59      */
60     int GetNfcState();
61     /**
62      * @Checks whether a device supports NFC.
63      * @param void
64      * @return If the device supports NFC return 1; otherwise return 0.
65      */
66     bool IsNfcAvailable();
67     /**
68      * @Description Checks whether NFC is enabled.
69      * @param isOpen The output for checking nfc is open or not.
70      * @return The status code of calling function.
71      */
72     int IsNfcOpen(bool &isOpen);
73     /**
74      * @Description Registers the callback for nfc state changed notification.
75      * @param callback the callback to be registered.
76      * @param type the type for this callback, it's "nfcStateChange"
77      * @return The status code for register operation.
78      */
79     ErrorCode RegListener(const sptr<INfcControllerCallback> &callback, const std::string& type);
80     /**
81      * @Description Unregisters the callback for nfc state changed notification.
82      * @param type the type for this callback, it's "nfcStateChange"
83      * @return The status code for unregister operation.
84      */
85     ErrorCode UnregListener(const std::string& type);
86 
87     /**
88      * @brief Get the Tag Service Iface object
89      *
90      * @return OHOS::sptr<IRemoteObject> the remote object of tag service.
91      */
92     OHOS::sptr<IRemoteObject> GetTagServiceIface();
93 
94     OHOS::sptr<IRemoteObject> GetHceServiceIface(int32_t &res);
95 
96     void OnRemoteDied(const wptr<IRemoteObject> &remoteObject);
97 
98     ErrorCode RegNdefMsgCb(const sptr<INdefMsgCallback> &callback);
99 
100 #ifdef VENDOR_APPLICATIONS_ENABLED
101     ErrorCode RegQueryApplicationCb(const std::string& type,
102         QueryApplicationByVendor tagCallback, QueryHceAppByVendor hceCallback);
103 
104     ErrorCode RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback);
105     ErrorCode NotifyEventStatus(int eventType, int arg1 = 0, std::string arg2 = "");
106 #endif
107 
108 private:
109     class NfcServiceDeathRecipient : public IRemoteObject::DeathRecipient {
110         public:
NfcServiceDeathRecipient(NfcController & client)111             explicit NfcServiceDeathRecipient(NfcController &client) : client_(client) {}
112             ~NfcServiceDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)113             void OnRemoteDied(const wptr<IRemoteObject> &remoteObject) override
114             {
115                 client_.OnRemoteDied(remoteObject);
116             }
117         private:
118             NfcController &client_;
119     };
120 
121 private:
122     static void InitNfcRemoteSA();
123 
124 private:
125     static bool initialized_;
126     static std::mutex mutex_;
127     static bool remoteDied_;
128     static sptr<IRemoteObject> remote_;
129     static sptr<IRemoteObject::DeathRecipient> deathRecipient_;
130 };
131 } // namespace KITS
132 } // namespace NFC
133 } // namespace OHOS
134 #endif // NFC_CONTROLLER_H
135