• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dslm_device_list.h"
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include "securec.h"
23 
24 #include "device_security_defines.h"
25 #include "dslm_core_defines.h"
26 #include "dslm_fsm_process.h"
27 #include "utils_list.h"
28 #include "utils_log.h"
29 #include "utils_mem.h"
30 #include "utils_mutex.h"
31 #include "utils_state_machine.h"
32 
33 #define MAX_DEVICE_CNT 128
34 
GetDeviceListMutex(void)35 static inline Mutex *GetDeviceListMutex(void)
36 {
37     static Mutex mutex = INITED_MUTEX;
38     return &mutex;
39 }
40 
GetDeviceList(void)41 static ListHead *GetDeviceList(void)
42 {
43     static ListHead list = INIT_LIST(list);
44     return &list;
45 }
46 
GetDeviceListSize(void)47 int32_t GetDeviceListSize(void)
48 {
49     int32_t size = 0;
50     ListNode *node = NULL;
51 
52     LockMutex(GetDeviceListMutex());
53     FOREACH_LIST_NODE (node, GetDeviceList()) {
54         size++;
55     }
56     UnlockMutex(GetDeviceListMutex());
57     return size;
58 }
59 
GetDslmDeviceInfo(const DeviceIdentify * device)60 DslmDeviceInfo *GetDslmDeviceInfo(const DeviceIdentify *device)
61 {
62     if (device == NULL) {
63         return NULL;
64     }
65 
66     DslmDeviceInfo *result = NULL;
67     ListNode *node = NULL;
68 
69     LockMutex(GetDeviceListMutex());
70     FOREACH_LIST_NODE (node, GetDeviceList()) {
71         DslmDeviceInfo *info = LIST_ENTRY(node, DslmDeviceInfo, linkNode);
72         if (IsSameDevice(&info->identity, device)) {
73             result = info;
74             break;
75         }
76     }
77     UnlockMutex(GetDeviceListMutex());
78     return result;
79 }
80 
CreatOrGetDslmDeviceInfo(const DeviceIdentify * device)81 DslmDeviceInfo *CreatOrGetDslmDeviceInfo(const DeviceIdentify *device)
82 {
83     if (device == NULL) {
84         return NULL;
85     }
86 
87     if (device->length != DEVICE_ID_MAX_LEN) {
88         return NULL;
89     }
90 
91     DslmDeviceInfo *info = GetDslmDeviceInfo(device);
92     if (info != NULL) {
93         return info;
94     }
95 
96     if (GetDeviceListSize() > MAX_DEVICE_CNT) {
97         return NULL;
98     }
99 
100     info = MALLOC(sizeof(DslmDeviceInfo));
101     if (info == NULL) {
102         return NULL;
103     }
104     (void)memset_s(info, sizeof(DslmDeviceInfo), 0, sizeof(DslmDeviceInfo));
105 
106     if (memcpy_s(&info->identity, sizeof(DeviceIdentify), device, sizeof(DeviceIdentify)) != EOK) {
107         FREE(info);
108         return NULL;
109     }
110 
111     info->result = UINT32_MAX;
112     info->notifyListSize = 0;
113     info->historyListSize = 0;
114 
115     InitDslmStateMachine(info);
116     LockMutex(GetDeviceListMutex());
117     AddListNodeBefore(&info->linkNode, GetDeviceList());
118     InitListHead(&info->notifyList);
119     InitListHead(&info->historyList);
120     UnlockMutex(GetDeviceListMutex());
121     SECURITY_LOG_INFO("create new DslmDeviceInfo %{public}x", info->machine.machineId);
122     return info;
123 }
124 
IsSameDevice(const DeviceIdentify * first,const DeviceIdentify * second)125 bool IsSameDevice(const DeviceIdentify *first, const DeviceIdentify *second)
126 {
127     if ((first == NULL) || (second == NULL)) {
128         return false;
129     }
130     if (first->length != second->length) {
131         return false;
132     }
133     if (memcmp(first->identity, second->identity, first->length) != 0) {
134         return false;
135     }
136     return true;
137 }
138 
ForEachDeviceDump(const ProcessDumpFunction dumper,int32_t dumpHandle)139 void ForEachDeviceDump(const ProcessDumpFunction dumper, int32_t dumpHandle)
140 {
141     ListNode *node = NULL;
142     if (dumper == NULL) {
143         return;
144     }
145 
146     LockMutex(GetDeviceListMutex());
147     FOREACH_LIST_NODE (node, GetDeviceList()) {
148         const DslmDeviceInfo *info = LIST_ENTRY(node, DslmDeviceInfo, linkNode);
149         dumper(info, dumpHandle);
150     }
151     UnlockMutex(GetDeviceListMutex());
152 }
153