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 "usbd_subscriber.h"
17 #include "hdf_log.h"
18 #include "usb_errors.h"
19
20 namespace OHOS {
21 namespace USB {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)22 int32_t UsbdSubscriber::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
23 {
24 int32_t ret;
25 UsbInfo info;
26 PortInfo pinfo;
27 HDF_LOGI("%{public}s:%{public}d subscriber event code:%{public}d", __func__, __LINE__, code);
28 switch (code) {
29 case CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT:
30 if (data.ReadInterfaceToken() != GetDescriptor()) {
31 HDF_LOGE("%{public}s:%{public}d token check failed\n", __func__, __LINE__);
32 return UEC_HDF_ERR_INVALID_PARAM;
33 }
34 ret = ParserUsbInfo(data, reply, option, info);
35 if (ret == UEC_OK) {
36 ret = DeviceEvent(info);
37 }
38 break;
39 case CMD_NOTIFY_PORT_CHANGED:
40 if (data.ReadInterfaceToken() != GetDescriptor()) {
41 HDF_LOGE("%{public}s:%{public}d token check failed\n", __func__, __LINE__);
42 return UEC_HDF_ERR_INVALID_PARAM;
43 }
44 ret = ParserPortInfo(data, reply, option, pinfo);
45 if (ret == UEC_OK) {
46 ret = PortChangedEvent(pinfo.portId, pinfo.powerRole, pinfo.dataRole, pinfo.mode);
47 }
48 break;
49 default:
50 ret = ERR_INVALID_OPERATION;
51 break;
52 }
53 return ret;
54 }
55
ParserUsbInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option,UsbInfo & info)56 int32_t UsbdSubscriber::ParserUsbInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option, UsbInfo &info)
57 {
58 int32_t status;
59 int32_t busNum;
60 int32_t devNum;
61
62 if (!data.ReadInt32(status)) {
63 HDF_LOGE("Read devInfo status error");
64 return UEC_HDF_ERR_INVALID_PARAM;
65 }
66 if (!data.ReadInt32(busNum)) {
67 HDF_LOGE("Read devInfo busNum error");
68 return UEC_HDF_ERR_INVALID_PARAM;
69 }
70 if (!data.ReadInt32(devNum)) {
71 HDF_LOGE("Read devInfo devNum error");
72 return UEC_HDF_ERR_INVALID_PARAM;
73 }
74 info.setDevInfoStatus(status);
75 info.setDevInfoBusNum(busNum);
76 info.setDevInfoDevNum(devNum);
77 return UEC_OK;
78 }
79
ParserPortInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option,PortInfo & info)80 int32_t UsbdSubscriber::ParserPortInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option, PortInfo &info)
81 {
82 int32_t portId;
83 int32_t powerRole;
84 int32_t dataRole;
85 int32_t mode;
86
87 if (!data.ReadInt32(portId)) {
88 HDF_LOGE("Read PortInfo id error");
89 return UEC_HDF_ERR_INVALID_PARAM;
90 }
91 if (!data.ReadInt32(powerRole)) {
92 HDF_LOGE("Read PortInfo powerRole error");
93 return UEC_HDF_ERR_INVALID_PARAM;
94 }
95 if (!data.ReadInt32(dataRole)) {
96 HDF_LOGE("Read PortInfo dataRole error");
97 return UEC_HDF_ERR_INVALID_PARAM;
98 }
99 if (!data.ReadInt32(mode)) {
100 HDF_LOGE("Read PortInfo mode error");
101 return UEC_HDF_ERR_INVALID_PARAM;
102 }
103 info.portId = portId;
104 info.powerRole = powerRole;
105 info.dataRole = dataRole;
106 info.mode = mode;
107 return UEC_OK;
108 }
109 } // namespace USB
110 } // namespace OHOS
111