• 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 <cstring>
19 
20 #include "securec.h"
21 #include "sensor_errors.h"
22 #include "sensor_service_client.h"
23 #undef LOG_TAG
24 #define LOG_TAG "SensorAgentProxy"
25 using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace Sensors {
28 namespace {
29 constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff;
30 std::mutex sensorInfoMutex_;
31 SensorInfo *sensorInfos_ = nullptr;
32 std::mutex sensorActiveInfoMutex_;
33 SensorActiveInfo *sensorActiveInfos_ = nullptr;
34 int32_t sensorInfoCount_ = 0;
35 }  // namespace
36 
37 #define SEN_CLIENT SensorServiceClient::GetInstance()
38 std::recursive_mutex SensorAgentProxy::subscribeMutex_;
39 std::mutex SensorAgentProxy::chanelMutex_;
40 
SensorAgentProxy()41 SensorAgentProxy::SensorAgentProxy()
42     : dataChannel_(new (std::nothrow) SensorDataChannel())
43 {}
44 
~SensorAgentProxy()45 SensorAgentProxy::~SensorAgentProxy()
46 {
47     CALL_LOG_ENTER;
48     ClearSensorInfos();
49 }
50 
HandleSensorData(SensorEvent * events,int32_t num,void * data)51 void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void *data)
52 {
53     CHKPV(events);
54     if (num <= 0) {
55         SEN_HILOGE("events is null or num is invalid");
56         return;
57     }
58     SensorEvent eventStream;
59     for (int32_t i = 0; i < num; ++i) {
60         eventStream = events[i];
61         RecordSensorCallback fun;
62         {
63             std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
64             auto iter = subscribeMap_.find(eventStream.sensorTypeId);
65             if (iter == subscribeMap_.end()) {
66                 SEN_HILOGE("Sensor is not subscribed");
67                 return;
68             }
69             const SensorUser *user = iter->second;
70             CHKPV(user);
71             fun = user->callback;
72         }
73         CHKPV(fun);
74         fun(&eventStream);
75     }
76 }
77 
CreateSensorDataChannel()78 int32_t SensorAgentProxy::CreateSensorDataChannel()
79 {
80     CALL_LOG_ENTER;
81     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
82     if (isChannelCreated_) {
83         SEN_HILOGI("The channel has already been created");
84         return ERR_OK;
85     }
86     CHKPR(dataChannel_, INVALID_POINTER);
87     auto ret = dataChannel_->CreateSensorDataChannel(std::bind(&SensorAgentProxy::HandleSensorData,
88         this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), nullptr);
89     if (ret != ERR_OK) {
90         SEN_HILOGE("Create data channel failed, ret:%{public}d", ret);
91         return ret;
92     }
93     ret = SEN_CLIENT.TransferDataChannel(dataChannel_);
94     if (ret != ERR_OK) {
95         auto destroyRet = dataChannel_->DestroySensorDataChannel();
96         SEN_HILOGE("Transfer data channel failed, ret:%{public}d, destroyRet:%{public}d", ret, destroyRet);
97         return ret;
98     }
99     isChannelCreated_ = true;
100     return ERR_OK;
101 }
102 
DestroySensorDataChannel()103 int32_t SensorAgentProxy::DestroySensorDataChannel()
104 {
105     CALL_LOG_ENTER;
106     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
107     if (!isChannelCreated_) {
108         SEN_HILOGI("Channel has been destroyed");
109         return ERR_OK;
110     }
111     CHKPR(dataChannel_, INVALID_POINTER);
112     int32_t ret = dataChannel_->DestroySensorDataChannel();
113     if (ret != ERR_OK) {
114         SEN_HILOGE("Destroy data channel failed, ret:%{public}d", ret);
115         return ret;
116     }
117     ret = SEN_CLIENT.DestroyDataChannel();
118     if (ret != ERR_OK) {
119         SEN_HILOGE("Destroy service data channel fail, ret:%{public}d", ret);
120         return ret;
121     }
122     isChannelCreated_ = false;
123     return ERR_OK;
124 }
125 
ActivateSensor(int32_t sensorId,const SensorUser * user)126 int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user)
127 {
128     CHKPR(user, OHOS::Sensors::ERROR);
129     CHKPR(user->callback, OHOS::Sensors::ERROR);
130     if (samplingInterval_ < 0 || reportInterval_ < 0) {
131         SEN_HILOGE("SamplingPeriod or reportInterval_ is invalid");
132         return ERROR;
133     }
134     if (!SEN_CLIENT.IsValid(sensorId)) {
135         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
136         return PARAMETER_ERROR;
137     }
138     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
139     if ((subscribeMap_.find(sensorId) == subscribeMap_.end()) || (subscribeMap_[sensorId] != user)) {
140         SEN_HILOGE("Subscribe sensorId first");
141         return ERROR;
142     }
143     int32_t ret = SEN_CLIENT.EnableSensor(sensorId, samplingInterval_, reportInterval_);
144     samplingInterval_ = -1;
145     reportInterval_ = -1;
146     if (ret != 0) {
147         SEN_HILOGE("Enable sensor failed, ret:%{public}d", ret);
148         subscribeMap_.erase(sensorId);
149         return ret;
150     }
151     return ret;
152 }
153 
DeactivateSensor(int32_t sensorId,const SensorUser * user)154 int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user)
155 {
156     CHKPR(user, OHOS::Sensors::ERROR);
157     CHKPR(user->callback, OHOS::Sensors::ERROR);
158     if (!SEN_CLIENT.IsValid(sensorId)) {
159         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
160         return PARAMETER_ERROR;
161     }
162     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
163     if ((subscribeMap_.find(sensorId) == subscribeMap_.end()) || (subscribeMap_[sensorId] != user)) {
164         SEN_HILOGE("Subscribe sensorId first");
165         return OHOS::Sensors::ERROR;
166     }
167     subscribeMap_.erase(sensorId);
168     unsubscribeMap_[sensorId] = user;
169     int32_t ret = SEN_CLIENT.DisableSensor(sensorId);
170     if (ret != 0) {
171         SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
172         return ret;
173     }
174     return ret;
175 }
176 
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)177 int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval,
178                                    int64_t reportInterval)
179 {
180     CHKPR(user, OHOS::Sensors::ERROR);
181     if (!SEN_CLIENT.IsValid(sensorId)) {
182         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
183         return PARAMETER_ERROR;
184     }
185     if (samplingInterval < 0 || reportInterval < 0) {
186         SEN_HILOGE("samplingInterval or reportInterval is invalid");
187         return OHOS::Sensors::ERROR;
188     }
189     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
190     if ((subscribeMap_.find(sensorId) == subscribeMap_.end()) || (subscribeMap_.at(sensorId) != user)) {
191         SEN_HILOGE("Subscribe sensorId first");
192         return OHOS::Sensors::ERROR;
193     }
194     samplingInterval_ = samplingInterval;
195     reportInterval_ = reportInterval;
196     return OHOS::Sensors::SUCCESS;
197 }
198 
SubscribeSensor(int32_t sensorId,const SensorUser * user)199 int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *user)
200 {
201     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
202     CHKPR(user, OHOS::Sensors::ERROR);
203     CHKPR(user->callback, OHOS::Sensors::ERROR);
204     if (!SEN_CLIENT.IsValid(sensorId)) {
205         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
206         return PARAMETER_ERROR;
207     }
208     int32_t ret = CreateSensorDataChannel();
209     if (ret != ERR_OK) {
210         SEN_HILOGE("Create sensor data chanel failed");
211         return OHOS::Sensors::ERROR;
212     }
213     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
214     subscribeMap_[sensorId] = user;
215     return OHOS::Sensors::SUCCESS;
216 }
217 
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)218 int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
219 {
220     SEN_HILOGI("In, sensorId:%{public}d", sensorId);
221     CHKPR(user, OHOS::Sensors::ERROR);
222     CHKPR(user->callback, OHOS::Sensors::ERROR);
223     if (!SEN_CLIENT.IsValid(sensorId)) {
224         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
225         return PARAMETER_ERROR;
226     }
227     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
228     if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end() || unsubscribeMap_[sensorId] != user) {
229         SEN_HILOGE("Deactivate sensorId first");
230         return OHOS::Sensors::ERROR;
231     }
232     if (subscribeMap_.empty()) {
233         int32_t ret = DestroySensorDataChannel();
234         if (ret != ERR_OK) {
235             SEN_HILOGE("Destroy data channel fail, ret:%{public}d", ret);
236             return ret;
237         }
238     }
239     unsubscribeMap_.erase(sensorId);
240     return OHOS::Sensors::SUCCESS;
241 }
242 
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)243 int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
244 {
245     CHKPR(user, OHOS::Sensors::ERROR);
246     CHKPR(user->callback, OHOS::Sensors::ERROR);
247     if (!SEN_CLIENT.IsValid(sensorId)) {
248         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
249         return ERROR;
250     }
251     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
252     if ((subscribeMap_.find(sensorId) == subscribeMap_.end()) || (subscribeMap_.at(sensorId) != user)) {
253         SEN_HILOGE("Subscribe sensorId first");
254         return OHOS::Sensors::ERROR;
255     }
256     return OHOS::Sensors::SUCCESS;
257 }
258 
ClearSensorInfos() const259 void SensorAgentProxy::ClearSensorInfos() const
260 {
261     if (sensorActiveInfos_ != nullptr) {
262         free(sensorActiveInfos_);
263         sensorActiveInfos_ = nullptr;
264     }
265     CHKPV(sensorInfos_);
266     free(sensorInfos_);
267     sensorInfos_ = nullptr;
268 }
269 
ConvertSensorInfos() const270 int32_t SensorAgentProxy::ConvertSensorInfos() const
271 {
272     CALL_LOG_ENTER;
273     std::vector<Sensor> sensorList = SEN_CLIENT.GetSensorList();
274     if (sensorList.empty()) {
275         SEN_HILOGE("Get sensor lists failed");
276         return ERROR;
277     }
278     size_t count = sensorList.size();
279     if (count > MAX_SENSOR_LIST_SIZE) {
280         SEN_HILOGE("The number of sensors exceeds the maximum value");
281         return ERROR;
282     }
283     sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count);
284     CHKPR(sensorInfos_, ERROR);
285     for (size_t i = 0; i < count; ++i) {
286         SensorInfo *sensorInfo = sensorInfos_ + i;
287         errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN,
288             sensorList[i].GetSensorName().c_str());
289         CHKCR(ret == EOK, ERROR);
290         ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN,
291             sensorList[i].GetVendorName().c_str());
292         CHKCR(ret == EOK, ERROR);
293         ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN,
294             sensorList[i].GetHardwareVersion().c_str());
295         CHKCR(ret == EOK, ERROR);
296         ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN,
297             sensorList[i].GetFirmwareVersion().c_str());
298         CHKCR(ret == EOK, ERROR);
299         sensorInfo->sensorId = sensorList[i].GetSensorId();
300         sensorInfo->sensorTypeId = sensorList[i].GetSensorTypeId();
301         sensorInfo->maxRange = sensorList[i].GetMaxRange();
302         sensorInfo->precision = sensorList[i].GetResolution();
303         sensorInfo->power = sensorList[i].GetPower();
304         sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs();
305         sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs();
306     }
307     sensorInfoCount_ = static_cast<int32_t>(count);
308     return SUCCESS;
309 }
310 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count) const311 int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const
312 {
313     CALL_LOG_ENTER;
314     CHKPR(sensorInfo, OHOS::Sensors::ERROR);
315     CHKPR(count, OHOS::Sensors::ERROR);
316     std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
317     if (sensorInfos_ == nullptr) {
318         int32_t ret = ConvertSensorInfos();
319         if (ret != SUCCESS) {
320             SEN_HILOGE("Convert sensor lists failed");
321             ClearSensorInfos();
322             return ERROR;
323         }
324     }
325     *sensorInfo = sensorInfos_;
326     *count = sensorInfoCount_;
327     return SUCCESS;
328 }
329 
SuspendSensors(int32_t pid)330 int32_t SensorAgentProxy::SuspendSensors(int32_t pid)
331 {
332     CALL_LOG_ENTER;
333     if (pid < 0) {
334         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
335         return PARAMETER_ERROR;
336     }
337     int32_t ret = SEN_CLIENT.SuspendSensors(pid);
338     if (ret != ERR_OK) {
339         SEN_HILOGD("Suspend sensors failed, ret:%{public}d", ret);
340     }
341     return ret;
342 }
343 
ResumeSensors(int32_t pid)344 int32_t SensorAgentProxy::ResumeSensors(int32_t pid)
345 {
346     CALL_LOG_ENTER;
347     if (pid < 0) {
348         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
349         return PARAMETER_ERROR;
350     }
351     int32_t ret = SEN_CLIENT.ResumeSensors(pid);
352     if (ret != ERR_OK) {
353         SEN_HILOGD("Resume sensors failed, ret:%{public}d", ret);
354     }
355     return ret;
356 }
357 
GetSensorActiveInfos(int32_t pid,SensorActiveInfo ** sensorActiveInfos,int32_t * count) const358 int32_t SensorAgentProxy::GetSensorActiveInfos(int32_t pid,
359     SensorActiveInfo **sensorActiveInfos, int32_t *count) const
360 {
361     CALL_LOG_ENTER;
362     if (pid < 0) {
363         SEN_HILOGE("Pid is invalid, pid:%{public}d", pid);
364         return PARAMETER_ERROR;
365     }
366     CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR);
367     CHKPR(count, OHOS::Sensors::ERROR);
368     std::lock_guard<std::mutex> sensorActiveInfoLock(sensorActiveInfoMutex_);
369     if (sensorActiveInfos_ != nullptr) {
370         free(sensorActiveInfos_);
371         sensorActiveInfos_ = nullptr;
372     }
373     std::vector<ActiveInfo> activeInfoList;
374     int32_t ret = SEN_CLIENT.GetActiveInfoList(pid, activeInfoList);
375     if (ret != ERR_OK) {
376         SEN_HILOGE("Get active info list failed, ret:%{public}d", ret);
377         return ret;
378     }
379     if (activeInfoList.empty()) {
380         SEN_HILOGD("Active info list is empty");
381         *sensorActiveInfos = nullptr;
382         *count = 0;
383         return ERR_OK;
384     }
385     size_t activeInfoCount = activeInfoList.size();
386     if (activeInfoCount > MAX_SENSOR_LIST_SIZE) {
387         SEN_HILOGE("The number of active info exceeds the maximum value, count:%{public}zu", activeInfoCount);
388         return ERROR;
389     }
390     sensorActiveInfos_ = (SensorActiveInfo *)malloc(sizeof(SensorActiveInfo) * activeInfoCount);
391     CHKPR(sensorActiveInfos_, ERROR);
392     for (size_t i = 0; i < activeInfoCount; ++i) {
393         SensorActiveInfo *curActiveInfo = sensorActiveInfos_ + i;
394         curActiveInfo->pid = activeInfoList[i].GetPid();
395         curActiveInfo->sensorId = activeInfoList[i].GetSensorId();
396         curActiveInfo->samplingPeriodNs = activeInfoList[i].GetSamplingPeriodNs();
397         curActiveInfo->maxReportDelayNs = activeInfoList[i].GetMaxReportDelayNs();
398     }
399     *sensorActiveInfos = sensorActiveInfos_;
400     *count = static_cast<int32_t>(activeInfoCount);
401     return ERR_OK;
402 }
403 
Register(SensorActiveInfoCB callback)404 int32_t SensorAgentProxy::Register(SensorActiveInfoCB callback)
405 {
406     CHKPR(callback, OHOS::Sensors::ERROR);
407     CHKPR(dataChannel_, INVALID_POINTER);
408     int32_t ret = SEN_CLIENT.Register(callback, dataChannel_);
409     if (ret != ERR_OK) {
410         SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
411     }
412     return ret;
413 }
414 
Unregister(SensorActiveInfoCB callback)415 int32_t SensorAgentProxy::Unregister(SensorActiveInfoCB callback)
416 {
417     CHKPR(callback, OHOS::Sensors::ERROR);
418     int32_t ret = SEN_CLIENT.Unregister(callback);
419     if (ret != ERR_OK) {
420         SEN_HILOGE("Unregister sensor active info callback failed, ret:%{public}d", ret);
421     }
422     return ret;
423 }
424 
ResetSensors() const425 int32_t SensorAgentProxy::ResetSensors() const
426 {
427     int32_t ret = SEN_CLIENT.ResetSensors();
428     if (ret != ERR_OK) {
429         SEN_HILOGE("Reset sensors failed, ret:%{public}d", ret);
430     }
431     return ret;
432 }
433 }  // namespace Sensors
434 }  // namespace OHOS