]> git.sesse.net Git - vlc/blob - plugins/satellite/satellite_tools.c
* ./configure.in, ./plugins/network/ipv6.c: support for the GNU glibc
[vlc] / plugins / satellite / satellite_tools.c
1 /*****************************************************************************
2  * linux_dvb_tools.c : functions to control a DVB card under Linux
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  *
6  * Authors: Damien Lucas <nitrox@via.ecp.fr>
7  *          Johan Bilien <jobi@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA    02111, USA.
22  *****************************************************************************/
23
24 #include <sys/ioctl.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/poll.h>
35
36 /* DVB Card Drivers */
37 #include <ost/sec.h>
38 #include <ost/dmx.h>
39 #include <ost/frontend.h>
40
41
42 #include "satellite_tools.h"
43
44 /*****************************************************************************
45  * ioctl_SECControl : commands the SEC device
46  *****************************************************************************/
47
48
49 int ioctl_SECControl( int freq, int pol, int lnb_slof, int diseqc)
50 {
51     struct secCommand scmd;
52     struct secCmdSequence scmds;
53     int sec;
54
55     if((sec = open(SEC,O_RDWR)) < 0)
56     {
57         return -1;
58     }
59
60     /* Set the frequency of the transponder, taking into account the
61        local frequencies of the LNB */
62     scmds.continuousTone = (freq<lnb_slof) ? SEC_TONE_OFF : SEC_TONE_ON;
63
64     /* Set the polarity of the transponder by setting the correct
65        voltage on the universal LNB */
66     scmds.voltage = (pol) ? SEC_VOLTAGE_18 : SEC_VOLTAGE_13;
67
68     /* In case we have a DiSEqC, set it to the correct address */
69     scmd.type=0;
70     scmd.u.diseqc.addr=0x10;
71     scmd.u.diseqc.cmd=0x38;
72     scmd.u.diseqc.numParams=1;
73     scmd.u.diseqc.params[0] = 0xF0 | ((diseqc * 4) & 0x0F) | 
74             (scmds.continuousTone == SEC_TONE_ON ? 1 : 0) |
75             (scmds.voltage==SEC_VOLTAGE_18 ? 2 : 0);
76
77     scmds.miniCommand=SEC_MINI_NONE;
78     scmds.numCommands=1;
79     scmds.commands=&scmd;
80
81     /* Send the data to the SEC device to prepare the LNB for tuning  */
82     /*intf_Msg("Sec: Sending data\n");*/
83     if (ioctl(sec, SEC_SEND_SEQUENCE, &scmds) < 0)
84     {
85         return -1;
86     }
87
88     close(sec);
89
90     return 0;
91 }
92
93 static int check_qpsk( int );
94
95 /*****************************************************************************
96  * ioctl_SetQPSKFrontend : controls the FE device
97  *****************************************************************************/
98
99 int ioctl_SetQPSKFrontend (int freq, int srate, int fec,\
100                         int lnb_lof1, int lnb_lof2, int lnb_slof)
101 {
102     FrontendParameters fep;
103     int front;
104     int rc;
105
106     /* Open the frontend device */
107     if((front = open(FRONTEND,O_RDWR)) < 0)
108     {
109         return -1;
110     }
111
112     /* Set the frequency of the transponder, taking into account the
113        local frequencies of the LNB */
114     fep.Frequency = (freq < lnb_slof) ? freq - lnb_lof1 : freq - lnb_lof2; 
115
116  /* Set symbol rate and FEC */
117     fep.u.qpsk.SymbolRate = srate;
118     fep.u.qpsk.FEC_inner = FEC_AUTO;
119
120     /* Now send it all to the frontend device */
121     if (ioctl(front, FE_SET_FRONTEND, &fep) < 0)
122     {
123         return -1;
124     }
125
126     /* Check if it worked */
127     rc=check_qpsk(front);
128
129     /* Close front end device */
130     close(front);
131     
132     return rc;
133 }
134
135
136
137 /******************************************************************
138  * Check completion of the frontend control sequence
139  ******************************************************************/
140 static int check_qpsk(int front)
141 {
142     struct pollfd pfd[1];
143     FrontendEvent event; 
144     /* poll for QPSK event to check if tuning worked */
145     pfd[0].fd = front;
146     pfd[0].events = POLLIN;
147
148     if (poll(pfd,1,3000))
149     {
150         if (pfd[0].revents & POLLIN)
151         {
152             if ( ioctl(front, FE_GET_EVENT, &event) == -EBUFFEROVERFLOW)
153             {
154                 return -5;
155             }
156         
157             switch(event.type)
158             {
159                 case FE_UNEXPECTED_EV:
160                     return -2;
161                 case FE_FAILURE_EV:
162                     return -1;
163                 case FE_COMPLETION_EV:
164                     break;
165             }
166         }
167         else
168         {
169             /* should come here */
170             return -3;
171         }
172     }
173     else
174     {
175         return -4;
176     }
177     
178     return 0;
179 }
180
181
182 /*****************************************************************************
183  * ioctl_SetDMXAudioFilter : controls the demux to add a filter
184  *****************************************************************************/
185
186 int ioctl_SetDMXFilter( int i_pid, int * pi_fd , int i_type ) 
187 {
188     struct dmxPesFilterParams s_filter_params;
189     
190     /* We first open the device */
191     if ((*pi_fd = open(DMX, O_RDWR|O_NONBLOCK))  < 0)
192     {
193         return -1;
194     }
195
196     /* We fill the DEMUX structure : */
197     s_filter_params.pid     =   i_pid;
198     s_filter_params.input   =   DMX_IN_FRONTEND;
199     s_filter_params.output  =   DMX_OUT_TS_TAP;
200     switch ( i_type )
201     {
202         case 1:
203             s_filter_params.pesType =   DMX_PES_VIDEO;
204             break;
205         case 2:
206             s_filter_params.pesType =   DMX_PES_AUDIO;
207             break;
208         case 3:
209             s_filter_params.pesType =   DMX_PES_OTHER;
210             break;
211     }
212     s_filter_params.flags   =   DMX_IMMEDIATE_START;
213
214     /* We then give the order to the device : */
215     if (ioctl(*pi_fd, DMX_SET_PES_FILTER, &s_filter_params) < 0)
216     {
217         return -1;
218     }
219
220     return 0;
221 }
222
223 /*****************************************************************************
224  * ioctl_UnsetDMXFilter : removes a filter
225  *****************************************************************************/
226 int ioctl_UnsetDMXFilter(int demux)
227 {
228     ioctl(demux, DMX_STOP);
229     close(demux);
230     return 0;
231 }