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 HDF_LOGI("%{public}s: notify cmd:%{public}d, start.", __func__, cmd);
115 OsalMutexLock(&g_ddkListenerList.listMutex);
116 if (DListIsEmpty(&g_ddkListenerList.listenerList)) {
117 HDF_LOGI("%{public}s: the listenerList is empty.", __func__);
118 OsalMutexUnlock(&g_ddkListenerList.listMutex);
119 return;
120 }
121
122 struct HdfDevEventlistener *pos = NULL;
123 struct HdfDevEventlistener *tmp = NULL;
124 struct UsbDdkDeviceHanldePriv handlePriv = {.cmd = cmd};
125 DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_ddkListenerList.listenerList, struct HdfDevEventlistener, listNode) {
126 handlePriv.listener = pos;
127 if (DdkListenerMgrNotifyOne(device, &handlePriv) != HDF_SUCCESS) {
128 HDF_LOGW("%{public}s: notify failed cmd:%{public}d", __func__, cmd);
129 }
130 }
131
132 OsalMutexUnlock(&g_ddkListenerList.listMutex);
133 HDF_LOGD("%{public}s: notify cmd:%{public}d, end.", __func__, cmd);
134 }
135
DdkListenerMgrAdd(struct HdfDevEventlistener * listener)136 int32_t DdkListenerMgrAdd(struct HdfDevEventlistener *listener)
137 {
138 if (listener == NULL) {
139 HDF_LOGE("%{public}s: invalid param", __func__);
140 return HDF_ERR_INVALID_PARAM;
141 }
142
143 // notify all device to listener
144 struct UsbDdkDeviceHanldePriv handlePriv = {.listener = listener, .cmd = USB_PNP_NOTIFY_ADD_DEVICE};
145 struct UsbDdkDeviceHanldePriv handlePriv1 = {.listener = listener, .cmd = USB_PNP_DRIVER_GADGET_ADD};
146 if (DdkListenerMgrIsExists(listener)) {
147 HDF_LOGW("%{public}s: add listener repeatedly", __func__);
148 } else {
149 OsalMutexLock(&g_ddkListenerList.listMutex);
150 DListInsertTail(&listener->listNode, &g_ddkListenerList.listenerList);
151 OsalMutexUnlock(&g_ddkListenerList.listMutex);
152 }
153 int32_t ret = DdkDevMgrForEachDeviceSafe(DdkListenerMgrNotifyOne, (void *)&handlePriv);
154 if (ret != HDF_SUCCESS) {
155 HDF_LOGE("%{public}s:DdkDevMgrForEachDeviceSafe failed", __func__);
156 return ret;
157 }
158 ret = DdkDevMgrGetGadgetLinkStatusSafe(DdkListenerMgrNotifyGadgetOne, (void *)&handlePriv1);
159 if (ret != HDF_SUCCESS) {
160 HDF_LOGE("%{public}s:DdkDevMgrGetGadgetLinkStatusSafe failed", __func__);
161 return ret;
162 }
163 return ret;
164 }
165
DdkListenerMgrRemove(struct HdfDevEventlistener * listener)166 int32_t DdkListenerMgrRemove(struct HdfDevEventlistener *listener)
167 {
168 if (!DdkListenerMgrIsExists(listener)) {
169 HDF_LOGE("%{public}s: no listener", __func__);
170 return HDF_DEV_ERR_NO_DEVICE;
171 }
172
173 OsalMutexLock(&g_ddkListenerList.listMutex);
174 DListRemove(&listener->listNode);
175 OsalMutexUnlock(&g_ddkListenerList.listMutex);
176 return HDF_SUCCESS;
177 }
178
DdkListenerMgrInit(void)179 int32_t DdkListenerMgrInit(void)
180 {
181 if (g_ddkListenerList.isInit) {
182 return HDF_SUCCESS;
183 }
184
185 int32_t ret = OsalMutexInit(&g_ddkListenerList.listMutex);
186 if (ret != HDF_SUCCESS) {
187 HDF_LOGE("%{public}s: init mutex failed", __func__);
188 return HDF_FAILURE;
189 }
190
191 DListHeadInit(&g_ddkListenerList.listenerList);
192 g_ddkListenerList.isInit = true;
193 return HDF_SUCCESS;
194 }
195