1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;16.&#160;Examples</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="LINUX MEDIA INFRASTRUCTURE API"><link rel="up" href="dvbapi.html" title="Part&#160;II.&#160;LINUX DVB API"><link rel="prev" href="ch15s11.html" title="stop_filtering()"><link rel="next" href="the_dvr_device.html" title="The DVR device"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;16.&#160;Examples</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch15s11.html">Prev</a>&#160;</td><th width="60%" align="center">Part&#160;II.&#160;LINUX DVB API</th><td width="20%" align="right">&#160;<a accesskey="n" href="the_dvr_device.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="dvb_examples"></a>Chapter&#160;16.&#160;Examples</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="dvb_examples.html#tuning">Tuning</a></span></dt><dt><span class="section"><a href="the_dvr_device.html">The DVR device</a></span></dt></dl></div><p>In this section we would like to present some examples for using the DVB API.
2</p><p>Maintainer note: This section is out of date. Please refer to the sample programs packaged
3with the driver distribution from <a class="ulink" href="http://linuxtv.org/hg/dvb-apps" target="_top">http://linuxtv.org/hg/dvb-apps</a>.
4</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="tuning"></a>Tuning</h2></div></div></div><p>We will start with a generic tuning subroutine that uses the frontend and SEC, as well as
5the demux devices. The example is given for QPSK tuners, but can easily be adjusted for
6QAM.
7</p><pre class="programlisting">
8 #include &lt;sys/ioctl.h&gt;
9 #include &lt;stdio.h&gt;
10 #include &lt;stdint.h&gt;
11 #include &lt;sys/types.h&gt;
12 #include &lt;sys/stat.h&gt;
13 #include &lt;fcntl.h&gt;
14 #include &lt;time.h&gt;
15 #include &lt;unistd.h&gt;
16
17 #include &lt;linux/dvb/dmx.h&gt;
18 #include &lt;linux/dvb/frontend.h&gt;
19 #include &lt;linux/dvb/sec.h&gt;
20 #include &lt;sys/poll.h&gt;
21
22 #define DMX "/dev/dvb/adapter0/demux1"
23 #define FRONT "/dev/dvb/adapter0/frontend1"
24 #define SEC "/dev/dvb/adapter0/sec1"
25
26 /&#8902; routine for checking if we have a signal and other status information&#8902;/
27 int FEReadStatus(int fd, fe_status_t &#8902;stat)
28 {
29	 int ans;
30
31	 if ( (ans = ioctl(fd,FE_READ_STATUS,stat) &lt; 0)){
32		 perror("FE READ STATUS: ");
33		 return -1;
34	 }
35
36	 if (&#8902;stat &amp; FE_HAS_POWER)
37		 printf("FE HAS POWER\n");
38
39	 if (&#8902;stat &amp; FE_HAS_SIGNAL)
40		 printf("FE HAS SIGNAL\n");
41
42	 if (&#8902;stat &amp; FE_SPECTRUM_INV)
43		 printf("SPEKTRUM INV\n");
44
45	 return 0;
46 }
47
48
49 /&#8902; tune qpsk &#8902;/
50 /&#8902; freq:             frequency of transponder                      &#8902;/
51 /&#8902; vpid, apid, tpid: PIDs of video, audio and teletext TS packets  &#8902;/
52 /&#8902; diseqc:           DiSEqC address of the used LNB                &#8902;/
53 /&#8902; pol:              Polarisation                                  &#8902;/
54 /&#8902; srate:            Symbol Rate                                   &#8902;/
55 /&#8902; fec.              FEC                                           &#8902;/
56 /&#8902; lnb_lof1:         local frequency of lower LNB band             &#8902;/
57 /&#8902; lnb_lof2:         local frequency of upper LNB band             &#8902;/
58 /&#8902; lnb_slof:         switch frequency of LNB                       &#8902;/
59
60 int set_qpsk_channel(int freq, int vpid, int apid, int tpid,
61		 int diseqc, int pol, int srate, int fec, int lnb_lof1,
62		 int lnb_lof2, int lnb_slof)
63 {
64	 struct secCommand scmd;
65	 struct secCmdSequence scmds;
66	 struct dmx_pes_filter_params pesFilterParams;
67	 FrontendParameters frp;
68	 struct pollfd pfd[1];
69	 FrontendEvent event;
70	 int demux1, demux2, demux3, front;
71
72	 frequency = (uint32_t) freq;
73	 symbolrate = (uint32_t) srate;
74
75	 if((front = open(FRONT,O_RDWR)) &lt; 0){
76		 perror("FRONTEND DEVICE: ");
77		 return -1;
78	 }
79
80	 if((sec = open(SEC,O_RDWR)) &lt; 0){
81		 perror("SEC DEVICE: ");
82		 return -1;
83	 }
84
85	 if (demux1 &lt; 0){
86		 if ((demux1=open(DMX, O_RDWR|O_NONBLOCK))
87		     &lt; 0){
88			 perror("DEMUX DEVICE: ");
89			 return -1;
90		 }
91	 }
92
93	 if (demux2 &lt; 0){
94		 if ((demux2=open(DMX, O_RDWR|O_NONBLOCK))
95		     &lt; 0){
96			 perror("DEMUX DEVICE: ");
97			 return -1;
98		 }
99	 }
100
101	 if (demux3 &lt; 0){
102		 if ((demux3=open(DMX, O_RDWR|O_NONBLOCK))
103		     &lt; 0){
104			 perror("DEMUX DEVICE: ");
105			 return -1;
106		 }
107	 }
108
109	 if (freq &lt; lnb_slof) {
110		 frp.Frequency = (freq - lnb_lof1);
111		 scmds.continuousTone = SEC_TONE_OFF;
112	 } else {
113		 frp.Frequency = (freq - lnb_lof2);
114		 scmds.continuousTone = SEC_TONE_ON;
115	 }
116	 frp.Inversion = INVERSION_AUTO;
117	 if (pol) scmds.voltage = SEC_VOLTAGE_18;
118	 else scmds.voltage = SEC_VOLTAGE_13;
119
120	 scmd.type=0;
121	 scmd.u.diseqc.addr=0x10;
122	 scmd.u.diseqc.cmd=0x38;
123	 scmd.u.diseqc.numParams=1;
124	 scmd.u.diseqc.params[0] = 0xF0 | ((diseqc &#8902; 4) &amp; 0x0F) |
125		 (scmds.continuousTone == SEC_TONE_ON ? 1 : 0) |
126		 (scmds.voltage==SEC_VOLTAGE_18 ? 2 : 0);
127
128	 scmds.miniCommand=SEC_MINI_NONE;
129	 scmds.numCommands=1;
130	 scmds.commands=&amp;scmd;
131	 if (ioctl(sec, SEC_SEND_SEQUENCE, &amp;scmds) &lt; 0){
132		 perror("SEC SEND: ");
133		 return -1;
134	 }
135
136	 if (ioctl(sec, SEC_SEND_SEQUENCE, &amp;scmds) &lt; 0){
137		 perror("SEC SEND: ");
138		 return -1;
139	 }
140
141	 frp.u.qpsk.SymbolRate = srate;
142	 frp.u.qpsk.FEC_inner = fec;
143
144	 if (ioctl(front, FE_SET_FRONTEND, &amp;frp) &lt; 0){
145		 perror("QPSK TUNE: ");
146		 return -1;
147	 }
148
149	 pfd[0].fd = front;
150	 pfd[0].events = POLLIN;
151
152	 if (poll(pfd,1,3000)){
153		 if (pfd[0].revents &amp; POLLIN){
154			 printf("Getting QPSK event\n");
155			 if ( ioctl(front, FE_GET_EVENT, &amp;event)
156
157			      == -EOVERFLOW){
158				 perror("qpsk get event");
159				 return -1;
160			 }
161			 printf("Received ");
162			 switch(event.type){
163			 case FE_UNEXPECTED_EV:
164				 printf("unexpected event\n");
165				 return -1;
166			 case FE_FAILURE_EV:
167				 printf("failure event\n");
168				 return -1;
169
170			 case FE_COMPLETION_EV:
171				 printf("completion event\n");
172			 }
173		 }
174	 }
175
176
177	 pesFilterParams.pid     = vpid;
178	 pesFilterParams.input   = DMX_IN_FRONTEND;
179	 pesFilterParams.output  = DMX_OUT_DECODER;
180	 pesFilterParams.pes_type = DMX_PES_VIDEO;
181	 pesFilterParams.flags   = DMX_IMMEDIATE_START;
182	 if (ioctl(demux1, DMX_SET_PES_FILTER, &amp;pesFilterParams) &lt; 0){
183		 perror("set_vpid");
184		 return -1;
185	 }
186
187	 pesFilterParams.pid     = apid;
188	 pesFilterParams.input   = DMX_IN_FRONTEND;
189	 pesFilterParams.output  = DMX_OUT_DECODER;
190	 pesFilterParams.pes_type = DMX_PES_AUDIO;
191	 pesFilterParams.flags   = DMX_IMMEDIATE_START;
192	 if (ioctl(demux2, DMX_SET_PES_FILTER, &amp;pesFilterParams) &lt; 0){
193		 perror("set_apid");
194		 return -1;
195	 }
196
197	 pesFilterParams.pid     = tpid;
198	 pesFilterParams.input   = DMX_IN_FRONTEND;
199	 pesFilterParams.output  = DMX_OUT_DECODER;
200	 pesFilterParams.pes_type = DMX_PES_TELETEXT;
201	 pesFilterParams.flags   = DMX_IMMEDIATE_START;
202	 if (ioctl(demux3, DMX_SET_PES_FILTER, &amp;pesFilterParams) &lt; 0){
203		 perror("set_tpid");
204		 return -1;
205	 }
206
207	 return has_signal(fds);
208 }
209
210</pre><p>The program assumes that you are using a universal LNB and a standard DiSEqC
211switch with up to 4 addresses. Of course, you could build in some more checking if
212tuning was successful and maybe try to repeat the tuning process. Depending on the
213external hardware, i.e. LNB and DiSEqC switch, and weather conditions this may be
214necessary.
215</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch15s11.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="dvbapi.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="the_dvr_device.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">stop_filtering()&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;The DVR device</td></tr></table></div></body></html>
216