1 /*
2  * Copyright (c) 2022-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 #ifdef DEVICE_MOVEMENT_PERCEPTION_ENABLE
16 #include "device_movement_observer.h"
17 
18 #include "res_sched_log.h"
19 #include "res_sched_mgr.h"
20 #include "res_type.h"
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int32_t DeviceMovementObserver::OnRemoteRequest(uint32_t code, MessageParcel &data,
25     MessageParcel &reply, MessageOption &option)
26 {
27     std::u16string descriptor = DeviceMovementObserver::GetDescriptor();
28     std::u16string remoteDescriptor = data.ReadInterfaceToken();
29     if (descriptor != remoteDescriptor) {
30         RESSCHED_LOGE("DeviceMovementObserver::OnRemoteRequest failed, descriptor mismatch");
31         return -1;
32     }
33 
34     switch (code) {
35         case static_cast<int32_t>(Msdp::ImovementCallback::MOVEMENT_CHANGE): {
36             return OnReceiveDeviceMovementEvent(data);
37         }
38         default:
39             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
40     }
41     return ERR_OK;
42 }
43 
OnReceiveDeviceMovementEvent(MessageParcel & data)44 int32_t DeviceMovementObserver::OnReceiveDeviceMovementEvent(MessageParcel &data)
45 {
46     int32_t type = data.ReadInt32();
47     int32_t value = data.ReadInt32();
48     Msdp::MovementDataUtils::MovementData movementData = {
49         static_cast<Msdp::MovementDataUtils::MovementType>(type),
50         static_cast<Msdp::MovementDataUtils::MovementValue>(value)
51     };
52     OnMovementChanged(movementData);
53     return ERR_OK;
54 }
55 
OnMovementChanged(const Msdp::MovementDataUtils::MovementData & movementData)56 void DeviceMovementObserver::OnMovementChanged(const Msdp::MovementDataUtils::MovementData &movementData)
57 {
58     RESSCHED_LOGD("enter DeviceMovementObserver::OnMovementChanged");
59     nlohmann::json payload;
60     payload["type"] = std::to_string(movementData.type);
61     payload["value"] = std::to_string(movementData.value);
62     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_DEVICE_STILL_STATE_CHANGE,
63         movementData.value, payload);
64 }
65 }
66 }
67 #endif
68