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 #ifndef DRIVER_EXTENSION_MANAGER_TYPES_H
17 #define DRIVER_EXTENSION_MANAGER_TYPES_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "ext_object.h"
23 #include "message_parcel.h"
24 
25 namespace OHOS {
26 namespace ExternalDeviceManager {
27 struct ErrMsg {
28     ErrMsg(UsbErrCode code = UsbErrCode::EDM_NOK, const std::string &message = "") : errCode(code), msg(message) {}
29 
IsOkErrMsg30     inline bool IsOk() const
31     {
32         return errCode == UsbErrCode::EDM_OK;
33     }
34 
35     bool Marshalling(MessageParcel &parcel) const;
36     static bool UnMarshalling(MessageParcel &parcel, ErrMsg &data);
37 
38     UsbErrCode errCode;
39     std::string msg;
40 };
41 
42 class DeviceData {
43 public:
44     virtual ~DeviceData() = default;
45 
46     virtual bool Marshalling(MessageParcel &parcel) const;
47     static std::shared_ptr<DeviceData> UnMarshalling(MessageParcel &parcel);
48     virtual std::string Dump();
49 
50     BusType busType;
51     uint64_t deviceId;
52     std::string descripton;
53 };
54 
55 class USBDevice : public DeviceData {
56 public:
57     virtual ~USBDevice() = default;
58 
59     bool Marshalling(MessageParcel &parcel) const override;
60     static std::shared_ptr<DeviceData> UnMarshalling(MessageParcel &parcel);
61     std::string Dump() override;
62 
63     uint16_t productId;
64     uint16_t vendorId;
65 };
66 
67 class DeviceInfoData {
68 public:
69     virtual ~DeviceInfoData() = default;
70     virtual bool Marshalling(MessageParcel &parcel) const;
71     static std::shared_ptr<DeviceInfoData> UnMarshalling(MessageParcel &parcel);
72     static bool DeviceInfosUnMarshalling(MessageParcel &parcel,
73         std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos);
74     static BusType GetBusTypeByDeviceId(uint64_t deviceId);
75 
76     uint64_t deviceId;
77     bool isDriverMatched = false;
78     std::string driverUid = "";
79 };
80 
81 class USBInterfaceDesc {
82 public:
83     virtual ~USBInterfaceDesc() = default;
84 
85     bool Marshalling(MessageParcel &parcel) const;
86     static std::shared_ptr<USBInterfaceDesc> UnMarshalling(MessageParcel &parcel);
87 
88     uint8_t bInterfaceNumber;
89     uint8_t bClass;
90     uint8_t bSubClass;
91     uint8_t bProtocol;
92 };
93 
94 class USBDeviceInfoData : public DeviceInfoData {
95 public:
96     virtual ~USBDeviceInfoData() = default;
97 
98     bool Marshalling(MessageParcel &parcel) const override;
99     static std::shared_ptr<USBDeviceInfoData> UnMarshalling(MessageParcel &parcel);
100 
101     uint16_t productId;
102     uint16_t vendorId;
103     std::vector<std::shared_ptr<USBInterfaceDesc>> interfaceDescList;
104 };
105 
106 class DriverInfoData {
107 public:
108     virtual ~DriverInfoData() = default;
109     virtual bool Marshalling(MessageParcel &parcel) const;
110     static std::shared_ptr<DriverInfoData> UnMarshalling(MessageParcel &parcel);
111     static bool DriverInfosUnMarshalling(MessageParcel &parcel,
112         std::vector<std::shared_ptr<DriverInfoData>> &driverInfos);
113 
114     BusType busType;
115     std::string driverUid;
116     std::string driverName;
117     std::string bundleSize;
118     std::string version;
119     std::string description;
120 };
121 
122 class USBDriverInfoData : public DriverInfoData {
123 public:
124     virtual ~USBDriverInfoData() = default;
125 
126     bool Marshalling(MessageParcel &parcel) const override;
127     static std::shared_ptr<USBDriverInfoData> UnMarshalling(MessageParcel &parcel);
128     std::vector<uint16_t> pids;
129     std::vector<uint16_t> vids;
130 };
131 } // namespace ExternalDeviceManager
132 } // namespace OHOS
133 #endif // DRIVER_EXTENSION_MANAGER_TYPES_H
134