1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hdf_attribute_macro.h"
10 #include "devhost_service_clnt.h"
11 #include "hcs_macro.h"
12 #include "hdf_attribute_manager.h"
13 #include "hdf_config_macro.h"
14 #include "hdf_host_info.h"
15 #include "hdf_log.h"
16 #include "osal_mem.h"
17 #define HDF_LOG_TAG hdf_attr_macro
HdfHostListCompare(struct HdfSListNode * listEntryFirst,struct HdfSListNode * listEntrySecond)18 static bool HdfHostListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond)
19 {
20 struct HdfHostInfo *attrFirst = NULL;
21 struct HdfHostInfo *attrSecond = NULL;
22 if (listEntryFirst == NULL || listEntrySecond == NULL) {
23 return false;
24 }
25 attrFirst = (struct HdfHostInfo *)listEntryFirst;
26 attrSecond = (struct HdfHostInfo *)listEntrySecond;
27 return attrFirst->priority <= attrSecond->priority;
28 }
29
GetHostInfo(const struct HdfHostType * hostNode,struct HdfHostInfo * hostInfo)30 static bool GetHostInfo(const struct HdfHostType *hostNode, struct HdfHostInfo *hostInfo)
31 {
32 hostInfo->hostName = hostNode->devHostName;
33 hostInfo->priority = hostNode->priority;
34 if (strcmp(hostInfo->hostName, "") == 0) {
35 HDF_LOGW("%s: get host name failed", __func__);
36 return false;
37 }
38 if (hostInfo->priority > MAX_PRIORITY_NUM) {
39 HDF_LOGW("%s: get host priority failed, priority is: %u", __func__, hostInfo->priority);
40 return false;
41 }
42
43 return true;
44 }
45
AttributeManagerFreeDevHost(struct HdfDevHostMgr * hostMgr)46 static void AttributeManagerFreeDevHost(struct HdfDevHostMgr *hostMgr)
47 {
48 struct HdfHostType *host = NULL;
49 struct HdfHostType *hostTemp = NULL;
50
51 DLIST_FOR_EACH_ENTRY_SAFE(host, hostTemp, &hostMgr->hosts, struct HdfHostType, hostEntry) {
52 OsalMemFree(host);
53 }
54 OsalMemFree(hostMgr);
55 }
56
HdfAttributeManagerGetHostList(struct HdfSList * hostList)57 bool HdfAttributeManagerGetHostList(struct HdfSList *hostList)
58 {
59 struct HdfDevHostMgr *devHost = NULL;
60 struct HdfHostType *host = NULL;
61 uint16_t hostId = 0;
62
63 if (hostList == NULL) {
64 return false;
65 }
66
67 devHost = (struct HdfDevHostMgr *)OsalMemCalloc(sizeof(*devHost));
68 if (devHost == NULL) {
69 return HDF_FAILURE;
70 }
71
72 DListHeadInit(&devHost->hosts);
73
74 HCS_FOREACH_CHILD_VARGS(HDF_DEVICE_INFO, HDF_DEAL_HOST, devHost->hosts);
75
76 DLIST_FOR_EACH_ENTRY(host, &devHost->hosts, struct HdfHostType, hostEntry) {
77 struct HdfHostInfo *hostInfo = HdfHostInfoNewInstance();
78 if (hostInfo == NULL) {
79 HdfSListFlush(hostList, HdfHostInfoDelete);
80 HDF_LOGE("%s: new hostInfo is null", __func__);
81 return false;
82 }
83
84 if (!GetHostInfo(host, hostInfo)) {
85 HdfHostInfoFreeInstance(hostInfo);
86 continue;
87 }
88
89 hostInfo->hostId = hostId;
90 if (!HdfSListAddOrder(hostList, &hostInfo->node, HdfHostListCompare)) {
91 HdfHostInfoFreeInstance(hostInfo);
92 continue;
93 }
94 hostId++;
95 }
96 AttributeManagerFreeDevHost(devHost);
97
98 return true;
99 }
100
HdfDeviceListCompare(struct HdfSListNode * listEntryFirst,struct HdfSListNode * listEntrySecond)101 static bool HdfDeviceListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond)
102 {
103 struct HdfDeviceInfo *attrFirst = NULL;
104 struct HdfDeviceInfo *attrSecond = NULL;
105 if (listEntryFirst == NULL || listEntrySecond == NULL) {
106 return false;
107 }
108 attrFirst = (struct HdfDeviceInfo *)listEntryFirst;
109 attrSecond = (struct HdfDeviceInfo *)listEntrySecond;
110 return attrFirst->priority <= attrSecond->priority;
111 }
112
CheckDeviceInfo(const struct HdfDeviceInfo * deviceNodeInfo)113 static bool CheckDeviceInfo(const struct HdfDeviceInfo *deviceNodeInfo)
114 {
115 if (deviceNodeInfo->policy >= SERVICE_POLICY_INVALID) {
116 HDF_LOGE("%s: policy %u is invalid", __func__, deviceNodeInfo->policy);
117 return false;
118 }
119
120 if (deviceNodeInfo->priority > MAX_PRIORITY_NUM) {
121 HDF_LOGE("%s: priority %u is invalid", __func__, deviceNodeInfo->priority);
122 return false;
123 }
124
125 if (deviceNodeInfo->preload >= DEVICE_PRELOAD_INVALID) {
126 HDF_LOGE("%s: preload %u is invalid", __func__, deviceNodeInfo->preload);
127 return false;
128 }
129
130 return (strcmp(deviceNodeInfo->moduleName, "") != 0);
131 }
132
GetDeviceNodeInfo(const struct HdfDeviceNodeType * deviceNode,struct HdfDeviceInfo * deviceNodeInfo)133 static bool GetDeviceNodeInfo(const struct HdfDeviceNodeType *deviceNode, struct HdfDeviceInfo *deviceNodeInfo)
134 {
135 deviceNodeInfo->policy = deviceNode->policy;
136 deviceNodeInfo->priority = deviceNode->priority;
137 deviceNodeInfo->preload = deviceNode->preload;
138 deviceNodeInfo->permission = deviceNode->permission;
139 deviceNodeInfo->deviceMatchAttr = deviceNode->deviceMatchAttr;
140 deviceNodeInfo->moduleName = deviceNode->moduleName;
141 deviceNodeInfo->svcName = deviceNode->svcName;
142
143 return CheckDeviceInfo(deviceNodeInfo);
144 }
145
GetDevcieNodeList(const struct HdfDeviceType * device,struct DevHostServiceClnt * hostClnt,uint16_t deviceIdx)146 static bool GetDevcieNodeList(const struct HdfDeviceType *device,
147 struct DevHostServiceClnt *hostClnt, uint16_t deviceIdx)
148 {
149 uint8_t deviceNodeIdx = 1;
150 uint16_t hostId = hostClnt->hostId;
151 struct HdfDeviceInfo *deviceNodeInfo = NULL;
152 const struct HdfDeviceNodeType *devNode = NULL;
153
154 DLIST_FOR_EACH_ENTRY(devNode, &device->deviceNodes, struct HdfDeviceNodeType, deviceNodeEntry) {
155 deviceNodeInfo = HdfDeviceInfoNewInstance();
156 if (deviceNodeInfo == NULL) {
157 return false;
158 }
159 if (!GetDeviceNodeInfo(devNode, deviceNodeInfo)) {
160 HdfDeviceInfoFreeInstance(deviceNodeInfo);
161 HDF_LOGE("%s: failed to parse device node info, ignore", __func__);
162 continue;
163 }
164
165 deviceNodeInfo->deviceId = MK_DEVID(hostId, deviceIdx, deviceNodeIdx);
166 if (deviceNodeInfo->preload != DEVICE_PRELOAD_DISABLE) {
167 if (!HdfSListAddOrder(&hostClnt->unloadDevInfos, &deviceNodeInfo->node, HdfDeviceListCompare)) {
168 HDF_LOGE("%s: failed to add device info to list %s", __func__, deviceNodeInfo->svcName);
169 HdfDeviceInfoFreeInstance(deviceNodeInfo);
170 continue;
171 }
172 } else {
173 HdfSListAdd(&hostClnt->dynamicDevInfos, &deviceNodeInfo->node);
174 }
175
176 deviceNodeIdx++;
177 }
178 return deviceNodeIdx > 1;
179 }
180
AttributeManagerFreeHost(struct HdfHostType * host)181 static void AttributeManagerFreeHost(struct HdfHostType *host)
182 {
183 struct HdfDeviceType *device = NULL;
184 struct HdfDeviceType *deviceTemp = NULL;
185 struct HdfDeviceNodeType *devNode = NULL;
186 struct HdfDeviceNodeType *devNodeTemp = NULL;
187
188 DLIST_FOR_EACH_ENTRY_SAFE(device, deviceTemp, &host->devices, struct HdfDeviceType, deviceEntry) {
189 DLIST_FOR_EACH_ENTRY_SAFE(devNode, devNodeTemp, &device->deviceNodes, struct HdfDeviceNodeType,
190 deviceNodeEntry) {
191 OsalMemFree(devNode);
192 }
193 OsalMemFree(device);
194 }
195 OsalMemFree(host);
196 }
197
HdfAttributeManagerGetDeviceList(struct DevHostServiceClnt * hostClnt)198 int HdfAttributeManagerGetDeviceList(struct DevHostServiceClnt *hostClnt)
199 {
200 struct HdfHostType *host = NULL;
201 struct HdfDeviceType *device = NULL;
202 struct HdfDeviceNodeType *deviceNode = NULL;
203
204 uint16_t deviceIdx = 1;
205 int ret = HDF_DEV_ERR_NO_DEVICE;
206
207 if (hostClnt == NULL) {
208 return HDF_ERR_INVALID_PARAM;
209 }
210
211 host = (struct HdfHostType *)OsalMemCalloc(sizeof(*host));
212 if (host == NULL) {
213 return HDF_FAILURE;
214 }
215
216 DListHeadInit(&host->devices);
217
218 HCS_FOREACH_CHILD_VARGS(HDF_DEVICE_INFO, HDF_FIND_HOST, hostClnt->hostName, host);
219
220 DLIST_FOR_EACH_ENTRY(device, &host->devices, struct HdfDeviceType, deviceEntry) {
221 if (!GetDevcieNodeList(device, hostClnt, deviceIdx)) {
222 return ret;
223 }
224 deviceIdx++;
225 }
226 AttributeManagerFreeHost(host);
227 return HDF_SUCCESS;
228 }