aboutsummaryrefslogtreecommitdiffstats
path: root/grc/gen_osmosdr_blocks.py
blob: a9d90809099c96c6f952c4787e63874a82b221ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""
Copyright 2012 Free Software Foundation, Inc.

This file is part of GNU Radio

GNU Radio Companion is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

GNU Radio Companion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
"""

MAIN_TMPL = """\
<?xml version="1.0"?>
<block>
	<name>$(title) $sourk.title()</name>
	<key>$(prefix)_$(sourk)_c</key>
	<category>Sources</category>
	<throttle>1</throttle>
	<import>import osmosdr</import>
	<make>osmosdr.$(sourk)_c( args="nchan=" + str(\$nchan) + " " + \$args )
self.\$(id).set_sample_rate(\$sample_rate)
#for $n in range($max_nchan)
\#if \$nchan() > $n
self.\$(id).set_center_freq(\$freq$(n), $n)
self.\$(id).set_freq_corr(\$corr$(n), $n)
self.\$(id).set_gain_mode(\$gain_mode$(n), $n)
self.\$(id).set_gain(\$gain$(n), $n)
self.\$(id).set_if_gain(\$if_gain$(n), $n)
\#if \$ant$(n)()
self.\$(id).set_antenna(\$ant$(n), $n)
\#end if
\#end if
#end for
	</make>
	<callback>set_sample_rate(\$sample_rate)</callback>
	#for $n in range($max_nchan)
	<callback>set_center_freq(\$freq$(n), $n)</callback>
	<callback>set_freq_corr(\$corr$(n), $n)</callback>
	<callback>set_gain_mode(\$gain_mode$(n), $n)</callback>
	<callback>set_gain(\$gain$(n), $n)</callback>
	<callback>set_if_gain(\$if_gain$(n), $n)</callback>
	<callback>set_antenna(\$ant$(n), $n)</callback>
	#end for
	<param>
		<name>$(dir.title())put Type</name>
		<key>type</key>
		<type>enum</type>
		<option>
			<name>Complex float32</name>
			<key>fc32</key>
			<opt>type:fc32</opt>
		</option>
	</param>
	<param>
		<name>Device Arguments</name>
		<key>args</key>
		<value></value>
		<type>string</type>
		<hide>
			\#if \$args()
				none
			\#else
				part
			\#end if
		</hide>
	</param>
	<param>
		<name>Num Channels</name>
		<key>nchan</key>
		<value>1</value>
		<type>int</type>
		#for $n in range(1, $max_nchan+1)
		<option>
			<name>$(n)</name>
			<key>$n</key>
		</option>
		#end for
	</param>
	<param>
		<name>Sample Rate (sps)</name>
		<key>sample_rate</key>
		<value>samp_rate</value>
		<type>real</type>
	</param>
	$params
	<check>$max_nchan >= \$nchan</check>
	<check>\$nchan > 0</check>
	<$sourk>
		<name>$dir</name>
		<type>\$type.type</type>
		<nports>\$nchan</nports>
	</$sourk>
	<doc>
The OsmoSDR $sourk.title() block:

While primarily being developed for the OsmoSDR hardware, this block as well supports:

 * FunCube Dongle through libgnuradio-fcd
 * OSMOCOM OsmoSDR Devices through libosmosdr
 * Ettus USRP Devices through Ettus UHD library
 * RTL2832U based DVB-T dongles through librtlsdr
 * RTL-TCP spectrum server (see librtlsdr project)
 * MSi2500 based DVB-T dongles through libmirisdr
 * gnuradio .cfile input through libgnuradio-core

By using the OsmoSDR block you can take advantage of a common software api in your application(s) independent of the underlying radio hardware.

Output Type:
This parameter controls the data type of the stream in gnuradio. Only complex float32 samples are supported at the moment.

Device Arguments:
The device argument is a comma delimited string used to locate devices on your system. Device arguments for multiple devices may be given by separating them with a space.
Use the device id or name/serial (if applicable) to specify a certain device or list of devices. If left blank, the first device found will be used.

Examples:

Optional arguments are placed into [] brackets, remove the brackets before using them! Specific variable values are separated with a |, choose one of them. Variable values containing spaces shall be enclosed in '' as demonstrated in examples section below.
Lines ending with ... mean it's possible to bind devices together by specifying multiple device arguments separated with a space.

  fcd=0
  miri=0[,buffers=32] ...
  rtl=serial_number ...
  rtl=0[,rtl_xtal=28.8e6][,tuner_xtal=28.8e6] ...
  rtl=1[,buffers=32][,buflen=N*512] ...
  rtl=2[,direct_samp=0|1|2][,offset_tune=0|1] ...
  rtl_tcp=127.0.0.1:1234[,psize=16384] ...
  uhd[,serial=...][,lo_offset=0][,mcr=52e6][,nchan=2][,subdev='\\\\'B:0 A:0\\\\''] ...
  osmosdr=0[,mcr=64e6][,nchan=5][,buffers=32][,buflen=N*512] ...
  file='/path/to/your file',rate=1e6[,freq=100e6][,repeat=true][,throttle=true] ...

Num Channels:
Selects the total number of channels in this multi-device configuration. Required when specifying multiple device arguments.

Sample Rate:
The sample rate is the number of samples per second output by this block on each channel.

Frequency:
The center frequency is the frequency the RF chain is tuned to.

Freq. Corr.:
The frequency correction factor in parts per million (ppm). Set to 0 if unknown.

Gain Mode:
Chooses between the manual (default) and automatic gain mode where appropriate.
Currently, only rtlsdr devices support automatic gain mode.

RF Gain:
Overall RF gain of the receiving device. For the new gain value to be applied, the manual gain mode must be enabled first.

IF Gain:
Overall IF gain of the receiving device. For the new gain value to be applied, the manual gain mode must be enabled first.
This setting has only effect for RTL-SDR and OsmoSDR devices with E4000 tuners. Observations lead to a reasonable gain range from 15 to 30dB.

Antenna:
For devices with only one antenna, this may be left blank.
Otherwise, the user should specify one of the possible antenna choices.

See the OsmoSDR project page for more detailed documentation:
http://sdr.osmocom.org/trac/
http://sdr.osmocom.org/trac/wiki/rtl-sdr
http://sdr.osmocom.org/trac/wiki/GrOsmoSDR
	</doc>
</block>
"""

PARAMS_TMPL = """
	<param>
		<name>Ch$(n): Frequency (Hz)</name>
		<key>freq$(n)</key>
		<value>100e6</value>
		<type>real</type>
		<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
	</param>
	<param>
	<name>Ch$(n): Freq. Corr. (ppm)</name>
		<key>corr$(n)</key>
		<value>0</value>
		<type>real</type>
		<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
	</param>
	<param>
		<name>Ch$(n): Gain Mode</name>
		<key>gain_mode$(n)</key>
		<value>0</value>
		<type>int</type>
		<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
		<option>
			<name>Manual</name>
			<key>0</key>
		</option>
		<option>
			<name>Auto</name>
			<key>1</key>
		</option>
	</param>
	<param>
		<name>Ch$(n): RF Gain (dB)</name>
		<key>gain$(n)</key>
		<value>10</value>
		<type>real</type>
		<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
	</param>
	<param>
		<name>Ch$(n): IF Gain (dB)</name>
		<key>if_gain$(n)</key>
		<value>24</value>
		<type>real</type>
		<hide>\#if \$nchan() > $n then 'none' else 'all'#</hide>
	</param>
	<param>
		<name>Ch$(n): Antenna</name>
		<key>ant$(n)</key>
		<value></value>
		<type>string</type>
		<hide>
			\#if not \$nchan() > $n
				all
			\#elif \$ant$(n)()
				none
			\#else
				part
			\#end if
		</hide>
	</param>
"""

def parse_tmpl(_tmpl, **kwargs):
	from Cheetah import Template
	return str(Template.Template(_tmpl, kwargs))

max_num_channels = 5

import os.path

if __name__ == '__main__':
	import sys
	for file in sys.argv[1:]:
		head, tail = os.path.split(file)

		if tail.startswith('rtlsdr'):
			title = 'RTLSDR'
			prefix = 'rtlsdr'
		elif tail.startswith('osmosdr'):
			title = 'OsmoSDR'
			prefix = 'osmosdr'
		else: raise Exception, 'file %s has wrong syntax!'%tail

		if tail.endswith ('source_c.xml'):
			sourk = 'source'
			dir = 'out'
		elif tail.endswith ('sink_c.xml'):
			sourk = 'sink'
			dir = 'in'
		else: raise Exception, 'is %s a source or sink?'%file

		params = ''.join([parse_tmpl(PARAMS_TMPL, n=n) for n in range(max_num_channels)])
		open(file, 'w').write(parse_tmpl(MAIN_TMPL,
			max_nchan=max_num_channels,
			params=params,
			title=title,
			prefix=prefix,
			sourk=sourk,
			dir=dir,
		))