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::QUERY_DEVICE_INFO):
40             return OnQueryDeviceInfo(data, reply, option);
41         case static_cast<uint32_t>(DriverExtMgrInterfaceCode::QUERY_DRIVER_INFO):
42             return OnQueryDriverInfo(data, reply, option);
43         default:
44             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
45     }
46 }
47 
OnQueryDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t DriverExtMgrStub::OnQueryDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
49 {
50     uint32_t busType = 0;
51     if (!data.ReadUint32(busType)) {
52         EDM_LOGE(MODULE_FRAMEWORK, "failed to read busType");
53         return UsbErrCode::EDM_ERR_INVALID_PARAM;
54     }
55 
56     std::vector<std::shared_ptr<DeviceData>> devices;
57     UsbErrCode ret = QueryDevice(busType, devices);
58     if (ret != UsbErrCode::EDM_OK) {
59         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDevice function:%{public}d", static_cast<int32_t>(ret));
60         return ret;
61     }
62 
63     if (!reply.WriteUint64(static_cast<uint64_t>(devices.size()))) {
64         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of devices");
65         return UsbErrCode::EDM_ERR_INVALID_PARAM;
66     }
67 
68     for (uint64_t i = 0; i < devices.size(); i++) {
69         if (devices[i] == nullptr) {
70             EDM_LOGE(MODULE_FRAMEWORK, "invalid %{public}016" PRIX64 " device", i);
71             return UsbErrCode::EDM_ERR_INVALID_PARAM;
72         }
73 
74         if (!devices[i]->Marshalling(reply)) {
75             EDM_LOGE(MODULE_FRAMEWORK, "failed to write %{public}016" PRIX64 " device", i);
76             return UsbErrCode::EDM_ERR_INVALID_PARAM;
77         }
78     }
79 
80     return UsbErrCode::EDM_OK;
81 }
82 
OnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)83 int32_t DriverExtMgrStub::OnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
84 {
85     uint64_t deviceId = 0;
86     if (!data.ReadUint64(deviceId)) {
87         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
88         return UsbErrCode::EDM_ERR_INVALID_PARAM;
89     }
90 
91     sptr<IRemoteObject> remote = data.ReadRemoteObject();
92     if (remote == nullptr) {
93         EDM_LOGE(MODULE_FRAMEWORK, "failed to read remote object of connectCallback");
94         return UsbErrCode::EDM_ERR_INVALID_PARAM;
95     }
96 
97     sptr<IDriverExtMgrCallback> connectCallback = iface_cast<IDriverExtMgrCallback>(remote);
98     if (connectCallback == nullptr) {
99         EDM_LOGE(MODULE_FRAMEWORK, "failed to create connectCallback object");
100         return UsbErrCode::EDM_ERR_INVALID_PARAM;
101     }
102 
103     UsbErrCode ret = BindDevice(deviceId, connectCallback);
104     if (ret != UsbErrCode::EDM_OK) {
105         EDM_LOGE(MODULE_FRAMEWORK, "failed to call BindDevice function:%{public}d", static_cast<int32_t>(ret));
106         return ret;
107     }
108 
109     return UsbErrCode::EDM_OK;
110 }
111 
OnUnBindDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)112 int32_t DriverExtMgrStub::OnUnBindDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
113 {
114     uint64_t deviceId = 0;
115     if (!data.ReadUint64(deviceId)) {
116         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
117         return UsbErrCode::EDM_ERR_INVALID_PARAM;
118     }
119 
120     UsbErrCode ret = UnBindDevice(deviceId);
121     if (ret != UsbErrCode::EDM_OK) {
122         EDM_LOGE(MODULE_FRAMEWORK, "failed to call UnBindDevice function:%{public}d", static_cast<int32_t>(ret));
123         return ret;
124     }
125 
126     return UsbErrCode::EDM_OK;
127 }
128 
OnQueryDeviceInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)129 int32_t DriverExtMgrStub::OnQueryDeviceInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
130 {
131     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDeviceInfo start");
132     bool isByDeviceId = false;
133     if (!data.ReadBool(isByDeviceId)) {
134         EDM_LOGE(MODULE_FRAMEWORK, "failed to read isByDeviceId");
135         return UsbErrCode::EDM_ERR_INVALID_PARAM;
136     }
137 
138     uint64_t deviceId = 0;
139     if (isByDeviceId && !data.ReadUint64(deviceId)) {
140         EDM_LOGE(MODULE_FRAMEWORK, "failed to read deviceId");
141         return UsbErrCode::EDM_ERR_INVALID_PARAM;
142     }
143 
144     std::vector<std::shared_ptr<DeviceInfoData>> deviceInfos;
145     int32_t ret = QueryDeviceInfo(deviceInfos, isByDeviceId, deviceId);
146     if (ret != UsbErrCode::EDM_OK) {
147         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDeviceInfo");
148         return ret;
149     }
150 
151     uint64_t deviceInfoSize = static_cast<uint64_t>(deviceInfos.size());
152     if (!reply.WriteUint64(deviceInfoSize)) {
153         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of deviceInfos");
154         return UsbErrCode::EDM_ERR_INVALID_PARAM;
155     }
156 
157     for (uint64_t i = 0; i < deviceInfoSize; i++) {
158         if (deviceInfos[i] == nullptr) {
159             EDM_LOGE(MODULE_FRAMEWORK, "invalid deviceInfo, index: %{public}" PRIu64, i);
160             return UsbErrCode::EDM_ERR_INVALID_PARAM;
161         }
162 
163         if (!deviceInfos[i]->Marshalling(reply)) {
164             EDM_LOGE(MODULE_FRAMEWORK, "failed to write deviceInfo, index: %{public}" PRIu64, i);
165             return UsbErrCode::EDM_ERR_INVALID_PARAM;
166         }
167     }
168 
169     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDeviceInfo end");
170 
171     return UsbErrCode::EDM_OK;
172 }
173 
OnQueryDriverInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)174 int32_t DriverExtMgrStub::OnQueryDriverInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
175 {
176     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDriverInfo start");
177 
178     bool isByDriverUid = false;
179     if (!data.ReadBool(isByDriverUid)) {
180         EDM_LOGE(MODULE_FRAMEWORK, "failed to read isByDriverUid");
181         return UsbErrCode::EDM_ERR_INVALID_PARAM;
182     }
183 
184     std::string driverUid = "";
185     if (isByDriverUid && !data.ReadString(driverUid)) {
186         EDM_LOGE(MODULE_FRAMEWORK, "failed to read driverUid");
187         return UsbErrCode::EDM_ERR_INVALID_PARAM;
188     }
189 
190     std::vector<std::shared_ptr<DriverInfoData>> driverInfos;
191     int32_t ret = QueryDriverInfo(driverInfos, isByDriverUid, driverUid);
192     if (ret != UsbErrCode::EDM_OK) {
193         EDM_LOGE(MODULE_FRAMEWORK, "failed to call QueryDriverInfo");
194         return ret;
195     }
196 
197     uint64_t driverInfoSize = static_cast<uint64_t>(driverInfos.size());
198     if (!reply.WriteUint64(driverInfoSize)) {
199         EDM_LOGE(MODULE_FRAMEWORK, "failed to write size of driverInfos");
200         return UsbErrCode::EDM_ERR_INVALID_PARAM;
201     }
202 
203     for (uint64_t i = 0; i < driverInfoSize; i++) {
204         if (driverInfos[i] == nullptr) {
205             EDM_LOGE(MODULE_FRAMEWORK, "invalid driverInfo, index: %{public}" PRIu64, i);
206             return UsbErrCode::EDM_ERR_INVALID_PARAM;
207         }
208 
209         if (!driverInfos[i]->Marshalling(reply)) {
210             EDM_LOGE(MODULE_FRAMEWORK, "failed to write driverInfo, index: %{public}" PRIu64, i);
211             return UsbErrCode::EDM_ERR_INVALID_PARAM;
212         }
213     }
214 
215     EDM_LOGD(MODULE_FRAMEWORK, "OnQueryDriverInfo end");
216 
217     return UsbErrCode::EDM_OK;
218 }
219 } // namespace ExternalDeviceManager
220 } // namespace OHOS
221