• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 = "",
userId_(userId)42         const int32_t userId = -1) : userId_(userId), 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     }
GetUserId()64     int32_t GetUserId() const
65     {
66         return userId_;
67     }
GetBundleName()68     std::string GetBundleName() const
69     {
70         return bundleName_;
71     }
GetDriverName()72     std::string GetDriverName() const
73     {
74         return driverName_;
75     }
GetVersion()76     std::string GetVersion() const
77     {
78         return version_;
79     }
GetDescription()80     std::string GetDescription() const
81     {
82         return description_;
83     }
GetDriverSize()84     std::string GetDriverSize() const
85     {
86         return driverSize_;
87     }
GetLaunchOnBind()88     bool GetLaunchOnBind() const
89     {
90         return launchOnBind_;
91     }
GetInfoExt()92     std::shared_ptr<DriverInfoExt> GetInfoExt() const
93     {
94         return driverInfoExt_;
95     }
IsAccessAllowed()96     bool IsAccessAllowed() const
97     {
98         return accessAllowed_;
99     }
100 private:
101     friend class DrvBundleStateCallback;
102     std::string bus_;
103     BusType busType_{0};
104     std::string driverUid_;
105     int32_t userId_ = -1;
106     std::string bundleName_;
107     std::string driverName_;
108     std::string vendor_;
109     std::string version_;
110     std::string description_;
111     std::string driverSize_;
112     bool launchOnBind_ = false;
113     bool accessAllowed_ = false;
114     std::shared_ptr<DriverInfoExt> driverInfoExt_;
115 };
116 
117 class DeviceInfo {
118 public:
119     DeviceInfo(
120         uint32_t busDeviceId,
121         BusType busType = BusType::BUS_TYPE_INVALID,
description_(description)122         const std::string &description = "") : description_(description)
123     {
124         devInfo_.devBusInfo.busType = busType;
125         devInfo_.devBusInfo.busDeviceId = busDeviceId;
126     }
127     virtual ~DeviceInfo() = default;
GetBusType()128     BusType GetBusType() const
129     {
130         return devInfo_.devBusInfo.busType;
131     }
GetDeviceId()132     uint64_t GetDeviceId() const
133     {
134         return devInfo_.deviceId;
135     }
GetBusDevId()136     uint32_t GetBusDevId() const
137     {
138         return devInfo_.devBusInfo.busDeviceId;
139     }
GetDeviceDescription()140     const std::string& GetDeviceDescription() const
141     {
142         return description_;
143     }
144 
145 private:
146     union DevInfo {
147         uint64_t deviceId;
148         struct {
149             BusType busType;
150             uint32_t busDeviceId;
151         } devBusInfo;
152     } devInfo_;
153     std::string description_ {""};
154 };
155 } // namespace ExternalDeviceManager
156 } // namespace OHOS
157 #endif // EXT_OBJECT_H