1 /*
2 * Copyright (c) 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 namespace OHOS {
19 namespace Msdp {
Unsubscribe(int32_t sensorTypeId)20 void AlgoBase::Unsubscribe(int32_t sensorTypeId)
21 {
22 DEV_HILOGD(SERVICE, "Enter");
23 if (algoCallback_ == nullptr) {
24 DEV_HILOGE(SERVICE, "algoCallback is nullptr");
25 return;
26 }
27 SensorDataCallback::GetInstance().UnsubscribeSensorEvent(sensorTypeId, algoCallback_);
28 }
29
GetData(int32_t sensorTypeId,AccelData * sensorData)30 bool AlgoBase::GetData(int32_t sensorTypeId, AccelData* sensorData)
31 {
32 DEV_HILOGD(SERVICE, "Enter");
33 if (sensorTypeId != SENSOR_TYPE_ID_ACCELEROMETER) {
34 DEV_HILOGE(SERVICE, "sensorTypeId:%{public}d is invalid", sensorTypeId);
35 return false;
36 }
37 if (sensorData == nullptr) {
38 DEV_HILOGE(SERVICE, "sensorData is nullptr");
39 return false;
40 }
41
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 DEV_HILOGE(SERVICE, "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 DEV_HILOGD(SERVICE, "x:%{public}f, y:%{public}f, z:%{public}f", algoPara_.x, algoPara_.y, algoPara_.z);
54 return true;
55 }
56
RegisterCallback(std::shared_ptr<DevicestatusMsdpInterface::MsdpAlgorithmCallback> callback)57 void AlgoBase::RegisterCallback(std::shared_ptr<DevicestatusMsdpInterface::MsdpAlgorithmCallback> callback)
58 {
59 DEV_HILOGD(SERVICE, "Enter");
60 state_ = UNKNOWN;
61 callback_ = callback;
62 }
63
UnregisterCallback()64 void AlgoBase::UnregisterCallback()
65 {
66 DEV_HILOGD(SERVICE, "Enter");
67 callback_ = nullptr;
68 }
69
UpdateStateAndReport(DevicestatusDataUtils::DevicestatusValue value,int32_t state,DevicestatusDataUtils::DevicestatusType type)70 void AlgoBase::UpdateStateAndReport(DevicestatusDataUtils::DevicestatusValue value,
71 int32_t state, DevicestatusDataUtils::DevicestatusType type)
72 {
73 DEV_HILOGD(SERVICE, "Enter type:%{public}d,value:%{public}d", type, value);
74 if (callback_ == nullptr) {
75 DEV_HILOGE(SERVICE, "callback_ is nullptr");
76 return;
77 }
78 state_ = state;
79 reportInfo_.type = type;
80 reportInfo_.value = value;
81 callback_->OnResult(reportInfo_);
82 }
83 } // namespace Msdp
84 } // namespace OHOS
85