• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "driver_ext_mgr_stub.h"
17 #include <cinttypes>
18 
19 #include "hilog_wrapper.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace ExternalDeviceManager {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int DriverExtMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
25 {
26     EDM_LOGD(MODULE_FRAMEWORK, "cmd:%u, flags:%d", code, option.GetFlags());
27     if (data.ReadInterfaceToken() != GetDescriptor()) {
28         EDM_LOGE(MODULE_FRAMEWORK, "remote descriptor is not matched");
29         return UsbErrCode::EDM_ERR_INVALID_PARAM;
30     }
31 
32     switch (code) {
33         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE):
34             return OnQueryDevice(data, reply, option);
35         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::BIND_DEVICE):
36             return OnBindDevice(data, reply, option);
37         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::UNBIND_DEVICE):
38             return OnUnBindDevice(data, reply, option);
39         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::BIND_DRIVER_WITH_DEVICE_ID):
40             return OnBindDriverWithDeviceId(data, reply, option);
41         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::UNBIND_DRIVER_WITH_DEVICE_ID):
42             return OnUnbindDriverWithDeviceId(data, reply, option);
43         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DEVICE_INFO):
44             return OnQueryDeviceInfo(data, reply, option);
45         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DRIVER_INFO):
46             return OnQueryDriverInfo(data, reply, option);
47         default:
48             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49     }
50 }
51 
OnQueryDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)52 int32_t DriverExtMgrStub::OnQueryDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
53 {
54     uint32_t busType = 0;
55     if (!data.ReadUint32(busType)) {
56         EDM_LOGE(MODULE_FRAMEWORK, "failed to read busType");
57         return UsbErrCode::EDM_ERR_INVALID_PARAM;
58     }
59 
60     std::vector<std::shared_ptr<DeviceData>> devices;
61     UsbErrCode ret = QueryDevice(busType, devices);
62     if (ret != UsbErrCode::EDM_OK) {
63         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDevice function:%{public}d", static_cast<int32_t>(ret));
64         return ret;
65     }
66 
67     if (!reply.WriteUint64(static_cast<uint64_t>(devices.size()))) {
68         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of devices");
69         return UsbErrCode::EDM_ERR_INVALID_PARAM;
70     }
71 
72     for (uint64_t i = 0; i < devices.size(); i++) {
73         if (devices[i] == nullptr) {
74             EDM_LOGE(MODULE_FRAMEWORK, "invalid %{public}016" PRIX64 " device", i);
75             return UsbErrCode::EDM_ERR_INVALID_PARAM;
76         }
77 
78         if (!devices[i]->Marshalling(reply)) {
79             EDM_LOGE(MODULE_FRAMEWORK, "failed to write %{public}016" PRIX64 " device", i);
80             return UsbErrCode::EDM_ERR_INVALID_PARAM;
81         }
82     }
83 
84     return UsbErrCode::EDM_OK;
85 }
86 
OnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)87 int32_t DriverExtMgrStub::OnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
88 {
89     uint64_t deviceId = 0;
90     if (!data.ReadUint64(deviceId)) {
91         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
92         return UsbErrCode::EDM_ERR_INVALID_PARAM;
93     }
94 
95     sptr<IRemoteObject> remote = data.ReadRemoteObject();
96     if (remote == nullptr) {
97         EDM_LOGE(MODULE_FRAMEWORK, "failed to read remote object of connectCallback");
98         return UsbErrCode::EDM_ERR_INVALID_PARAM;
99     }
100 
101     sptr<IDriverExtMgrCallback> connectCallback = iface_cast<IDriverExtMgrCallback>(remote);
102     if (connectCallback == nullptr) {
103         EDM_LOGE(MODULE_FRAMEWORK, "failed to create connectCallback object");
104         return UsbErrCode::EDM_ERR_INVALID_PARAM;
105     }
106 
107     UsbErrCode ret = BindDevice(deviceId, connectCallback);
108     if (ret != UsbErrCode::EDM_OK) {
109         EDM_LOGE(MODULE_FRAMEWORK, "failed to call BindDevice function:%{public}d", static_cast<int32_t>(ret));
110         return ret;
111     }
112 
113     return UsbErrCode::EDM_OK;
114 }
115 
OnUnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)116 int32_t DriverExtMgrStub::OnUnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
117 {
118     uint64_t deviceId = 0;
119     if (!data.ReadUint64(deviceId)) {
120         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
121         return UsbErrCode::EDM_ERR_INVALID_PARAM;
122     }
123 
124     UsbErrCode ret = UnBindDevice(deviceId);
125     if (ret != UsbErrCode::EDM_OK) {
126         EDM_LOGE(MODULE_FRAMEWORK, "failed to call UnBindDevice function:%{public}d", static_cast<int32_t>(ret));
127         return ret;
128     }
129 
130     return UsbErrCode::EDM_OK;
131 }
132 
OnBindDriverWithDeviceId(MessageParcel & data,MessageParcel & reply,MessageOption & option)133 int32_t DriverExtMgrStub::OnBindDriverWithDeviceId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
134 {
135     uint64_t deviceId = 0;
136     if (!data.ReadUint64(deviceId)) {
137         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
138         return UsbErrCode::EDM_ERR_INVALID_PARAM;
139     }
140 
141     sptr<IRemoteObject> remote = data.ReadRemoteObject();
142     if (remote == nullptr) {
143         EDM_LOGE(MODULE_FRAMEWORK, "failed to read remote object of connectCallback");
144         return UsbErrCode::EDM_ERR_INVALID_PARAM;
145     }
146 
147     sptr<IDriverExtMgrCallback> connectCallback = iface_cast<IDriverExtMgrCallback>(remote);
148     if (connectCallback == nullptr) {
149         EDM_LOGE(MODULE_FRAMEWORK, "failed to create connectCallback object");
150         return UsbErrCode::EDM_ERR_INVALID_PARAM;
151     }
152 
153     UsbErrCode ret = BindDriverWithDeviceId(deviceId, connectCallback);
154     if (ret != UsbErrCode::EDM_OK) {
155         EDM_LOGE(MODULE_FRAMEWORK, "failed to call BindDriverWithDeviceId function:%{public}d",
156             static_cast<int32_t>(ret));
157         return ret;
158     }
159 
160     return UsbErrCode::EDM_OK;
161 }
162 
OnUnbindDriverWithDeviceId(MessageParcel & data,MessageParcel & reply,MessageOption & option)163 int32_t DriverExtMgrStub::OnUnbindDriverWithDeviceId(MessageParcel &data, MessageParcel &reply, MessageOption &option)
164 {
165     uint64_t deviceId = 0;
166     if (!data.ReadUint64(deviceId)) {
167         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
168         return UsbErrCode::EDM_ERR_INVALID_PARAM;
169     }
170 
171     UsbErrCode ret = UnbindDriverWithDeviceId(deviceId);
172     if (ret != UsbErrCode::EDM_OK) {
173         EDM_LOGE(MODULE_FRAMEWORK, "failed to call UnbindDriverWithDeviceId function:%{public}d",
174             static_cast<int32_t>(ret));
175         return ret;
176     }
177 
178     return UsbErrCode::EDM_OK;
179 }
180 
OnQueryDeviceInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)181 int32_t DriverExtMgrStub::OnQueryDeviceInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
182 {
183     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDeviceInfo start");
184     bool isByDeviceId = false;
185     if (!data.ReadBool(isByDeviceId)) {
186         EDM_LOGE(MODULE_FRAMEWORK, "failed to read isByDeviceId");
187         return UsbErrCode::EDM_ERR_INVALID_PARAM;
188     }
189 
190     uint64_t deviceId = 0;
191     if (isByDeviceId && !data.ReadUint64(deviceId)) {
192         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
193         return UsbErrCode::EDM_ERR_INVALID_PARAM;
194     }
195 
196     std::vector<std::shared_ptr<DeviceInfoData>> deviceInfos;
197     int32_t ret = QueryDeviceInfo(deviceInfos, isByDeviceId, deviceId);
198     if (ret != UsbErrCode::EDM_OK) {
199         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDeviceInfo");
200         return ret;
201     }
202 
203     uint64_t deviceInfoSize = static_cast<uint64_t>(deviceInfos.size());
204     if (!reply.WriteUint64(deviceInfoSize)) {
205         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of deviceInfos");
206         return UsbErrCode::EDM_ERR_INVALID_PARAM;
207     }
208 
209     for (uint64_t i = 0; i < deviceInfoSize; i++) {
210         if (deviceInfos[i] == nullptr) {
211             EDM_LOGE(MODULE_FRAMEWORK, "invalid deviceInfo, index: %{public}" PRIu64, i);
212             return UsbErrCode::EDM_ERR_INVALID_PARAM;
213         }
214 
215         if (!deviceInfos[i]->Marshalling(reply)) {
216             EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceInfo, index: %{public}" PRIu64, i);
217             return UsbErrCode::EDM_ERR_INVALID_PARAM;
218         }
219     }
220 
221     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDeviceInfo end");
222 
223     return UsbErrCode::EDM_OK;
224 }
225 
OnQueryDriverInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)226 int32_t DriverExtMgrStub::OnQueryDriverInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
227 {
228     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDriverInfo start");
229 
230     bool isByDriverUid = false;
231     if (!data.ReadBool(isByDriverUid)) {
232         EDM_LOGE(MODULE_FRAMEWORK, "failed to read isByDriverUid");
233         return UsbErrCode::EDM_ERR_INVALID_PARAM;
234     }
235 
236     std::string driverUid = "";
237     if (isByDriverUid && !data.ReadString(driverUid)) {
238         EDM_LOGE(MODULE_FRAMEWORK, "failed to read driverUid");
239         return UsbErrCode::EDM_ERR_INVALID_PARAM;
240     }
241 
242     std::vector<std::shared_ptr<DriverInfoData>> driverInfos;
243     int32_t ret = QueryDriverInfo(driverInfos, isByDriverUid, driverUid);
244     if (ret != UsbErrCode::EDM_OK) {
245         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDriverInfo");
246         return ret;
247     }
248 
249     uint64_t driverInfoSize = static_cast<uint64_t>(driverInfos.size());
250     if (!reply.WriteUint64(driverInfoSize)) {
251         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of driverInfos");
252         return UsbErrCode::EDM_ERR_INVALID_PARAM;
253     }
254 
255     for (uint64_t i = 0; i < driverInfoSize; i++) {
256         if (driverInfos[i] == nullptr) {
257             EDM_LOGE(MODULE_FRAMEWORK, "invalid driverInfo, index: %{public}" PRIu64, i);
258             return UsbErrCode::EDM_ERR_INVALID_PARAM;
259         }
260 
261         if (!driverInfos[i]->Marshalling(reply)) {
262             EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverInfo, index: %{public}" PRIu64, i);
263             return UsbErrCode::EDM_ERR_INVALID_PARAM;
264         }
265     }
266 
267     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDriverInfo end");
268 
269     return UsbErrCode::EDM_OK;
270 }
271 } // namespace ExternalDeviceManager
272 } // namespace OHOS
273