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_controller_proxy.h"
21#include "nfc_sdk_common.h"
22#include "infc_controller_callback.h"
23#include "infc_controller_service.h"
24#ifdef VENDOR_APPLICATIONS_ENABLED
25#include "iquery_app_info_callback.h"
26#endif
27
28namespace OHOS {
29namespace NFC {
30namespace KITS {
31static const std::string NFC_SERVICE_NAME = "nfc";
32
33class NfcController final {
34public:
35    explicit NfcController();
36    ~NfcController();
37
38    /**
39     * @Description Get an object of nfc controller.
40     * @param void
41     * @return an object of nfc controller
42     */
43    static NfcController &GetInstance();
44    /**
45     * @Description Turn on Nfc of the device.
46     * @param void
47     * @return Errorcode of turn on nfc. if return 0, means successful.
48     */
49    int TurnOn();
50    /**
51     * @Description Turn off Nfc of the device.
52     * @param void
53     * @return Errorcode of turn off nfc. if return 0, means successful.
54     */
55    int TurnOff();
56    /**
57     * @Description Get nfc state of device.
58     * @param void
59     * @return nfc state.
60     */
61    int GetNfcState();
62    /**
63     * @Checks whether a device supports NFC.
64     * @param void
65     * @return If the device supports NFC return 1; otherwise return 0.
66     */
67    bool IsNfcAvailable();
68    /**
69     * @Description Checks whether NFC is enabled.
70     * @param isOpen The output for checking nfc is open or not.
71     * @return The status code of calling function.
72     */
73    int IsNfcOpen(bool &isOpen);
74    /**
75     * @Description Registers the callback for nfc state changed notification.
76     * @param callback the callback to be registered.
77     * @param type the type for this callback, it's "nfcStateChange"
78     * @return The status code for register operation.
79     */
80    ErrorCode RegListener(const sptr<INfcControllerCallback> &callback, const std::string& type);
81    /**
82     * @Description Unregisters the callback for nfc state changed notification.
83     * @param type the type for this callback, it's "nfcStateChange"
84     * @return The status code for unregister operation.
85     */
86    ErrorCode UnregListener(const std::string& type);
87
88    /**
89     * @brief Get the Tag Service Iface object
90     *
91     * @return OHOS::sptr<IRemoteObject> the remote object of tag service.
92     */
93    OHOS::sptr<IRemoteObject> GetTagServiceIface();
94
95    OHOS::sptr<IRemoteObject> GetHceServiceIface();
96
97    void OnRemoteDied(const wptr<IRemoteObject> &remoteObject);
98
99    ErrorCode RegNdefMsgCb(const sptr<INdefMsgCallback> &callback);
100
101#ifdef VENDOR_APPLICATIONS_ENABLED
102    ErrorCode RegQueryApplicationCb(const std::string& type,
103        QueryApplicationByVendor tagCallback, QueryHceAppByVendor hceCallback);
104
105    ErrorCode RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback);
106    ErrorCode NotifyEventStatus(int eventType, int arg1 = 0, std::string arg2 = "");
107#endif
108
109private:
110    class NfcServiceDeathRecipient : public IRemoteObject::DeathRecipient {
111        public:
112            explicit NfcServiceDeathRecipient(NfcController &client) : client_(client) {}
113            ~NfcServiceDeathRecipient() override = default;
114            void OnRemoteDied(const wptr<IRemoteObject> &remoteObject) override
115            {
116                client_.OnRemoteDied(remoteObject);
117            }
118        private:
119            NfcController &client_;
120    };
121
122private:
123    static void InitNfcRemoteSA();
124
125private:
126    static bool initialized_;
127    static std::shared_ptr<NfcControllerProxy> nfcControllerProxy_;
128    static std::weak_ptr<OHOS::NFC::INfcControllerService> nfcControllerService_;
129    static std::mutex mutex_;
130    static bool remoteDied_;
131    static sptr<IRemoteObject> remote_;
132    static sptr<IRemoteObject::DeathRecipient> deathRecipient_;
133};
134} // namespace KITS
135} // namespace NFC
136} // namespace OHOS
137#endif // NFC_CONTROLLER_H
138