• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "devicestatus_algorithm_manager.h"
16 
17 #include <cerrno>
18 #include <string>
19 
20 #include <linux/netlink.h>
21 #include <sys/epoll.h>
22 #include <sys/timerfd.h>
23 #include <unistd.h>
24 
25 #include "devicestatus_common.h"
26 #include "devicestatus_define.h"
27 
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace {
32 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "AlgoMgr" };
33 } // namespace
34 
StartSensor(Type type)35 bool AlgoMgr::StartSensor(Type type)
36 {
37     CALL_DEBUG_ENTER;
38     int32_t sensorType = GetSensorTypeId(type);
39     if (sensorType == RET_ERR) {
40         FI_HILOGE("Failed to get sensorType");
41         return false;
42     }
43     if (!CheckSensorTypeId(sensorType)) {
44         FI_HILOGE("Sensor type mismatch");
45         return false;
46     }
47 
48     SENSOR_DATA_CB.Init();
49     if (!SENSOR_DATA_CB.RegisterCallbackSensor(sensorType)) {
50         FI_HILOGE("Failed to register callback sensor");
51         return false;
52     }
53 
54     return true;
55 }
56 
RegisterCallback(std::shared_ptr<MsdpAlgoCallback> callback)57 ErrCode AlgoMgr::RegisterCallback(std::shared_ptr<MsdpAlgoCallback> callback)
58 {
59     CALL_DEBUG_ENTER;
60     switch (algoType_) {
61         case Type::TYPE_ABSOLUTE_STILL: {
62             if (still_ != nullptr) {
63                 still_->RegisterCallback(callback);
64             }
65             break;
66         }
67         case Type::TYPE_HORIZONTAL_POSITION: {
68             if (horizontalPosition_ != nullptr) {
69                 horizontalPosition_->RegisterCallback(callback);
70             }
71             break;
72         }
73         case Type::TYPE_VERTICAL_POSITION: {
74             if (verticalPosition_ != nullptr) {
75                 verticalPosition_->RegisterCallback(callback);
76             }
77             break;
78         }
79         default: {
80             FI_HILOGE("Unsupported algorithm type");
81             return RET_ERR;
82         }
83     }
84     return RET_OK;
85 }
86 
UnregisterCallback()87 ErrCode AlgoMgr::UnregisterCallback()
88 {
89     CALL_DEBUG_ENTER;
90     return RET_OK;
91 }
92 
CheckSensorTypeId(int32_t sensorTypeId)93 bool AlgoMgr::CheckSensorTypeId(int32_t sensorTypeId)
94 {
95     int32_t count = -1;
96     SensorInfo *sensorInfo = nullptr;
97     int32_t ret = GetAllSensors(&sensorInfo, &count);
98     if (ret != 0) {
99         FI_HILOGE("Get all sensors failed");
100         return false;
101     }
102     SensorInfo *pt = sensorInfo + count;
103     for (SensorInfo *ps = sensorInfo; ps < pt; ++ps) {
104         if (ps->sensorTypeId == sensorTypeId) {
105             return true;
106         }
107     }
108     FI_HILOGE("Get sensor failed");
109     return false;
110 }
111 
GetSensorTypeId(Type type)112 int32_t AlgoMgr::GetSensorTypeId(Type type)
113 {
114     switch (type) {
115         case Type::TYPE_ABSOLUTE_STILL: {
116             return SensorTypeId::SENSOR_TYPE_ID_ACCELEROMETER;
117         }
118         case Type::TYPE_HORIZONTAL_POSITION: {
119             return SensorTypeId::SENSOR_TYPE_ID_ACCELEROMETER;
120         }
121         case Type::TYPE_VERTICAL_POSITION: {
122             return SensorTypeId::SENSOR_TYPE_ID_ACCELEROMETER;
123         }
124         default: {
125             FI_HILOGW("GetSensorTypeId failed");
126             break;
127         }
128     }
129     return RET_ERR;
130 }
131 
Enable(Type type)132 ErrCode AlgoMgr::Enable(Type type)
133 {
134     CALL_DEBUG_ENTER;
135     if (!StartSensor(type)) {
136         FI_HILOGE("sensor init failed");
137         return RET_ERR;
138     }
139     switch (type) {
140         case Type::TYPE_ABSOLUTE_STILL: {
141             if (still_ == nullptr) {
142                 FI_HILOGE("still_ is nullptr");
143                 still_ = std::make_shared<AlgoAbsoluteStill>();
144                 still_->Init(type);
145                 callAlgoNum_[type] = 0;
146             }
147             callAlgoNum_[type]++;
148             break;
149         }
150         case Type::TYPE_HORIZONTAL_POSITION: {
151             if (horizontalPosition_ == nullptr) {
152                 FI_HILOGE("horizontalPosition_ is nullptr");
153                 horizontalPosition_ = std::make_shared<AlgoHorizontal>();
154                 horizontalPosition_->Init(type);
155                 callAlgoNum_[type] = 0;
156             }
157             callAlgoNum_[type]++;
158             break;
159         }
160         case Type::TYPE_VERTICAL_POSITION: {
161             if (verticalPosition_ == nullptr) {
162                 FI_HILOGE("verticalPosition_ is nullptr");
163                 verticalPosition_ = std::make_shared<AlgoVertical>();
164                 verticalPosition_->Init(type);
165                 callAlgoNum_[type] = 0;
166             }
167             callAlgoNum_[type]++;
168             break;
169         }
170         default: {
171             FI_HILOGE("Unsupported algorithm type");
172             return RET_ERR;
173         }
174     }
175     algoType_ = type;
176     return RET_OK;
177 }
178 
Disable(Type type)179 ErrCode AlgoMgr::Disable(Type type)
180 {
181     CALL_DEBUG_ENTER;
182     callAlgoNum_[type]--;
183     FI_HILOGI("callAlgoNum_:%{public}d", callAlgoNum_[type]);
184     if (callAlgoNum_[type] != 0) {
185         FI_HILOGE("callAlgoNum_[type] is not zero");
186         return RET_ERR;
187     }
188     switch (type) {
189         case Type::TYPE_ABSOLUTE_STILL: {
190             if (still_ != nullptr) {
191                 FI_HILOGD("still_ is not nullptr");
192                 still_->Unsubscribe(type);
193                 still_ = nullptr;
194             }
195             break;
196         }
197         case Type::TYPE_HORIZONTAL_POSITION: {
198             if (horizontalPosition_ != nullptr) {
199                 FI_HILOGD("horizontalPosition_ is not nullptr");
200                 horizontalPosition_->Unsubscribe(type);
201                 horizontalPosition_ = nullptr;
202             }
203             break;
204         }
205         case Type::TYPE_VERTICAL_POSITION: {
206             if (verticalPosition_ != nullptr) {
207                 FI_HILOGD("verticalPosition_ is not nullptr");
208                 verticalPosition_->Unsubscribe(type);
209                 verticalPosition_ = nullptr;
210             }
211             break;
212         }
213         default: {
214             FI_HILOGE("Unsupported algorithm type");
215             break;
216         }
217     }
218     callAlgoNum_.erase(type);
219     UnregisterSensor(type);
220     return RET_OK;
221 }
222 
DisableCount(Type type)223 ErrCode AlgoMgr::DisableCount(Type type)
224 {
225     CALL_DEBUG_ENTER;
226     return RET_OK;
227 }
228 
UnregisterSensor(Type type)229 ErrCode AlgoMgr::UnregisterSensor(Type type)
230 {
231     CALL_DEBUG_ENTER;
232     int32_t sensorType = GetSensorTypeId(type);
233     if (sensorType == RET_ERR) {
234         FI_HILOGE("Failed to get sensorType");
235         return false;
236     }
237     if (!SENSOR_DATA_CB.UnregisterCallbackSensor(sensorType)) {
238         FI_HILOGE("Failed to unregister callback sensor");
239         return RET_ERR;
240     }
241     return RET_OK;
242 }
243 
Create(void)244 extern "C" IMsdp *Create(void)
245 {
246     CALL_DEBUG_ENTER;
247     return new (std::nothrow) AlgoMgr();
248 }
249 
Destroy(const IMsdp * algorithm)250 extern "C" void Destroy(const IMsdp* algorithm)
251 {
252     CALL_DEBUG_ENTER;
253     if (algorithm != nullptr) {
254         FI_HILOGD("algorithm is not nullptr");
255         delete algorithm;
256     }
257 }
258 } // namespace DeviceStatus
259 } // namespace Msdp
260 } // namespace OHOS
261