| 1 |
# -*- Outline -*- |
|---|
| 2 |
# |
|---|
| 3 |
# Copyright 2004,2007 Free Software Foundation, Inc. |
|---|
| 4 |
# |
|---|
| 5 |
# This file is part of GNU Radio |
|---|
| 6 |
# |
|---|
| 7 |
# GNU Radio is free software; you can redistribute it and/or modify |
|---|
| 8 |
# it under the terms of the GNU General Public License as published by |
|---|
| 9 |
# the Free Software Foundation; either version 3, or (at your option) |
|---|
| 10 |
# any later version. |
|---|
| 11 |
# |
|---|
| 12 |
# GNU Radio is distributed in the hope that it will be useful, |
|---|
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 |
# GNU General Public License for more details. |
|---|
| 16 |
# |
|---|
| 17 |
# You should have received a copy of the GNU General Public License |
|---|
| 18 |
# along with GNU Radio; see the file COPYING. If not, write to |
|---|
| 19 |
# the Free Software Foundation, Inc., 51 Franklin Street, |
|---|
| 20 |
# Boston, MA 02110-1301, USA. |
|---|
| 21 |
# |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
Random notes on coding conventions, some explanations about why things |
|---|
| 25 |
aren't done differently, etc, etc, |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* C++ and Python |
|---|
| 29 |
|
|---|
| 30 |
GNU Radio is now a hybrid system. Some parts of the system are built |
|---|
| 31 |
in C++ and some of it in Python. In general, prefer Python to C++. |
|---|
| 32 |
Signal processing primitives are still built in C++ for performance. |
|---|
| 33 |
|
|---|
| 34 |
It is no longer possible to build user applications entirely in C++. |
|---|
| 35 |
Essential parts of the runtime system have been moved into Python. |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
* C++ namespaces |
|---|
| 39 |
|
|---|
| 40 |
In the cleanup process, I considered putting everything in the |
|---|
| 41 |
gnuradio namespace and dropping the Gr|gr prefix. In fact, I think |
|---|
| 42 |
it's probably the right idea, but when I tested it out, I ran into |
|---|
| 43 |
problems with SWIG's handling of namespaces. Bottom line, SWIG |
|---|
| 44 |
(1.3.21) got confused and generated bad code when I started playing |
|---|
| 45 |
around with namespaces in a not particularly convoluted way. I saw |
|---|
| 46 |
problems using the boost::shared_ptr template in combination with |
|---|
| 47 |
classes defined in the gnuradio namespace. It wasn't pretty... |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
* Naming conventions |
|---|
| 51 |
|
|---|
| 52 |
Death to CamelCaseNames! We've returned to a kinder, gentler era. |
|---|
| 53 |
We're now using the "STL style" naming convention with a couple of |
|---|
| 54 |
modifications since we're not using namespaces. |
|---|
| 55 |
|
|---|
| 56 |
With the exception of macros and other constant values, all |
|---|
| 57 |
identifiers shall be lower case with words_separated_like_this. |
|---|
| 58 |
|
|---|
| 59 |
Macros and constant values (e.g., enumerated values, |
|---|
| 60 |
static const int FOO = 23) shall be in UPPER_CASE. |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
** Global names |
|---|
| 64 |
|
|---|
| 65 |
All globally visible names (types, functions, variables, consts, etc) |
|---|
| 66 |
shall begin with a "package prefix", followed by an '_'. The bulk of |
|---|
| 67 |
the code in GNU Radio logically belongs to the "gr" package, hence |
|---|
| 68 |
names look like gr_open_file (...). |
|---|
| 69 |
|
|---|
| 70 |
Large coherent bodies of code may use other package prefixes, but |
|---|
| 71 |
let's try to keep them to a well thought out list. See the list |
|---|
| 72 |
below. |
|---|
| 73 |
|
|---|
| 74 |
*** Package prefixes |
|---|
| 75 |
|
|---|
| 76 |
These are the current package prefixes: |
|---|
| 77 |
|
|---|
| 78 |
gr_ Almost everything |
|---|
| 79 |
|
|---|
| 80 |
gri_ Implementation primitives. Sometimes we |
|---|
| 81 |
have both a gr_<foo> and a gri_<foo>. In that case, |
|---|
| 82 |
gr_<foo> would be derived from gr_block and gri_<foo> |
|---|
| 83 |
would be the low level guts of the function. |
|---|
| 84 |
|
|---|
| 85 |
atsc_ Code related to the Advanced Television |
|---|
| 86 |
Standards Committee HDTV implementation |
|---|
| 87 |
|
|---|
| 88 |
usrp_ Universal Software Radio Peripheral |
|---|
| 89 |
|
|---|
| 90 |
qa_ Quality Assurance. Test code. |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
** Class data members (instance variables) |
|---|
| 94 |
|
|---|
| 95 |
All class data members shall begin with d_<foo>. |
|---|
| 96 |
|
|---|
| 97 |
The big win is when you're staring at a block of code it's obvious |
|---|
| 98 |
which of the things being assigned to persist outside of the block. |
|---|
| 99 |
This also keeps you from having to be creative with parameter names |
|---|
| 100 |
for methods and constructors. You just use the same name as the |
|---|
| 101 |
instance variable, without the d_. |
|---|
| 102 |
|
|---|
| 103 |
class gr_wonderfulness { |
|---|
| 104 |
std::string d_name; |
|---|
| 105 |
double d_wonderfulness_factor; |
|---|
| 106 |
|
|---|
| 107 |
public: |
|---|
| 108 |
gr_wonderfulness (std::string name, double wonderfulness_factor) |
|---|
| 109 |
: d_name (name), d_wonderfulness_factor (wonderfulness_factor) |
|---|
| 110 |
{ |
|---|
| 111 |
... |
|---|
| 112 |
} |
|---|
| 113 |
... |
|---|
| 114 |
}; |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
** Class static data members (class variables) |
|---|
| 118 |
|
|---|
| 119 |
All class static data members shall begin with s_<foo>. |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
** File names |
|---|
| 123 |
|
|---|
| 124 |
Each significant class shall be contained in it's own file. The |
|---|
| 125 |
declaration of class gr_foo shall be in gr_foo.h, the definition in |
|---|
| 126 |
gr_foo.cc. |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
* Storage management |
|---|
| 131 |
|
|---|
| 132 |
Strongly consider using the boost smart pointer templates, scoped_ptr |
|---|
| 133 |
and shared_ptr. scoped_ptr should be used for locals that contain |
|---|
| 134 |
pointers to objects that we need to delete when we exit the current |
|---|
| 135 |
scope. shared_ptr implements transparent reference counting and is a |
|---|
| 136 |
major win. You never have to worry about calling delete. The right |
|---|
| 137 |
thing happens. |
|---|
| 138 |
|
|---|
| 139 |
See http://www.boost.org/libs/smart_ptr/smart_ptr.htm |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* Unit tests |
|---|
| 143 |
|
|---|
| 144 |
Build unit tests for everything non-trivial and run them after every |
|---|
| 145 |
change. Check out Extreme Programming: |
|---|
| 146 |
http://c2.com/cgi/wiki?ExtremeProgrammingRoadmap |
|---|
| 147 |
|
|---|
| 148 |
Unit tests should also be written for all examples. This should kill |
|---|
| 149 |
off the bit rot we've been plagued with. |
|---|
| 150 |
|
|---|
| 151 |
** C++ unit tests |
|---|
| 152 |
|
|---|
| 153 |
For C++ we're using the cppunit framework. cppunit has its bad |
|---|
| 154 |
smells, but it's mostly workable. http://cppunit.sf.net |
|---|
| 155 |
|
|---|
| 156 |
Currently each directory <dirname> contains files qa_<dirname>.{h,cc} |
|---|
| 157 |
that bring together all the qa_<foo> test suites in the directory. |
|---|
| 158 |
We ought to be able to automate this without too much trouble. |
|---|
| 159 |
|
|---|
| 160 |
The directory gnuradio-core/src/tests contains programs that run |
|---|
| 161 |
the tests. test_all runs all of the registered C++ unit tests. |
|---|
| 162 |
|
|---|
| 163 |
As far as I can tell, the cppunit TestFactoryRegistry maybe able to be |
|---|
| 164 |
tricked into doing what we want. As is, I don't think it's enough by |
|---|
| 165 |
itself, since there's nothing dragging the qa* files out of the |
|---|
| 166 |
library and into the program. I haven't tested out this idea. |
|---|
| 167 |
|
|---|
| 168 |
** Python unit tests |
|---|
| 169 |
|
|---|
| 170 |
We use the standard unittest package for unit testing of Python code. |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
* Subversion line ending styles |
|---|
| 174 |
|
|---|
| 175 |
All text files in the tree should have the subversion property |
|---|
| 176 |
'svn:eol-style' set to 'native', with the following exceptions: |
|---|
| 177 |
|
|---|
| 178 |
config/*.m4 |
|---|
| 179 |
configure.ac |
|---|
| 180 |
gr-howto-write-a-block/config/*.m4 |
|---|
| 181 |
gr-howto-write-a-block/configure.ac |
|---|
| 182 |
|
|---|
| 183 |
The easiest way to ensure this is to add or edit the following lines in |
|---|
| 184 |
your svn client configuration file (~/.subversion/config): |
|---|
| 185 |
|
|---|
| 186 |
enable-auto-props=yes |
|---|
| 187 |
|
|---|
| 188 |
[auto-props] |
|---|
| 189 |
*.c = svn:eol-style=native |
|---|
| 190 |
*.cc = svn:eol-style=native |
|---|
| 191 |
*.i = svn:eol-style=native |
|---|
| 192 |
*.h = svn:eol-style=native |
|---|
| 193 |
*.am = svn:eol-style=native |
|---|
| 194 |
*.py = svn:eol-style=native |
|---|
| 195 |
*.ac = svn:eol-style=LF |
|---|
| 196 |
*.m4 = svn:eol-style=LF |
|---|
| 197 |
|
|---|
| 198 |
* Misc tips |
|---|
| 199 |
|
|---|
| 200 |
ccache, a compiler cache, can really speed up your builds. |
|---|
| 201 |
See http://ccache.samba.org/ |
|---|
| 202 |
|
|---|
| 203 |
Be sure to create links for gcc and g++ |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
* Standard command line options |
|---|
| 207 |
|
|---|
| 208 |
When writing programs that are executable from the command line, |
|---|
| 209 |
please follow these guidelines for command line argument names (short |
|---|
| 210 |
and long) and types of the arguments. We list them below using the |
|---|
| 211 |
Python optparse syntax. In general, the default value should be coded |
|---|
| 212 |
into the help string using the "... [default=%default]" syntax. |
|---|
| 213 |
|
|---|
| 214 |
** Mandatory options by gr_block |
|---|
| 215 |
|
|---|
| 216 |
*** USRP source |
|---|
| 217 |
|
|---|
| 218 |
Any program using a USRP source (usrp.source_*) shall include: |
|---|
| 219 |
|
|---|
| 220 |
add_option("", "--which-usrp", type="intx", default=0, |
|---|
| 221 |
help="select which USRP to use [default=%default]") |
|---|
| 222 |
|
|---|
| 223 |
add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0), |
|---|
| 224 |
help="select USRP Rx side A or B [default=A]") |
|---|
| 225 |
|
|---|
| 226 |
You are free to change the default if it makes sense in your application. |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
*** USRP sink |
|---|
| 230 |
|
|---|
| 231 |
Any program using a USRP sink (usrp.sink_*) shall include: |
|---|
| 232 |
|
|---|
| 233 |
add_option("", "--which-usrp", type="intx", default=0, |
|---|
| 234 |
help="select which USRP to use [default=%default]") |
|---|
| 235 |
|
|---|
| 236 |
add_option("-T", "--tx-subdev-spec", type="subdev", default=(0, 0), |
|---|
| 237 |
help="select USRP Tx side A or B [default=A]") |
|---|
| 238 |
|
|---|
| 239 |
You are free to change the default if it makes sense in your application. |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
*** Audio source |
|---|
| 243 |
|
|---|
| 244 |
Any program using an audio source shall include: |
|---|
| 245 |
|
|---|
| 246 |
add_option("-I", "--audio-input", type="string", default="", |
|---|
| 247 |
help="pcm input device name. E.g., hw:0,0 or /dev/dsp") |
|---|
| 248 |
|
|---|
| 249 |
The default must be "". This allows an audio module-dependent default |
|---|
| 250 |
to be specified in the user preferences file. |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
*** Audio sink |
|---|
| 254 |
|
|---|
| 255 |
add_option("-O", "--audio-output", type="string", default="", |
|---|
| 256 |
help="pcm output device name. E.g., hw:0,0 or /dev/dsp") |
|---|
| 257 |
|
|---|
| 258 |
The default must be "". This allows an audio module-dependent default |
|---|
| 259 |
to be specified in the user preferences file. |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
** Standard options names by parameter |
|---|
| 263 |
|
|---|
| 264 |
Whenever you want an integer, use the "intx" type. This allows the |
|---|
| 265 |
user to input decimal, hex or octal numbers. E.g., 10, 012, 0xa. |
|---|
| 266 |
|
|---|
| 267 |
Whenever you want a float, use the "eng_float" type. This allows the |
|---|
| 268 |
user to input numbers with SI suffixes. E.g, 10000, 10k, 10M, 10m, 92.1M |
|---|
| 269 |
|
|---|
| 270 |
If your program allows the user to specify values for any of the |
|---|
| 271 |
following parameters, please use these options to specify them: |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
To specify a frequency (typically an RF center frequency) use: |
|---|
| 275 |
|
|---|
| 276 |
add_option("-f", "--freq", type="eng_float", default=<your-default-here>, |
|---|
| 277 |
help="set frequency to FREQ [default=%default]") |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
To specify a decimation factor use: |
|---|
| 281 |
|
|---|
| 282 |
add_option("-d", "--decim", type="intx", default=<your-default-here>, |
|---|
| 283 |
help="set decimation rate to DECIM [default=%default]") |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
To specify an interpolation factor use: |
|---|
| 287 |
|
|---|
| 288 |
add_option("-i", "--interp", type="intx", default=<your-default-here>, |
|---|
| 289 |
help="set interpolation rate to INTERP [default=%default]") |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
To specify a gain setting use: |
|---|
| 293 |
|
|---|
| 294 |
add_option("-g", "--gain", type="eng_float", default=<your-default-here>, |
|---|
| 295 |
help="set gain in dB [default=%default]") |
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
If your application specifies both a tx and an rx gain, use: |
|---|
| 299 |
|
|---|
| 300 |
add_option("", "--rx-gain", type="eng_float", default=<your-default-here>, |
|---|
| 301 |
help="set receive gain in dB [default=%default]") |
|---|
| 302 |
|
|---|
| 303 |
add_option("", "--tx-gain", type="eng_float", default=<your-default-here>, |
|---|
| 304 |
help="set transmit gain in dB [default=%default]") |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
To specify the number of channels of something use: |
|---|
| 308 |
|
|---|
| 309 |
add_option("-n", "--nchannels", type="intx", default=1, |
|---|
| 310 |
help="specify number of channels [default=%default]") |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
To specify an output filename use: |
|---|
| 314 |
|
|---|
| 315 |
add_option("-o", "--output-filename", type="string", default=<your-default-here>, |
|---|
| 316 |
help="specify output-filename [default=%default]") |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
To specify a rate use: |
|---|
| 320 |
|
|---|
| 321 |
add_option("-r", "--bit-rate", type="eng_float", default=<your-default-here>, |
|---|
| 322 |
help="specify bit-rate [default=%default]") |
|---|
| 323 |
or |
|---|
| 324 |
|
|---|
| 325 |
add_option("-r", "--sample-rate", type="eng_float", default=<your-default-here>, |
|---|
| 326 |
help="specify sample-rate [default=%default]") |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
If your application has a verbose option, use: |
|---|
| 330 |
|
|---|
| 331 |
add_option('-v', '--verbose', action="store_true", default=False, |
|---|
| 332 |
help="verbose output") |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
If your application allows the user to specify the "fast USB" options, use: |
|---|
| 336 |
|
|---|
| 337 |
add_option("", "--fusb-block-size", type="intx", default=0, |
|---|
| 338 |
help="specify fast usb block size [default=%default]") |
|---|
| 339 |
|
|---|
| 340 |
add_option("", "--fusb-nblocks", type="intx", default=0, |
|---|
| 341 |
help="specify number of fast usb blocks [default=%default]") |
|---|