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