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