1 /*
2 * Copyright (c) 2024 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 #include <system_ability_definition.h>
16
17 #include "mission/wifi_state_listener.h"
18
19 #include "dtbschedmgr_log.h"
20 #include "mission/dms_continue_recv_manager.h"
21 #include "mission/wifi_state_adapter.h"
22 #include "wifi_device.h"
23 #include "wifi_msg.h"
24
25 namespace OHOS {
26 namespace DistributedSchedule {
27 namespace {
28 const std::string TAG = "WifiStateListener";
29 }
30
OnReceiveEvent(const EventFwk::CommonEventData & data)31 void WifiStateListener::OnReceiveEvent(const EventFwk::CommonEventData &data)
32 {
33 HILOGI("receive event code = %{public}d", data.GetCode());
34 switch (data.GetCode()) {
35 case int32_t(OHOS::Wifi::WifiState::DISABLED): {
36 HILOGI("on wifi disabled");
37 WifiStateAdapter::GetInstance().UpdateWifiState(false);
38 DMSContinueRecvMgr::GetInstance().OnContinueSwitchOff();
39 break;
40 }
41
42 case int32_t(Wifi::WifiDetailState::STATE_SEMI_ACTIVE): {
43 HILOGI("on wifi SEMI_ACTIVE");
44 WifiStateAdapter::GetInstance().UpdateWifiState(true);
45 break;
46 }
47
48 case int32_t(OHOS::Wifi::WifiState::ENABLED): {
49 HILOGI("on wifi enabled");
50 WifiStateAdapter::GetInstance().UpdateWifiState(true);
51 break;
52 }
53
54 default: {
55 HILOGI("unhandled code");
56 break;
57 }
58 }
59 }
60
CheckWifiStateIsActived()61 bool WifiStateListener::CheckWifiStateIsActived()
62 {
63 std::shared_ptr<Wifi::WifiDevice> wifiDevicePtr = Wifi::WifiDevice::GetInstance(WIFI_DEVICE_SYS_ABILITY_ID);
64 if (wifiDevicePtr == nullptr) {
65 HILOGE("wifiDevice is nullptr");
66 return false;
67 }
68 Wifi::WifiDetailState wifiDetailState;
69 auto ret = wifiDevicePtr->GetWifiDetailState(wifiDetailState);
70 if (ret != Wifi::WIFI_OPT_SUCCESS) {
71 HILOGE("GetWifiDetailState failed, ret %{public}d", ret);
72 return false;
73 }
74
75 HILOGI("get wifi detail state is %{public}d", wifiDetailState);
76 return (wifiDetailState == Wifi::WifiDetailState::STATE_SEMI_ACTIVE) ||
77 (wifiDetailState == Wifi::WifiDetailState::STATE_ACTIVATED);
78 }
79
InitWifiState()80 void WifiStateListener::InitWifiState()
81 {
82 bool isWifiActive = CheckWifiStateIsActived();
83 WifiStateAdapter::GetInstance().UpdateWifiState(isWifiActive);
84 HILOGI("init wifiActive, State=%{public}d", isWifiActive);
85 }
86 } // namespace DistributedSchedule
87 } // namespace OHOS