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_service.h"
17
18 #include <cinttypes>
19 #include <string_ex.h>
20 #include <sys/socket.h>
21 #include <unistd.h>
22
23 #include "hisysevent.h"
24 #include "iservice_registry.h"
25 #include "permission_util.h"
26 #include "securec.h"
27 #include "sensor.h"
28 #include "sensor_dump.h"
29 #include "sensor_errors.h"
30 #include "system_ability_definition.h"
31
32 #undef LOG_TAG
33 #define LOG_TAG "SensorService"
34
35 namespace OHOS {
36 namespace Sensors {
37 using namespace OHOS::HiviewDFX;
38 namespace {
39 auto g_sensorService = SensorDelayedSpSingleton<SensorService>::GetInstance();
40 const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(g_sensorService.GetRefPtr());
41 constexpr int32_t INVALID_PID = -1;
42 constexpr int64_t MAX_EVENT_COUNT = 1000;
43 std::atomic_bool g_isRegister = false;
44 } // namespace
45
SensorService()46 SensorService::SensorService()
47 : SystemAbility(SENSOR_SERVICE_ABILITY_ID, true), state_(SensorServiceState::STATE_STOPPED)
48 {
49 SEN_HILOGD("Add SystemAbility");
50 }
51
~SensorService()52 SensorService::~SensorService() {}
53
OnDump()54 void SensorService::OnDump()
55 {
56 SEN_HILOGI("OnDump");
57 }
58
OnStart()59 void SensorService::OnStart()
60 {
61 CALL_LOG_ENTER;
62 if (state_ == SensorServiceState::STATE_RUNNING) {
63 SEN_HILOGW("SensorService has already started");
64 return;
65 }
66 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
67 if (!InitInterface()) {
68 SEN_HILOGE("Init interface error");
69 }
70 if (!InitDataCallback()) {
71 SEN_HILOGE("Init data callback error");
72 }
73 if (!InitSensorList()) {
74 SEN_HILOGE("Init sensor list error");
75 }
76 sensorDataProcesser_ = new (std::nothrow) SensorDataProcesser(sensorMap_);
77 CHKPV(sensorDataProcesser_);
78 #endif // HDF_DRIVERS_INTERFACE_SENSOR
79 if (!InitSensorPolicy()) {
80 SEN_HILOGE("Init sensor policy error");
81 }
82 if (!SystemAbility::Publish(SensorDelayedSpSingleton<SensorService>::GetInstance())) {
83 SEN_HILOGE("Publish SensorService error");
84 return;
85 }
86 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
87 sensorManager_.InitSensorMap(sensorMap_, sensorDataProcesser_, reportDataCallback_);
88 #else
89 sensorManager_.InitSensorMap(sensorMap_);
90 #endif // HDF_DRIVERS_INTERFACE_SENSOR
91 state_ = SensorServiceState::STATE_RUNNING;
92 }
93
94 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
InitInterface()95 bool SensorService::InitInterface()
96 {
97 auto ret = sensorHdiConnection_.ConnectHdi();
98 if (ret != ERR_OK) {
99 SEN_HILOGE("Connect hdi failed");
100 return false;
101 }
102 return true;
103 }
104
InitDataCallback()105 bool SensorService::InitDataCallback()
106 {
107 reportDataCallback_ = new (std::nothrow) ReportDataCallback();
108 CHKPF(reportDataCallback_);
109 ReportDataCb cb = &ReportDataCallback::ReportEventCallback;
110 auto ret = sensorHdiConnection_.RegisterDataReport(cb, reportDataCallback_);
111 if (ret != ERR_OK) {
112 SEN_HILOGE("RegisterDataReport failed");
113 return false;
114 }
115 return true;
116 }
117
InitSensorList()118 bool SensorService::InitSensorList()
119 {
120 std::lock_guard<std::mutex> sensorLock(sensorsMutex_);
121 int32_t ret = sensorHdiConnection_.GetSensorList(sensors_);
122 if (ret != 0) {
123 SEN_HILOGE("GetSensorList is failed");
124 return false;
125 }
126 {
127 std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_);
128 for (const auto &it : sensors_) {
129 if (!(sensorMap_.insert(std::make_pair(it.GetSensorId(), it)).second)) {
130 SEN_HILOGW("sensorMap_ insert failed");
131 }
132 }
133 }
134 return true;
135 }
136 #endif // HDF_DRIVERS_INTERFACE_SENSOR
137
InitSensorPolicy()138 bool SensorService::InitSensorPolicy()
139 {
140 return true;
141 }
142
OnStop()143 void SensorService::OnStop()
144 {
145 CALL_LOG_ENTER;
146 if (state_ == SensorServiceState::STATE_STOPPED) {
147 SEN_HILOGW("Already stopped");
148 return;
149 }
150 state_ = SensorServiceState::STATE_STOPPED;
151 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
152 int32_t ret = sensorHdiConnection_.DestroyHdiConnection();
153 if (ret != ERR_OK) {
154 SEN_HILOGE("Destroy hdi connect fail");
155 }
156 #endif // HDF_DRIVERS_INTERFACE_SENSOR
157 UnregisterPermCallback();
158 }
159
ReportSensorSysEvent(int32_t sensorId,bool enable,int32_t pid)160 void SensorService::ReportSensorSysEvent(int32_t sensorId, bool enable, int32_t pid)
161 {
162 std::string packageName("");
163 AccessTokenID tokenId = clientInfo_.GetTokenIdByPid(pid);
164 sensorManager_.GetPackageName(tokenId, packageName);
165 const int logLevel = 4;
166 int32_t uid = clientInfo_.GetUidByPid(pid);
167 if (enable) {
168 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "ENABLE_SENSOR", HiSysEvent::EventType::STATISTIC,
169 "LEVEL", logLevel, "UID", uid, "PKG_NAME", packageName, "TYPE", sensorId);
170 } else {
171 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "DISABLE_SENSOR", HiSysEvent::EventType::STATISTIC,
172 "LEVEL", logLevel, "UID", uid, "PKG_NAME", packageName, "TYPE", sensorId);
173 }
174 }
175
ReportOnChangeData(int32_t sensorId)176 void SensorService::ReportOnChangeData(int32_t sensorId)
177 {
178 std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_);
179 auto it = sensorMap_.find(sensorId);
180 if (it == sensorMap_.end()) {
181 SEN_HILOGE("sensorId is invalid");
182 return;
183 }
184 if ((SENSOR_ON_CHANGE & it->second.GetFlags()) != SENSOR_ON_CHANGE) {
185 SEN_HILOGW("The data has not changed , no need to report");
186 return;
187 }
188 SensorData sensorData;
189 auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData);
190 if (ret != ERR_OK) {
191 SEN_HILOGE("There is no data to be reported");
192 return;
193 }
194 sptr<SensorBasicDataChannel> channel = clientInfo_.GetSensorChannelByPid(GetCallingPid());
195 CHKPV(channel);
196 auto sendRet = channel->SendData(&sensorData, sizeof(sensorData));
197 if (sendRet != ERR_OK) {
198 SEN_HILOGE("Send data failed");
199 return;
200 }
201 }
202
SaveSubscriber(int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)203 ErrCode SensorService::SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
204 {
205 if (!sensorManager_.SaveSubscriber(sensorId, GetCallingPid(), samplingPeriodNs, maxReportDelayNs)) {
206 SEN_HILOGE("SaveSubscriber failed");
207 return UPDATE_SENSOR_INFO_ERR;
208 }
209 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
210 sensorManager_.StartDataReportThread();
211 SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, GetCallingPid());
212 if (!sensorManager_.SetBestSensorParams(sensorId,
213 sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs())) {
214 SEN_HILOGE("SetBestSensorParams failed");
215 clientInfo_.RemoveSubscriber(sensorId, GetCallingPid());
216 return SET_SENSOR_CONFIG_ERR;
217 }
218 #endif // HDF_DRIVERS_INTERFACE_SENSOR
219 return ERR_OK;
220 }
221
CheckSensorId(int32_t sensorId)222 bool SensorService::CheckSensorId(int32_t sensorId)
223 {
224 std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_);
225 auto it = sensorMap_.find(sensorId);
226 if (it == sensorMap_.end()) {
227 SEN_HILOGE("Invalid sensorId, sensorId:%{public}d", sensorId);
228 return false;
229 }
230 return true;
231 }
232
EnableSensor(int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)233 ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
234 {
235 CALL_LOG_ENTER;
236 if ((!CheckSensorId(sensorId)) ||
237 ((samplingPeriodNs != 0L) && ((maxReportDelayNs / samplingPeriodNs) > MAX_EVENT_COUNT))) {
238 SEN_HILOGE("sensorId is invalid or maxReportDelayNs exceeded the maximum value");
239 return ERR_NO_INIT;
240 }
241 int32_t pid = GetCallingPid();
242 std::lock_guard<std::mutex> serviceLock(serviceLock_);
243 if (clientInfo_.GetSensorState(sensorId)) {
244 SEN_HILOGW("Sensor has been enabled already");
245 auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs);
246 if (ret != ERR_OK) {
247 SEN_HILOGE("SaveSubscriber failed");
248 return ret;
249 }
250 ReportSensorSysEvent(sensorId, true, pid);
251 if (ret != ERR_OK) {
252 SEN_HILOGE("ret:%{public}d", ret);
253 }
254 ReportOnChangeData(sensorId);
255 if (isReportActiveInfo_) {
256 ReportActiveInfo(sensorId, pid);
257 }
258 return ERR_OK;
259 }
260 auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs);
261 if (ret != ERR_OK) {
262 SEN_HILOGE("SaveSubscriber failed");
263 clientInfo_.RemoveSubscriber(sensorId, GetCallingPid());
264 return ret;
265 }
266 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
267 ret = sensorHdiConnection_.EnableSensor(sensorId);
268 if (ret != ERR_OK) {
269 SEN_HILOGE("EnableSensor failed");
270 clientInfo_.RemoveSubscriber(sensorId, GetCallingPid());
271 return ENABLE_SENSOR_ERR;
272 }
273 #endif // HDF_DRIVERS_INTERFACE_SENSOR
274 if ((!g_isRegister) && (RegisterPermCallback(sensorId))) {
275 g_isRegister = true;
276 }
277 ReportSensorSysEvent(sensorId, true, pid);
278 if (isReportActiveInfo_) {
279 ReportActiveInfo(sensorId, pid);
280 }
281 return ret;
282 }
283
DisableSensor(int32_t sensorId,int32_t pid)284 ErrCode SensorService::DisableSensor(int32_t sensorId, int32_t pid)
285 {
286 CALL_LOG_ENTER;
287 if (!CheckSensorId(sensorId)) {
288 SEN_HILOGE("sensorId is invalid");
289 return ERR_NO_INIT;
290 }
291 if (pid < 0) {
292 SEN_HILOGE("pid is invalid, pid:%{public}d", pid);
293 return CLIENT_PID_INVALID_ERR;
294 }
295 ReportSensorSysEvent(sensorId, false, pid);
296 std::lock_guard<std::mutex> serviceLock(serviceLock_);
297 if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) {
298 SEN_HILOGW("Other client is using this sensor now, can't disable");
299 return ERR_OK;
300 }
301 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
302 if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) {
303 SEN_HILOGE("DisableSensor is failed");
304 return DISABLE_SENSOR_ERR;
305 }
306 #endif // HDF_DRIVERS_INTERFACE_SENSOR
307 int32_t uid = clientInfo_.GetUidByPid(pid);
308 clientInfo_.DestroyCmd(uid);
309 clientInfo_.ClearDataQueue(sensorId);
310 return sensorManager_.AfterDisableSensor(sensorId);
311 }
312
DisableSensor(int32_t sensorId)313 ErrCode SensorService::DisableSensor(int32_t sensorId)
314 {
315 CALL_LOG_ENTER;
316 return DisableSensor(sensorId, GetCallingPid());
317 }
318
GetSensorList()319 std::vector<Sensor> SensorService::GetSensorList()
320 {
321 std::lock_guard<std::mutex> sensorLock(sensorsMutex_);
322 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
323 int32_t ret = sensorHdiConnection_.GetSensorList(sensors_);
324 if (ret != 0) {
325 SEN_HILOGE("GetSensorList is failed");
326 return sensors_;
327 }
328 #endif // HDF_DRIVERS_INTERFACE_SENSOR
329 for (const auto &it : sensors_) {
330 std::lock_guard<std::mutex> sensorMapLock(sensorMapMutex_);
331 sensorMap_.insert(std::make_pair(it.GetSensorId(), it));
332 }
333 return sensors_;
334 }
335
TransferDataChannel(const sptr<SensorBasicDataChannel> & sensorBasicDataChannel,const sptr<IRemoteObject> & sensorClient)336 ErrCode SensorService::TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel,
337 const sptr<IRemoteObject> &sensorClient)
338 {
339 CHKPR(sensorBasicDataChannel, ERR_NO_INIT);
340 auto pid = GetCallingPid();
341 auto uid = GetCallingUid();
342 auto callerToken = GetCallingTokenID();
343 if (!clientInfo_.UpdateAppThreadInfo(pid, uid, callerToken)) {
344 SEN_HILOGE("UpdateUid is failed");
345 return UPDATE_UID_ERR;
346 }
347 if (!clientInfo_.UpdateSensorChannel(pid, sensorBasicDataChannel)) {
348 SEN_HILOGE("UpdateSensorChannel is failed");
349 return UPDATE_SENSOR_CHANNEL_ERR;
350 }
351 sensorBasicDataChannel->SetSensorStatus(true);
352 RegisterClientDeathRecipient(sensorClient, pid);
353 return ERR_OK;
354 }
355
DestroySensorChannel(sptr<IRemoteObject> sensorClient)356 ErrCode SensorService::DestroySensorChannel(sptr<IRemoteObject> sensorClient)
357 {
358 CALL_LOG_ENTER;
359 const int32_t clientPid = GetCallingPid();
360 if (clientPid < 0) {
361 SEN_HILOGE("clientPid is invalid, clientPid:%{public}d", clientPid);
362 return CLIENT_PID_INVALID_ERR;
363 }
364 std::lock_guard<std::mutex> serviceLock(serviceLock_);
365 bool destroyRet = clientInfo_.DestroySensorChannel(clientPid);
366 if (!destroyRet) {
367 SEN_HILOGE("DestroySensorChannel is failed");
368 return DESTROY_SENSOR_CHANNEL_ERR;
369 }
370 clientInfo_.DestroyCmd(GetCallingUid());
371 UnregisterClientDeathRecipient(sensorClient);
372 return ERR_OK;
373 }
374
ProcessDeathObserver(const wptr<IRemoteObject> & object)375 void SensorService::ProcessDeathObserver(const wptr<IRemoteObject> &object)
376 {
377 CALL_LOG_ENTER;
378 sptr<IRemoteObject> client = object.promote();
379 CHKPV(client);
380 int32_t pid = clientInfo_.FindClientPid(client);
381 if (pid == INVALID_PID) {
382 SEN_HILOGE("pid is invalid");
383 return;
384 }
385 SEN_HILOGI("pid is %{public}d", pid);
386 std::vector<int32_t> activeSensors = clientInfo_.GetSensorIdByPid(pid);
387 for (size_t i = 0; i < activeSensors.size(); ++i) {
388 int32_t ret = DisableSensor(activeSensors[i], pid);
389 if (ret != ERR_OK) {
390 SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
391 }
392 }
393 DelSession(pid);
394 clientInfo_.DelActiveInfoCBPid(pid);
395 clientInfo_.DestroySensorChannel(pid);
396 clientInfo_.DestroyClientPid(client);
397 clientInfo_.DestroyCmd(clientInfo_.GetUidByPid(pid));
398 }
399
RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient,int32_t pid)400 void SensorService::RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient, int32_t pid)
401 {
402 CALL_LOG_ENTER;
403 CHKPV(sensorClient);
404 if (clientDeathObserver_ == nullptr) {
405 clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorService *>(this));
406 CHKPV(clientDeathObserver_);
407 }
408 sensorClient->AddDeathRecipient(clientDeathObserver_);
409 clientInfo_.SaveClientPid(sensorClient, pid);
410 }
411
UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient)412 void SensorService::UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient)
413 {
414 CALL_LOG_ENTER;
415 CHKPV(sensorClient);
416 int32_t pid = clientInfo_.FindClientPid(sensorClient);
417 if (pid == INVALID_PID) {
418 SEN_HILOGE("Pid is invalid");
419 return;
420 }
421 if (!clientInfo_.CallingService(pid)) {
422 SEN_HILOGD("Can't unregister client death recipient");
423 return;
424 }
425 sensorClient->RemoveDeathRecipient(clientDeathObserver_);
426 clientInfo_.DestroyClientPid(sensorClient);
427 }
428
Dump(int32_t fd,const std::vector<std::u16string> & args)429 int32_t SensorService::Dump(int32_t fd, const std::vector<std::u16string> &args)
430 {
431 CALL_LOG_ENTER;
432 if (fd < 0) {
433 SEN_HILOGE("Invalid fd");
434 return DUMP_PARAM_ERR;
435 }
436 SensorDump &sensorDump = SensorDump::GetInstance();
437 if (args.empty()) {
438 SEN_HILOGE("Param cannot be empty");
439 dprintf(fd, "param cannot be empty\n");
440 sensorDump.DumpHelp(fd);
441 return DUMP_PARAM_ERR;
442 }
443 std::vector<std::string> argList = { "" };
444 std::transform(args.begin(), args.end(), std::back_inserter(argList),
445 [](const std::u16string &arg) {
446 return Str16ToStr8(arg);
447 });
448 sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_);
449 return ERR_OK;
450 }
451
SuspendSensors(int32_t pid)452 ErrCode SensorService::SuspendSensors(int32_t pid)
453 {
454 CALL_LOG_ENTER;
455 if (pid < 0) {
456 SEN_HILOGE("Pid is invalid");
457 return CLIENT_PID_INVALID_ERR;
458 }
459 return POWER_POLICY.SuspendSensors(pid);
460 }
461
ResumeSensors(int32_t pid)462 ErrCode SensorService::ResumeSensors(int32_t pid)
463 {
464 CALL_LOG_ENTER;
465 if (pid < 0) {
466 SEN_HILOGE("Pid is invalid");
467 return CLIENT_PID_INVALID_ERR;
468 }
469 return POWER_POLICY.ResumeSensors(pid);
470 }
471
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)472 ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
473 {
474 CALL_LOG_ENTER;
475 if (pid < 0) {
476 SEN_HILOGE("Pid is invalid");
477 return CLIENT_PID_INVALID_ERR;
478 }
479 activeInfoList = POWER_POLICY.GetActiveInfoList(pid);
480 return ERR_OK;
481 }
482
CreateSocketChannel(sptr<IRemoteObject> sensorClient,int32_t & clientFd)483 ErrCode SensorService::CreateSocketChannel(sptr<IRemoteObject> sensorClient, int32_t &clientFd)
484 {
485 CALL_LOG_ENTER;
486 CHKPR(sensorClient, INVALID_POINTER);
487 int32_t serverFd = -1;
488 int32_t ret = AddSocketPairInfo(GetCallingUid(), GetCallingPid(),
489 AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()),
490 serverFd, std::ref(clientFd));
491 if (ret != ERR_OK) {
492 SEN_HILOGE("Add socket pair info failed, ret:%{public}d", ret);
493 return ret;
494 }
495 RegisterClientDeathRecipient(sensorClient, GetCallingPid());
496 return ERR_OK;
497 }
498
DestroySocketChannel(sptr<IRemoteObject> sensorClient)499 ErrCode SensorService::DestroySocketChannel(sptr<IRemoteObject> sensorClient)
500 {
501 CALL_LOG_ENTER;
502 CHKPR(sensorClient, INVALID_POINTER);
503 DelSession(GetCallingPid());
504 UnregisterClientDeathRecipient(sensorClient);
505 return ERR_OK;
506 }
507
EnableActiveInfoCB()508 ErrCode SensorService::EnableActiveInfoCB()
509 {
510 CALL_LOG_ENTER;
511 isReportActiveInfo_ = true;
512 return clientInfo_.AddActiveInfoCBPid(GetCallingPid());
513 }
514
DisableActiveInfoCB()515 ErrCode SensorService::DisableActiveInfoCB()
516 {
517 CALL_LOG_ENTER;
518 isReportActiveInfo_ = false;
519 return clientInfo_.DelActiveInfoCBPid(GetCallingPid());
520 }
521
ResetSensors()522 ErrCode SensorService::ResetSensors()
523 {
524 CALL_LOG_ENTER;
525 return POWER_POLICY.ResetSensors();
526 }
527
ReportActiveInfo(int32_t sensorId,int32_t pid)528 void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid)
529 {
530 CALL_LOG_ENTER;
531 std::vector<SessionPtr> sessionList;
532 auto pidList = clientInfo_.GetActiveInfoCBPid();
533 for (const auto &pid : pidList) {
534 auto sess = GetSessionByPid(pid);
535 if (sess != nullptr) {
536 sessionList.push_back(sess);
537 }
538 }
539 SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid);
540 ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(),
541 sensorInfo.GetMaxReportDelayNs());
542 POWER_POLICY.ReportActiveInfo(activeInfo, sessionList);
543 }
544
RegisterPermCallback(int32_t sensorId)545 bool SensorService::RegisterPermCallback(int32_t sensorId)
546 {
547 CALL_LOG_ENTER;
548 if ((sensorId != SENSOR_TYPE_ID_PEDOMETER) && (sensorId != SENSOR_TYPE_ID_PEDOMETER_DETECTION) &&
549 (sensorId != SENSOR_TYPE_ID_HEART_RATE)) {
550 SEN_HILOGD("No need listen for the sensor permission changes");
551 return false;
552 }
553 Security::AccessToken::PermStateChangeScope scope = {
554 .permList = { ACTIVITY_MOTION_PERMISSION, READ_HEALTH_DATA_PERMISSION }
555 };
556 permStateChangeCb_ = std::make_shared<PermStateChangeCb>(scope, this);
557 int32_t ret = Security::AccessToken::AccessTokenKit::RegisterPermStateChangeCallback(permStateChangeCb_);
558 if (ret != ERR_OK) {
559 SEN_HILOGE("RegisterPermStateChangeCallback fail");
560 return false;
561 }
562 return true;
563 }
564
UnregisterPermCallback()565 void SensorService::UnregisterPermCallback()
566 {
567 CALL_LOG_ENTER;
568 CHKPV(permStateChangeCb_);
569 int32_t ret = Security::AccessToken::AccessTokenKit::UnRegisterPermStateChangeCallback(permStateChangeCb_);
570 if (ret != ERR_OK) {
571 SEN_HILOGE("UnregisterPermStateChangeCallback fail");
572 return;
573 }
574 g_isRegister = false;
575 }
576
PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo & result)577 void SensorService::PermStateChangeCb::PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo &result)
578 {
579 CALL_LOG_ENTER;
580 CHKPV(server_);
581 server_->clientInfo_.ChangeSensorPerm(result.tokenID, result.permissionName,
582 (result.permStateChangeType != 0));
583 }
584 } // namespace Sensors
585 } // namespace OHOS
586