comparison src/checksignal.c @ 124:9c7bc6c0327e

Add DLNA server function test. (from uShare project)
author naoyan@johnstown.minaminoshima.org
date Wed, 29 Sep 2010 23:18:55 +0900
parents recpt1/checksignal.c@38a793ac3d9d
children 4e39ce051c57
comparison
equal deleted inserted replaced
123:215a51fa3df3 124:9c7bc6c0327e
1 /* -*- tab-width: 4; indent-tabs-mode: nil -*- */
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <time.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <pthread.h>
10 #include <math.h>
11 #include <unistd.h>
12 #include <getopt.h>
13 #include <signal.h>
14 #include <errno.h>
15 #include <sys/time.h>
16 #include <ctype.h>
17 #include <libgen.h>
18
19 #include <netdb.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22
23 #include <sys/ioctl.h>
24 #include "pt1_ioctl.h"
25
26 #include "config.h"
27 #include "decoder.h"
28 #include "recpt1.h"
29 #include "version.h"
30 #include "mkpath.h"
31
32 #include <sys/ipc.h>
33 #include <sys/msg.h>
34 #include "pt1_dev.h"
35 #include "tssplitter_lite.h"
36
37 #define MAX_RETRY (2)
38
39 /* type definitions */
40 typedef int boolean;
41
42 typedef struct thread_data {
43 int ch;
44 int lnb; /* LNB voltage */
45 int tfd; /* tuner fd */
46 ISDB_T_FREQ_CONV_TABLE *table;
47 } thread_data;
48
49 /* globals */
50 boolean f_exit = FALSE;
51 boolean use_bell = FALSE;
52
53 /* prototypes */
54 int tune(char *channel, thread_data *tdata, char *device);
55 int close_tuner(thread_data *tdata);
56
57 void
58 cleanup(thread_data *tdata)
59 {
60 f_exit = TRUE;
61 }
62
63 /* will be signal handler thread */
64 void *
65 process_signals(void *data)
66 {
67 sigset_t waitset;
68 int sig;
69 thread_data *tdata = (thread_data *)data;
70
71 sigemptyset(&waitset);
72 sigaddset(&waitset, SIGINT);
73 sigaddset(&waitset, SIGTERM);
74 sigaddset(&waitset, SIGUSR1);
75
76 sigwait(&waitset, &sig);
77
78 switch(sig) {
79 case SIGINT:
80 fprintf(stderr, "\nSIGINT received. cleaning up...\n");
81 cleanup(tdata);
82 break;
83 case SIGTERM:
84 fprintf(stderr, "\nSIGTERM received. cleaning up...\n");
85 cleanup(tdata);
86 break;
87 case SIGUSR1: /* normal exit*/
88 cleanup(tdata);
89 break;
90 }
91
92 return NULL; /* dummy */
93 }
94
95 void
96 init_signal_handlers(pthread_t *signal_thread, thread_data *tdata)
97 {
98 sigset_t blockset;
99
100 sigemptyset(&blockset);
101 sigaddset(&blockset, SIGINT);
102 sigaddset(&blockset, SIGTERM);
103 sigaddset(&blockset, SIGUSR1);
104
105 if(pthread_sigmask(SIG_BLOCK, &blockset, NULL))
106 fprintf(stderr, "pthread_sigmask() failed.\n");
107
108 pthread_create(signal_thread, NULL, process_signals, tdata);
109 }
110
111 /* lookup frequency conversion table*/
112 ISDB_T_FREQ_CONV_TABLE *
113 searchrecoff(char *channel)
114 {
115 int lp;
116
117 for(lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) {
118 /* return entry number in the table when strings match and
119 * lengths are same. */
120 if((memcmp(isdb_t_conv_table[lp].parm_freq, channel,
121 strlen(channel)) == 0) &&
122 (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))) {
123 return &isdb_t_conv_table[lp];
124 }
125 }
126 return NULL;
127 }
128
129 void
130 show_usage(char *cmd)
131 {
132 fprintf(stderr, "Usage: \n%s [--device devicefile] [--lnb voltage] [--bell] channel\n", cmd);
133 fprintf(stderr, "\n");
134 }
135
136 void
137 show_options(void)
138 {
139 fprintf(stderr, "Options:\n");
140 fprintf(stderr, "--device devicefile: Specify devicefile to use\n");
141 fprintf(stderr, "--lnb voltage: Specify LNB voltage (0, 11, 15)\n");
142 fprintf(stderr, "--bell: Notify signal quality by bell\n");
143 fprintf(stderr, "--help: Show this help\n");
144 fprintf(stderr, "--version: Show version\n");
145 fprintf(stderr, "--list: Show channel list\n");
146 }
147
148 void
149 show_channels(void)
150 {
151 FILE *f;
152 char *home;
153 char buf[255], filename[255];
154
155 fprintf(stderr, "Available Channels:\n");
156
157 home = getenv("HOME");
158 sprintf(filename, "%s/.recpt1-channels", home);
159 f = fopen(filename, "r");
160 if(f) {
161 while(fgets(buf, 255, f))
162 fprintf(stderr, "%s", buf);
163 fclose(f);
164 }
165 else
166 fprintf(stderr, "13-62: Terrestrial Channels\n");
167
168 fprintf(stderr, "101ch: NHK BS1\n");
169 fprintf(stderr, "102ch: NHK BS2\n");
170 fprintf(stderr, "103ch: NHK BShi\n");
171 fprintf(stderr, "141ch: BS Nittele\n");
172 fprintf(stderr, "151ch: BS Asahi\n");
173 fprintf(stderr, "161ch: BS-TBS\n");
174 fprintf(stderr, "171ch: BS Japan\n");
175 fprintf(stderr, "181ch: BS Fuji\n");
176 fprintf(stderr, "191ch: WOWOW\n");
177 fprintf(stderr, "192ch: WOWOW2\n");
178 fprintf(stderr, "193ch: WOWOW3\n");
179 fprintf(stderr, "200ch: Star Channel\n");
180 fprintf(stderr, "211ch: BS11 Digital\n");
181 fprintf(stderr, "222ch: TwellV\n");
182 fprintf(stderr, "C13-C63: CATV Channels\n");
183 fprintf(stderr, "CS2-CS24: CS Channels\n");
184 }
185
186 float
187 getsignal_isdb_s(int signal)
188 {
189 /* apply linear interpolation */
190 static const float afLevelTable[] = {
191 24.07f, // 00 00 0 24.07dB
192 24.07f, // 10 00 4096 24.07dB
193 18.61f, // 20 00 8192 18.61dB
194 15.21f, // 30 00 12288 15.21dB
195 12.50f, // 40 00 16384 12.50dB
196 10.19f, // 50 00 20480 10.19dB
197 8.140f, // 60 00 24576 8.140dB
198 6.270f, // 70 00 28672 6.270dB
199 4.550f, // 80 00 32768 4.550dB
200 3.730f, // 88 00 34816 3.730dB
201 3.630f, // 88 FF 35071 3.630dB
202 2.940f, // 90 00 36864 2.940dB
203 1.420f, // A0 00 40960 1.420dB
204 0.000f // B0 00 45056 -0.01dB
205 };
206
207 unsigned char sigbuf[4];
208 memset(sigbuf, '\0', sizeof(sigbuf));
209 sigbuf[0] = (((signal & 0xFF00) >> 8) & 0XFF);
210 sigbuf[1] = (signal & 0xFF);
211
212 /* calculate signal level */
213 if(sigbuf[0] <= 0x10U) {
214 /* clipped maximum */
215 return 24.07f;
216 }
217 else if (sigbuf[0] >= 0xB0U) {
218 /* clipped minimum */
219 return 0.0f;
220 }
221 else {
222 /* linear interpolation */
223 const float fMixRate =
224 (float)(((unsigned short)(sigbuf[0] & 0x0FU) << 8) |
225 (unsigned short)sigbuf[0]) / 4096.0f;
226 return afLevelTable[sigbuf[0] >> 4] * (1.0f - fMixRate) +
227 afLevelTable[(sigbuf[0] >> 4) + 0x01U] * fMixRate;
228 }
229 }
230
231 void
232 do_bell(int bell)
233 {
234 int i;
235 for(i=0; i < bell; i++) {
236 fprintf(stderr, "\a");
237 usleep(400000);
238 }
239 }
240
241 void
242 calc_cn(int fd, int type)
243 {
244 int rc;
245 double P;
246 double CNR;
247 int bell = 0;
248
249 if(ioctl(fd, GET_SIGNAL_STRENGTH, &rc) < 0) {
250 fprintf(stderr, "Tuner Select Error\n");
251 return ;
252 }
253
254 if(type == CHTYPE_GROUND) {
255 P = log10(5505024/(double)rc) * 10;
256 CNR = (0.000024 * P * P * P * P) - (0.0016 * P * P * P) +
257 (0.0398 * P * P) + (0.5491 * P)+3.0965;
258 }
259 else {
260 CNR = getsignal_isdb_s(rc);
261 }
262
263 if(CNR >= 30.0)
264 bell = 3;
265 else if(CNR >= 15.0 && CNR < 30.0)
266 bell = 2;
267 else if(CNR < 15.0)
268 bell = 1;
269
270 fprintf(stderr, "\rC/N = %fdB", CNR);
271 if(use_bell)
272 do_bell(bell);
273 }
274
275 int
276 tune(char *channel, thread_data *tdata, char *device)
277 {
278 char **tuner;
279 int num_devs;
280 int lp;
281 FREQUENCY freq;
282
283 /* get channel */
284 tdata->table = searchrecoff(channel);
285 if(tdata->table == NULL) {
286 fprintf(stderr, "Invalid Channel: %s\n", channel);
287 return 1;
288 }
289
290 freq.frequencyno = tdata->table->set_freq;
291 freq.slot = tdata->table->add_freq;
292
293 /* open tuner */
294 /* case 1: specified tuner device */
295 if(device) {
296 tdata->tfd = open(device, O_RDONLY);
297 if(tdata->tfd < 0) {
298 fprintf(stderr, "Cannot open tuner device: %s\n", device);
299 return 1;
300 }
301
302 /* power on LNB */
303 if(tdata->table->type == CHTYPE_SATELLITE) {
304 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
305 fprintf(stderr, "Power on LNB failed: %s\n", device);
306 }
307 }
308
309 /* tune to specified channel */
310 while(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) {
311 if(f_exit) {
312 close_tuner(tdata);
313 return 1;
314 }
315 fprintf(stderr, "No signal. Still trying: %s\n", device);
316 }
317
318 fprintf(stderr, "device = %s\n", device);
319 tdata->ch = atoi(channel);
320 }
321 else {
322 /* case 2: loop around available devices */
323 if(tdata->table->type == CHTYPE_SATELLITE) {
324 tuner = bsdev;
325 num_devs = NUM_BSDEV;
326 }
327 else {
328 tuner = isdb_t_dev;
329 num_devs = NUM_ISDB_T_DEV;
330 }
331
332 for(lp = 0; lp < num_devs; lp++) {
333 int count = 0;
334
335 tdata->tfd = open(tuner[lp], O_RDONLY);
336 if(tdata->tfd >= 0) {
337 /* power on LNB */
338 if(tdata->table->type == CHTYPE_SATELLITE) {
339 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
340 fprintf(stderr, "Warning: Power on LNB failed: %s\n", tuner[lp]);
341 }
342 }
343
344 /* tune to specified channel */
345 while(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0 &&
346 count < MAX_RETRY) {
347 if(f_exit) {
348 close_tuner(tdata);
349 return 1;
350 }
351 fprintf(stderr, "No signal. Still trying: %s\n", tuner[lp]);
352 count++;
353 }
354
355 if(count >= MAX_RETRY) {
356 close_tuner(tdata);
357 count = 0;
358 continue;
359 }
360
361 fprintf(stderr, "device = %s\n", tuner[lp]);
362 break; /* found suitable tuner */
363 }
364 }
365
366 /* all tuners cannot be used */
367 if(tdata->tfd < 0) {
368 fprintf(stderr, "Cannot tune to the specified channel\n");
369 return 1;
370 }
371 else {
372 tdata->ch = atoi(channel);
373 }
374 }
375
376 return 0; /* success */
377 }
378
379 int
380 close_tuner(thread_data *tdata)
381 {
382 int rv = 0;
383
384 if(tdata->tfd == -1)
385 return rv;
386
387 if(tdata->table->type == CHTYPE_SATELLITE) {
388 if(ioctl(tdata->tfd, LNB_DISABLE, 0) < 0) {
389 rv = 1;
390 }
391 }
392 close(tdata->tfd);
393 tdata->tfd = -1;
394
395 return rv;
396 }
397
398 int
399 main(int argc, char **argv)
400 {
401 pthread_t signal_thread;
402 static thread_data tdata;
403 int result;
404 int option_index;
405 struct option long_options[] = {
406 { "bell", 0, NULL, 'b'},
407 { "help", 0, NULL, 'h'},
408 { "version", 0, NULL, 'v'},
409 { "list", 0, NULL, 'l'},
410 { "LNB", 1, NULL, 'n'},
411 { "lnb", 1, NULL, 'n'},
412 { "device", 1, NULL, 'd'},
413 {0, 0, NULL, 0} /* terminate */
414 };
415
416 char *device = NULL;
417 int val;
418 char *voltage[] = {"0V", "11V", "15V"};
419
420 while((result = getopt_long(argc, argv, "bhvln:d:",
421 long_options, &option_index)) != -1) {
422 switch(result) {
423 case 'b':
424 use_bell = TRUE;
425 break;
426 case 'h':
427 fprintf(stderr, "\n");
428 show_usage(argv[0]);
429 fprintf(stderr, "\n");
430 show_options();
431 fprintf(stderr, "\n");
432 show_channels();
433 fprintf(stderr, "\n");
434 exit(0);
435 break;
436 case 'v':
437 fprintf(stderr, "%s %s\n", argv[0], version);
438 fprintf(stderr, "signal check utility for PT1/2 digital tuner.\n");
439 exit(0);
440 break;
441 case 'l':
442 show_channels();
443 exit(0);
444 break;
445 /* following options require argument */
446 case 'n':
447 val = atoi(optarg);
448 switch(val) {
449 case 11:
450 tdata.lnb = 1;
451 break;
452 case 15:
453 tdata.lnb = 2;
454 break;
455 default:
456 tdata.lnb = 0;
457 break;
458 }
459 fprintf(stderr, "LNB = %s\n", voltage[tdata.lnb]);
460 break;
461 case 'd':
462 device = optarg;
463 break;
464 }
465 }
466
467 if(argc - optind < 1) {
468 fprintf(stderr, "channel must be specified!\n");
469 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
470 return 1;
471 }
472
473 /* spawn signal handler thread */
474 init_signal_handlers(&signal_thread, &tdata);
475
476 /* tune */
477 if(tune(argv[optind], &tdata, device) != 0)
478 return 1;
479
480 while(1) {
481 if(f_exit)
482 break;
483 /* show signal strength */
484 calc_cn(tdata.tfd, tdata.table->type);
485 sleep(1);
486 }
487
488 /* wait for signal thread */
489 pthread_kill(signal_thread, SIGUSR1);
490 pthread_join(signal_thread, NULL);
491
492 /* close tuner */
493 if(close_tuner(&tdata) != 0)
494 return 1;
495
496 return 0;
497 }