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 
30 #define USB_FUNCTION_MTP     (1 << 3)
31 #define USB_FUNCTION_PTP     (1 << 4)
32 #define USB_FUNCTION_STORAGE     (1 << 9)
33 namespace OHOS {
34 namespace USB {
35 class UsbDeviceManager {
36 public:
37     UsbDeviceManager();
38     ~UsbDeviceManager();
39     int32_t Init();
40     static bool AreSettableFunctions(int32_t funcs);
41 
42     int32_t SetUsbd(const sptr<HDI::Usb::V1_0::IUsbInterface> &usbd);
43     static uint32_t ConvertFromString(std::string_view funcs);
44     static std::string ConvertToString(uint32_t func);
45     void UpdateFunctions(int32_t func);
46     int32_t GetCurrentFunctions();
47     void HandleEvent(int32_t status);
48     void GetDumpHelp(int32_t fd);
49     void Dump(int32_t fd, const std::vector<std::string> &args);
50     bool IsGadgetConnected(void);
51     int32_t UserChangeProcess();
52 private:
53     void ProcessFunctionSwitchWindow(bool connected);
54     void DumpGetSupportFunc(int32_t fd);
55     void DumpSetFunc(int32_t fd, const std::string &args);
56     void ReportFuncChangeSysEvent(int32_t currentFunctions, int32_t updateFunctions);
57     void ReportDevicePlugSysEvent(int32_t currentFunctions, bool connected);
58     void ProcessFuncChange(bool connected, int32_t currentFunc);
59     void BroadcastFuncChange(bool connected, int32_t currentFunc);
60     static constexpr uint32_t functionSettable_ = UsbSrvSupport::FUNCTION_HDC | UsbSrvSupport::FUNCTION_ACM |
61         UsbSrvSupport::FUNCTION_ECM | UsbSrvSupport::FUNCTION_MTP | UsbSrvSupport::FUNCTION_PTP |
62         UsbSrvSupport::FUNCTION_RNDIS | UsbSrvSupport::FUNCTION_STORAGE;
63     static const std::map<std::string_view, uint32_t> FUNCTION_MAPPING_N2C;
64     int32_t currentFunctions_ {UsbSrvSupport::FUNCTION_HDC};
65     bool connected_ {false};
66     bool gadgetConnected_ {false};
67     sptr<HDI::Usb::V1_0::IUsbInterface> usbd_ = nullptr;
68     Utils::Timer delayDisconn_ {"delayDisconnTimer"};
69     uint32_t delayDisconnTimerId_ {UINT32_MAX};
70 };
71 } // namespace USB
72 } // namespace OHOS
73 
74 #endif // USB_FUNCTION_MANAGER_H
75