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 
33 namespace OHOS {
34 namespace NFC {
35 namespace KITS {
36 using OHOS::AppExecFwk::ElementName;
37 class NfcNapiHceAdapter {
38 public:
39     static napi_value Init(napi_env env, napi_value exports);
40     static napi_value Constructor(napi_env env, napi_callback_info info);
41     static void Destructor(napi_env env, void* nativeObject, void* /*finalize_hint*/);
42     static napi_value OnHceCmd(napi_env env, napi_callback_info info);
43     static napi_value Transmit(napi_env env, napi_callback_info info);
44     static napi_value StopHce(napi_env env, napi_callback_info cbinfo);
45     static napi_value StartHCEDeprecated(napi_env env, napi_callback_info cbinfo);
46     static napi_value StartHCE(napi_env env, napi_callback_info cbinfo);
47     static napi_value StopHCEDeprecated(napi_env env, napi_callback_info cbinfo);
48     static napi_value SendResponse(napi_env env, napi_callback_info cbinfo);
49 };
50 
51 struct NfcHceSessionContext : BaseContext {
52     std::string value;     // out
53     std::string dataBytes; // in
54 };
55 
56 class AsyncEventData {
57 public:
58     napi_env env;
59     napi_ref callbackRef;
60     std::function<napi_value()> packResult;
61 
AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> v)62     explicit AsyncEventData(napi_env e,
63                             napi_ref r,
64                             std::function<napi_value()> v) : env(e), callbackRef(r), packResult(v) {}
65 
66     AsyncEventData() = delete;
67 
~AsyncEventData()68     virtual ~AsyncEventData() {}
69 };
70 /**
71  * @brief register obj
72  */
73 class RegObj {
74 public:
RegObj()75     RegObj() : m_regEnv(0), m_regHanderRef(nullptr) {}
76 
RegObj(const napi_env & env,const napi_ref & ref)77     explicit RegObj(const napi_env& env, const napi_ref& ref) : m_regEnv(env), m_regHanderRef(ref) {}
78 
~RegObj()79     ~RegObj() {}
80 
81     napi_env m_regEnv;
82     napi_ref m_regHanderRef;
83 };
84 class EventRegister {
85 public:
EventRegister()86     EventRegister() {}
~EventRegister()87     ~EventRegister() {}
88 
89     static EventRegister& GetInstance();
90 
91     void Register(const napi_env& env, const std::string& type, napi_value handler);
92     void Unregister(const napi_env& env, ElementName& element);
93 private:
94     ErrorCode RegHceCmdCallbackEvents(const napi_env& env, const std::string& type);
95     ErrorCode UnregisterHceEvents(const napi_env& env, ElementName &element);
96     bool IsEventSupport(const std::string& type);
97     void DeleteHceCmdRegisterObj(const napi_env& env);
98 
99     static bool isEventRegistered;
100 };
101 } // namespace KITS
102 } // namespace NFC
103 } // namespace OHOS
104 #endif