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 return 0;
55 }
56
CreateIfaceChangedParam(napi_env env,void * data)57 napi_value InterfaceStateObserver::CreateIfaceChangedParam(napi_env env, void *data)
58 {
59 auto pair(reinterpret_cast<std::pair<std::string, bool> *>(data));
60 napi_value obj = NapiUtils::CreateObject(env);
61 if (pair != nullptr) {
62 NapiUtils::SetStringPropertyUtf8(env, obj, KEY_IFACE, pair->first);
63 NapiUtils::SetBooleanProperty(env, obj, KEY_ACTIVE, pair->second);
64 }
65 delete pair;
66 return obj;
67 }
68
IfaceChangedCallback(uv_work_t * work,int status)69 void InterfaceStateObserver::IfaceChangedCallback(uv_work_t *work, int status)
70 {
71 CallbackTemplate<CreateIfaceChangedParam>(work, status);
72 }
73 } // namespace NetManagerStandard
74 } // namespace OHOS
75