• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 OHOS_HDI_USB_V1_2_USBD_FUNCTION_H
17 #define OHOS_HDI_USB_V1_2_USBD_FUNCTION_H
18 
19 #include <stdint.h>
20 #include <string>
21 
22 #include "osal_mutex.h"
23 
24 #define USB_FUNCTION_NONE    0
25 #define USB_FUNCTION_ACM     (1 << 0)
26 #define USB_FUNCTION_ECM     (1 << 1)
27 #define USB_FUNCTION_HDC     (1 << 2)
28 #define USB_FUNCTION_MTP     (1 << 3)
29 #define USB_FUNCTION_PTP     (1 << 4)
30 #define USB_FUNCTION_RNDIS   (1 << 5)
31 #define USB_FUNCTION_NCM     (1 << 8)
32 #define USB_FUNCTION_STORAGE (1 << 9)
33 #define USB_FUNCTION_MANUFACTURE (1 << 10)
34 #define USB_FUNCTION_ACCESSORY (1 << 11)
35 #define USB_FUNCTION_DEVMODE_AUTH (1 << 12)
36 #define USB_FUNCTION_SUPPORT                                                                        \
37     (USB_FUNCTION_ACM | USB_FUNCTION_ECM | USB_FUNCTION_HDC | USB_FUNCTION_MTP | USB_FUNCTION_PTP | \
38         USB_FUNCTION_RNDIS | USB_FUNCTION_STORAGE | USB_FUNCTION_MANUFACTURE | USB_FUNCTION_ACCESSORY | \
39         USB_FUNCTION_NCM | USB_FUNCTION_DEVMODE_AUTH)
40 
41 #define DEV_SERVICE_NAME "usbfn"
42 #define ACM_SERVICE_NAME "usbfn_cdcacm"
43 #define ECM_SERVICE_NAME "usbfn_cdcecm"
44 
45 #define SYS_USB_CONFIGFS                "sys.usb.configfs"
46 #define SYS_USB_CONFIG                  "sys.usb.config"
47 #define SYS_USB_STATE                   "sys.usb.state"
48 #define PERSIST_SYS_USB_CONFIG          "persist.sys.usb.config"
49 #define HDC_CONFIG_OFF                  "none"
50 #define HDC_CONFIG_HDC                  "hdc"
51 #define HDC_CONFIG_ON                   "hdc_debug"
52 #define HDC_CONFIG_RNDIS                "rndis"
53 #define HDC_CONFIG_STORAGE              "storage"
54 #define HDC_CONFIG_RNDIS_HDC            "rndis_hdc"
55 #define HDC_CONFIG_STORAGE_HDC          "storage_hdc"
56 #define HDC_CONFIG_MANUFACTURE_HDC      "manufacture_hdc"
57 #define HDC_CONFIG_AOA                  "aoa"
58 #define HDC_CONFIG_NCM                  "ncm"
59 #define HDC_CONFIG_NCM_HDC              "ncm_hdc"
60 #define HDC_CONFIG_DEVMODE_AUTH         "devmode_auth"
61 #define HDC_CONFIGFS_OFF       "0"
62 #define HDC_CONFIGFS_ON        "1"
63 
64 #define FUNCTION_ADD 1
65 #define FUNCTION_DEL 2
66 
67 #define ACM_INIT        100
68 #define ACM_RELEASE     101
69 #define ECM_INIT        100
70 #define ECM_RELEASE     101
71 #define MTP_PTP_INIT    100
72 #define MTP_PTP_RELEASE 101
73 
74 #define USB_DDK_FUNCTION_SUPPORT (USB_FUNCTION_ACM | USB_FUNCTION_ECM | USB_FUNCTION_MTP | USB_FUNCTION_PTP)
75 #define HDC_READY_TIME           2000
76 
77 namespace OHOS {
78 namespace HDI {
79 namespace Usb {
80 namespace V1_2 {
81 class UsbdFunction {
82 public:
83     UsbdFunction() = default;
84     ~UsbdFunction() = default;
85     static void UsbdInitLock();
86     static void UsbdDestroyLock();
87     static int32_t UsbdSetFunction(uint32_t funcs);
88     static int32_t UsbdGetFunction();
89     static bool IsHdcOpen();
90     static int32_t UsbdUpdateFunction(uint32_t funcs);
91 
92 private:
93     static int32_t SendCmdToService(const char *name, int32_t cmd, unsigned char funcMask);
94     static int32_t RemoveHdc();
95     static int32_t AddHdc();
96     static int32_t SetFunctionToNone();
97     static int32_t SetFunctionToRndis();
98     static int32_t SetFunctionToStorage();
99     static int32_t SetFunctionToRndisHdc();
100     static int32_t SetFunctionToStorageHdc();
101     static int32_t SetFunctionToManufactureHdc();
102     static int32_t SetFunctionToUsbAccessory();
103     static int32_t SetFunctionToNcm();
104     static int32_t SetFunctionToNcmHdc();
105     static int32_t SetFunctionToDevModeAuth();
106     static int32_t SetDDKFunction(uint32_t funcs);
107     static int32_t UsbdEnableDevice(int32_t funcs);
108     static int32_t UsbdWaitUdc();
109     static int32_t UsbdWaitToNone();
110     static int32_t UsbdInitDDKFunction(uint32_t funcs);
111     static int32_t UsbdSetKernelFunction(int32_t kfuns, int32_t funcs);
112     static int32_t UsbdReadUdc(char* udcName, size_t len);
113     static int32_t UsbdWriteUdc(char* udcName, size_t len);
114     static void UsbdUnregisterDevice(const std::string &serviceName);
115     static int32_t UsbdRegisterDevice(const std::string &serviceName);
116     static int32_t InitMtp();
117     static int32_t ReleaseMtp();
118     static int32_t UsbdInnerSetFunction(uint32_t funcs);
119 
120     static uint32_t currentFuncs_;
121     static OsalMutex setFunctionLock_;
122 };
123 } // namespace V1_2
124 } // namespace Usb
125 } // namespace HDI
126 } // namespace OHOS
127 #endif // OHOS_HDI_USB_V1_2_USBD_FUNCTION_H
128