• 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 #include "dev_slinfo_list.h"
17 #include <pthread.h>
18 #include "securec.h"
19 #include "dev_slinfo_adpt.h"
20 #include "dev_slinfo_log.h"
21 
22 pthread_mutex_t g_mutex;
23 
InitList(void)24 struct DATASLListParams* InitList(void)
25 {
26     (void)pthread_mutex_lock(&g_mutex);
27     struct DATASLListParams *list = (struct DATASLListParams *)malloc(sizeof(struct DATASLListParams));
28     if (list == NULL) {
29         (void)pthread_mutex_unlock(&g_mutex);
30         return NULL;
31     }
32     list->next = list;
33     list->prev = list;
34     list->callbackParams = NULL;
35     (void)pthread_mutex_unlock(&g_mutex);
36     return list;
37 }
38 
UpdateListNode(struct DATASLListParams * newListNode,struct DATASLListParams * prevListNode,struct DATASLListParams * nextListNode)39 static void UpdateListNode(struct DATASLListParams *newListNode,
40     struct DATASLListParams *prevListNode, struct DATASLListParams *nextListNode)
41 {
42     nextListNode->prev = newListNode;
43     newListNode->next = nextListNode;
44     newListNode->prev = prevListNode;
45     prevListNode->next = newListNode;
46 }
47 
PushListNode(struct DATASLListParams * list,struct DATASLCallbackParams * callbackParams)48 int32_t PushListNode(struct DATASLListParams *list, struct DATASLCallbackParams *callbackParams)
49 {
50     (void)pthread_mutex_lock(&g_mutex);
51     struct DATASLListParams *newList = (struct DATASLListParams*)malloc(sizeof(struct DATASLListParams));
52     if (newList == NULL) {
53         (void)pthread_mutex_unlock(&g_mutex);
54         return DEVSL_ERR_OUT_OF_MEMORY;
55     }
56 
57     UpdateListNode(newList, list->prev, list);
58     newList->callbackParams = callbackParams;
59     (void)pthread_mutex_unlock(&g_mutex);
60     return DEVSL_SUCCESS;
61 }
62 
RemoveListNode(struct DATASLListParams * list,struct DATASLCallbackParams * callbackParams)63 void RemoveListNode(struct DATASLListParams *list,  struct DATASLCallbackParams *callbackParams)
64 {
65     (void)pthread_mutex_lock(&g_mutex);
66     struct DATASLListParams *pList = list->next;
67     while (pList != NULL && pList != list) {
68         if (CompareUdid(&(pList->callbackParams->queryParams), &(callbackParams->queryParams)) == DEVSL_SUCCESS) {
69             pList->prev->next = pList->next;
70             pList->next->prev = pList->prev;
71             if (pList->callbackParams != NULL) {
72                 free(pList->callbackParams);
73             }
74             free(pList);
75             break;
76         }
77         pList = pList->next;
78     }
79     (void)pthread_mutex_unlock(&g_mutex);
80 }
81 
ClearList(struct DATASLListParams * list)82 void ClearList(struct DATASLListParams *list)
83 {
84     (void)pthread_mutex_lock(&g_mutex);
85     if (list == NULL) {
86         (void)pthread_mutex_unlock(&g_mutex);
87         return;
88     }
89     struct DATASLListParams *pList = list->next;
90     while (pList != NULL && pList != list) {
91         struct DATASLListParams *delList = pList;
92         pList = pList->next;
93         if (delList->callbackParams != NULL) {
94             free(delList->callbackParams);
95         }
96         free(delList);
97     }
98     if (list->callbackParams != NULL) {
99         free(list->callbackParams);
100     }
101     free(list);
102     (void)pthread_mutex_unlock(&g_mutex);
103 }
104 
GetListLength(struct DATASLListParams * list)105 int32_t GetListLength(struct DATASLListParams *list)
106 {
107     (void)pthread_mutex_lock(&g_mutex);
108     struct DATASLListParams *pList = list->next;
109     int32_t listLength = 0;
110     while (pList != NULL && pList != list) {
111         listLength++;
112         pList = pList->next;
113     }
114     (void)pthread_mutex_unlock(&g_mutex);
115     return listLength;
116 }
117 
LookupCallback(struct DATASLListParams * list,DEVSLQueryParams * queryParams,int32_t result,uint32_t levelInfo)118 void LookupCallback(struct DATASLListParams *list, DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo)
119 {
120     struct DATASLCallbackParams tmpCallbackParams;
121     (void)memset_s(&tmpCallbackParams, sizeof(struct DATASLCallbackParams), 0, sizeof(struct DATASLCallbackParams));
122     int32_t ret = DEVSL_ERROR;
123     (void)pthread_mutex_lock(&g_mutex);
124     struct DATASLListParams *tmpCallback = list->next;
125     while (tmpCallback != NULL && tmpCallback != list) {
126         struct DATASLListParams *nextCallback = tmpCallback->next;
127         ret = CompareUdid(&(tmpCallback->callbackParams->queryParams), queryParams);
128         if (ret == DEVSL_SUCCESS) {
129             (void)memcpy_s(&tmpCallbackParams.queryParams, sizeof(DEVSLQueryParams),
130                 queryParams, sizeof(DEVSLQueryParams));
131             tmpCallbackParams.callback = tmpCallback->callbackParams->callback;
132             tmpCallback->prev->next = tmpCallback->next;
133             tmpCallback->next->prev = tmpCallback->prev;
134             if (tmpCallback->callbackParams != NULL) {
135                 free(tmpCallback->callbackParams);
136             }
137             free(tmpCallback);
138             break;
139         }
140         tmpCallback = nextCallback;
141     }
142     (void)pthread_mutex_unlock(&g_mutex);
143     if (ret == DEVSL_SUCCESS) {
144         tmpCallbackParams.callback(&(tmpCallbackParams.queryParams), result, levelInfo);
145     }
146 }
147 
InitPthreadMutex(void)148 int32_t InitPthreadMutex(void)
149 {
150     int32_t ret;
151     ret = pthread_mutex_init(&g_mutex, NULL);
152     return ret;
153 }
154 
DestroyPthreadMutex(void)155 void DestroyPthreadMutex(void)
156 {
157     pthread_mutex_destroy(&g_mutex);
158 }