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 "nfc_controller_callback_stub.h" 19 #include "nfc_controller_proxy.h" 20 #include "nfc_sdk_common.h" 21 #include "infc_controller_callback.h" 22 #include "infc_controller_service.h" 23 24 namespace OHOS { 25 namespace NFC { 26 namespace KITS { 27 static const std::string NFC_SERVICE_NAME = "nfc"; 28 const std::string NFC_EXTRA_STATE = "ohos.nfc.extra.ADAPTER_STATE"; 29 const std::string COMMON_EVENT_NFC_ACTION_STATE_CHANGED = "usual.event.nfc.action.ADAPTER_STATE_CHANGED"; 30 31 class NfcController final { 32 public: 33 explicit NfcController(); 34 ~NfcController(); 35 36 /** 37 * @Description Get an object of nfc controller. 38 * @param void 39 * @return an object of nfc controller 40 */ 41 static NfcController &GetInstance(); 42 /** 43 * @Description Turn on Nfc of the device. 44 * @param void 45 * @return Errorcode of turn on nfc. if return 0, means successful. 46 */ 47 int TurnOn(); 48 /** 49 * @Description Turn off Nfc of the device. 50 * @param void 51 * @return Errorcode of turn off nfc. if return 0, means successful. 52 */ 53 int TurnOff(); 54 /** 55 * @Description Get nfc state of device. 56 * @param void 57 * @return nfc state. 58 */ 59 int GetNfcState(); 60 /** 61 * @Checks whether a device supports NFC. 62 * @param void 63 * @return If the device supports NFC return 1; otherwise return 0. 64 */ 65 bool IsNfcAvailable(); 66 /** 67 * @Description Checks whether NFC is enabled. 68 * @param isOpen The output for checking nfc is open or not. 69 * @return The status code of calling function. 70 */ 71 int IsNfcOpen(bool &isOpen); 72 /** 73 * @Description Registers the callback for nfc state changed notification. 74 * @param callback the callback to be registered. 75 * @param type the type for this callback, it's "nfcStateChange" 76 * @return The status code for register operation. 77 */ 78 ErrorCode RegListener(const sptr<INfcControllerCallback> &callback, const std::string& type); 79 /** 80 * @Description Unregisters the callback for nfc state changed notification. 81 * @param type the type for this callback, it's "nfcStateChange" 82 * @return The status code for unregister operation. 83 */ 84 ErrorCode UnregListener(const std::string& type); 85 86 /** 87 * @brief Get the Tag Service Iface object 88 * 89 * @return OHOS::sptr<IRemoteObject> the remote object of tag service. 90 */ 91 OHOS::sptr<IRemoteObject> GetTagServiceIface(); 92 private: 93 static void InitNfcController(); 94 95 private: 96 static bool initialized_; 97 static std::shared_ptr<NfcControllerProxy> nfcControllerProxy_; 98 static std::weak_ptr<OHOS::NFC::INfcControllerService> nfcControllerService_; 99 static std::mutex mutex_; 100 }; 101 } // namespace KITS 102 } // namespace NFC 103 } // namespace OHOS 104 #endif // NFC_CONTROLLER_H 105