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