1 /* 2 * Copyright (c) 2023 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 16 #ifndef NFC_NAPI_HCE_SERVICE_H 17 #define NFC_NAPI_HCE_SERVICE_H 18 19 #include <map> 20 #include <set> 21 #include <shared_mutex> 22 #include <string> 23 #include <uv.h> 24 #include <vector> 25 26 #include "napi/native_api.h" 27 #include "napi/native_node_api.h" 28 #include "ihce_cmd_callback.h" 29 #include "nfc_napi_common_utils.h" 30 #include "nfc_sdk_common.h" 31 #include "element_name.h" 32 #include "system_ability_status_change_stub.h" 33 34 namespace OHOS { 35 namespace NFC { 36 namespace KITS { 37 using OHOS::AppExecFwk::ElementName; 38 class NfcNapiHceAdapter { 39 public: 40 static napi_value Init(napi_env env, napi_value exports); 41 static napi_value Constructor(napi_env env, napi_callback_info info); 42 static void Destructor(napi_env env, void* nativeObject, void* /*finalize_hint*/); 43 static napi_value OnHceCmd(napi_env env, napi_callback_info info); 44 static napi_value OffHceCmd(napi_env env, napi_callback_info info); 45 static napi_value Transmit(napi_env env, napi_callback_info info); 46 static napi_value StopHce(napi_env env, napi_callback_info cbinfo); 47 static napi_value StartHCEDeprecated(napi_env env, napi_callback_info cbinfo); 48 static napi_value StartHCE(napi_env env, napi_callback_info cbinfo); 49 static napi_value StopHCEDeprecated(napi_env env, napi_callback_info cbinfo); 50 static napi_value SendResponse(napi_env env, napi_callback_info cbinfo); 51 }; 52 53 struct NfcHceSessionContext : BaseContext { 54 std::string value; // out 55 std::string dataBytes; // in 56 }; 57 58 class AsyncEventData { 59 public: 60 napi_env env; 61 napi_ref callbackRef; 62 std::function<napi_value()> packResult; 63 AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> v)64 explicit AsyncEventData(napi_env e, 65 napi_ref r, 66 std::function<napi_value()> v) : env(e), callbackRef(r), packResult(v) {} 67 68 AsyncEventData() = delete; 69 ~AsyncEventData()70 virtual ~AsyncEventData() {} 71 }; 72 /** 73 * @brief register obj 74 */ 75 class RegObj { 76 public: RegObj()77 RegObj() : m_regEnv(0), m_regHanderRef(nullptr) {} 78 RegObj(const napi_env & env,const napi_ref & ref)79 explicit RegObj(const napi_env& env, const napi_ref& ref) : m_regEnv(env), m_regHanderRef(ref) {} 80 ~RegObj()81 ~RegObj() {} 82 83 napi_env m_regEnv; 84 napi_ref m_regHanderRef; 85 }; 86 class NfcNapiHceAbilityStatusChange : public SystemAbilityStatusChangeStub { 87 public: 88 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 89 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 90 void Init(int32_t systemAbilityId); 91 }; 92 class EventRegister { 93 public: EventRegister()94 EventRegister() 95 { 96 saStatusListener_ = std::make_shared<NfcNapiHceAbilityStatusChange>(); 97 saStatusListener_->Init(NFC_MANAGER_SYS_ABILITY_ID); 98 } ~EventRegister()99 ~EventRegister() 100 { 101 saStatusListener_ = nullptr; 102 } 103 104 static EventRegister& GetInstance(); 105 106 void Register(const napi_env& env, const std::string& type, napi_value handler); 107 void Unregister(const napi_env& env, ElementName& element); 108 void UnregisterForOffIntf(const napi_env& env, const std::string& type); 109 private: 110 ErrorCode RegHceCmdCallbackEvents(const napi_env& env, const std::string& type); 111 ErrorCode UnRegHceCmdCallbackEvents(const napi_env& env, const std::string& type); 112 ErrorCode UnregisterHceEvents(const napi_env& env, ElementName &element); 113 bool IsEventSupport(const std::string& type); 114 void DeleteHceCmdRegisterObj(const napi_env& env); 115 116 static bool isEventRegistered; 117 std::shared_ptr<NfcNapiHceAbilityStatusChange> saStatusListener_; 118 }; 119 } // namespace KITS 120 } // namespace NFC 121 } // namespace OHOS 122 #endif