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 std::lock_guard<std::mutex> clientDeathObserverLock(clientDeathObserverMutex_);
405 if (clientDeathObserver_ == nullptr) {
406 clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorService *>(this));
407 CHKPV(clientDeathObserver_);
408 }
409 sensorClient->AddDeathRecipient(clientDeathObserver_);
410 clientInfo_.SaveClientPid(sensorClient, pid);
411 }
412
UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient)413 void SensorService::UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient)
414 {
415 CALL_LOG_ENTER;
416 CHKPV(sensorClient);
417 int32_t pid = clientInfo_.FindClientPid(sensorClient);
418 if (pid == INVALID_PID) {
419 SEN_HILOGE("Pid is invalid");
420 return;
421 }
422 if (!clientInfo_.CallingService(pid)) {
423 SEN_HILOGD("Can't unregister client death recipient");
424 return;
425 }
426 std::lock_guard<std::mutex> clientDeathObserverLock(clientDeathObserverMutex_);
427 sensorClient->RemoveDeathRecipient(clientDeathObserver_);
428 clientInfo_.DestroyClientPid(sensorClient);
429 }
430
Dump(int32_t fd,const std::vector<std::u16string> & args)431 int32_t SensorService::Dump(int32_t fd, const std::vector<std::u16string> &args)
432 {
433 CALL_LOG_ENTER;
434 if (fd < 0) {
435 SEN_HILOGE("Invalid fd");
436 return DUMP_PARAM_ERR;
437 }
438 SensorDump &sensorDump = SensorDump::GetInstance();
439 if (args.empty()) {
440 SEN_HILOGE("Param cannot be empty");
441 dprintf(fd, "param cannot be empty\n");
442 sensorDump.DumpHelp(fd);
443 return DUMP_PARAM_ERR;
444 }
445 std::vector<std::string> argList = { "" };
446 std::transform(args.begin(), args.end(), std::back_inserter(argList),
447 [](const std::u16string &arg) {
448 return Str16ToStr8(arg);
449 });
450 sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_);
451 return ERR_OK;
452 }
453
SuspendSensors(int32_t pid)454 ErrCode SensorService::SuspendSensors(int32_t pid)
455 {
456 CALL_LOG_ENTER;
457 if (pid < 0) {
458 SEN_HILOGE("Pid is invalid");
459 return CLIENT_PID_INVALID_ERR;
460 }
461 return POWER_POLICY.SuspendSensors(pid);
462 }
463
ResumeSensors(int32_t pid)464 ErrCode SensorService::ResumeSensors(int32_t pid)
465 {
466 CALL_LOG_ENTER;
467 if (pid < 0) {
468 SEN_HILOGE("Pid is invalid");
469 return CLIENT_PID_INVALID_ERR;
470 }
471 return POWER_POLICY.ResumeSensors(pid);
472 }
473
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)474 ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
475 {
476 CALL_LOG_ENTER;
477 if (pid < 0) {
478 SEN_HILOGE("Pid is invalid");
479 return CLIENT_PID_INVALID_ERR;
480 }
481 activeInfoList = POWER_POLICY.GetActiveInfoList(pid);
482 return ERR_OK;
483 }
484
CreateSocketChannel(sptr<IRemoteObject> sensorClient,int32_t & clientFd)485 ErrCode SensorService::CreateSocketChannel(sptr<IRemoteObject> sensorClient, int32_t &clientFd)
486 {
487 CALL_LOG_ENTER;
488 CHKPR(sensorClient, INVALID_POINTER);
489 int32_t serverFd = -1;
490 int32_t ret = AddSocketPairInfo(GetCallingUid(), GetCallingPid(),
491 AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()),
492 serverFd, std::ref(clientFd));
493 if (ret != ERR_OK) {
494 SEN_HILOGE("Add socket pair info failed, ret:%{public}d", ret);
495 return ret;
496 }
497 RegisterClientDeathRecipient(sensorClient, GetCallingPid());
498 return ERR_OK;
499 }
500
DestroySocketChannel(sptr<IRemoteObject> sensorClient)501 ErrCode SensorService::DestroySocketChannel(sptr<IRemoteObject> sensorClient)
502 {
503 CALL_LOG_ENTER;
504 CHKPR(sensorClient, INVALID_POINTER);
505 DelSession(GetCallingPid());
506 UnregisterClientDeathRecipient(sensorClient);
507 return ERR_OK;
508 }
509
EnableActiveInfoCB()510 ErrCode SensorService::EnableActiveInfoCB()
511 {
512 CALL_LOG_ENTER;
513 isReportActiveInfo_ = true;
514 return clientInfo_.AddActiveInfoCBPid(GetCallingPid());
515 }
516
DisableActiveInfoCB()517 ErrCode SensorService::DisableActiveInfoCB()
518 {
519 CALL_LOG_ENTER;
520 isReportActiveInfo_ = false;
521 return clientInfo_.DelActiveInfoCBPid(GetCallingPid());
522 }
523
ResetSensors()524 ErrCode SensorService::ResetSensors()
525 {
526 CALL_LOG_ENTER;
527 return POWER_POLICY.ResetSensors();
528 }
529
ReportActiveInfo(int32_t sensorId,int32_t pid)530 void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid)
531 {
532 CALL_LOG_ENTER;
533 std::vector<SessionPtr> sessionList;
534 auto pidList = clientInfo_.GetActiveInfoCBPid();
535 for (const auto &pid : pidList) {
536 auto sess = GetSessionByPid(pid);
537 if (sess != nullptr) {
538 sessionList.push_back(sess);
539 }
540 }
541 SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid);
542 ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(),
543 sensorInfo.GetMaxReportDelayNs());
544 POWER_POLICY.ReportActiveInfo(activeInfo, sessionList);
545 }
546
RegisterPermCallback(int32_t sensorId)547 bool SensorService::RegisterPermCallback(int32_t sensorId)
548 {
549 CALL_LOG_ENTER;
550 if ((sensorId != SENSOR_TYPE_ID_PEDOMETER) && (sensorId != SENSOR_TYPE_ID_PEDOMETER_DETECTION) &&
551 (sensorId != SENSOR_TYPE_ID_HEART_RATE)) {
552 SEN_HILOGD("No need listen for the sensor permission changes");
553 return false;
554 }
555 Security::AccessToken::PermStateChangeScope scope = {
556 .permList = { ACTIVITY_MOTION_PERMISSION, READ_HEALTH_DATA_PERMISSION }
557 };
558 permStateChangeCb_ = std::make_shared<PermStateChangeCb>(scope, this);
559 int32_t ret = Security::AccessToken::AccessTokenKit::RegisterPermStateChangeCallback(permStateChangeCb_);
560 if (ret != ERR_OK) {
561 SEN_HILOGE("RegisterPermStateChangeCallback fail");
562 return false;
563 }
564 return true;
565 }
566
UnregisterPermCallback()567 void SensorService::UnregisterPermCallback()
568 {
569 CALL_LOG_ENTER;
570 CHKPV(permStateChangeCb_);
571 int32_t ret = Security::AccessToken::AccessTokenKit::UnRegisterPermStateChangeCallback(permStateChangeCb_);
572 if (ret != ERR_OK) {
573 SEN_HILOGE("UnregisterPermStateChangeCallback fail");
574 return;
575 }
576 g_isRegister = false;
577 }
578
PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo & result)579 void SensorService::PermStateChangeCb::PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo &result)
580 {
581 CALL_LOG_ENTER;
582 CHKPV(server_);
583 server_->clientInfo_.ChangeSensorPerm(result.tokenID, result.permissionName,
584 (result.permStateChangeType != 0));
585 }
586 } // namespace Sensors
587 } // namespace OHOS
588