• 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_proxy.h"
17 
18 #include <vector>
19 
20 #include "hisysevent.h"
21 #include "message_parcel.h"
22 #include "sensor_client_proxy.h"
23 #include "sensors_errors.h"
24 
25 namespace OHOS {
26 namespace Sensors {
27 using namespace OHOS::HiviewDFX;
28 
29 namespace {
30 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceProxy" };
31 constexpr int32_t MAX_SENSOR_COUNT = 200;
32 enum {
33     FLUSH = 0,
34     SET_MODE,
35     RESERVED,
36 };
37 }  // namespace
38 
SensorServiceProxy(const sptr<IRemoteObject> & impl)39 SensorServiceProxy::SensorServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<ISensorService>(impl)
40 {}
41 
EnableSensor(uint32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)42 ErrCode SensorServiceProxy::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
43 {
44     MessageParcel data;
45     MessageParcel reply;
46     MessageOption option;
47     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
48         SEN_HILOGE("write descriptor failed");
49         return WRITE_MSG_ERR;
50     }
51     if (!data.WriteUint32(sensorId)) {
52         SEN_HILOGE("write sensorId failed");
53         return WRITE_MSG_ERR;
54     }
55     if (!data.WriteInt64(samplingPeriodNs)) {
56         SEN_HILOGE("write samplingPeriodNs failed");
57         return WRITE_MSG_ERR;
58     }
59     if (!data.WriteInt64(maxReportDelayNs)) {
60         SEN_HILOGE("write maxReportDelayNs failed");
61         return WRITE_MSG_ERR;
62     }
63     sptr<IRemoteObject> remote = Remote();
64     CHKPR(remote, ERROR);
65     int32_t ret = remote->SendRequest(ISensorService::ENABLE_SENSOR, data, reply, option);
66     if (ret != NO_ERROR) {
67         HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION",
68             HiSysEvent::EventType::FAULT, "PKG_NAME", "EnaleSensor", "ERROR_CODE", ret);
69         SEN_HILOGE("failed, ret:%{public}d", ret);
70     }
71     return static_cast<ErrCode>(ret);
72 }
73 
DisableSensor(uint32_t sensorId)74 ErrCode SensorServiceProxy::DisableSensor(uint32_t sensorId)
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option;
79     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
80         SEN_HILOGE("write descriptor failed");
81         return WRITE_MSG_ERR;
82     }
83     if (!data.WriteUint32(sensorId)) {
84         SEN_HILOGE("write sensorId failed");
85         return WRITE_MSG_ERR;
86     }
87     sptr<IRemoteObject> remote = Remote();
88     CHKPR(remote, ERROR);
89     int32_t ret = remote->SendRequest(ISensorService::DISABLE_SENSOR, data, reply, option);
90     if (ret != NO_ERROR) {
91         HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION",
92             HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableSensor", "ERROR_CODE", ret);
93         SEN_HILOGE("failed, ret:%{public}d", ret);
94     }
95     return static_cast<ErrCode>(ret);
96 }
97 
GetSensorList()98 std::vector<Sensor> SensorServiceProxy::GetSensorList()
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103     std::vector<Sensor> sensors;
104     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
105         SEN_HILOGE("write descriptor failed");
106         return sensors;
107     }
108     sptr<IRemoteObject> remote = Remote();
109     if (remote == nullptr) {
110         SEN_HILOGE("remote is null");
111         return sensors;
112     }
113     int32_t ret = remote->SendRequest(ISensorService::GET_SENSOR_LIST, data, reply, option);
114     if (ret != NO_ERROR) {
115         HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION",
116             HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorList", "ERROR_CODE", ret);
117         SEN_HILOGE("failed, ret:%{public}d", ret);
118         return sensors;
119     }
120     int32_t sensorCount = reply.ReadInt32();
121     SEN_HILOGD("sensorCount:%{public}d", sensorCount);
122     if (sensorCount > MAX_SENSOR_COUNT) {
123         sensorCount = MAX_SENSOR_COUNT;
124     }
125     Sensor sensor;
126     for (int32_t i = 0; i < sensorCount; i++) {
127         auto tmpSensor = sensor.Unmarshalling(reply);
128         if (tmpSensor == nullptr) {
129             continue;
130         }
131         sensors.push_back(*tmpSensor);
132     }
133     return sensors;
134 }
135 
TransferDataChannel(const sptr<SensorBasicDataChannel> & sensorBasicDataChannel,const sptr<IRemoteObject> & sensorClient)136 ErrCode SensorServiceProxy::TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel,
137                                                 const sptr<IRemoteObject> &sensorClient)
138 {
139     CHKPR(sensorBasicDataChannel, OBJECT_NULL);
140     CHKPR(sensorClient, OBJECT_NULL);
141     MessageParcel data;
142     MessageParcel reply;
143     MessageOption option;
144     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
145         SEN_HILOGE("write descriptor failed");
146         return WRITE_MSG_ERR;
147     }
148     sensorBasicDataChannel->SendToBinder(data);
149     if (!data.WriteRemoteObject(sensorClient)) {
150         SEN_HILOGE("sensorClient failed");
151         return WRITE_MSG_ERR;
152     }
153     sptr<IRemoteObject> remote = Remote();
154     CHKPR(remote, ERROR);
155     int32_t ret = remote->SendRequest(ISensorService::TRANSFER_DATA_CHANNEL, data, reply, option);
156     if (ret != NO_ERROR) {
157         HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION",
158             HiSysEvent::EventType::FAULT, "PKG_NAME", "TransferDataChannel", "ERROR_CODE", ret);
159         SEN_HILOGE("failed, ret:%{public}d", ret);
160     }
161     sensorBasicDataChannel->CloseSendFd();
162     return static_cast<ErrCode>(ret);
163 }
164 
DestroySensorChannel(sptr<IRemoteObject> sensorClient)165 ErrCode SensorServiceProxy::DestroySensorChannel(sptr<IRemoteObject> sensorClient)
166 {
167     CHKPR(sensorClient, OBJECT_NULL);
168     MessageParcel data;
169     MessageParcel reply;
170     MessageOption option;
171     if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) {
172         SEN_HILOGE("write descriptor failed");
173         return WRITE_MSG_ERR;
174     }
175     if (!data.WriteRemoteObject(sensorClient)) {
176         SEN_HILOGE("write sensorClient failed");
177         return WRITE_MSG_ERR;
178     }
179     sptr<IRemoteObject> remote = Remote();
180     CHKPR(remote, ERROR);
181     int32_t ret = remote->SendRequest(ISensorService::DESTROY_SENSOR_CHANNEL, data, reply, option);
182     if (ret != NO_ERROR) {
183         HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION",
184             HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroySensorChannel", "ERROR_CODE", ret);
185         SEN_HILOGE("failed, ret:%{public}d", ret);
186     }
187     return static_cast<ErrCode>(ret);
188 }
189 }  // namespace Sensors
190 }  // namespace OHOS
191