• 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             if (pList != NULL) {
75                 free(pList);
76             }
77             break;
78         }
79         pList = pList->next;
80     }
81     (void)pthread_mutex_unlock(&g_mutex);
82 }
83 
ClearList(struct DATASLListParams * list)84 void ClearList(struct DATASLListParams *list)
85 {
86     (void)pthread_mutex_lock(&g_mutex);
87     if (list == NULL) {
88         (void)pthread_mutex_unlock(&g_mutex);
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     (void)pthread_mutex_unlock(&g_mutex);
107 }
108 
GetListLength(struct DATASLListParams * list)109 int32_t GetListLength(struct DATASLListParams *list)
110 {
111     (void)pthread_mutex_lock(&g_mutex);
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     (void)pthread_mutex_unlock(&g_mutex);
119     return listLength;
120 }
121 
LookupCallback(struct DATASLListParams * list,DEVSLQueryParams * queryParams,int32_t result,uint32_t levelInfo)122 void LookupCallback(struct DATASLListParams *list, DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo)
123 {
124     struct DATASLCallbackParams tmpCallbackParams;
125     (void)memset_s(&tmpCallbackParams, sizeof(struct DATASLCallbackParams), 0, sizeof(struct DATASLCallbackParams));
126     int32_t ret = DEVSL_ERROR;
127     (void)pthread_mutex_lock(&g_mutex);
128     struct DATASLListParams *tmpCallback = list->next;
129     while (tmpCallback != NULL && tmpCallback != list) {
130         struct DATASLListParams *nextCallback = tmpCallback->next;
131         ret = CompareUdid(&(tmpCallback->callbackParams->queryParams), queryParams);
132         if (ret == DEVSL_SUCCESS) {
133             (void)memcpy_s(&tmpCallbackParams.queryParams, sizeof(DEVSLQueryParams),
134                 queryParams, sizeof(DEVSLQueryParams));
135             tmpCallbackParams.callback = tmpCallback->callbackParams->callback;
136             tmpCallback->prev->next = tmpCallback->next;
137             tmpCallback->next->prev = tmpCallback->prev;
138             if (tmpCallback->callbackParams != NULL) {
139                 free(tmpCallback->callbackParams);
140             }
141             if (tmpCallback != NULL) {
142                 free(tmpCallback);
143             }
144             break;
145         }
146         tmpCallback = nextCallback;
147     }
148     (void)pthread_mutex_unlock(&g_mutex);
149     if (ret == DEVSL_SUCCESS) {
150         tmpCallbackParams.callback(&(tmpCallbackParams.queryParams), result, levelInfo);
151     }
152 }
153 
InitPthreadMutex(void)154 int32_t InitPthreadMutex(void)
155 {
156     int32_t ret;
157     ret = pthread_mutex_init(&g_mutex, NULL);
158     return ret;
159 }
160 
DestroyPthreadMutex(void)161 void DestroyPthreadMutex(void)
162 {
163     pthread_mutex_destroy(&g_mutex);
164 }