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 #include "driver_ext_mgr_types.h"
16 #include <sstream>
17
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace ExternalDeviceManager {
Marshalling(MessageParcel & parcel) const22 bool ErrMsg::Marshalling(MessageParcel &parcel) const
23 {
24 if (!parcel.WriteInt32(errCode)) {
25 EDM_LOGE(MODULE_DEV_MGR, "failed to write errCode");
26 return false;
27 }
28
29 if (!parcel.WriteString(msg)) {
30 EDM_LOGE(MODULE_DEV_MGR, "failed to write msg");
31 return false;
32 }
33 return true;
34 }
35
UnMarshalling(MessageParcel & parcel,ErrMsg & data)36 bool ErrMsg::UnMarshalling(MessageParcel &parcel, ErrMsg &data)
37 {
38 int32_t err = 0;
39 if (!parcel.ReadInt32(err)) {
40 EDM_LOGE(MODULE_DEV_MGR, "failed to read errCode");
41 return false;
42 }
43 data.errCode = static_cast<UsbErrCode>(err);
44
45 if (!parcel.ReadString(data.msg)) {
46 EDM_LOGE(MODULE_DEV_MGR, "failed to read msg");
47 return false;
48 }
49 return true;
50 }
51
Marshalling(MessageParcel & parcel) const52 bool DeviceData::Marshalling(MessageParcel &parcel) const
53 {
54 if (!parcel.WriteUint32(static_cast<uint32_t>(busType))) {
55 EDM_LOGE(MODULE_DEV_MGR, "failed to write busType");
56 return false;
57 }
58
59 if (!parcel.WriteUint64(deviceId)) {
60 EDM_LOGE(MODULE_DEV_MGR, "failed to write deviceId");
61 return false;
62 }
63
64 if (!parcel.WriteString(descripton)) {
65 EDM_LOGE(MODULE_DEV_MGR, "failed to write descripton");
66 return false;
67 }
68
69 return true;
70 }
71
Marshalling(MessageParcel & parcel) const72 bool USBDevice::Marshalling(MessageParcel &parcel) const
73 {
74 if (!DeviceData::Marshalling(parcel)) {
75 return false;
76 }
77
78 if (!parcel.WriteUint16(productId)) {
79 EDM_LOGE(MODULE_DEV_MGR, "failed to write productId");
80 return false;
81 }
82
83 if (!parcel.WriteUint16(vendorId)) {
84 EDM_LOGE(MODULE_DEV_MGR, "failed to write vendorId");
85 return false;
86 }
87
88 return true;
89 }
90
UnMarshalling(MessageParcel & parcel)91 std::shared_ptr<DeviceData> DeviceData::UnMarshalling(MessageParcel &parcel)
92 {
93 uint32_t busTypeData = 0;
94 if (!parcel.ReadUint32(busTypeData)) {
95 EDM_LOGE(MODULE_DEV_MGR, "failed to read busType");
96 return nullptr;
97 }
98
99 BusType busType = static_cast<BusType>(busTypeData);
100 if (busType == BusType::BUS_TYPE_INVALID) {
101 EDM_LOGE(MODULE_DEV_MGR, "invalid busType:%{public}u", busTypeData);
102 return nullptr;
103 }
104
105 // if you need to extend the DeviceData type, please add code to read it here
106 std::shared_ptr<DeviceData> device;
107 switch (busType) {
108 case BusType::BUS_TYPE_USB: {
109 device = USBDevice::UnMarshalling(parcel);
110 break;
111 }
112 default:
113 break;
114 }
115
116 if (device != nullptr) {
117 device->busType = busType;
118 }
119 return device;
120 }
121
Dump()122 std::string DeviceData::Dump()
123 {
124 std::stringstream os;
125 os << "{busType:" << busType << ", ";
126 os << "deviceId:" << deviceId << ", ";
127 os << "descripton:" << descripton << "}";
128 return os.str();
129 }
130
UnMarshalling(MessageParcel & parcel)131 std::shared_ptr<DeviceData> USBDevice::UnMarshalling(MessageParcel &parcel)
132 {
133 // the busType has been read
134 std::shared_ptr<USBDevice> device = std::make_shared<USBDevice>();
135 if (!parcel.ReadUint64(device->deviceId)) {
136 EDM_LOGE(MODULE_DEV_MGR, "failed to read deviceId");
137 return nullptr;
138 }
139
140 if (!parcel.ReadString(device->descripton)) {
141 EDM_LOGE(MODULE_DEV_MGR, "failed to read descripton");
142 return nullptr;
143 }
144
145 if (!parcel.ReadUint16(device->productId)) {
146 EDM_LOGE(MODULE_DEV_MGR, "failed to read productId");
147 return nullptr;
148 }
149
150 if (!parcel.ReadUint16(device->vendorId)) {
151 EDM_LOGE(MODULE_DEV_MGR, "failed to read vendorId");
152 return nullptr;
153 }
154
155 return device;
156 }
157
EmitItemMarshalling(int32_t deviceId,const std::vector<EmitItem> & items,MessageParcel & parcel)158 bool EmitItemMarshalling(int32_t deviceId, const std::vector<EmitItem> &items, MessageParcel &parcel)
159 {
160 if (!parcel.WriteUint32(deviceId)) {
161 EDM_LOGE(MODULE_DEV_MGR, "failed to write device id");
162 return false;
163 }
164
165 if (!parcel.WriteUint32(static_cast<uint32_t>(items.size()))) {
166 EDM_LOGE(MODULE_DEV_MGR, "failed to write vector size");
167 return false;
168 }
169
170 for (auto &ele : items) {
171 if (!parcel.WriteUint16(ele.type)) {
172 EDM_LOGE(MODULE_DEV_MGR, "failed to write type");
173 return false;
174 }
175
176 if (!parcel.WriteUint16(ele.code)) {
177 EDM_LOGE(MODULE_DEV_MGR, "failed to write code");
178 return false;
179 }
180 if (!parcel.WriteUint32(ele.value)) {
181 EDM_LOGE(MODULE_DEV_MGR, "failed to write value");
182 return false;
183 }
184 }
185
186 return true;
187 }
188
EmitItemUnMarshalling(MessageParcel & parcel,int32_t & deviceId)189 std::optional<std::vector<EmitItem>> EmitItemUnMarshalling(MessageParcel &parcel, int32_t &deviceId)
190 {
191 uint32_t id = 0;
192 if (!parcel.ReadUint32(id)) {
193 EDM_LOGE(MODULE_DEV_MGR, "failed to read device id");
194 return std::nullopt;
195 }
196 deviceId = static_cast<int32_t>(id);
197
198 uint32_t size = 0;
199 if (!parcel.ReadUint32(size)) {
200 EDM_LOGE(MODULE_DEV_MGR, "failed to read size");
201 return std::nullopt;
202 }
203
204 if (size > MAX_EMIT_ITEM_NUM) {
205 EDM_LOGE(MODULE_DEV_MGR, "size out of range");
206 return std::nullopt;
207 }
208
209 std::vector<EmitItem> items;
210 EmitItem item;
211 for (uint32_t i = 0; i < size; ++i) {
212 if (!parcel.ReadUint16(item.type)) {
213 EDM_LOGE(MODULE_DEV_MGR, "failed to read type");
214 return std::nullopt;
215 }
216
217 if (!parcel.ReadUint16(item.code)) {
218 EDM_LOGE(MODULE_DEV_MGR, "failed to read code");
219 return std::nullopt;
220 }
221
222 if (!parcel.ReadUint32(item.value)) {
223 EDM_LOGE(MODULE_DEV_MGR, "failed to read value");
224 return std::nullopt;
225 }
226 items.push_back(item);
227 }
228 return items;
229 }
230
Dump()231 std::string USBDevice::Dump()
232 {
233 std::stringstream os;
234 os << "{busType:" << busType << ", ";
235 os << "deviceId:" << deviceId << ", ";
236 os << "descripton:" << descripton << ", ";
237 os << "productId:" << productId << ", ";
238 os << "vendorId:" << vendorId << "}";
239 return os.str();
240 }
241 } // namespace ExternalDeviceManager
242 } // namespace OHOS