1 /*
2  * Copyright (C) 2021 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 "nstackx_event.h"
17 #include "nstackx_log.h"
18 #include "securec.h"
19 
20 #define TAG "nStackXEvent"
21 
22 void DeleteEventNode(EventNode *node);
23 
SearchEventNode(const List * eventNodeChain,EpollDesc epollfd)24 EventNode *SearchEventNode(const List *eventNodeChain, EpollDesc epollfd)
25 {
26     List *pos = NULL;
27     List *tmp = NULL;
28     EventNode *node = NULL;
29 
30     LIST_FOR_EACH_SAFE(pos, tmp, eventNodeChain) {
31         node = (EventNode *)pos;
32         if (IsEpollDescEqual(node->epollfd, epollfd)) {
33             break;
34         }
35         node = NULL;
36     }
37     return node;
38 }
39 
EventModuleClean(const List * eventNodeChain,EpollDesc epollfd)40 void EventModuleClean(const List *eventNodeChain, EpollDesc epollfd)
41 {
42     List *pos = NULL;
43     EventNode *node = NULL;
44     if (eventNodeChain == NULL) {
45         LOGE(TAG, "eventNodeChain is null");
46         return;
47     }
48     LIST_FOR_EACH(pos, eventNodeChain) {
49         node = (EventNode *)pos;
50         if (IsEpollDescEqual(node->epollfd, epollfd)) {
51             break;
52         }
53         node = NULL;
54     }
55 
56     if (node == NULL) {
57         return;
58     }
59 
60     DeleteEventNode(node);
61 }
62 
EventNodeChainClean(List * eventNodeChain)63 void EventNodeChainClean(List *eventNodeChain)
64 {
65     List *tmp = NULL;
66     List *pos = NULL;
67     EventNode *node = NULL;
68 
69     if (eventNodeChain == NULL) {
70         LOGE(TAG, "eventNodeChain is null");
71         return;
72     }
73 
74     LIST_FOR_EACH_SAFE(pos, tmp, eventNodeChain) {
75         node = (EventNode *)pos;
76         if (node != NULL) {
77             DeleteEventNode(node);
78         }
79     }
80 }
81 
GetEpollTask(List * eventNodeChain,EpollDesc epollfd)82 EpollTask *GetEpollTask(List *eventNodeChain, EpollDesc epollfd)
83 {
84     if (eventNodeChain == NULL) {
85         LOGE(TAG, "eventNodeChain is null");
86         return NULL;
87     }
88 
89     EventNode *node = SearchEventNode(eventNodeChain, epollfd);
90     if (node == NULL) {
91         LOGE(TAG, "Cannot find event node for %d", REPRESENT_EPOLL_DESC(epollfd));
92         return NULL;
93     }
94     return &node->task;
95 }
96