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_callback_stub.h"
17 #include "hilog_wrapper.h"
18 
19 namespace OHOS {
20 namespace ExternalDeviceManager {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)21 int DriverExtMgrCallbackStub::OnRemoteRequest(
22     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
23 {
24     EDM_LOGD(MODULE_DEV_MGR, "cmd:%u, flags:%d", code, option.GetFlags());
25     if (data.ReadInterfaceToken() != GetDescriptor()) {
26         EDM_LOGE(MODULE_DEV_MGR, "remote descriptor is not matched");
27         return UsbErrCode::EDM_ERR_INVALID_PARAM;
28     }
29 
30     switch (code) {
31         case static_cast<uint32_t>(DriverExtMgrCallbackInterfaceCode::ON_CONNECT):
32             return StubOnConnect(data, reply, option);
33         case static_cast<uint32_t>(DriverExtMgrCallbackInterfaceCode::ON_DISCONNECT):
34             return StubOnDisconnect(data, reply, option);
35         case static_cast<uint32_t>(DriverExtMgrCallbackInterfaceCode::ON_UNBIND):
36             return StubOnUnBind(data, reply, option);
37         default:
38             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
39     }
40 }
41 
StubOnConnect(MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int32_t DriverExtMgrCallbackStub::StubOnConnect(MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44     ErrMsg errMsg;
45     if (!ErrMsg::UnMarshalling(data, errMsg)) {
46         EDM_LOGE(MODULE_DEV_MGR, "failed to read errMsg");
47         return UsbErrCode::EDM_ERR_INVALID_PARAM;
48     }
49 
50     uint64_t deviceId = 0;
51     sptr<IRemoteObject> drvExtObj = nullptr;
52     if (errMsg.IsOk()) {
53         if (!data.ReadUint64(deviceId)) {
54             EDM_LOGE(MODULE_DEV_MGR, "failed to read deviceId");
55             return UsbErrCode::EDM_ERR_INVALID_PARAM;
56         }
57 
58         drvExtObj = data.ReadRemoteObject();
59         if (drvExtObj == nullptr) {
60             EDM_LOGE(MODULE_DEV_MGR, "failed to read drvExtObj");
61             return UsbErrCode::EDM_ERR_INVALID_PARAM;
62         }
63     }
64 
65     OnConnect(deviceId, drvExtObj, errMsg);
66     return UsbErrCode::EDM_OK;
67 }
68 
StubOnDisconnect(MessageParcel & data,MessageParcel & reply,MessageOption & option)69 int32_t DriverExtMgrCallbackStub::StubOnDisconnect(MessageParcel &data, MessageParcel &reply, MessageOption &option)
70 {
71     ErrMsg errMsg;
72     if (!ErrMsg::UnMarshalling(data, errMsg)) {
73         EDM_LOGE(MODULE_DEV_MGR, "failed to read errMsg");
74         return UsbErrCode::EDM_ERR_INVALID_PARAM;
75     }
76 
77     uint64_t deviceId = 0;
78     if (errMsg.IsOk()) {
79         if (!data.ReadUint64(deviceId)) {
80             EDM_LOGE(MODULE_DEV_MGR, "failed to read deviceId");
81             return UsbErrCode::EDM_ERR_INVALID_PARAM;
82         }
83     }
84 
85     OnDisconnect(deviceId, errMsg);
86     return UsbErrCode::EDM_OK;
87 }
88 
StubOnUnBind(MessageParcel & data,MessageParcel & reply,MessageOption & option)89 int32_t DriverExtMgrCallbackStub::StubOnUnBind(MessageParcel &data, MessageParcel &reply, MessageOption &option)
90 {
91     ErrMsg errMsg;
92     if (!ErrMsg::UnMarshalling(data, errMsg)) {
93         EDM_LOGE(MODULE_DEV_MGR, "failed to read errMsg");
94         return UsbErrCode::EDM_ERR_INVALID_PARAM;
95     }
96 
97     uint64_t deviceId = 0;
98     if (errMsg.IsOk()) {
99         if (!data.ReadUint64(deviceId)) {
100             EDM_LOGE(MODULE_DEV_MGR, "failed to read deviceId");
101             return UsbErrCode::EDM_ERR_INVALID_PARAM;
102         }
103     }
104 
105     OnUnBind(deviceId, errMsg);
106     return UsbErrCode::EDM_OK;
107 }
108 } // namespace ExternalDeviceManager
109 } // namespace OHOS
110