comparison src/pt1_common.c @ 169:cb54f9777734

Add pt1_common.c
author Naoya OYAMA <naoya.oyama@gmail.com>
date Sun, 07 Oct 2012 00:37:34 +0900
parents
children 4f3640bf350d
comparison
equal deleted inserted replaced
168:061ef2cd98f0 169:cb54f9777734
1 /* pt1_common.c -- This file is part of recpt1.
2
3 Copyright 2012 Naoya OYAMA <naoya.oyama@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <ctype.h>
19 #include <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <pt1_ioctl.h>
29 #include "recpt1.h"
30 #include "pt1_dev.h"
31 #include "pt1_common.h"
32
33 // 変換テーブル(ISDB-T用)
34 // 実際にioctl()を行う値の部分はREADMEを参照の事。
35 // BS/CSの設定値およびスロット番号は
36 // http://www5e.biglobe.ne.jp/~kazu_f/digital-sat/index.htmlより取得。
37 //
38
39 ISDB_T_FREQ_CONV_TABLE isdb_t_conv_table[] = {
40 { 0, CHTYPE_SATELLITE, 0, "151"}, /* 151ch:BS朝日 */
41 { 0, CHTYPE_SATELLITE, 1, "161"}, /* 161ch:BS-TBS */
42 { 1, CHTYPE_SATELLITE, 0, "191"}, /* 191ch:WOWOW prime */
43 { 1, CHTYPE_SATELLITE, 1, "171"}, /* 171ch:BSジャパン */
44 { 2, CHTYPE_SATELLITE, 0, "192"}, /* 192ch:WOWOWライブ */
45 { 2, CHTYPE_SATELLITE, 1, "193"}, /* 193ch:WOWOWシネマ */
46 { 3, CHTYPE_SATELLITE, 0, "201"}, /* 201ch:スター・チャンネル2 */
47 { 3, CHTYPE_SATELLITE, 0, "202"}, /* 202ch:スター・チャンネル3 */
48 { 3, CHTYPE_SATELLITE, 1, "236"}, /* 236ch:BSアニマックス */
49 { 3, CHTYPE_SATELLITE, 2, "256"}, /* 256ch:ディズニー・チャンネル */
50 { 4, CHTYPE_SATELLITE, 0, "211"}, /* 211ch:BS11デジタル */
51 { 4, CHTYPE_SATELLITE, 1, "200"}, /* 200ch:スター・チャンネル1 */
52 { 4, CHTYPE_SATELLITE, 2, "222"}, /* 222ch:TwellV */
53 { 5, CHTYPE_SATELLITE, 0, "238"}, /* 238ch:FOX bs238 */
54 { 5, CHTYPE_SATELLITE, 1, "241"}, /* 241ch:BSスカパー! */
55 { 5, CHTYPE_SATELLITE, 2, "231"}, /* 231ch:放送大学テレビ1 */
56 { 5, CHTYPE_SATELLITE, 2, "232"}, /* 232ch:放送大学テレビ2 */
57 { 5, CHTYPE_SATELLITE, 2, "233"}, /* 233ch:放送大学テレビ3 */
58 { 5, CHTYPE_SATELLITE, 2, "531"}, /* 531ch:放送大学ラジオ */
59 { 6, CHTYPE_SATELLITE, 0, "141"}, /* 141ch:BS日テレ */
60 { 6, CHTYPE_SATELLITE, 1, "181"}, /* 181ch:BSフジ */
61 { 7, CHTYPE_SATELLITE, 0, "101"}, /* 101ch:NHK-BS1 */
62 { 7, CHTYPE_SATELLITE, 0, "102"}, /* 102ch:NHK-BS1臨時 */
63 { 7, CHTYPE_SATELLITE, 1, "103"}, /* 103ch:NHK-BSプレミアム*/
64 { 7, CHTYPE_SATELLITE, 1, "910"}, /* 910ch:ウェザーニュース(WNI)*/
65 { 8, CHTYPE_SATELLITE, 2, "291"}, /* 291ch:NHK総合テレビジョン(東京)*/
66 { 8, CHTYPE_SATELLITE, 2, "292"}, /* 292ch:NHK教育テレビジョン(東京)*/
67 { 8, CHTYPE_SATELLITE, 1, "294"}, /* 294ch:日本テレビ */
68 { 8, CHTYPE_SATELLITE, 1, "295"}, /* 295ch:テレビ朝日 */
69 { 8, CHTYPE_SATELLITE, 1, "296"}, /* 296ch:TBSテレビ */
70 { 8, CHTYPE_SATELLITE, 1, "297"}, /* 297ch:テレビ東京 */
71 { 8, CHTYPE_SATELLITE, 2, "298"}, /* 298ch:フジテレビ */
72 { 9, CHTYPE_SATELLITE, 0, "234"}, /* 234ch:グリーンチャンネル */
73 { 9, CHTYPE_SATELLITE, 1, "242"}, /* 242ch:J SPORTS 1 */
74 { 9, CHTYPE_SATELLITE, 2, "243"}, /* 243ch:J SPORTS 2 */
75 { 10, CHTYPE_SATELLITE, 0, "252"}, /* 252ch:IMAGICA BS */
76 { 10, CHTYPE_SATELLITE, 1, "244"}, /* 244ch:J SPORTS 3 */
77 { 10, CHTYPE_SATELLITE, 2, "245"}, /* 245ch:J SPORTS 4 */
78 { 11, CHTYPE_SATELLITE, 0, "251"}, /* 251ch:BS釣りビジョン */
79 { 11, CHTYPE_SATELLITE, 1, "255"}, /* 255ch:日本映画専門チャンネル */
80 { 11, CHTYPE_SATELLITE, 2, "258"}, /* 258ch:D-Life */
81 { 12, CHTYPE_SATELLITE, 0, "CS2"}, /* ND2:
82 * 237ch:スター・チャンネル プラス
83 * 239ch:日本映画専門チャンネルHD
84 * 306ch:フジテレビNEXT */
85 { 13, CHTYPE_SATELLITE, 0, "CS4"}, /* ND4:
86 * 100ch:e2プロモ
87 * 256ch:J sports ESPN
88 * 312ch:FOX
89 * 322ch:スペースシャワーTV
90 * 331ch:カートゥーンネットワーク
91 * 294ch:ホームドラマチャンネル
92 * 334ch:トゥーン・ディズニー */
93 { 14, CHTYPE_SATELLITE, 0, "CS6"}, /* ND6:
94 * 221ch:東映チャンネル
95 * 222ch:衛星劇場
96 * 223ch:チャンネルNECO
97 * 224ch:洋画★シネフィル・イマジカ
98 * 292ch:時代劇専門チャンネル
99 * 238ch:スター・チャンネル クラシック
100 * 310ch:スーパー!ドラマTV
101 * 311ch:AXN
102 * 250ch:スカイ・Asports+ */
103 { 15, CHTYPE_SATELLITE, 0, "CS8"}, /* ND8:
104 * 055ch:ショップ チャンネル
105 * 335ch:キッズステーションHD */
106 { 16, CHTYPE_SATELLITE, 0, "CS10"}, /* ND10:
107 * 228ch:ザ・シネマ
108 * 800ch:スカチャンHD800
109 * 801ch:スカチャン801
110 * 802ch:スカチャン802 */
111 { 17, CHTYPE_SATELLITE, 0, "CS12"}, /* ND12:
112 * 260ch:ザ・ゴルフ・チャンネル
113 * 303ch:テレ朝チャンネル
114 * 323ch:MTV 324ch:大人の音楽専門TV◆ミュージック・エア
115 * 352ch:朝日ニュースター
116 * 353ch:BBCワールドニュース
117 * 354ch:CNNj
118 * 110ch:ワンテンポータル */
119 { 18, CHTYPE_SATELLITE, 0, "CS14"}, /* ND14:
120 * 251ch:J sports 1
121 * 252ch:J sports 2
122 * 253ch:J sports Plus
123 * 254ch:GAORA */
124 { 19, CHTYPE_SATELLITE, 0, "CS16"}, /* ND16:
125 * 305ch:チャンネル銀河
126 * 333ch:アニメシアターX(AT-X)
127 * 342ch:ヒストリーチャンネル
128 * 290ch:TAKARAZUKA SKYSTAGE
129 * 803ch:スカチャン803
130 * 804ch:スカチャン804 */
131 { 20, CHTYPE_SATELLITE, 0, "CS18"}, /* ND18:
132 * 240ch:ムービープラスHD
133 * 262ch:ゴルフネットワーク
134 * 314ch:LaLa HDHV */
135 { 21, CHTYPE_SATELLITE, 0, "CS20"}, /* ND20:
136 * 258ch:フジテレビONE
137 * 302ch:フジテレビTWO
138 * 332ch:アニマックス
139 * 340ch:ディスカバリーチャンネル
140 * 341ch:アニマルプラネット */
141 { 22, CHTYPE_SATELLITE, 0, "CS22"}, /* ND22:
142 * 160ch:C-TBSウェルカムチャンネル
143 * 161ch:QVC
144 * 185ch:プライム365.TV
145 * 293ch:ファミリー劇場
146 * 301ch:TBSチャンネル
147 * 304ch:ディズニー・チャンネル
148 * 325ch:MUSIC ON! TV
149 * 351ch:TBSニュースバード
150 * 343ch:ナショナルジオグラフィックチャンネル */
151 { 23, CHTYPE_SATELLITE, 0, "CS24"}, /* ND24:
152 * 257ch:日テレG+ HD
153 * 291ch:fashiontv
154 * 300ch:日テレプラス
155 * 315ch:FOXプラス
156 * 321ch:MusicJapan TV
157 * 350ch:日テレNEWS24
158 * 362ch:旅チャンネル */
159 { 0, CHTYPE_GROUND, 0, "1"}, { 1, CHTYPE_GROUND, 0, "2"},
160 { 2, CHTYPE_GROUND, 0, "3"}, { 3, CHTYPE_GROUND, 0, "C13"},
161 { 4, CHTYPE_GROUND, 0, "C14"}, { 5, CHTYPE_GROUND, 0, "C15"},
162 { 6, CHTYPE_GROUND, 0, "C16"}, { 7, CHTYPE_GROUND, 0, "C17"},
163 { 8, CHTYPE_GROUND, 0, "C18"}, { 9, CHTYPE_GROUND, 0, "C19"},
164 { 10, CHTYPE_GROUND, 0, "C20"}, { 11, CHTYPE_GROUND, 0, "C21"},
165 { 12, CHTYPE_GROUND, 0, "C22"}, { 13, CHTYPE_GROUND, 0, "4"},
166 { 14, CHTYPE_GROUND, 0, "5"}, { 15, CHTYPE_GROUND, 0, "6"},
167 { 16, CHTYPE_GROUND, 0, "7"}, { 17, CHTYPE_GROUND, 0, "8"},
168 { 18, CHTYPE_GROUND, 0, "9"}, { 19, CHTYPE_GROUND, 0, "10"},
169 { 20, CHTYPE_GROUND, 0, "11"}, { 21, CHTYPE_GROUND, 0, "12"},
170 { 22, CHTYPE_GROUND, 0, "C23"}, { 23, CHTYPE_GROUND, 0, "C24"},
171 { 24, CHTYPE_GROUND, 0, "C25"}, { 25, CHTYPE_GROUND, 0, "C26"},
172 { 26, CHTYPE_GROUND, 0, "C27"}, { 27, CHTYPE_GROUND, 0, "C28"},
173 { 28, CHTYPE_GROUND, 0, "C29"}, { 29, CHTYPE_GROUND, 0, "C30"},
174 { 30, CHTYPE_GROUND, 0, "C31"}, { 31, CHTYPE_GROUND, 0, "C32"},
175 { 32, CHTYPE_GROUND, 0, "C33"}, { 33, CHTYPE_GROUND, 0, "C34"},
176 { 34, CHTYPE_GROUND, 0, "C35"}, { 35, CHTYPE_GROUND, 0, "C36"},
177 { 36, CHTYPE_GROUND, 0, "C37"}, { 37, CHTYPE_GROUND, 0, "C38"},
178 { 38, CHTYPE_GROUND, 0, "C39"}, { 39, CHTYPE_GROUND, 0, "C40"},
179 { 40, CHTYPE_GROUND, 0, "C41"}, { 41, CHTYPE_GROUND, 0, "C42"},
180 { 42, CHTYPE_GROUND, 0, "C43"}, { 43, CHTYPE_GROUND, 0, "C44"},
181 { 44, CHTYPE_GROUND, 0, "C45"}, { 45, CHTYPE_GROUND, 0, "C46"},
182 { 46, CHTYPE_GROUND, 0, "C47"}, { 47, CHTYPE_GROUND, 0, "C48"},
183 { 48, CHTYPE_GROUND, 0, "C49"}, { 49, CHTYPE_GROUND, 0, "C50"},
184 { 50, CHTYPE_GROUND, 0, "C51"}, { 51, CHTYPE_GROUND, 0, "C52"},
185 { 52, CHTYPE_GROUND, 0, "C53"}, { 53, CHTYPE_GROUND, 0, "C54"},
186 { 54, CHTYPE_GROUND, 0, "C55"}, { 55, CHTYPE_GROUND, 0, "C56"},
187 { 56, CHTYPE_GROUND, 0, "C57"}, { 57, CHTYPE_GROUND, 0, "C58"},
188 { 58, CHTYPE_GROUND, 0, "C59"}, { 59, CHTYPE_GROUND, 0, "C60"},
189 { 60, CHTYPE_GROUND, 0, "C61"}, { 61, CHTYPE_GROUND, 0, "C62"},
190 { 62, CHTYPE_GROUND, 0, "C63"}, { 63, CHTYPE_GROUND, 0, "13"},
191 { 64, CHTYPE_GROUND, 0, "14"}, { 65, CHTYPE_GROUND, 0, "15"},
192 { 66, CHTYPE_GROUND, 0, "16"}, { 67, CHTYPE_GROUND, 0, "17"},
193 { 68, CHTYPE_GROUND, 0, "18"}, { 69, CHTYPE_GROUND, 0, "19"},
194 { 70, CHTYPE_GROUND, 0, "20"}, { 71, CHTYPE_GROUND, 0, "21"},
195 { 72, CHTYPE_GROUND, 0, "22"}, { 73, CHTYPE_GROUND, 0, "23"},
196 { 74, CHTYPE_GROUND, 0, "24"}, { 75, CHTYPE_GROUND, 0, "25"},
197 { 76, CHTYPE_GROUND, 0, "26"}, { 77, CHTYPE_GROUND, 0, "27"},
198 { 78, CHTYPE_GROUND, 0, "28"}, { 79, CHTYPE_GROUND, 0, "29"},
199 { 80, CHTYPE_GROUND, 0, "30"}, { 81, CHTYPE_GROUND, 0, "31"},
200 { 82, CHTYPE_GROUND, 0, "32"}, { 83, CHTYPE_GROUND, 0, "33"},
201 { 84, CHTYPE_GROUND, 0, "34"}, { 85, CHTYPE_GROUND, 0, "35"},
202 { 86, CHTYPE_GROUND, 0, "36"}, { 87, CHTYPE_GROUND, 0, "37"},
203 { 88, CHTYPE_GROUND, 0, "38"}, { 89, CHTYPE_GROUND, 0, "39"},
204 { 90, CHTYPE_GROUND, 0, "40"}, { 91, CHTYPE_GROUND, 0, "41"},
205 { 92, CHTYPE_GROUND, 0, "42"}, { 93, CHTYPE_GROUND, 0, "43"},
206 { 94, CHTYPE_GROUND, 0, "44"}, { 95, CHTYPE_GROUND, 0, "45"},
207 { 96, CHTYPE_GROUND, 0, "46"}, { 97, CHTYPE_GROUND, 0, "47"},
208 { 98, CHTYPE_GROUND, 0, "48"}, { 99, CHTYPE_GROUND, 0, "49"},
209 { 100, CHTYPE_GROUND, 0, "50"}, { 101, CHTYPE_GROUND, 0, "51"},
210 { 102, CHTYPE_GROUND, 0, "52"}, { 103, CHTYPE_GROUND, 0, "53"},
211 { 104, CHTYPE_GROUND, 0, "54"}, { 105, CHTYPE_GROUND, 0, "55"},
212 { 106, CHTYPE_GROUND, 0, "56"}, { 107, CHTYPE_GROUND, 0, "57"},
213 { 108, CHTYPE_GROUND, 0, "58"}, { 109, CHTYPE_GROUND, 0, "59"},
214 { 110, CHTYPE_GROUND, 0, "60"}, { 111, CHTYPE_GROUND, 0, "61"},
215 { 112, CHTYPE_GROUND, 0, "62"},
216 { 0, 0, 0, NULL} /* terminate */
217 };
218
219 char *bsdev[NUM_BSDEV] = {
220 "/dev/pt1video1",
221 "/dev/pt1video0",
222 "/dev/pt1video5",
223 "/dev/pt1video4",
224 "/dev/pt1video9",
225 "/dev/pt1video8",
226 "/dev/pt1video13",
227 "/dev/pt1video12",
228 "/dev/pt3video1",
229 "/dev/pt3video0",
230 "/dev/pt3video5",
231 "/dev/pt3video4",
232 "/dev/pt3video9",
233 "/dev/pt3video8",
234 "/dev/pt3video13",
235 "/dev/pt3video12"
236 };
237 char *isdb_t_dev[NUM_ISDB_T_DEV] = {
238 "/dev/pt1video2",
239 "/dev/pt1video3",
240 "/dev/pt1video6",
241 "/dev/pt1video7",
242 "/dev/pt1video10",
243 "/dev/pt1video11",
244 "/dev/pt1video14",
245 "/dev/pt1video15",
246 "/dev/pt3video2",
247 "/dev/pt3video3",
248 "/dev/pt3video6",
249 "/dev/pt3video7",
250 "/dev/pt3video10",
251 "/dev/pt3video11",
252 "/dev/pt3video14",
253 "/dev/pt3video15"
254 };
255
256 //global
257 char bs_channel_buf[8];
258 ISDB_T_FREQ_CONV_TABLE isdb_t_conv_set = { 0, CHTYPE_SATELLITE, 0, bs_channel_buf };
259 extern boolean f_exit;
260
261 /* lookup frequency conversion table*/
262 ISDB_T_FREQ_CONV_TABLE *
263 searchrecoff(char *channel)
264 {
265 int lp;
266
267 if((channel[0] == 'B' || channel[0] == 'b') &&
268 (channel[1] == 'S' || channel[1] == 's')) {
269 int node = 0;
270 int slot = 0;
271 char *bs_ch;
272
273 bs_ch = channel + 2;
274 while(isdigit(*bs_ch)) {
275 node *= 10;
276 node += *bs_ch++ - '0';
277 }
278 if(*bs_ch == '_' && (node&0x01) && node < ISDB_T_NODE_LIMIT) {
279 if(isdigit(*++bs_ch)) {
280 slot = *bs_ch - '0';
281 if(*++bs_ch == '\0' && slot < ISDB_T_SLOT_LIMIT) {
282 isdb_t_conv_set.set_freq = node / 2;
283 isdb_t_conv_set.add_freq = slot;
284 sprintf(bs_channel_buf, "BS%d_%d", node, slot);
285 return &isdb_t_conv_set;
286 }
287 }
288 }
289 return NULL;
290 }
291
292 for(lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) {
293 /* return entry number in the table when strings match and
294 * lengths are same. */
295 if((memcmp(isdb_t_conv_table[lp].parm_freq, channel,
296 strlen(channel)) == 0) &&
297 (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))) {
298 return &isdb_t_conv_table[lp];
299 }
300 }
301 return NULL;
302 }
303
304 void
305 show_channels(void)
306 {
307 FILE *f;
308 char *home;
309 char buf[255], filename[255];
310
311 fprintf(stderr, "Available Channels:\n");
312
313 home = getenv("HOME");
314 sprintf(filename, "%s/.recpt1-channels", home);
315 f = fopen(filename, "r");
316 if(f) {
317 while(fgets(buf, 255, f))
318 fprintf(stderr, "%s", buf);
319 fclose(f);
320 }
321 else {
322 fprintf(stderr, "13-62: Terrestrial Channels\n");
323 fprintf(stderr, "101ch: NHK BS1\n");
324 fprintf(stderr, "102ch: NHK BS2\n");
325 fprintf(stderr, "103ch: NHK BShi\n");
326 fprintf(stderr, "141ch: BS Nittele\n");
327 fprintf(stderr, "151ch: BS Asahi\n");
328 fprintf(stderr, "161ch: BS-TBS\n");
329 fprintf(stderr, "171ch: BS Japan\n");
330 fprintf(stderr, "181ch: BS Fuji\n");
331 fprintf(stderr, "191ch: WOWOW\n");
332 fprintf(stderr, "192ch: WOWOW2\n");
333 fprintf(stderr, "193ch: WOWOW3\n");
334 fprintf(stderr, "200ch: Star Channel\n");
335 fprintf(stderr, "211ch: BS11 Digital\n");
336 fprintf(stderr, "222ch: TwellV\n");
337 fprintf(stderr, "C13-C63: CATV Channels\n");
338 }
339 fprintf(stderr, "CS2-CS24: CS Channels\n");
340 }
341
342 float
343 getsignal_isdb_s(int signal)
344 {
345 /* apply linear interpolation */
346 static const float afLevelTable[] = {
347 24.07f, // 00 00 0 24.07dB
348 24.07f, // 10 00 4096 24.07dB
349 18.61f, // 20 00 8192 18.61dB
350 15.21f, // 30 00 12288 15.21dB
351 12.50f, // 40 00 16384 12.50dB
352 10.19f, // 50 00 20480 10.19dB
353 8.140f, // 60 00 24576 8.140dB
354 6.270f, // 70 00 28672 6.270dB
355 4.550f, // 80 00 32768 4.550dB
356 3.730f, // 88 00 34816 3.730dB
357 3.630f, // 88 FF 35071 3.630dB
358 2.940f, // 90 00 36864 2.940dB
359 1.420f, // A0 00 40960 1.420dB
360 0.000f // B0 00 45056 -0.01dB
361 };
362
363 unsigned char sigbuf[4];
364 memset(sigbuf, '\0', sizeof(sigbuf));
365 sigbuf[0] = (((signal & 0xFF00) >> 8) & 0XFF);
366 sigbuf[1] = (signal & 0xFF);
367
368 /* calculate signal level */
369 if(sigbuf[0] <= 0x10U) {
370 /* clipped maximum */
371 return 24.07f;
372 }
373 else if (sigbuf[0] >= 0xB0U) {
374 /* clipped minimum */
375 return 0.0f;
376 }
377 else {
378 /* linear interpolation */
379 const float fMixRate =
380 (float)(((unsigned short)(sigbuf[0] & 0x0FU) << 8) |
381 (unsigned short)sigbuf[0]) / 4096.0f;
382 return afLevelTable[sigbuf[0] >> 4] * (1.0f - fMixRate) +
383 afLevelTable[(sigbuf[0] >> 4) + 0x01U] * fMixRate;
384 }
385 }
386
387 static void
388 do_bell(int bell)
389 {
390 int i;
391 for(i=0; i < bell; i++) {
392 fprintf(stderr, "\a");
393 usleep(400000);
394 }
395 }
396
397 void
398 calc_cn(int fd, int type, boolean use_bell)
399 {
400 int rc;
401 double P;
402 double CNR;
403 int bell = 0;
404
405 if(ioctl(fd, GET_SIGNAL_STRENGTH, &rc) < 0) {
406 fprintf(stderr, "Tuner Select Error\n");
407 return ;
408 }
409
410 if(type == CHTYPE_GROUND) {
411 P = log10(5505024/(double)rc) * 10;
412 CNR = (0.000024 * P * P * P * P) - (0.0016 * P * P * P) +
413 (0.0398 * P * P) + (0.5491 * P)+3.0965;
414 }
415 else {
416 CNR = getsignal_isdb_s(rc);
417 }
418
419 if(CNR >= 30.0)
420 bell = 3;
421 else if(CNR >= 15.0 && CNR < 30.0)
422 bell = 2;
423 else if(CNR < 15.0)
424 bell = 1;
425
426 fprintf(stderr, "\rC/N = %fdB", CNR);
427 if(use_bell)
428 do_bell(bell);
429 }
430
431 int
432 tune(char *channel, thread_data *tdata, char *device)
433 {
434 char **tuner;
435 int num_devs;
436 int lp;
437 FREQUENCY freq;
438
439 /* get channel */
440 tdata->table = searchrecoff(channel);
441 if(tdata->table == NULL) {
442 fprintf(stderr, "Invalid Channel: %s\n", channel);
443 return 1;
444 }
445
446 freq.frequencyno = tdata->table->set_freq;
447 freq.slot = tdata->table->add_freq;
448
449 /* open tuner */
450 /* case 1: specified tuner device */
451 if(device) {
452 tdata->tfd = open(device, O_RDONLY);
453 if(tdata->tfd < 0) {
454 fprintf(stderr, "Cannot open tuner device: %s\n", device);
455 return 1;
456 }
457
458 /* power on LNB */
459 if(tdata->table->type == CHTYPE_SATELLITE) {
460 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
461 fprintf(stderr, "Power on LNB failed: %s\n", device);
462 }
463 }
464
465 /* tune to specified channel */
466 while(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) {
467 if(f_exit) {
468 close_tuner(tdata);
469 return 1;
470 }
471 fprintf(stderr, "No signal. Still trying: %s\n", device);
472 }
473
474 fprintf(stderr, "device = %s\n", device);
475 strncpy(tdata->ch, channel, sizeof(tdata->ch));
476 }
477 else {
478 /* case 2: loop around available devices */
479 if(tdata->table->type == CHTYPE_SATELLITE) {
480 tuner = bsdev;
481 num_devs = NUM_BSDEV;
482 }
483 else {
484 tuner = isdb_t_dev;
485 num_devs = NUM_ISDB_T_DEV;
486 }
487
488 for(lp = 0; lp < num_devs; lp++) {
489 int count = 0;
490
491 tdata->tfd = open(tuner[lp], O_RDONLY);
492 if(tdata->tfd >= 0) {
493 /* power on LNB */
494 if(tdata->table->type == CHTYPE_SATELLITE) {
495 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) {
496 fprintf(stderr, "Warning: Power on LNB failed: %s\n", tuner[lp]);
497 }
498 }
499
500 /* tune to specified channel */
501 while(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0 &&
502 count < MAX_RETRY) {
503 if(f_exit) {
504 close_tuner(tdata);
505 return 1;
506 }
507 fprintf(stderr, "No signal. Still trying: %s\n", tuner[lp]);
508 count++;
509 }
510
511 if(count >= MAX_RETRY) {
512 close_tuner(tdata);
513 count = 0;
514 continue;
515 }
516
517 fprintf(stderr, "device = %s\n", tuner[lp]);
518 break; /* found suitable tuner */
519 }
520 }
521
522 /* all tuners cannot be used */
523 if(tdata->tfd < 0) {
524 fprintf(stderr, "Cannot tune to the specified channel\n");
525 return 1;
526 }
527 else {
528 strncpy(tdata->ch, channel, sizeof(tdata->ch));
529 }
530 }
531
532 return 0; /* success */
533 }
534
535 int
536 close_tuner(thread_data *tdata)
537 {
538 int rv = 0;
539
540 if(tdata->tfd == -1)
541 return rv;
542
543 if(tdata->table->type == CHTYPE_SATELLITE) {
544 if(ioctl(tdata->tfd, LNB_DISABLE, 0) < 0) {
545 rv = 1;
546 }
547 }
548 close(tdata->tfd);
549 tdata->tfd = -1;
550
551 return rv;
552 }