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
16 #include "algo_base.h"
17
18 #include "devicestatus_define.h"
19
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 namespace {
24 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "AlgoBase" };
25 } // namespace
26
Unsubscribe(int32_t sensorTypeId)27 void AlgoBase::Unsubscribe(int32_t sensorTypeId)
28 {
29 CALL_DEBUG_ENTER;
30 CHKPV(algoCallback_);
31 SENSOR_DATA_CB.UnsubscribeSensorEvent(sensorTypeId, algoCallback_);
32 }
33
SetData(int32_t sensorTypeId,AccelData * sensorData)34 bool AlgoBase::SetData(int32_t sensorTypeId, AccelData* sensorData)
35 {
36 CALL_DEBUG_ENTER;
37 if (sensorTypeId != SENSOR_TYPE_ID_ACCELEROMETER) {
38 FI_HILOGE("sensorTypeId:%{public}d", sensorTypeId);
39 return false;
40 }
41 CHKPF(sensorData);
42 AccelData* data = sensorData;
43 if ((abs(data->x) > ACC_VALID_THRHD) ||
44 (abs(data->y) > ACC_VALID_THRHD) ||
45 (abs(data->z) > ACC_VALID_THRHD)) {
46 FI_HILOGE("Acc data is invalid");
47 return false;
48 }
49
50 algoPara_.x = data->y;
51 algoPara_.y = data->x;
52 algoPara_.z = -(data->z);
53 FI_HILOGD("x:%{public}f, y:%{public}f, z:%{public}f", algoPara_.x, algoPara_.y, algoPara_.z);
54 return true;
55 }
56
RegisterCallback(const std::shared_ptr<IMsdp::MsdpAlgoCallback> callback)57 void AlgoBase::RegisterCallback(const std::shared_ptr<IMsdp::MsdpAlgoCallback> callback)
58 {
59 CALL_DEBUG_ENTER;
60 callback_ = callback;
61 }
62
UpdateStateAndReport(OnChangedValue value,int32_t state,Type type)63 void AlgoBase::UpdateStateAndReport(OnChangedValue value, int32_t state, Type type)
64 {
65 CALL_DEBUG_ENTER;
66 CHKPV(callback_);
67 state_ = state;
68 reportInfo_.type = type;
69 reportInfo_.value = value;
70 FI_HILOGI("type:%{public}d,value:%{public}d", type, value);
71 callback_->OnResult(reportInfo_);
72 }
73 } // namespace DeviceStatus
74 } // namespace Msdp
75 } // namespace OHOS
76