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 #include "interface_state_observer.h"
17 
18 #include "constant.h"
19 #include "interface_state_observer_wrapper.h"
20 #include "netmanager_ext_log.h"
21 
22 static constexpr const char *KEY_IFACE = "iface";
23 static constexpr const char *KEY_ACTIVE = "active";
24 static constexpr const char *EVENT_STATS_CHANGE = "interfaceStateChange";
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
OnInterfaceAdded(const std::string & iface)28 int32_t InterfaceStateObserver::OnInterfaceAdded(const std::string &iface)
29 {
30     if (!InterfaceStateObserverWrapper::GetInstance().GetEventManager()->HasEventListener(EVENT_STATS_CHANGE)) {
31         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_STATS_CHANGE);
32         return 0;
33     }
34     auto pair = new std::pair<std::string, bool>(iface, true);
35     InterfaceStateObserverWrapper::GetInstance().GetEventManager()->EmitByUv(EVENT_STATS_CHANGE, pair,
36                                                                              IfaceChangedCallback);
37     return 0;
38 }
39 
OnInterfaceRemoved(const std::string & iface)40 int32_t InterfaceStateObserver::OnInterfaceRemoved(const std::string &iface)
41 {
42     if (!InterfaceStateObserverWrapper::GetInstance().GetEventManager()->HasEventListener(EVENT_STATS_CHANGE)) {
43         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_STATS_CHANGE);
44         return -1;
45     }
46     auto pair = new std::pair<std::string, bool>(iface, false);
47     InterfaceStateObserverWrapper::GetInstance().GetEventManager()->EmitByUv(EVENT_STATS_CHANGE, pair,
48                                                                              IfaceChangedCallback);
49     return 0;
50 }
51 
OnInterfaceChanged(const std::string & iface,bool up)52 int32_t InterfaceStateObserver::OnInterfaceChanged(const std::string &iface, bool up)
53 {
54     if (!InterfaceStateObserverWrapper::GetInstance().GetEventManager()->HasEventListener(EVENT_STATS_CHANGE)) {
55         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_STATS_CHANGE);
56         return -1;
57     }
58     auto pair = new std::pair<std::string, bool>(iface, up);
59     InterfaceStateObserverWrapper::GetInstance().GetEventManager()->EmitByUv(EVENT_STATS_CHANGE, pair,
60                                                                              IfaceChangedCallback);
61     return 0;
62 }
63 
CreateIfaceChangedParam(napi_env env,void * data)64 napi_value InterfaceStateObserver::CreateIfaceChangedParam(napi_env env, void *data)
65 {
66     auto pair(reinterpret_cast<std::pair<std::string, bool> *>(data));
67     napi_value obj = NapiUtils::CreateObject(env);
68     if (pair != nullptr) {
69         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_IFACE, pair->first);
70         NapiUtils::SetBooleanProperty(env, obj, KEY_ACTIVE, pair->second);
71     }
72     delete pair;
73     return obj;
74 }
75 
IfaceChangedCallback(uv_work_t * work,int status)76 void InterfaceStateObserver::IfaceChangedCallback(uv_work_t *work, int status)
77 {
78     CallbackTemplate<CreateIfaceChangedParam>(work, status);
79 }
80 } // namespace NetManagerStandard
81 } // namespace OHOS
82