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