• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 USB_CONFIG_H
17 #define USB_CONFIG_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <sstream>
23 #include <vector>
24 #include "usb_common.h"
25 #include "usb_interface.h"
26 
27 namespace OHOS {
28 namespace USB {
29 class USBConfig {
30 public:
USBConfig(uint32_t id,uint32_t attributes,std::string name,uint32_t maxPower,std::vector<UsbInterface> interfaces)31     USBConfig(uint32_t id, uint32_t attributes, std::string name, uint32_t maxPower,
32         std::vector<UsbInterface> interfaces)
33     {
34         this->id_ = static_cast<int32_t>(id);
35         this->attributes_ = attributes;
36         this->maxPower_ = static_cast<int32_t>(maxPower);
37         this->name_ = name;
38         this->interfaces_ = interfaces;
39     }
40 
USBConfig(const Json::Value & config)41     explicit USBConfig(const Json::Value &config)
42     {
43         id_ = config["id"].asInt();
44         attributes_ = config["attributes"].asUInt();
45         maxPower_ = config["maxPower"].asInt();
46         name_ = config["name"].asString();
47 
48         Json::Value interfaces = config["interfaces"];
49         for (uint32_t idx = 0; idx < interfaces.size(); ++idx) {
50             interfaces_.emplace_back(interfaces[idx]);
51         }
52     }
53 
USBConfig()54     USBConfig() {}
~USBConfig()55     ~USBConfig() {}
56 
GetId()57     const int32_t &GetId() const
58     {
59         return id_;
60     }
61 
GetAttributes()62     const uint32_t &GetAttributes() const
63     {
64         return attributes_;
65     }
66 
GetInterface(uint32_t index,UsbInterface & interface)67     bool GetInterface(uint32_t index, UsbInterface &interface) const
68     {
69         if (index >= interfaces_.size()) {
70             return false;
71         }
72         interface = interfaces_[index];
73         return true;
74     }
75 
GetInterfaceCount()76     uint32_t GetInterfaceCount() const
77     {
78         return interfaces_.size();
79     }
80 
GetMaxPower()81     int32_t GetMaxPower() const
82     {
83         // 2 represent maxPower units
84         return maxPower_ * 2;
85     }
86 
GetName()87     const std::string &GetName() const
88     {
89         return name_;
90     }
91 
IsRemoteWakeup()92     bool IsRemoteWakeup() const
93     {
94         return (attributes_ & USB_CFG_REMOTE_WAKEUP) != 0;
95     }
96 
IsSelfPowered()97     bool IsSelfPowered() const
98     {
99         return (attributes_ & USB_CFG_SELF_POWERED) != 0;
100     }
101 
SetInterfaces(const std::vector<UsbInterface> & interfaces)102     void SetInterfaces(const std::vector<UsbInterface> &interfaces)
103     {
104         this->interfaces_ = interfaces;
105     }
106 
GetInterfaces()107     std::vector<UsbInterface> &GetInterfaces()
108     {
109         return interfaces_;
110     }
111 
SetId(int32_t id)112     void SetId(int32_t id)
113     {
114         this->id_ = id;
115     }
116 
SetAttribute(uint32_t attributes)117     void SetAttribute(uint32_t attributes)
118     {
119         this->attributes_ = attributes;
120     }
121 
SetMaxPower(int32_t maxPower)122     void SetMaxPower(int32_t maxPower)
123     {
124         this->maxPower_ = maxPower;
125     }
126 
ToString()127     std::string ToString() const
128     {
129         std::ostringstream ss;
130         ss << "name=" << name_ << ","
131            << "id=" << id_ << ","
132            << "iConfiguration=" << (int32_t)iConfiguration_ << ","
133            << "attributes=" << attributes_ << ","
134            << "maxPower=" << maxPower_ << ";  ";
135         std::string str = "USBConfig[" + ss.str() + "]";
136         ss.str("");
137         for (size_t i = 0; i < interfaces_.size(); ++i) {
138             const UsbInterface &interface = interfaces_[i];
139             str += interface.ToString();
140         }
141         return str;
142     }
143 
SetName(const std::string & name)144     void SetName(const std::string &name)
145     {
146         this->name_ = name;
147     }
148 
SetiConfiguration(uint8_t idx)149     void SetiConfiguration(uint8_t idx)
150     {
151         this->iConfiguration_ = idx;
152     }
153 
GetiConfiguration()154     uint8_t GetiConfiguration()
155     {
156         return this->iConfiguration_;
157     }
158 
ToJson()159     Json::Value ToJson() const
160     {
161         Json::Value config;
162         config["id"] = id_;
163         config["attributes"] = attributes_;
164         config["maxPower"] = maxPower_;
165         config["name"] = name_;
166         config["isRemoteWakeup"] = IsRemoteWakeup();
167         config["isSelfPowered"] = IsSelfPowered();
168 
169         Json::Value interfaces;
170         for (auto &intf : interfaces_) {
171             interfaces.append(intf.ToJson());
172         }
173         config["interfaces"] = interfaces;
174 
175         return config;
176     }
177 
178 private:
179     int32_t id_ = INVALID_USB_INT_VALUE;
180     uint32_t attributes_ = 0;
181     std::vector<UsbInterface> interfaces_;
182     int32_t maxPower_ = INVALID_USB_INT_VALUE;
183     std::string name_;
184     uint8_t iConfiguration_ = UINT8_MAX;
185 };
186 } // namespace USB
187 } // namespace OHOS
188 
189 #endif // USB_CONFIG_H
190