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_proxy.h"
17 
18 #include <vector>
19 
20 #include "hisysevent.h"
21 #include "message_parcel.h"
22 #include "sensor_client_proxy.h"
23 #include "sensor_errors.h"
24 #include "sensor_parcel.h"
25 
26 #undef LOG_TAG
27 #define LOG_TAG "SensorServiceProxy"
28 
29 namespace OHOS {
30 namespace Sensors {
31 using namespace OHOS::HiviewDFX;
32 
SensorServiceProxy(const sptr<IRemoteObject> & impl)33 SensorServiceProxy::SensorServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<ISensorService>(impl)
34 {}
35 
EnableSensor(int32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)36 ErrCode SensorServiceProxy::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
37 {
38     MessageParcel data;
39     MessageParcel reply;
40     MessageOption option;
41     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
42         SEN_HILOGE("Write descriptor failed");
43         return WRITE_PARCEL_ERR;
44     }
45     WRITEINT32(data, sensorId, WRITE_PARCEL_ERR);
46     WRITEINT64(data, samplingPeriodNs, WRITE_PARCEL_ERR);
47     WRITEINT64(data, maxReportDelayNs, WRITE_PARCEL_ERR);
48     sptr<IRemoteObject> remote = Remote();
49     CHKPR(remote, ERROR);
50     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::ENABLE_SENSOR),
51         data, reply, option);
52     if (ret != NO_ERROR) {
53         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
54             HiSysEvent::EventType::FAULT, "PKG_NAME", "EnableSensor", "ERROR_CODE", ret);
55         SEN_HILOGE("Failed, ret:%{public}d", ret);
56     }
57     return static_cast<ErrCode>(ret);
58 }
59 
DisableSensor(int32_t sensorId)60 ErrCode SensorServiceProxy::DisableSensor(int32_t sensorId)
61 {
62     MessageParcel data;
63     MessageParcel reply;
64     MessageOption option;
65     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
66         SEN_HILOGE("Write descriptor failed");
67         return WRITE_PARCEL_ERR;
68     }
69     WRITEINT32(data, sensorId, WRITE_PARCEL_ERR);
70     sptr<IRemoteObject> remote = Remote();
71     CHKPR(remote, ERROR);
72     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::DISABLE_SENSOR),
73         data, reply, option);
74     if (ret != NO_ERROR) {
75         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
76             HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableSensor", "ERROR_CODE", ret);
77         SEN_HILOGE("Failed, ret:%{public}d", ret);
78     }
79     return static_cast<ErrCode>(ret);
80 }
81 
GetSensorList()82 std::vector<Sensor> SensorServiceProxy::GetSensorList()
83 {
84     MessageParcel data;
85     MessageParcel reply;
86     MessageOption option;
87     std::vector<Sensor> sensors;
88     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
89         SEN_HILOGE("Write descriptor failed");
90         return sensors;
91     }
92     sptr<IRemoteObject> remote = Remote();
93     if (remote == nullptr) {
94         SEN_HILOGE("remote is null");
95         return sensors;
96     }
97     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::GET_SENSOR_LIST),
98         data, reply, option);
99     if (ret != NO_ERROR) {
100         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
101             HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorList", "ERROR_CODE", ret);
102         SEN_HILOGE("Failed, ret:%{public}d", ret);
103         return sensors;
104     }
105     uint32_t sensorCount;
106     if (!reply.ReadUint32(sensorCount)) {
107         SEN_HILOGE("Parcel read failed");
108         return sensors;
109     }
110     SEN_HILOGD("SensorCount:%{public}u", sensorCount);
111     if (sensorCount > MAX_SENSOR_COUNT) {
112         sensorCount = MAX_SENSOR_COUNT;
113     }
114     Sensor sensor;
115     for (uint32_t i = 0; i < sensorCount; ++i) {
116         auto tmpSensor = sensor.Unmarshalling(reply);
117         CHKPC(tmpSensor);
118         sensors.push_back(*tmpSensor);
119     }
120     return sensors;
121 }
122 
TransferDataChannel(const sptr<SensorBasicDataChannel> & sensorBasicDataChannel,const sptr<IRemoteObject> & sensorClient)123 ErrCode SensorServiceProxy::TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel,
124                                                 const sptr<IRemoteObject> &sensorClient)
125 {
126     CHKPR(sensorBasicDataChannel, OBJECT_NULL);
127     CHKPR(sensorClient, OBJECT_NULL);
128     MessageParcel data;
129     MessageParcel reply;
130     MessageOption option;
131     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
132         SEN_HILOGE("Write descriptor failed");
133         return WRITE_PARCEL_ERR;
134     }
135     sensorBasicDataChannel->SendToBinder(data);
136     WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR);
137     sptr<IRemoteObject> remote = Remote();
138     CHKPR(remote, ERROR);
139     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::TRANSFER_DATA_CHANNEL),
140         data, reply, option);
141     if (ret != NO_ERROR) {
142         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
143             HiSysEvent::EventType::FAULT, "PKG_NAME", "TransferDataChannel", "ERROR_CODE", ret);
144         SEN_HILOGE("Failed, ret:%{public}d", ret);
145     }
146     sensorBasicDataChannel->CloseSendFd();
147     return static_cast<ErrCode>(ret);
148 }
149 
DestroySensorChannel(sptr<IRemoteObject> sensorClient)150 ErrCode SensorServiceProxy::DestroySensorChannel(sptr<IRemoteObject> sensorClient)
151 {
152     CHKPR(sensorClient, OBJECT_NULL);
153     MessageParcel data;
154     MessageParcel reply;
155     MessageOption option;
156     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
157         SEN_HILOGE("Write descriptor failed");
158         return WRITE_PARCEL_ERR;
159     }
160     WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR);
161     sptr<IRemoteObject> remote = Remote();
162     CHKPR(remote, ERROR);
163     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::DESTROY_SENSOR_CHANNEL),
164         data, reply, option);
165     if (ret != NO_ERROR) {
166         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
167             HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroySensorChannel", "ERROR_CODE", ret);
168         SEN_HILOGE("Failed, ret:%{public}d", ret);
169     }
170     return static_cast<ErrCode>(ret);
171 }
172 
SuspendSensors(int32_t pid)173 ErrCode SensorServiceProxy::SuspendSensors(int32_t pid)
174 {
175     MessageParcel data;
176     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
177         SEN_HILOGE("Parcel write descriptor failed");
178         return WRITE_PARCEL_ERR;
179     }
180     WRITEINT32(data, pid, WRITE_PARCEL_ERR);
181     sptr<IRemoteObject> remote = Remote();
182     CHKPR(remote, ERROR);
183     MessageParcel reply;
184     MessageOption option;
185     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::SUSPEND_SENSORS),
186         data, reply, option);
187     if (ret != NO_ERROR) {
188         SEN_HILOGD("Failed, ret:%{public}d", ret);
189     }
190     return static_cast<ErrCode>(ret);
191 }
192 
ResumeSensors(int32_t pid)193 ErrCode SensorServiceProxy::ResumeSensors(int32_t pid)
194 {
195     MessageParcel data;
196     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
197         SEN_HILOGE("Parcel write descriptor failed");
198         return WRITE_PARCEL_ERR;
199     }
200     WRITEINT32(data, pid, WRITE_PARCEL_ERR);
201     sptr<IRemoteObject> remote = Remote();
202     CHKPR(remote, ERROR);
203     MessageParcel reply;
204     MessageOption option;
205     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::RESUME_SENSORS),
206         data, reply, option);
207     if (ret != NO_ERROR) {
208         SEN_HILOGD("Failed, ret:%{public}d", ret);
209     }
210     return static_cast<ErrCode>(ret);
211 }
212 
GetActiveInfoList(int32_t pid,std::vector<ActiveInfo> & activeInfoList)213 ErrCode SensorServiceProxy::GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList)
214 {
215     MessageParcel data;
216     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
217         SEN_HILOGE("Parcel write descriptor failed");
218         return WRITE_PARCEL_ERR;
219     }
220     WRITEINT32(data, pid, WRITE_PARCEL_ERR);
221     sptr<IRemoteObject> remote = Remote();
222     CHKPR(remote, ERROR);
223     MessageParcel reply;
224     MessageOption option;
225     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::GET_ACTIVE_INFO_LIST),
226         data, reply, option);
227     if (ret != NO_ERROR) {
228         SEN_HILOGE("Failed, ret:%{public}d", ret);
229         return static_cast<ErrCode>(ret);
230     }
231     uint32_t activeInfoCount;
232     READUINT32(reply, activeInfoCount, READ_PARCEL_ERR);
233     SEN_HILOGD("activeInfoCount:%{public}u", activeInfoCount);
234     if (activeInfoCount > MAX_SENSOR_COUNT) {
235         activeInfoCount = MAX_SENSOR_COUNT;
236     }
237     ActiveInfo activeInfo;
238     for (uint32_t i = 0; i < activeInfoCount; ++i) {
239         auto tmpActiveInfo = activeInfo.Unmarshalling(reply);
240         CHKPC(tmpActiveInfo);
241         activeInfoList.push_back(*tmpActiveInfo);
242     }
243     return static_cast<ErrCode>(ret);
244 }
245 
CreateSocketChannel(sptr<IRemoteObject> sensorClient,int32_t & clientFd)246 ErrCode SensorServiceProxy::CreateSocketChannel(sptr<IRemoteObject> sensorClient, int32_t &clientFd)
247 {
248     CHKPR(sensorClient, INVALID_POINTER);
249     MessageParcel data;
250     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
251         SEN_HILOGE("Parcel write descriptor failed");
252         return WRITE_PARCEL_ERR;
253     }
254     WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR);
255     MessageParcel reply;
256     MessageOption option;
257     sptr<IRemoteObject> remote = Remote();
258     CHKPR(remote, ERROR);
259     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::CREATE_SOCKET_CHANNEL),
260         data, reply, option);
261     if (ret != NO_ERROR) {
262         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
263             HiSysEvent::EventType::FAULT, "PKG_NAME", "CreateSocketChannel", "ERROR_CODE", ret);
264         SEN_HILOGE("Failed, ret:%{public}d", ret);
265         return ERROR;
266     }
267     clientFd = reply.ReadFileDescriptor();
268     if (clientFd < 0) {
269         SEN_HILOGE("Invalid fd, clientFd:%{public}d", clientFd);
270         return ERROR;
271     }
272     return static_cast<ErrCode>(ret);
273 }
274 
DestroySocketChannel(sptr<IRemoteObject> sensorClient)275 ErrCode SensorServiceProxy::DestroySocketChannel(sptr<IRemoteObject> sensorClient)
276 {
277     CHKPR(sensorClient, INVALID_POINTER);
278     MessageParcel data;
279     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
280         SEN_HILOGE("Parcel write descriptor failed");
281         return WRITE_PARCEL_ERR;
282     }
283     WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR);
284     sptr<IRemoteObject> remote = Remote();
285     CHKPR(remote, ERROR);
286     MessageParcel reply;
287     MessageOption option;
288     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::DESTROY_SOCKET_CHANNEL),
289         data, reply, option);
290     if (ret != NO_ERROR) {
291         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
292             HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroySocketChannel", "ERROR_CODE", ret);
293         SEN_HILOGE("Failed, ret:%{public}d", ret);
294     }
295     return static_cast<ErrCode>(ret);
296 }
297 
EnableActiveInfoCB()298 ErrCode SensorServiceProxy::EnableActiveInfoCB()
299 {
300     MessageParcel data;
301     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
302         SEN_HILOGE("Parcel write descriptor failed");
303         return WRITE_PARCEL_ERR;
304     }
305     sptr<IRemoteObject> remote = Remote();
306     CHKPR(remote, ERROR);
307     MessageParcel reply;
308     MessageOption option;
309     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::ENABLE_ACTIVE_INFO_CB),
310         data, reply, option);
311     if (ret != NO_ERROR) {
312         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
313             HiSysEvent::EventType::FAULT, "PKG_NAME", "EnableActiveInfoCB", "ERROR_CODE", ret);
314         SEN_HILOGE("Failed, ret:%{public}d", ret);
315     }
316     return static_cast<ErrCode>(ret);
317 }
318 
DisableActiveInfoCB()319 ErrCode SensorServiceProxy::DisableActiveInfoCB()
320 {
321     MessageParcel data;
322     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
323         SEN_HILOGE("Parcel write descriptor failed");
324         return WRITE_PARCEL_ERR;
325     }
326     sptr<IRemoteObject> remote = Remote();
327     CHKPR(remote, ERROR);
328     MessageParcel reply;
329     MessageOption option;
330     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::DISABLE_ACTIVE_INFO_CB),
331         data, reply, option);
332     if (ret != NO_ERROR) {
333         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
334             HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableActiveInfoCB", "ERROR_CODE", ret);
335         SEN_HILOGE("Failed, ret:%{public}d", ret);
336     }
337     return static_cast<ErrCode>(ret);
338 }
339 
ResetSensors()340 ErrCode SensorServiceProxy::ResetSensors()
341 {
342     MessageParcel data;
343     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
344         SEN_HILOGE("Parcel write descriptor failed");
345         return WRITE_PARCEL_ERR;
346     }
347     sptr<IRemoteObject> remote = Remote();
348     CHKPR(remote, ERROR);
349     MessageParcel reply;
350     MessageOption option;
351     int32_t ret = remote->SendRequest(static_cast<uint32_t>(SensorInterfaceCode::RESET_SENSORS),
352         data, reply, option);
353     if (ret != NO_ERROR) {
354         HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SERVICE_IPC_EXCEPTION",
355             HiSysEvent::EventType::FAULT, "PKG_NAME", "ResetSensors", "ERROR_CODE", ret);
356         SEN_HILOGE("Failed, ret:%{public}d", ret);
357     }
358     return static_cast<ErrCode>(ret);
359 }
360 }  // namespace Sensors
361 }  // namespace OHOS
362