• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 COMMUNICATION_NET_MANAGER_BASE_EAP_EVENT_MANAGER_H
17 #define COMMUNICATION_NET_MANAGER_BASE_EAP_EVENT_MANAGER_H
18 
19 #include <napi/native_api.h>
20 #include <map>
21 #include <vector>
22 #include <memory>
23 #include "ethernet_client.h"
24 #include "napi_utils.h"
25 #include "net_eap_callback_stub.h"
26 #include "netmanager_ext_log.h"
27 #include "system_ability_status_change_stub.h"
28 #include "iservice_registry.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 
33 class RegObj {
34 public:
RegObj()35     RegObj() : m_regEnv(0), m_regHandlerRef(nullptr)
36     {
37     }
38 
RegObj(const napi_env & env,const napi_ref & ref)39     explicit RegObj(const napi_env& env, const napi_ref& ref)
40     {
41         m_regEnv = env;
42         m_regHandlerRef = ref;
43     }
44 
~RegObj()45     ~RegObj()
46     {
47     }
48 
49     bool operator == (const RegObj& other) const
50     {
51         return m_regEnv == other.m_regEnv && m_regHandlerRef == other.m_regHandlerRef;
52     }
53 
54     bool operator != (const RegObj& other) const
55     {
56         return !(*this == other);
57     }
58 
59     bool operator < (const RegObj& other) const
60     {
61         return m_regEnv < other.m_regEnv || (m_regEnv == other.m_regEnv && m_regHandlerRef < other.m_regHandlerRef);
62     }
63 
64     napi_env m_regEnv;
65     napi_ref m_regHandlerRef;
66 };
67 
68 using TypeMapRegObj = std::map<uint32_t, std::vector<RegObj>>;
69 
70 class AsyncEventData {
71 public:
72     napi_env env_;
73     napi_ref callbackRef_;
74     std::function<napi_value ()> packResult_;
75     int32_t key_ = 0;
76     NetType netType_ = NetType::INVALID;
77     int32_t msgId_ = -1;
78 
AsyncEventData(napi_env e,napi_ref r,std::function<napi_value ()> p)79     AsyncEventData(napi_env e, napi_ref r, std::function<napi_value ()> p)
80     {
81         env_ = e;
82         callbackRef_ = r;
83         packResult_ = p;
84     }
~AsyncEventData()85     virtual ~AsyncEventData() {
86     }
87 
Init(const int32_t key,NetType netType,int32_t msgId)88     void Init(const int32_t key, NetType netType, int32_t msgId)
89     {
90         key_ = key;
91         netType_ = netType;
92         msgId_ = msgId;
93     }
94 };
95 
96 class NetEapPostBackCallback : public NetEapPostbackCallbackStub {
97 public:
98     NetEapPostBackCallback() = default;
99     virtual ~NetEapPostBackCallback() = default;
100 
101 public:
102     int32_t OnEapSupplicantPostback(NetType netType, const sptr<EapData> &eapData) override;
103 
104 private:
105     bool CheckAndNotifyApp(NetType netType, const int32_t key, const sptr<EapData> &eapData);
106     void EventNotify(const std::shared_ptr<AsyncEventData> &asyncEvent);
107     void SendTask(const std::shared_ptr<AsyncEventData> &asyncEvent);
108     void InitScope(const std::shared_ptr<AsyncEventData> &asyncEvent);
109     void EndSendTask(const std::shared_ptr<AsyncEventData> &asyncEvent, bool unrefRef, uint32_t refCount);
110     napi_value CreateResult(const napi_env& env, const sptr<EapData> &eapData);
111 
112 private:
113     napi_handle_scope scope_ = nullptr;
114 };
115 
116 class NetManagerNapiAbilityStatusChange : public SystemAbilityStatusChangeStub {
117 public:
118     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
119     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
120 };
121 
122 class EapEventMgr final {
123 public:
124     EapEventMgr();
125     ~EapEventMgr() = default;
126 
127     static EapEventMgr &GetInstance();
128     int32_t RegCustomEapHandler(napi_env env, NetType netType, uint32_t eapCode, uint32_t eapType, napi_value handler);
129     int32_t UnRegCustomEapHandler(napi_env env, NetType netType, uint32_t eapCode, uint32_t eapType,
130         napi_value handler);
131     int32_t ReplyCustomEapData(CustomResult result, const sptr<EapData> &eapData);
132     int32_t RegCustomEapHandler(NetType netType, RegTriggerMode triggerMode);
133     int32_t UnRegCustomEapHandler(NetType netType);
134     std::map<NetType, TypeMapRegObj> GetRegisterInfoMap();
135 
136 private:
137     sptr<NetEapPostBackCallback> eapPostBackCallback_ = nullptr;
138     OHOS::sptr<OHOS::ISystemAbilityStatusChange> mSaStatusListener = nullptr;
139     std::map<NetType, TypeMapRegObj> eventRegisterInfo_;
140 };
141 } // namespace NetManagerStandard
142 } // namespace OHOS
143 
144 #endif /* COMMUNICATION_NET_MANAGER_BASE_EAP_EVENT_MANAGER_H */