-
Notifications
You must be signed in to change notification settings - Fork 6
/
recpt1core.c
554 lines (509 loc) · 13.2 KB
/
recpt1core.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dlfcn.h>
#include "../typedef.h"
#include "../IBonDriver2.h"
#include "AribChannel.h"
#include "recpt1core.h"
#include "version.h"
#include "pt1_dev.h"
/* globals */
boolean f_exit = FALSE;
BON_CHANNEL_SET *isdb_t_conv_set = NULL;
int getBonNumber(DWORD node, DWORD slot)
{
for(int lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) {
if(isdb_t_conv_table[lp].set_freq == (int)node && isdb_t_conv_table[lp].add_freq == (int)slot){
return lp;
}
}
return ARIB_CH_ERROR;
}
/* lookup frequency conversion table*/
BON_CHANNEL_SET *
searchrecoff(char *channel)
{
BON_CHANNEL_SET *isdb_t_conv_tmp;
DWORD node;
DWORD slot;
DWORD dwBonChannel = channelAribToBon(channel);
if( dwBonChannel == ARIB_CH_ERROR )
return NULL;
isdb_t_conv_tmp = (BON_CHANNEL_SET *)malloc(sizeof(BON_CHANNEL_SET));
memset(isdb_t_conv_tmp->parm_freq, 0, 16);
isdb_t_conv_tmp->set_freq = (int)dwBonChannel;
switch(dwBonChannel>>16){
case BON_CHANNEL:
isdb_t_conv_tmp->bon_num = (int)dwBonChannel;
isdb_t_conv_tmp->type = CHTYPE_BonNUMBER;
sprintf(isdb_t_conv_tmp->parm_freq, "B%d", node);
break;
case ARIB_GROUND:
case ARIB_CATV:
isdb_t_conv_tmp->bon_num = (int)(dwBonChannel & 0xFFFFU);
isdb_t_conv_tmp->type = CHTYPE_GROUND;
sprintf(isdb_t_conv_tmp->parm_freq, "%s", channel);
break;
case ARIB_BS:
node = (dwBonChannel & 0x01f0U) >> 4;
slot = dwBonChannel & 0x0007U;
isdb_t_conv_tmp->type = CHTYPE_SATELLITE;
sprintf(isdb_t_conv_tmp->parm_freq, "BS%d_%d", node, slot);
isdb_t_conv_tmp->bon_num = getBonNumber(node/2, slot);
break;
case ARIB_BS_SID:
for(int lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) {
/* return entry number in the table when strings match and
* lengths are same. */
// if((memcmp(isdb_t_conv_table[lp].parm_freq, channel, strlen(channel)) == 0) &&
// (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))) {
if(strcmp(isdb_t_conv_table[lp].parm_freq, channel) == 0){
isdb_t_conv_tmp->bon_num = lp;
isdb_t_conv_tmp->type = CHTYPE_SATELLITE;
sprintf(isdb_t_conv_tmp->parm_freq, "BS%d_%d", isdb_t_conv_table[lp].set_freq*2+1, isdb_t_conv_table[lp].add_freq);
goto FIND_EXIT;
}
}
free(isdb_t_conv_tmp);
return NULL;
case ARIB_CS:
node = dwBonChannel & 0x00ffU;
isdb_t_conv_tmp->type = CHTYPE_SATELLITE;
sprintf(isdb_t_conv_tmp->parm_freq, "CS%d", node);
isdb_t_conv_tmp->bon_num = getBonNumber(node/2+11, 0);
break;
case ARIB_TSID:
node = (dwBonChannel & 0x01f0U) >> 4;
isdb_t_conv_tmp->type = CHTYPE_SATELLITE;
if( (dwBonChannel & 0xf008U) == 0x4000U ){
slot = dwBonChannel & 0x0007U;
if( node == 15 )
slot--;
sprintf(isdb_t_conv_tmp->parm_freq, "BS%d_%d", node, slot);
isdb_t_conv_tmp->bon_num = getBonNumber(node/2, slot);
}else{
sprintf(isdb_t_conv_tmp->parm_freq, "CS%d", node);
isdb_t_conv_tmp->bon_num = getBonNumber(node/2+11, 0);
}
break;
}
FIND_EXIT:
return isdb_t_conv_tmp;
}
int
open_tuner(thread_data *tdata, char *driver)
{
// モジュールロード
tdata->hModule = dlopen(driver, RTLD_LAZY);
if(!tdata->hModule) {
fprintf(stderr, "Cannot open tuner driver: %s\ndlopen error: %s\n", driver, dlerror());
return -1;
}
// インスタンス作成
tdata->pIBon = tdata->pIBon2 = NULL;
char *err;
IBonDriver *(*f)() = (IBonDriver *(*)())dlsym(tdata->hModule, "CreateBonDriver");
if ((err = dlerror()) == NULL)
{
tdata->pIBon = f();
if (tdata->pIBon)
tdata->pIBon2 = dynamic_cast<IBonDriver2 *>(tdata->pIBon);
}
else
{
fprintf(stderr, "dlsym error: %s\n", err);
dlclose(tdata->hModule);
return -2;
}
if (!tdata->pIBon || !tdata->pIBon2)
{
fprintf(stderr, "CreateBonDriver error: tdata->pIBon[%p] tdata->pIBon2[%p]\n", tdata->pIBon, tdata->pIBon2);
dlclose(tdata->hModule);
return -3;
}
// ここから実質のチューナオープン & TS取得処理
BOOL b = tdata->pIBon->OpenTuner();
if (!b)
{
tdata->pIBon->Release();
dlclose(tdata->hModule);
return -4;
}else
tdata->tfd = TRUE;
#if 0 // linuxのBonDriverはOpenTuner()内で自動変更・SetLnbPower()未実装
/* power on LNB */
if(tdata->lnb != -1 && tdata->table->type == CHTYPE_SATELLITE) {
if(!tdata->pIBon->SetLnbPower(tdata->lnb>0 ? TRUE : FALSE)) {
fprintf(stderr, "Power on LNB failed: %s\n", driver);
}
}
#endif
return 0;
}
int
close_tuner(thread_data *tdata)
{
int rv = 0;
if(tdata->table){
free(tdata->table);
tdata->table = NULL;
}
if(tdata->hModule == NULL)
return rv;
#if 0 // linuxのBonDriverはCloseTuner()内で自動変更・SetLnbPower()未実装
if(tdata->lnb > 0 && tdata->table->type == CHTYPE_SATELLITE) {
if(!tdata->pIBon->SetLnbPower(FALSE)) {
rv = 1;
}
}
#endif
// チューナクローズ
if(tdata->tfd){
tdata->pIBon->CloseTuner();
tdata->tfd = FALSE;
}
// インスタンス解放 & モジュールリリース
tdata->pIBon->Release();
dlclose(tdata->hModule);
tdata->hModule = NULL;
return rv;
}
void
calc_cn(thread_data *tdata, boolean use_bell)
{
double CNR = (double)tdata->pIBon->GetSignalLevel();
if(use_bell) {
int bell = 0;
if(CNR >= 30.0)
bell = 3;
else if(CNR >= 15.0 && CNR < 30.0)
bell = 2;
else if(CNR < 15.0)
bell = 1;
fprintf(stderr, "\rC/N = %fdB ", CNR);
do_bell(bell);
}
else {
fprintf(stderr, "\rC/N = %fdB", CNR);
}
}
void
show_channels(void)
{
FILE *f;
char *home;
char buf[255], filename[255];
fprintf(stderr, "Available Channels:\n");
home = getenv("HOME");
sprintf(filename, "%s/.recpt1-channels", home);
f = fopen(filename, "r");
if(f) {
while(fgets(buf, 255, f))
fprintf(stderr, "%s", buf);
fclose(f);
}
else
fprintf(stderr, "13-62: Terrestrial Channels\n");
fprintf(stderr, "101ch: NHK BS1\n");
fprintf(stderr, "103ch: NHK BS Premium\n");
fprintf(stderr, "141ch: BS Nittele\n");
fprintf(stderr, "151ch: BS Asahi\n");
fprintf(stderr, "161ch: BS-TBS\n");
fprintf(stderr, "171ch: BS Japan\n");
fprintf(stderr, "181ch: BS Fuji\n");
fprintf(stderr, "191ch: WOWOW Prime\n");
fprintf(stderr, "192ch: WOWOW Live\n");
fprintf(stderr, "193ch: WOWOW Cinema\n");
fprintf(stderr, "200ch: Star Channel1\n");
fprintf(stderr, "201ch: Star Channel2\n");
fprintf(stderr, "202ch: Star Channel3\n");
fprintf(stderr, "211ch: BS11 Digital\n");
fprintf(stderr, "222ch: TwellV\n");
fprintf(stderr, "231ch: Housou Daigaku 1\n");
fprintf(stderr, "232ch: Housou Daigaku 2\n");
fprintf(stderr, "233ch: Housou Daigaku 3\n");
fprintf(stderr, "234ch: Green Channel\n");
fprintf(stderr, "236ch: BS Animax\n");
fprintf(stderr, "238ch: FOX bs238\n");
fprintf(stderr, "241ch: BS SkyPer!\n");
fprintf(stderr, "242ch: J SPORTS 1\n");
fprintf(stderr, "243ch: J SPORTS 2\n");
fprintf(stderr, "244ch: J SPORTS 3\n");
fprintf(stderr, "245ch: J SPORTS 4\n");
fprintf(stderr, "251ch: BS Tsuri Vision\n");
fprintf(stderr, "252ch: IMAGICA BS\n");
fprintf(stderr, "255ch: Nihon Eiga Senmon Channel\n");
fprintf(stderr, "256ch: Disney Channel\n");
fprintf(stderr, "258ch: Dlife\n");
fprintf(stderr, "C13-C63: CATV Channels\n");
fprintf(stderr, "CS2-CS24: CS Channels\n");
fprintf(stderr, "B0-: BonDriver Channel\n");
fprintf(stderr, "BS1_0-BS25_1: BS Channels(Transport)\n");
fprintf(stderr, "0x4000-0x7FFF: BS/CS Channels(TSID)\n");
}
int
parse_time(const char * rectimestr, int *recsec)
{
/* indefinite */
if(!strcmp("-", rectimestr)) {
*recsec = -1;
return 0;
}
/* colon */
else if(strchr(rectimestr, ':')) {
int n1, n2, n3;
if(sscanf(rectimestr, "%d:%d:%d", &n1, &n2, &n3) == 3)
*recsec = n1 * 3600 + n2 * 60 + n3;
else if(sscanf(rectimestr, "%d:%d", &n1, &n2) == 2)
*recsec = n1 * 3600 + n2 * 60;
else
return 1; /* unsuccessful */
return 0;
}
/* HMS */
else {
char *tmpstr;
char *p1, *p2;
int flag;
if( *rectimestr == '-' ){
rectimestr++;
flag = 1;
}else
flag = 0;
tmpstr = strdup(rectimestr);
p1 = tmpstr;
while(*p1 && !isdigit(*p1))
p1++;
/* hour */
if((p2 = strchr(p1, 'H')) || (p2 = strchr(p1, 'h'))) {
*p2 = '\0';
*recsec += atoi(p1) * 3600;
p1 = p2 + 1;
while(*p1 && !isdigit(*p1))
p1++;
}
/* minute */
if((p2 = strchr(p1, 'M')) || (p2 = strchr(p1, 'm'))) {
*p2 = '\0';
*recsec += atoi(p1) * 60;
p1 = p2 + 1;
while(*p1 && !isdigit(*p1))
p1++;
}
/* second */
*recsec += atoi(p1);
if( flag )
*recsec *= -1;
free(tmpstr);
return 0;
} /* else */
return 1; /* unsuccessful */
}
void
do_bell(int bell)
{
int i;
for(i=0; i < bell; i++) {
fprintf(stderr, "\a");
usleep(400000);
}
}
int
tune(char *channel, thread_data *tdata, char *driver)
{
/* get channel */
BON_CHANNEL_SET *table_tmp = searchrecoff(channel);
if(table_tmp == NULL) {
fprintf(stderr, "Invalid Channel: %s\n", channel);
return 1;
}
DWORD dwSendBonNum;
boolean reqChannel;
if(table_tmp->bon_num != ARIB_CH_ERROR){
dwSendBonNum = table_tmp->bon_num;
reqChannel = FALSE;
}else{
dwSendBonNum = table_tmp->set_freq;
reqChannel = TRUE;
}
/* open tuner */
char *dri_tmp = driver;
int aera;
char **tuner;
int num_devs;
if(dri_tmp && *dri_tmp == 'P'){
// proxy
dri_tmp++;
aera = 1;
}else
aera = 0;
if(dri_tmp && *dri_tmp != '\0') {
/* case 1: specified tuner driver */
int num = 0;
int code;
if(*dri_tmp == 'S'){
if(table_tmp->type != CHTYPE_GROUND){
if(aera == 0){
tuner = bsdev;
num_devs = NUM_BSDEV;
}else{
tuner = bsdev_proxy;
num_devs = NUM_BSDEV_PROXY;
}
}else
goto OPEN_TUNER;
}else if(*dri_tmp == 'T'){
if(table_tmp->type != CHTYPE_SATELLITE){
if(aera == 0){
tuner = isdb_t_dev;
num_devs = NUM_ISDB_T_DEV;
}else{
tuner = isdb_t_dev_proxy;
num_devs = NUM_ISDB_T_DEV_PROXY;
}
}else
goto OPEN_TUNER;
}else
goto OPEN_TUNER;
if(!isdigit(*++dri_tmp))
goto OPEN_TUNER;
do{
num = num * 10 + *dri_tmp++ - '0';
}while(isdigit(*dri_tmp));
if(*dri_tmp == '\0' && num+1 <= num_devs)
driver = tuner[num];
OPEN_TUNER:;
if((code = open_tuner(tdata, driver))){
if(code == -4)
fprintf(stderr, "OpenTuner erro: %s\n", driver);
free(table_tmp);
return 1;
}
#if 0
DWORD m_dwChannel = tdata->pIBon2->GetCurChannel();
if(m_dwChannel != ARIB_CH_ERROR && m_dwChannel != dwSendBonNum){
fprintf(stderr, "Tuner has been used: %s\n", driver);
goto err;
}
#endif
/* tune to specified channel */
while(tdata->pIBon2->SetChannel(tdata->dwSpace, dwSendBonNum) == FALSE) {
if(tdata->tune_persistent) {
if(f_exit)
goto err;
fprintf(stderr, "No signal. Still trying: %s\n", driver);
}
else {
fprintf(stderr, "Cannot tune to the specified channel: %s\n", driver);
goto err;
}
}
fprintf(stderr, "driver = %s\n", driver);
}
else {
/* case 2: loop around available devices */
int lp;
switch(table_tmp->type){
case CHTYPE_BonNUMBER:
default:
fprintf(stderr, "No driver name\n");
goto err;
case CHTYPE_SATELLITE:
if(aera == 0){
tuner = bsdev;
num_devs = NUM_BSDEV;
}else{
tuner = bsdev_proxy;
num_devs = NUM_BSDEV_PROXY;
}
break;
case CHTYPE_GROUND:
if(aera == 0){
tuner = isdb_t_dev;
num_devs = NUM_ISDB_T_DEV;
}else{
tuner = isdb_t_dev_proxy;
num_devs = NUM_ISDB_T_DEV_PROXY;
}
break;
}
for(lp = 0; lp < num_devs; lp++) {
if(open_tuner(tdata, tuner[lp]) == 0) {
// 同CHチェック・BSのCh比較は、局再編があると正しくない可能性がある
DWORD m_dwChannel = tdata->pIBon2->GetCurChannel();
if(m_dwChannel == dwSendBonNum)
goto SUCCESS_EXIT;
else{
close_tuner(tdata);
continue;
}
}
}
for(lp = 0; lp < num_devs; lp++) {
int count = 0;
if(open_tuner(tdata, tuner[lp]) == 0) {
// 使用中チェック・BSのCh比較は、局再編があると正しくない可能性がある
DWORD m_dwChannel = tdata->pIBon2->GetCurChannel();
if(m_dwChannel != ARIB_CH_ERROR){
if(m_dwChannel != dwSendBonNum){
close_tuner(tdata);
continue;
}
}else{
/* tune to specified channel */
if(tdata->tune_persistent) {
while(tdata->pIBon2->SetChannel(tdata->dwSpace, dwSendBonNum) == FALSE && count < MAX_RETRY) {
if(f_exit)
goto err;
fprintf(stderr, "No signal. Still trying: %s\n", tuner[lp]);
count++;
}
if(count >= MAX_RETRY) {
close_tuner(tdata);
count = 0;
continue;
}
} /* tune_persistent */
else {
if(tdata->pIBon2->SetChannel(tdata->dwSpace, dwSendBonNum) == FALSE) {
close_tuner(tdata);
continue;
}
}
}
goto SUCCESS_EXIT; /* found suitable tuner */
}
}
/* all tuners cannot be used */
free(table_tmp);
fprintf(stderr, "Cannot tune to the specified channel\n");
return 1;
SUCCESS_EXIT:
if(tdata->tune_persistent)
fprintf(stderr, "driver = %s\n", tuner[lp]);
}
tdata->table = table_tmp;
if(reqChannel)
table_tmp->bon_num = tdata->pIBon2->GetCurChannel();
// TS受信開始待ち
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 50 * 1000 * 1000; // 50ms
{
int lop = 0;
do{
nanosleep(&ts, NULL);
}while(tdata->pIBon->GetReadyCount() < 2 && ++lop < 20);
}
if(!tdata->tune_persistent) {
/* show signal strength */
calc_cn(tdata, FALSE);
}
return 0; /* success */
err:
free(table_tmp);
close_tuner(tdata);
return 1;
}