• 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 
16 #include "ddk_pnp_listener_mgr.h"
17 
18 #include <ctype.h>
19 #include <dirent.h>
20 
21 #include "ddk_device_manager.h"
22 #include "hdf_base.h"
23 #include "hdf_dlist.h"
24 #include "hdf_log.h"
25 #include "hdf_usb_pnp_manage.h"
26 #include "osal_mem.h"
27 #include "osal_mutex.h"
28 #include "securec.h"
29 
30 #define HDF_LOG_TAG usb_ddk_listener_mgr
31 
32 struct UsbDdkListenerList {
33     bool isInit;
34     struct OsalMutex listMutex;
35     struct DListHead listenerList;
36 };
37 
38 struct UsbDdkDeviceHanldePriv {
39     struct HdfDevEventlistener *listener;
40     enum UsbPnpNotifyServiceCmd cmd;
41 };
42 
43 static struct UsbDdkListenerList g_ddkListenerList = {.isInit = false};
44 
DdkListenerMgrIsExists(const struct HdfDevEventlistener * listener)45 static bool DdkListenerMgrIsExists(const struct HdfDevEventlistener *listener)
46 {
47     OsalMutexLock(&g_ddkListenerList.listMutex);
48     if (DListIsEmpty(&g_ddkListenerList.listenerList)) {
49         HDF_LOGI("%{public}s: the listenerList is empty.", __func__);
50         OsalMutexUnlock(&g_ddkListenerList.listMutex);
51         return false;
52     }
53 
54     struct HdfDevEventlistener *pos = NULL;
55     struct HdfDevEventlistener *tmp = NULL;
56     bool findFlag = false;
57     DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_ddkListenerList.listenerList, struct HdfDevEventlistener, listNode) {
58         if (pos == listener) {
59             findFlag = true;
60             break;
61         }
62     }
63 
64     OsalMutexUnlock(&g_ddkListenerList.listMutex);
65     return findFlag;
66 }
67 
DdkListenerMgrNotifyOne(const struct UsbPnpNotifyMatchInfoTable * device,void * priv)68 static int32_t DdkListenerMgrNotifyOne(const struct UsbPnpNotifyMatchInfoTable *device, void *priv)
69 {
70     struct UsbDdkDeviceHanldePriv *handlePriv = priv;
71     const struct HdfDevEventlistener *listener = handlePriv->listener;
72     // pack device
73     struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
74     if (data == NULL) {
75         HDF_LOGE("%{public}s: get buf failed", __func__);
76         return HDF_DEV_ERR_NO_MEMORY;
77     }
78 
79     int32_t ret = HDF_SUCCESS;
80     do {
81         struct HdfSBuf *dataTmp = NULL;
82         if (device != NULL) {
83             if (!HdfSbufWriteBuffer(data, device, sizeof(struct UsbPnpNotifyMatchInfoTable))) {
84                 HDF_LOGE("%{public}s: write buf failed", __func__);
85                 ret = HDF_FAILURE;
86                 break;
87             }
88             dataTmp = data;
89         }
90 
91         if (listener->callBack(listener->priv, handlePriv->cmd, dataTmp) != HDF_SUCCESS) {
92             HDF_LOGE("%{public}s:callback failed", __func__);
93             ret = HDF_FAILURE;
94         }
95     } while (0);
96 
97     HdfSbufRecycle(data);
98     return ret;
99 }
100 
DdkListenerMgrNotifyGadgetOne(void * priv)101 static int32_t DdkListenerMgrNotifyGadgetOne(void *priv)
102 {
103     struct UsbDdkDeviceHanldePriv *handlePriv = (struct UsbDdkDeviceHanldePriv *)priv;
104     const struct HdfDevEventlistener *listener = handlePriv->listener;
105     if (listener->callBack(listener->priv, handlePriv->cmd, NULL) != HDF_SUCCESS) {
106         HDF_LOGE("%{public}s:callback failed", __func__);
107         return HDF_FAILURE;
108     }
109     return HDF_SUCCESS;
110 }
111 
DdkListenerMgrNotifyAll(const struct UsbPnpNotifyMatchInfoTable * device,enum UsbPnpNotifyServiceCmd cmd)112 void DdkListenerMgrNotifyAll(const struct UsbPnpNotifyMatchInfoTable *device, enum UsbPnpNotifyServiceCmd cmd)
113 {
114     OsalMutexLock(&g_ddkListenerList.listMutex);
115     if (DListIsEmpty(&g_ddkListenerList.listenerList)) {
116         HDF_LOGI("%{public}s: the listenerList is empty.", __func__);
117         OsalMutexUnlock(&g_ddkListenerList.listMutex);
118         return;
119     }
120 
121     struct HdfDevEventlistener *pos = NULL;
122     struct HdfDevEventlistener *tmp = NULL;
123     struct UsbDdkDeviceHanldePriv handlePriv = {.cmd = cmd};
124     DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_ddkListenerList.listenerList, struct HdfDevEventlistener, listNode) {
125         handlePriv.listener = pos;
126         if (DdkListenerMgrNotifyOne(device, &handlePriv) != HDF_SUCCESS) {
127             HDF_LOGW("%{public}s: notify failed cmd:%{public}d", __func__, cmd);
128         }
129     }
130 
131     OsalMutexUnlock(&g_ddkListenerList.listMutex);
132 }
133 
DdkListenerMgrAdd(struct HdfDevEventlistener * listener)134 int32_t DdkListenerMgrAdd(struct HdfDevEventlistener *listener)
135 {
136     if (listener == NULL) {
137         HDF_LOGE("%{public}s: invalid param", __func__);
138         return HDF_ERR_INVALID_PARAM;
139     }
140 
141     // notify all device to listener
142     struct UsbDdkDeviceHanldePriv handlePriv = {.listener = listener, .cmd = USB_PNP_NOTIFY_ADD_DEVICE};
143     struct UsbDdkDeviceHanldePriv handlePriv1 = {.listener = listener, .cmd = USB_PNP_DRIVER_GADGET_ADD};
144     if (DdkListenerMgrIsExists(listener)) {
145         HDF_LOGW("%{public}s: add listener repeatedly", __func__);
146     } else {
147         OsalMutexLock(&g_ddkListenerList.listMutex);
148         DListInsertTail(&listener->listNode, &g_ddkListenerList.listenerList);
149         OsalMutexUnlock(&g_ddkListenerList.listMutex);
150     }
151     int32_t ret = DdkDevMgrForEachDeviceSafe(DdkListenerMgrNotifyOne, (void *)&handlePriv);
152     if (ret != HDF_SUCCESS) {
153         HDF_LOGE("%{public}s:DdkDevMgrForEachDeviceSafe failed", __func__);
154         return ret;
155     }
156     ret = DdkDevMgrGetGadgetLinkStatusSafe(DdkListenerMgrNotifyGadgetOne, (void *)&handlePriv1);
157     if (ret != HDF_SUCCESS) {
158         HDF_LOGE("%{public}s:DdkDevMgrGetGadgetLinkStatusSafe failed", __func__);
159         return ret;
160     }
161     return ret;
162 }
163 
DdkListenerMgrRemove(struct HdfDevEventlistener * listener)164 int32_t DdkListenerMgrRemove(struct HdfDevEventlistener *listener)
165 {
166     if (!DdkListenerMgrIsExists(listener)) {
167         HDF_LOGE("%{public}s: no listener", __func__);
168         return HDF_DEV_ERR_NO_DEVICE;
169     }
170 
171     OsalMutexLock(&g_ddkListenerList.listMutex);
172     DListRemove(&listener->listNode);
173     OsalMutexUnlock(&g_ddkListenerList.listMutex);
174     return HDF_SUCCESS;
175 }
176 
DdkListenerMgrInit(void)177 int32_t DdkListenerMgrInit(void)
178 {
179     if (g_ddkListenerList.isInit) {
180         return HDF_SUCCESS;
181     }
182 
183     int32_t ret = OsalMutexInit(&g_ddkListenerList.listMutex);
184     if (ret != HDF_SUCCESS) {
185         HDF_LOGE("%{public}s: init mutex failed", __func__);
186         return HDF_FAILURE;
187     }
188 
189     DListHeadInit(&g_ddkListenerList.listenerList);
190     g_ddkListenerList.isInit = true;
191     return HDF_SUCCESS;
192 }
193