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_dslm_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 #define DEFAULT_TYPE 10
35
GetDeviceListMutex(void)36 static inline Mutex *GetDeviceListMutex(void)
37 {
38 static Mutex mutex = INITED_MUTEX;
39 return &mutex;
40 }
41
GetDeviceList(void)42 static ListHead *GetDeviceList(void)
43 {
44 static ListHead list = INIT_LIST(list);
45 return &list;
46 }
47
GetDeviceListSize(void)48 int32_t GetDeviceListSize(void)
49 {
50 int32_t size = 0;
51 ListNode *node = NULL;
52
53 LockMutex(GetDeviceListMutex());
54 FOREACH_LIST_NODE (node, GetDeviceList()) {
55 size++;
56 }
57 UnlockMutex(GetDeviceListMutex());
58 return size;
59 }
60
GetDslmDeviceInfo(const DeviceIdentify * device)61 DslmDeviceInfo *GetDslmDeviceInfo(const DeviceIdentify *device)
62 {
63 if (device == NULL) {
64 return NULL;
65 }
66
67 DslmDeviceInfo *result = NULL;
68 ListNode *node = NULL;
69
70 LockMutex(GetDeviceListMutex());
71 FOREACH_LIST_NODE (node, GetDeviceList()) {
72 DslmDeviceInfo *info = LIST_ENTRY(node, DslmDeviceInfo, linkNode);
73 if (IsSameDevice(&info->identity, device)) {
74 result = info;
75 break;
76 }
77 }
78 UnlockMutex(GetDeviceListMutex());
79 return result;
80 }
81
CreatOrGetDslmDeviceInfo(const DeviceIdentify * device)82 DslmDeviceInfo *CreatOrGetDslmDeviceInfo(const DeviceIdentify *device)
83 {
84 if (device == NULL) {
85 return NULL;
86 }
87
88 if (device->length != DEVICE_ID_MAX_LEN) {
89 return NULL;
90 }
91
92 DslmDeviceInfo *info = GetDslmDeviceInfo(device);
93 if (info != NULL) {
94 return info;
95 }
96
97 if (GetDeviceListSize() > MAX_DEVICE_CNT) {
98 return NULL;
99 }
100
101 info = MALLOC(sizeof(DslmDeviceInfo));
102 if (info == NULL) {
103 return NULL;
104 }
105 (void)memset_s(info, sizeof(DslmDeviceInfo), 0, sizeof(DslmDeviceInfo));
106
107 if (memcpy_s(&info->identity, sizeof(DeviceIdentify), device, sizeof(DeviceIdentify)) != EOK) {
108 FREE(info);
109 return NULL;
110 }
111
112 info->result = UINT32_MAX;
113 info->notifyListSize = 0;
114 info->historyListSize = 0;
115
116 InitDslmStateMachine(info);
117 LockMutex(GetDeviceListMutex());
118 AddListNodeBefore(&info->linkNode, GetDeviceList());
119 InitListHead(&info->notifyList);
120 InitListHead(&info->historyList);
121 UnlockMutex(GetDeviceListMutex());
122 SECURITY_LOG_INFO("create new DslmDeviceInfo %{public}x", info->machine.machineId);
123 return info;
124 }
125
IsSameDevice(const DeviceIdentify * first,const DeviceIdentify * second)126 bool IsSameDevice(const DeviceIdentify *first, const DeviceIdentify *second)
127 {
128 if ((first == NULL) || (second == NULL)) {
129 return false;
130 }
131 if (first->length != second->length) {
132 return false;
133 }
134 if (memcmp(first->identity, second->identity, first->length) != 0) {
135 return false;
136 }
137 return true;
138 }
139
ForEachDeviceDump(const ProcessDumpFunction dumper,int32_t dumpHandle)140 void ForEachDeviceDump(const ProcessDumpFunction dumper, int32_t dumpHandle)
141 {
142 ListNode *node = NULL;
143 if (dumper == NULL) {
144 return;
145 }
146
147 LockMutex(GetDeviceListMutex());
148 FOREACH_LIST_NODE (node, GetDeviceList()) {
149 const DslmDeviceInfo *info = LIST_ENTRY(node, DslmDeviceInfo, linkNode);
150 dumper(info, dumpHandle);
151 }
152 UnlockMutex(GetDeviceListMutex());
153 }
154
JudgeListDeviceType(void)155 bool JudgeListDeviceType(void)
156 {
157 bool result = true;
158 ListNode *node = NULL;
159
160 LockMutex(GetDeviceListMutex());
161 FOREACH_LIST_NODE (node, GetDeviceList()) {
162 DslmDeviceInfo *info = LIST_ENTRY(node, DslmDeviceInfo, linkNode);
163 if (info->osType != DEFAULT_TYPE) {
164 result = false;
165 break;
166 }
167 }
168 UnlockMutex(GetDeviceListMutex());
169
170 return result;
171 }