• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "usb_device_manager.h"
17 #include "common_event_data.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "hisysevent.h"
21 #include "usb_errors.h"
22 #include "usb_srv_support.h"
23 #include "usbd_type.h"
24 
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::EventFwk;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::HDI::Usb::V1_0;
29 
30 namespace OHOS {
31 namespace USB {
32 const std::map<std::string_view, uint32_t> UsbDeviceManager::FUNCTION_MAPPING_N2C = {
33     {UsbSrvSupport::FUNCTION_NAME_NONE, UsbSrvSupport::FUNCTION_NONE},
34     {UsbSrvSupport::FUNCTION_NAME_ACM, UsbSrvSupport::FUNCTION_ACM},
35     {UsbSrvSupport::FUNCTION_NAME_ECM, UsbSrvSupport::FUNCTION_ECM},
36     {UsbSrvSupport::FUNCTION_NAME_HDC, UsbSrvSupport::FUNCTION_HDC},
37     {UsbSrvSupport::FUNCTION_NAME_MTP, UsbSrvSupport::FUNCTION_MTP},
38     {UsbSrvSupport::FUNCTION_NAME_PTP, UsbSrvSupport::FUNCTION_PTP},
39     {UsbSrvSupport::FUNCTION_NAME_RNDIS, UsbSrvSupport::FUNCTION_RNDIS},
40     {UsbSrvSupport::FUNCTION_NAME_STORAGE, UsbSrvSupport::FUNCTION_STORAGE},
41 };
42 
UsbDeviceManager()43 UsbDeviceManager::UsbDeviceManager()
44 {
45     USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceManager::Init start");
46     usbd_ = IUsbInterface::Get();
47     if (usbd_ == nullptr) {
48         USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::Get inteface failed");
49     }
50 }
51 
AreSettableFunctions(int32_t funcs)52 bool UsbDeviceManager::AreSettableFunctions(int32_t funcs)
53 {
54     return static_cast<uint32_t>(funcs) == UsbSrvSupport::FUNCTION_NONE ||
55         ((~functionSettable_ & static_cast<uint32_t>(funcs)) == 0);
56 }
57 
ConvertFromString(std::string_view strFun)58 uint32_t UsbDeviceManager::ConvertFromString(std::string_view strFun)
59 {
60     if (strFun == UsbSrvSupport::FUNCTION_NAME_NONE) {
61         return UsbSrvSupport::FUNCTION_NONE;
62     }
63 
64     size_t len = strFun.length();
65     if (len == 0) {
66         return UEC_SERVICE_INVALID_VALUE;
67     }
68 
69     std::vector<std::string_view> vModeStr;
70     size_t pos = 0;
71     while (pos < len) {
72         size_t findPos = strFun.find(",", pos);
73         if (findPos == strFun.npos) {
74             vModeStr.push_back(strFun.substr(pos, len - pos));
75             break;
76         }
77         vModeStr.push_back(strFun.substr(pos, findPos - pos));
78         pos = findPos + 1;
79     }
80 
81     uint32_t ret = 0;
82     for (auto &&item : vModeStr) {
83         auto it = FUNCTION_MAPPING_N2C.find(item);
84         if (it != FUNCTION_MAPPING_N2C.end()) {
85             ret |= it->second;
86         } else {
87             USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::ConvertFromString Invalid argument of usb function");
88             return UEC_SERVICE_INVALID_VALUE;
89         }
90     }
91 
92     return ret;
93 }
94 
ConvertToString(uint32_t function)95 std::string UsbDeviceManager::ConvertToString(uint32_t function)
96 {
97     std::string stream;
98     if (function <= UsbSrvSupport::FUNCTION_NONE || function > functionSettable_) {
99         stream = std::string {UsbSrvSupport::FUNCTION_NAME_NONE};
100         return stream;
101     }
102     bool flag = false;
103     for (auto it = FUNCTION_MAPPING_N2C.begin(); it != FUNCTION_MAPPING_N2C.end(); ++it) {
104         if ((function & it->second) != 0) {
105             if (flag) {
106                 stream += ",";
107             }
108             stream += std::string {it->first};
109             flag = true;
110         }
111     }
112     USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceManager::ConvertToString success");
113     return stream;
114 }
115 
UpdateFunctions(int32_t func)116 void UsbDeviceManager::UpdateFunctions(int32_t func)
117 {
118     ReportFuncChangeSysEvent(currentFunctions_, func);
119     currentFunctions_ = func;
120 }
121 
GetCurrentFunctions()122 int32_t UsbDeviceManager::GetCurrentFunctions()
123 {
124     return currentFunctions_;
125 }
126 
HandleEvent(int32_t status)127 void UsbDeviceManager::HandleEvent(int32_t status)
128 {
129     if (usbd_ == nullptr) {
130         USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::usbd_ is nullptr");
131         return;
132     }
133     switch (status) {
134         case ACT_UPDEVICE: {
135             connected_ = true;
136             usbd_->GetCurrentFunctions(currentFunctions_);
137             break;
138         }
139         case ACT_DOWNDEVICE: {
140             connected_ = false;
141             break;
142         }
143         default:
144             USB_HILOGE(MODULE_USB_SERVICE, "invalid status %{public}d", status);
145             return;
146     }
147 
148     Want want;
149     want.SetAction(CommonEventSupport::COMMON_EVENT_USB_STATE);
150 
151     want.SetParam(std::string {UsbSrvSupport::CONNECTED}, connected_);
152     uint32_t remainderFunc = static_cast<uint32_t>(currentFunctions_);
153     // start from bit 1
154     uint32_t bit = 1;
155     while (remainderFunc != 0) {
156         if (remainderFunc & bit) {
157             want.SetParam(ConvertToString(bit), true);
158             // set current bit to zero
159             remainderFunc &= ~bit;
160         }
161         // 1 means to next bit
162         bit = bit << 1;
163     }
164 
165     CommonEventData data(want);
166     CommonEventPublishInfo publishInfo;
167     USB_HILOGI(MODULE_SERVICE, "send COMMON_EVENT_USB_STATE broadcast connected:%{public}d, "
168         "currentFunctions:%{public}u", connected_, currentFunctions_);
169     CommonEventManager::PublishCommonEvent(data, publishInfo);
170     ReportDevicePlugSysEvent(currentFunctions_, connected_);
171 }
172 
Dump(int fd,const std::string & args)173 bool UsbDeviceManager::Dump(int fd, const std::string &args)
174 {
175     if (args.compare("-a") != 0) {
176         dprintf(fd, "args is not -a\n");
177         return false;
178     }
179 
180     dprintf(fd, "Usb Device function list info:\n");
181     dprintf(fd, "current function: %s\n", ConvertToString(currentFunctions_).c_str());
182     dprintf(fd, "supported functions list: %s\n", ConvertToString(functionSettable_).c_str());
183     return true;
184 }
185 
ReportFuncChangeSysEvent(int32_t currentFunctions,int32_t updateFunctions)186 void UsbDeviceManager::ReportFuncChangeSysEvent(int32_t currentFunctions, int32_t updateFunctions)
187 {
188     USB_HILOGI(MODULE_USB_SERVICE, "Device function Indicates the switch point information:");
189     HiSysEvent::Write("USB", "USB_FUNCTION_CHANGED", HiSysEvent::EventType::BEHAVIOR, "CURRENT_FUNCTION",
190         currentFunctions_, "UPDATE_FUNCTION", updateFunctions);
191 }
192 
ReportDevicePlugSysEvent(int32_t currentFunctions,bool connected)193 void UsbDeviceManager::ReportDevicePlugSysEvent(int32_t currentFunctions, bool connected)
194 {
195     USB_HILOGI(MODULE_USB_SERVICE, "Device mode Indicates the insertion and removal information:");
196     HiSysEvent::Write("USB", "USB_PLUG_IN_OUT_DEVICE_MODE", HiSysEvent::EventType::BEHAVIOR, "CURRENT_FUNCTIONS",
197         currentFunctions, "CONNECTED", connected);
198 }
199 } // namespace USB
200 } // namespace OHOS
201