1 /*
2 * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 "medical_sensor_service_proxy.h"
17
18 #include <vector>
19
20 #include "dmd_report.h"
21 #include "message_parcel.h"
22 #include "medical_errors.h"
23 #include "medical_log_domain.h"
24
25 namespace OHOS {
26 namespace Sensors {
27 using namespace OHOS::HiviewDFX;
28
29 enum class FlushCmdId {
30 FLUSH = 0,
31 SET_MODE,
32 RESERVED,
33 };
34
35 namespace {
36 constexpr HiLogLabel LABEL = {
37 LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_FRAMEWORK, "MedicalSensorServiceProxy"
38 };
39 constexpr int32_t MAX_SENSOR_COUNT = 200;
40 } // namespace
41
MedicalSensorServiceProxy(const sptr<IRemoteObject> & impl)42 MedicalSensorServiceProxy::MedicalSensorServiceProxy(const sptr<IRemoteObject> &impl)
43 : IRemoteProxy<IMedicalSensorService>(impl)
44 {}
45
EnableSensor(uint32_t sensorId,int64_t samplingPeriodNs,int64_t maxReportDelayNs)46 ErrCode MedicalSensorServiceProxy::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs)
47 {
48 MessageParcel data;
49 MessageParcel reply;
50 MessageOption option;
51 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
52 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
53 return WRITE_MSG_ERR;
54 }
55 if (!data.WriteUint32(sensorId)) {
56 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
57 return WRITE_MSG_ERR;
58 }
59 if (!data.WriteInt64(samplingPeriodNs)) {
60 HiLog::Error(LABEL, "%{public}s write samplingPeriodNs failed", __func__);
61 return WRITE_MSG_ERR;
62 }
63 if (!data.WriteInt64(maxReportDelayNs)) {
64 HiLog::Error(LABEL, "%{public}s write maxReportDelayNs failed", __func__);
65 return WRITE_MSG_ERR;
66 }
67 int32_t ret = Remote()->SendRequest(IMedicalSensorService::ENABLE_SENSOR, data, reply, option);
68 if (ret != NO_ERROR) {
69 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "EnableSensor", ret);
70 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
71 }
72 return static_cast<ErrCode>(ret);
73 }
74
DisableSensor(uint32_t sensorId)75 ErrCode MedicalSensorServiceProxy::DisableSensor(uint32_t sensorId)
76 {
77 MessageParcel data;
78 MessageParcel reply;
79 MessageOption option;
80 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
81 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
82 return WRITE_MSG_ERR;
83 }
84 if (!data.WriteUint32(sensorId)) {
85 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
86 return WRITE_MSG_ERR;
87 }
88 int32_t ret = Remote()->SendRequest(IMedicalSensorService::DISABLE_SENSOR, data, reply, option);
89 if (ret != NO_ERROR) {
90 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "DisableSensor", ret);
91 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
92 }
93 return static_cast<ErrCode>(ret);
94 }
95
SetOption(uint32_t sensorId,uint32_t opt)96 ErrCode MedicalSensorServiceProxy::SetOption(uint32_t sensorId, uint32_t opt)
97 {
98 MessageParcel data;
99 MessageParcel reply;
100 MessageOption option;
101 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
102 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
103 return WRITE_MSG_ERR;
104 }
105 if (!data.WriteUint32(sensorId)) {
106 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
107 return WRITE_MSG_ERR;
108 }
109 if (!data.WriteUint32(opt)) {
110 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
111 return WRITE_MSG_ERR;
112 }
113
114 int32_t ret = Remote()->SendRequest(IMedicalSensorService::SET_OPTION, data, reply, option);
115 if (ret != NO_ERROR) {
116 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "DisableSensor", ret);
117 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
118 }
119 return static_cast<ErrCode>(ret);
120 }
121
GetSensorState(uint32_t sensorId)122 int32_t MedicalSensorServiceProxy::GetSensorState(uint32_t sensorId)
123 {
124 MessageParcel data;
125 MessageParcel reply;
126 MessageOption option;
127 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
128 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
129 return WRITE_MSG_ERR;
130 }
131 if (!data.WriteUint32(sensorId)) {
132 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
133 return WRITE_MSG_ERR;
134 }
135 int32_t ret = Remote()->SendRequest(IMedicalSensorService::GET_SENSOR_STATE, data, reply, option);
136 if (ret != NO_ERROR) {
137 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "GetSensorState", ret);
138 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
139 }
140 return static_cast<ErrCode>(ret);
141 }
142
RunCommand(uint32_t sensorId,uint32_t cmdType,uint32_t params)143 ErrCode MedicalSensorServiceProxy::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t params)
144 {
145 if (cmdType > static_cast<uint32_t>(FlushCmdId::RESERVED)) {
146 HiLog::Error(LABEL, "%{public}s failed, cmdType : %{public}u", __func__, cmdType);
147 return CMD_TYPE_ERR;
148 }
149 MessageParcel data;
150 MessageParcel reply;
151 MessageOption option;
152 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
153 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
154 return WRITE_MSG_ERR;
155 }
156 if (!data.WriteUint32(sensorId)) {
157 HiLog::Error(LABEL, "%{public}s write sensorId failed", __func__);
158 return WRITE_MSG_ERR;
159 }
160 if (!data.WriteUint32(cmdType)) {
161 HiLog::Error(LABEL, "%{public}s write cmdType failed", __func__);
162 return WRITE_MSG_ERR;
163 }
164 if (!data.WriteUint32(params)) {
165 HiLog::Error(LABEL, "%{public}s write params failed", __func__);
166 return WRITE_MSG_ERR;
167 }
168 int32_t ret = Remote()->SendRequest(IMedicalSensorService::RUN_COMMAND, data, reply, option);
169 if (ret != NO_ERROR) {
170 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "RunCommand", ret);
171 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
172 }
173 return static_cast<ErrCode>(ret);
174 }
175
GetSensorList()176 std::vector<MedicalSensor> MedicalSensorServiceProxy::GetSensorList()
177 {
178 MessageParcel data;
179 MessageParcel reply;
180 MessageOption option;
181 std::vector<MedicalSensor> sensors;
182 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
183 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
184 return sensors;
185 }
186 int32_t ret = Remote()->SendRequest(IMedicalSensorService::GET_SENSOR_LIST, data, reply, option);
187 if (ret != NO_ERROR) {
188 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "GetSensorList", ret);
189 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
190 return sensors;
191 }
192
193 int32_t sensorCount = reply.ReadInt32();
194 HiLog::Debug(LABEL, "%{public}s sensorCount : %{public}d", __func__, sensorCount);
195 if (sensorCount > MAX_SENSOR_COUNT) {
196 sensorCount = MAX_SENSOR_COUNT;
197 }
198 MedicalSensor sensor;
199 for (int32_t i = 0; i < sensorCount; i++) {
200 auto tmpSensor = sensor.Unmarshalling(reply);
201 if (tmpSensor == nullptr) {
202 continue;
203 }
204 sensors.push_back(*tmpSensor);
205 }
206 return sensors;
207 }
208
TransferDataChannel(const sptr<MedicalSensorBasicDataChannel> & sensorBasicDataChannel,const sptr<IRemoteObject> & afeClient)209 ErrCode MedicalSensorServiceProxy::TransferDataChannel(
210 const sptr<MedicalSensorBasicDataChannel> &sensorBasicDataChannel, const sptr<IRemoteObject> &afeClient)
211 {
212 HiLog::Debug(LABEL, "%{public}s sendFd: %{public}d", __func__, sensorBasicDataChannel->GetSendDataFd());
213 if (sensorBasicDataChannel == nullptr || afeClient == nullptr) {
214 HiLog::Error(LABEL, "%{public}s sensorBasicDataChannel or afeClient cannot be null", __func__);
215 return OBJECT_NULL;
216 }
217 MessageParcel data;
218 MessageParcel reply;
219 MessageOption option;
220 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
221 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
222 return WRITE_MSG_ERR;
223 }
224 sensorBasicDataChannel->SendToBinder(data);
225 if (!data.WriteRemoteObject(afeClient)) {
226 HiLog::Error(LABEL, "%{public}s write afeClient failed", __func__);
227 return WRITE_MSG_ERR;
228 }
229
230 int32_t ret = Remote()->SendRequest(IMedicalSensorService::TRANSFER_DATA_CHANNEL, data, reply, option);
231 if (ret != NO_ERROR) {
232 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "TransferDataChannel", ret);
233 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
234 }
235 sensorBasicDataChannel->CloseSendFd();
236 return static_cast<ErrCode>(ret);
237 }
238
DestroySensorChannel(sptr<IRemoteObject> afeClient)239 ErrCode MedicalSensorServiceProxy::DestroySensorChannel(sptr<IRemoteObject> afeClient)
240 {
241 if (afeClient == nullptr) {
242 HiLog::Error(LABEL, "%{public}s afeClient cannot be null", __func__);
243 return OBJECT_NULL;
244 }
245 MessageParcel data;
246 MessageParcel reply;
247 MessageOption option;
248 if (!data.WriteInterfaceToken(MedicalSensorServiceProxy::GetDescriptor())) {
249 HiLog::Error(LABEL, "%{public}s write descriptor failed", __func__);
250 return WRITE_MSG_ERR;
251 }
252 if (!data.WriteRemoteObject(afeClient)) {
253 HiLog::Error(LABEL, "%{public}s write afeClient failed", __func__);
254 return WRITE_MSG_ERR;
255 }
256 int32_t ret = Remote()->SendRequest(IMedicalSensorService::DESTROY_SENSOR_CHANNEL, data, reply, option);
257 if (ret != NO_ERROR) {
258 DmdReport::ReportException(SENSOR_SERVICE_IPC_EXCEPTION, "DestroySensorChannel", ret);
259 HiLog::Error(LABEL, "%{public}s failed, ret : %{public}d", __func__, ret);
260 }
261 return static_cast<ErrCode>(ret);
262 }
263 } // namespace Sensors
264 } // namespace OHOS
265