• 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 #ifndef USB_FUNCTION_MANAGER_H
17 #define USB_FUNCTION_MANAGER_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include "timer.h"
23 
24 #include "usb_common.h"
25 #include "usb_function_switch_window.h"
26 #include "usb_srv_support.h"
27 #include "v1_0/iusb_interface.h"
28 #include "v1_0/iusbd_subscriber.h"
29 #ifdef USB_MANAGER_V2_0
30 #include "usb_manager_subscriber.h"
31 #include "v2_0/iusb_device_interface.h"
32 #include "mem_mgr_proxy.h"
33 #include "mem_mgr_client.h"
34 #include "system_ability_definition.h"
35 #endif // USB_MANAGER_V2_0
36 
37 #define USB_FUNCTION_HDC     (1 << 2)
38 #define USB_FUNCTION_MTP     (1 << 3)
39 #define USB_FUNCTION_PTP     (1 << 4)
40 #define USB_FUNCTION_STORAGE     (1 << 9)
41 namespace OHOS {
42 namespace USB {
43 class UsbDeviceManager {
44 public:
45 #ifdef USB_MANAGER_V2_0
46     bool InitUsbDeviceInterface();
47     void Stop();
48     int32_t BindUsbdSubscriber(const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber);
49     int32_t UnbindUsbdSubscriber(const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber);
50 #endif // USB_MANAGER_V2_0
51     UsbDeviceManager();
52     ~UsbDeviceManager();
53     int32_t Init();
54     static bool IsSettableFunctions(int32_t funcs);
55 
56     int32_t SetUsbd(const sptr<HDI::Usb::V1_0::IUsbInterface> &usbd);
57     static uint32_t ConvertFromString(std::string_view funcs);
58     static std::string ConvertToString(uint32_t func);
59     void UpdateFunctions(int32_t func);
60     int32_t GetCurrentFunctions();
61     void HandleEvent(int32_t status);
62     void GetDumpHelp(int32_t fd);
63     void Dump(int32_t fd, const std::vector<std::string> &args);
64     bool IsGadgetConnected(void);
65     int32_t UserChangeProcess();
66     int32_t GetCurrentFunctions(int32_t& funcs);
67     int32_t SetCurrentFunctions(int32_t funcs);
68 private:
69     void ProcessFunctionSwitchWindow(bool connected);
70     void ProcessFunctionNotifier(bool connected, int32_t func);
71     void DumpGetSupportFunc(int32_t fd);
72     void DumpSetFunc(int32_t fd, const std::string &args);
73     void ReportFuncChangeSysEvent(int32_t currentFunctions, int32_t updateFunctions);
74     void ReportDevicePlugSysEvent(int32_t currentFunctions, bool connected);
75     void ProcessFuncChange(bool connected, int32_t currentFunc, bool isDisableDialog = false);
76     void BroadcastFuncChange(bool connected, int32_t currentFunc);
77     void ProcessStatus(int32_t status, bool &curConnect);
78     static constexpr uint32_t functionSettable_ = UsbSrvSupport::FUNCTION_HDC | UsbSrvSupport::FUNCTION_ACM |
79         UsbSrvSupport::FUNCTION_ECM | UsbSrvSupport::FUNCTION_MTP | UsbSrvSupport::FUNCTION_PTP |
80         UsbSrvSupport::FUNCTION_RNDIS | UsbSrvSupport::FUNCTION_NCM | UsbSrvSupport::FUNCTION_STORAGE;
81     static const std::map<std::string_view, uint32_t> FUNCTION_MAPPING_N2C;
82     int32_t currentFunctions_ {UsbSrvSupport::FUNCTION_HDC};
83     bool connected_ {false};
84     bool hasHdcNotifier_ {false};
85     bool gadgetConnected_ {false};
86     bool isDisableDialog_ {false};
87     sptr<HDI::Usb::V1_0::IUsbInterface> usbd_ = nullptr;
88     Utils::Timer delayDisconn_ {"delayDisconnTimer"};
89     uint32_t delayDisconnTimerId_ {UINT32_MAX};
90     uint32_t delayAccTimerId_ {UINT32_MAX};
91     std::mutex functionMutex_;
92 #ifdef USB_MANAGER_V2_0
93     sptr<HDI::Usb::V2_0::IUsbDeviceInterface> usbDeviceInterface_ = nullptr;
94     sptr<UsbManagerSubscriber> usbManagerSubscriber_;
95 #endif // USB_MANAGER_V2_0
96 };
97 } // namespace USB
98 } // namespace OHOS
99 
100 #endif // USB_FUNCTION_MANAGER_H
101