• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "ddk_uevent_handle.h"
16 
17 #include <string.h>
18 #include <unistd.h>
19 #include <pthread.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <linux/netlink.h>
23 
24 #include "ddk_pnp_listener_mgr.h"
25 #include "default_config.h"
26 #include "hdf_base.h"
27 #include "hdf_log.h"
28 #include "securec.h"
29 #include "usbfn_uevent_handle.h"
30 
31 #define UEVENT_MSG_LEN          2048
32 #define UEVENT_SOCKET_GROUPS    0xffffffff
33 #define UEVENT_SOCKET_BUFF_SIZE (64 * 1024)
34 #define TIMEVAL_SECOND          0
35 #define TIMEVAL_USECOND         (100 * 1000)
36 
37 #define HDF_LOG_TAG usbfn_uevent
38 
39 #ifndef USB_GADGET_UEVENT_PATH
40 #define USB_GADGET_UEVENT_PATH "invalid_path"
41 #endif
42 
43 struct UsbFnUeventInfo {
44     const char *devPath;
45     const char *subSystem;
46     const char *usbState;
47     const char *dualRoleMode;
48 };
49 
UsbFnDispatchUevent(const struct UsbFnUeventInfo * info)50 static void UsbFnDispatchUevent(const struct UsbFnUeventInfo *info)
51 {
52     bool isGadgetConnect = strcmp(info->devPath, USB_GADGET_UEVENT_PATH) == 0;
53         isGadgetConnect = isGadgetConnect && strcmp(info->usbState, "CONNECTED") == 0;
54     if (isGadgetConnect) {
55         DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_GADGET_ADD);
56         return;
57     }
58 
59     bool isGadgetDisconnect = strcmp(info->devPath, USB_GADGET_UEVENT_PATH) == 0;
60         isGadgetDisconnect = isGadgetDisconnect && strcmp(info->usbState, "DISCONNECTED") == 0;
61     if (isGadgetDisconnect) {
62         DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_GADGET_REMOVE);
63         return;
64     }
65 
66     bool isPort2Device = strcmp(info->subSystem, "dual_role_usb") == 0;
67         isPort2Device = isPort2Device && strcmp(info->dualRoleMode, "ufp") == 0;
68     if (isPort2Device) {
69         DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_PORT_DEVICE);
70         return;
71     }
72 
73     bool isPort2Host = strcmp(info->subSystem, "dual_role_usb") == 0;
74         isPort2Host = isPort2Host && strcmp(info->dualRoleMode, "dfp") == 0;
75     if (isPort2Host) {
76         DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_PORT_HOST);
77         return;
78     }
79 }
80 
UsbFnHandleUevent(const char msg[],ssize_t rcvLen)81 void UsbFnHandleUevent(const char msg[], ssize_t rcvLen)
82 {
83     struct UsbFnUeventInfo info = {
84         .devPath = "",
85         .subSystem = "",
86         .usbState = "",
87         .dualRoleMode = "",
88     };
89 
90     const char *msgTmp = msg;
91     while (*msgTmp && (msgTmp - msg < rcvLen)) {
92         if (strncmp(msgTmp, "DEVPATH=", strlen("DEVPATH=")) == 0) {
93             msgTmp += strlen("DEVPATH=");
94             info.devPath = msgTmp;
95         } else if (strncmp(msgTmp, "SUBSYSTEM=", strlen("SUBSYSTEM=")) == 0 &&
96             strlen(info.subSystem) == 0) { // some uevent has more than one SUBSYSTEM property
97             msgTmp += strlen("SUBSYSTEM=");
98             info.subSystem = msgTmp;
99         } else if (strncmp(msgTmp, "USB_STATE=", strlen("USB_STATE=")) == 0) {
100             msgTmp += strlen("USB_STATE=");
101             info.usbState = msgTmp;
102         } else if (strncmp(msgTmp, "DUAL_ROLE_MODE=", strlen("DUAL_ROLE_MODE=")) == 0) {
103             msgTmp += strlen("DUAL_ROLE_MODE=");
104             info.dualRoleMode = msgTmp;
105         }
106         msgTmp += strlen(msgTmp) + 1; // 1 is a skip character '\0'
107     }
108 
109     UsbFnDispatchUevent(&info);
110     return;
111 }