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 #ifndef EXT_OBJECT_H
16 #define EXT_OBJECT_H
17 
18 #include <memory>
19 #include <string>
20 #include "edm_errors.h"
21 namespace OHOS {
22 namespace ExternalDeviceManager {
23 enum BusType : uint32_t {
24     BUS_TYPE_INVALID = 0,
25     BUS_TYPE_USB = 1,
26     BUS_TYPE_MAX,
27     BUS_TYPE_TEST,
28 };
29 
30 class DrvBundleStateCallback;
31 class DriverInfoExt {
32 public:
33     virtual ~DriverInfoExt() = default;
34     virtual int32_t Serialize(std::string &str) = 0;
35     virtual int32_t UnSerialize(const std::string &str) = 0;
36 };
37 
38 class DriverInfo : public DriverInfoExt {
39 public:
40     DriverInfo() = default;
41     DriverInfo(const std::string &bundleName, const std::string &driverName, const std::string &driverUid = "")
42         : bundleName_(bundleName), driverName_(driverName)
43     {
44         if (driverUid.empty()) {
45             driverUid_ = bundleName + "-" + driverName;
46         } else {
47             driverUid_ = driverUid;
48         }
49     }
50     int32_t Serialize(std::string &str) override;
51     int32_t UnSerialize(const std::string &str) override;
GetBusName()52     std::string GetBusName() const
53     {
54         return bus_;
55     }
GetBusType()56     BusType GetBusType() const
57     {
58         return busType_;
59     }
GetDriverUid()60     std::string GetDriverUid() const
61     {
62         return driverUid_;
63     }
GetBundleName()64     std::string GetBundleName() const
65     {
66         return bundleName_;
67     }
GetDriverName()68     std::string GetDriverName() const
69     {
70         return driverName_;
71     }
GetVersion()72     std::string GetVersion() const
73     {
74         return version_;
75     }
GetDescription()76     std::string GetDescription() const
77     {
78         return description_;
79     }
GetDriverSize()80     std::string GetDriverSize() const
81     {
82         return driverSize_;
83     }
GetInfoExt()84     std::shared_ptr<DriverInfoExt> GetInfoExt() const
85     {
86         return driverInfoExt_;
87     }
88 private:
89     friend class DrvBundleStateCallback;
90     std::string bus_;
91     BusType busType_;
92     std::string driverUid_;
93     std::string bundleName_;
94     std::string driverName_;
95     std::string vendor_;
96     std::string version_;
97     std::string description_;
98     std::string driverSize_;
99     std::shared_ptr<DriverInfoExt> driverInfoExt_;
100 };
101 
102 class DeviceInfo {
103 public:
104     DeviceInfo(
105         uint32_t busDeviceId,
106         BusType busType = BusType::BUS_TYPE_INVALID,
107         const std::string &description = "") : description_(description)
108     {
109         devInfo_.devBusInfo.busType = busType;
110         devInfo_.devBusInfo.busDeviceId = busDeviceId;
111     }
112     virtual ~DeviceInfo() = default;
GetBusType()113     BusType GetBusType() const
114     {
115         return devInfo_.devBusInfo.busType;
116     }
GetDeviceId()117     uint64_t GetDeviceId() const
118     {
119         return devInfo_.deviceId;
120     }
GetBusDevId()121     uint32_t GetBusDevId() const
122     {
123         return devInfo_.devBusInfo.busDeviceId;
124     }
GetDeviceDescription()125     const std::string& GetDeviceDescription() const
126     {
127         return description_;
128     }
129 
130 private:
131     union DevInfo {
132         uint64_t deviceId;
133         struct {
134             BusType busType;
135             uint32_t busDeviceId;
136         } devBusInfo;
137     } devInfo_;
138     std::string description_ {""};
139 };
140 } // namespace ExternalDeviceManager
141 } // namespace OHOS
142 #endif // EXT_OBJECT_H