-
Notifications
You must be signed in to change notification settings - Fork 0
/
satip_config.c
679 lines (544 loc) · 18.2 KB
/
satip_config.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* satip: tuning and pid config
*
* Copyright (C) 2014 [email protected]
* DVB-C and DVB-T(2) support added by [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include "satip_config.h"
#include "log.h"
/* configuration items present */
#define PC_FREQ 0x00000001
#define PC_POL 0x00000002
#define PC_ROLLOFF 0x00000004
#define PC_MODSYS 0x00000008
#define PC_MODTYPE 0x00000010
#define PC_PILOTS 0x00000020
#define PC_SYMRATE 0x00000040
#define PC_FECINNER 0x00000080
#define PC_POSITION 0x00000100
#define PC_GUARDINT 0x00000200
#define PC_TRANSMODE 0x00000400
#define PC_BANDWIDTH 0x00000800
#define PC_INVERSION 0x00001000
#define PC_PLP 0x00002000
#define PC_T2ID 0x00004000
#define PC_SM 0x00008000
#define PC_COMPLETE_DVBS \
( PC_FREQ | PC_POL | PC_MODSYS | PC_SYMRATE | \
PC_FECINNER | PC_POSITION )
#define PC_COMPLETE_DVBS2 \
( PC_FREQ | PC_POL | PC_ROLLOFF | PC_MODSYS | \
PC_MODTYPE | PC_PILOTS | PC_SYMRATE | \
PC_FECINNER | PC_POSITION )
#define PC_COMPLETE_DVBT \
( PC_FREQ | PC_MODSYS | PC_MODTYPE | PC_FECINNER | \
PC_GUARDINT | PC_TRANSMODE | PC_BANDWIDTH )
#define PC_COMPLETE_DVBT2 \
( PC_FREQ | PC_MODSYS | PC_MODTYPE | PC_FECINNER | \
PC_GUARDINT | PC_TRANSMODE | PC_BANDWIDTH | PC_PLP )
#define PC_COMPLETE_DVBC \
( PC_FREQ | PC_MODSYS | PC_MODTYPE | PC_SYMRATE )
/* PID handling */
#define PID_VALID 0
#define PID_IGNORE 1
#define PID_ADD 2
#define PID_DELETE 3
extern int enabled_workarounds;
/* strings for query strings */
char const strmap_polarization[] = { 'h', 'v', 'l', 'r' };
char* const strmap_fecinner[] = { "12","23","34","56","78","89","35","45","910" };
char* const strmap_rolloff[] = { "0.35","0.25","0.20" };
char* const strmap_modtype[] = { "qpsk", "8psk", "16qam", "32qam", "64qam", "128qam", "256qam"};
char* const strmap_guartinterval[] = {"14", "19256", "18", "19128", "116", "132", "1128"};
char* const strmap_transmode[] = {"1k", "2k", "4k", "8k", "16k", "32k"};
char* const strmap_bandwidth[] = {"5", "6", "7", "8", "10", "1.712"};
t_satip_config* satip_new_config(int frontend, int type)
{
t_satip_config* cfg;
cfg=(t_satip_config*) malloc(sizeof(t_satip_config));
cfg->frontend = frontend;
cfg->fe_type = type;
satip_clear_config(cfg);
return cfg;
}
/*
* PIDs need extra handling to cover "addpids" and "delpids" use cases
*/
static void pidupdate_status(t_satip_config* cfg)
{
int i;
int mod_found=0;
for (i=0; i<SATIPCFG_MAX_PIDS; i++)
if ( cfg->mod_pid[i] == PID_ADD ||
cfg->mod_pid[i] == PID_DELETE )
{
mod_found=1;
break;
}
switch (cfg->status)
{
case SATIPCFG_SETTLED:
if (mod_found)
cfg->status = SATIPCFG_PID_CHANGED;
break;
case SATIPCFG_PID_CHANGED:
if (!mod_found)
cfg->status = SATIPCFG_SETTLED;
break;
case SATIPCFG_CHANGED:
case SATIPCFG_INCOMPLETE:
break;
}
}
void satip_del_allpid(t_satip_config* cfg)
{
int i;
for ( i=0; i<SATIPCFG_MAX_PIDS; i++ )
satip_del_pid(cfg, cfg->pid[i]);
}
int satip_del_pid(t_satip_config* cfg,unsigned short pid)
{
int i;
for (i=0; i<SATIPCFG_MAX_PIDS; i++)
{
if ( cfg->pid[i] == pid )
switch (cfg->mod_pid[i])
{
case PID_VALID: /* mark it for deletion */
cfg->mod_pid[i] = PID_DELETE;
pidupdate_status(cfg);
return SATIPCFG_OK;
case PID_ADD: /* pid shall be added, ignore it */
cfg->mod_pid[i] = PID_IGNORE;
pidupdate_status(cfg);
return SATIPCFG_OK;
case PID_IGNORE:
break;
case PID_DELETE: /* pid shall already be deleted*/
return SATIPCFG_OK;
}
}
/* pid was not found, ignore request */
return SATIPCFG_OK;
}
int satip_add_pid(t_satip_config* cfg,unsigned short pid)
{
int i;
/* check if pid is present and valid, to be added, to be deleted */
for (i=0; i<SATIPCFG_MAX_PIDS; i++)
{
if ( cfg->pid[i] == pid )
switch (cfg->mod_pid[i])
{
case PID_VALID: /* already present */
case PID_ADD: /* pid shall be already added */
/* just return current status, no update required */
return SATIPCFG_OK;
case PID_IGNORE:
break;
case PID_DELETE:
/* pid shall be deleted, make it valid again */
cfg->mod_pid[i] = PID_VALID;
pidupdate_status(cfg);
return SATIPCFG_OK;
}
}
/* pid was not found, add it */
for ( i=0; i<SATIPCFG_MAX_PIDS; i++)
{
if (cfg->mod_pid[i] == PID_IGNORE )
{
cfg->mod_pid[i] = PID_ADD;
cfg->pid[i] = pid;
pidupdate_status(cfg);
return SATIPCFG_OK;
}
}
/* could not add it */
return SATIPCFG_ERROR;
}
static void param_update_status(t_satip_config* cfg)
{
if ( cfg->fe_type == SATIP_FE_TYPE_SAT )
{
if (
((cfg->mod_sys == SATIPCFG_MS_DVB_S) && ((cfg->param_cfg & PC_COMPLETE_DVBS ) == PC_COMPLETE_DVBS )) ||
((cfg->mod_sys == SATIPCFG_MS_DVB_S2) && ((cfg->param_cfg & PC_COMPLETE_DVBS2) == PC_COMPLETE_DVBS2 ))
)
cfg->status = SATIPCFG_CHANGED;
else
cfg->status = SATIPCFG_INCOMPLETE;
}
else if ( cfg->fe_type == SATIP_FE_TYPE_TER )
{
if (
((cfg->mod_sys == SATIPCFG_MS_DVB_T) && ((cfg->param_cfg & PC_COMPLETE_DVBT ) == PC_COMPLETE_DVBT )) ||
((cfg->mod_sys == SATIPCFG_MS_DVB_T2) && ((cfg->param_cfg & PC_COMPLETE_DVBT2) == PC_COMPLETE_DVBT2 ))
)
cfg->status = SATIPCFG_CHANGED;
else
cfg->status = SATIPCFG_INCOMPLETE;
}
else if ( cfg->fe_type == SATIP_FE_TYPE_CAB )
{
if (
(cfg->mod_sys == SATIPCFG_MS_DVB_C) && ((cfg->param_cfg & PC_COMPLETE_DVBC) == PC_COMPLETE_DVBC )
)
cfg->status = SATIPCFG_CHANGED;
else
cfg->status = SATIPCFG_INCOMPLETE;
}
else
{
cfg->status = SATIPCFG_INCOMPLETE;
}
DEBUG(MSG_MAIN,"Tuning parameter status: %08lx -> config status: %d\n",cfg->param_cfg, cfg->status);
}
int satip_set_freq(t_satip_config* cfg,unsigned int freq)
{
if ( (cfg->param_cfg & PC_FREQ) && cfg->frequency == freq )
return SATIPCFG_OK;
cfg->param_cfg |= PC_FREQ;
cfg->frequency = freq;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_polarization(t_satip_config* cfg,t_polarization pol)
{
if ( (cfg->param_cfg & PC_POL) && cfg->polarization == pol )
return SATIPCFG_OK;
cfg->param_cfg |= PC_POL;
cfg->polarization = pol;
/* polarization shall only trigger tuning if it was powered down explicitly */
if ( cfg->lnb_off==1 )
{
cfg->lnb_off = 0;
param_update_status(cfg);
}
return SATIPCFG_OK;
}
int satip_lnb_off(t_satip_config* cfg)
{
cfg->lnb_off = 1;
cfg->param_cfg &= ~PC_POL;
cfg->status = SATIPCFG_INCOMPLETE;
return SATIPCFG_OK;
}
int satip_set_rolloff(t_satip_config* cfg,t_roll_off rolloff)
{
if ( (cfg->param_cfg & PC_ROLLOFF) && cfg->roll_off == rolloff )
return SATIPCFG_OK;
cfg->param_cfg |= PC_ROLLOFF;
cfg->roll_off = rolloff;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_inversion(t_satip_config* cfg,t_inversion inversion)
{
if ( (cfg->param_cfg & PC_INVERSION) && cfg->inversion == inversion )
return SATIPCFG_OK;
cfg->param_cfg |= PC_INVERSION;
cfg->inversion = inversion;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_modsys(t_satip_config* cfg,t_mod_sys modsys)
{
if ( (cfg->param_cfg & PC_MODSYS) && cfg->mod_sys == modsys )
return SATIPCFG_OK;
cfg->param_cfg |= PC_MODSYS;
cfg->mod_sys = modsys;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_modtype(t_satip_config* cfg,t_mod_type modtype)
{
if ( (cfg->param_cfg & PC_MODTYPE) && cfg->mod_type == modtype )
return SATIPCFG_OK;
cfg->param_cfg |= PC_MODTYPE;
cfg->mod_type = modtype;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_guardinterval(t_satip_config* cfg,t_guard_interval guard_interval)
{
if ( (cfg->param_cfg & PC_GUARDINT) && cfg->guard_interval == guard_interval )
return SATIPCFG_OK;
cfg->param_cfg |= PC_GUARDINT;
cfg->guard_interval = guard_interval;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_transmode(t_satip_config* cfg,t_trans_mode trans_mode)
{
if ( (cfg->param_cfg & PC_TRANSMODE) && cfg->trans_mode == trans_mode )
return SATIPCFG_OK;
cfg->param_cfg |= PC_TRANSMODE;
cfg->trans_mode = trans_mode;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_bandwidth(t_satip_config* cfg,t_bandwidth bandwidth)
{
if ( (cfg->param_cfg & PC_BANDWIDTH) && cfg->bandwidth == bandwidth )
return SATIPCFG_OK;
cfg->param_cfg |= PC_BANDWIDTH;
cfg->bandwidth = bandwidth;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_plp(t_satip_config* cfg,unsigned int plp)
{
if ( (cfg->param_cfg & PC_PLP) && cfg->plp == plp )
return SATIPCFG_OK;
cfg->param_cfg |= PC_PLP;
cfg->plp = plp;
param_update_status(cfg);
return SATIPCFG_OK;
}
/* unused */
int satip_set_t2id(t_satip_config* cfg,unsigned short t2id)
{
if ( (cfg->param_cfg & PC_T2ID) && cfg->t2id == t2id )
return SATIPCFG_OK;
cfg->param_cfg |= PC_T2ID;
cfg->t2id = t2id;
param_update_status(cfg);
return SATIPCFG_OK;
}
/* unused */
int satip_set_sisomosi_mode(t_satip_config* cfg,t_siso_miso sm)
{
if ( (cfg->param_cfg & PC_SM) && cfg->sisomiso_mode == sm )
return SATIPCFG_OK;
cfg->param_cfg |= PC_SM;
cfg->sisomiso_mode = sm;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_pilots(t_satip_config* cfg,t_pilots pilots)
{
if ( (cfg->param_cfg & PC_PILOTS) && cfg->pilots == pilots )
return SATIPCFG_OK;
cfg->param_cfg |= PC_PILOTS;
cfg->pilots = pilots;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_symbol_rate(t_satip_config* cfg,unsigned int symrate)
{
if ( (cfg->param_cfg & PC_SYMRATE) && cfg->symbol_rate == symrate )
return SATIPCFG_OK;
cfg->param_cfg |= PC_SYMRATE;
cfg->symbol_rate = symrate;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_fecinner(t_satip_config* cfg, t_fec_inner fecinner)
{
if ( (cfg->param_cfg & PC_FECINNER) && cfg->fec_inner == fecinner )
return SATIPCFG_OK;
cfg->param_cfg |= PC_FECINNER;
cfg->fec_inner = fecinner;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_set_position(t_satip_config* cfg, int position)
{
if ( (cfg->param_cfg & PC_POSITION) && cfg->position == position )
return SATIPCFG_OK;
cfg->param_cfg |= PC_POSITION;
cfg->position = position;
param_update_status(cfg);
return SATIPCFG_OK;
}
int satip_valid_config(t_satip_config* cfg)
{
return ( cfg->status != SATIPCFG_INCOMPLETE );
}
int satip_tuning_required(t_satip_config* cfg)
{
return ( cfg->status == SATIPCFG_CHANGED );
}
int satip_pid_update_required(t_satip_config* cfg)
{
return ( cfg->status == SATIPCFG_PID_CHANGED );
}
static int setpidlist(t_satip_config* cfg, char* str,int maxlen,const char* firststr,int modtype1, int modtype2)
{
int i;
int printed=0;
int first=1;
for ( i=0; i<SATIPCFG_MAX_PIDS; i++ )
if ( cfg->mod_pid[i] == modtype1 ||
cfg->mod_pid[i] == modtype2 )
{
printed += snprintf(str+printed, maxlen-printed, "%s%d",
first ? firststr : ",",
cfg->pid[i]);
first=0;
if ( printed>=maxlen )
return printed;
}
return printed;
}
int satip_prepare_tuning(t_satip_config* cfg, char* str, int maxlen)
{
int printed;
char frontend_str[7]="";
/* optional: specific frontend */
if ( cfg->frontend > 0 && cfg->frontend<100)
sprintf(frontend_str, "fe=%d&", cfg->frontend);
if ((cfg->mod_sys == SATIPCFG_MS_DVB_S) || (cfg->mod_sys == SATIPCFG_MS_DVB_S2))
{
/* DVB-S mandatory parameters */
printed = snprintf(str, maxlen,
"src=%d&%sfreq=%d.%d&pol=%c&msys=%s&sr=%d&fec=%s",
cfg->position,
frontend_str,
cfg->frequency/10, cfg->frequency%10,
strmap_polarization[cfg->polarization],
cfg->mod_sys == SATIPCFG_MS_DVB_S ? "dvbs" : "dvbs2",
cfg->symbol_rate,
strmap_fecinner[cfg->fec_inner]);
if ( printed>=maxlen )
return printed;
str += printed;
/* DVB-S2 additional required parameters */
if ( cfg->mod_sys == SATIPCFG_MS_DVB_S2 )
{
printed += snprintf(str, maxlen-printed, "&ro=%s&mtype=%s&plts=%s",
strmap_rolloff[cfg->roll_off],
strmap_modtype[cfg->mod_type],
cfg->pilots == SATIPCFG_P_OFF ? "off" : "on" );
}
}
else if ((cfg->mod_sys == SATIPCFG_MS_DVB_T) || (cfg->mod_sys == SATIPCFG_MS_DVB_T2))
{
/* DVB-T mandatory parameters */
printed = snprintf(str, maxlen,
"%sfreq=%d.%d&bw=%s&msys=%s&tmode=%s&mtype=%s&gi=%s&fec=%s",
frontend_str,
cfg->frequency/1000000, cfg->frequency%1000000,
strmap_bandwidth[cfg->bandwidth],
cfg->mod_sys == SATIPCFG_MS_DVB_T ? "dvbt" : "dvbt2",
strmap_transmode[cfg->trans_mode],
strmap_modtype[cfg->mod_type],
strmap_guartinterval[cfg->guard_interval],
strmap_fecinner[cfg->fec_inner]);
if ( printed>=maxlen )
return printed;
str += printed;
/* DVB-T2 additional required parameters */
if ( cfg->mod_sys == SATIPCFG_MS_DVB_T2 )
{
printed += snprintf(str, maxlen-printed, "&plp=%d",
cfg->plp);
/* do not know how to handle these parameters as there is no counterpart in the DVB API.
printed += snprintf(str, maxlen-printed, "&plp=%d&t2id=%d&sm=%d",
cfg->plp,
cfg->t2id,
cfg->sisomiso_mode);
*/
}
}
else if ((cfg->mod_sys == SATIPCFG_MS_DVB_C))
{
/* DVB-C mandatory parameters */
printed = snprintf(str, maxlen,
"%sfreq=%d.%d&msys=%s&mtype=%s&sr=%d",
frontend_str,
cfg->frequency/1000000, cfg->frequency%1000000,
cfg->mod_sys == SATIPCFG_MS_DVB_C ? "dvbc" : "dvbc2",
strmap_modtype[cfg->mod_type],
cfg->symbol_rate);
if ( printed>=maxlen )
return printed;
str += printed;
/* DVB-C additional optional parameters */
if ( cfg->inversion != SATIPCFG_I_NONE )
{
printed += snprintf(str, maxlen-printed, "&specinv=%s",
cfg->inversion == SATIPCFG_I_ON ? "1" : "0");
}
}
/* don´t forget to check on caller ! */
return printed;
}
int satip_prepare_pids(t_satip_config* cfg, char* str, int maxlen,int modpid)
{
int printed;
if (modpid)
{
printed = setpidlist(cfg,str,maxlen,"addpids=",PID_ADD, PID_ADD);
if ( printed>=maxlen )
return printed;
printed += setpidlist(cfg, str+printed,maxlen-printed,
printed>0 ? "&delpids=" : "delpids=",PID_DELETE, PID_DELETE);
}
else
{
// Add workaround for Fritzdvbc quirk: add dummy pid
if (enabled_workarounds & 0x01)
printed = setpidlist(cfg,str,maxlen,"pids=100,",PID_VALID, PID_ADD);
else
printed = setpidlist(cfg,str,maxlen,"pids=",PID_VALID, PID_ADD);
}
/* nothing was added, use "none" */
if ( printed == 0 )
{
printed = snprintf(str,maxlen,"pids=none");
}
/* don´t forget to check on caller */
return printed;
}
int satip_settle_config(t_satip_config* cfg)
{
int i;
int retval=SATIPCFG_OK;
switch (cfg->status)
{
case SATIPCFG_CHANGED:
case SATIPCFG_PID_CHANGED:
/* clear up addpids delpids */
for ( i=0; i<SATIPCFG_MAX_PIDS; i++)
if ( cfg->mod_pid[i] == PID_ADD )
cfg->mod_pid[i] = PID_VALID;
else if (cfg->mod_pid[i] == PID_DELETE )
cfg->mod_pid[i] = PID_IGNORE;
/* now settled */
cfg->status = SATIPCFG_SETTLED;
break;
case SATIPCFG_SETTLED:
break;
case SATIPCFG_INCOMPLETE: /* cannot settle this.. */
default:
retval=SATIPCFG_ERROR;
break;
}
return retval;
}
void satip_clear_config(t_satip_config* cfg)
{
int i;
cfg->status = SATIPCFG_INCOMPLETE;
cfg->param_cfg = 0;
cfg->lnb_off = 0;
for ( i=0; i<SATIPCFG_MAX_PIDS; i++)
cfg->mod_pid[i]=PID_IGNORE;
}