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
274
275
276
277
278
279
</*
ZAP utility DVB functions
Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <sys/poll.h>
#include <libdvbapi/dvbdemux.h>
#include <libucsi/section.h>
#include <libucsi/mpeg/section.h>
#include <libucsi/dvb/section.h>
#include "zap_dvb.h"
#include "zap_ca.h"
#define FE_STATUS_PARAMS (DVBFE_INFO_LOCKSTATUS|DVBFE_INFO_SIGNAL_STRENGTH|DVBFE_INFO_BER|DVBFE_INFO_SNR|DVBFE_INFO_UNCORRECTED_BLOCKS)
static int dvbthread_shutdown = 0;
static pthread_t dvbthread;
static int pat_version = -1;
static int ca_pmt_version = -1;
static void *dvbthread_func(void* arg);
static void process_pat(int pat_fd, struct zap_dvb_params *params, int *pmt_fd, struct pollfd *pollfd);
static void process_tdt(int tdt_fd);
static void process_pmt(int pmt_fd, struct zap_dvb_params *params);
static int create_section_filter(int adapter, int demux, uint16_t pid, uint8_t table_id);
int zap_dvb_start(struct zap_dvb_params *params)
{
pthread_create(&dvbthread, NULL, dvbthread_func, (void*) params);
return 0;
}
void zap_dvb_stop(void)
{
dvbthread_shutdown = 1;
pthread_join(dvbthread, NULL);
}
static void *dvbthread_func(void* arg)
{
int tune_state = 0;
int pat_fd = -1;
int pmt_fd = -1;
int tdt_fd = -1;
struct pollfd pollfds[3];
struct zap_dvb_params *params = (struct zap_dvb_params *) arg;
// create PAT filter
if ((pat_fd = create_section_filter(params->adapter_id, params->demux_id,
TRANSPORT_PAT_PID, stag_mpeg_program_association)) < 0) {
fprintf(stderr, "Failed to create PAT section filter\n");
exit(1);
}
pollfds[0].fd = pat_fd;
pollfds[0].events = POLLIN|POLLPRI|POLLERR;
// create TDT filter
if ((tdt_fd = create_section_filter(params->adapter_id, params->demux_id, TRANSPORT_TDT_PID, stag_dvb_time_date)) < 0) {
fprintf(stderr, "Failed to create TDT section filter\n");
exit(1);
}
pollfds[1].fd = tdt_fd;
pollfds[1].events = POLLIN|POLLPRI|POLLERR;
// zero PMT filter
pollfds[2].fd = 0;
pollfds[2].events = 0;
// the DVB loop
while(!dvbthread_shutdown) {
// tune frontend + monitor lock status
if (tune_state == 0) {
// get the type of frontend
struct dvbfe_info result;
char *types;
memset(&result, 0, sizeof(result));
dvbfe_get_info(params->fe, 0, &result, DVBFE_INFO_QUERYTYPE_IMMEDIATE, 0);
switch(result.type) {
case DVBFE_TYPE_DVBS:
types = "DVB-S";
break;
case DVBFE_TYPE_DVBC:
types = "DVB-C";
break;
case DVBFE_TYPE_DVBT:
types = "DVB-T";
break;
case DVBFE_TYPE_ATSC:
types = "ATSC";
break;
default:
types = "Unknown";
}
fprintf(stderr, "Using frontend \"%s\", type %s\n", result.name, types);
// do we have a valid SEC configuration?
struct dvbsec_config *sec = NULL;
if (params->valid_sec)
sec = ¶ms->sec;
// tune!
if (dvbsec_set(params->fe,
sec,
params->channel.polarization,
(params->channel.diseqc_switch & 0x01) ? DISEQC_SWITCH_B : DISEQC_SWITCH_A,
(params->channel.diseqc_switch & 0x02) ? DISEQC_SWITCH_B : DISEQC_SWITCH_A,
¶ms->channel.fe_params,
0)) {
fprintf(stderr, "Failed to set frontend\n");
exit(1);
}
tune_state++;
} else if (tune_state == 1) {
struct dvbfe_info result;
memset(&result, 0, sizeof(result));
if (dvbfe_get_info(params->fe,
FE_STATUS_PARAMS,
&result,
DVBFE_INFO_QUERYTYPE_IMMEDIATE,
0) != FE_STATUS_PARAMS) {
fprintf(stderr, "Problem retrieving frontend information: %m\n");
}
fprintf(stderr, "status %c%c%c%c%c | signal %04x | snr %04x | ber %08x | unc %08x | %s\r",
result.signal ? 'S' : ' ',
result.carrier ? 'C' : ' ',
result.viterbi ? 'V' : ' ',
result.sync ? 'Y' : ' ',
result.lock ? 'L' : ' ',
result.signal_strength,
result.snr,
result.ber,
result.ucblocks,
result.lock ? "FE_HAS_LOCK" : "");
fflush(stderr);
if (result.lock) {
tune_state++;
fprintf(stderr, "\n");
fflush(stderr);
} else {
usleep(500000);
}
}
// is there SI data?
|