1 /*
2 * Copyright (c) 2021-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 "sensor_agent_proxy.h"
17
18 #include "print_sensor_data.h"
19 #include "sensor_service_client.h"
20 #include "sensor_xcollie.h"
21 #undef LOG_TAG
22 #define LOG_TAG "SensorAgentProxy"
23 using namespace OHOS::HiviewDFX;
24 namespace OHOS {
25 namespace Sensors {
26 namespace {
27 constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff;
28 constexpr int32_t MAX_SENSOR_INFO_COUNT = 0Xffff;
29 constexpr int32_t IS_LOCAL_DEVICE = 1;
30 constexpr int32_t SENSOR_ONLINE = 1;
31 std::mutex sensorInfoMutex_;
32 SensorInfoCheck sensorInfoCheck_;
33 std::mutex sensorActiveInfoMutex_;
34 SensorActiveInfo *sensorActiveInfos_ = nullptr;
35 int32_t sensorInfoCount_ = 0;
36 } // namespace
37
38 #define SEN_CLIENT SensorServiceClient::GetInstance()
39 std::recursive_mutex SensorAgentProxy::subscribeMutex_;
40 std::recursive_mutex SensorAgentProxy::subscribePlugMutex_;
41 std::mutex SensorAgentProxy::chanelMutex_;
42 std::mutex SensorAgentProxy::createChannelMutex_;
43
SensorAgentProxy()44 SensorAgentProxy::SensorAgentProxy()
45 : dataChannel_(new (std::nothrow) SensorDataChannel())
46 {}
47
~SensorAgentProxy()48 SensorAgentProxy::~SensorAgentProxy()
49 {
50 CALL_LOG_ENTER;
51 ClearSensorInfos();
52 }
53
GetSubscribeUserCallback(const SensorDescription & sensorDesc)54 std::set<RecordSensorCallback> SensorAgentProxy::GetSubscribeUserCallback(const SensorDescription &sensorDesc)
55 {
56 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
57 auto iter = subscribeMap_.find(sensorDesc);
58 if (iter == subscribeMap_.end()) {
59 SEN_HILOGE("Sensor is not subscribed");
60 return {};
61 }
62 std::set<RecordSensorCallback> callback;
63 for (const auto &it : iter->second) {
64 auto ret = callback.insert(it->callback);
65 if (!ret.second) {
66 SEN_HILOGE("callback insert fail");
67 }
68 }
69 return callback;
70 }
71
HandleSensorData(SensorEvent * events,int32_t num,void * data)72 void SensorAgentProxy::HandleSensorData(SensorEvent *events,
73 int32_t num, void *data) __attribute__((no_sanitize("cfi")))
74 {
75 CHKPV(events);
76 if (num <= 0) {
77 SEN_HILOGE("events is null or num is invalid");
78 return;
79 }
80 SensorEvent eventStream;
81 for (int32_t i = 0; i < num; ++i) {
82 eventStream = events[i];
83 auto callbacks = GetSubscribeUserCallback({eventStream.deviceId, eventStream.sensorTypeId,
84 eventStream.sensorId, eventStream.location});
85 for (const auto &callback : callbacks) {
86 CHKPV(callback);
87 if (eventStream.sensorTypeId == SENSOR_TYPE_ID_HALL_EXT) {
88 PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream);
89 }
90 callback(&eventStream);
91 PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream);
92 }
93 }
94 }
95
SetIsChannelCreated(bool isChannelCreated)96 void SensorAgentProxy::SetIsChannelCreated(bool isChannelCreated)
97 {
98 CALL_LOG_ENTER;
99 std::lock_guard<std::mutex> chanelLock(chanelMutex_);
100 isChannelCreated_ = isChannelCreated;
101 }
102
CreateSensorDataChannel()103 int32_t SensorAgentProxy::CreateSensorDataChannel()
104 {
105 CALL_LOG_ENTER;
106 std::lock_guard<std::mutex> chanelLock(chanelMutex_);
107 if (isChannelCreated_) {
108 SEN_HILOGI("The channel has already been created");
109 return ERR_OK;
110 }
111 CHKPR(dataChannel_, INVALID_POINTER);
112 auto ret = dataChannel_->CreateSensorDataChannel(
113 [weakSelf = std::weak_ptr<SensorAgentProxy>(SENSOR_AGENT_IMPL)](SensorEvent *events, int32_t num, void *data) {
114 if (auto sharedSelf = weakSelf.lock()) {
115 sharedSelf->HandleSensorData(events, num, data);
116 } else {
117 SEN_HILOGD("SensorAgentProxy object has been released");
118 }
119 }, nullptr);
120 if (ret != ERR_OK) {
121 SEN_HILOGE("Create data channel failed, ret:%{public}d", ret);
122 return ret;
123 }
124 SensorXcollie sensorXcollie("SensorAgentProxy:TransferDataChannel", XCOLLIE_TIMEOUT_5S);
125 ret = SEN_CLIENT.TransferDataChannel(dataChannel_);
126 if (ret != ERR_OK) {
127 auto destroyRet = dataChannel_->DestroySensorDataChannel();
128 SEN_HILOGE("Transfer data channel failed, ret:%{public}d, destroyRet:%{public}d", ret, destroyRet);
129 return ret;
130 }
131 isChannelCreated_ = true;
132 return ERR_OK;
133 }
134
DestroySensorDataChannel()135 int32_t SensorAgentProxy::DestroySensorDataChannel()
136 {
137 CALL_LOG_ENTER;
138 std::lock_guard<std::mutex> chanelLock(chanelMutex_);
139 if (!isChannelCreated_) {
140 SEN_HILOGI("Channel has been destroyed");
141 return ERR_OK;
142 }
143 CHKPR(dataChannel_, INVALID_POINTER);
144 int32_t ret = dataChannel_->DestroySensorDataChannel();
145 if (ret != ERR_OK) {
146 SEN_HILOGE("Destroy data channel failed, ret:%{public}d", ret);
147 return ret;
148 }
149 SensorXcollie sensorXcollie("SensorAgentProxy:DestroyDataChannel", XCOLLIE_TIMEOUT_5S);
150 ret = SEN_CLIENT.DestroyDataChannel();
151 if (ret != ERR_OK) {
152 SEN_HILOGE("Destroy service data channel fail, ret:%{public}d", ret);
153 return ret;
154 }
155 isChannelCreated_ = false;
156 return ERR_OK;
157 }
158
ActivateSensor(const SensorDescription & sensorDesc,const SensorUser * user)159 int32_t SensorAgentProxy::ActivateSensor(const SensorDescription &sensorDesc, const SensorUser *user)
160 {
161 SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
162 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
163 CHKPR(user, OHOS::Sensors::ERROR);
164 CHKPR(user->callback, OHOS::Sensors::ERROR);
165 if (samplingInterval_ < 0 || reportInterval_ < 0) {
166 SEN_HILOGE("SamplingPeriod or reportInterval_ is invalid");
167 return ERROR;
168 }
169 if (!SEN_CLIENT.IsValid(sensorDesc)) {
170 SEN_HILOGE("sensorDesc is invalid, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
171 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
172 return PARAMETER_ERROR;
173 }
174 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
175 if (subscribeMap_.find(sensorDesc) == subscribeMap_.end()) {
176 SEN_HILOGE("Subscribe sensorId first");
177 return ERROR;
178 }
179 auto& subscribeSet = subscribeMap_[sensorDesc];
180 if (subscribeSet.find(user) == subscribeSet.end()) {
181 SEN_HILOGE("Subscribe user first");
182 return ERROR;
183 }
184 SensorXcollie sensorXcollie("SensorAgentProxy:EnableSensor", XCOLLIE_TIMEOUT_15S);
185 int32_t ret = SEN_CLIENT.EnableSensor(sensorDesc, samplingInterval_, reportInterval_);
186 if (ret != 0) {
187 SEN_HILOGE("Enable sensor failed, ret:%{public}d", ret);
188 subscribeSet.erase(user);
189 if (subscribeSet.empty()) {
190 subscribeMap_.erase(sensorDesc);
191 }
192 return ret;
193 }
194 SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
195 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
196 return ret;
197 }
198
DeactivateSensor(const SensorDescription & sensorDesc,const SensorUser * user)199 int32_t SensorAgentProxy::DeactivateSensor(const SensorDescription &sensorDesc, const SensorUser *user)
200 {
201 SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d, peripheralId:%{public}d",
202 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId, sensorDesc.location);
203 CHKPR(user, OHOS::Sensors::ERROR);
204 CHKPR(user->callback, OHOS::Sensors::ERROR);
205 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
206 if ((!SEN_CLIENT.IsValid(sensorDesc)) && subscribeMap_.find(sensorDesc) == subscribeMap_.end()) {
207 SEN_HILOGE("sensorDesc is invalid, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
208 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
209 return PARAMETER_ERROR;
210 }
211 if (subscribeMap_.find(sensorDesc) == subscribeMap_.end()) {
212 SEN_HILOGE("Subscribe sensorId first");
213 return OHOS::Sensors::ERROR;
214 }
215 auto& subscribeSet = subscribeMap_[sensorDesc];
216 if (subscribeSet.find(user) == subscribeSet.end()) {
217 SEN_HILOGE("Subscribe user first");
218 return OHOS::Sensors::ERROR;
219 }
220 auto status = unsubscribeMap_[sensorDesc].insert(user);
221 if (!status.second) {
222 SEN_HILOGE("User has been unsubscribed");
223 }
224 subscribeSet.erase(user);
225 if (subscribeSet.empty()) {
226 subscribeMap_.erase(sensorDesc);
227 if (!SEN_CLIENT.IsValid(sensorDesc)) {
228 SEN_HILOGW("No need to call DisableSensor");
229 return OHOS::Sensors::SUCCESS;
230 }
231 SensorXcollie sensorXcollie("SensorAgentProxy:DisableSensor", XCOLLIE_TIMEOUT_15S);
232 int32_t ret = SEN_CLIENT.DisableSensor(sensorDesc);
233 if (ret != 0) {
234 SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
235 return ret;
236 }
237 }
238 SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
239 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
240 return OHOS::Sensors::SUCCESS;
241 }
242
SetBatch(const SensorDescription & sensorDesc,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)243 int32_t SensorAgentProxy::SetBatch(const SensorDescription &sensorDesc, const SensorUser *user,
244 int64_t samplingInterval, int64_t reportInterval)
245 {
246 CHKPR(user, OHOS::Sensors::ERROR);
247 if (!SEN_CLIENT.IsValid(sensorDesc)) {
248 SEN_HILOGE("sensorDesc is invalid, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
249 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
250 return PARAMETER_ERROR;
251 }
252 if (samplingInterval < 0 || reportInterval < 0) {
253 SEN_HILOGE("samplingInterval or reportInterval is invalid");
254 return OHOS::Sensors::ERROR;
255 }
256 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
257 if (subscribeMap_.find(sensorDesc) == subscribeMap_.end()) {
258 SEN_HILOGE("Subscribe sensorId first");
259 return OHOS::Sensors::ERROR;
260 }
261 auto& subscribeSet = subscribeMap_[sensorDesc];
262 if (subscribeSet.find(user) == subscribeSet.end()) {
263 SEN_HILOGE("Subscribe user first");
264 return OHOS::Sensors::ERROR;
265 }
266 samplingInterval_ = samplingInterval;
267 reportInterval_ = reportInterval;
268 return OHOS::Sensors::SUCCESS;
269 }
270
SubscribeSensor(const SensorDescription & sensorDesc,const SensorUser * user)271 int32_t SensorAgentProxy::SubscribeSensor(const SensorDescription &sensorDesc, const SensorUser *user)
272 {
273 SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
274 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
275 CHKPR(user, OHOS::Sensors::ERROR);
276 CHKPR(user->callback, OHOS::Sensors::ERROR);
277 if (!SEN_CLIENT.IsValid(sensorDesc)) {
278 SEN_HILOGE("sensorDesc is invalid, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
279 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
280 return PARAMETER_ERROR;
281 }
282 std::lock_guard<std::mutex> createChannelLock(createChannelMutex_);
283 int32_t ret = CreateSensorDataChannel();
284 if (ret != ERR_OK) {
285 SEN_HILOGE("Create sensor data chanel failed");
286 return OHOS::Sensors::ERROR;
287 }
288 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
289 auto status = subscribeMap_[sensorDesc].insert(user);
290 if (!status.second) {
291 SEN_HILOGE("User has been subscribed");
292 }
293 if (PrintSensorData::GetInstance().IsContinuousType(sensorDesc.sensorType)) {
294 PrintSensorData::GetInstance().SavePrintUserInfo(user->callback);
295 }
296 SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
297 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
298 return OHOS::Sensors::SUCCESS;
299 }
300
IsSubscribeMapEmpty() const301 bool SensorAgentProxy::IsSubscribeMapEmpty() const
302 {
303 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
304 return subscribeMap_.empty();
305 }
306
UnsubscribeSensor(const SensorDescription & sensorDesc,const SensorUser * user)307 int32_t SensorAgentProxy::UnsubscribeSensor(const SensorDescription &sensorDesc, const SensorUser *user)
308 {
309 SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
310 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
311 CHKPR(user, OHOS::Sensors::ERROR);
312 CHKPR(user->callback, OHOS::Sensors::ERROR);
313 {
314 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
315 if ((!SEN_CLIENT.IsValid(sensorDesc)) && unsubscribeMap_.find(sensorDesc) == unsubscribeMap_.end()) {
316 SEN_HILOGE("sensorDesc is invalid, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
317 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
318 return PARAMETER_ERROR;
319 }
320 if (unsubscribeMap_.find(sensorDesc) == unsubscribeMap_.end()) {
321 SEN_HILOGE("Deactivate sensorId first");
322 return OHOS::Sensors::ERROR;
323 }
324 auto& unsubscribeSet = unsubscribeMap_[sensorDesc];
325 if (unsubscribeSet.find(user) == unsubscribeSet.end()) {
326 SEN_HILOGE("Deactivate user first");
327 return OHOS::Sensors::ERROR;
328 }
329 unsubscribeSet.erase(user);
330 if (unsubscribeSet.empty()) {
331 unsubscribeMap_.erase(sensorDesc);
332 }
333 }
334 std::lock_guard<std::mutex> createChannelLock(createChannelMutex_);
335 if (IsSubscribeMapEmpty()) {
336 int32_t ret = DestroySensorDataChannel();
337 if (ret != ERR_OK) {
338 SEN_HILOGE("Destroy data channel fail, ret:%{public}d", ret);
339 return ret;
340 }
341 }
342 if (PrintSensorData::GetInstance().IsContinuousType(sensorDesc.sensorType)) {
343 PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback);
344 }
345 SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
346 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
347 return OHOS::Sensors::SUCCESS;
348 }
349
SetMode(const SensorDescription & sensorDesc,const SensorUser * user,int32_t mode)350 int32_t SensorAgentProxy::SetMode(const SensorDescription &sensorDesc, const SensorUser *user, int32_t mode)
351 {
352 CHKPR(user, OHOS::Sensors::ERROR);
353 CHKPR(user->callback, OHOS::Sensors::ERROR);
354 if (!SEN_CLIENT.IsValid(sensorDesc)) {
355 SEN_HILOGE("sensorDesc is invalid,deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
356 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
357 return ERROR;
358 }
359 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
360 if (subscribeMap_.find(sensorDesc) == subscribeMap_.end()) {
361 SEN_HILOGE("Subscribe sensorId first");
362 return OHOS::Sensors::ERROR;
363 }
364 auto& subscribeSet = subscribeMap_[sensorDesc];
365 if (subscribeSet.find(user) == subscribeSet.end()) {
366 SEN_HILOGE("Subscribe user first");
367 return OHOS::Sensors::ERROR;
368 }
369 return OHOS::Sensors::SUCCESS;
370 }
371
ClearSensorInfos() const372 void SensorAgentProxy::ClearSensorInfos() const
373 {
374 if (sensorActiveInfos_ != nullptr) {
375 free(sensorActiveInfos_);
376 sensorActiveInfos_ = nullptr;
377 }
378 CHKPV(sensorInfoCheck_.sensorInfos);
379 free(sensorInfoCheck_.sensorInfos);
380 sensorInfoCheck_.sensorInfos = nullptr;
381 sensorInfoCheck_.checkCode = CHECK_CODE;
382 sensorInfoCount_ = 0;
383 }
384
ConvertSensorInfos() const385 int32_t SensorAgentProxy::ConvertSensorInfos() const
386 {
387 CALL_LOG_ENTER;
388 SensorXcollie sensorXcollie("SensorAgentProxy:GetSensorList", XCOLLIE_TIMEOUT_5S);
389 std::vector<Sensor> sensorList = SEN_CLIENT.GetSensorList();
390 if (sensorList.empty()) {
391 SEN_HILOGE("Get sensor lists failed");
392 return ERROR;
393 }
394 size_t count = sensorList.size();
395 if (count > MAX_SENSOR_LIST_SIZE) {
396 SEN_HILOGE("The number of sensors exceeds the maximum value");
397 return ERROR;
398 }
399 if (sensorInfoCount_ > 0 && sensorInfoCount_ == static_cast<int32_t>(count)) {
400 return SUCCESS;
401 } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast<int32_t>(count) &&
402 sensorInfoCheck_.checkCode == CHECK_CODE) {
403 SEN_HILOGW("sensorInfos_ error, sensorInfoCount_:%{public}d, sensorListCount:%{public}d", sensorInfoCount_,
404 static_cast<int32_t>(count));
405 ClearSensorInfos();
406 } else if (sensorInfoCheck_.checkCode != CHECK_CODE) {
407 SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode);
408 ClearSensorInfos();
409 }
410 sensorInfoCheck_.sensorInfos = (SensorInfo *)malloc(sizeof(SensorInfo) * count);
411 CHKPR(sensorInfoCheck_.sensorInfos, ERROR);
412 SEN_HILOGI("Sensor count is %{public}zu", count);
413 for (size_t i = 0; i < count; ++i) {
414 SensorInfo *sensorInfo = sensorInfoCheck_.sensorInfos + i;
415 errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN,
416 sensorList[i].GetSensorName().c_str());
417 CHKCR(ret == EOK, ERROR);
418 ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN,
419 sensorList[i].GetVendorName().c_str());
420 CHKCR(ret == EOK, ERROR);
421 ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN,
422 sensorList[i].GetHardwareVersion().c_str());
423 CHKCR(ret == EOK, ERROR);
424 ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN,
425 sensorList[i].GetFirmwareVersion().c_str());
426 CHKCR(ret == EOK, ERROR);
427 sensorInfo->deviceId = sensorList[i].GetDeviceId();
428 sensorInfo->sensorId = sensorList[i].GetSensorTypeId();
429 sensorInfo->sensorTypeId = sensorList[i].GetSensorTypeId();
430 sensorInfo->sensorIndex = sensorList[i].GetSensorId();
431 sensorInfo->location = sensorList[i].GetLocation();
432 sensorInfo->maxRange = sensorList[i].GetMaxRange();
433 sensorInfo->precision = sensorList[i].GetResolution();
434 sensorInfo->power = sensorList[i].GetPower();
435 sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs();
436 sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs();
437 SEN_HILOGI("deviceId %{public}d: sensorTypeId %{public}d, sensorIndex %{public}d",
438 sensorInfo->deviceId, sensorInfo->sensorTypeId, sensorInfo->sensorIndex);
439 }
440 sensorInfoCount_ = static_cast<int32_t>(count);
441 return SUCCESS;
442 }
443
GetDeviceSensors(int32_t deviceId,SensorInfo ** singleDevSensorInfo,int32_t * count)444 int32_t SensorAgentProxy::GetDeviceSensors(int32_t deviceId, SensorInfo **singleDevSensorInfo, int32_t *count)
445 {
446 CALL_LOG_ENTER;
447 CHKPR(singleDevSensorInfo, OHOS::Sensors::ERROR);
448 CHKPR(count, OHOS::Sensors::ERROR);
449 std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
450 if (sensorInfoCheck_.sensorInfos == nullptr) {
451 int32_t ret = ConvertSensorInfos();
452 if (ret != ERR_OK) {
453 SEN_HILOGE("Convert sensor lists failed");
454 ClearSensorInfos();
455 return ERROR;
456 }
457 }
458 std::vector<Sensor> singleDevSensors;
459 int32_t ret = SEN_CLIENT.GetSensorListByDevice(deviceId, singleDevSensors);
460 if (ret != ERR_OK || singleDevSensors.empty()) {
461 SEN_HILOGE("Get device sensor lists failed");
462 return ERROR;
463 }
464 ret = UpdateSensorInfosCache(singleDevSensors);
465 if (ret != ERR_OK) {
466 SEN_HILOGE("Update sensor infos cache failed");
467 return ERROR;
468 }
469 std::vector<SensorInfo> filteredSensorInfos;
470 for (int32_t i = 0; i < sensorInfoCount_; ++i) {
471 if (sensorInfoCheck_.sensorInfos[i].deviceId == deviceId) {
472 filteredSensorInfos.push_back(sensorInfoCheck_.sensorInfos[i]);
473 }
474 }
475 int32_t filteredCount = static_cast<int32_t>(filteredSensorInfos.size());
476 auto newSensorInfo = std::make_unique<SensorInfo[]>(filteredCount);
477 if (!newSensorInfo) {
478 SEN_HILOGE("Failed to allocate memory for filtered sensorInfos");
479 return ERROR;
480 }
481 for (int32_t i = 0; i < filteredCount; ++i) {
482 newSensorInfo[i] = filteredSensorInfos[i];
483 }
484 free(*singleDevSensorInfo);
485 *singleDevSensorInfo = newSensorInfo.release();
486 *count = filteredCount;
487 return SUCCESS;
488 }
489
UpdateSensorInfo(SensorInfo * sensorInfo,const Sensor & sensor)490 int32_t SensorAgentProxy::UpdateSensorInfo(SensorInfo* sensorInfo, const Sensor& sensor)
491 {
492 CALL_LOG_ENTER;
493 CHKPR(sensorInfo, OHOS::Sensors::ERROR);
494 errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN,
495 sensor.GetSensorName().c_str());
496 CHKCR(ret == EOK, ERROR);
497 ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN,
498 sensor.GetVendorName().c_str());
499 CHKCR(ret == EOK, ERROR);
500 ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN,
501 sensor.GetHardwareVersion().c_str());
502 CHKCR(ret == EOK, ERROR);
503 ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN,
504 sensor.GetFirmwareVersion().c_str());
505 CHKCR(ret == EOK, ERROR);
506 sensorInfo->deviceId = sensor.GetDeviceId();
507 sensorInfo->sensorId = sensor.GetSensorTypeId();
508 sensorInfo->sensorTypeId = sensor.GetSensorTypeId();
509 sensorInfo->sensorIndex = sensor.GetSensorId();
510 sensorInfo->location = sensor.GetLocation();
511 sensorInfo->maxRange = sensor.GetMaxRange();
512 sensorInfo->precision = sensor.GetResolution();
513 sensorInfo->power = sensor.GetPower();
514 sensorInfo->minSamplePeriod = sensor.GetMinSamplePeriodNs();
515 sensorInfo->maxSamplePeriod = sensor.GetMaxSamplePeriodNs();
516 return SUCCESS;
517 }
518
FindSensorInfo(int32_t deviceId,int32_t sensorIndex,int32_t sensorTypeId)519 bool SensorAgentProxy::FindSensorInfo(int32_t deviceId, int32_t sensorIndex, int32_t sensorTypeId)
520 {
521 CALL_LOG_ENTER;
522 if (sensorInfoCheck_.sensorInfos == nullptr) {
523 return false;
524 }
525 for (int32_t i = 0; i < sensorInfoCount_; ++i) {
526 if (sensorInfoCheck_.sensorInfos[i].deviceId == deviceId &&
527 sensorInfoCheck_.sensorInfos[i].sensorIndex == sensorIndex &&
528 sensorInfoCheck_.sensorInfos[i].sensorTypeId == sensorTypeId) {
529 SEN_HILOGI("FindSensorInfo deviceIndex:%{public}d, sensorTypeId:%{public}d, sensorId:%{public}d",
530 deviceId, sensorTypeId, sensorIndex);
531 return true;
532 }
533 }
534 return false;
535 }
536
UpdateSensorInfosCache(const std::vector<Sensor> & singleDevSensors)537 int32_t SensorAgentProxy::UpdateSensorInfosCache(const std::vector<Sensor>& singleDevSensors)
538 {
539 CALL_LOG_ENTER;
540 size_t newSensorsCount = 0;
541 for (const auto& sensor : singleDevSensors) {
542 if (!FindSensorInfo(sensor.GetDeviceId(), sensor.GetSensorId(), sensor.GetSensorTypeId())) {
543 newSensorsCount++;
544 }
545 }
546 if (newSensorsCount == 0) {
547 return SUCCESS;
548 }
549 if (sensorInfoCount_ < 0 || sensorInfoCount_ > MAX_SENSOR_INFO_COUNT) {
550 SEN_HILOGE("sensorInfoCount_ invalid, sensorInfoCount_:%{public}d", sensorInfoCount_);
551 return ERROR;
552 }
553 size_t currentInfoCount = static_cast<size_t>(sensorInfoCount_);
554 size_t newTotalCount = currentInfoCount + newSensorsCount;
555 if (newTotalCount > MAX_SENSOR_LIST_SIZE) {
556 SEN_HILOGE("The number of sensors exceeds the maximum value");
557 return ERROR;
558 }
559 std::unique_ptr<SensorInfo[]> newSensorInfos = std::make_unique<SensorInfo[]>(newTotalCount);
560 if (newSensorInfos == nullptr) {
561 SEN_HILOGE("Failed to allocate memory for newSensorInfos");
562 return ERROR;
563 }
564 if (sensorInfoCheck_.sensorInfos) {
565 std::copy(sensorInfoCheck_.sensorInfos, sensorInfoCheck_.sensorInfos + sensorInfoCount_, newSensorInfos.get());
566 }
567 for (const auto& sensor : singleDevSensors) {
568 if (!FindSensorInfo(sensor.GetDeviceId(), sensor.GetSensorId(), sensor.GetSensorTypeId())) {
569 UpdateSensorInfo(&newSensorInfos[currentInfoCount++], sensor);
570 }
571 }
572 sensorInfoCheck_.sensorInfos = newSensorInfos.release();
573 sensorInfoCount_ = static_cast<int32_t>(newTotalCount);
574 return SUCCESS;
575 }
576
GetLocalDeviceId(int32_t & deviceId) const577 int32_t SensorAgentProxy::GetLocalDeviceId(int32_t& deviceId) const
578 {
579 CALL_LOG_ENTER;
580 return SEN_CLIENT.GetLocalDeviceId(deviceId);
581 }
582
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count) const583 int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const
584 {
585 CALL_LOG_ENTER;
586 CHKPR(sensorInfo, OHOS::Sensors::ERROR);
587 CHKPR(count, OHOS::Sensors::ERROR);
588 std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
589 int32_t ret = ConvertSensorInfos();
590 if (ret != SUCCESS) {
591 SEN_HILOGE("Convert sensor lists failed");
592 ClearSensorInfos();
593 return ERROR;
594 }
595 if (sensorInfoCheck_.checkCode != CHECK_CODE) {
596 SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode);
597 ClearSensorInfos();
598 return ERROR;
599 }
600 CHKPR(sensorInfoCheck_.sensorInfos, OHOS::Sensors::ERROR);
601 *sensorInfo = sensorInfoCheck_.sensorInfos;
602 *count = sensorInfoCount_;
603 PrintSensorData::GetInstance().PrintSensorInfo(sensorInfoCheck_.sensorInfos, sensorInfoCount_);
604 return SUCCESS;
605 }
606
SuspendSensors(int32_t pid)607 int32_t SensorAgentProxy::SuspendSensors(int32_t pid)
608 {
609 CALL_LOG_ENTER;
610 if (pid < 0) {
611 SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
612 return PARAMETER_ERROR;
613 }
614 SensorXcollie sensorXcollie("SensorAgentProxy:SuspendSensors", XCOLLIE_TIMEOUT_5S);
615 int32_t ret = SEN_CLIENT.SuspendSensors(pid);
616 if (ret != ERR_OK) {
617 SEN_HILOGD("Suspend sensors failed, ret:%{public}d", ret);
618 }
619 return ret;
620 }
621
ResumeSensors(int32_t pid)622 int32_t SensorAgentProxy::ResumeSensors(int32_t pid)
623 {
624 CALL_LOG_ENTER;
625 if (pid < 0) {
626 SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
627 return PARAMETER_ERROR;
628 }
629 SensorXcollie sensorXcollie("SensorAgentProxy:ResumeSensors", XCOLLIE_TIMEOUT_5S);
630 int32_t ret = SEN_CLIENT.ResumeSensors(pid);
631 if (ret != ERR_OK) {
632 SEN_HILOGD("Resume sensors failed, ret:%{public}d", ret);
633 }
634 return ret;
635 }
636
GetSensorActiveInfos(int32_t pid,SensorActiveInfo ** sensorActiveInfos,int32_t * count) const637 int32_t SensorAgentProxy::GetSensorActiveInfos(int32_t pid,
638 SensorActiveInfo **sensorActiveInfos, int32_t *count) const
639 {
640 CALL_LOG_ENTER;
641 if (pid < 0) {
642 SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
643 return PARAMETER_ERROR;
644 }
645 CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR);
646 CHKPR(count, OHOS::Sensors::ERROR);
647 std::lock_guard<std::mutex> sensorActiveInfoLock(sensorActiveInfoMutex_);
648 if (sensorActiveInfos_ != nullptr) {
649 free(sensorActiveInfos_);
650 sensorActiveInfos_ = nullptr;
651 }
652 std::vector<ActiveInfo> activeInfoList;
653 SensorXcollie sensorXcollie("SensorAgentProxy:GetActiveInfoList", XCOLLIE_TIMEOUT_5S);
654 int32_t ret = SEN_CLIENT.GetActiveInfoList(pid, activeInfoList);
655 if (ret != ERR_OK) {
656 SEN_HILOGE("Get active info list failed, ret:%{public}d", ret);
657 return ret;
658 }
659 if (activeInfoList.empty()) {
660 SEN_HILOGD("Active info list is empty");
661 *sensorActiveInfos = nullptr;
662 *count = 0;
663 return ERR_OK;
664 }
665 size_t activeInfoCount = activeInfoList.size();
666 if (activeInfoCount > MAX_SENSOR_LIST_SIZE) {
667 SEN_HILOGE("The number of active info exceeds the maximum value, count:%{public}zu", activeInfoCount);
668 return ERROR;
669 }
670 sensorActiveInfos_ = (SensorActiveInfo *)malloc(sizeof(SensorActiveInfo) * activeInfoCount);
671 CHKPR(sensorActiveInfos_, ERROR);
672 for (size_t i = 0; i < activeInfoCount; ++i) {
673 SensorActiveInfo *curActiveInfo = sensorActiveInfos_ + i;
674 curActiveInfo->pid = activeInfoList[i].GetPid();
675 curActiveInfo->sensorId = activeInfoList[i].GetSensorId();
676 curActiveInfo->samplingPeriodNs = activeInfoList[i].GetSamplingPeriodNs();
677 curActiveInfo->maxReportDelayNs = activeInfoList[i].GetMaxReportDelayNs();
678 }
679 *sensorActiveInfos = sensorActiveInfos_;
680 *count = static_cast<int32_t>(activeInfoCount);
681 return ERR_OK;
682 }
683
Register(SensorActiveInfoCB callback)684 int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback)
685 {
686 CHKPR(callback, OHOS::Sensors::ERROR);
687 CHKPR(dataChannel_, INVALID_POINTER);
688 SensorXcollie sensorXcollie("SensorAgentProxy:Register", XCOLLIE_TIMEOUT_5S);
689 int32_t ret = SEN_CLIENT.Register(callback, dataChannel_);
690 if (ret != ERR_OK) {
691 SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
692 }
693 return ret;
694 }
695
Unregister(SensorActiveInfoCB callback)696 int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback)
697 {
698 CHKPR(callback, OHOS::Sensors::ERROR);
699 SensorXcollie sensorXcollie("SensorAgentProxy:Unregister", XCOLLIE_TIMEOUT_5S);
700 int32_t ret = SEN_CLIENT.Unregister(callback);
701 if (ret != ERR_OK) {
702 SEN_HILOGE("Unregister sensor active info callback failed, ret:%{public}d", ret);
703 }
704 return ret;
705 }
706
ResetSensors() const707 int32_t SensorAgentProxy::ResetSensors() const
708 {
709 SensorXcollie sensorXcollie("SensorAgentProxy:ResetSensors", XCOLLIE_TIMEOUT_5S);
710 int32_t ret = SEN_CLIENT.ResetSensors();
711 if (ret != ERR_OK) {
712 SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret);
713 }
714 return ret;
715 }
716
SetDeviceStatus(uint32_t deviceStatus) const717 void SensorAgentProxy::SetDeviceStatus(uint32_t deviceStatus) const
718 {
719 SEN_HILOGI("SetDeviceStatus in, deviceStatus:%{public}d", deviceStatus);
720 SEN_CLIENT.SetDeviceStatus(deviceStatus);
721 }
722
SubscribeSensorPlug(const SensorUser * user)723 int32_t SensorAgentProxy::SubscribeSensorPlug(const SensorUser *user)
724 {
725 CALL_LOG_ENTER;
726 CHKPR(user, OHOS::Sensors::ERROR);
727 CHKPR(user->plugCallback, OHOS::Sensors::ERROR);
728
729 int32_t ret = 0;
730 ret = SEN_CLIENT.CreateClientRemoteObject();
731 if (ret != ERR_OK) {
732 SEN_HILOGE("CreateClientRemoteObject failed, ret:%{public}d", ret);
733 return ret;
734 }
735
736 std::lock_guard<std::recursive_mutex> subscribePlugLock(subscribePlugMutex_);
737 auto status = subscribeSet_.insert(user);
738 if (!status.second) {
739 SEN_HILOGD("User has been subscribed");
740 }
741 return OHOS::Sensors::SUCCESS;
742 }
743
UnsubscribeSensorPlug(const SensorUser * user)744 int32_t SensorAgentProxy::UnsubscribeSensorPlug(const SensorUser *user)
745 {
746 CALL_LOG_ENTER;
747 CHKPR(user, OHOS::Sensors::ERROR);
748 CHKPR(user->plugCallback, OHOS::Sensors::ERROR);
749
750 std::lock_guard<std::recursive_mutex> subscribePlugLock(subscribePlugMutex_);
751 if (subscribeSet_.find(user) == subscribeSet_.end()) {
752 SEN_HILOGD("Deactivate user first");
753 return OHOS::Sensors::ERROR;
754 }
755 subscribeSet_.erase(user);
756 return OHOS::Sensors::SUCCESS;
757 }
758
UpdateSensorStatusEvent(SensorStatusEvent & event,const SensorPlugData & info)759 void SensorAgentProxy::UpdateSensorStatusEvent(SensorStatusEvent &event, const SensorPlugData &info)
760 {
761 event.sensorType = info.sensorTypeId;
762 event.sensorId = info.sensorId;
763 event.isSensorOnline = false;
764 if (info.status == SENSOR_ONLINE) {
765 event.isSensorOnline = true;
766 }
767 event.deviceId = info.deviceId;
768 event.deviceName = info.deviceName;
769 event.location = false;
770 if (info.location == IS_LOCAL_DEVICE) {
771 event.location = true;
772 }
773 event.timestamp = info.timestamp;
774 }
775
UpdateSensorInfo(const SensorPlugData & info)776 bool SensorAgentProxy::UpdateSensorInfo(const SensorPlugData &info)
777 {
778 CALL_LOG_ENTER;
779 if (info.status == SENSOR_ONLINE) {
780 SensorInfo *sensorInfos = nullptr;
781 int32_t count = 0;
782 int32_t ret = GetDeviceSensors(info.deviceId, &sensorInfos, &count);
783 if (ret != ERR_OK) {
784 SEN_HILOGE("UpdateSensorInfo failed");
785 return false;
786 }
787 free(sensorInfos);
788 sensorInfos = nullptr;
789 } else {
790 {
791 std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
792 int32_t ret = 0;
793 if (subscribeMap_.find({info.deviceId, info.sensorTypeId, info.sensorId, info.location}) !=
794 subscribeMap_.end()) {
795 ret = SEN_CLIENT.DisableSensor({info.deviceId, info.sensorTypeId, info.sensorId, info.location});
796 }
797 if (ret != 0) {
798 SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
799 }
800 }
801 if (!(SEN_CLIENT.EraseCacheSensorList(info))) {
802 SEN_HILOGE("EraseCacheSensorList failed");
803 return false;
804 }
805 EraseCacheSensorInfos(info);
806 }
807 return true;
808 }
809
EraseCacheSensorInfos(const SensorPlugData & info)810 void SensorAgentProxy::EraseCacheSensorInfos(const SensorPlugData &info)
811 {
812 std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
813 for (int32_t i = 0; i < sensorInfoCount_; ++i) {
814 if ((sensorInfoCheck_.sensorInfos[i].deviceId == info.deviceId) &&
815 (sensorInfoCheck_.sensorInfos[i].sensorTypeId == info.sensorTypeId) &&
816 (sensorInfoCheck_.sensorInfos[i].sensorIndex == info.sensorId)) {
817 for (int j = i; j < sensorInfoCount_ - 1; ++j) {
818 sensorInfoCheck_.sensorInfos[j] = sensorInfoCheck_.sensorInfos[j+1];
819 }
820 sensorInfoCount_--;
821 break;
822 }
823 }
824 }
825
HandlePlugSensorData(const SensorPlugData & info)826 bool SensorAgentProxy::HandlePlugSensorData(const SensorPlugData &info) __attribute__((no_sanitize("cfi")))
827 {
828 CALL_LOG_ENTER;
829 if (!UpdateSensorInfo(info)) {
830 return false;
831 }
832 std::lock_guard<std::recursive_mutex> subscribePlugLock(subscribePlugMutex_);
833 SensorStatusEvent event;
834 UpdateSensorStatusEvent(event, info);
835 for (const auto &it : subscribeSet_) {
836 if (it->plugCallback != nullptr) {
837 SEN_HILOGD("plug callback is run");
838 it->plugCallback(&event);
839 }
840 }
841 return true;
842 }
843 } // namespace Sensors
844 } // namespace OHOS