]>
Commit | Line | Data |
---|---|---|
1859c480 UH |
1 | ------------------------------------------------------------------------------- |
2 | HACKING | |
3 | ------------------------------------------------------------------------------- | |
4 | ||
5 | Coding style | |
6 | ------------ | |
7 | ||
fb83ab34 UH |
8 | This project is programmed using the Linux kernel coding style: |
9 | ||
10 | https://www.kernel.org/doc/html/latest/process/coding-style.html | |
1859c480 UH |
11 | |
12 | Please use the same style for any code contributions, thanks! | |
13 | ||
14 | The Python decoders should follow the usual Python conventions and use | |
15 | Python idioms as far as it makes sense. The coding style should mostly follow | |
fb83ab34 UH |
16 | the Python PEP-8, which includes the convention of 4 spaces for indentation: |
17 | ||
18 | http://www.python.org/dev/peps/pep-0008/ | |
1859c480 UH |
19 | |
20 | ||
21 | Contributions | |
22 | ------------- | |
23 | ||
24 | - Patches should be sent to the development mailinglist at | |
25 | sigrok-devel@lists.sourceforge.net (please subscribe to the list first). | |
26 | ||
27 | https://lists.sourceforge.net/lists/listinfo/sigrok-devel | |
28 | ||
29 | - Alternatively, you can also clone the git repository and let us know | |
30 | from where to pull/review your changes. You can use gitorious.org, | |
31 | github.com, or any other public git hosting site. | |
32 | ||
33 | ||
34 | Random notes | |
35 | ------------ | |
36 | ||
0672779d UH |
37 | - Don't do variable declarations in compound statements, only at the |
38 | beginning of a function. | |
39 | ||
40 | - Generally avoid assigning values to variables at declaration time, | |
41 | especially so for complex and/or run-time dependent values. | |
42 | ||
077fa8ac | 43 | - Consistently use g_*malloc() / g_*malloc0(). Do not use standard |
1859c480 UH |
44 | malloc()/calloc() if it can be avoided (sometimes other libs such |
45 | as libftdi can return malloc()'d memory, for example). | |
46 | ||
47 | - Always properly match allocations with the proper *free() functions. If | |
077fa8ac | 48 | glib's g_*malloc()/g_*malloc0() was used, use g_free() to free the |
1859c480 UH |
49 | memory. Otherwise use standard free(). Never use the wrong function! |
50 | ||
077fa8ac UH |
51 | - We assume that "small" memory allocations (< 1MB) will always succeed. |
52 | Thus, it's fine to use g_malloc() or g_malloc0() for allocations of | |
53 | simple/small structs and such (instead of using g_try_malloc()), and | |
54 | there's no need to check the return value. | |
55 | ||
56 | Do use g_try_malloc() or g_try_malloc0() for large (>= 1MB) allocations | |
57 | and check the return value. | |
1859c480 UH |
58 | |
59 | - You should never print any messages (neither to stdout nor stderr nor | |
60 | elsewhere) "manually" via e.g. printf() or g_log() or similar functions. | |
61 | Only srd_err()/srd_warn()/srd_info()/srd_dbg()/srd_spew() should be used. | |
62 | ||
63 | - Use glib's gboolean / TRUE / FALSE for boolean types consistently. | |
64 | Do not use <stdbool.h> and its true / false, and do not invent private | |
65 | definitions for this either. | |
66 | ||
67 | - Consistently use the same naming convention for #include guards in headers: | |
68 | <PROJECTNAME>_<PATH_TO_FILE>_<FILE> | |
69 | This ensures that all #include guards are always unique and consistent. | |
0672779d | 70 | Example: LIBSIGROKDECODE_LIBSIGROKDECODE_INTERNAL_H |
1859c480 UH |
71 | |
72 | - Consistently use the same naming convention for API functions: | |
73 | <libprefix>_<groupname>_<action>(). | |
74 | ||
75 | Examples: | |
76 | srd_log_loglevel_set(), srd_log_loglevel_get(), srd_log_handler_set(), | |
77 | srd_log_handler_set_default(), and so on. | |
78 | ||
79 | Getter/setter function names should usually end with "_get" or "_set". | |
80 | Functions creating new "objects" should end with "_new". | |
81 | Functions destroying "objects" should end with "_destroy". | |
82 | Functions adding or removing items (e.g. from lists) should end with | |
83 | either "_add" or "_remove". | |
84 | Functions operating on all items from a list (not on only one of them), | |
85 | should end with "_all", e.g. "_remove_all", "_get_all", and so on. | |
86 | Use "_remove_all" in favor of "_clear" for consistency. | |
87 | ||
0672779d UH |
88 | - All enums should generally use an explicit start number of 10000. |
89 | If there are multiple "categories" in the enum entries, each category | |
90 | should be 10000 entries apart from the next one. The start of categories | |
91 | are thus 10000, 20000, 30000, and so on. | |
92 | ||
93 | Adding items to an enum MUST always append to a "category", never add | |
94 | items in the middle of a category. The order of items MUST NOT be changed. | |
95 | Any of the above would break the ABI. | |
96 | ||
97 | The enum item 0 is special and is used as terminator in some lists, thus | |
98 | enums should not use this for "valid" entries (and start at 10000 instead). | |
99 | ||
31e615a5 UH |
100 | |
101 | Doxygen | |
102 | ------- | |
103 | ||
1859c480 UH |
104 | - In Doxygen comments, put an empty line between the block of @param lines |
105 | and the final @return line. The @param lines themselves (if there is more | |
106 | than one) are not separated by empty lines. | |
107 | ||
31e615a5 UH |
108 | - Mark private functions (SRD_PRIV) with /** @private */, so that Doxygen |
109 | doesn't include them in the output. Functions that are "static" anyway | |
110 | don't need to be marked like this. | |
111 | ||
112 | - Mark private variables/#defines with /** @cond PRIVATE */ and | |
113 | /** @endcond */, so that Doxygen doesn't include them in the output. | |
114 | Variables that are "static" don't need to be marked like this. | |
115 | ||
f11e9498 | 116 | - Mark all public API functions (SRD_API) with a @since tag which indicates |
0672779d UH |
117 | in which release the respective function was added (e.g. "@since 0.1.0"). |
118 | ||
119 | If the function has existed before, but its API changed later, the @since | |
120 | tag should mention only the release when the API last changed. | |
121 | ||
122 | Example: The srd_foo() call was added in 0.1.0, but the API changed in | |
123 | the later 0.2.0 release. The docs should read "@since 0.2.0" in that case. | |
f11e9498 UH |
124 | |
125 | Non-public functions (static ones, and those marked SRD_PRIV) don't need | |
126 | to have @since markers. | |
127 | ||
128 | The @since tag should be the last one, i.e. it should come after @param, | |
129 | @return, @see, and so on. | |
130 | ||
1859c480 UH |
131 | |
132 | Protocol decoder guidelines | |
133 | --------------------------- | |
134 | ||
135 | - The 'desc' metadata field for a protocol decoder, which contains a | |
136 | short, one-line description of the protocol/bus, should be at most 55 | |
137 | characters long, and end with a full stop. This short description can be | |
138 | displayed on the command-line using "sigrok-cli -V -l 3", or in various | |
139 | different places in GUIs. | |
140 | ||
141 | - Longer, multi-line descriptions should be placed in the protocol | |
142 | decoder's __init__.py file as docstring. It can be viewed (for a specific | |
143 | protocol decoder, e.g., UART) via "sigrok-cli -a uart", or in various | |
144 | other places in GUIs. | |
145 | ||
146 | - Generally use strings for states (of the PD state machine), not integers. | |
147 | This avoids having to keep a list of state definitions at the top of file. | |
148 | The performance overhead for this is negligible in practice. | |
149 | ||
150 | Recommended: | |
151 | self.state = 'IDLE' | |
152 | self.state = 'GET STOP BIT' | |
153 | Not recommended: | |
154 | self.state = IDLE | |
155 | self.state = GET_STOP_BIT | |
156 | (where IDLE = 0 and GET_STOP_BIT = 1, for example) | |
157 | ||
158 | - Generally use strings for commands/IDs in generated protocol packets. | |
159 | This avoids having to know magic numbers of the PD in higher-level PDs. | |
160 | The performance overhead for this is negligible in practice. | |
161 | ||
162 | Recommended: | |
163 | self.put(x, y, p, ['STOPBIT', 0, 0]) | |
164 | self.put(x, y, p, ['ADDRESS READ', 0x51]) | |
165 | Not recommended: | |
166 | self.put(x, y, p, [STOPBIT, 0, 0]) | |
167 | self.put(x, y, p, [ADDRESS_READ, 0x51]) | |
168 | (with STOPBIT = 3 and ADDRESS_READ = 7, for example) | |
169 | ||
170 | - Use ALL-CAPS names for PD states and protocol packet commands/ID. | |
171 | Words should be separated by spaces (not underscores or the like). | |
172 | ||
173 | Recommended: | |
174 | 'FIND ADDRESS', 'GET TEMPERATURE', 'START' | |
175 | Not recommended: | |
176 | 'FIND_ADDRESS', 'Get Temperature', 'start' | |
177 | ||
178 | ||
0672779d UH |
179 | Testsuite |
180 | --------- | |
181 | ||
182 | You can run the libsigrokdecode testsuite using: | |
183 | ||
184 | $ make check | |
185 | ||
186 | ||
187 | Protocol decoder test framework | |
188 | ------------------------------- | |
189 | ||
c414f199 UH |
190 | Please see the sigrok-test repository for a protocol decoder test suite that |
191 | checks the decoded data of various PDs against known-good reference data. | |
0672779d UH |
192 | |
193 | ||
1859c480 UH |
194 | Release engineering |
195 | ------------------- | |
196 | ||
197 | See | |
198 | ||
199 | http://sigrok.org/wiki/Developers/Release_process | |
200 | ||
201 | for a list of items that need to be done when releasing a new tarball. | |
202 |