4 Every project has its coding style, and BlueZ is not an exception. This
5 document describes the preferred coding style for BlueZ code, in order to keep
6 some level of consistency among developers so that code can be easily
7 understood and maintained.
9 First of all, BlueZ coding style must follow every rule for Linux kernel
10 (http://www.kernel.org/doc/Documentation/CodingStyle). There also exists a tool
11 named checkpatch.pl to help you check the compliance with it. Just type
12 "checkpatch.pl --no-tree patch_name" to check your patch. In theory, you need
13 to clean up all the warnings and errors except this one: "ERROR: Missing
14 Signed-off-by: line(s)". BlueZ does not used Signed-Off lines, so including
15 them is actually an error. In certain circumstances one can ignore the 80
16 character per line limit. This is generally only allowed if the alternative
17 would make the code even less readable.
19 Besides the kernel coding style above, BlueZ has special flavors for its own.
20 Some of them are mandatory (marked as 'M'), while some others are optional
21 (marked as 'O'), but generally preferred.
23 M1: Blank line before and after an if/while/do/for statement
24 ============================================================
26 There should be a blank line before if statement unless the if is nested and
27 not preceded by an expression or variable declaration.
54 The only exception to this rule applies when a variable is being checked for
57 err = stat(filename, &st);
58 if (err || !S_ISDIR(st.st_mode))
61 M2: Multiple line comment
62 =========================
64 If your comment has more than one line, please start it from the second line.
68 * first line comment // correct
74 M3: Space before and after operator
75 ===================================
77 There should be a space before and after each operator.
86 If your condition in if, while, for statement or a function declaration is too
87 long to fit in one line, the new line needs to be indented not aligned with the
92 if ((adapter->supported_settings & MGMT_SETTING_SSP) &&
93 !(adapter->current_settings & MGMT_SETTING_SSP)) // wrong
96 if ((adapter->supported_settings & MGMT_SETTING_SSP) &&
97 !(adapter->current_settings & MGMT_SETTING_SSP))
100 void btd_adapter_register_pin_cb(struct btd_adapter *adapter,
101 btd_adapter_pin_cb_t cb) // wrong
104 void btd_adapter_register_pin_cb(struct btd_adapter *adapter,
105 btd_adapter_pin_cb_t cb)
107 The referred style for line wrapping is to indent as far as possible to the
108 right without hitting the 80 columns limit.
110 M5: Space when doing type casting
111 =================================
113 There should be a space between new type and variable.
117 a = (int *)b; // wrong
119 a = (int *) b; // correct
122 M6: Don't initialize variable unnecessarily
123 ===========================================
125 When declaring a variable, try not to initialize it unless necessary.
130 for (i = 0; i < 3; i++) {
133 M7: Follow the order of include header elements
134 ===============================================
136 When writing an include header the various elements should be in the following
139 - forward declarations
143 - function declarations and inline function definitions
145 M8: Internal headers must not use include guards
146 ================================================
148 Any time when creating a new header file with non-public API, that header
149 must not contain include guards.
154 Enums must have a descriptive name. The enum type should be small caps and
155 it should not be typedef-ed. Enum contents should be in CAPITAL letters and
156 prefixed by the enum type name.
161 ANIMAL_TYPE_FOUR_LEGS,
162 ANIMAL_TYPE_EIGHT_LEGS,
163 ANIMAL_TYPE_TWO_LEGS,
166 If the enum contents have values (e.g. from specification) the formatting
167 should be as follows:
170 ANIMAL_TYPE_FOUR_LEGS = 4,
171 ANIMAL_TYPE_EIGHT_LEGS = 8,
172 ANIMAL_TYPE_TWO_LEGS = 2,
175 M10: Enum as switch variable
176 ============================
178 If the variable of a switch is an enum, you must include all values in
179 switch body even if providing default. This is enforced by compiler option
180 enabling extra warning in such case. The reason for this is to ensure that if
181 later on enum is modified and one forget to change the switch accordingly, the
182 compiler will complain the new added type hasn't been handled.
187 ANIMAL_TYPE_FOUR_LEGS = 4,
188 ANIMAL_TYPE_EIGHT_LEGS = 8,
189 ANIMAL_TYPE_TWO_LEGS = 2,
195 case ANIMAL_TYPE_FOUR_LEGS:
198 case ANIMAL_TYPE_EIGHT_LEGS:
201 case ANIMAL_TYPE_TWO_LEGS:
208 switch (t) { // Wrong
209 case ANIMAL_TYPE_FOUR_LEGS:
212 case ANIMAL_TYPE_TWO_LEGS:
219 However if the enum comes from an external header file outside BlueZ, such as
220 Android headers, we cannot make any assumption of how the enum is defined and
221 this rule might not apply.
223 M11: Always use parenthesis with sizeof
224 =======================================
226 The expression argument to the sizeof operator should always be in
231 memset(stuff, 0, sizeof(*stuff));
234 memset(stuff, 0, sizeof *stuff); // Wrong
236 M12: Use void if function has no parameters
237 ===========================================
239 A function with no parameters must use void in the parameter list.
252 O1: Try to avoid complex if body
253 ================================
255 It's better not to have a complicated statement for if. You may judge its
256 contrary condition and return | break | continue | goto ASAP.
260 if (device) { // worse
261 memset(&eir_data, 0, sizeof(eir_data));
263 eir_parse(&eir_data, ev->eir, eir_len);
266 error("Unable to get device object for %s", addr);
272 error("Unable to get device object for %s", addr);
276 memset(&eir_data, 0, sizeof(eir_data));
278 eir_parse(&eir_data, ev->eir, eir_len);