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