mirror of
https://github.com/openharmony/drivers_framework.git
synced 2026-07-19 16:24:45 -04:00
feat: add L0 hdf config macro parse
Signed-off-by: zhang <zhangfengxi@huawei.com>
This commit is contained in:
Executable
+227
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
*
|
||||
* HDF is dual licensed: you can use it either under the terms of
|
||||
* the GPL, or the BSD license, at your option.
|
||||
* See the LICENSE file in the root of this repository for complete details.
|
||||
*/
|
||||
|
||||
#include "hdf_attribute_manager.h"
|
||||
#include "hdf_attribute_macro.h"
|
||||
#include "devhost_service_clnt.h"
|
||||
#include "hdf_config_macro.h"
|
||||
#include "hcs_macro.h"
|
||||
#include "hdf_host_info.h"
|
||||
#include "hdf_log.h"
|
||||
#include "osal_mem.h"
|
||||
#define HDF_LOG_TAG hdf_attr_macro
|
||||
static bool HdfHostListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond)
|
||||
{
|
||||
struct HdfHostInfo *attrFirst = NULL;
|
||||
struct HdfHostInfo *attrSecond = NULL;
|
||||
if (listEntryFirst == NULL || listEntrySecond == NULL) {
|
||||
return false;
|
||||
}
|
||||
attrFirst = (struct HdfHostInfo *)listEntryFirst;
|
||||
attrSecond = (struct HdfHostInfo *)listEntrySecond;
|
||||
return attrFirst->priority <= attrSecond->priority;
|
||||
}
|
||||
|
||||
static bool GetHostInfo(const struct HdfHostType *hostNode, struct HdfHostInfo *hostInfo)
|
||||
{
|
||||
hostInfo->hostName = hostNode->devHostName;
|
||||
hostInfo->priority = hostNode->priority;
|
||||
if (strcmp(hostInfo->hostName, "") == 0) {
|
||||
HDF_LOGW("%s: get host name failed", __func__);
|
||||
return false;
|
||||
}
|
||||
if (hostInfo->priority > MAX_PRIORITY_NUM) {
|
||||
HDF_LOGW("%s: get host priority failed, priority is: %u", __func__, hostInfo->priority);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void AttributeManagerFreeDevHost(struct HdfDevHostMgr *hostMgr)
|
||||
{
|
||||
struct HdfHostType *host = NULL;
|
||||
struct HdfHostType *hostTemp = NULL;
|
||||
|
||||
DLIST_FOR_EACH_ENTRY_SAFE(host, hostTemp, &hostMgr->hosts, struct HdfHostType, hostEntry) {
|
||||
OsalMemFree(host);
|
||||
}
|
||||
OsalMemFree(hostMgr);
|
||||
}
|
||||
|
||||
bool HdfAttributeManagerGetHostList(struct HdfSList *hostList)
|
||||
{
|
||||
struct HdfDevHostMgr *devHost = NULL;
|
||||
struct HdfHostType *host = NULL;
|
||||
uint16_t hostId = 0;
|
||||
|
||||
if (hostList == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
devHost = (struct HdfDevHostMgr *)OsalMemCalloc(sizeof(*devHost));
|
||||
if (devHost == NULL) {
|
||||
return HDF_FAILURE;
|
||||
}
|
||||
|
||||
DListHeadInit(&devHost->hosts);
|
||||
|
||||
HCS_FOREACH_CHILD_VARGS(HDF_DEVICE_INFO, HDF_DEAL_HOST, devHost->hosts);
|
||||
|
||||
DLIST_FOR_EACH_ENTRY(host, &devHost->hosts, struct HdfHostType, hostEntry) {
|
||||
struct HdfHostInfo *hostInfo = HdfHostInfoNewInstance();
|
||||
if (hostInfo == NULL) {
|
||||
HdfSListFlush(hostList, HdfHostInfoDelete);
|
||||
HDF_LOGE("%s: new hostInfo is null", __func__);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!GetHostInfo(host, hostInfo)) {
|
||||
HdfHostInfoFreeInstance(hostInfo);
|
||||
continue;
|
||||
}
|
||||
|
||||
hostInfo->hostId = hostId;
|
||||
if (!HdfSListAddOrder(hostList, &hostInfo->node, HdfHostListCompare)) {
|
||||
HdfHostInfoFreeInstance(hostInfo);
|
||||
continue;
|
||||
}
|
||||
hostId++;
|
||||
}
|
||||
AttributeManagerFreeDevHost(devHost);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool HdfDeviceListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond)
|
||||
{
|
||||
struct HdfDeviceInfo *attrFirst = NULL;
|
||||
struct HdfDeviceInfo *attrSecond = NULL;
|
||||
if (listEntryFirst == NULL || listEntrySecond == NULL) {
|
||||
return false;
|
||||
}
|
||||
attrFirst = (struct HdfDeviceInfo *)listEntryFirst;
|
||||
attrSecond = (struct HdfDeviceInfo *)listEntrySecond;
|
||||
return attrFirst->priority <= attrSecond->priority;
|
||||
}
|
||||
|
||||
static bool CheckDeviceInfo(const struct HdfDeviceInfo *deviceNodeInfo)
|
||||
{
|
||||
if (deviceNodeInfo->policy >= SERVICE_POLICY_INVALID) {
|
||||
HDF_LOGE("%s: policy %u is invalid", __func__, deviceNodeInfo->policy);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (deviceNodeInfo->priority > MAX_PRIORITY_NUM) {
|
||||
HDF_LOGE("%s: priority %u is invalid", __func__, deviceNodeInfo->priority);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (deviceNodeInfo->preload >= DEVICE_PRELOAD_INVALID) {
|
||||
HDF_LOGE("%s: preload %u is invalid", __func__, deviceNodeInfo->preload);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (strcmp(deviceNodeInfo->moduleName, "") != 0);
|
||||
}
|
||||
|
||||
static bool GetDeviceNodeInfo(const struct HdfDeviceNodeType *deviceNode, struct HdfDeviceInfo *deviceNodeInfo)
|
||||
{
|
||||
deviceNodeInfo->policy = deviceNode->policy;
|
||||
deviceNodeInfo->priority = deviceNode->priority;
|
||||
deviceNodeInfo->preload = deviceNode->preload;
|
||||
deviceNodeInfo->permission = deviceNode->permission;
|
||||
deviceNodeInfo->deviceMatchAttr = deviceNode->deviceMatchAttr;
|
||||
deviceNodeInfo->moduleName = deviceNode->moduleName;
|
||||
deviceNodeInfo->svcName = deviceNode->svcName;
|
||||
|
||||
return CheckDeviceInfo(deviceNodeInfo);
|
||||
}
|
||||
|
||||
static bool GetDevcieNodeList(const struct HdfDeviceType *device,
|
||||
struct DevHostServiceClnt *hostClnt, uint16_t deviceIdx)
|
||||
{
|
||||
uint8_t deviceNodeIdx = 1;
|
||||
uint16_t hostId = hostClnt->hostId;
|
||||
struct HdfDeviceInfo *deviceNodeInfo = NULL;
|
||||
const struct HdfDeviceNodeType *devNode = NULL;
|
||||
|
||||
DLIST_FOR_EACH_ENTRY(devNode, &device->deviceNodes, struct HdfDeviceNodeType, deviceNodeEntry) {
|
||||
deviceNodeInfo = HdfDeviceInfoNewInstance();
|
||||
if (deviceNodeInfo == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (!GetDeviceNodeInfo(devNode, deviceNodeInfo)) {
|
||||
HdfDeviceInfoFreeInstance(deviceNodeInfo);
|
||||
HDF_LOGE("%s: failed to parse device node info, ignore", __func__);
|
||||
continue;
|
||||
}
|
||||
|
||||
deviceNodeInfo->deviceId = MK_DEVID(hostId, deviceIdx, deviceNodeIdx);
|
||||
if (deviceNodeInfo->preload != DEVICE_PRELOAD_DISABLE) {
|
||||
if (!HdfSListAddOrder(&hostClnt->unloadDevInfos, &deviceNodeInfo->node, HdfDeviceListCompare)) {
|
||||
HDF_LOGE("%s: failed to add device info to list %s", __func__, deviceNodeInfo->svcName);
|
||||
HdfDeviceInfoFreeInstance(deviceNodeInfo);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
HdfSListAdd(&hostClnt->dynamicDevInfos, &deviceNodeInfo->node);
|
||||
}
|
||||
|
||||
deviceNodeIdx++;
|
||||
}
|
||||
return deviceNodeIdx > 1;
|
||||
}
|
||||
|
||||
static void AttributeManagerFreeHost(struct HdfHostType *host)
|
||||
{
|
||||
struct HdfDeviceType *device = NULL;
|
||||
struct HdfDeviceType *deviceTemp = NULL;
|
||||
struct HdfDeviceNodeType *devNode = NULL;
|
||||
struct HdfDeviceNodeType *devNodeTemp = NULL;
|
||||
|
||||
DLIST_FOR_EACH_ENTRY_SAFE(device, deviceTemp, &host->devices, struct HdfDeviceType, deviceEntry) {
|
||||
DLIST_FOR_EACH_ENTRY_SAFE(devNode, devNodeTemp, &device->deviceNodes, struct HdfDeviceNodeType, deviceNodeEntry) {
|
||||
OsalMemFree(devNode);
|
||||
}
|
||||
OsalMemFree(device);
|
||||
}
|
||||
OsalMemFree(host);
|
||||
}
|
||||
|
||||
int HdfAttributeManagerGetDeviceList(struct DevHostServiceClnt *hostClnt)
|
||||
{
|
||||
struct HdfHostType *host = NULL;
|
||||
struct HdfDeviceType *device = NULL;
|
||||
struct HdfDeviceNodeType *deviceNode = NULL;
|
||||
|
||||
uint16_t deviceIdx = 1;
|
||||
int ret = HDF_DEV_ERR_NO_DEVICE;
|
||||
|
||||
if (hostClnt == NULL) {
|
||||
return HDF_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
host = (struct HdfHostType *)OsalMemCalloc(sizeof(*host));
|
||||
if (host == NULL) {
|
||||
return HDF_FAILURE;
|
||||
}
|
||||
|
||||
DListHeadInit(&host->devices);
|
||||
|
||||
HCS_FOREACH_CHILD_VARGS(HDF_DEVICE_INFO, HDF_FIND_HOST, hostClnt->hostName, host);
|
||||
|
||||
DLIST_FOR_EACH_ENTRY(device, &host->devices, struct HdfDeviceType, deviceEntry) {
|
||||
if (!GetDevcieNodeList(device, hostClnt, deviceIdx)) {
|
||||
return ret;
|
||||
}
|
||||
deviceIdx++;
|
||||
}
|
||||
AttributeManagerFreeHost(host);
|
||||
return HDF_SUCCESS;
|
||||
}
|
||||
@@ -267,10 +267,15 @@ struct HdfDeviceNode *HdfDeviceNodeNewInstance(const struct HdfDeviceInfo *devic
|
||||
HdfDeviceNodeFreeInstance(devNode);
|
||||
return NULL;
|
||||
}
|
||||
#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
|
||||
devNode->deviceObject.deviceMatchAttr = deviceInfo->deviceMatchAttr;
|
||||
#else
|
||||
devNode->deviceObject.property = HcsGetNodeByMatchAttr(HdfGetHcsRootNode(), deviceInfo->deviceMatchAttr);
|
||||
if (devNode->deviceObject.property == NULL) {
|
||||
HDF_LOGD("node %s property empty, match attr: %s", deviceInfo->moduleName, deviceInfo->deviceMatchAttr);
|
||||
}
|
||||
#endif
|
||||
|
||||
devNode->devStatus = DEVNODE_INITED;
|
||||
return devNode;
|
||||
}
|
||||
|
||||
@@ -167,7 +167,11 @@ bool HdfDeviceSetClass(struct HdfDeviceObject *deviceObject, DeviceClass deviceC
|
||||
void HdfDeviceObjectConstruct(struct HdfDeviceObject *deviceObject)
|
||||
{
|
||||
if (deviceObject != NULL) {
|
||||
#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
|
||||
deviceObject->deviceMatchAttr = NULL;
|
||||
#else
|
||||
deviceObject->property = NULL;
|
||||
#endif
|
||||
deviceObject->service = NULL;
|
||||
deviceObject->deviceClass = DEVICE_CLASS_DEFAULT;
|
||||
}
|
||||
|
||||
Executable
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
*
|
||||
* HDF is dual licensed: you can use it either under the terms of
|
||||
* the GPL, or the BSD license, at your option.
|
||||
* See the LICENSE file in the root of this repository for complete details.
|
||||
*/
|
||||
|
||||
#ifndef HDF_ATTRIBUTE_MACRO_H
|
||||
#define HDF_ATTRIBUTE_MACRO_H
|
||||
|
||||
#include "hdf_log.h"
|
||||
#include "hdf_dlist.h"
|
||||
|
||||
struct HdfDevHostMgr {
|
||||
struct DListHead hosts;
|
||||
};
|
||||
|
||||
struct HdfHostType {
|
||||
const char *devHostName;
|
||||
uint16_t priority;
|
||||
struct DListHead devices;
|
||||
struct DListHead hostEntry;
|
||||
};
|
||||
|
||||
struct HdfDeviceType {
|
||||
struct DListHead deviceNodes;
|
||||
struct DListHead deviceEntry;
|
||||
};
|
||||
|
||||
struct HdfDeviceNodeType {
|
||||
uint16_t policy;
|
||||
uint16_t priority;
|
||||
uint16_t preload;
|
||||
uint16_t permission;
|
||||
const char *moduleName;
|
||||
const char *svcName;
|
||||
const char *deviceMatchAttr;
|
||||
struct DListHead deviceNodeEntry;
|
||||
};
|
||||
|
||||
#define HDF_DEAL_DEVICE_NODE(node, deviceNodes) \
|
||||
do { \
|
||||
deviceNode = (struct HdfDeviceNodeType *)OsalMemCalloc(sizeof(*deviceNode)); \
|
||||
if (deviceNode == NULL) { \
|
||||
HDF_LOGE("%s malloc fail", __func__); \
|
||||
return false; \
|
||||
} \
|
||||
deviceNode->policy = HCS_PROP(node, policy); \
|
||||
deviceNode->priority = HCS_PROP(node, priority); \
|
||||
deviceNode->preload = HCS_PROP(node, preload); \
|
||||
deviceNode->permission = HCS_PROP(node, permission); \
|
||||
deviceNode->moduleName = HCS_PROP(node, moduleName); \
|
||||
deviceNode->svcName = HCS_PROP(node, serviceName); \
|
||||
deviceNode->deviceMatchAttr = HCS_PROP(node, deviceMatchAttr); \
|
||||
DListInsertTail(&deviceNode->deviceNodeEntry, &deviceNodes); \
|
||||
} while (0)
|
||||
|
||||
#define HDF_DEAL_DEVICE(node, devices) \
|
||||
do { \
|
||||
device = (struct HdfDeviceType *)OsalMemCalloc(sizeof(*device)); \
|
||||
if (device == NULL) { \
|
||||
HDF_LOGE("%s malloc fail", __func__); \
|
||||
return false; \
|
||||
} \
|
||||
DListHeadInit(&device->deviceNodes); \
|
||||
node##_foreach_child_vargs(HDF_DEAL_DEVICE_NODE, device->deviceNodes); \
|
||||
DListInsertTail(&device->deviceEntry, &devices); \
|
||||
} while (0)
|
||||
|
||||
#define HDF_FIND_HOST(node, name, host) \
|
||||
do { \
|
||||
if (strcmp(HCS_PROP(node, hostName), name) == 0) { \
|
||||
host->devHostName = HCS_PROP(node, hostName); \
|
||||
host->priority = HCS_PROP(node, priority); \
|
||||
node##_foreach_child_vargs(HDF_DEAL_DEVICE, host->devices); \
|
||||
break; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define HDF_DEAL_HOST(node, hosts) \
|
||||
do { \
|
||||
host = (struct HdfHostType *)OsalMemCalloc(sizeof(*host)); \
|
||||
if (host == NULL) { \
|
||||
HDF_LOGE("%s malloc fail", __func__); \
|
||||
return false; \
|
||||
} \
|
||||
host->devHostName = HCS_PROP(node, hostName); \
|
||||
host->priority = HCS_PROP(node, priority); \
|
||||
DListInsertTail(&host->hostEntry, &hosts); \
|
||||
} while (0)
|
||||
|
||||
#define HDF_DEVICE_INFO HCS_NODE(HCS_ROOT, device_info)
|
||||
|
||||
#endif /* HDF_ATTRIBUTE_MACRO_H */
|
||||
@@ -94,7 +94,11 @@ struct HdfDeviceObject {
|
||||
struct IDeviceIoService *service;
|
||||
/** Pointer to the property of the device, which is read by the HDF from the configuration file and
|
||||
transmitted to the driver. */
|
||||
#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
|
||||
const char *deviceMatchAttr;
|
||||
#else
|
||||
const struct DeviceResourceNode *property;
|
||||
#endif
|
||||
DeviceClass deviceClass;
|
||||
/** Pointer to the private data of the device */
|
||||
void *priv;
|
||||
|
||||
@@ -177,9 +177,9 @@ bool MacroGen::GenNodeForeach(uint32_t depth, const std::shared_ptr<AstObject> &
|
||||
for (iter = subList.begin(); iter != subList.end(); iter++) {
|
||||
index--;
|
||||
if (index) {
|
||||
ofs_ << TAB << "func(" << *iter << ") \\\n";
|
||||
ofs_ << TAB << "func(" << *iter << "); \\\n";
|
||||
} else {
|
||||
ofs_ << TAB << "func(" << *iter << ")\n";
|
||||
ofs_ << TAB << "func(" << *iter << ");\n";
|
||||
}
|
||||
}
|
||||
ofs_ << std::endl;
|
||||
@@ -189,9 +189,9 @@ bool MacroGen::GenNodeForeach(uint32_t depth, const std::shared_ptr<AstObject> &
|
||||
for (iter = subList.begin(); iter != subList.end(); iter++) {
|
||||
index--;
|
||||
if (index) {
|
||||
ofs_ << TAB << "func(" << *iter << ", __VA_ARGS__) \\\n";
|
||||
ofs_ << TAB << "func(" << *iter << ", __VA_ARGS__); \\\n";
|
||||
} else {
|
||||
ofs_ << TAB << "func(" << *iter << ", __VA_ARGS__)\n";
|
||||
ofs_ << TAB << "func(" << *iter << ", __VA_ARGS__);\n";
|
||||
}
|
||||
}
|
||||
ofs_ << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user