• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::mutex sensorInfoMutex_;
29 SensorInfoCheck sensorInfoCheck_;
30 std::mutex sensorActiveInfoMutex_;
31 SensorActiveInfo *sensorActiveInfos_ = nullptr;
32 int32_t sensorInfoCount_ = 0;
33 } // namespace
34 
35 #define SEN_CLIENT SensorServiceClient::GetInstance()
36 std::recursive_mutex SensorAgentProxy::subscribeMutex_;
37 std::mutex SensorAgentProxy::chanelMutex_;
38 std::mutex SensorAgentProxy::createChannelMutex_;
39 
SensorAgentProxy()40 SensorAgentProxy::SensorAgentProxy()
41     : dataChannel_(new (std::nothrow) SensorDataChannel())
42 {}
43 
~SensorAgentProxy()44 SensorAgentProxy::~SensorAgentProxy()
45 {
46     CALL_LOG_ENTER;
47     ClearSensorInfos();
48 }
49 
GetSubscribeUserCallback(int32_t sensorId)50 std::set<RecordSensorCallback> SensorAgentProxy::GetSubscribeUserCallback(int32_t sensorId)
51 {
52     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
53     auto iter = subscribeMap_.find(sensorId);
54     if (iter == subscribeMap_.end()) {
55         SEN_HILOGE("Sensor is not subscribed");
56         return {};
57     }
58     std::set<RecordSensorCallback> callback;
59     for (const auto &it : iter->second) {
60         auto ret = callback.insert(it->callback);
61         if (!ret.second) {
62             SEN_HILOGE("callback insert fail");
63         }
64     }
65     return callback;
66 }
67 
HandleSensorData(SensorEvent * events,int32_t num,void * data)68 void SensorAgentProxy::HandleSensorData(SensorEvent *events,
69     int32_t num, void *data) __attribute__((no_sanitize("cfi")))
70 {
71     CHKPV(events);
72     if (num <= 0) {
73         SEN_HILOGE("events is null or num is invalid");
74         return;
75     }
76     SensorEvent eventStream;
77     for (int32_t i = 0; i < num; ++i) {
78         eventStream = events[i];
79         auto callbacks = GetSubscribeUserCallback(eventStream.sensorTypeId);
80         for (const auto &callback : callbacks) {
81             CHKPV(callback);
82             if (eventStream.sensorTypeId == SENSOR_TYPE_ID_HALL_EXT) {
83                 PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream);
84             }
85             callback(&eventStream);
86             PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream);
87         }
88     }
89 }
90 
SetIsChannelCreated(bool isChannelCreated)91 void SensorAgentProxy::SetIsChannelCreated(bool isChannelCreated)
92 {
93     CALL_LOG_ENTER;
94     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
95     isChannelCreated_ = isChannelCreated;
96 }
97 
CreateSensorDataChannel()98 int32_t SensorAgentProxy::CreateSensorDataChannel()
99 {
100     SEN_HILOGI("In");
101     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
102     if (isChannelCreated_) {
103         SEN_HILOGI("The channel has already been created");
104         return ERR_OK;
105     }
106     CHKPR(dataChannel_, INVALID_POINTER);
107     auto ret = dataChannel_->CreateSensorDataChannel([this] (SensorEvent *events, int32_t num, void *data) {
108         this->HandleSensorData(events, num, data);
109     }, nullptr);
110     if (ret != ERR_OK) {
111         SEN_HILOGE("Create data channel failed, ret:%{public}d", ret);
112         return ret;
113     }
114     {
115         SensorXcollie SensorXcollie("SensorAgentProxy:TransferDataChannel", XCOLLIE_TIMEOUT_5S);
116         ret = SEN_CLIENT.TransferDataChannel(dataChannel_);
117     }
118     if (ret != ERR_OK) {
119         auto destroyRet = dataChannel_->DestroySensorDataChannel();
120         SEN_HILOGE("Transfer data channel failed, ret:%{public}d, destroyRet:%{public}d", ret, destroyRet);
121         return ret;
122     }
123     isChannelCreated_ = true;
124     SEN_HILOGI("Done");
125     return ERR_OK;
126 }
127 
DestroySensorDataChannel()128 int32_t SensorAgentProxy::DestroySensorDataChannel()
129 {
130     SEN_HILOGI("In");
131     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
132     if (!isChannelCreated_) {
133         SEN_HILOGI("Channel has been destroyed");
134         return ERR_OK;
135     }
136     CHKPR(dataChannel_, INVALID_POINTER);
137     int32_t ret = dataChannel_->DestroySensorDataChannel();
138     if (ret != ERR_OK) {
139         SEN_HILOGE("Destroy data channel failed, ret:%{public}d", ret);
140         return ret;
141     }
142     {
143         SensorXcollie SensorXcollie("SensorAgentProxy:DestroyDataChannel", XCOLLIE_TIMEOUT_5S);
144         ret = SEN_CLIENT.DestroyDataChannel();
145     }
146     if (ret != ERR_OK) {
147         SEN_HILOGE("Destroy service data channel fail, ret:%{public}d", ret);
148         return ret;
149     }
150     isChannelCreated_ = false;
151     SEN_HILOGI("Done");
152     return ERR_OK;
153 }
154 
ActivateSensor(int32_t sensorId,const SensorUser * user)155 int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user)
156 {
157     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
158     CHKPR(user, OHOS::Sensors::ERROR);
159     CHKPR(user->callback, OHOS::Sensors::ERROR);
160     if (samplingInterval_ < 0 || reportInterval_ < 0) {
161         SEN_HILOGE("SamplingPeriod or reportInterval_ is invalid");
162         return ERROR;
163     }
164     if (!SEN_CLIENT.IsValid(sensorId)) {
165         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
166         return PARAMETER_ERROR;
167     }
168     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
169     if (subscribeMap_.find(sensorId) == subscribeMap_.end()) {
170         SEN_HILOGE("Subscribe sensorId first");
171         return ERROR;
172     }
173     auto& subscribeSet = subscribeMap_[sensorId];
174     if (subscribeSet.find(user) == subscribeSet.end()) {
175         SEN_HILOGE("Subscribe user first");
176         return ERROR;
177     }
178     int32_t ret = 0;
179     {
180         SensorXcollie SensorXcollie("SensorAgentProxy:EnableSensor", XCOLLIE_TIMEOUT_15S);
181         ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval_, reportInterval_);
182     }
183     if (ret != 0) {
184         SEN_HILOGE("Enable sensor failed, ret:%{public}d", ret);
185         subscribeSet.erase(user);
186         if (subscribeSet.empty()) {
187             subscribeMap_.erase(sensorId);
188         }
189         return ret;
190     }
191     SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
192     return ret;
193 }
194 
DeactivateSensor(int32_t sensorId,const SensorUser * user)195 int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user)
196 {
197     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
198     CHKPR(user, OHOS::Sensors::ERROR);
199     CHKPR(user->callback, OHOS::Sensors::ERROR);
200     if (!SEN_CLIENT.IsValid(sensorId)) {
201         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
202         return PARAMETER_ERROR;
203     }
204     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
205     if (subscribeMap_.find(sensorId) == subscribeMap_.end()) {
206         SEN_HILOGE("Subscribe sensorId first");
207         return OHOS::Sensors::ERROR;
208     }
209     auto& subscribeSet = subscribeMap_[sensorId];
210     if (subscribeSet.find(user) == subscribeSet.end()) {
211         SEN_HILOGE("Subscribe user first");
212         return OHOS::Sensors::ERROR;
213     }
214     auto status = unsubscribeMap_[sensorId].insert(user);
215     if (!status.second) {
216         SEN_HILOGE("User has been unsubscribed");
217     }
218     subscribeSet.erase(user);
219     if (subscribeSet.empty()) {
220         subscribeMap_.erase(sensorId);
221         int32_t ret = 0;
222         {
223             SensorXcollie SensorXcollie("SensorAgentProxy:DisableSensor", XCOLLIE_TIMEOUT_15S);
224             ret = SEN_CLIENT.DisableSensor(sensorId);
225         }
226         if (ret != 0) {
227             SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
228             return ret;
229         }
230     }
231     SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
232     return OHOS::Sensors::SUCCESS;
233 }
234 
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)235 int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval,
236                                    int64_t reportInterval)
237 {
238     CHKPR(user, OHOS::Sensors::ERROR);
239     if (!SEN_CLIENT.IsValid(sensorId)) {
240         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
241         return PARAMETER_ERROR;
242     }
243     if (samplingInterval < 0 || reportInterval < 0) {
244         SEN_HILOGE("samplingInterval or reportInterval is invalid");
245         return OHOS::Sensors::ERROR;
246     }
247     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
248     if (subscribeMap_.find(sensorId) == subscribeMap_.end()) {
249         SEN_HILOGE("Subscribe sensorId first");
250         return OHOS::Sensors::ERROR;
251     }
252     auto& subscribeSet = subscribeMap_[sensorId];
253     if (subscribeSet.find(user) == subscribeSet.end()) {
254         SEN_HILOGE("Subscribe user first");
255         return OHOS::Sensors::ERROR;
256     }
257     samplingInterval_ = samplingInterval;
258     reportInterval_ = reportInterval;
259     return OHOS::Sensors::SUCCESS;
260 }
261 
SubscribeSensor(int32_t sensorId,const SensorUser * user)262 int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *user)
263 {
264     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
265     CHKPR(user, OHOS::Sensors::ERROR);
266     CHKPR(user->callback, OHOS::Sensors::ERROR);
267     if (!SEN_CLIENT.IsValid(sensorId)) {
268         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
269         return PARAMETER_ERROR;
270     }
271     std::lock_guard<std::mutex> createChannelLock(createChannelMutex_);
272     int32_t ret = CreateSensorDataChannel();
273     if (ret != ERR_OK) {
274         SEN_HILOGE("Create sensor data chanel failed");
275         return OHOS::Sensors::ERROR;
276     }
277     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
278     auto status = subscribeMap_[sensorId].insert(user);
279     if (!status.second) {
280         SEN_HILOGE("User has been subscribed");
281     }
282     if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) {
283         PrintSensorData::GetInstance().SavePrintUserInfo(user->callback);
284     }
285     SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
286     return OHOS::Sensors::SUCCESS;
287 }
288 
IsSubscribeMapEmpty() const289 bool SensorAgentProxy::IsSubscribeMapEmpty() const
290 {
291     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
292     return subscribeMap_.empty();
293 }
294 
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)295 int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
296 {
297     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
298     CHKPR(user, OHOS::Sensors::ERROR);
299     CHKPR(user->callback, OHOS::Sensors::ERROR);
300     if (!SEN_CLIENT.IsValid(sensorId)) {
301         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
302         return PARAMETER_ERROR;
303     }
304     {
305         std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
306         if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end()) {
307             SEN_HILOGE("Deactivate sensorId first");
308             return OHOS::Sensors::ERROR;
309         }
310         auto& unsubscribeSet = unsubscribeMap_[sensorId];
311         if (unsubscribeSet.find(user) == unsubscribeSet.end()) {
312             SEN_HILOGE("Deactivate user first");
313             return OHOS::Sensors::ERROR;
314         }
315         unsubscribeSet.erase(user);
316         if (unsubscribeSet.empty()) {
317             unsubscribeMap_.erase(sensorId);
318         }
319     }
320     std::lock_guard<std::mutex> createChannelLock(createChannelMutex_);
321     if (IsSubscribeMapEmpty()) {
322         int32_t ret = DestroySensorDataChannel();
323         if (ret != ERR_OK) {
324             SEN_HILOGE("Destroy data channel fail, ret:%{public}d", ret);
325             return ret;
326         }
327     }
328     if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) {
329         PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback);
330     }
331     SEN_HILOGI("Done, sensorId:%{public}d", sensorId);
332     return OHOS::Sensors::SUCCESS;
333 }
334 
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)335 int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
336 {
337     CHKPR(user, OHOS::Sensors::ERROR);
338     CHKPR(user->callback, OHOS::Sensors::ERROR);
339     if (!SEN_CLIENT.IsValid(sensorId)) {
340         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
341         return ERROR;
342     }
343     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
344     if (subscribeMap_.find(sensorId) == subscribeMap_.end()) {
345         SEN_HILOGE("Subscribe sensorId first");
346         return OHOS::Sensors::ERROR;
347     }
348     auto& subscribeSet = subscribeMap_[sensorId];
349     if (subscribeSet.find(user) == subscribeSet.end()) {
350         SEN_HILOGE("Subscribe user first");
351         return OHOS::Sensors::ERROR;
352     }
353     return OHOS::Sensors::SUCCESS;
354 }
355 
ClearSensorInfos() const356 void SensorAgentProxy::ClearSensorInfos() const
357 {
358     if (sensorActiveInfos_ != nullptr) {
359         free(sensorActiveInfos_);
360         sensorActiveInfos_ = nullptr;
361     }
362     CHKPV(sensorInfoCheck_.sensorInfos);
363     free(sensorInfoCheck_.sensorInfos);
364     sensorInfoCheck_.sensorInfos = nullptr;
365     sensorInfoCheck_.checkCode = CHECK_CODE;
366     sensorInfoCount_ = 0;
367 }
368 
ConvertSensorInfos() const369 int32_t SensorAgentProxy::ConvertSensorInfos() const
370 {
371     CALL_LOG_ENTER;
372     std::vector<Sensor> sensorList;
373     {
374         SensorXcollie SensorXcollie("SensorAgentProxy:GetSensorList", XCOLLIE_TIMEOUT_5S);
375         sensorList = SEN_CLIENT.GetSensorList();
376     }
377     if (sensorList.empty()) {
378         SEN_HILOGE("Get sensor lists failed");
379         return ERROR;
380     }
381     size_t count = sensorList.size();
382     if (count > MAX_SENSOR_LIST_SIZE) {
383         SEN_HILOGE("The number of sensors exceeds the maximum value");
384         return ERROR;
385     }
386     if (sensorInfoCount_ > 0 && sensorInfoCount_ == static_cast<int32_t>(count)) {
387         return SUCCESS;
388     } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast<int32_t>(count) &&
389         sensorInfoCheck_.checkCode == CHECK_CODE) {
390         SEN_HILOGW("sensorInfos_ error, sensorInfoCount_:%{public}d, sensorListCount:%{public}d", sensorInfoCount_,
391             static_cast<int32_t>(count));
392         ClearSensorInfos();
393     } else if (sensorInfoCheck_.checkCode != CHECK_CODE) {
394         SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode);
395         ClearSensorInfos();
396     }
397     sensorInfoCheck_.sensorInfos = (SensorInfo *)malloc(sizeof(SensorInfo) * count);
398     CHKPR(sensorInfoCheck_.sensorInfos, ERROR);
399     SEN_HILOGI("Sensor count is %{public}zu", count);
400     for (size_t i = 0; i < count; ++i) {
401         SensorInfo *sensorInfo = sensorInfoCheck_.sensorInfos + i;
402         errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN,
403             sensorList[i].GetSensorName().c_str());
404         CHKCR(ret == EOK, ERROR);
405         ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN,
406             sensorList[i].GetVendorName().c_str());
407         CHKCR(ret == EOK, ERROR);
408         ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN,
409             sensorList[i].GetHardwareVersion().c_str());
410         CHKCR(ret == EOK, ERROR);
411         ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN,
412             sensorList[i].GetFirmwareVersion().c_str());
413         CHKCR(ret == EOK, ERROR);
414         sensorInfo->sensorId = sensorList[i].GetSensorId();
415         sensorInfo->sensorTypeId = sensorList[i].GetSensorTypeId();
416         sensorInfo->maxRange = sensorList[i].GetMaxRange();
417         sensorInfo->precision = sensorList[i].GetResolution();
418         sensorInfo->power = sensorList[i].GetPower();
419         sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs();
420         sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs();
421         SEN_HILOGI("Sensor %{public}zu: sensorId is %{public}d, sensorTypeId is %{public}d",
422             i, sensorInfo->sensorId, sensorInfo->sensorTypeId);
423     }
424     sensorInfoCount_ = static_cast<int32_t>(count);
425     return SUCCESS;
426 }
427 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count) const428 int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const
429 {
430     CALL_LOG_ENTER;
431     CHKPR(sensorInfo, OHOS::Sensors::ERROR);
432     CHKPR(count, OHOS::Sensors::ERROR);
433     std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
434     int32_t ret = ConvertSensorInfos();
435     if (ret != SUCCESS) {
436         SEN_HILOGE("Convert sensor lists failed");
437         ClearSensorInfos();
438         return ERROR;
439     }
440     if (sensorInfoCheck_.checkCode != CHECK_CODE) {
441         SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode);
442         ClearSensorInfos();
443         return ERROR;
444     }
445     CHKPR(sensorInfoCheck_.sensorInfos, OHOS::Sensors::ERROR);
446     *sensorInfo = sensorInfoCheck_.sensorInfos;
447     *count = sensorInfoCount_;
448     PrintSensorData::GetInstance().PrintSensorInfo(sensorInfoCheck_.sensorInfos, sensorInfoCount_);
449     return SUCCESS;
450 }
451 
SuspendSensors(int32_t pid)452 int32_t SensorAgentProxy::SuspendSensors(int32_t pid)
453 {
454     CALL_LOG_ENTER;
455     if (pid < 0) {
456         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
457         return PARAMETER_ERROR;
458     }
459     int32_t ret = 0;
460     {
461         SensorXcollie SensorXcollie("SensorAgentProxy:SuspendSensors", XCOLLIE_TIMEOUT_5S);
462         ret = SEN_CLIENT.SuspendSensors(pid);
463     }
464     if (ret != ERR_OK) {
465         SEN_HILOGD("Suspend sensors failed, ret:%{public}d", ret);
466     }
467     return ret;
468 }
469 
ResumeSensors(int32_t pid)470 int32_t SensorAgentProxy::ResumeSensors(int32_t pid)
471 {
472     CALL_LOG_ENTER;
473     if (pid < 0) {
474         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
475         return PARAMETER_ERROR;
476     }
477     int32_t ret = 0;
478     {
479         SensorXcollie SensorXcollie("SensorAgentProxy:ResumeSensors", XCOLLIE_TIMEOUT_5S);
480         ret = SEN_CLIENT.ResumeSensors(pid);
481     }
482     if (ret != ERR_OK) {
483         SEN_HILOGD("Resume sensors failed, ret:%{public}d", ret);
484     }
485     return ret;
486 }
487 
GetSensorActiveInfos(int32_t pid,SensorActiveInfo ** sensorActiveInfos,int32_t * count) const488 int32_t SensorAgentProxy::GetSensorActiveInfos(int32_t pid,
489     SensorActiveInfo **sensorActiveInfos, int32_t *count) const
490 {
491     CALL_LOG_ENTER;
492     if (pid < 0) {
493         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
494         return PARAMETER_ERROR;
495     }
496     CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR);
497     CHKPR(count, OHOS::Sensors::ERROR);
498     std::lock_guard<std::mutex> sensorActiveInfoLock(sensorActiveInfoMutex_);
499     if (sensorActiveInfos_ != nullptr) {
500         free(sensorActiveInfos_);
501         sensorActiveInfos_ = nullptr;
502     }
503     std::vector<ActiveInfo> activeInfoList;
504     int32_t ret = 0;
505     {
506         SensorXcollie SensorXcollie("SensorAgentProxy:GetActiveInfoList", XCOLLIE_TIMEOUT_5S);
507         ret = SEN_CLIENT.GetActiveInfoList(pid, activeInfoList);
508     }
509     if (ret != ERR_OK) {
510         SEN_HILOGE("Get active info list failed, ret:%{public}d", ret);
511         return ret;
512     }
513     if (activeInfoList.empty()) {
514         SEN_HILOGD("Active info list is empty");
515         *sensorActiveInfos = nullptr;
516         *count = 0;
517         return ERR_OK;
518     }
519     size_t activeInfoCount = activeInfoList.size();
520     if (activeInfoCount > MAX_SENSOR_LIST_SIZE) {
521         SEN_HILOGE("The number of active info exceeds the maximum value, count:%{public}zu", activeInfoCount);
522         return ERROR;
523     }
524     sensorActiveInfos_ = (SensorActiveInfo *)malloc(sizeof(SensorActiveInfo) * activeInfoCount);
525     CHKPR(sensorActiveInfos_, ERROR);
526     for (size_t i = 0; i < activeInfoCount; ++i) {
527         SensorActiveInfo *curActiveInfo = sensorActiveInfos_ + i;
528         curActiveInfo->pid = activeInfoList[i].GetPid();
529         curActiveInfo->sensorId = activeInfoList[i].GetSensorId();
530         curActiveInfo->samplingPeriodNs = activeInfoList[i].GetSamplingPeriodNs();
531         curActiveInfo->maxReportDelayNs = activeInfoList[i].GetMaxReportDelayNs();
532     }
533     *sensorActiveInfos = sensorActiveInfos_;
534     *count = static_cast<int32_t>(activeInfoCount);
535     return ERR_OK;
536 }
537 
Register(SensorActiveInfoCB callback)538 int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback)
539 {
540     CHKPR(callback, OHOS::Sensors::ERROR);
541     CHKPR(dataChannel_, INVALID_POINTER);
542     int32_t ret = 0;
543     {
544         SensorXcollie SensorXcollie("SensorAgentProxy:Register", XCOLLIE_TIMEOUT_5S);
545         ret = SEN_CLIENT.Register(callback, dataChannel_);
546     }
547     if (ret != ERR_OK) {
548         SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
549     }
550     return ret;
551 }
552 
Unregister(SensorActiveInfoCB callback)553 int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback)
554 {
555     CHKPR(callback, OHOS::Sensors::ERROR);
556     int32_t ret = 0;
557     {
558         SensorXcollie SensorXcollie("SensorAgentProxy:Unregister", XCOLLIE_TIMEOUT_5S);
559         ret = SEN_CLIENT.Unregister(callback);
560     }
561     if (ret != ERR_OK) {
562         SEN_HILOGE("Unregister sensor active info callback failed, ret:%{public}d", ret);
563     }
564     return ret;
565 }
566 
ResetSensors() const567 int32_t SensorAgentProxy::ResetSensors() const
568 {
569     int32_t ret = 0;
570     {
571         SensorXcollie SensorXcollie("SensorAgentProxy:ResetSensors", XCOLLIE_TIMEOUT_5S);
572         ret = SEN_CLIENT.ResetSensors();
573     }
574     if (ret != ERR_OK) {
575         SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret);
576     }
577     return ret;
578 }
579 } // namespace Sensors
580 } // namespace OHOS