• 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 <sys/socket.h>
19 #include <thread>
20 #include <unistd.h>
21 #include <vector>
22 
23 #include "death_recipient_template.h"
24 #include "hisysevent.h"
25 #include "hitrace_meter.h"
26 #include "ipc_skeleton.h"
27 #include "sensor_agent_proxy.h"
28 #include "sensor_errors.h"
29 #include "sensor_service_proxy.h"
30 #include "system_ability_definition.h"
31 #include "rust_binding.h"
32 
33 #undef LOG_TAG
34 #define LOG_TAG "SensorServiceClient"
35 
36 namespace OHOS {
37 namespace Sensors {
38 using namespace OHOS::HiviewDFX;
39 
40 namespace {
41 #ifdef OHOS_BUILD_ENABLE_RUST
42 extern "C" {
43     void ReadClientPackets(RustStreamBuffer *, OHOS::Sensors::SensorServiceClient *,
44         void(*)(OHOS::Sensors::SensorServiceClient *, RustNetPacket *));
OnPacket(SensorServiceClient * object,RustNetPacket * cPkt)45     void OnPacket(SensorServiceClient *object, RustNetPacket *cPkt)
46     {
47         NetPacket pkt(cPkt->msgId);
48         pkt.streamBufferPtr_.reset(cPkt->streamBuffer);
49         object->HandleNetPacke(pkt);
50     }
51 }
52 #endif // OHOS_BUILD_ENABLE_RUST
53 }  // namespace
54 
~SensorServiceClient()55 SensorServiceClient::~SensorServiceClient()
56 {
57     if (sensorServer_ != nullptr && serviceDeathObserver_ != nullptr) {
58         auto remoteObject = sensorServer_->AsObject();
59         if (remoteObject != nullptr) {
60             remoteObject->RemoveDeathRecipient(serviceDeathObserver_);
61         }
62     }
63     Disconnect();
64 }
65 
InitServiceClient()66 int32_t SensorServiceClient::InitServiceClient()
67 {
68     CALL_LOG_ENTER;
69     std::lock_guard<std::mutex> clientLock(clientMutex_);
70     if (sensorServer_ != nullptr) {
71         SEN_HILOGD("Already init");
72         if (sensorList_.empty()) {
73             sensorList_ = sensorServer_->GetSensorList();
74             SEN_HILOGW("sensorList is %{public}s", sensorList_.empty() ? "empty" : "not empty");
75         }
76         return ERR_OK;
77     }
78     if (sensorClientStub_ == nullptr) {
79         sensorClientStub_ = new (std::nothrow) SensorClientStub();
80     }
81     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82     CHKPR(systemAbilityManager, SENSOR_NATIVE_SAM_ERR);
83     auto object = systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID);
84     CHKPR(object, SENSOR_NATIVE_GET_SERVICE_ERR);
85     sensorServer_ = iface_cast<ISensorService>(object);
86     if (sensorServer_ != nullptr) {
87         SEN_HILOGD("Get service success");
88         serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorServiceClient *>(this));
89         CHKPR(serviceDeathObserver_, SENSOR_NATIVE_GET_SERVICE_ERR);
90         auto remoteObject = sensorServer_->AsObject();
91         CHKPR(remoteObject, SENSOR_NATIVE_GET_SERVICE_ERR);
92         remoteObject->AddDeathRecipient(serviceDeathObserver_);
93         sensorList_ = sensorServer_->GetSensorList();
94         if (sensorList_.empty()) {
95             SEN_HILOGW("sensorList_ is empty when connecting to the service for the first time");
96         }
97         return ERR_OK;
98     }
99     SEN_HILOGW("Get service failed");
100     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "SERVICE_EXCEPTION",
101         HiSysEvent::EventType::FAULT, "PKG_NAME", "InitServiceClient", "ERROR_CODE", SENSOR_NATIVE_GET_SERVICE_ERR);
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     StartTrace(HITRACE_TAG_SENSORS, "EnableSensor");
136     ret = sensorServer_->EnableSensor(sensorId, samplingPeriod, maxReportDelay);
137     FinishTrace(HITRACE_TAG_SENSORS);
138     if (ret == ERR_OK) {
139         UpdateSensorInfoMap(sensorId, samplingPeriod, maxReportDelay);
140     }
141     return ret;
142 }
143 
DisableSensor(int32_t sensorId)144 int32_t SensorServiceClient::DisableSensor(int32_t sensorId)
145 {
146     CALL_LOG_ENTER;
147     int32_t ret = InitServiceClient();
148     if (ret != ERR_OK) {
149         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
150         return ret;
151     }
152     std::lock_guard<std::mutex> clientLock(clientMutex_);
153     CHKPR(sensorServer_, ERROR);
154     StartTrace(HITRACE_TAG_SENSORS, "DisableSensor");
155     ret = sensorServer_->DisableSensor(sensorId);
156     FinishTrace(HITRACE_TAG_SENSORS);
157     if (ret == ERR_OK) {
158         DeleteSensorInfoItem(sensorId);
159     }
160     return ret;
161 }
162 
GetSensorList()163 std::vector<Sensor> SensorServiceClient::GetSensorList()
164 {
165     CALL_LOG_ENTER;
166     int32_t ret = InitServiceClient();
167     if (ret != ERR_OK) {
168         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
169         return {};
170     }
171     std::lock_guard<std::mutex> clientLock(clientMutex_);
172     if (sensorList_.empty()) {
173         SEN_HILOGE("sensorList_ cannot be empty");
174     }
175     return sensorList_;
176 }
177 
TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)178 int32_t SensorServiceClient::TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)
179 {
180     SEN_HILOGI("In");
181     CHKPR(sensorDataChannel, INVALID_POINTER);
182     {
183         std::lock_guard<std::mutex> channelLock(channelMutex_);
184         dataChannel_ = sensorDataChannel;
185     }
186     int32_t ret = InitServiceClient();
187     if (ret != ERR_OK) {
188         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
189         return ret;
190     }
191     std::lock_guard<std::mutex> clientLock(clientMutex_);
192     CHKPR(sensorServer_, ERROR);
193     StartTrace(HITRACE_TAG_SENSORS, "TransferDataChannel");
194     CHKPR(sensorClientStub_, INVALID_POINTER);
195     auto remoteObject = sensorClientStub_->AsObject();
196     CHKPR(remoteObject, INVALID_POINTER);
197     ret = sensorServer_->TransferDataChannel(sensorDataChannel, remoteObject);
198     FinishTrace(HITRACE_TAG_SENSORS);
199     SEN_HILOGI("Done");
200     return ret;
201 }
202 
DestroyDataChannel()203 int32_t SensorServiceClient::DestroyDataChannel()
204 {
205     CALL_LOG_ENTER;
206     int32_t ret = InitServiceClient();
207     if (ret != ERR_OK) {
208         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
209         return ret;
210     }
211     std::lock_guard<std::mutex> clientLock(clientMutex_);
212     CHKPR(sensorServer_, ERROR);
213     StartTrace(HITRACE_TAG_SENSORS, "DestroyDataChannel");
214     CHKPR(sensorClientStub_, INVALID_POINTER);
215     auto remoteObject = sensorClientStub_->AsObject();
216     CHKPR(remoteObject, INVALID_POINTER);
217     ret = sensorServer_->DestroySensorChannel(remoteObject);
218     FinishTrace(HITRACE_TAG_SENSORS);
219     return ret;
220 }
221 
ReenableSensor()222 void SensorServiceClient::ReenableSensor()
223 {
224     CALL_LOG_ENTER;
225     {
226         std::lock_guard<std::mutex> clientLock(clientMutex_);
227         std::lock_guard<std::mutex> mapLock(mapMutex_);
228         for (const auto &it : sensorInfoMap_) {
229             if (sensorServer_ != nullptr) {
230                 sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs());
231             }
232         }
233     }
234     if (!isConnected_) {
235         SEN_HILOGD("Previous socket channel status is false, not need retry creat socket channel");
236         return;
237     }
238     Disconnect();
239     CreateSocketChannel();
240 }
241 
ProcessDeathObserver(const wptr<IRemoteObject> & object)242 void SensorServiceClient::ProcessDeathObserver(const wptr<IRemoteObject> &object)
243 {
244     CALL_LOG_ENTER;
245     {
246         std::lock_guard<std::mutex> channelLock(channelMutex_);
247         if (dataChannel_ == nullptr) {
248             SEN_HILOGI("dataChannel_ is nullptr");
249             {
250                 std::lock_guard<std::mutex> clientLock(clientMutex_);
251                 sensorServer_ = nullptr;
252             }
253             if (InitServiceClient() != ERR_OK) {
254                 SEN_HILOGE("InitServiceClient failed");
255                 return;
256             }
257         } else {
258             SEN_HILOGI("dataChannel_ is not nullptr");
259             dataChannel_->DestroySensorDataChannel();
260             int32_t ret = dataChannel_->RestoreSensorDataChannel();
261             if (ret == ERR_OK) {
262                 SENSOR_AGENT_IMPL->SetIsChannelCreated(true);
263             }
264             {
265                 std::lock_guard<std::mutex> clientLock(clientMutex_);
266                 sensorServer_ = nullptr;
267             }
268             if (InitServiceClient() != ERR_OK) {
269                 SEN_HILOGE("InitServiceClient failed");
270                 dataChannel_->DestroySensorDataChannel();
271                 SENSOR_AGENT_IMPL->SetIsChannelCreated(false);
272                 return;
273             }
274             std::lock_guard<std::mutex> clientLock(clientMutex_);
275             if (sensorServer_ != nullptr && sensorClientStub_ != nullptr) {
276                 auto remoteObject = sensorClientStub_->AsObject();
277                 if (remoteObject != nullptr) {
278                     sensorServer_->TransferDataChannel(dataChannel_, remoteObject);
279                 }
280             }
281         }
282     }
283     ReenableSensor();
284 }
285 
UpdateSensorInfoMap(int32_t sensorId,int64_t samplingPeriod,int64_t maxReportDelay)286 void SensorServiceClient::UpdateSensorInfoMap(int32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay)
287 {
288     SEN_HILOGI("In");
289     SensorBasicInfo sensorInfo;
290     sensorInfo.SetSamplingPeriodNs(samplingPeriod);
291     sensorInfo.SetMaxReportDelayNs(maxReportDelay);
292     sensorInfo.SetSensorState(true);
293     std::lock_guard<std::mutex> mapLock(mapMutex_);
294     sensorInfoMap_[sensorId] = sensorInfo;
295     SEN_HILOGI("Done");
296     return;
297 }
298 
DeleteSensorInfoItem(int32_t sensorId)299 void SensorServiceClient::DeleteSensorInfoItem(int32_t sensorId)
300 {
301     SEN_HILOGI("In");
302     std::lock_guard<std::mutex> mapLock(mapMutex_);
303     auto it = sensorInfoMap_.find(sensorId);
304     if (it != sensorInfoMap_.end()) {
305         sensorInfoMap_.erase(it);
306     }
307     SEN_HILOGI("Done");
308     return;
309 }
310 
SuspendSensors(int32_t pid)311 int32_t SensorServiceClient::SuspendSensors(int32_t pid)
312 {
313     CALL_LOG_ENTER;
314     int32_t ret = InitServiceClient();
315     if (ret != ERR_OK) {
316         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
317         return ret;
318     }
319     std::lock_guard<std::mutex> clientLock(clientMutex_);
320     CHKPR(sensorServer_, ERROR);
321     StartTrace(HITRACE_TAG_SENSORS, "SuspendSensors");
322     ret = sensorServer_->SuspendSensors(pid);
323     FinishTrace(HITRACE_TAG_SENSORS);
324     return ret;
325 }
326 
ResumeSensors(int32_t pid)327 int32_t SensorServiceClient::ResumeSensors(int32_t pid)
328 {
329     CALL_LOG_ENTER;
330     int32_t ret = InitServiceClient();
331     if (ret != ERR_OK) {
332         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
333         return ret;
334     }
335     std::lock_guard<std::mutex> clientLock(clientMutex_);
336     CHKPR(sensorServer_, ERROR);
337     StartTrace(HITRACE_TAG_SENSORS, "ResumeSensors");
338     ret = sensorServer_->ResumeSensors(pid);
339     FinishTrace(HITRACE_TAG_SENSORS);
340     return ret;
341 }
342 
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)343 int32_t SensorServiceClient::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
344 {
345     CALL_LOG_ENTER;
346     int32_t ret = InitServiceClient();
347     if (ret != ERR_OK) {
348         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
349         return ret;
350     }
351     std::lock_guard<std::mutex> clientLock(clientMutex_);
352     CHKPR(sensorServer_, ERROR);
353     StartTrace(HITRACE_TAG_SENSORS, "GetActiveInfoList");
354     ret = sensorServer_->GetActiveInfoList(pid, activeInfoList);
355     FinishTrace(HITRACE_TAG_SENSORS);
356     return ret;
357 }
358 
Register(SensorActiveInfoCB callback,sptr<SensorDataChannel> sensorDataChannel)359 int32_t SensorServiceClient::Register(SensorActiveInfoCB callback, sptr<SensorDataChannel> sensorDataChannel)
360 {
361     CALL_LOG_ENTER;
362     if (!isConnected_) {
363         CHKPR(sensorDataChannel, INVALID_POINTER);
364         {
365             std::lock_guard<std::mutex> channelLock(channelMutex_);
366             dataChannel_ = sensorDataChannel;
367         }
368         int32_t ret = CreateSocketChannel();
369         if (ret != ERR_OK) {
370             SEN_HILOGE("Register sensor active info callback failed, ret:%{public}d", ret);
371             return ret;
372         }
373     }
374     std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
375     activeInfoCBSet_.insert(callback);
376     return ERR_OK;
377 }
378 
Unregister(SensorActiveInfoCB callback)379 int32_t SensorServiceClient::Unregister(SensorActiveInfoCB callback)
380 {
381     CALL_LOG_ENTER;
382     {
383         std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
384         activeInfoCBSet_.erase(callback);
385         if (!activeInfoCBSet_.empty()) {
386             return ERR_OK;
387         }
388     }
389     int32_t ret = InitServiceClient();
390     if (ret != ERR_OK) {
391         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
392         return ret;
393     }
394     std::lock_guard<std::mutex> clientLock(clientMutex_);
395     CHKPR(sensorServer_, ERROR);
396     StartTrace(HITRACE_TAG_SENSORS, "DisableActiveInfoCB");
397     ret = sensorServer_->DisableActiveInfoCB();
398     FinishTrace(HITRACE_TAG_SENSORS);
399     if (ret != ERR_OK) {
400         SEN_HILOGE("Disable active info callback failed, ret:%{public}d", ret);
401         return ret;
402     }
403     Disconnect();
404     StartTrace(HITRACE_TAG_SENSORS, "DestroySocketChannel");
405     CHKPR(sensorClientStub_, INVALID_POINTER);
406     auto remoteObject = sensorClientStub_->AsObject();
407     CHKPR(remoteObject, INVALID_POINTER);
408     ret = sensorServer_->DestroySocketChannel(remoteObject);
409     FinishTrace(HITRACE_TAG_SENSORS);
410     if (ret != ERR_OK) {
411         SEN_HILOGE("Destroy socket channel failed, ret:%{public}d", ret);
412         return ret;
413     }
414     isConnected_ = false;
415     return ERR_OK;
416 }
417 
ResetSensors()418 int32_t SensorServiceClient::ResetSensors()
419 {
420     CALL_LOG_ENTER;
421     int32_t ret = InitServiceClient();
422     if (ret != ERR_OK) {
423         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
424         return ret;
425     }
426     std::lock_guard<std::mutex> clientLock(clientMutex_);
427     CHKPR(sensorServer_, ERROR);
428     StartTrace(HITRACE_TAG_SENSORS, "ResetSensors");
429     ret = sensorServer_->ResetSensors();
430     FinishTrace(HITRACE_TAG_SENSORS);
431     return ret;
432 }
433 
ReceiveMessage(const char * buf,size_t size)434 void SensorServiceClient::ReceiveMessage(const char *buf, size_t size)
435 {
436     CHKPV(buf);
437     if (size == 0 || size > MAX_PACKET_BUF_SIZE) {
438         SEN_HILOGE("Invalid input param size. size:%{public}zu", size);
439         return;
440     }
441     if (!circBuf_.Write(buf, size)) {
442         SEN_HILOGE("Write data failed. size:%{public}zu", size);
443     }
444 #ifdef OHOS_BUILD_ENABLE_RUST
445     ReadClientPackets(circBuf_.streamBufferPtr_.get(), this, OnPacket);
446 #else
447     OnReadPackets(circBuf_, [this] (NetPacket &pkt) { this->HandleNetPacke(pkt); });
448 #endif // OHOS_BUILD_ENABLE_RUST
449 }
450 
HandleNetPacke(NetPacket & pkt)451 void SensorServiceClient::HandleNetPacke(NetPacket &pkt)
452 {
453     auto id = pkt.GetMsgId();
454     if (id != MessageId::ACTIVE_INFO) {
455         SEN_HILOGE("NetPacke message id is not ACTIVE_INFO");
456         return;
457     }
458     SensorActiveInfo sensorActiveInfo;
459     pkt >> sensorActiveInfo.pid >> sensorActiveInfo.sensorId >> sensorActiveInfo.samplingPeriodNs >>
460         sensorActiveInfo.maxReportDelayNs;
461 #ifdef OHOS_BUILD_ENABLE_RUST
462     if (StreamBufferChkRWError(pkt.streamBufferPtr_.get())) {
463 #else
464     if (pkt.ChkRWError()) {
465 #endif // OHOS_BUILD_ENABLE_RUST
466         SEN_HILOGE("Packet read type failed");
467         return;
468     }
469     std::lock_guard<std::mutex> activeInfoCBLock(activeInfoCBMutex_);
470     for (auto callback : activeInfoCBSet_) {
471         if (callback != nullptr) {
472             callback(sensorActiveInfo);
473         }
474     }
475 }
476 
477 void SensorServiceClient::Disconnect()
478 {
479     CALL_LOG_ENTER;
480     int32_t fd = GetFd();
481     if (fd < 0) {
482         return;
483     }
484     {
485         std::lock_guard<std::mutex> channelLock(channelMutex_);
486         if (dataChannel_ != nullptr) {
487             int32_t ret = dataChannel_->DelFdListener(fd);
488             if (ret != ERR_OK) {
489                 SEN_HILOGE("Delete fd listener failed, fd:%{public}d, ret:%{public}d", fd, ret);
490             }
491         }
492     }
493     Close();
494 }
495 
496 int32_t SensorServiceClient::CreateSocketChannel()
497 {
498     CALL_LOG_ENTER;
499     int32_t ret = InitServiceClient();
500     if (ret != ERR_OK) {
501         SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret);
502         return ret;
503     }
504     std::lock_guard<std::mutex> clientLock(clientMutex_);
505     CHKPR(sensorServer_, ERROR);
506     int32_t clientFd = -1;
507     StartTrace(HITRACE_TAG_SENSORS, "CreateSocketChannel");
508     CHKPR(sensorClientStub_, INVALID_POINTER);
509     auto remoteObject = sensorClientStub_->AsObject();
510     CHKPR(remoteObject, INVALID_POINTER);
511     ret = sensorServer_->CreateSocketChannel(remoteObject, clientFd);
512     FinishTrace(HITRACE_TAG_SENSORS);
513     if (ret != ERR_OK || clientFd < 0) {
514         Close();
515         SEN_HILOGE("Create socket channel failed, ret:%{public}d", ret);
516         return ret;
517     }
518 #ifdef OHOS_BUILD_ENABLE_RUST
519     StreamSocketSetFd(streamSocketPtr_.get(), clientFd);
520 #else
521     fd_ = clientFd;
522 #endif // OHOS_BUILD_ENABLE_RUST
523     {
524         std::lock_guard<std::mutex> channelLock(channelMutex_);
525         if (dataChannel_->AddFdListener(GetFd(),
526             [this] (const char *buf, size_t size) { this->ReceiveMessage(buf, size); },
527             [this] { this->Disconnect(); })!= ERR_OK) {
528             Close();
529             SEN_HILOGE("Add fd listener failed, fd:%{public}d", GetFd());
530             return ERROR;
531         }
532     }
533     StartTrace(HITRACE_TAG_SENSORS, "EnableActiveInfoCB");
534     ret = sensorServer_->EnableActiveInfoCB();
535     FinishTrace(HITRACE_TAG_SENSORS);
536     if (ret != ERR_OK) {
537         SEN_HILOGE("Enable active info callback failed, ret:%{public}d", ret);
538         Disconnect();
539         return ret;
540     }
541     isConnected_ = true;
542     return ERR_OK;
543 }
544 }  // namespace Sensors
545 }  // namespace OHOS
546