1 /*
2  * Copyright (c) 2021-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 "wifi_msdp_state_listener.h"
17 #include "wifi_logger.h"
18 #include "wifi_config_center.h"
19 #include "define.h"
20 #include "wifi_service_manager.h"
21 
22 namespace OHOS {
23 namespace Wifi {
24 DEFINE_WIFILOG_LABEL("WifiMsdpStateListener");
25 
DeviceMovementCallback()26 DeviceMovementCallback::DeviceMovementCallback()
27 {
28     movementChangeEventHandler = std::make_unique<WifiEventHandler>("WIFI_MOVEMENT_STATE_AWARE_THREAD");
29 }
30 
~DeviceMovementCallback()31 DeviceMovementCallback::~DeviceMovementCallback()
32 {
33     if (movementChangeEventHandler) {
34         movementChangeEventHandler.reset();
35     }
36 }
37 
OnMovementChanged(const Msdp::MovementDataUtils::MovementData & movementData)38 void DeviceMovementCallback::OnMovementChanged(const Msdp::MovementDataUtils::MovementData &movementData)
39 {
40     WIFI_LOGI("enter DeviceMovementCallback::OnMovementChanged type=%{public}d, value=%{public}d",
41         movementData.type, movementData.value);
42     if (movementData.type == Msdp::MovementDataUtils::MovementType::TYPE_STILL) {
43         if (movementData.value == Msdp::MovementDataUtils::MovementValue::VALUE_ENTER) {
44             WifiConfigCenter::GetInstance().SetFreezeModeState(MODE_STATE_OPEN);
45         } else {
46             WifiConfigCenter::GetInstance().SetFreezeModeState(MODE_STATE_CLOSE);
47         }
48     }
49     if (movementData.type == Msdp::MovementDataUtils::MovementType::TYPE_STAY) {
50         HandleMovementChange();
51     }
52 }
53 
HandleMovementChange()54 void DeviceMovementCallback::HandleMovementChange()
55 {
56     WIFI_LOGI("HandleMovementChange enter");
57     if (!movementChangeEventHandler) {
58         WIFI_LOGE("%{public}s movementChangeEventHandler is null", __func__);
59         return;
60     }
61     movementChangeEventHandler->PostAsyncTask([this]() {
62         for (int i = 0; i < STA_INSTANCE_MAX_NUM; ++i) {
63             IScanService *pScanService = WifiServiceManager::GetInstance().GetScanServiceInst(i);
64             if (pScanService == nullptr) {
65                 WIFI_LOGE("scan service is NOT start!");
66                 return;
67             }
68             if (pScanService->OnMovingFreezeStateChange() != WIFI_OPT_SUCCESS) {
69                 WIFI_LOGE("OnMovingFreezeStateChange failed");
70             }
71         }
72     });
73 }
74 } // namespace Wifi
75 } // namespace OHOS
76