• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "sensor_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 } // namespace
49 
~SensorServiceClient()50 SensorServiceClient::~SensorServiceClient()
51 {
52     if (sensorServer_ != nullptr && serviceDeathObserver_ != nullptr) {
53         auto remoteObject = sensorServer_->AsObject();
54         if (remoteObject != nullptr) {
55             remoteObject->RemoveDeathRecipient(serviceDeathObserver_);
56         }
57     }
58     Disconnect();
59 }
60 
InitServiceClient()61 int32_t SensorServiceClient::InitServiceClient()
62 {
63     CALL_LOG_ENTER;
64     std::lock_guard<std::mutex> clientLock(clientMutex_);
65     if (sensorServer_ != nullptr) {
66         SEN_HILOGD("Already init");
67         if (sensorList_.empty()) {
68             int32_t ret = sensorServer_->GetSensorList(sensorList_);
69             WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST, ret);
70             SEN_HILOGW("sensorList is %{public}s", sensorList_.empty() ? "empty" : "not empty");
71         }
72         return ERR_OK;
73     }
74     if (sensorClientStub_ == nullptr) {
75         sensorClientStub_ = new (std::nothrow) SensorClientStub();
76     }
77     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
78     CHKPR(systemAbilityManager, SENSOR_NATIVE_SAM_ERR);
79     auto object = systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID);
80     CHKPR(object, SENSOR_NATIVE_GET_SERVICE_ERR);
81     sensorServer_ = iface_cast<ISensorService>(object);
82     if (sensorServer_ != nullptr) {
83         SEN_HILOGD("Get service success");
84         serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorServiceClient *>(this));
85         CHKPR(serviceDeathObserver_, SENSOR_NATIVE_GET_SERVICE_ERR);
86         auto remoteObject = sensorServer_->AsObject();
87         CHKPR(remoteObject, SENSOR_NATIVE_GET_SERVICE_ERR);
88         remoteObject->AddDeathRecipient(serviceDeathObserver_);
89         sensorList_.clear();
90         int32_t ret = sensorServer_->GetSensorList(sensorList_);
91         WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST, ret);
92         if (sensorList_.empty()) {
93             SEN_HILOGW("sensorList_ is empty when connecting to the service for the first time");
94         }
95         return ERR_OK;
96     }
97     SEN_HILOGW("Get service failed");
98 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
99     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SERVICE_EXCEPTION",
100         HiSysEvent::EventType::FAULT, "PKG_NAME", "InitServiceClient", "ERROR_CODE", SENSOR_NATIVE_GET_SERVICE_ERR);
101 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
102     SEN_HILOGE("Get service failed");
103     return SENSOR_NATIVE_GET_SERVICE_ERR;
104 }
105 
IsValid(int32_t sensorId)106 bool SensorServiceClient::IsValid(int32_t sensorId)
107 {
108     int32_t ret = InitServiceClient();
109     if (ret != ERR_OK) {
110         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
111     }
112     std::lock_guard<std::mutex> clientLock(clientMutex_);
113     if (sensorList_.empty()) {
114         SEN_HILOGE("sensorList_ cannot be empty");
115         return false;
116     }
117     for (auto &sensor : sensorList_) {
118         if (sensor.GetSensorId() == sensorId) {
119             return true;
120         }
121     }
122     return false;
123 }
124 
EnableSensor(int32_t sensorId,int64_t samplingPeriod,int64_t maxReportDelay)125 int32_t SensorServiceClient::EnableSensor(int32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay)
126 {
127     CALL_LOG_ENTER;
128     int32_t ret = InitServiceClient();
129     if (ret != ERR_OK) {
130         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
131         return ret;
132     }
133     std::lock_guard<std::mutex> clientLock(clientMutex_);
134     CHKPR(sensorServer_, ERROR);
135 #ifdef HIVIEWDFX_HITRACE_ENABLE
136     StartTrace(HITRACE_TAG_SENSORS, "EnableSensor");
137 #endif // HIVIEWDFX_HITRACE_ENABLE
138     ret = sensorServer_->EnableSensor(sensorId, samplingPeriod, maxReportDelay);
139     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR, ret);
140 #ifdef HIVIEWDFX_HITRACE_ENABLE
141     FinishTrace(HITRACE_TAG_SENSORS);
142 #endif // HIVIEWDFX_HITRACE_ENABLE
143     if (ret == ERR_OK) {
144         UpdateSensorInfoMap(sensorId, samplingPeriod, maxReportDelay);
145     }
146     return ret;
147 }
148 
DisableSensor(int32_t sensorId)149 int32_t SensorServiceClient::DisableSensor(int32_t sensorId)
150 {
151     CALL_LOG_ENTER;
152     int32_t ret = InitServiceClient();
153     if (ret != ERR_OK) {
154         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
155         return ret;
156     }
157     std::lock_guard<std::mutex> clientLock(clientMutex_);
158     CHKPR(sensorServer_, ERROR);
159 #ifdef HIVIEWDFX_HITRACE_ENABLE
160     StartTrace(HITRACE_TAG_SENSORS, "DisableSensor");
161 #endif // HIVIEWDFX_HITRACE_ENABLE
162     ret = sensorServer_->DisableSensor(sensorId);
163     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DISABLE_SENSOR, ret);
164 #ifdef HIVIEWDFX_HITRACE_ENABLE
165     FinishTrace(HITRACE_TAG_SENSORS);
166 #endif // HIVIEWDFX_HITRACE_ENABLE
167     if (ret == ERR_OK) {
168         DeleteSensorInfoItem(sensorId);
169     }
170     return ret;
171 }
172 
GetSensorList()173 std::vector<Sensor> SensorServiceClient::GetSensorList()
174 {
175     CALL_LOG_ENTER;
176     int32_t ret = InitServiceClient();
177     if (ret != ERR_OK) {
178         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
179         return {};
180     }
181     std::lock_guard<std::mutex> clientLock(clientMutex_);
182     if (sensorList_.empty()) {
183         SEN_HILOGE("sensorList_ cannot be empty");
184     }
185     return sensorList_;
186 }
187 
TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)188 int32_t SensorServiceClient::TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)
189 {
190     SEN_HILOGI("In");
191     CHKPR(sensorDataChannel, INVALID_POINTER);
192     {
193         std::lock_guard<std::mutex> channelLock(channelMutex_);
194         dataChannel_ = sensorDataChannel;
195     }
196     int32_t ret = InitServiceClient();
197     if (ret != ERR_OK) {
198         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
199         return ret;
200     }
201     std::lock_guard<std::mutex> clientLock(clientMutex_);
202     CHKPR(sensorServer_, ERROR);
203 #ifdef HIVIEWDFX_HITRACE_ENABLE
204     StartTrace(HITRACE_TAG_SENSORS, "TransferDataChannel");
205 #endif // HIVIEWDFX_HITRACE_ENABLE
206     CHKPR(sensorClientStub_, INVALID_POINTER);
207     auto remoteObject = sensorClientStub_->AsObject();
208     CHKPR(remoteObject, INVALID_POINTER);
209     ret = sensorServer_->TransferDataChannel(sensorDataChannel->GetSendDataFd(), remoteObject);
210     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL, ret);
211 #ifdef HIVIEWDFX_HITRACE_ENABLE
212     FinishTrace(HITRACE_TAG_SENSORS);
213 #endif // HIVIEWDFX_HITRACE_ENABLE
214     SEN_HILOGI("Done");
215     return ret;
216 }
217 
DestroyDataChannel()218 int32_t SensorServiceClient::DestroyDataChannel()
219 {
220     CALL_LOG_ENTER;
221     int32_t ret = InitServiceClient();
222     if (ret != ERR_OK) {
223         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
224         return ret;
225     }
226     std::lock_guard<std::mutex> clientLock(clientMutex_);
227     CHKPR(sensorServer_, ERROR);
228 #ifdef HIVIEWDFX_HITRACE_ENABLE
229     StartTrace(HITRACE_TAG_SENSORS, "DestroyDataChannel");
230 #endif // HIVIEWDFX_HITRACE_ENABLE
231     CHKPR(sensorClientStub_, INVALID_POINTER);
232     auto remoteObject = sensorClientStub_->AsObject();
233     CHKPR(remoteObject, INVALID_POINTER);
234     ret = sensorServer_->DestroySensorChannel(remoteObject);
235     if (ret != NO_ERROR) {
236 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
237         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME",
238             "DestroySensorChannel", "ERROR_CODE", ret);
239 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
240     }
241 #ifdef HIVIEWDFX_HITRACE_ENABLE
242     FinishTrace(HITRACE_TAG_SENSORS);
243 #endif // HIVIEWDFX_HITRACE_ENABLE
244     return ret;
245 }
246 
ReenableSensor()247 void SensorServiceClient::ReenableSensor()
248 {
249     CALL_LOG_ENTER;
250     {
251         std::lock_guard<std::mutex> clientLock(clientMutex_);
252         std::lock_guard<std::mutex> mapLock(mapMutex_);
253         for (const auto &it : sensorInfoMap_) {
254             if (sensorServer_ != nullptr) {
255                 int32_t ret = sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(),
256                     it.second.GetMaxReportDelayNs());
257                 WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR, ret);
258             }
259         }
260     }
261     if (!isConnected_) {
262         SEN_HILOGD("Previous socket channel status is false, not need retry creat socket channel");
263         return;
264     }
265     Disconnect();
266     CreateSocketChannel();
267 }
268 
WriteHiSysIPCEvent(ISensorServiceIpcCode code,int32_t ret)269 void SensorServiceClient::WriteHiSysIPCEvent(ISensorServiceIpcCode code, int32_t ret)
270 {
271 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
272     if (ret != NO_ERROR) {
273         switch (code) {
274             case ISensorServiceIpcCode::COMMAND_ENABLE_SENSOR:
275                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
276                     "PKG_NAME", "EnableSensor", "ERROR_CODE", ret);
277                 break;
278             case ISensorServiceIpcCode::COMMAND_DISABLE_SENSOR:
279                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
280                     "PKG_NAME", "DisableSensor", "ERROR_CODE", ret);
281                 break;
282             case ISensorServiceIpcCode::COMMAND_GET_SENSOR_LIST:
283                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
284                     "PKG_NAME", "GetSensorList", "ERROR_CODE", ret);
285                 break;
286             case ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL:
287                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
288                     "PKG_NAME", "TransferDataChannel", "ERROR_CODE", ret);
289                 break;
290             case ISensorServiceIpcCode::COMMAND_CREATE_SOCKET_CHANNEL:
291                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
292                     "PKG_NAME", "CreateSocketChannel", "ERROR_CODE", ret);
293                 break;
294             case ISensorServiceIpcCode::COMMAND_DESTROY_SOCKET_CHANNEL:
295                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
296                     "PKG_NAME", "DestroySocketChannel", "ERROR_CODE", ret);
297                 break;
298             case ISensorServiceIpcCode::COMMAND_ENABLE_ACTIVE_INFO_C_B:
299                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
300                     "PKG_NAME", "EnableActiveInfoCB", "ERROR_CODE", ret);
301                 break;
302             case ISensorServiceIpcCode::COMMAND_DISABLE_ACTIVE_INFO_C_B:
303                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
304                     "PKG_NAME", "DisableActiveInfoCB", "ERROR_CODE", ret);
305                 break;
306             case ISensorServiceIpcCode::COMMAND_RESET_SENSORS:
307                 HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION", HiSysEvent::EventType::FAULT,
308                     "PKG_NAME", "ResetSensors", "ERROR_CODE", ret);
309                 break;
310             default:
311                 SEN_HILOGW("Code does not exist, code:%{public}d", static_cast<int32_t>(code));
312                 break;
313         }
314     }
315 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
316 }
317 
ProcessDeathObserver(const wptr<IRemoteObject> & object)318 void SensorServiceClient::ProcessDeathObserver(const wptr<IRemoteObject> &object)
319 {
320     CALL_LOG_ENTER;
321     {
322         std::lock_guard<std::mutex> channelLock(channelMutex_);
323         if (dataChannel_ == nullptr) {
324             SEN_HILOGI("dataChannel_ is nullptr");
325             {
326                 std::lock_guard<std::mutex> clientLock(clientMutex_);
327                 sensorServer_ = nullptr;
328             }
329             if (InitServiceClient() != ERR_OK) {
330                 SEN_HILOGE("InitServiceClient failed");
331                 return;
332             }
333         } else {
334             SEN_HILOGI("dataChannel_ is not nullptr");
335             dataChannel_->DestroySensorDataChannel();
336             int32_t ret = dataChannel_->RestoreSensorDataChannel();
337             if (ret == ERR_OK) {
338                 SENSOR_AGENT_IMPL->SetIsChannelCreated(true);
339             }
340             {
341                 std::lock_guard<std::mutex> clientLock(clientMutex_);
342                 sensorServer_ = nullptr;
343             }
344             if (InitServiceClient() != ERR_OK) {
345                 SEN_HILOGE("InitServiceClient failed");
346                 dataChannel_->DestroySensorDataChannel();
347                 SENSOR_AGENT_IMPL->SetIsChannelCreated(false);
348                 return;
349             }
350             std::lock_guard<std::mutex> clientLock(clientMutex_);
351             if (sensorServer_ != nullptr && sensorClientStub_ != nullptr) {
352                 auto remoteObject = sensorClientStub_->AsObject();
353                 if (remoteObject != nullptr) {
354                     ret = sensorServer_->TransferDataChannel(dataChannel_->GetSendDataFd(), remoteObject);
355                     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_TRANSFER_DATA_CHANNEL, ret);
356                 }
357             }
358         }
359     }
360     ReenableSensor();
361 }
362 
UpdateSensorInfoMap(int32_t sensorId,int64_t samplingPeriod,int64_t maxReportDelay)363 void SensorServiceClient::UpdateSensorInfoMap(int32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay)
364 {
365     SEN_HILOGI("In");
366     SensorBasicInfo sensorInfo;
367     sensorInfo.SetSamplingPeriodNs(samplingPeriod);
368     sensorInfo.SetMaxReportDelayNs(maxReportDelay);
369     sensorInfo.SetSensorState(true);
370     std::lock_guard<std::mutex> mapLock(mapMutex_);
371     sensorInfoMap_[sensorId] = sensorInfo;
372     SEN_HILOGI("Done");
373     return;
374 }
375 
DeleteSensorInfoItem(int32_t sensorId)376 void SensorServiceClient::DeleteSensorInfoItem(int32_t sensorId)
377 {
378     SEN_HILOGI("In");
379     std::lock_guard<std::mutex> mapLock(mapMutex_);
380     auto it = sensorInfoMap_.find(sensorId);
381     if (it != sensorInfoMap_.end()) {
382         sensorInfoMap_.erase(it);
383     }
384     SEN_HILOGI("Done");
385     return;
386 }
387 
SuspendSensors(int32_t pid)388 int32_t SensorServiceClient::SuspendSensors(int32_t pid)
389 {
390     CALL_LOG_ENTER;
391     int32_t ret = InitServiceClient();
392     if (ret != ERR_OK) {
393         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
394         return ret;
395     }
396     std::lock_guard<std::mutex> clientLock(clientMutex_);
397     CHKPR(sensorServer_, ERROR);
398 #ifdef HIVIEWDFX_HITRACE_ENABLE
399     StartTrace(HITRACE_TAG_SENSORS, "SuspendSensors");
400 #endif // HIVIEWDFX_HITRACE_ENABLE
401     ret = sensorServer_->SuspendSensors(pid);
402 #ifdef HIVIEWDFX_HITRACE_ENABLE
403     FinishTrace(HITRACE_TAG_SENSORS);
404 #endif // HIVIEWDFX_HITRACE_ENABLE
405     return ret;
406 }
407 
ResumeSensors(int32_t pid)408 int32_t SensorServiceClient::ResumeSensors(int32_t pid)
409 {
410     CALL_LOG_ENTER;
411     int32_t ret = InitServiceClient();
412     if (ret != ERR_OK) {
413         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
414         return ret;
415     }
416     std::lock_guard<std::mutex> clientLock(clientMutex_);
417     CHKPR(sensorServer_, ERROR);
418 #ifdef HIVIEWDFX_HITRACE_ENABLE
419     StartTrace(HITRACE_TAG_SENSORS, "ResumeSensors");
420 #endif // HIVIEWDFX_HITRACE_ENABLE
421     ret = sensorServer_->ResumeSensors(pid);
422 #ifdef HIVIEWDFX_HITRACE_ENABLE
423     FinishTrace(HITRACE_TAG_SENSORS);
424 #endif // HIVIEWDFX_HITRACE_ENABLE
425     return ret;
426 }
427 
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)428 int32_t SensorServiceClient::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
429 {
430     CALL_LOG_ENTER;
431     int32_t ret = InitServiceClient();
432     if (ret != ERR_OK) {
433         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
434         return ret;
435     }
436     std::lock_guard<std::mutex> clientLock(clientMutex_);
437     CHKPR(sensorServer_, ERROR);
438 #ifdef HIVIEWDFX_HITRACE_ENABLE
439     StartTrace(HITRACE_TAG_SENSORS, "GetActiveInfoList");
440 #endif // HIVIEWDFX_HITRACE_ENABLE
441     ret = sensorServer_->GetActiveInfoList(pid, activeInfoList);
442 #ifdef HIVIEWDFX_HITRACE_ENABLE
443     FinishTrace(HITRACE_TAG_SENSORS);
444 #endif // HIVIEWDFX_HITRACE_ENABLE
445     return ret;
446 }
447 
Register(SensorActiveInfoCB callback,sptr<SensorDataChannel> sensorDataChannel)448 int32_t SensorServiceClient::Register(SensorActiveInfoCB callback, sptr<SensorDataChannel> sensorDataChannel)
449 {
450     CALL_LOG_ENTER;
451     if (!isConnected_) {
452         CHKPR(sensorDataChannel, INVALID_POINTER);
453         {
454             std::lock_guard<std::mutex> channelLock(channelMutex_);
455             dataChannel_ = sensorDataChannel;
456         }
457         int32_t ret = CreateSocketChannel();
458         if (ret != ERR_OK) {
459             SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
460             return ret;
461         }
462     }
463     std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
464     activeInfoCBSet_.insert(callback);
465     return ERR_OK;
466 }
467 
Unregister(SensorActiveInfoCB callback)468 int32_t SensorServiceClient::Unregister(SensorActiveInfoCB callback)
469 {
470     CALL_LOG_ENTER;
471     {
472         std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
473         activeInfoCBSet_.erase(callback);
474         if (!activeInfoCBSet_.empty()) {
475             return ERR_OK;
476         }
477     }
478     int32_t ret = InitServiceClient();
479     if (ret != ERR_OK) {
480         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
481         return ret;
482     }
483     std::lock_guard<std::mutex> clientLock(clientMutex_);
484     CHKPR(sensorServer_, ERROR);
485 #ifdef HIVIEWDFX_HITRACE_ENABLE
486     StartTrace(HITRACE_TAG_SENSORS, "DisableActiveInfoCB");
487 #endif // HIVIEWDFX_HITRACE_ENABLE
488     ret = sensorServer_->DisableActiveInfoCB();
489     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DISABLE_ACTIVE_INFO_C_B, ret);
490 #ifdef HIVIEWDFX_HITRACE_ENABLE
491     FinishTrace(HITRACE_TAG_SENSORS);
492 #endif // HIVIEWDFX_HITRACE_ENABLE
493     if (ret != ERR_OK) {
494         SEN_HILOGE("Disable active info callback failed, ret:%{public}d", ret);
495         return ret;
496     }
497     Disconnect();
498 #ifdef HIVIEWDFX_HITRACE_ENABLE
499     StartTrace(HITRACE_TAG_SENSORS, "DestroySocketChannel");
500 #endif // HIVIEWDFX_HITRACE_ENABLE
501     CHKPR(sensorClientStub_, INVALID_POINTER);
502     auto remoteObject = sensorClientStub_->AsObject();
503     CHKPR(remoteObject, INVALID_POINTER);
504     ret = sensorServer_->DestroySocketChannel(remoteObject);
505     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_DESTROY_SOCKET_CHANNEL, ret);
506 #ifdef HIVIEWDFX_HITRACE_ENABLE
507     FinishTrace(HITRACE_TAG_SENSORS);
508 #endif // HIVIEWDFX_HITRACE_ENABLE
509     if (ret != ERR_OK) {
510         SEN_HILOGE("Destroy socket channel failed, ret:%{public}d", ret);
511         return ret;
512     }
513     isConnected_ = false;
514     return ERR_OK;
515 }
516 
ResetSensors()517 int32_t SensorServiceClient::ResetSensors()
518 {
519     CALL_LOG_ENTER;
520     int32_t ret = InitServiceClient();
521     if (ret != ERR_OK) {
522         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
523         return ret;
524     }
525     std::lock_guard<std::mutex> clientLock(clientMutex_);
526     CHKPR(sensorServer_, ERROR);
527 #ifdef HIVIEWDFX_HITRACE_ENABLE
528     StartTrace(HITRACE_TAG_SENSORS, "ResetSensors");
529 #endif // HIVIEWDFX_HITRACE_ENABLE
530     ret = sensorServer_->ResetSensors();
531     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_RESET_SENSORS, ret);
532 #ifdef HIVIEWDFX_HITRACE_ENABLE
533     FinishTrace(HITRACE_TAG_SENSORS);
534 #endif // HIVIEWDFX_HITRACE_ENABLE
535     return ret;
536 }
537 
ReceiveMessage(const char * buf,size_t size)538 void SensorServiceClient::ReceiveMessage(const char *buf, size_t size)
539 {
540     CHKPV(buf);
541     if (size == 0 || size > MAX_PACKET_BUF_SIZE) {
542         SEN_HILOGE("Invalid input param size. size:%{public}zu", size);
543         return;
544     }
545     if (!circBuf_.Write(buf, size)) {
546         SEN_HILOGE("Write data failed. size:%{public}zu", size);
547     }
548 #ifdef OHOS_BUILD_ENABLE_RUST
549     ReadClientPackets(circBuf_.streamBufferPtr_.get(), this, OnPacket);
550 #else
551     OnReadPackets(circBuf_, [this] (NetPacket &pkt) { this->HandleNetPacke(pkt); });
552 #endif // OHOS_BUILD_ENABLE_RUST
553 }
554 
HandleNetPacke(NetPacket & pkt)555 void SensorServiceClient::HandleNetPacke(NetPacket &pkt)
556 {
557     auto id = pkt.GetMsgId();
558     if (id != MessageId::ACTIVE_INFO) {
559         SEN_HILOGE("NetPacke message id is not ACTIVE_INFO");
560         return;
561     }
562     SensorActiveInfo sensorActiveInfo;
563     pkt >> sensorActiveInfo.pid >> sensorActiveInfo.sensorId >> sensorActiveInfo.samplingPeriodNs >>
564         sensorActiveInfo.maxReportDelayNs;
565 #ifdef OHOS_BUILD_ENABLE_RUST
566     if (StreamBufferChkRWError(pkt.streamBufferPtr_.get())) {
567 #else
568     if (pkt.ChkRWError()) {
569 #endif // OHOS_BUILD_ENABLE_RUST
570         SEN_HILOGE("Packet read type failed");
571         return;
572     }
573     std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
574     for (auto callback : activeInfoCBSet_) {
575         if (callback != nullptr) {
576             callback(sensorActiveInfo);
577         }
578     }
579 }
580 
581 void SensorServiceClient::Disconnect()
582 {
583     CALL_LOG_ENTER;
584     int32_t fd = GetFd();
585     if (fd < 0) {
586         return;
587     }
588     {
589         std::lock_guard<std::mutex> channelLock(channelMutex_);
590         if (dataChannel_ != nullptr) {
591             int32_t ret = dataChannel_->DelFdListener(fd);
592             if (ret != ERR_OK) {
593                 SEN_HILOGE("Delete fd listener failed, fd:%{public}d, ret:%{public}d", fd, ret);
594             }
595         }
596     }
597     Close();
598 }
599 
600 int32_t SensorServiceClient::CreateSocketClientFd(int32_t &clientFd)
601 {
602 #ifdef HIVIEWDFX_HITRACE_ENABLE
603     StartTrace(HITRACE_TAG_SENSORS, "CreateSocketChannel");
604 #endif // HIVIEWDFX_HITRACE_ENABLE
605     CHKPR(sensorClientStub_, INVALID_POINTER);
606     auto remoteObject = sensorClientStub_->AsObject();
607     CHKPR(remoteObject, INVALID_POINTER);
608     int ret = sensorServer_->CreateSocketChannel(remoteObject, clientFd);
609     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_CREATE_SOCKET_CHANNEL, ret);
610 #ifdef HIVIEWDFX_HITRACE_ENABLE
611     FinishTrace(HITRACE_TAG_SENSORS);
612 #endif // HIVIEWDFX_HITRACE_ENABLE
613     return ret;
614 }
615 
616 int32_t SensorServiceClient::CreateSocketChannel()
617 {
618     CALL_LOG_ENTER;
619     int32_t ret = InitServiceClient();
620     if (ret != ERR_OK) {
621         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
622         return ret;
623     }
624     std::lock_guard<std::mutex> clientLock(clientMutex_);
625     CHKPR(sensorServer_, ERROR);
626     int32_t clientFd = -1;
627     ret = CreateSocketClientFd(clientFd);
628     if (ret != ERR_OK || clientFd < 0) {
629         Close();
630         SEN_HILOGE("Create socket channel failed, ret:%{public}d", ret);
631         return ret;
632     }
633 #ifdef OHOS_BUILD_ENABLE_RUST
634     StreamSocketSetFd(streamSocketPtr_.get(), clientFd);
635 #else
636     fd_ = clientFd;
637 #endif // OHOS_BUILD_ENABLE_RUST
638     {
639         std::lock_guard<std::mutex> channelLock(channelMutex_);
640         if (dataChannel_->AddFdListener(GetFd(),
641             [this] (const char *buf, size_t size) { this->ReceiveMessage(buf, size); },
642             [this] { this->Disconnect(); })!= ERR_OK) {
643             Close();
644             SEN_HILOGE("Add fd listener failed, fd:%{public}d", GetFd());
645             return ERROR;
646         }
647     }
648 #ifdef HIVIEWDFX_HITRACE_ENABLE
649     StartTrace(HITRACE_TAG_SENSORS, "EnableActiveInfoCB");
650 #endif // HIVIEWDFX_HITRACE_ENABLE
651     ret = sensorServer_->EnableActiveInfoCB();
652     WriteHiSysIPCEvent(ISensorServiceIpcCode::COMMAND_ENABLE_ACTIVE_INFO_C_B, ret);
653 #ifdef HIVIEWDFX_HITRACE_ENABLE
654     FinishTrace(HITRACE_TAG_SENSORS);
655 #endif // HIVIEWDFX_HITRACE_ENABLE
656     if (ret != ERR_OK) {
657         SEN_HILOGE("Enable active info callback failed, ret:%{public}d", ret);
658         Disconnect();
659         return ret;
660     }
661     isConnected_ = true;
662     return ERR_OK;
663 }
664 } // namespace Sensors
665 } // namespace OHOS
666