• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
10  * @addtogroup DriverConfig
11  * @{
12  *
13  * @brief Defines APIs for HDF driver developers to read driver configuration information.
14  *
15  * During version compilation of the device resource source file defined by developers, the compilation tool
16  * (for example, the compilation tool of the HCS file is hc-gen) generates bytecodes. When the HDF starts,
17  * it transfers the bytecode memory to the <b>DriverConfig</b> module. The <b>DriverConfig</b> module converts
18  * the bytecodes into a configuration tree and provides APIs for developers to query the tree.
19  *
20  * @since 1.0
21  */
22 
23 /**
24  * @file hcs_macro.h
25  *
26  * @brief Defines the macro-based APIs used for querying the configuration tree.
27  *
28  * During version compilation, the hc-gen tool can be used to parse the configuration tree into macro definitions.
29  * The following types of macro definitions are available:
30  * 1. Macro definition for determining whether a node exists in the configuration tree: The macro name is a string
31  * consisting of the node name and <b>_exists</b>. The value of the macro definition is <b>1</b>.
32  * 2. Macro definition for obtaining the node attributes in the configuration tree. The macro name is a string
33  * consisting of the node name and attribute name. The macro value is the attribute of the node.
34  * 3. Macro definition for determining whether a node attribute exists. The macro name is a string consisting of
35  * the node attribute name and <b>_exists</b>. The value of the macro definition is <b>1</b>.
36  * 4. Macro definition for traversing the nodes in the configuration tree. The macro name is a string consisting of
37  * the node name and <b>_foreach_child</b>. The value of the macro definition is <b>func (child node)</b>.
38  * 5. Macro definition for obtaining the array type attribute of the configuration tree. The macro name is a string
39  * consisting of the node attribute name and <b>_data</b>. The value of the macro definition is the array type
40  * attribute of the node.
41  * 6. Macro definition for obtaining the array size of a node in the configuration tree. The macro name is a string
42  * consisting of the node attribute name and <b>_array_size</b>. The value of the macro definition is the array
43  * size of the node.
44  *
45  * The macro definition of the configuration tree node name is a string consisting of the parent node name and
46  * the node name. The macro definition of the configuration tree node attribute name is a string consists of
47  * the node name and attribute name.
48  *
49  * The driver uses the macro-based APIs to concatenate name strings to macro definition names and obtain the macro
50  * definition values, that is, the attribute values of the configuration tree.
51  *
52  * Macro definition example: #define root_device_info_match_attr "hdf_manager"
53  * Example of using macro-based APIs to obtain the configuration:
54  * printf("%s", HCS_PROP(HCS_NODE(HCS_ROOT, device_info), match_attr)); -- The output is <b>hdf_manager</b>.
55  * @since 1.0
56  */
57 
58 #ifndef HCS_MACRO_H
59 #define HCS_MACRO_H
60 
61 /**
62  * @brief Concatenates parameter 1 and parameter 2 into a string.
63  *
64  * @param part1 Indicates parameter 1.
65  * @param part2 Indicates parameter 2.
66  * @since 1.0
67  */
68 #define HCS_CAT(part1, part2) part1 ## part2
69 
70 /**
71  * @brief Defines the root node of the configuration tree.
72  *
73  * @since 1.0
74  */
75 #define HCS_ROOT root
76 
77 /**
78  * @brief Checks whether a node exists in the configuration tree.
79  *
80  * @param node Indicates the node to check.
81  * @since 1.0
82  */
83 #define HCS_NODE_EXISTS(node) HCS_CAT(node, _exists)
84 
85 /**
86  * @brief Obtains the macro definition of a node in the configuration tree.
87  *
88  * @param parent Indicates the parent node of the target node.
89  * @param node Indicates the target node.
90  * @since 1.0
91  */
92 #define HCS_NODE(parent, node) HCS_CAT(parent, _##node)
93 
94 /**
95  * @brief Checks whether a configuration node attribute exists.
96  *
97  * @param node Indicates the target node.
98  * @param prop Indicates the node attribute to check.
99  * @since 1.0
100  */
101 #define HCS_NODE_HAS_PROP(node, prop) HCS_CAT(node, _##prop##_exists)
102 
103 /**
104  * @brief Obtains the macro definition of a node attribute in the configuration tree.
105  *
106  * @param node Indicates the target node.
107  * @param prop Indicates the attribute name.
108  * @since 1.0
109  */
110 #define HCS_PROP(node, prop) HCS_CAT(node, _##prop)
111 
112 /**
113  * @brief Calls a function to traverse a node in the configuration tree.
114  *
115  * @param node Indicates the target node.
116  * @param func Indicates the function to call.
117  * @since 1.0
118  */
119 #define HCS_FOREACH_CHILD(node, func) \
120     HCS_CAT(node, _foreach_child)(func)
121 
122 /**
123  * @brief Calls a function with passed variables to traverse a node in the configuration tree.
124  *
125  * @param node Indicates the target node.
126  * @param func Indicates the function to call. Variables can be passed to the function to be called.
127  * @since 1.0
128  */
129 #define HCS_FOREACH_CHILD_VARGS(node, func, ...) \
130     HCS_CAT(node, _foreach_child_vargs)(func, __VA_ARGS__)
131 
132 /**
133  * @brief Obtains the attribute value of the node array type in the configuration tree.
134  *
135  * @param arrays_node Indicates the attribute name of the node array type.
136  * @since 1.0
137  */
138 #define HCS_ARRAYS(arrays_node) HCS_CAT(arrays_node, _data)
139 
140 /**
141  * @brief Obtain the size of the attribute value of the node array type in the configuration tree.
142  *
143  * @param arrays_node Indicates the attribute name of the node array type.
144  * @since 1.0
145  */
146 #define HCS_ARRAYS_SIZE(arrays_node) HCS_CAT(arrays_node, _array_size)
147 
148 #endif // HCS_MACRO_H
149