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 "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 gMutex;
23
InitList(void)24 struct DATASLListParams* InitList(void)
25 {
26 pthread_mutex_lock(&gMutex);
27 struct DATASLListParams *list = (struct DATASLListParams *)malloc(sizeof(struct DATASLListParams));
28 if (list == NULL) {
29 pthread_mutex_unlock(&gMutex);
30 return NULL;
31 }
32 list->next = list;
33 list->prev = list;
34 list->callbackParams = NULL;
35 pthread_mutex_unlock(&gMutex);
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 pthread_mutex_lock(&gMutex);
51 struct DATASLListParams *newList = (struct DATASLListParams*)malloc(sizeof(struct DATASLListParams));
52 if (newList == NULL) {
53 pthread_mutex_unlock(&gMutex);
54 return DEVSL_ERR_OUT_OF_MEMORY;
55 }
56
57 UpdateListNode(newList, list->prev, list);
58 newList->callbackParams = callbackParams;
59 pthread_mutex_unlock(&gMutex);
60 return DEVSL_SUCCESS;
61 }
62
RemoveListNode(struct DATASLListParams * list,struct DATASLCallbackParams * callbackParams)63 void RemoveListNode(struct DATASLListParams *list, struct DATASLCallbackParams *callbackParams)
64 {
65 pthread_mutex_lock(&gMutex);
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 if (pList != NULL) {
75 free(pList);
76 }
77 break;
78 }
79 pList = pList->next;
80 }
81 pthread_mutex_unlock(&gMutex);
82 }
83
ClearList(struct DATASLListParams * list)84 void ClearList(struct DATASLListParams *list)
85 {
86 pthread_mutex_lock(&gMutex);
87 if (list == NULL) {
88 pthread_mutex_unlock(&gMutex);
89 return;
90 }
91 struct DATASLListParams *pList = list->next;
92 while (pList != NULL && pList != list) {
93 struct DATASLListParams *delList = pList;
94 pList = pList->next;
95 if (delList->callbackParams != NULL) {
96 free(delList->callbackParams);
97 }
98 if (delList != NULL) {
99 free(delList);
100 }
101 }
102 if (list->callbackParams != NULL) {
103 free(list->callbackParams);
104 }
105 free(list);
106 pthread_mutex_unlock(&gMutex);
107 }
108
GetListLength(struct DATASLListParams * list)109 int32_t GetListLength(struct DATASLListParams *list)
110 {
111 pthread_mutex_lock(&gMutex);
112 struct DATASLListParams *pList = list->next;
113 int32_t listLength = 0;
114 while (pList != NULL && pList != list) {
115 listLength++;
116 pList = pList->next;
117 }
118 pthread_mutex_unlock(&gMutex);
119 return listLength;
120 }
121
FindListNode(struct DATASLListParams * list,struct DATASLCallbackParams * callbackParams)122 int32_t FindListNode(struct DATASLListParams *list, struct DATASLCallbackParams *callbackParams)
123 {
124 pthread_mutex_lock(&gMutex);
125 struct DATASLListParams *pList = list->next;
126 while (pList != NULL && pList != list) {
127 if (CompareUdid(&(pList->callbackParams->queryParams), &(callbackParams->queryParams)) == DEVSL_SUCCESS) {
128 pthread_mutex_unlock(&gMutex);
129 return DEVSL_SUCCESS;
130 }
131 pList = pList->next;
132 }
133 pthread_mutex_unlock(&gMutex);
134 return DEVSL_ERROR;
135 }
136
LookupCallback(struct DATASLListParams * list,DEVSLQueryParams * queryParams,int32_t result,uint32_t levelInfo)137 void LookupCallback(struct DATASLListParams *list, DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo)
138 {
139 struct DATASLCallbackParams tmpCallbackParams;
140 (void)memset_s(&tmpCallbackParams, sizeof(struct DATASLCallbackParams), 0, sizeof(struct DATASLCallbackParams));
141 int32_t ret = DEVSL_ERROR;
142 pthread_mutex_lock(&gMutex);
143 struct DATASLListParams *tmpCallback = list->next;
144 while (tmpCallback != NULL && tmpCallback != list) {
145 struct DATASLListParams *nextCallback = tmpCallback->next;
146 ret = CompareUdid(&(tmpCallback->callbackParams->queryParams), queryParams);
147 if (ret == DEVSL_SUCCESS) {
148 (void)memcpy_s(&tmpCallbackParams.queryParams, sizeof(DEVSLQueryParams),
149 queryParams, sizeof(DEVSLQueryParams));
150 tmpCallbackParams.callback = tmpCallback->callbackParams->callback;
151 tmpCallback->prev->next = tmpCallback->next;
152 tmpCallback->next->prev = tmpCallback->prev;
153 if (tmpCallback->callbackParams != NULL) {
154 free(tmpCallback->callbackParams);
155 }
156 if (tmpCallback != NULL) {
157 free(tmpCallback);
158 }
159 break;
160 }
161 tmpCallback = nextCallback;
162 }
163 pthread_mutex_unlock(&gMutex);
164 if (ret == DEVSL_SUCCESS) {
165 tmpCallbackParams.callback(&(tmpCallbackParams.queryParams), result, levelInfo);
166 }
167 }
168
InitPthreadMutex(void)169 int32_t InitPthreadMutex(void)
170 {
171 int32_t ret;
172 ret = pthread_mutex_init(&gMutex, NULL);
173 return ret;
174 }
175
DestroyPthreadMutex(void)176 void DestroyPthreadMutex(void)
177 {
178 pthread_mutex_destroy(&gMutex);
179 }