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_client.h"
17
18 #include "death_recipient_template.h"
19 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
20 #include "hisysevent.h"
21 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
22 #ifdef HIVIEWDFX_HITRACE_ENABLE
23 #include "hitrace_meter.h"
24 #endif // HIVIEWDFX_HITRACE_ENABLE
25 #include "sensor_agent_proxy.h"
26 #include "system_ability_definition.h"
27
28 #undef LOG_TAG
29 #define LOG_TAG "SensorServiceClient"
30
31 namespace OHOS {
32 namespace Sensors {
33 using namespace OHOS::HiviewDFX;
34
35 namespace {
36 #ifdef OHOS_BUILD_ENABLE_RUST
37 extern "C" {
38 void ReadClientPackets(RustStreamBuffer *, OHOS::Sensors::SensorServiceClient *,
39 void(*)(OHOS::Sensors::SensorServiceClient *, RustNetPacket *));
OnPacket(SensorServiceClient * object,RustNetPacket * cPkt)40 void OnPacket(SensorServiceClient *object, RustNetPacket *cPkt)
41 {
42 NetPacket pkt(cPkt->msgId);
43 pkt.streamBufferPtr_.reset(cPkt->streamBuffer);
44 object->HandleNetPacke(pkt);
45 }
46 }
47 #endif // OHOS_BUILD_ENABLE_RUST
48 constexpr int32_t LOADSA_TIMEOUT_MS = 10000;
49 } // namespace
50
~SensorServiceClient()51 SensorServiceClient::~SensorServiceClient()
52 {
53 if (sensorServer_ != nullptr && serviceDeathObserver_ != nullptr) {
54 auto remoteObject = sensorServer_->AsObject();
55 if (remoteObject != nullptr) {
56 remoteObject->RemoveDeathRecipient(serviceDeathObserver_);
57 }
58 }
59 DestroyClientRemoteObject();
60 Disconnect();
61 }
62
DealAfterServiceAlive()63 int32_t SensorServiceClient::DealAfterServiceAlive()
64 {
65 CALL_LOG_ENTER;
66 serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorServiceClient *>(this));
67 CHKPR(serviceDeathObserver_, SENSOR_NATIVE_GET_SERVICE_ERR);
68 sptr<IRemoteObject> remoteObject = sensorServer_->AsObject();
69 CHKPR(remoteObject, SENSOR_NATIVE_GET_SERVICE_ERR);
70 remoteObject->AddDeathRecipient(serviceDeathObserver_);
71 sensorList_.clear();
72 int32_t ret = sensorServer_->GetSensorList(sensorList_);
73 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST, ret);
74 if (sensorList_.empty()) {
75 SEN_HILOGW("sensorList_ is empty when connecting to the service for the first time");
76 }
77 ret = TransferClientRemoteObject();
78 if (ret != ERR_OK) {
79 SEN_HILOGE("TransferClientRemoteObject failed, ret:%{public}d", ret);
80 }
81 return ret;
82 }
83
InitServiceClient()84 int32_t SensorServiceClient::InitServiceClient()
85 {
86 CALL_LOG_ENTER;
87 std::lock_guard<std::mutex> clientLock(clientMutex_);
88 if (sensorServer_ != nullptr) {
89 SEN_HILOGD("Already init");
90 if (sensorList_.empty()) {
91 int32_t ret = sensorServer_->GetSensorList(sensorList_);
92 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST, ret);
93 SEN_HILOGW("sensorList is %{public}s", sensorList_.empty() ? "empty" : "not empty");
94 }
95 return ERR_OK;
96 }
97 if (sensorClientStub_ == nullptr) {
98 sensorClientStub_ = new (std::nothrow) SensorClientStub();
99 }
100 sptr<ISystemAbilityManager> systemAbilityManager =
101 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 CHKPR(systemAbilityManager, SENSOR_NATIVE_SAM_ERR);
103 sensorServer_ = iface_cast<ISensorService>(systemAbilityManager->CheckSystemAbility(SENSOR_SERVICE_ABILITY_ID));
104 if (sensorServer_ == nullptr) {
105 if (!LoadSensorService()) {
106 SEN_HILOGE("LoadSensorService failed");
107 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
108 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SERVICE_EXCEPTION",
109 HiSysEvent::EventType::FAULT, "PKG_NAME", "InitServiceClient", "ERROR_CODE",
110 SENSOR_NATIVE_GET_SERVICE_ERR);
111 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
112 return SENSOR_NATIVE_GET_SERVICE_ERR;
113 }
114 return ERR_OK;
115 }
116 SEN_HILOGD("Get service success");
117 int32_t ret = DealAfterServiceAlive();
118 if (ret != ERR_OK) {
119 SEN_HILOGE("DealAfterServiceAlive failed, ret:%{public}d", ret);
120 }
121 return ret;
122 }
123
LoadSensorService()124 bool SensorServiceClient::LoadSensorService()
125 {
126 SEN_HILOGI("LoadSensorService in");
127 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
128 if (samgr == nullptr) {
129 SEN_HILOGE("samgr is nullptr");
130 return false;
131 }
132 auto sensorSa = samgr->LoadSystemAbility(SENSOR_SERVICE_ABILITY_ID, LOADSA_TIMEOUT_MS);
133 if (sensorSa == nullptr) {
134 SEN_HILOGE("Load sensor sa failed");
135 return false;
136 }
137 sensorServer_ = iface_cast<ISensorService>(sensorSa);
138 if (sensorServer_ == nullptr) {
139 SEN_HILOGI("LoadSensorService out");
140 return false;
141 }
142 SEN_HILOGW("LoadSensorService success");
143 int32_t ret = DealAfterServiceAlive();
144 if (ret != ERR_OK) {
145 SEN_HILOGE("DealAfterServiceAlive failed, ret:%{public}d", ret);
146 return false;
147 }
148 return true;
149 }
150
IsValid(const SensorDescription & sensorDesc)151 bool SensorServiceClient::IsValid(const SensorDescription &sensorDesc)
152 {
153 int32_t ret = InitServiceClient();
154 if (ret != ERR_OK) {
155 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
156 }
157 std::lock_guard<std::mutex> clientLock(clientMutex_);
158 if (sensorList_.empty()) {
159 SEN_HILOGE("sensorList_ cannot be empty");
160 return false;
161 }
162 for (auto &sensor : sensorList_) {
163 if ((sensor.GetDeviceId() == sensorDesc.deviceId) && (sensor.GetSensorTypeId() == sensorDesc.sensorType) &&
164 (sensor.GetSensorId() == sensorDesc.sensorId)) {
165 return true;
166 }
167 }
168 return false;
169 }
170
EnableSensor(const SensorDescription & sensorDesc,int64_t samplingPeriod,int64_t maxReportDelay)171 int32_t SensorServiceClient::EnableSensor(const SensorDescription &sensorDesc, int64_t samplingPeriod,
172 int64_t maxReportDelay)
173 {
174 CALL_LOG_ENTER;
175 int32_t ret = InitServiceClient();
176 if (ret != ERR_OK) {
177 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
178 return ret;
179 }
180 std::lock_guard<std::mutex> clientLock(clientMutex_);
181 CHKPR(sensorServer_, ERROR);
182 #ifdef HIVIEWDFX_HITRACE_ENABLE
183 StartTrace(HITRACE_TAG_SENSORS, "EnableSensor");
184 #endif // HIVIEWDFX_HITRACE_ENABLE
185 ret = sensorServer_->EnableSensor({sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId,
186 sensorDesc.location}, samplingPeriod, maxReportDelay);
187 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR, ret);
188 #ifdef HIVIEWDFX_HITRACE_ENABLE
189 FinishTrace(HITRACE_TAG_SENSORS);
190 #endif // HIVIEWDFX_HITRACE_ENABLE
191 if (ret == ERR_OK) {
192 UpdateSensorInfoMap(sensorDesc, samplingPeriod, maxReportDelay);
193 }
194 return ret;
195 }
196
DisableSensor(const SensorDescription & sensorDesc)197 int32_t SensorServiceClient::DisableSensor(const SensorDescription &sensorDesc)
198 {
199 CALL_LOG_ENTER;
200 int32_t ret = InitServiceClient();
201 if (ret != ERR_OK) {
202 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
203 return ret;
204 }
205 std::lock_guard<std::mutex> clientLock(clientMutex_);
206 CHKPR(sensorServer_, ERROR);
207 #ifdef HIVIEWDFX_HITRACE_ENABLE
208 StartTrace(HITRACE_TAG_SENSORS, "DisableSensor");
209 #endif // HIVIEWDFX_HITRACE_ENABLE
210 ret = sensorServer_->DisableSensor({sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId,
211 sensorDesc.location});
212 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DISABLE_SENSOR, ret);
213 #ifdef HIVIEWDFX_HITRACE_ENABLE
214 FinishTrace(HITRACE_TAG_SENSORS);
215 #endif // HIVIEWDFX_HITRACE_ENABLE
216 if (ret == ERR_OK) {
217 DeleteSensorInfoItem(sensorDesc);
218 }
219 return ret;
220 }
221
GetSensorList()222 std::vector<Sensor> SensorServiceClient::GetSensorList()
223 {
224 CALL_LOG_ENTER;
225 int32_t ret = InitServiceClient();
226 if (ret != ERR_OK) {
227 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
228 return {};
229 }
230 std::lock_guard<std::mutex> clientLock(clientMutex_);
231 if (sensorList_.empty()) {
232 SEN_HILOGE("sensorList_ cannot be empty");
233 }
234 return sensorList_;
235 }
236
GetSensorListByDevice(int32_t deviceId,std::vector<Sensor> & singleDevSensors)237 int32_t SensorServiceClient::GetSensorListByDevice(int32_t deviceId, std::vector<Sensor> &singleDevSensors)
238 {
239 CALL_LOG_ENTER;
240 int32_t ret = InitServiceClient();
241 if (ret != ERR_OK) {
242 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
243 return ret;
244 }
245 std::lock_guard<std::mutex> clientLock(clientMutex_);
246 for (const auto& sensor : sensorList_) {
247 if (sensor.GetDeviceId() == deviceId) {
248 SEN_HILOGD("sensor.GetDeviceId():%{public}d, deviceIndex:%{public}d", sensor.GetDeviceId(), deviceId);
249 singleDevSensors.push_back(sensor);
250 }
251 }
252 if (singleDevSensors.empty()) {
253 singleDevSensors = GetSensorListByDevice(deviceId);
254 if (singleDevSensors.empty()) {
255 SEN_HILOGW("singleDevSensors is empty");
256 }
257 }
258 return ERR_OK;
259 }
260
GetSensorListByDevice(int32_t deviceId)261 std::vector<Sensor> SensorServiceClient::GetSensorListByDevice(int32_t deviceId)
262 {
263 CALL_LOG_ENTER;
264 std::vector<Sensor> singleDevSensors;
265 int32_t ret = sensorServer_->GetSensorListByDevice(deviceId, singleDevSensors);
266 if (ret != ERR_OK) {
267 SEN_HILOGE("GetSensorListByDevice failed, ret:%{public}d", ret);
268 return {};
269 }
270 if (singleDevSensors.empty()) {
271 SEN_HILOGE("GetSensorListByDevice failed,singleDevSensors cannot be empty");
272 return {};
273 }
274 for (const auto& newSensor : singleDevSensors) {
275 bool found = false;
276 for (const auto& oldSensor : sensorList_) {
277 if (oldSensor.GetDeviceId() == newSensor.GetDeviceId() &&
278 oldSensor.GetSensorId() == newSensor.GetSensorId() &&
279 oldSensor.GetSensorTypeId() == newSensor.GetSensorTypeId()) {
280 found = true;
281 break;
282 }
283 }
284 if (!found) {
285 SEN_HILOGD("Sensor not found in sensorList_");
286 sensorList_.push_back(newSensor);
287 }
288 }
289 return singleDevSensors;
290 }
291
GetLocalDeviceId(int32_t & deviceId)292 int32_t SensorServiceClient::GetLocalDeviceId(int32_t& deviceId)
293 {
294 CALL_LOG_ENTER;
295 if (sensorList_.empty()) {
296 std::vector<Sensor> allSensors = GetSensorList();
297 }
298 for (const auto& sensor : sensorList_) {
299 if (sensor.GetLocation() == 1) {
300 SEN_HILOGD("local deviceId is:%{public}d", sensor.GetDeviceId());
301 deviceId = sensor.GetDeviceId();
302 return ERR_OK;
303 }
304 }
305 SEN_HILOGE("Get local deviceId failed, sensor list size: %{public}zu", sensorList_.size());
306 return SERVICE_EXCEPTION;
307 }
308
TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)309 int32_t SensorServiceClient::TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)
310 {
311 SEN_HILOGI("In");
312 CHKPR(sensorDataChannel, INVALID_POINTER);
313 {
314 std::lock_guard<std::mutex> channelLock(channelMutex_);
315 dataChannel_ = sensorDataChannel;
316 }
317 int32_t ret = InitServiceClient();
318 if (ret != ERR_OK) {
319 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
320 return ret;
321 }
322 std::lock_guard<std::mutex> clientLock(clientMutex_);
323 CHKPR(sensorServer_, ERROR);
324 #ifdef HIVIEWDFX_HITRACE_ENABLE
325 StartTrace(HITRACE_TAG_SENSORS, "TransferDataChannel");
326 #endif // HIVIEWDFX_HITRACE_ENABLE
327 CHKPR(sensorClientStub_, INVALID_POINTER);
328 auto remoteObject = sensorClientStub_->AsObject();
329 CHKPR(remoteObject, INVALID_POINTER);
330 ret = sensorServer_->TransferDataChannel(sensorDataChannel->GetSendDataFd(), remoteObject);
331 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL, ret);
332 #ifdef HIVIEWDFX_HITRACE_ENABLE
333 FinishTrace(HITRACE_TAG_SENSORS);
334 #endif // HIVIEWDFX_HITRACE_ENABLE
335 SEN_HILOGI("Done");
336 return ret;
337 }
338
DestroyDataChannel()339 int32_t SensorServiceClient::DestroyDataChannel()
340 {
341 CALL_LOG_ENTER;
342 int32_t ret = InitServiceClient();
343 if (ret != ERR_OK) {
344 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
345 return ret;
346 }
347 std::lock_guard<std::mutex> clientLock(clientMutex_);
348 CHKPR(sensorServer_, ERROR);
349 #ifdef HIVIEWDFX_HITRACE_ENABLE
350 StartTrace(HITRACE_TAG_SENSORS, "DestroyDataChannel");
351 #endif // HIVIEWDFX_HITRACE_ENABLE
352 CHKPR(sensorClientStub_, INVALID_POINTER);
353 auto remoteObject = sensorClientStub_->AsObject();
354 CHKPR(remoteObject, INVALID_POINTER);
355 ret = sensorServer_->DestroySensorChannel(remoteObject);
356 if (ret != NO_ERROR) {
357 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
358 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME",
359 "DestroySensorChannel", "ERROR_CODE", ret);
360 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
361 }
362 #ifdef HIVIEWDFX_HITRACE_ENABLE
363 FinishTrace(HITRACE_TAG_SENSORS);
364 #endif // HIVIEWDFX_HITRACE_ENABLE
365 return ret;
366 }
367
ReenableSensor()368 void SensorServiceClient::ReenableSensor()
369 {
370 CALL_LOG_ENTER;
371 {
372 std::lock_guard<std::mutex> clientLock(clientMutex_);
373 std::lock_guard<std::mutex> mapLock(mapMutex_);
374 for (const auto &it : sensorInfoMap_) {
375 if (sensorServer_ != nullptr) {
376 int32_t ret = sensorServer_->EnableSensor({it.first.deviceId, it.first.sensorType, it.first.sensorId,
377 it.first.location}, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs());
378 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR, ret);
379 }
380 }
381 }
382 if (!isConnected_) {
383 SEN_HILOGD("Previous socket channel status is false, not need retry creat socket channel");
384 return;
385 }
386 Disconnect();
387 CreateSocketChannel();
388 }
389
CreateClientRemoteObject()390 int32_t SensorServiceClient::CreateClientRemoteObject()
391 {
392 CALL_LOG_ENTER;
393 int32_t ret = InitServiceClient();
394 if (ret != ERR_OK) {
395 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
396 }
397 return ret;
398 }
399
TransferClientRemoteObject()400 int32_t SensorServiceClient::TransferClientRemoteObject()
401 {
402 CALL_LOG_ENTER;
403 #ifdef HIVIEWDFX_HITRACE_ENABLE
404 StartTrace(HITRACE_TAG_SENSORS, "TransferClientRemoteObject");
405 #endif // HIVIEWDFX_HITRACE_ENABLE
406 CHKPR(sensorClientStub_, INVALID_POINTER);
407 auto remoteObject = sensorClientStub_->AsObject();
408 CHKPR(remoteObject, INVALID_POINTER);
409 int32_t ret = sensorServer_->TransferClientRemoteObject(remoteObject);
410 SEN_HILOGI("TransferClientRemoteObject ret:%{public}d", ret);
411 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_TRANSFER_CLIENT_REMOTE_OBJECT, ret);
412 #ifdef HIVIEWDFX_HITRACE_ENABLE
413 FinishTrace(HITRACE_TAG_SENSORS);
414 #endif // HIVIEWDFX_HITRACE_ENABLE
415 SEN_HILOGI("Done");
416 return ret;
417 }
418
DestroyClientRemoteObject()419 int32_t SensorServiceClient::DestroyClientRemoteObject()
420 {
421 CALL_LOG_ENTER;
422 int32_t ret = InitServiceClient();
423 if (ret != ERR_OK) {
424 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
425 }
426 std::lock_guard<std::mutex> clientLock(clientMutex_);
427 CHKPR(sensorServer_, ERROR);
428 #ifdef HIVIEWDFX_HITRACE_ENABLE
429 StartTrace(HITRACE_TAG_SENSORS, "TransferClientRemoteObject");
430 #endif // HIVIEWDFX_HITRACE_ENABLE
431 CHKPR(sensorClientStub_, INVALID_POINTER);
432 auto remoteObject = sensorClientStub_->AsObject();
433 CHKPR(remoteObject, INVALID_POINTER);
434 ret = sensorServer_->DestroyClientRemoteObject(remoteObject);
435 SEN_HILOGI("DestroyClientRemoteObject ret:%{public}d", ret);
436 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DESTROY_CLIENT_REMOTE_OBJECT, ret);
437 #ifdef HIVIEWDFX_HITRACE_ENABLE
438 FinishTrace(HITRACE_TAG_SENSORS);
439 #endif // HIVIEWDFX_HITRACE_ENABLE
440 SEN_HILOGI("Done");
441 return ret;
442 }
443
WriteHiSysIPCEvent(ISensorServiceIpcCode code,int32_t ret)444 void SensorServiceClient::WriteHiSysIPCEvent(ISensorServiceIpcCode code, int32_t ret)
445 {
446 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
447 if (ret != NO_ERROR) {
448 switch (code) {
449 case ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR:
450 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
451 "PKG_NAME", "EnableSensor", "ERROR_CODE", ret);
452 break;
453 case ISensorServiceIpcCode::COMMAND_DISABLE_SENSOR:
454 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
455 "PKG_NAME", "DisableSensor", "ERROR_CODE", ret);
456 break;
457 case ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST:
458 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
459 "PKG_NAME", "GetSensorList", "ERROR_CODE", ret);
460 break;
461 case ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL:
462 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
463 "PKG_NAME", "TransferDataChannel", "ERROR_CODE", ret);
464 break;
465 case ISensorServiceIpcCode::COMMAND_CREATE_SOCKET_CHANNEL:
466 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
467 "PKG_NAME", "CreateSocketChannel", "ERROR_CODE", ret);
468 break;
469 case ISensorServiceIpcCode::COMMAND_DESTROY_SOCKET_CHANNEL:
470 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
471 "PKG_NAME", "DestroySocketChannel", "ERROR_CODE", ret);
472 break;
473 case ISensorServiceIpcCode::COMMAND_ENABLE_ACTIVE_INFO_C_B:
474 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
475 "PKG_NAME", "EnableActiveInfoCB", "ERROR_CODE", ret);
476 break;
477 case ISensorServiceIpcCode::COMMAND_DISABLE_ACTIVE_INFO_C_B:
478 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
479 "PKG_NAME", "DisableActiveInfoCB", "ERROR_CODE", ret);
480 break;
481 case ISensorServiceIpcCode::COMMAND_RESET_SENSORS:
482 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
483 "PKG_NAME", "ResetSensors", "ERROR_CODE", ret);
484 break;
485 default:
486 break;
487 }
488 }
489 WriteHiSysIPCEventSplit(code, ret);
490 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
491 }
492
WriteHiSysIPCEventSplit(ISensorServiceIpcCode code,int32_t ret)493 void SensorServiceClient::WriteHiSysIPCEventSplit(ISensorServiceIpcCode code, int32_t ret)
494 {
495 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
496 if (ret != NO_ERROR) {
497 switch (code) {
498 case ISensorServiceIpcCode::COMMAND_SET_DEVICE_STATUS:
499 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
500 "PKG_NAME", "SetDeviceStatus", "ERROR_CODE", ret);
501 break;
502 case ISensorServiceIpcCode::COMMAND_TRANSFER_CLIENT_REMOTE_OBJECT:
503 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
504 "PKG_NAME", "TransferClientRemoteObject", "ERROR_CODE", ret);
505 break;
506 case ISensorServiceIpcCode::COMMAND_DESTROY_CLIENT_REMOTE_OBJECT:
507 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
508 "PKG_NAME", "DestroyClientRemoteObject", "ERROR_CODE", ret);
509 break;
510 default:
511 SEN_HILOGW("Code does not exist, code:%{public}d", static_cast<int32_t>(code));
512 break;
513 }
514 }
515 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
516 }
517
ProcessDeathObserver(const wptr<IRemoteObject> & object)518 void SensorServiceClient::ProcessDeathObserver(const wptr<IRemoteObject> &object)
519 {
520 CALL_LOG_ENTER;
521 {
522 std::lock_guard<std::mutex> channelLock(channelMutex_);
523 if (dataChannel_ == nullptr) {
524 SEN_HILOGI("dataChannel_ is nullptr");
525 {
526 std::lock_guard<std::mutex> clientLock(clientMutex_);
527 sensorServer_ = nullptr;
528 }
529 if (InitServiceClient() != ERR_OK) {
530 SEN_HILOGE("InitServiceClient failed");
531 return;
532 }
533 } else {
534 SEN_HILOGI("dataChannel_ is not nullptr");
535 dataChannel_->DestroySensorDataChannel();
536 int32_t ret = dataChannel_->RestoreSensorDataChannel();
537 if (ret == ERR_OK) {
538 SENSOR_AGENT_IMPL->SetIsChannelCreated(true);
539 }
540 {
541 std::lock_guard<std::mutex> clientLock(clientMutex_);
542 sensorServer_ = nullptr;
543 }
544 if (InitServiceClient() != ERR_OK) {
545 SEN_HILOGE("InitServiceClient failed");
546 dataChannel_->DestroySensorDataChannel();
547 SENSOR_AGENT_IMPL->SetIsChannelCreated(false);
548 return;
549 }
550 std::lock_guard<std::mutex> clientLock(clientMutex_);
551 if (sensorServer_ != nullptr && sensorClientStub_ != nullptr) {
552 auto remoteObject = sensorClientStub_->AsObject();
553 if (remoteObject != nullptr) {
554 ret = sensorServer_->TransferDataChannel(dataChannel_->GetSendDataFd(), remoteObject);
555 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL, ret);
556 }
557 }
558 }
559 }
560 ReenableSensor();
561 }
562
UpdateSensorInfoMap(const SensorDescription & sensorDesc,int64_t samplingPeriod,int64_t maxReportDelay)563 void SensorServiceClient::UpdateSensorInfoMap(const SensorDescription &sensorDesc, int64_t samplingPeriod,
564 int64_t maxReportDelay)
565 {
566 SEN_HILOGI("In");
567 SensorBasicInfo sensorInfo;
568 sensorInfo.SetSamplingPeriodNs(samplingPeriod);
569 sensorInfo.SetMaxReportDelayNs(maxReportDelay);
570 sensorInfo.SetSensorState(true);
571 std::lock_guard<std::mutex> mapLock(mapMutex_);
572 sensorInfoMap_[sensorDesc] = sensorInfo;
573 SEN_HILOGI("Done");
574 return;
575 }
576
DeleteSensorInfoItem(const SensorDescription & sensorDesc)577 void SensorServiceClient::DeleteSensorInfoItem(const SensorDescription &sensorDesc)
578 {
579 SEN_HILOGI("In");
580 std::lock_guard<std::mutex> mapLock(mapMutex_);
581 auto it = sensorInfoMap_.find(sensorDesc);
582 if (it != sensorInfoMap_.end()) {
583 sensorInfoMap_.erase(it);
584 }
585 SEN_HILOGI("Done");
586 return;
587 }
588
SuspendSensors(int32_t pid)589 int32_t SensorServiceClient::SuspendSensors(int32_t pid)
590 {
591 CALL_LOG_ENTER;
592 int32_t ret = InitServiceClient();
593 if (ret != ERR_OK) {
594 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
595 return ret;
596 }
597 std::lock_guard<std::mutex> clientLock(clientMutex_);
598 CHKPR(sensorServer_, ERROR);
599 #ifdef HIVIEWDFX_HITRACE_ENABLE
600 StartTrace(HITRACE_TAG_SENSORS, "SuspendSensors");
601 #endif // HIVIEWDFX_HITRACE_ENABLE
602 ret = sensorServer_->SuspendSensors(pid);
603 #ifdef HIVIEWDFX_HITRACE_ENABLE
604 FinishTrace(HITRACE_TAG_SENSORS);
605 #endif // HIVIEWDFX_HITRACE_ENABLE
606 return ret;
607 }
608
ResumeSensors(int32_t pid)609 int32_t SensorServiceClient::ResumeSensors(int32_t pid)
610 {
611 CALL_LOG_ENTER;
612 int32_t ret = InitServiceClient();
613 if (ret != ERR_OK) {
614 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
615 return ret;
616 }
617 std::lock_guard<std::mutex> clientLock(clientMutex_);
618 CHKPR(sensorServer_, ERROR);
619 #ifdef HIVIEWDFX_HITRACE_ENABLE
620 StartTrace(HITRACE_TAG_SENSORS, "ResumeSensors");
621 #endif // HIVIEWDFX_HITRACE_ENABLE
622 ret = sensorServer_->ResumeSensors(pid);
623 #ifdef HIVIEWDFX_HITRACE_ENABLE
624 FinishTrace(HITRACE_TAG_SENSORS);
625 #endif // HIVIEWDFX_HITRACE_ENABLE
626 return ret;
627 }
628
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)629 int32_t SensorServiceClient::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
630 {
631 CALL_LOG_ENTER;
632 int32_t ret = InitServiceClient();
633 if (ret != ERR_OK) {
634 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
635 return ret;
636 }
637 std::lock_guard<std::mutex> clientLock(clientMutex_);
638 CHKPR(sensorServer_, ERROR);
639 #ifdef HIVIEWDFX_HITRACE_ENABLE
640 StartTrace(HITRACE_TAG_SENSORS, "GetActiveInfoList");
641 #endif // HIVIEWDFX_HITRACE_ENABLE
642 ret = sensorServer_->GetActiveInfoList(pid, activeInfoList);
643 #ifdef HIVIEWDFX_HITRACE_ENABLE
644 FinishTrace(HITRACE_TAG_SENSORS);
645 #endif // HIVIEWDFX_HITRACE_ENABLE
646 return ret;
647 }
648
Register(SensorActiveInfoCB callback,sptr<SensorDataChannel> sensorDataChannel)649 int32_t SensorServiceClient::Register(SensorActiveInfoCB callback, sptr<SensorDataChannel> sensorDataChannel)
650 {
651 CALL_LOG_ENTER;
652 if (!isConnected_) {
653 CHKPR(sensorDataChannel, INVALID_POINTER);
654 {
655 std::lock_guard<std::mutex> channelLock(channelMutex_);
656 dataChannel_ = sensorDataChannel;
657 }
658 int32_t ret = CreateSocketChannel();
659 if (ret != ERR_OK) {
660 SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
661 return ret;
662 }
663 }
664 std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
665 activeInfoCBSet_.insert(callback);
666 return ERR_OK;
667 }
668
Unregister(SensorActiveInfoCB callback)669 int32_t SensorServiceClient::Unregister(SensorActiveInfoCB callback)
670 {
671 CALL_LOG_ENTER;
672 {
673 std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
674 activeInfoCBSet_.erase(callback);
675 if (!activeInfoCBSet_.empty()) {
676 return ERR_OK;
677 }
678 }
679 int32_t ret = InitServiceClient();
680 if (ret != ERR_OK) {
681 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
682 return ret;
683 }
684 std::lock_guard<std::mutex> clientLock(clientMutex_);
685 CHKPR(sensorServer_, ERROR);
686 #ifdef HIVIEWDFX_HITRACE_ENABLE
687 StartTrace(HITRACE_TAG_SENSORS, "DisableActiveInfoCB");
688 #endif // HIVIEWDFX_HITRACE_ENABLE
689 ret = sensorServer_->DisableActiveInfoCB();
690 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DISABLE_ACTIVE_INFO_C_B, ret);
691 #ifdef HIVIEWDFX_HITRACE_ENABLE
692 FinishTrace(HITRACE_TAG_SENSORS);
693 #endif // HIVIEWDFX_HITRACE_ENABLE
694 if (ret != ERR_OK) {
695 SEN_HILOGE("Disable active info callback failed, ret:%{public}d", ret);
696 return ret;
697 }
698 Disconnect();
699 #ifdef HIVIEWDFX_HITRACE_ENABLE
700 StartTrace(HITRACE_TAG_SENSORS, "DestroySocketChannel");
701 #endif // HIVIEWDFX_HITRACE_ENABLE
702 CHKPR(sensorClientStub_, INVALID_POINTER);
703 auto remoteObject = sensorClientStub_->AsObject();
704 CHKPR(remoteObject, INVALID_POINTER);
705 ret = sensorServer_->DestroySocketChannel(remoteObject);
706 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DESTROY_SOCKET_CHANNEL, ret);
707 #ifdef HIVIEWDFX_HITRACE_ENABLE
708 FinishTrace(HITRACE_TAG_SENSORS);
709 #endif // HIVIEWDFX_HITRACE_ENABLE
710 if (ret != ERR_OK) {
711 SEN_HILOGE("Destroy socket channel failed, ret:%{public}d", ret);
712 return ret;
713 }
714 isConnected_ = false;
715 return ERR_OK;
716 }
717
ResetSensors()718 int32_t SensorServiceClient::ResetSensors()
719 {
720 CALL_LOG_ENTER;
721 int32_t ret = InitServiceClient();
722 if (ret != ERR_OK) {
723 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
724 return ret;
725 }
726 std::lock_guard<std::mutex> clientLock(clientMutex_);
727 CHKPR(sensorServer_, ERROR);
728 #ifdef HIVIEWDFX_HITRACE_ENABLE
729 StartTrace(HITRACE_TAG_SENSORS, "ResetSensors");
730 #endif // HIVIEWDFX_HITRACE_ENABLE
731 ret = sensorServer_->ResetSensors();
732 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_RESET_SENSORS, ret);
733 #ifdef HIVIEWDFX_HITRACE_ENABLE
734 FinishTrace(HITRACE_TAG_SENSORS);
735 #endif // HIVIEWDFX_HITRACE_ENABLE
736 return ret;
737 }
738
ReceiveMessage(const char * buf,size_t size)739 void SensorServiceClient::ReceiveMessage(const char *buf, size_t size)
740 {
741 CHKPV(buf);
742 if (size == 0 || size > MAX_PACKET_BUF_SIZE) {
743 SEN_HILOGE("Invalid input param size. size:%{public}zu", size);
744 return;
745 }
746 if (!circBuf_.Write(buf, size)) {
747 SEN_HILOGE("Write data failed. size:%{public}zu", size);
748 }
749 #ifdef OHOS_BUILD_ENABLE_RUST
750 ReadClientPackets(circBuf_.streamBufferPtr_.get(), this, OnPacket);
751 #else
752 OnReadPackets(circBuf_, [this] (NetPacket &pkt) { this->HandleNetPacke(pkt); });
753 #endif // OHOS_BUILD_ENABLE_RUST
754 }
755
HandleNetPacke(NetPacket & pkt)756 void SensorServiceClient::HandleNetPacke(NetPacket &pkt)
757 {
758 auto id = pkt.GetMsgId();
759 if (id != MessageId::ACTIVE_INFO) {
760 SEN_HILOGE("NetPacke message id is not ACTIVE_INFO");
761 return;
762 }
763 SensorActiveInfo sensorActiveInfo;
764 pkt >> sensorActiveInfo.pid >> sensorActiveInfo.sensorId >> sensorActiveInfo.samplingPeriodNs >>
765 sensorActiveInfo.maxReportDelayNs;
766 #ifdef OHOS_BUILD_ENABLE_RUST
767 if (StreamBufferChkRWError(pkt.streamBufferPtr_.get())) {
768 #else
769 if (pkt.ChkRWError()) {
770 #endif // OHOS_BUILD_ENABLE_RUST
771 SEN_HILOGE("Packet read type failed");
772 return;
773 }
774 std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
775 for (auto callback : activeInfoCBSet_) {
776 if (callback != nullptr) {
777 callback(sensorActiveInfo);
778 }
779 }
780 }
781
782 void SensorServiceClient::Disconnect()
783 {
784 CALL_LOG_ENTER;
785 int32_t fd = GetFd();
786 if (fd < 0) {
787 return;
788 }
789 {
790 std::lock_guard<std::mutex> channelLock(channelMutex_);
791 if (dataChannel_ != nullptr) {
792 int32_t ret = dataChannel_->DelFdListener(fd);
793 if (ret != ERR_OK) {
794 SEN_HILOGE("Delete fd listener failed, fd:%{public}d, ret:%{public}d", fd, ret);
795 }
796 }
797 }
798 Close();
799 }
800
801 int32_t SensorServiceClient::CreateSocketClientFd(int32_t &clientFd)
802 {
803 #ifdef HIVIEWDFX_HITRACE_ENABLE
804 StartTrace(HITRACE_TAG_SENSORS, "CreateSocketChannel");
805 #endif // HIVIEWDFX_HITRACE_ENABLE
806 CHKPR(sensorClientStub_, INVALID_POINTER);
807 auto remoteObject = sensorClientStub_->AsObject();
808 CHKPR(remoteObject, INVALID_POINTER);
809 int ret = sensorServer_->CreateSocketChannel(remoteObject, clientFd);
810 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_CREATE_SOCKET_CHANNEL, ret);
811 #ifdef HIVIEWDFX_HITRACE_ENABLE
812 FinishTrace(HITRACE_TAG_SENSORS);
813 #endif // HIVIEWDFX_HITRACE_ENABLE
814 return ret;
815 }
816
817 int32_t SensorServiceClient::CreateSocketChannel()
818 {
819 CALL_LOG_ENTER;
820 int32_t ret = InitServiceClient();
821 if (ret != ERR_OK) {
822 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
823 return ret;
824 }
825 std::lock_guard<std::mutex> clientLock(clientMutex_);
826 CHKPR(sensorServer_, ERROR);
827 int32_t clientFd = -1;
828 ret = CreateSocketClientFd(clientFd);
829 fdsan_exchange_owner_tag(clientFd, 0, TAG);
830 if (ret != ERR_OK || clientFd < 0) {
831 Close();
832 SEN_HILOGE("Create socket channel failed, ret:%{public}d", ret);
833 return ret;
834 }
835 #ifdef OHOS_BUILD_ENABLE_RUST
836 StreamSocketSetFd(streamSocketPtr_.get(), clientFd);
837 #else
838 fd_ = clientFd;
839 #endif // OHOS_BUILD_ENABLE_RUST
840 {
841 std::lock_guard<std::mutex> channelLock(channelMutex_);
842 if (dataChannel_->AddFdListener(GetFd(),
843 [this] (const char *buf, size_t size) { this->ReceiveMessage(buf, size); },
844 [this] { this->Disconnect(); })!= ERR_OK) {
845 Close();
846 SEN_HILOGE("Add fd listener failed, fd:%{public}d", GetFd());
847 return ERROR;
848 }
849 }
850 #ifdef HIVIEWDFX_HITRACE_ENABLE
851 StartTrace(HITRACE_TAG_SENSORS, "EnableActiveInfoCB");
852 #endif // HIVIEWDFX_HITRACE_ENABLE
853 ret = sensorServer_->EnableActiveInfoCB();
854 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_ACTIVE_INFO_C_B, ret);
855 #ifdef HIVIEWDFX_HITRACE_ENABLE
856 FinishTrace(HITRACE_TAG_SENSORS);
857 #endif // HIVIEWDFX_HITRACE_ENABLE
858 if (ret != ERR_OK) {
859 SEN_HILOGE("Enable active info callback failed, ret:%{public}d", ret);
860 Disconnect();
861 return ret;
862 }
863 isConnected_ = true;
864 return ERR_OK;
865 }
866
867 void SensorServiceClient::SetDeviceStatus(uint32_t deviceStatus)
868 {
869 CALL_LOG_ENTER;
870 int32_t ret = InitServiceClient();
871 if (ret != ERR_OK) {
872 SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
873 return;
874 }
875 std::lock_guard<std::mutex> clientLock(clientMutex_);
876 CHKPV(sensorServer_);
877 #ifdef HIVIEWDFX_HITRACE_ENABLE
878 StartTrace(HITRACE_TAG_SENSORS, "SetDeviceStatus");
879 #endif // HIVIEWDFX_HITRACE_ENABLE
880 ret = sensorServer_->SetDeviceStatus(deviceStatus);
881 if (ret != ERR_OK) {
882 SEN_HILOGE("SetDeviceStatus failed, ret:%{public}d", ret);
883 }
884 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_SET_DEVICE_STATUS, ret);
885 #ifdef HIVIEWDFX_HITRACE_ENABLE
886 FinishTrace(HITRACE_TAG_SENSORS);
887 #endif // HIVIEWDFX_HITRACE_ENABLE
888 }
889
890 bool SensorServiceClient::EraseCacheSensorList(const SensorPlugData &info)
891 {
892 CALL_LOG_ENTER;
893 std::lock_guard<std::mutex> clientLock(clientMutex_);
894 if (sensorList_.empty()) {
895 SEN_HILOGE("sensorList_ cannot be empty");
896 return false;
897 }
898 auto it = std::find_if(sensorList_.begin(), sensorList_.end(), [&](const Sensor& sensor) {
899 return sensor.GetDeviceId() == info.deviceId &&
900 sensor.GetSensorTypeId() == info.sensorTypeId &&
901 sensor.GetSensorId() == info.sensorId;
902 });
903 if (it != sensorList_.end()) {
904 sensorList_.erase(it);
905 return true;
906 }
907 SEN_HILOGD("sensorList_ cannot find the sensor");
908 return true;
909 }
910 } // namespace Sensors
911 } // namespace OHOS
912