• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "dmd_report.h"
25 #include "ipc_skeleton.h"
26 #include "sensor_service_proxy.h"
27 #include "sensors_errors.h"
28 #include "sensors_log_domain.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace Sensors {
33 using namespace OHOS::HiviewDFX;
34 
35 namespace {
36 constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_NATIVE, "SensorServiceClient" };
37 constexpr int32_t GET_SERVICE_MAX_COUNT = 30;
38 constexpr uint32_t WAIT_MS = 200;
39 }  // namespace
40 
InitServiceClient()41 int32_t SensorServiceClient::InitServiceClient()
42 {
43     HiLog::Debug(LABEL, "%{public}s begin", __func__);
44     std::lock_guard<std::mutex> clientLock(clientMutex_);
45     if (sensorServer_ != nullptr) {
46         HiLog::Debug(LABEL, "%{public}s already init", __func__);
47         return ERR_OK;
48     }
49     if (sensorClientStub_ == nullptr) {
50         sensorClientStub_ = new (std::nothrow) SensorClientStub();
51     }
52     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (systemAbilityManager == nullptr) {
54         HiLog::Error(LABEL, "%{public}s systemAbilityManager cannot be null", __func__);
55         return SENSOR_NATIVE_SAM_ERR;
56     }
57     int32_t retry = 0;
58     while (retry < GET_SERVICE_MAX_COUNT) {
59         sensorServer_ = iface_cast<ISensorService>(systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID));
60         if (sensorServer_ != nullptr) {
61             HiLog::Debug(LABEL, "%{public}s get service success, retry : %{public}d", __func__, retry);
62             serviceDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<SensorServiceClient *>(this));
63             if (serviceDeathObserver_ != nullptr) {
64                 sensorServer_->AsObject()->AddDeathRecipient(serviceDeathObserver_);
65             }
66             sensorList_ = sensorServer_->GetSensorList();
67             return ERR_OK;
68         }
69         HiLog::Warn(LABEL, "%{public}s get service failed, retry : %{public}d", __func__, retry);
70         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
71         retry++;
72     }
73     DmdReport::ReportException(SENSOR_SERVICE_EXCEPTION, "InitServiceClient", SENSOR_NATIVE_GET_SERVICE_ERR);
74     HiLog::Error(LABEL, "%{public}s get service failed", __func__);
75     return SENSOR_NATIVE_GET_SERVICE_ERR;
76 }
77 
IsValidSensorId(uint32_t sensorId)78 bool SensorServiceClient::IsValidSensorId(uint32_t sensorId)
79 {
80     if (sensorList_.empty()) {
81         HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__);
82         return false;
83     }
84     for (auto &sensor : sensorList_) {
85         if (sensor.GetSensorId() == sensorId) {
86             return true;
87         }
88     }
89     return false;
90 }
91 
EnableSensor(uint32_t sensorId,int64_t samplingPeriod,int64_t maxReportDelay)92 int32_t SensorServiceClient::EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay)
93 {
94     HiLog::Debug(LABEL, "%{public}s begin", __func__);
95     if (!IsValidSensorId(sensorId)) {
96         HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__);
97         return SENSOR_NATIVE_SAM_ERR;
98     }
99     int32_t ret = InitServiceClient();
100     if (ret != ERR_OK) {
101         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
102         return ret;
103     }
104     ret = sensorServer_->EnableSensor(sensorId, samplingPeriod, maxReportDelay);
105     if (ret == ERR_OK) {
106         UpdateSensorInfoMap(sensorId, samplingPeriod, maxReportDelay);
107     }
108     return ret;
109 }
110 
DisableSensor(uint32_t sensorId)111 int32_t SensorServiceClient::DisableSensor(uint32_t sensorId)
112 {
113     HiLog::Debug(LABEL, "%{public}s begin", __func__);
114     if (!IsValidSensorId(sensorId)) {
115         HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__);
116         return SENSOR_NATIVE_SAM_ERR;
117     }
118     int32_t ret = InitServiceClient();
119     if (ret != ERR_OK) {
120         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
121         return ret;
122     }
123     ret = sensorServer_->DisableSensor(sensorId);
124     if (ret == ERR_OK) {
125         DeleteSensorInfoItem(sensorId);
126     }
127     return ret;
128 }
129 
RunCommand(uint32_t sensorId,int32_t cmdType,int32_t params)130 int32_t SensorServiceClient::RunCommand(uint32_t sensorId, int32_t cmdType, int32_t params)
131 {
132     HiLog::Debug(LABEL, "%{public}s begin", __func__);
133     if (!IsValidSensorId(sensorId)) {
134         HiLog::Error(LABEL, "%{public}s sensorId is invalid", __func__);
135         return SENSOR_NATIVE_SAM_ERR;
136     }
137     int32_t ret = InitServiceClient();
138     if (ret != ERR_OK) {
139         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
140         return ret;
141     }
142     ret = sensorServer_->RunCommand(sensorId, cmdType, params);
143     if (ret != ERR_OK) {
144         HiLog::Error(LABEL, "%{public}s RunCommand failed", __func__);
145         return ret;
146     }
147     return ret;
148 }
149 
GetSensorList()150 std::vector<Sensor> SensorServiceClient::GetSensorList()
151 {
152     HiLog::Debug(LABEL, "%{public}s begin", __func__);
153     int32_t ret = InitServiceClient();
154     if (ret != ERR_OK) {
155         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
156         return {};
157     }
158     if (sensorList_.empty()) {
159         HiLog::Error(LABEL, "%{public}s sensorList_ cannot be empty", __func__);
160     }
161     return sensorList_;
162 }
163 
TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)164 int32_t SensorServiceClient::TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel)
165 {
166     HiLog::Debug(LABEL, "%{public}s begin", __func__);
167     dataChannel_ = sensorDataChannel;
168     int32_t ret = InitServiceClient();
169     if (ret != ERR_OK) {
170         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
171         return ret;
172     }
173     return sensorServer_->TransferDataChannel(sensorDataChannel, sensorClientStub_);
174 }
175 
DestroyDataChannel()176 int32_t SensorServiceClient::DestroyDataChannel()
177 {
178     HiLog::Debug(LABEL, "%{public}s begin", __func__);
179     int32_t ret = InitServiceClient();
180     if (ret != ERR_OK) {
181         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
182         return ret;
183     }
184     return sensorServer_->DestroySensorChannel(sensorClientStub_);
185 }
186 
ProcessDeathObserver(const wptr<IRemoteObject> & object)187 void SensorServiceClient::ProcessDeathObserver(const wptr<IRemoteObject> &object)
188 {
189     HiLog::Debug(LABEL, "%{public}s begin", __func__);
190     (void)object;
191     if (dataChannel_ == nullptr) {
192         HiLog::Error(LABEL, "%{public}s dataChannel_ cannot be null", __func__);
193         return;
194     }
195     // STEP1 : Destroy revious data channel
196     dataChannel_->DestroySensorDataChannel();
197 
198     // STEP2 : Restore data channel
199     dataChannel_->RestoreSensorDataChannel();
200 
201     // STEP3 : Clear sensorlist and sensorServer_
202     sensorList_.clear();
203     sensorServer_ = nullptr;
204 
205     // STEP4 : ReGet sensors  3601 service
206     int32_t ret = InitServiceClient();
207     if (ret != ERR_OK) {
208         HiLog::Error(LABEL, "%{public}s InitServiceClient failed, ret : %{public}d", __func__, ret);
209         dataChannel_->DestroySensorDataChannel();
210         return;
211     }
212 
213     // STEP5 : Retransfer new channel to sensors
214     sensorServer_->TransferDataChannel(dataChannel_, sensorClientStub_);
215 
216     // STEP6 : Restore Sensor status
217     std::lock_guard<std::mutex> mapLock(mapMutex_);
218     for (const auto &it : sensorInfoMap_) {
219         sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs());
220     }
221 }
222 
UpdateSensorInfoMap(uint32_t sensorId,int64_t samplingPeriod,int64_t maxReportDelay)223 void SensorServiceClient::UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay)
224 {
225     HiLog::Debug(LABEL, "%{public}s begin", __func__);
226     std::lock_guard<std::mutex> mapLock(mapMutex_);
227     SensorBasicInfo sensorInfo;
228     sensorInfo.SetSamplingPeriodNs(samplingPeriod);
229     sensorInfo.SetMaxReportDelayNs(maxReportDelay);
230     sensorInfo.SetSensorState(SensorState::SENSOR_ENABLED);
231     sensorInfoMap_[sensorId] = sensorInfo;
232     return;
233 }
234 
DeleteSensorInfoItem(uint32_t sensorId)235 void SensorServiceClient::DeleteSensorInfoItem(uint32_t sensorId)
236 {
237     HiLog::Debug(LABEL, "%{public}s begin", __func__);
238     std::lock_guard<std::mutex> mapLock(mapMutex_);
239     auto it = sensorInfoMap_.find(sensorId);
240     if (it != sensorInfoMap_.end()) {
241         sensorInfoMap_.erase(it);
242     }
243     return;
244 }
245 }  // namespace Sensors
246 }  // namespace OHOS
247