source: titan/titan/frontenddev.h @ 42191

Last change on this file since 42191 was 42191, checked in by gost, 6 years ago

[titan] test unicable2

File size: 69.7 KB
Line 
1#ifndef FRONTENDDEV_H
2#define FRONTENDDEV_H
3
4enum {
5                T_Bandwidth_8MHz, T_Bandwidth_7MHz, T_Bandwidth_6MHz, T_Bandwidth_Auto, T_Bandwidth_5MHz, T_Bandwidth_1_712MHz, T_Bandwidth_10MHz
6};
7
8enum {
9                T_FEC_1_2=0, T_FEC_2_3=1, T_FEC_3_4=2, T_FEC_5_6=3, T_FEC_7_8=4, T_FEC_Auto=5, T_FEC_6_7=6, T_FEC_8_9=7
10};
11
12enum {
13                System_DVB_T_T2 = -1, System_DVB_T, System_DVB_T2
14};
15
16enum {
17                T_TransmissionMode_2k, T_TransmissionMode_8k, T_TransmissionMode_Auto, T_TransmissionMode_4k, T_TransmissionMode_1k, T_TransmissionMode_16k, T_TransmissionMode_32k
18};
19
20enum {
21                T_GuardInterval_1_32, T_GuardInterval_1_16, T_GuardInterval_1_8, T_GuardInterval_1_4, T_GuardInterval_Auto, T_GuardInterval_1_128, T_GuardInterval_19_128, T_GuardInterval_19_256
22};
23
24enum {
25                T_Hierarchy_None, T_Hierarchy_1, T_Hierarchy_2, T_Hierarchy_4, T_Hierarchy_Auto
26};
27
28enum {
29                T_Modulation_QPSK, T_Modulation_QAM16, T_Modulation_QAM64, T_Modulation_Auto, T_Modulation_QAM256
30};
31
32enum {
33                T_Inversion_Off, T_Inversion_On, T_Inversion_Unknown
34};
35
36int calclof(struct dvbdev* node, struct transponder* tpnode, char* feaktnr, int flag)
37{
38        int loftype = 0;
39        int lofl, lofh, lofthreshold;
40        int satcrfrequ = 0;
41        char* tmpstr = NULL;
42
43        if(node == NULL || tpnode == NULL)
44        {
45                err("NULL detect");
46                return -1;
47        }
48
49        if(node->feinfo->type != FE_QPSK)
50                return 0;
51
52        unsigned int frequency = tpnode->frequency;
53        node->feunicable = 0;
54
55        if(feaktnr == NULL) feaktnr = node->feaktnr;
56
57        tmpstr = ostrcat(node->feshortname, "_lnb_loftype", 0, 0);
58        loftype = getconfigint(tmpstr, feaktnr);
59        free(tmpstr); tmpstr = NULL;
60        switch(loftype)
61        {
62                case 1: //c-band
63                        lofl = 5150 * 1000;
64                        lofh = 5150 * 1000;
65                        lofthreshold = 5150 * 1000;
66                        break;
67                case 2: //user
68                        tmpstr = ostrcat(node->feshortname, "_lnb_lofl", 0, 0);
69                        lofl = getconfigint(tmpstr, feaktnr) * 1000;
70                        free(tmpstr); tmpstr = NULL;
71                        tmpstr = ostrcat(node->feshortname, "_lnb_lofh", 0, 0);
72                        lofh = getconfigint(tmpstr, feaktnr) * 1000;
73                        free(tmpstr); tmpstr = NULL;
74                        tmpstr = ostrcat(node->feshortname, "_lnb_lofthreshold", 0, 0);
75                        lofthreshold = getconfigint(tmpstr, feaktnr) * 1000;
76                        free(tmpstr); tmpstr = NULL;
77                        break;
78                case 3: //unicable
79                case 4: //user unicable
80                        tmpstr = ostrcat(node->feshortname, "_lnb_lofl", 0, 0);
81                        lofl = getconfigint(tmpstr, feaktnr) * 1000;
82                        free(tmpstr); tmpstr = NULL;
83                        if(lofl == 0) lofl = 9750 * 1000;
84                        tmpstr = ostrcat(node->feshortname, "_lnb_lofh", 0, 0);
85                        lofh = getconfigint(tmpstr, feaktnr) * 1000;
86                        free(tmpstr); tmpstr = NULL;
87                        if(lofh == 0) lofh = 10600 * 1000;
88                        tmpstr = ostrcat(node->feshortname, "_lnb_lofthreshold", 0, 0);
89                        lofthreshold = getconfigint(tmpstr, feaktnr) * 1000;
90                        free(tmpstr); tmpstr = NULL;
91                        if(lofthreshold == 0) lofthreshold = 11700 * 1000;
92                        tmpstr = ostrcat(node->feshortname, "_lnb_satcrfrequ", 0, 0);
93                        satcrfrequ = getconfigint(tmpstr, feaktnr) * 1000;
94                        free(tmpstr); tmpstr = NULL;
95                        break;
96                default: //standard lnb
97                        lofl = 9750 * 1000;
98                        lofh = 10600 * 1000;
99                        lofthreshold = 11700 * 1000;
100        }
101
102        if(lofthreshold && lofh && frequency >= lofthreshold)
103        {
104                if(flag == 1) return 1;
105                node->feaktband = 1;
106        }
107        else
108        {
109                if(flag == 1) return 0;
110                node->feaktband = 0;
111        }
112
113        node->feloffrequency_uni = 0;
114       
115        if(satcrfrequ == 0)
116        {
117                if(node->feaktband)
118                        node->feloffrequency = frequency - lofh;
119                else
120                {
121                        if(frequency < lofl)
122                                node->feloffrequency = lofl - frequency;
123                        else
124                                node->feloffrequency = frequency - lofl;
125                }
126        }
127        else
128        {
129                tmpstr = ostrcat(node->feshortname, "_lnb_satcr", 0, 0);
130                int satcr = getconfigint(tmpstr, node->feaktnr) - 1;
131                free(tmpstr); tmpstr = NULL;
132                if(satcr < 8)
133                {
134                        int lof = (node->feaktband & 1) ? lofh : lofl;
135                        unsigned int tmp = (frequency - lof) + satcrfrequ;
136                        node->feloffrequency = (tmp / 4) - 350000;
137                        node->feunicable = 1;
138                }
139                else
140                {
141                        int lof = (node->feaktband & 1) ? lofh : lofl;
142                        unsigned int tmp = (frequency - lof) - 100000;
143                        node->feloffrequency = (1000 + 2 * tmp) / (2 *1000); //round to multiple of 1000
144                        node->feloffrequency_uni = frequency - lof -(satcrfrequ - (satcrfrequ - (tmp - (1000 * node->feloffrequency))));
145                        node->feunicable = 1;
146                }
147        }
148
149        debug(200, "tuning to freq %d (befor lof %d), band=%d, unicable=%d unicableoff=%d", node->feloffrequency, frequency, node->feaktband, node->feunicable, node->feloffrequency_uni);
150        return node->feaktband;
151}
152
153char* fegettypestr(struct dvbdev* dvbnode)
154{
155        char* text = NULL;
156
157        if(dvbnode == NULL)
158        {
159                err("NULL detect");
160                return NULL;
161        }
162
163        switch(dvbnode->feinfo->type)
164        {
165                case FE_QPSK: text = ostrcat(text, "DVB-S", 1, 0); break;
166                case FE_QAM: text = ostrcat(text, "DVB-C", 1, 0); break;
167                case FE_OFDM: text = ostrcat(text, "DVB-T", 1, 0); break;
168                default: text = ostrcat(text, "unknown", 1, 0);
169        }
170
171        return text;
172}
173
174struct dvbdev* fegetbyshortname(char* feshortname)
175{
176        struct dvbdev* dvbnode = dvbdev;
177       
178        while(dvbnode != NULL)
179        {
180                if(dvbnode->type == FRONTENDDEV && ostrcmp(dvbnode->feshortname, feshortname) == 0)
181                        return dvbnode;
182                dvbnode = dvbnode->next;
183        }
184        return NULL;
185}
186
187void fegetconfig(struct dvbdev *dvbnode, struct transponder *tpnode, char** aktnr, char* tmpnr)
188{
189        char* tmpstr = NULL;
190
191        if(dvbnode == NULL || tpnode == NULL)
192        {
193                err("NULL detect");
194                return;
195        }
196
197        tmpstr = ostrcat(dvbnode->feshortname, "_satnr", 0, 0);
198        *aktnr = getconfig(tmpstr, tmpnr);
199        free(tmpstr); tmpstr = NULL;
200}
201
202struct dvbdev* fegetdummy()
203{
204        struct dvbdev* dvbnode = dvbdev;
205
206        while(dvbnode != NULL)
207        {
208                if(dvbnode->type == FRONTENDDEVDUMMY)
209                        return dvbnode;
210                dvbnode = dvbnode->next;
211        }
212        return NULL;
213}
214
215void settunerstatus()
216{
217        struct dvbdev* dvbnode = dvbdev;
218        char *tmpstr = NULL;
219        char *buf = NULL;
220        int fbc = 0;
221        while(dvbnode != NULL)
222        {
223                //FRONTENDDEV first in the list
224                if(dvbnode->type != FRONTENDDEV) break;
225               
226                if(dvbnode != NULL && ostrstr(dvbnode->feinfo->name, "BCM45208") != NULL)
227                        fbc = 1;
228                else
229                        fbc = 0;
230
231                if((checkbox("DM900") == 1 || checkbox("DM920") == 1 || checkbox("DM520") == 1 || checkbox("DM525") == 1) && fbc != 1)
232                {
233                        if(ostrcmp("fe_01", dvbnode->feshortname) == 0)
234                        {
235                                if(ostrcmp("fe_00", getconfig(dvbnode->feshortname, NULL)) == 0)
236                                        system("echo internal > /proc/stb/frontend/1/rf_switch");
237                                else
238                                        system("echo external > /proc/stb/frontend/1/rf_switch");
239                        }
240                }
241                if(fbc == 1)
242                {
243                        buf = malloc(MINMALLOC);
244                        if(buf == NULL)
245                        {
246                                err("no memory");
247                                return;
248                        }
249                        sprintf(buf, "/proc/stb/frontend/%d/input",dvbnode->devnr);
250                        tmpstr = ostrcat(dvbnode->feshortname, "_fbc", 0, 0);
251                        if(ostrcmp("AM", getconfig(tmpstr, NULL)) == 0 || ostrcmp("AU", getconfig(tmpstr, NULL)) == 0 || ostrcmp("A", getconfig(tmpstr, NULL)) == 0)
252                                writesys(buf, "A", 1);
253                        else
254                                writesys(buf, "B", 1); 
255                        free(buf); buf = NULL;
256                        free(tmpstr); tmpstr = NULL;
257                }
258                //check if tuner is deactivate
259                if(ostrcmp("x", getconfig(dvbnode->feshortname, NULL)) == 0)
260                        dvbnode->deactive = 1;
261                else
262                        dvbnode->deactive = 0;
263
264                dvbnode = dvbnode->next;
265        }
266}
267
268//flag 0 = normal
269//flag 1 = check only
270//flag 2 = from record
271//flag 3 = from rectimer
272struct dvbdev* fegetfree(struct transponder* tpnode, int flag, struct dvbdev* dvbfirst)
273{
274        struct dvbdev* dvbnode = NULL;
275        struct dvbdev* tmpdvbnode = NULL;
276        char* tmpstr = NULL, *tmpnr = NULL, *aktnr = NULL;
277        int i, orbitalpos = 0, band = 0;
278        int found = 0;
279        //char *CharPtrTmp[20];
280        struct dvbdev* CharPtrTmp[20];
281       
282        if(dvbfirst != NULL)
283                dvbnode = dvbfirst;
284        else
285                dvbnode = dvbdev;
286
287        if(tpnode == NULL)
288        {
289                err("NULL detect");
290                return NULL;
291        }
292
293        //suche tuner der auf der gleichen orbitalpos/frequency/pol/band ist
294        while(dvbnode != NULL)
295        {
296                //FRONTENDDEV first in the list
297                if(dvbnode->type != FRONTENDDEV) break;
298
299                //check if tuner is deactivate
300                if(dvbnode->deactive == 1)
301                {
302                        dvbnode = dvbnode->next;
303                        continue;
304                }
305                if(dvbnode->type == FRONTENDDEV && dvbnode->feinfo->type == tpnode->fetype)
306                {
307                        if(dvbnode->feakttransponder != NULL && dvbnode->felock == 0 && status.aktservice->fedev != dvbnode)
308                        {
309                                if(flag != 1) debug(200, "clear tuner %s", dvbnode->feshortname);
310                                dvbnode->felasttransponder = dvbnode->feakttransponder;
311                                dvbnode->feakttransponder = NULL;
312                        }       
313                        if(dvbnode->feakttransponder != NULL && dvbnode->feakttransponder->orbitalpos == tpnode->orbitalpos && dvbnode->feakttransponder->frequency == tpnode->frequency && dvbnode->feaktpolarization == tpnode->polarization)
314                        {
315                                band = calclof(dvbnode, tpnode, dvbnode->feaktnr, 1);
316                                if(dvbnode->feaktband != band)
317                                {
318                                        dvbnode = dvbnode->next;
319                                        continue;
320                                }
321                                dvbnode->felasttransponder = dvbnode->feakttransponder;
322                                dvbnode->feakttransponder = tpnode;
323                                if(flag != 1) debug(200, "found tuner with same orbitalpos/frequency/pol/band %s", dvbnode->feshortname);
324                                return(dvbnode);
325                        }
326                }
327                dvbnode = dvbnode->next;
328        }
329        if(dvbfirst != NULL)
330                dvbnode = dvbfirst;
331        else
332                dvbnode = dvbdev;
333
334        //suche tuner der die gewuenschte orbitalpos kann und belegt ist und dessen looptuner es kann
335        while(dvbnode != NULL)
336        {
337                //FRONTENDDEV first in the list
338                if(dvbnode->type != FRONTENDDEV) break;
339
340                //check if tuner is deactivate
341                if(dvbnode->deactive == 1)
342                {
343                        dvbnode = dvbnode->next;
344                        continue;
345                }
346                if(dvbnode->type == FRONTENDDEV && dvbnode->feinfo->type == tpnode->fetype && (dvbnode->felock != 0 || (flag == 2 && status.aktservice->fedev == dvbnode)))
347                {
348                        //check if tuner is main tuner
349                        if(getconfig(dvbnode->feshortname, NULL) != NULL)
350                        {
351                                dvbnode = dvbnode->next;
352                                continue;                               
353                        }
354                        if(dvbnode->feaktpolarization != tpnode->polarization || dvbnode->feakttransponder->orbitalpos != tpnode->orbitalpos)
355                        {
356                                dvbnode = dvbnode->next;
357                                continue;                               
358                        }
359                        found = 0;
360                        //check looptuner is free
361                        tmpstr = getconfigbyval(dvbnode->feshortname, NULL);
362                        CharPtrTmp[0] = NULL;
363                        while(tmpstr != NULL) //found loop tuner
364                        {
365                                tmpdvbnode = fegetbyshortname(tmpstr);
366                                if(tmpdvbnode != NULL)
367                                {
368                                        if(tmpdvbnode->feakttransponder == NULL || (tmpdvbnode->felock == 0 && status.aktservice->fedev != tmpdvbnode))
369                                        {
370                                                found = 99;
371                                                break;
372                                        }
373                                        else
374                                        {
375                                                CharPtrTmp[found] = tmpdvbnode;
376                                                found = found + 1;
377                                        }
378                                }
379                                tmpstr = getconfigbyval(tmpstr, NULL); //loop tuner also loop  ?
380                        }
381                        CharPtrTmp[0] = NULL;
382                        if(found != 99)
383                        {
384                                dvbnode = dvbnode->next;
385                                continue;
386                        }
387                        tmpstr = ostrcat(dvbnode->feshortname, "_sat", 0, 0);
388                        for(i = 1; i <= getmaxsat(dvbnode->feshortname); i++)
389                        {
390                                tmpnr = oitoa(i);
391                                orbitalpos = getconfigint(tmpstr, tmpnr);
392                                if(orbitalpos == tpnode->orbitalpos)
393                                {
394                                        fegetconfig(dvbnode, tpnode, &aktnr, tmpnr);
395                                        if(flag == 3)
396                                                band = calclof(dvbnode, tpnode, aktnr, 0);
397                                        else
398                                                band = calclof(dvbnode, tpnode, aktnr, 1);
399                                        found = 0;
400                                        if(dvbnode->feaktband != band)
401                                        {
402                                                found = -1;
403                                                break;
404                                        }
405                                        if(flag == 1)
406                                        {
407                                                free(tmpstr); tmpstr = NULL;
408                                                free(tmpnr); tmpnr = NULL;
409                                                return tmpdvbnode;
410                                        }
411                                        if(tmpdvbnode->feakttransponder != NULL)
412                                                tmpdvbnode->felasttransponder = tmpdvbnode->feakttransponder;
413                                        tmpdvbnode->feakttransponder = tpnode;
414                                        tmpdvbnode->feaktpolarization = tpnode->polarization;
415                                        tmpdvbnode->feaktband = band;
416                                        free(tmpdvbnode->feaktnr);
417                                        if(aktnr != NULL)
418                                                tmpdvbnode->feaktnr = ostrcat(aktnr, NULL, 0, 0);
419                                        else
420                                                tmpdvbnode->feaktnr = NULL;
421
422                                        free(tmpstr); tmpstr = NULL;
423                                        free(tmpnr); tmpnr = NULL;
424                                        if(flag != 1) debug(200, "found free tuner witch same orbitalpos %s", dvbnode->feshortname);
425                                        return tmpdvbnode;
426                                }
427                                free(tmpnr); tmpnr = NULL;
428                        }
429                        free(tmpstr); tmpstr = NULL;
430                }
431                dvbnode = dvbnode->next;
432        }
433        if(dvbfirst != NULL)
434                dvbnode = dvbfirst;
435        else
436                dvbnode = dvbdev;
437
438        //suche tuner der die gewuenschte orbitalpos kann und nicht belegt ist
439        while(dvbnode != NULL)
440        {
441                //FRONTENDDEV first in the list
442                if(dvbnode->type != FRONTENDDEV) break;
443
444                //check if tuner is deactivate
445                if(dvbnode->deactive == 1)
446                {
447                        dvbnode = dvbnode->next;
448                        continue;
449                }
450                if(dvbnode->type == FRONTENDDEV && dvbnode->feinfo->type == tpnode->fetype && dvbnode->felock == 0)
451                {
452                        //check if tuner is main tuner
453                        if(getconfig(dvbnode->feshortname, NULL) != NULL)
454                        {
455                                dvbnode = dvbnode->next;
456                                continue;                               
457                        }
458                        if(flag == 2 && status.aktservice->fedev == dvbnode)
459                        {
460                                dvbnode = dvbnode->next;
461                                continue;
462                        }
463                        found = 0;
464                        //check if tuner is loop and looptuner is locked
465                        tmpstr = getconfigbyval(dvbnode->feshortname, NULL);
466                        CharPtrTmp[0] = NULL;
467                        while(tmpstr != NULL) //found loop tuner
468                        {
469                                tmpdvbnode = fegetbyshortname(tmpstr);
470                                if(tmpdvbnode != NULL)
471                                {
472                                        if(tmpdvbnode->feakttransponder != NULL && (tmpdvbnode->feaktpolarization != tpnode->polarization || tmpdvbnode->feakttransponder->orbitalpos != tpnode->orbitalpos) && (tmpdvbnode->felock != 0 || (flag == 2 && tmpdvbnode->felock == 0)))
473                                        {
474                                                found = -1;
475                                                break;
476                                        }
477                                        else
478                                        {
479                                                CharPtrTmp[found] = tmpdvbnode;
480                                                found = found + 1;
481                                        }
482                                }
483                                tmpstr = getconfigbyval(tmpstr, NULL); //loop tuner also loop  ?
484                        }
485                        if(found == -1)
486                        {
487                                dvbnode = dvbnode->next;
488                                continue;
489                        }
490                        CharPtrTmp[found] = NULL;
491                        tmpstr = ostrcat(dvbnode->feshortname, "_sat", 0, 0);
492                        for(i = 1; i <= getmaxsat(dvbnode->feshortname); i++)
493                        {
494                                tmpnr = oitoa(i);
495
496                                orbitalpos = getconfigint(tmpstr, tmpnr);
497                                if(orbitalpos == tpnode->orbitalpos)
498                                {
499                                        fegetconfig(dvbnode, tpnode, &aktnr, tmpnr);
500                                        if(flag == 3)
501                                                band = calclof(dvbnode, tpnode, aktnr, 0);
502                                        else
503                                                band = calclof(dvbnode, tpnode, aktnr, 1);
504                                        found = 0;
505                                        while(CharPtrTmp[found] != NULL)
506                                        {
507                                                if(CharPtrTmp[found]->feakttransponder != NULL)
508                                                {
509                                                        if(CharPtrTmp[found]->feaktband != band && (CharPtrTmp[found]->felock != 0 || (flag == 2 && CharPtrTmp[found]->felock == 0)))
510                                                        {
511                                                                found = 99;
512                                                                break;
513                                                        }
514                                                }
515                                                found = found + 1;
516                                        }
517                                        if(found == 99)
518                                        {
519                                                found = 0;
520                                                free(tmpnr); tmpnr = NULL;
521                                                continue;
522                                        }       
523                                        if(flag == 1)
524                                        {
525                                                free(tmpstr); tmpstr = NULL;
526                                                free(tmpnr); tmpnr = NULL;
527                                                return dvbnode;
528                                        }
529                                        found = 0;
530                                        while(CharPtrTmp[found] != NULL)
531                                        {
532                                                CharPtrTmp[found]->feaktband = band;
533                                                CharPtrTmp[found]->feaktpolarization = tpnode->polarization;
534                                                found = found + 1;
535                                        }
536                                        dvbnode->felasttransponder = dvbnode->feakttransponder;
537                                        dvbnode->feakttransponder = tpnode;
538                                        dvbnode->feaktpolarization = tpnode->polarization;
539                                        free(dvbnode->feaktnr);
540                                        if(aktnr != NULL)
541                                                dvbnode->feaktnr = ostrcat(aktnr, NULL, 0, 0);
542                                        else
543                                                dvbnode->feaktnr = NULL;
544                                        free(tmpstr); tmpstr = NULL;
545                                        free(tmpnr); tmpnr = NULL;
546                                        if(flag != 1) debug(200, "found free tuner witch same orbitalpos %s", dvbnode->feshortname);
547                                        return dvbnode;
548                                }
549                                free(tmpnr); tmpnr = NULL;
550                        }
551                        free(tmpstr); tmpstr = NULL;
552                }
553                dvbnode = dvbnode->next;
554        }
555        if(dvbfirst != NULL)
556                dvbnode = dvbfirst;
557        else
558                dvbnode = dvbdev;
559
560        //suche loop tuner, wo der haupttuner
561        //die gewuenschte orbitalpos kann, nicht belegt ist
562        //und auf der gleichen poarization/band ist, wo wir hintunen wollen
563        while(dvbnode != NULL)
564        {
565                //FRONTENDDEV first in the list
566                if(dvbnode->type != FRONTENDDEV) break;
567
568                //check if tuner is deactivate
569                if(dvbnode->deactive == 1)
570                {
571                        dvbnode = dvbnode->next;
572                        continue;
573                }
574                if(dvbnode->type == FRONTENDDEV && dvbnode->feinfo->type == tpnode->fetype && dvbnode->felock == 0)
575                {
576                        if(flag == 2 && status.aktservice->fedev == dvbnode)
577                        {
578                                dvbnode = dvbnode->next;
579                                continue;
580                        }
581                        if(getconfig(dvbnode->feshortname, NULL) == NULL)
582                        {
583                                dvbnode = dvbnode->next;
584                                continue;                               
585                        }
586                        //check if tuner is loop and an other loopt is locked
587                        found = 0;
588                        tmpstr = getconfigbyval(dvbnode->feshortname, NULL);
589                        while(tmpstr != NULL) //found loop tuner
590                        {
591                                tmpdvbnode = fegetbyshortname(tmpstr);
592                                if(tmpdvbnode != NULL)
593                                {
594                                        if(tmpdvbnode->feakttransponder != NULL && (tmpdvbnode->feaktpolarization != tpnode->polarization || tmpdvbnode->feakttransponder->orbitalpos != tpnode->orbitalpos) && (tmpdvbnode->felock != 0 || (flag == 2 && tmpdvbnode->felock == 0)))
595                                        {
596                                                found = -1;
597                                                break;
598                                        }
599                                        else
600                                        {
601                                                CharPtrTmp[found] = tmpdvbnode;
602                                                found = found + 1;
603                                        }
604                                }
605                                tmpstr = getconfigbyval(tmpstr, NULL); //loop tuner also loop  ?
606                        }
607                        if(found == -1)
608                        {
609                                dvbnode = dvbnode->next;
610                                continue;
611                        }       
612                        //check if tuner is loop an looptuner is locked
613                        tmpstr = getconfig(dvbnode->feshortname, NULL);
614                        while(tmpstr != NULL) //found loop tuner
615                        {
616                                tmpdvbnode = fegetbyshortname(tmpstr);
617                                if(tmpdvbnode != NULL)
618                                {
619                                        if(tmpdvbnode->feakttransponder != NULL && (tmpdvbnode->feaktpolarization != tpnode->polarization || tmpdvbnode->feakttransponder->orbitalpos != tpnode->orbitalpos) && (tmpdvbnode->felock != 0 || (flag == 2 && tmpdvbnode->felock == 0)))
620                                        {
621                                                found = -1;
622                                                break;
623                                        }
624                                        else
625                                        {
626                                                CharPtrTmp[found] = tmpdvbnode;
627                                                found = found + 1;
628                                        }
629                                }
630                                tmpstr = getconfig(tmpstr, NULL);
631                        }
632                        CharPtrTmp[found] = NULL;
633                        if(found == -1)
634                        {
635                                dvbnode = dvbnode->next;
636                                continue;
637                        }
638       
639                        tmpstr = ostrcat(tmpdvbnode->feshortname, "_sat", 0, 0);
640                        for(i = 1; i <= getmaxsat(tmpdvbnode->feshortname); i++)
641                        {
642                                tmpnr = oitoa(i);
643                                orbitalpos = getconfigint(tmpstr, tmpnr);
644                                if(orbitalpos == tpnode->orbitalpos)
645                                {
646                                        fegetconfig(tmpdvbnode, tpnode, &aktnr, tmpnr);
647                                        if(flag == 3)
648                                                band = calclof(dvbnode, tpnode, aktnr, 0);
649                                        else
650                                                band = calclof(dvbnode, tpnode, aktnr, 1);
651                                        found = 0;
652                                        while(CharPtrTmp[found] != NULL)
653                                        {
654                                                if(CharPtrTmp[found]->feakttransponder != NULL && CharPtrTmp[found]->feaktband != band && (CharPtrTmp[found]->felock != 0 || (flag >= 2 && CharPtrTmp[found]->felock == 0)))   
655                                                {
656                                                        found = 99;
657                                                        break;
658                                                }
659                                                found = found + 1;
660                                        }
661                                        if(found == 99)
662                                        {
663                                                found = 0;
664                                                free(tmpnr); tmpnr = NULL;
665                                                continue;
666                                        }
667                                               
668                                        if(flag == 1)
669                                        {
670                                                free(tmpstr); tmpstr = NULL;
671                                                free(tmpnr); tmpnr = NULL;
672                                                return dvbnode;
673                                        }
674                                        found = 0;
675                                        while(CharPtrTmp[found] != NULL)
676                                        {
677                                                CharPtrTmp[found]->feaktband = band;
678                                                CharPtrTmp[found]->feaktpolarization = tpnode->polarization;
679                                                found = found + 1;
680                                        }
681                                        dvbnode->felasttransponder = dvbnode->feakttransponder;
682                                        dvbnode->feakttransponder = tpnode;
683                                        dvbnode->feaktpolarization = tpnode->polarization;
684                                        free(dvbnode->feaktnr);
685                                        if(aktnr != NULL)
686                                                dvbnode->feaktnr = ostrcat(aktnr, NULL, 0, 0);
687                                        else
688                                                dvbnode->feaktnr = NULL;
689                                        free(tmpstr); tmpstr = NULL;
690                                        free(tmpnr); tmpnr = NULL;
691                                        if(flag != 1) debug(200, "found free looptuner witch same orbitalpos/polarization/band %s main: %s", dvbnode->feshortname, tmpdvbnode->feshortname);
692                                        return dvbnode;
693                                }
694                                free(tmpnr); tmpnr = NULL;
695                        }
696                        free(tmpstr); tmpstr = NULL;
697                }
698                dvbnode = dvbnode->next;
699        }
700
701        return NULL;
702}
703
704int feopen(struct dvbdev* node, char *fedev)
705{
706        int fd = -1;
707
708        if(node != NULL)
709        {       
710                if((fd = open(node->dev, O_RDWR | O_NONBLOCK)) < 0)
711                        debug(200, "open frontend failed %s", node->dev);
712                node->fd = fd;
713        }
714        else
715        {
716                if((fd = open(fedev, O_RDWR | O_NONBLOCK)) < 0)
717                        debug(200, "open frontend failed %s", fedev);
718        }
719
720        closeonexec(fd);
721        return fd;
722}
723
724void feclose(struct dvbdev* node, int fd)
725{
726        if(node != NULL)
727        {
728                close(node->fd);
729                node->fd = -1;
730        }
731        else
732                close(fd);
733}
734
735int fegetunlock(struct dvbdev* node)
736{
737        fe_status_t status;
738
739        if(node == NULL)
740        {
741                err("NULL detect");
742                return 1;
743        }
744
745#ifdef SIMULATE
746        return 0;
747#endif
748
749        if(ioctl(node->fd, FE_READ_STATUS, &status) == -1)
750                perr("FE_READ_STATUS");
751
752        if(status & FE_HAS_LOCK)
753                return 0;
754        else
755                return 1;
756}
757
758int fewait(struct dvbdev* node)
759{
760        //struct dvb_frontend_event ev;
761        fe_status_t status;
762        fe_status_t status_m = 0;
763
764        int count = 0;
765
766        if(node == NULL)
767        {
768                err("NULL detect");
769                return 1;
770        }
771
772#ifdef SIMULATE
773        return 0;
774#endif
775
776        int timer = 500;
777
778#ifdef MIPSEL
779        timer = 2000;
780#endif
781
782#ifdef ARM
783        timer = 500;
784#endif
785        if(checkrealbox("HD51") == 1)
786                timer = 1000;
787        //wait for tuner ready
788        debug(200, "wait for tuner start");
789        while(count <= timer)
790        {
791                count++;
792
793                //ioctl(node->fd, FE_GET_EVENT, &ev);
794                //if(ev.status & FE_HAS_LOCK)
795                //      return 0;
796                ioctl(node->fd, FE_READ_STATUS, &status);
797                if(status != 0)
798                {
799                        if(status_m != status)
800                        {
801                                debug(200, "status=%02x, fe_lock=%02x, count=%d", status, FE_HAS_LOCK, count);
802                                status_m = status;
803                        }
804                }
805
806                if(errno == ERANGE)
807                {
808                        usleep(1000);
809                        continue;
810                }
811
812                if(status & FE_HAS_LOCK)
813                {
814        //              if(FE_HAS_SYNC | FE_HAS_LOCK)
815                        debug(200, "wait for tuner end with 0");
816                        return 0;
817                }
818                if(node != NULL && ostrstr(node->feinfo->name, "BCM45208") != NULL)
819                {
820                        if(status & FE_TIMEDOUT)
821                        {
822                                debug(200, "wait for tuner end with FE_TIMEDOUT");
823                                return 1;
824                        }
825                }
826                usleep(1000);
827        }
828        debug(200, "wait for tuner end");
829        //if(ev.status & FE_HAS_LOCK)
830        //      return 0;
831        if(status & FE_HAS_LOCK)
832//      if(FE_HAS_SYNC | FE_HAS_LOCK)
833                return 0;
834        else
835                return 1;
836}
837
838void fegetfrontend(struct dvbdev* node)
839{
840        if(node == NULL)
841        {
842                err("NULL detect");
843                return;
844        }
845
846#if DVB_API_VERSION >= 5
847        struct dtv_property p[8];
848        struct dtv_properties cmdseq;
849        cmdseq.props = p;
850
851        p[0].cmd = DTV_DELIVERY_SYSTEM;
852        p[1].cmd = DTV_FREQUENCY;
853        p[2].cmd = DTV_MODULATION;
854        p[3].cmd = DTV_SYMBOL_RATE;
855        p[4].cmd = DTV_INNER_FEC;
856        p[5].cmd = DTV_INVERSION;
857        p[6].cmd = DTV_ROLLOFF;
858        p[7].cmd = DTV_PILOT;
859        cmdseq.num = 8;
860       
861        if(ioctl(node->fd, FE_GET_PROPERTY, &cmdseq) < 0)
862        {
863                perr("FE_GET_PROPERTY");
864        }
865        else
866        {
867                debug(200, "frontend akt delivery system = %d", p[0].u.data);
868                debug(200, "frontend akt frequency = %d", p[1].u.data);
869                debug(200, "frontend akt inversion = %d", p[5].u.data);
870                debug(200, "frontend akt symbol_rate = %d", p[3].u.data);
871                debug(200, "frontend akt fec_inner = %d", p[4].u.data);
872                debug(200, "frontend akt modulation = %d", p[2].u.data);
873                debug(200, "frontend akt rolloff = %d", p[6].u.data);
874                debug(200, "frontend akt pilot = %d", p[7].u.data);
875        }
876#else
877        struct dvb_frontend_parameters fe_param;
878
879        if(ioctl(node->fd, FE_GET_FRONTEND, &fe_param) < 0)
880        {
881                perr("FE_GET_FRONTEND");
882        }
883        else
884        {
885                debug(200, "frontend akt frequency = %d", fe_param.frequency);
886                debug(200, "frontend akt inversion = %d", fe_param.inversion);
887                debug(200, "frontend akt u.qpsk.symbol_rate = %d", fe_param.u.qpsk.symbol_rate);
888                debug(200, "frontend akt u.qam.symbol_rate = %d", fe_param.u.qam.symbol_rate);
889                debug(200, "frontend akt u.qpsk.fec_inner = %d", fe_param.u.qpsk.fec_inner);
890                debug(200, "frontend akt u.qam.fec_inner = %d", fe_param.u.qam.fec_inner);
891                debug(200, "frontend akt u.qam.modulation = %d", fe_param.u.qam.modulation);
892        }
893#endif
894}
895
896int fesettone(struct dvbdev* node, fe_sec_tone_mode_t tone, int wait)
897{
898        int ret = 0;
899       
900        if(node == NULL)
901        {
902                err("NULL detect");
903                return 1;
904        }
905
906        debug(200, "FE_SET_TONE: %d (%s)", tone, node->feshortname);
907        if(ioctl(node->fd, FE_SET_TONE, tone) == -1)
908        {
909                perr("FE_SET_TONE");
910                ret = 1;
911        }
912        else
913        {
914                node->feakttone = tone;
915                usleep(wait * 1000);
916        }
917       
918        return ret;
919}
920
921//flag 0: reset tuner params on volt off
922//flag 1: don't reset tuner params on volt off
923int fesetvoltage(struct dvbdev* node, fe_sec_voltage_t volt, int wait)
924{
925        int ret = 0;
926       
927        if(node == NULL)
928        {
929                err("NULL detect");
930                return 1;
931        }
932
933        debug(200, "FE_SET_VOLT: %d (%s)", volt, node->feshortname);
934        if(ioctl(node->fd, FE_SET_VOLTAGE, volt) == -1)
935        {
936                perr("FE_SET_VOLTAGE");
937                ret = 1;
938        }
939        else
940        {
941                node->feaktvolt = volt;
942                if(wait > 0) usleep(wait * 1000);
943
944                if(volt == SEC_VOLTAGE_OFF)
945                {
946                        node->feakttransponder = NULL;
947                        node->felasttransponder = NULL;
948                        node->feunicable = 0;
949                        node->feloffrequency = 0;
950                        node->feaktband = 0;
951                        node->feaktpolarization = 0;
952                        node->feakttone = 0;
953                }
954        }
955
956        return ret;
957}
958
959void fediseqcsendburst(struct dvbdev* node, fe_sec_mini_cmd_t burst, int wait)
960{
961        if(node == NULL)
962        {
963                err("NULL detect");
964                return;
965        }
966
967        debug(200, "FE_DISEQC_SEND_BURST: %d (%s)", burst, node->feshortname);
968        if(ioctl(node->fd, FE_DISEQC_SEND_BURST, burst) == -1)
969                perr("FE_DISEQC_SEND_BURST");
970        usleep(wait * 1000);
971}
972
973void fediseqcsendmastercmd(struct dvbdev* node, struct dvb_diseqc_master_cmd *cmd, int wait)
974{
975        int i, repeat = 0, imsg = 0;
976        char* tmpstr = NULL;
977
978        if(node == NULL)
979        {
980                err("NULL detect");
981                return;
982        }
983       
984        if(cmd == NULL) return;
985        if(cmd->msg_len == 0) return;
986
987        tmpstr = ostrcat(node->feshortname, "_diseqc_repeat", 0, 0);
988        repeat = getconfigint(tmpstr, node->feaktnr);
989        free(tmpstr); tmpstr = NULL;
990        if(repeat < 1) repeat = 1;
991
992        for(i = 0; i < repeat; i++)
993        {
994                if(ioctl(node->fd, FE_DISEQC_SEND_MASTER_CMD, cmd) == -1)
995                {
996                        perr("FE_DISEQC_SEND_MASTER_CMD");
997                }
998                usleep(wait * 1000);
999        }
1000        imsg = (cmd->msg[0] << 24) | (cmd->msg[1] << 16) | (cmd->msg[2] << 8) | cmd->msg[3];
1001        debug(200, "DISEQC Master cmd (%s -> %04X)", node->feshortname, imsg);
1002}
1003
1004void fesdiseqcpoweron(struct dvbdev* node)
1005{
1006        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1007
1008        if(node == NULL)
1009        {
1010                err("NULL detect");
1011                return;
1012        }
1013
1014        cmd.msg[0] = 0xE0;
1015        cmd.msg[1] = 0x00;
1016        cmd.msg[2] = 0x03;
1017        cmd.msg_len = 3;
1018
1019        debug(200, "DISEQC Power on (%s)", node->feshortname);
1020        fediseqcsendmastercmd(node, &cmd, 100);
1021}
1022
1023void fesdiseqcreset(struct dvbdev* node)
1024{
1025        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1026       
1027        if(node == NULL)
1028        {
1029                err("NULL detect");
1030                return;
1031        }
1032
1033        cmd.msg[0] = 0xE0;
1034        cmd.msg[1] = 0x00;
1035        cmd.msg[2] = 0x00;
1036        cmd.msg_len = 3;
1037
1038        debug(200, "DISEQC Reset (%s)", node->feshortname);
1039        fediseqcsendmastercmd(node, &cmd, 100);
1040}
1041
1042void fesdiseqcstandby(struct dvbdev* node)
1043{
1044        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1045       
1046        if(node == NULL)
1047        {
1048                err("NULL detect");
1049                return;
1050        }
1051       
1052        cmd.msg[0] = 0xE0;
1053        cmd.msg[1] = 0x00;
1054        cmd.msg[2] = 0x02;
1055        cmd.msg_len = 3;
1056
1057        debug(200, "DISEQC Standby (%s)", node->feshortname);
1058        fediseqcsendmastercmd(node, &cmd, 100);
1059}
1060
1061void fediseqcrotor(struct dvbdev* node, struct transponder* tpnode, int pos, int flag)
1062{
1063        int orbitalpos = 0;
1064        fe_sec_voltage_t oldvolt = 0;
1065        fe_sec_tone_mode_t oldtone = 0;
1066        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1067       
1068        if(node == NULL)
1069        {
1070                err("NULL detect");
1071                return;
1072        }
1073       
1074        fesdiseqcpoweron(node);
1075       
1076        oldvolt = node->feaktvolt;
1077        oldtone = node->feakttone;
1078
1079        if(tpnode == NULL)
1080                orbitalpos = 0;
1081        else
1082                orbitalpos = tpnode->orbitalpos;
1083       
1084        //float speed13V = 1.5; //1.5 Grad pro sec
1085        float speed18V = 1; //2.4 Grad pro sek
1086        float degreesmov, waittime;
1087       
1088        switch(flag)
1089        {
1090                case 0: //stop move
1091                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x60; cmd.msg_len = 3;
1092                        debug(200, "DISEQC Rotorpos stop move (%s)", node->feshortname);
1093                        break;
1094                case 1: //disable limits
1095                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x63; cmd.msg_len = 3;
1096                        debug(200, "DISEQC Rotorpos disable limits (%s)", node->feshortname);
1097                        break;
1098                case 2: //enable limits
1099                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x6A; cmd.msg[3] = 0x00; cmd.msg_len = 4;
1100                        debug(200, "DISEQC Rotorpos enable limits (%s)", node->feshortname);
1101                        break;
1102                case 3: //set east limit
1103                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x66; cmd.msg_len = 3;
1104                        debug(200, "DISEQC Rotorpos set east limit (%s)", node->feshortname);
1105                        break;
1106                case 4: //set west limit
1107                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x67; cmd.msg_len = 3;
1108                        debug(200, "DISEQC Rotorpos set west limit (%s)", node->feshortname);
1109                        break;
1110                case 5: //move east cont.
1111                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x68; cmd.msg[3] = 0x00; cmd.msg_len = 4;
1112                        debug(200, "DISEQC Rotorpos move east cont. (%s)", node->feshortname);
1113                        break;
1114                case 6: //move west cont.
1115                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x69; cmd.msg[3] = 0x00; cmd.msg_len = 4;
1116                        debug(200, "DISEQC Rotorpos move west cont. (%s)", node->feshortname);
1117                        break;
1118                case 7: //store pos
1119                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x6A; cmd.msg[3] = pos; cmd.msg_len = 4;
1120                        debug(200, "DISEQC Rotorpos store pos=%d (%s)", pos, node->feshortname);
1121                        break;
1122                case 8: //goto pos
1123                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x6B; cmd.msg[3] = pos; cmd.msg_len = 4;
1124                        debug(200, "DISEQC Rotorpos goto pos=%d (%s)", pos, node->feshortname);
1125                        break;
1126                case 9: //step xx pos east
1127                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x68; cmd.msg[3] = 256 - pos; cmd.msg_len = 4;
1128                        debug(200, "DISEQC Rotorpos step east pos=%d (%s)", pos, node->feshortname);
1129                        break;
1130                case 10: //step xx pos west
1131                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x69; cmd.msg[3] = 256 - pos; cmd.msg_len = 4;
1132                        debug(200, "DISEQC Rotorpos step west pos=%d (%s)", pos, node->feshortname);
1133                        break;
1134                case 11: //goto xx
1135                        cmd.msg[0] = 0xE0; cmd.msg[1] = 0x31; cmd.msg[2] = 0x6E; cmd.msg[3] = (pos >> 8) & 0xff; cmd.msg[4] = pos & 0xff; cmd.msg_len = 5;
1136                        debug(200, "DISEQC Rotorpos goto xx pos=%d (%s)", pos, node->feshortname);
1137                        break;
1138        }
1139
1140        if(flag >= 0 && flag < 7)
1141        {
1142                fesetvoltage(node, SEC_VOLTAGE_18, 15);
1143                fesettone(node, SEC_TONE_OFF, 15);
1144                fediseqcsendmastercmd(node, &cmd, 100);
1145        }
1146
1147        if((flag == 7 || flag == 9 || flag == 10) && pos != 0)
1148        {
1149                fesetvoltage(node, SEC_VOLTAGE_18, 15);
1150                fesettone(node, SEC_TONE_OFF, 15);
1151                fediseqcsendmastercmd(node, &cmd, 100);
1152        }
1153
1154        if((flag == 8 || flag == 11) && (orbitalpos == 0 || status.rotoroldorbitalpos == 0 || orbitalpos != status.rotoroldorbitalpos))
1155        {
1156                fesetvoltage(node, SEC_VOLTAGE_18, 15);
1157                fesettone(node, SEC_TONE_OFF, 15);
1158                fediseqcsendmastercmd(node, &cmd, 100);
1159
1160                if(status.rotoroldorbitalpos == 0 || orbitalpos == 0)
1161                        waittime = 15;
1162                else
1163                {
1164                        degreesmov = abs(orbitalpos - status.rotoroldorbitalpos) / 10;
1165                        waittime = (int)ceil((float)degreesmov / (float)speed18V);
1166                }
1167
1168                status.rotoroldorbitalpos = orbitalpos;
1169                sleep(waittime);
1170        }
1171       
1172        fesetvoltage(node, oldvolt, 15);
1173        fesettone(node, oldtone, 15);
1174}
1175
1176void fesetunicable(struct dvbdev* node)
1177{
1178        int unicabletune = 0;
1179        char* tmpstr = NULL;
1180        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1181
1182        if(node == NULL)
1183        {
1184                err("NULL detect");
1185                return;
1186        }
1187
1188        tmpstr = ostrcat(node->feshortname, "_lnb_satcr", 0, 0);
1189        int satcr = getconfigint(tmpstr, node->feaktnr) - 1;
1190        if(satcr < 0) satcr = 0;
1191        free(tmpstr); tmpstr = NULL;
1192
1193        tmpstr = ostrcat(node->feshortname, "_diseqc", 0, 0);
1194        int aktdiseqc = getconfigint(tmpstr, node->feaktnr);
1195        if(aktdiseqc < 1) aktdiseqc = 1;
1196        free(tmpstr); tmpstr = NULL;
1197
1198        if(satcr < 8)
1199        {
1200                int unicabletune = 0;
1201
1202                unicabletune |= ((satcr & 0x7) << 13);
1203                unicabletune |= (((aktdiseqc - 1) & 0x1) << 12);
1204                unicabletune |= (((!node->feaktpolarization) & 0x1) << 11);
1205                unicabletune |= ((node->feaktband & 0x1) << 10);
1206                unicabletune |= ((node->feloffrequency / 1000) & 0x3ff);
1207
1208                debug(200, "unicabletune %04X", unicabletune);
1209       
1210                if(status.firstunicablewait == 0)
1211                {
1212                        status.firstunicablewait = getconfigint("firstunicablewait", NULL);
1213                        if(status.firstunicablewait == 0)
1214                                status.firstunicablewait = 1000;
1215                }               
1216       
1217                if(status.firstunicablewait > 0)
1218                {
1219                        usleep(status.firstunicablewait * 1000);
1220                        status.firstunicablewait = -1;
1221                }
1222       
1223                fesetvoltage(node, SEC_VOLTAGE_13, 15);
1224                fesetvoltage(node, SEC_VOLTAGE_18, 15);
1225                fesettone(node, SEC_TONE_OFF, 15);
1226
1227                //feunicable
1228                //byte1 (bit 7/6/5) -> satcr number
1229                //byte1 (bit 4/3/2) -> lnb number
1230                //byte1 (bit 1/0) -> frequ
1231                //byte0 -> frequ
1232       
1233                cmd.msg[0] = 0xE0;
1234                cmd.msg[1] = 0x10;
1235                cmd.msg[2] = 0x5A;
1236                cmd.msg[3] = (unicabletune >> 8) & 0xff;
1237                cmd.msg[4] = unicabletune & 0xff;
1238                cmd.msg_len = 5;
1239        }
1240        else
1241        {
1242                uint64_t unicabletune = 0;
1243
1244                unicabletune |= ((satcr & 0x1F) << 19);
1245                unicabletune |= (((node->feloffrequency / 1000) & 0x7ff) << 8);
1246                unicabletune |= (((aktdiseqc - 1) & 0x1) << 2);
1247                unicabletune |= (((!node->feaktpolarization) & 0x1) << 1);
1248                unicabletune |= (node->feaktband & 0x1);
1249               
1250                debug(200, "unicabletune %04X", unicabletune);
1251       
1252                if(status.firstunicablewait == 0)
1253                {
1254                        status.firstunicablewait = getconfigint("firstunicablewait", NULL);
1255                        if(status.firstunicablewait == 0)
1256                                status.firstunicablewait = 1000;
1257                }               
1258       
1259                if(status.firstunicablewait > 0)
1260                {
1261                        usleep(status.firstunicablewait * 1000);
1262                        status.firstunicablewait = -1;
1263                }
1264       
1265                fesetvoltage(node, SEC_VOLTAGE_13, 15);
1266                fesetvoltage(node, SEC_VOLTAGE_18, 15);
1267                fesettone(node, SEC_TONE_OFF, 15);
1268
1269                //feunicable 2
1270                //byte2 (bit 7/6/5/4/3) -> satcr number
1271                //byte2 (bit 2/1/0) -> frequ
1272                //byte1 -> frequ
1273                //byte0 (bit 3/1/0) -> lnb number
1274               
1275                cmd.msg[0] = 0x70;
1276                cmd.msg[1] = (unicabletune >> 16) & 0xff;
1277                cmd.msg[2] = (unicabletune >> 8) & 0xff;
1278                cmd.msg[3] = unicabletune & 0xff;
1279                cmd.msg_len = 4;
1280        }       
1281
1282        debug(200, "send diseqc unicable cmd (%s)", node->feshortname);
1283        fediseqcsendmastercmd(node, &cmd, 100);
1284        fesetvoltage(node, SEC_VOLTAGE_13, 15);
1285}
1286
1287void fediseqcset(struct dvbdev* node, struct transponder* tpnode)
1288{
1289        char* tmpstr = NULL;
1290        int toneburst = 0, cmdorder = 0, input = 0, uinput = 0, diseqmode = 0, rotorpos = 0, latpos = 0, longpos = 0;
1291        float latitude = 0, longitude = 0;
1292        fe_sec_mini_cmd_t mini = -1;
1293        struct dvb_diseqc_master_cmd cmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1294        struct dvb_diseqc_master_cmd ucmd = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0};
1295       
1296        if(node == NULL) return;
1297       
1298        tmpstr = ostrcat(node->feshortname, "_diseqc_committedcmd", 0, 0);
1299        input = getconfigint(tmpstr, node->feaktnr);
1300        free(tmpstr); tmpstr = NULL;
1301        tmpstr = ostrcat(node->feshortname, "_diseqc_uncommittedcmd", 0, 0);
1302        uinput = getconfigint(tmpstr, node->feaktnr);
1303        free(tmpstr); tmpstr = NULL;
1304        tmpstr = ostrcat(node->feshortname, "_diseqc_mode", 0, 0);
1305        diseqmode = getconfigint(tmpstr, node->feaktnr);
1306        free(tmpstr); tmpstr = NULL;
1307        tmpstr = ostrcat(node->feshortname, "_diseqc_cmdorder", 0, 0);
1308        cmdorder = getconfigint(tmpstr, node->feaktnr);
1309        free(tmpstr); tmpstr = NULL;
1310        tmpstr = ostrcat(node->feshortname, "_diseqc_toneburst", 0, 0);
1311        toneburst = getconfigint(tmpstr, node->feaktnr);
1312        free(tmpstr); tmpstr = NULL;
1313        tmpstr = ostrcat(node->feshortname, "_diseqc_rotorpos", 0, 0);
1314        rotorpos = getconfigint(tmpstr, node->feaktnr);
1315        free(tmpstr); tmpstr = NULL;
1316       
1317        latitude = getconfigfloat("latitude", NULL);
1318        longitude = getconfigfloat("longitude", NULL);
1319        latpos = getconfigint("latpos", NULL);
1320        longpos = getconfigint("longpos", NULL);
1321
1322        tmpstr = ostrcat(node->feshortname, "_diseqc", 0, 0);
1323        int aktdiseqc = getconfigint(tmpstr, node->feaktnr);
1324        if(aktdiseqc < 1) aktdiseqc = 1;
1325        free(tmpstr); tmpstr = NULL;
1326
1327        debug(200, "set diseqc: number=%d, band=%d, pol=%d, diseqmode=%d, input=%d, uinput=%d, cmdorder=%d, toneburst=%d (%s)", aktdiseqc, node->feaktband, node->feaktpolarization, diseqmode, input, uinput, cmdorder, toneburst, node->feshortname);
1328         
1329        switch(toneburst)
1330        {
1331                case 1: mini = SEC_MINI_A; break;
1332                case 2: mini = SEC_MINI_B; break;
1333        }
1334       
1335        if(diseqmode == 100) // Tonburst A/B
1336        {
1337                debug(200, "set diseqc: Tonburst A/B (%s)", node->feshortname);
1338                if(mini == -1)
1339                        mini = (aktdiseqc - 1) % 2 ? SEC_MINI_B : SEC_MINI_A;
1340                fediseqcsendburst(node, mini, 15);
1341                return;
1342        }
1343               
1344        if(diseqmode == 0 || diseqmode == 1) // Diseqc 1.0 + 1.1
1345        {
1346                debug(200, "set committed switch (%s)", node->feshortname);
1347                fesdiseqcpoweron(node);
1348                cmd.msg[0] = 0xE0;
1349                cmd.msg[1] = 0x10;
1350                cmd.msg[2] = 0x38;
1351
1352                if(input == 0)
1353                        cmd.msg[3] = 0xF0 | ((((aktdiseqc - 1) * 4) & 0x0F) | (node->feaktband ? 1 : 0) | (node->feaktpolarization ? 0 : 2));
1354                else
1355                        cmd.msg[3] = 0xF0 + ((input - 1) & 0x0F);
1356
1357                cmd.msg_len = 4;
1358        }
1359
1360        if(diseqmode == 1) // Diseqc 1.1
1361        {
1362                if(uinput > 0)
1363                {
1364                        debug(200, "set uncommitted switch (%s)", node->feshortname);
1365                        fesdiseqcpoweron(node);
1366                        ucmd.msg[0] = 0xE0;
1367                        ucmd.msg[1] = 0x10;
1368                        ucmd.msg[2] = 0x39;
1369                        ucmd.msg[3] = 0xF0 + ((uinput - 1) & 0x0F);
1370                        ucmd.msg_len = 4;
1371                }
1372        }
1373                 
1374        switch(cmdorder)
1375        {
1376                case 1:
1377                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1378                        fediseqcsendmastercmd(node, &cmd, 100);
1379                        break;
1380                case 2:
1381                        fediseqcsendmastercmd(node, &cmd, 100);
1382                        if(uinput > 0) fediseqcsendmastercmd(node, &ucmd, 100);
1383                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1384                        break;
1385                case 3:
1386                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1387                        fediseqcsendmastercmd(node, &cmd, 100);
1388                        if(uinput > 0) fediseqcsendmastercmd(node, &ucmd, 100);
1389                        break;
1390                case 4:
1391                        if(uinput > 0) fediseqcsendmastercmd(node, &ucmd, 100);
1392                        fediseqcsendmastercmd(node, &cmd, 100);
1393                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1394                        break;
1395                case 5:
1396                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1397                        if(uinput > 0) fediseqcsendmastercmd(node, &ucmd, 100);
1398                        fediseqcsendmastercmd(node, &cmd, 100);
1399                        break;
1400                default:
1401                        fediseqcsendmastercmd(node, &cmd, 100);
1402                        if(mini != -1) fediseqcsendburst(node, mini, 15);
1403                        break;
1404        }
1405       
1406        if(diseqmode == 2) // Diseqc 1.2
1407        {
1408                fediseqcrotor(node, tpnode, rotorpos, 8);
1409        }
1410       
1411        if(diseqmode == 3) // Diseqc 1.3 (USALS)
1412        {
1413                double orbitalpos = tpnode->orbitalpos / 10.00;
1414
1415                if(latpos == 1) // south
1416                        latitude = -latitude;
1417
1418                if(longpos == 1) // west
1419                        longitude = 360 - longitude;
1420
1421                double satHourAngle = calcSatHourangle(orbitalpos, latitude, longitude);
1422                int gotoXTable[10] = {0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E};
1423
1424                if(latitude >= 0) // Northern Hemisphere
1425                {
1426                        int tmp = (int)round(fabs(180 - satHourAngle) * 10.0);
1427                        rotorpos = (tmp / 10) * 0x10 + gotoXTable[tmp % 10];
1428
1429                        if(satHourAngle < 180) // the east
1430                                rotorpos |= 0xE000;
1431                        else // west
1432                                rotorpos |= 0xD000;
1433                }
1434                else // Southern Hemisphere
1435                {
1436                        if(satHourAngle < 180) // the east
1437                        {
1438                                int tmp = (int)round(fabs(satHourAngle) * 10.0);
1439                                rotorpos = (tmp / 10) * 0x10 + gotoXTable[tmp % 10];
1440                                rotorpos |= 0xD000;
1441                        }
1442                        else // west
1443                        {
1444                                int tmp = (int)round(fabs(360 - satHourAngle) * 10.0);
1445                                rotorpos = (tmp / 10) * 0x10 + gotoXTable[tmp % 10];
1446                                rotorpos |= 0xE000;
1447                        }
1448                }
1449                debug(200, "orbitalpos=%f, latitude=%f, longitude=%f, rotorpos=%04x", orbitalpos, latitude, longitude, rotorpos);
1450
1451                fediseqcrotor(node, tpnode, rotorpos, 11);
1452        }
1453}
1454
1455void feset(struct dvbdev* node, struct transponder* tpnode)
1456{
1457        int voltagemode = 0, tonemode = 0;
1458        fe_sec_tone_mode_t tone;
1459        fe_sec_voltage_t volt;
1460        struct dvbdev* dvbnode = dvbdev;
1461        char* tmpstr = NULL;
1462
1463        if(node == NULL)
1464        {
1465                err("NULL detect");
1466                return;
1467        }
1468
1469        // set volage off from other unused frontend
1470        while(dvbnode != NULL)
1471        {
1472                if(dvbnode->type != FRONTENDDEV) break;
1473                if(dvbnode->type == FRONTENDDEV && dvbnode != node && dvbnode->felock == 0 && dvbnode != status.aktservice->fedev)
1474                {
1475                        fesetvoltage(dvbnode, SEC_VOLTAGE_OFF, 0);
1476                }
1477                dvbnode = dvbnode->next;
1478        }
1479
1480        calclof(node, tpnode, NULL, 0);
1481
1482        tmpstr = ostrcat(node->feshortname, "lnb_voltagemode", 0, 0);
1483        voltagemode = getconfigint(tmpstr, node->feaktnr);
1484
1485        free(tmpstr); tmpstr = NULL;
1486        switch(voltagemode)
1487        {
1488                case 1: volt = SEC_VOLTAGE_13; break;
1489                case 2: volt = SEC_VOLTAGE_18; break;
1490                default: volt = node->feaktpolarization ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18;
1491                         if(node->feunicable == 1) volt = SEC_VOLTAGE_13;
1492        }
1493        fesetvoltage(node, volt, 15);
1494
1495        tmpstr = ostrcat(node->feshortname, "_diseqc", 0, 0);
1496        if(getconfigint(tmpstr, node->feaktnr) == 0 || node->feunicable == 1)
1497        {
1498                debug(200, "don't use diseqc");
1499        }
1500        else
1501        {
1502                fesettone(node, SEC_TONE_OFF, 15);
1503                fediseqcset(node, tpnode);
1504        }
1505        free(tmpstr); tmpstr = NULL;
1506
1507        tmpstr = ostrcat(node->feshortname, "_lnb_tonemode", 0, 0);
1508        tonemode = getconfigint(tmpstr, node->feaktnr);
1509        free(tmpstr); tmpstr = NULL;
1510        switch(tonemode)
1511        {
1512                case 1: tone = SEC_TONE_ON; break;
1513                case 2: tone = SEC_TONE_OFF; break;
1514                default: tone = node->feaktband ? SEC_TONE_ON : SEC_TONE_OFF;
1515                         if(node->feunicable == 1) tone = SEC_TONE_OFF;
1516        }
1517        fesettone(node, tone, 15);
1518}
1519
1520void fediscard(struct dvbdev* node)
1521{
1522        struct dvb_frontend_event ev;
1523        int count = 0;
1524
1525        if(node == NULL)
1526        {
1527                err("NULL detect");
1528                return;
1529        }
1530
1531        /* discard stale QPSK events */
1532        while(count < 20)
1533        {
1534                count++;
1535                if(ioctl(node->fd, FE_GET_EVENT, &ev) == -1)
1536                        break;
1537        }
1538}
1539
1540uint16_t fereadsnr(struct dvbdev* node)
1541{
1542        uint16_t snr = 0;
1543        if(node == NULL)
1544        {
1545                err("NULL detect");
1546                return 0;
1547        }
1548        int signalquality = 0;
1549        int signalqualitydb = 0;
1550//#ifdef ARM
1551#ifdef MIPSEL
1552//#if DVB_API_VERSION >= 5
1553#if DVB_API_VERSION > 5 || DVB_API_VERSION == 5 && DVB_API_VERSION_MINOR >= 10
1554
1555        int test1 = 0;
1556        int test2 = 0;
1557        struct dtv_property prop[1];
1558        memset(prop, 0, sizeof(prop));
1559        prop[0].cmd = DTV_STAT_CNR;
1560        struct dtv_properties props;
1561        props.props = prop;
1562        props.num = 1;
1563       
1564        int wasopen = 0;
1565       
1566        if(node->fd == -1)
1567                node->fd = feopen(node, NULL);
1568        else
1569                wasopen = 1;
1570               
1571        if(ioctl(node->fd, FE_GET_PROPERTY,  &props) < 0 && errno != ERANGE)
1572        {
1573                perr("FE_GET_PROPERTY");
1574        }
1575        else
1576        {
1577                for(unsigned int i=0; i<prop[0].u.st.len; i++)
1578                {
1579                        if (prop[0].u.st.stat[i].scale == FE_SCALE_DECIBEL && test1 == 0)
1580                        {
1581                                test1 = 1;
1582                                signalqualitydb = prop[0].u.st.stat[i].svalue / 10;
1583                                printf("***** new snr signalqualitydb:%d\n", signalqualitydb);
1584                        }
1585                        else if (prop[0].u.st.stat[i].scale == FE_SCALE_RELATIVE && test2 == 0)
1586                        {
1587                                test2 = 1;
1588                                signalquality = prop[0].u.st.stat[i].svalue;
1589                                printf("***** new snr signalquality:%d\n", signalquality);
1590                        }
1591                }
1592        }
1593        if(wasopen != 1)
1594                feclose(node, -1);
1595#endif
1596        if(!signalquality && !signalqualitydb)
1597        {
1598                int ret = 0x12345678;
1599                int sat_max = 1600; // we assume a max of 16db here
1600                int ter_max = 2900; // we assume a max of 29db here
1601                int cab_max = 4200; // we assume a max of 42db here
1602                //int atsc_max = 4200; // we assume a max of 42db here
1603               
1604                ioctl(node->fd, FE_READ_SNR, &snr);
1605               
1606                printf("***** old snr signalquality\n");               
1607               
1608                if(ostrstr(node->feinfo->name, "Si2166B") != NULL)
1609                {
1610                        ret = (snr * 240) >> 8;
1611                }
1612                else if (ostrstr(node->feinfo->name, "ATBM781x") != NULL)
1613                {
1614                        ret = snr*10;
1615                }
1616                else if(ostrstr(node->feinfo->name, "BCM4506") != NULL || ostrstr(node->feinfo->name, "BCM4506 (internal)") != NULL || ostrstr(node->feinfo->name, "BCM4505") != NULL || ostrstr(node->feinfo->name, "BCM73625 (G3)") != NULL || ostrstr(node->feinfo->name, "BCM45208") != NULL)
1617                {
1618                        ret = (snr * 100) >> 8;
1619                }
1620                else if (ostrstr(node->feinfo->name, "Si216") != NULL) // all new Models with SI Tuners
1621                {
1622                        ret = snr;
1623                }
1624                signalqualitydb = ret;
1625                if (ret == 0x12345678) // no snr db calculation avail.. return untouched snr value..
1626                {
1627                        signalquality = snr;
1628                }
1629                else
1630                {
1631                        if(node->feinfo->type == FE_QPSK)
1632                                signalquality = (ret >= sat_max ? 65536 : ret * 65536 / sat_max);
1633                        else if(node->feinfo->type == FE_QAM)
1634                                signalquality = (ret >= cab_max ? 65536 : ret * 65536 / cab_max);
1635                        else if(node->feinfo->type == FE_OFDM)
1636                                signalquality = (ret >= ter_max ? 65536 : ret * 65536 / ter_max);
1637                }
1638        }
1639        debug(200, "frontend snr = %02x", signalquality );
1640        return signalquality;
1641#else
1642        ioctl(node->fd, FE_READ_SNR, &snr);
1643        debug(200, "frontend snr = %02x", (snr * 100) / 0xffff);
1644        return snr;
1645#endif
1646}
1647
1648uint16_t fereadsignalstrength(struct dvbdev* node)
1649{
1650        uint16_t signal = 0;
1651
1652        if(node == NULL)
1653        {
1654                err("NULL detect");
1655                return 0;
1656        }
1657//#ifdef ARM
1658//#ifdef MIPSEL
1659
1660#ifdef ARM             
1661        struct dtv_property prop[1];
1662        prop[0].cmd = DTV_STAT_SIGNAL_STRENGTH;
1663        struct dtv_properties props;
1664        props.props = prop;
1665        props.num = 1;
1666        ioctl(node->fd, FE_GET_PROPERTY, &props);
1667        for(unsigned int i=0; i<prop[0].u.st.len; i++)
1668        {
1669                if (prop[0].u.st.stat[i].scale == FE_SCALE_RELATIVE)
1670                        signal = prop[0].u.st.stat[i].uvalue;
1671        }
1672        if (!signal)
1673        {
1674                ioctl(node->fd, FE_READ_SIGNAL_STRENGTH, &signal);
1675                printf("brutto STRENGTH %02x\n", signal);
1676                if(ostrstr(node->feinfo->name, "Si2166B") != NULL)
1677                {
1678                        if(signal == 128)
1679                                signal = 0xffff;
1680                        else if(signal > 128)
1681                                signal =  0xffff - ((signal - 128) / 2 * 655);
1682                        else if(signal < 128)
1683                                signal =  0xffff - ((128 - signal) / 2 * 655);
1684                }
1685                printf("frontend signal = %02x\n", (signal * 100) / 0xffff);
1686        }
1687        return signal;
1688#else   
1689
1690#if DVB_API_VERSION > 5 || DVB_API_VERSION == 5 && DVB_API_VERSION_MINOR >= 10
1691        struct dtv_property prop[1];
1692        memset(prop, 0, sizeof(prop));
1693        prop[0].cmd = DTV_STAT_SIGNAL_STRENGTH;
1694        struct dtv_properties props;
1695        props.props = prop;
1696        props.num = 1;
1697       
1698        int wasopen = 0;
1699       
1700        if(node->fd == -1)
1701                node->fd = feopen(node, NULL);
1702        else
1703                wasopen = 1;
1704       
1705        if (ioctl(node->fd, FE_GET_PROPERTY, &props) < 0 && errno != ERANGE)
1706        {
1707                debug(200, "DTV_STAT_SIGNAL_STRENGTH failed");
1708        }
1709        else
1710        {
1711                for(unsigned int i=0; i<prop[0].u.st.len; i++)
1712                {
1713                        if (prop[0].u.st.stat[i].scale == FE_SCALE_RELATIVE)
1714                                signal = prop[0].u.st.stat[i].uvalue;
1715                }
1716        }
1717        if (signal)
1718        {
1719                if(wasopen != 1)
1720                        feclose(node, -1);
1721                printf("new STRENGTH %d\n", signal);
1722                return signal;
1723        }
1724        // fallback to old DVB API
1725//#endif
1726#endif 
1727        if(ioctl(node->fd, FE_READ_SIGNAL_STRENGTH, &signal) < 0 && errno != ERANGE)
1728        {
1729                debug(200, "FE_READ_SIGNAL_STRENGTH failed");
1730        }
1731        else
1732        {
1733                printf("brutto STRENGTH %d tuner:%s\n", signal, node->feinfo->name ); 
1734                if(ostrstr(node->feinfo->name, "Si2166B") != NULL || ostrstr(node->feinfo->name, "BCM45208") != NULL)
1735                        signal = signal * 257;
1736                debug(200, "frontend signal = %02x", (signal * 100) / 0xffff);
1737        }
1738        if(signal == 0)
1739        {
1740                fe_status_t status = fereadstatus(node);
1741                if(status & FE_HAS_LOCK) signal = 1;
1742        }
1743        printf("old STRENGTH %d\n", signal);       
1744        return signal;
1745#endif
1746}
1747
1748
1749uint32_t fereadber(struct dvbdev* node)
1750{
1751        uint32_t ber = 0;
1752
1753        if(node == NULL)
1754        {
1755                err("NULL detect");
1756                return 0;
1757        }
1758
1759        ioctl(node->fd, FE_READ_BER, &ber);
1760        debug(200, "frontend ber = %02x", ber);
1761        return ber;
1762}
1763
1764uint32_t fereaduncorrectedblocks(struct dvbdev* node)
1765{
1766        uint32_t unc = 0;
1767
1768        if(node == NULL)
1769        {
1770                err("NULL detect");
1771                return 0;
1772        }
1773
1774        ioctl(node->fd, FE_READ_UNCORRECTED_BLOCKS, &unc);
1775        debug(200, "frontend unc = %02x", unc);
1776        return unc;
1777}
1778
1779fe_status_t fereadstatus(struct dvbdev* node)
1780{
1781        fe_status_t status;
1782
1783        if(node == NULL)
1784        {
1785                err("NULL detect");
1786                return -1;
1787        }
1788
1789        if(ioctl(node->fd, FE_READ_STATUS, &status) == -1)
1790                perr("FE_READ_STATUS");
1791
1792        debug(200, "frontend status = %02x", status);
1793        if(status & FE_HAS_LOCK) debug(200, "frontend = FE_HAS_LOCK");
1794        if(status & FE_HAS_SIGNAL) debug(200, "frontend = FE_HAS_SIGNAL");
1795        if(status & FE_HAS_CARRIER) debug(200, "frontend = FE_HAS_CARRIER");
1796        if(status & FE_HAS_VITERBI) debug(200, "frontend = FE_HAS_VITERBI");
1797        if(status & FE_HAS_SYNC) debug(200, "frontend = FE_HAS_SYNC");
1798        if(status & FE_TIMEDOUT) debug(200, "frontend = FE_TIMEDOUT");
1799        if(status & FE_REINIT) debug(200, "frontend = FE_REINIT");
1800
1801        return status;
1802}
1803
1804int fetunedvbs(struct dvbdev* node, struct transponder* tpnode)
1805{
1806        if(node == NULL || tpnode == NULL)
1807        {
1808                err("NULL detect");
1809                return 1;
1810        }
1811       
1812        if(node->feunicable == 1)
1813        {
1814                fesetunicable(node);
1815                if(node->feloffrequency_uni == 0)
1816                {
1817                        char* tmpstr = ostrcat(node->feshortname, "_lnb_satcrfrequ", 0, 0);
1818                        node->feloffrequency = getconfigint(tmpstr, node->feaktnr) * 1000;
1819                        free(tmpstr); tmpstr = NULL;
1820          }
1821          else
1822                node->feloffrequency = node->feloffrequency_uni;
1823        }
1824
1825#if DVB_API_VERSION >= 5
1826        struct dtv_property p[10];
1827        struct dtv_properties cmdseq;
1828        cmdseq.props = p;
1829
1830        //convert transponderlist for dvbapi5
1831        int system = tpnode->system;
1832        switch(system)
1833        {
1834                case 0: system = SYS_DVBS; break;
1835                case 1: system = SYS_DVBS2; break;
1836                default: system = SYS_DVBS; break;
1837        }
1838
1839        int fec = tpnode->fec;
1840        switch(fec)
1841        {
1842                case 0: fec = FEC_AUTO; break;
1843                case 1: fec = FEC_1_2; break;
1844                case 2: fec = FEC_2_3; break;
1845                case 3: fec = FEC_3_4; break;
1846                case 4: fec = FEC_5_6; break;
1847                case 5: fec = FEC_7_8; break;
1848                case 6: fec = FEC_8_9; break;
1849                case 7: fec = FEC_3_5; break;
1850                case 8: fec = FEC_4_5; break;
1851                case 9: fec = FEC_9_10; break;
1852                case 15: fec = FEC_NONE; break;
1853                default: fec = FEC_AUTO; break;
1854        }
1855        int pilot = tpnode->pilot;
1856        switch(pilot)
1857        {
1858                case 0: pilot = PILOT_OFF; break;
1859                case 1: pilot = PILOT_ON; break;
1860                case 2: pilot = PILOT_AUTO; break;
1861                default: pilot = PILOT_AUTO; break;
1862        }
1863        int rolloff = tpnode->rolloff;
1864        switch(rolloff)
1865        {
1866                case 0: rolloff = ROLLOFF_35; break;
1867                case 1: rolloff = ROLLOFF_25; break;
1868                case 2: rolloff = ROLLOFF_20; break;
1869                default: rolloff = ROLLOFF_35; break;
1870        }
1871
1872        int modulation = tpnode->modulation;
1873        switch(modulation)
1874        {
1875                case 0: modulation = QPSK; break;
1876                case 1: modulation = QPSK; break;
1877                case 2: modulation = PSK_8; break;
1878                case 3: modulation = QAM_16; break;
1879                default: modulation = QPSK; break;
1880        }
1881
1882        p[0].cmd = DTV_CLEAR;
1883        p[1].cmd = DTV_DELIVERY_SYSTEM, p[1].u.data = system;
1884        p[2].cmd = DTV_FREQUENCY,       p[2].u.data = node->feloffrequency;
1885        p[3].cmd = DTV_MODULATION,      p[3].u.data = modulation;
1886        p[4].cmd = DTV_SYMBOL_RATE,     p[4].u.data = tpnode->symbolrate;
1887        p[5].cmd = DTV_INNER_FEC,       p[5].u.data = fec;
1888        p[6].cmd = DTV_INVERSION,       p[6].u.data = (fe_spectral_inversion_t) tpnode->inversion;
1889        if(system == SYS_DVBS2)
1890        {
1891                p[7].cmd = DTV_ROLLOFF,         p[7].u.data = rolloff;
1892                p[8].cmd = DTV_PILOT,           p[8].u.data = pilot;
1893                p[9].cmd = DTV_TUNE;
1894                cmdseq.num = 10;
1895        }
1896        else
1897        {
1898                p[7].cmd = DTV_TUNE;
1899                cmdseq.num = 8;
1900        }
1901
1902        debug(200, "new dvbapi: frequ=%d, inversion=%d, pilot=%d, rolloff=%d, fec=%d, sr=%d, modulation=%d, system=%d (%s)", node->feloffrequency, tpnode->inversion, pilot, rolloff, fec, tpnode->symbolrate, modulation, system, node->feshortname);
1903#else
1904        struct dvb_frontend_parameters tuneto;
1905        fe_spectral_inversion_t inversion = tpnode->inversion;
1906
1907        //convert transponderlist for dvbapi3
1908        int fec = tpnode->fec;
1909        if(tpnode->system == 1)
1910        {
1911                if(tpnode->modulation == 1) fec = fec + 9;
1912                if(tpnode->modulation == 2) fec = fec + 18;
1913        }
1914
1915        inversion |= (tpnode->rolloff << 2) | inversion; // use bit 2..3 of inversion for rolloff
1916        inversion |= (tpnode->pilot << 4) | inversion; // use bit 4..5 of inversion for pilot
1917
1918        tuneto.frequency = node->feloffrequency;
1919        tuneto.inversion = inversion;
1920        tuneto.u.qpsk.symbol_rate = tpnode->symbolrate;
1921        tuneto.u.qpsk.fec_inner = fec;
1922
1923        debug(200, "old dvbapi: frequ=%d, inversion=%d, pilot=%d, rolloff=%d, fec=%d, sr=%d modulation=%d, system=%d (%s)", node->feloffrequency, inversion, tpnode->pilot, tpnode->rolloff, fec, tpnode->symbolrate, tpnode->modulation, tpnode->system, node->feshortname);
1924#endif
1925
1926        fediscard(node);
1927
1928#if DVB_API_VERSION >= 5
1929        if((ioctl(node->fd, FE_SET_PROPERTY, &cmdseq)) == -1)
1930        {
1931                perr("FE_SET_PROPERTY");
1932                return 1;
1933        }
1934#else
1935        if(ioctl(node->fd, FE_SET_FRONTEND, &tuneto) == -1)
1936        {
1937                perr("FE_SET_FRONTEND");
1938                return 1;
1939        }
1940#endif
1941
1942        return 0;
1943}
1944
1945int fetunedvbc(struct dvbdev* node, struct transponder* tpnode)
1946{
1947        if(node == NULL || tpnode == NULL)
1948        {
1949                err("NULL detect");
1950                return 1;
1951        }
1952       
1953        int fec = tpnode->fec;
1954        switch(fec)
1955        {
1956                case 0: fec = FEC_AUTO; break;
1957                case 1: fec = FEC_1_2; break;
1958                case 2: fec = FEC_2_3; break;
1959                case 3: fec = FEC_3_4; break;
1960                case 4: fec = FEC_5_6; break;
1961                case 5: fec = FEC_7_8; break;
1962                case 6: fec = FEC_8_9; break;
1963                case 7: fec = FEC_3_5; break;
1964                case 8: fec = FEC_4_5; break;
1965                case 9: fec = FEC_9_10; break;
1966                case 15: fec = FEC_NONE; break;
1967                default: fec = FEC_AUTO; break;
1968        }
1969
1970        int modulation = tpnode->modulation;
1971        switch(modulation)
1972        {
1973                case 0: modulation = QAM_AUTO; break;
1974                case 1: modulation = QAM_16; break;
1975                case 2: modulation = QAM_32; break;
1976                case 3: modulation = QAM_64; break;
1977                case 4: modulation = QAM_128; break;
1978                case 5: modulation = QAM_256; break;
1979                default: modulation = QAM_AUTO; break;
1980        }
1981
1982#if DVB_API_VERSION >= 5
1983        struct dtv_property p[8];
1984        struct dtv_properties cmdseq;
1985        cmdseq.props = p;
1986       
1987        int system = tpnode->system;
1988
1989        switch(system)
1990        {       
1991#ifdef DREAMBOX
1992                //case 0: system = SYS_DVBC_ANNEX_A; break;
1993                //case 1: system = SYS_DVBC_ANNEX_C; break;
1994                //default: system = SYS_DVBC_ANNEX_A; break;
1995                case 0: system = 1; break;
1996                case 1: system = 18; break;
1997                default: system = 1; break;
1998#elif ARM
1999                case 0: system = SYS_DVBC_ANNEX_A; break;
2000                case 1: system = SYS_DVBC_ANNEX_C; break;
2001                default: system = SYS_DVBC_ANNEX_A; break;
2002#else
2003                case 0: system = 1; break;
2004                case 1: system = 3; break;
2005                default: system = 1; break;
2006#endif
2007        }
2008       
2009        p[0].cmd = DTV_CLEAR;
2010        p[1].cmd = DTV_DELIVERY_SYSTEM, p[1].u.data = system;
2011        p[2].cmd = DTV_FREQUENCY,       p[2].u.data = tpnode->frequency;
2012        p[3].cmd = DTV_MODULATION,      p[3].u.data = modulation;
2013        p[4].cmd = DTV_SYMBOL_RATE,     p[4].u.data = tpnode->symbolrate;
2014        p[5].cmd = DTV_INVERSION,       p[5].u.data = (fe_spectral_inversion_t) tpnode->inversion;
2015        p[6].cmd = DTV_INNER_FEC,       p[6].u.data = fec;
2016        p[7].cmd = DTV_TUNE;
2017        cmdseq.num = 8;
2018
2019        debug(200, "new dvbapi: frequ=%d, inversion=%d, fec=%d, sr=%d, modulation=%d, system=%d (%s)", tpnode->frequency, tpnode->inversion, fec, tpnode->symbolrate, modulation, tpnode->system, node->feshortname);
2020#else
2021        struct dvb_frontend_parameters tuneto;
2022        tuneto.frequency = tpnode->frequency;
2023        tuneto.inversion = tpnode->inversion;
2024        tuneto.u.qam.symbol_rate = tpnode->symbolrate;
2025        tuneto.u.qam.fec_inner = tpnode->fec;
2026        tuneto.u.qam.modulation = tpnode->modulation;
2027
2028        debug(200, "old dvbapi: frequ=%d, inversion=%d, fec=%d, sr=%d, modulation=%d (%s)", tpnode->frequency, tpnode->inversion, fec, tpnode->symbolrate, modulation, node->feshortname);
2029#endif
2030
2031        fediscard(node);
2032
2033#if DVB_API_VERSION >= 5
2034        if((ioctl(node->fd, FE_SET_PROPERTY, &cmdseq)) == -1)
2035        {
2036                perr("FE_SET_PROPERTY");
2037                return 1;
2038        }
2039#else
2040        if(ioctl(node->fd, FE_SET_FRONTEND, &tuneto) == -1)
2041        {
2042                perr("FE_SET_FRONTEND");
2043                return 1;
2044        }
2045#endif
2046
2047        return 0;
2048}
2049
2050int fetunedvbt(struct dvbdev* node, struct transponder* tpnode)
2051{
2052        if(node == NULL || tpnode == NULL)
2053        {
2054                err("NULL detect");
2055                return 1;
2056        }
2057        debug(200, "transponder:frequ=%d, inversion=%d, bandwidth=%d, hp=%d, lp=%d, modulation=%d transmission=%d guardinterval=%d hierarchy=%d system=%d (%s)", tpnode->frequency, tpnode->inversion, tpnode->symbolrate, tpnode->fec, tpnode->polarization, tpnode->modulation, tpnode->pilot, tpnode->rolloff, tpnode->system, tpnode->system, node->feshortname);
2058       
2059        char* tmpstr = ostrcat(node->feshortname, "_terr_volt", 0, 0);
2060        if(getconfigint(tmpstr, NULL) == 1)
2061                fesetvoltage(node, SEC_VOLTAGE_13, 10);
2062        else
2063                fesetvoltage(node, SEC_VOLTAGE_OFF, 10);
2064        free(tmpstr); tmpstr = NULL;
2065       
2066       
2067        fe_delivery_system_t system = tpnode->system;
2068       
2069        int hp = tpnode->fec; //fec = hp on DVBT
2070        switch(hp)
2071        {
2072                case T_FEC_1_2: hp = FEC_1_2; break;
2073                case T_FEC_2_3: hp = FEC_2_3; break;
2074                case T_FEC_3_4: hp = FEC_3_4; break;
2075                case T_FEC_5_6: hp = FEC_5_6; break;
2076                case T_FEC_6_7: hp = FEC_6_7; break;   
2077                case T_FEC_7_8: hp = FEC_7_8; break;
2078                case T_FEC_8_9: hp = FEC_8_9; break;
2079                default: hp = FEC_AUTO; break;
2080        }
2081
2082        int lp = tpnode->polarization; //polarization = lp on DVBT
2083        switch(lp)
2084        {
2085                case T_FEC_1_2: lp = FEC_1_2; break;
2086                case T_FEC_2_3: lp = FEC_2_3; break;
2087                case T_FEC_3_4: lp = FEC_3_4; break;
2088                case T_FEC_5_6: lp = FEC_5_6; break;
2089                case T_FEC_6_7: lp = FEC_6_7; break;   
2090                case T_FEC_7_8: lp = FEC_7_8; break;
2091                case T_FEC_8_9: lp = FEC_8_9; break;
2092                default: lp = FEC_AUTO; break;
2093        }
2094
2095        int modulation = tpnode->modulation;
2096        switch(modulation)
2097        {
2098                case T_Modulation_QPSK: modulation = QPSK; break;
2099                case T_Modulation_QAM16: modulation = QAM_16; break;
2100                case T_Modulation_QAM64: modulation = QAM_64; break;
2101                case T_Modulation_QAM256: modulation = QAM_256; break;
2102                default: modulation = QAM_AUTO; break;
2103        }
2104
2105        int bandwidth = tpnode->symbolrate; //symbolrate = bandwidth on DVBT
2106        switch(bandwidth)
2107        {
2108                case T_Bandwidth_8MHz: bandwidth = 8000000; break;
2109                case T_Bandwidth_7MHz: bandwidth = 7000000; break;
2110                case T_Bandwidth_6MHz: bandwidth = 6000000; break;
2111                case T_Bandwidth_5MHz: bandwidth = 5000000; break;
2112                case T_Bandwidth_1_712MHz: bandwidth = 1712000; break;
2113                case T_Bandwidth_10MHz: bandwidth = 10000000; break;   
2114                case T_Bandwidth_Auto: bandwidth = 0; break;
2115                default: bandwidth = 0; break;
2116        }
2117       
2118        int transmission = tpnode->pilot; //pilot = transmission on DVBT
2119        switch(transmission)
2120        {
2121                case T_TransmissionMode_2k: transmission = TRANSMISSION_MODE_2K; break;
2122                case T_TransmissionMode_4k: transmission = TRANSMISSION_MODE_4K; break;
2123                case T_TransmissionMode_8k: transmission = TRANSMISSION_MODE_8K; break;
2124                case T_TransmissionMode_Auto: transmission = TRANSMISSION_MODE_AUTO; break;
2125#if defined TRANSMISSION_MODE_1K
2126                case T_TransmissionMode_1k: transmission = TRANSMISSION_MODE_1K; break;
2127                case T_TransmissionMode_16k: transmission = TRANSMISSION_MODE_16K; break;
2128                case T_TransmissionMode_32k: transmission = TRANSMISSION_MODE_32K; break;
2129#endif
2130                default: transmission = TRANSMISSION_MODE_AUTO; break;
2131        }
2132
2133        int guardinterval = tpnode->rolloff; //rolloff = guardinterval on DVBT
2134        switch(guardinterval)
2135        {
2136                case T_GuardInterval_1_32: guardinterval = GUARD_INTERVAL_1_32; break;
2137                case T_GuardInterval_1_16: guardinterval = GUARD_INTERVAL_1_16; break;
2138                case T_GuardInterval_1_8: guardinterval = GUARD_INTERVAL_1_8; break;
2139                case T_GuardInterval_1_4: guardinterval = GUARD_INTERVAL_1_4; break;
2140                case T_GuardInterval_Auto: guardinterval = GUARD_INTERVAL_AUTO; break;
2141#if defined GUARD_INTERVAL_1_128
2142                case T_GuardInterval_1_128: guardinterval = GUARD_INTERVAL_1_128; break;
2143                case T_GuardInterval_19_128: guardinterval = GUARD_INTERVAL_19_128; break;
2144                case T_GuardInterval_19_256: guardinterval = GUARD_INTERVAL_19_256; break;
2145#endif
2146                default: guardinterval = GUARD_INTERVAL_AUTO; break;
2147        }
2148       
2149        int hierarchy = tpnode->system; //system = hierarchy on DVBT
2150        if(tpnode->system == System_DVB_T2) //system = DVB-T2 then hierarchy = HIERARCHY_AUTO
2151                hierarchy = T_Hierarchy_Auto;
2152       
2153        switch(hierarchy)
2154        {
2155                case T_Hierarchy_None: hierarchy = HIERARCHY_NONE;
2156                case T_Hierarchy_1: hierarchy = HIERARCHY_1;
2157                case T_Hierarchy_2: hierarchy = HIERARCHY_2;
2158                case T_Hierarchy_4: hierarchy = HIERARCHY_4;
2159                case T_Hierarchy_Auto: hierarchy = HIERARCHY_AUTO;
2160                default: hierarchy = HIERARCHY_AUTO; break;
2161        }
2162       
2163        int inversion = tpnode->inversion;
2164        switch(inversion)
2165        {
2166                case T_Inversion_Off: inversion = INVERSION_OFF;
2167                case T_Inversion_On: inversion = INVERSION_ON;
2168                default: inversion = INVERSION_AUTO; break;
2169        }
2170
2171        int ret = 0;
2172#ifndef ARM
2173        fediscard(node);
2174#endif
2175
2176#if DVB_API_VERSION >= 5
2177        struct dtv_property p[13];
2178        struct dtv_properties cmdseq;
2179        cmdseq.props = p;
2180
2181// suchlauf geht an nemesis mit system=0
2182        switch(system)
2183        {
2184                case System_DVB_T: system = SYS_DVBT; break; //3
2185                case System_DVB_T2: system = SYS_DVBT2; break; //16
2186                default: system = SYS_DVBT; break;
2187        }
2188
2189        p[0].cmd = DTV_CLEAR;
2190        p[1].cmd = DTV_DELIVERY_SYSTEM, p[1].u.data = system;
2191        p[2].cmd = DTV_FREQUENCY,       p[2].u.data = tpnode->frequency;
2192        p[3].cmd = DTV_INVERSION,       p[3].u.data = (fe_spectral_inversion_t) inversion;
2193        p[4].cmd = DTV_CODE_RATE_LP, p[4].u.data = (fe_code_rate_t) lp;
2194        p[5].cmd = DTV_CODE_RATE_HP, p[5].u.data = (fe_code_rate_t) hp;
2195        p[6].cmd = DTV_MODULATION, p[6].u.data = (fe_modulation_t) modulation;
2196        p[7].cmd = DTV_TRANSMISSION_MODE,       p[7].u.data = (fe_transmit_mode_t) transmission;
2197        p[8].cmd = DTV_GUARD_INTERVAL, p[8].u.data = (fe_guard_interval_t) guardinterval;
2198        p[9].cmd = DTV_HIERARCHY, p[9].u.data = (fe_hierarchy_t) hierarchy;
2199        p[10].cmd = DTV_BANDWIDTH_HZ, p[10].u.data = bandwidth;
2200               
2201        if(system == SYS_DVBT2)
2202        {
2203#if defined DTV_STREAM_ID
2204                p[11].cmd = DTV_STREAM_ID, p[11].u.data = 0;
2205                p[12].cmd = DTV_TUNE;
2206                cmdseq.num = 13;
2207#elif defined DTV_DVBT2_PLP_ID
2208                p[11].cmd = DTV_DVBT2_PLP_ID, p[11].u.data = 0;
2209                p[12].cmd = DTV_TUNE;
2210                cmdseq.num = 13;
2211#else
2212                p[11].cmd = DTV_TUNE;
2213                cmdseq.num = 12;
2214#endif
2215        }
2216        else
2217        {
2218                p[11].cmd = DTV_TUNE;
2219                cmdseq.num = 12;
2220        }
2221       
2222        if((ioctl(node->fd, FE_SET_PROPERTY, &cmdseq)) == -1)
2223        {
2224                perr("FE_SET_PROPERTY");
2225                ret = 1;
2226        }
2227
2228        debug(200, "new dvbapi 1: frequ=%d, inversion=%d, bandwidth=%d, hp=%d, lp=%d, modulation=%d transmission=%d guardinterval=%d hierarchy=%d system=%d (%s)", tpnode->frequency, tpnode->inversion, bandwidth, hp, lp, modulation, transmission, guardinterval, hierarchy, system, node->feshortname);
2229
2230#else
2231        struct dvb_frontend_parameters tuneto;
2232
2233        tuneto.frequency = tpnode->frequency;
2234        tuneto.inversion = tpnode->inversion;
2235        tuneto.u.ofdm.bandwidth = bandwidth;
2236        tuneto.u.ofdm.code_rate_HP = hp;
2237        tuneto.u.ofdm.code_rate_LP = lp;
2238        tuneto.u.ofdm.constellation = modulation;
2239        tuneto.u.ofdm.transmission_mode = transmission;
2240        tuneto.u.ofdm.guard_interval = guardinterval;
2241        tuneto.u.ofdm.hierarchy_information = hierarchy;
2242       
2243        if(ioctl(node->fd, FE_SET_FRONTEND, &tuneto) == -1)
2244        {
2245                perr("FE_SET_FRONTEND");
2246                ret = 1;
2247        }
2248
2249        debug(200, "old dvbapi 1: frequ=%d, inversion=%d, bandwidth=%d, hp=%d, lp=%d, modulation=%d transmission=%d guardinterval=%d hierarchy=%d system=%d (%s)", tpnode->frequency, tpnode->inversion, bandwidth, hp, lp, modulation, transmission, guardinterval, hierarchy, system, node->feshortname);
2250
2251#endif
2252        return ret;
2253}
2254
2255#ifdef SIMULATE
2256int tunercount = 0;
2257#endif
2258struct dvb_frontend_info* fegetinfo(struct dvbdev* node, int fd)
2259{
2260        struct dvb_frontend_info* feinfo = NULL;
2261        int tmpfd = -1;
2262
2263        if(node != NULL)
2264                tmpfd = node->fd;
2265        else
2266                tmpfd = fd;
2267
2268        feinfo = (struct dvb_frontend_info*)malloc(sizeof(struct dvb_frontend_info));
2269        if(feinfo == NULL)
2270        {
2271                err("no mem");
2272                return NULL;
2273        }
2274
2275#ifndef SIMULATE
2276        if(ioctl(tmpfd, FE_GET_INFO, feinfo) < 0)
2277        {
2278                perr("FE_GET_INFO");
2279                free(feinfo);
2280                return NULL;
2281        }
2282#else
2283        tunercount++;
2284        if(tunercount == 1)
2285        {
2286                sprintf(feinfo->name, "%s", "Conax 7500 DVB-C");
2287                feinfo->type = FE_QAM;
2288        }
2289        else
2290        {
2291                sprintf(feinfo->name, "%s", "Conax 7500 DVB-S");
2292                feinfo->type = FE_QPSK;
2293                //feinfo->type = FE_QAM;
2294        }
2295#endif
2296        return feinfo;
2297}
2298
2299#if defined DTV_ENUM_DELSYS
2300int fesetDeliverySystem(fe_delivery_system_t delsys, int fd)
2301{
2302        struct dtv_property p[2];
2303        memset(p, 0, sizeof(p));
2304        struct dtv_properties cmdseq;
2305        cmdseq.props = p;
2306        cmdseq.num = 2;
2307        p[0].cmd = DTV_CLEAR;
2308        p[1].cmd = DTV_DELIVERY_SYSTEM;
2309        p[1].u.data = delsys;
2310        if (ioctl(fd, FE_SET_PROPERTY, &cmdseq) == -1)
2311        {
2312                perr("[DTV_ENUM_DELSYS] FE_SET_PROPERTY failed");
2313                return 0;
2314        }
2315        return 1;
2316}
2317#endif
2318
2319int fegetdev()
2320{
2321        int i, y, fd = -1, count = 0;
2322        char *buf = NULL, *frontenddev = NULL, *fehyprid = NULL, *tmpstr = NULL, *tmpstr1 = NULL;
2323        struct dvb_frontend_info* feinfo = NULL;
2324        struct dvbdev* dvbnode = NULL;
2325
2326        frontenddev = getconfig("frontenddev", NULL);
2327        if(frontenddev == NULL)
2328        {
2329                err("NULL detect");
2330                return count;
2331        }
2332
2333        buf = malloc(MINMALLOC);
2334        if(buf == NULL)
2335        {
2336                err("no memory");
2337                return count;
2338        }
2339
2340#if defined DTV_ENUM_DELSYS     
2341        if(getconfig("tempnim", NULL) == NULL)
2342                addconfig("tempnim", "/tmp/tempnim");
2343        if(file_exist(getconfig("tempnim", NULL)) == 1)
2344                unlink(getconfig("tempnim", NULL));
2345#endif
2346       
2347        for(i = 0; i < MAXDVBADAPTER; i++)
2348        {
2349                for(y = 0; y < MAXFRONTENDDEV; y++)
2350                {
2351                        sprintf(buf, frontenddev, i, y);
2352                        fd = feopen(NULL, buf);
2353                        if(fd >= 0)
2354                        {
2355                               
2356#if defined DTV_ENUM_DELSYS
2357                                struct dtv_property p[1];
2358                                memset(p, 0, sizeof(p));
2359                                p[0].cmd = DTV_ENUM_DELSYS;
2360                                struct dtv_properties cmdseq;
2361                                cmdseq.num = 1;
2362                                cmdseq.props = p;
2363                                int type = -1;
2364                                int mz = -1;
2365                                struct dvb_frontend_info* t_feinfo = NULL;
2366                                       
2367                                //tricky... tempor\E4re nim_sockets bauen da ansonsten viel im code ge\E4ndert werden m\FC\DFte
2368                                free(tmpstr); tmpstr= NULL;
2369                                free(tmpstr1); tmpstr1= NULL;
2370                                tmpstr = ostrcat("NIM Socket " , oitoa(y), 0, 1);
2371                                tmpstr = ostrcat(tmpstr , ":\n", 1, 0);
2372                               
2373                                t_feinfo = (struct dvb_frontend_info*)malloc(sizeof(struct dvb_frontend_info));
2374                                if (ioctl(fd, FE_GET_PROPERTY, &cmdseq) >= 0)
2375                                {
2376                                        for (; p[0].u.buffer.len > 0; p[0].u.buffer.len--)
2377                                        {
2378                                                memset(t_feinfo, 0, sizeof(struct dvb_frontend_info));
2379                                                fe_delivery_system_t delsys = (fe_delivery_system_t)p[0].u.buffer.data[p[0].u.buffer.len - 1];
2380                                                printf("--------> delsys %d devive: %d\n", delsys, y);
2381                                                fesetDeliverySystem(delsys, fd);
2382                                                if (ioctl(fd, FE_GET_INFO, t_feinfo) < 0)
2383                                                {
2384                                                perr("[DTV_ENUM_DELSYS] ioctl FE_GET_INFO failed");
2385                                        }
2386                                        else
2387                                        {
2388                                                printf("------> name %s %d\n", t_feinfo->name, t_feinfo->type);
2389                                                if(type != t_feinfo->type)
2390                                                {
2391                                                        type = t_feinfo->type;
2392                                                        mz = mz + 1;
2393                                                        switch (type)
2394                                                                {
2395                                                                        case FE_QPSK:
2396                                                                        {
2397                                                                                tmpstr = ostrcat(tmpstr , "        Mode ", 1, 0);
2398                                                                                tmpstr = ostrcat(tmpstr , oitoa(feSatellite), 1, 1);
2399                                                                                tmpstr = ostrcat(tmpstr , ": DVB-S/S2\n", 1, 0);
2400                                                                                //tmpstr1 = ostrcat(tmpstr1 , oitoa(feSatellite), 1, 1);
2401                                                                                //tmpstr1 = ostrcat(tmpstr1 , "\n", 1, 0);
2402                                                                                               
2403                                                                                break;
2404                                                                        }
2405                                                                        case FE_QAM:
2406                                                                        {
2407                                                                                tmpstr = ostrcat(tmpstr , "        Mode ", 1, 0);
2408                                                                                tmpstr = ostrcat(tmpstr , oitoa(feCable), 1, 1);
2409                                                                                tmpstr = ostrcat(tmpstr , ": DVB-C\n", 1, 0);
2410                                                                                //tmpstr1 = ostrcat(tmpstr1 , oitoa(feCable), 1, 1);
2411                                                                                //tmpstr1 = ostrcat(tmpstr1 , "\n", 1, 0);
2412                                                                                break;
2413                                                                        }
2414                                                                        case FE_OFDM:
2415                                                                        {
2416                                                                                tmpstr = ostrcat(tmpstr , "        Mode ", 1, 0);
2417                                                                                tmpstr = ostrcat(tmpstr , oitoa(feTerrestrial), 1, 1);
2418                                                                                tmpstr = ostrcat(tmpstr , ": DVB-T/T2\n", 1, 0);
2419                                                                                //tmpstr1 = ostrcat(tmpstr1 , oitoa(feTerrestrial), 1, 1);
2420                                                                                //tmpstr1 = ostrcat(tmpstr1 , "\n", 1, 0);
2421                                                                                break;
2422                                                                        }
2423                                                        }
2424                                                }
2425                                                }
2426                                        }
2427                                        if(mz > 0)
2428                                        {
2429                                                tmpstr = ostrcat(tmpstr , "        Frontend_Device: ", 1, 0);   
2430                                                tmpstr = ostrcat(tmpstr , oitoa(y), 1, 1);
2431                                                tmpstr = ostrcat(tmpstr , "\n", 1, 0);         
2432                                                tmpstr = ostrcat(tmpstr , "        I2C_Device: x\n", 1, 0);     
2433                                                writesys(getconfig("tempnim", NULL), tmpstr, 2);
2434                                                //fehyprid = ostrcat(tmpstr1 , NULL, 0, 0);     
2435                                        }
2436                                        free(tmpstr); tmpstr = NULL;
2437                                        free(tmpstr1); tmpstr1 = NULL;
2438                                }
2439                                else
2440                                {
2441                                        perr("[DTV_ENUM_DELSYS] ioctl FE_GET_PROPERTY failed");
2442                                }
2443                                free(t_feinfo); t_feinfo = NULL;
2444#endif                         
2445                               
2446                                fehyprid = gethypridtunerchoicesvalue(y);
2447                               
2448                                if(fehyprid != NULL)
2449                                {
2450                                        if(y < 10)
2451                                                tmpstr = ostrcat(tmpstr, "fe_0", 1, 0);
2452                                        else
2453                                                tmpstr = ostrcat(tmpstr, "fe_1", 1, 0);
2454
2455                                        tmpstr = ostrcat(tmpstr, oitoa(y), 1, 1);
2456                                        tmpstr = ostrcat(tmpstr, "_hyprid", 1, 0);
2457                                        tmpstr1 = ostrcat(tmpstr, "_last", 0, 0);
2458                                        addconfig(tmpstr1, "999"); //first set
2459                                        free(tmpstr1), tmpstr1 = NULL;
2460#ifndef MIPSEL
2461                                        if(getconfig(tmpstr, NULL) != NULL)
2462                                                sethypridtuner(y, getconfig(tmpstr, NULL));
2463                                        free(tmpstr), tmpstr = NULL;
2464#endif
2465                                }
2466                               
2467                                feinfo = fegetinfo(NULL, fd);
2468
2469                                if(feinfo != NULL)
2470                                {
2471                                        count++;
2472                                        dvbnode = adddvbdev(buf, i, y, fd, FRONTENDDEV, feinfo, NULL, fehyprid, 0);
2473#ifdef MIPSEL
2474                                        if(fehyprid != NULL && getconfig(tmpstr, NULL) != NULL)
2475                                        {
2476                                                sethypridtunernew(dvbnode, getconfig(tmpstr, NULL));
2477                                                feinfo = fegetinfo(NULL, fd);
2478                                                dvbnode->feinfo = feinfo;
2479                                        }
2480                                        free(tmpstr), tmpstr = NULL;
2481#endif                                 
2482                                        if(dvbnode->feinfo->type == FE_QPSK)
2483                                                fesetvoltage(dvbnode, SEC_VOLTAGE_OFF, 15);
2484                                               
2485                                        if(y < 10)
2486                                                tmpstr = ostrcat(tmpstr, "fe_0", 1, 0);
2487                                        else
2488                                                tmpstr = ostrcat(tmpstr, "fe_1", 1, 0);
2489                                        tmpstr = ostrcat(tmpstr, oitoa(y), 1, 1);
2490                                        tmpstr = ostrcat(tmpstr, "_fbc", 1, 0);
2491                                        if(ostrstr(feinfo->name, "BCM45208") != NULL) //fbc Tuner
2492                                        {
2493                                                if(getconfig(tmpstr, NULL) == NULL)
2494                                                        addconfig(tmpstr, "A");
2495                                        }
2496                                        else
2497                                                addconfig(tmpstr, "");
2498                                        free(tmpstr), tmpstr = NULL;
2499                                }
2500                                if(tmpstr != NULL)
2501                                        free(tmpstr), tmpstr = NULL;
2502                        }
2503                }
2504        }
2505
2506        free(buf);
2507        return count;
2508}
2509
2510int fecreatedummy()
2511{
2512        //create dummy frontend for playback
2513        char *buf = NULL, *frontenddev = NULL;
2514        struct dvbdev* dvbnode = NULL;
2515
2516        frontenddev = getconfig("frontenddev", NULL);
2517        if(frontenddev == NULL)
2518        {
2519                err("NULL detect");
2520                return 1;
2521        }
2522
2523        buf = malloc(MINMALLOC);
2524        if(buf == NULL)
2525        {
2526                err("no memory");
2527                return 1;
2528        }
2529
2530        dvbnode = dmxgetlast(0);
2531        if(dvbnode != NULL)
2532        {
2533                sprintf(buf, frontenddev, 0, dvbnode->devnr);
2534                adddvbdev(buf, 0, dvbnode->devnr, -1, FRONTENDDEVDUMMY, NULL, NULL, NULL, 0);
2535        }
2536
2537        free(buf);
2538        return 0;
2539}
2540
2541int fegetlock(int tunernr)
2542{
2543        struct dvbdev* dvbnode = NULL;
2544        dvbnode = dvbdev;
2545
2546        while(dvbnode != NULL)
2547        {
2548                if(dvbnode->deactive == 1)
2549                {
2550                        dvbnode = dvbnode->next;
2551                        continue;
2552                }
2553                if(dvbnode->type == FRONTENDDEV && dvbnode->devnr == tunernr)
2554                {
2555                        return dvbnode->felock;
2556                }
2557                dvbnode = dvbnode->next;
2558        }
2559        return -1;
2560}
2561
2562#ifdef MIPSEL
2563int fechangetype(struct dvbdev* tuner, char* value)
2564{
2565#if DVB_API_VERSION >= 5       
2566
2567        struct dtv_property p[2];
2568        memset(p, 0, sizeof(p));
2569        struct dtv_properties cmdseq;
2570        cmdseq.props = p;
2571        cmdseq.num = 2;
2572        p[0].cmd = DTV_CLEAR;
2573        p[1].cmd = DTV_DELIVERY_SYSTEM;
2574        p[1].u.data = SYS_UNDEFINED;
2575        char* buf = NULL, *hypridtuner = NULL;
2576        int type = 0;
2577        int ret = 0;
2578        int wasopen = 0;
2579        char* realname = gethypridtunerchoicesvaluename(tuner->devnr, value);
2580        char* tmpstr = NULL;
2581       
2582        if(tuner->fd == -1)
2583        {
2584                if(feopen(tuner, NULL) < 0)
2585                        err("open tuner dev");
2586        }
2587        else
2588                wasopen = 1;
2589       
2590        printf("**** > realname: %s\n", realname);
2591       
2592        if(realname != NULL && ostrstr(realname, "DVB-S") != NULL)
2593                type = feSatellite;
2594        else if(realname != NULL && ostrstr(realname, "DVB-C") != NULL)
2595                type = feCable;
2596        else if(realname != NULL && ostrstr(realname, "DVB-T") != NULL)
2597                type = feTerrestrial;
2598        else
2599                type = -1;
2600       
2601        switch (type)
2602        {
2603                case feSatellite:
2604                {
2605                        p[1].u.data = SYS_DVBS;
2606                        break;
2607                }
2608                case feTerrestrial:
2609                {
2610                        p[1].u.data = SYS_DVBT;
2611                        break;
2612                }
2613                case feCable:
2614                {
2615                         fesetvoltage(tuner, SEC_VOLTAGE_OFF, 10);
2616#ifdef SYS_DVBC_ANNEX_A
2617                        p[1].u.data = SYS_DVBC_ANNEX_A;
2618#else
2619                        p[1].u.data = SYS_DVBC_ANNEX_AC;
2620#endif
2621                        break;
2622                }
2623#ifdef feATSC
2624                case feATSC:
2625                {
2626                        p[1].u.data = SYS_ATSC;
2627                        break;
2628                }
2629#endif
2630                default:
2631                        debug(200, "not supported delivery system type %i", type);
2632                        return 0; //false
2633        }
2634        debug(200, "data %d",p[1].u.data );
2635        ret = 0;
2636        if (ioctl(tuner->fd, FE_SET_PROPERTY, &cmdseq) == -1)
2637        {
2638                perr("FE_SET_PROPERTY");
2639                err("FE_SET_PROPERTY failed -> use procfs to switch delivery system tuner %d mode %s type %d",tuner->devnr ,value, type);
2640                ret = -1;
2641
2642                hypridtuner = getconfig("hypridtuner", NULL);
2643                if(hypridtuner != NULL)
2644                {
2645                        buf = malloc(MINMALLOC);
2646                        if(buf == NULL)
2647                        {
2648                                err("no memory");
2649                                return 0;
2650                        }
2651                }
2652                sprintf(buf, hypridtuner, tuner->devnr);
2653                if(file_exist(buf))
2654                {
2655                        if(tuner->fd > -1)
2656                        {
2657                                feclose(tuner, -1);
2658                                ret = writesys(buf, value, 0);
2659                                tuner->fd = feopen(tuner, NULL);
2660                        }
2661                        else
2662                                ret = writesys(buf, value, 0);
2663                        printf("set %s to %s RC:%i\n", buf, value, ret);
2664                }
2665                else
2666                        printf("set system tuner to %s ... file not found -> %s\n", value, buf);
2667                free(buf); buf = NULL;
2668        }
2669        else
2670        {
2671                printf("fe set property value %s data %d ... RC:%i\n", value, p[1].u.data, ret);
2672        }
2673       
2674        if(type == feTerrestrial)
2675        {
2676                tmpstr = ostrcat(tuner->feshortname, "_terr_volt", 0, 0);
2677                if(getconfigint(tmpstr, NULL) == 1)
2678                        fesetvoltage(tuner, SEC_VOLTAGE_13, 10);
2679                else
2680                        fesetvoltage(tuner, SEC_VOLTAGE_OFF, 10);
2681                free(tmpstr); tmpstr = NULL;
2682        }
2683
2684        if(wasopen != 1)
2685                feclose(tuner, -1);
2686        if(ret > -1)
2687                return 1; //true
2688        else
2689                return 0; //false
2690
2691#else //if DVB_API_VERSION < 5
2692        printf("Hinweis -> DVB API kleiner Version 5\n");
2693        if(sethypridtuner(tuner, value) == 0)
2694                return 1;
2695        return 0; //false
2696#endif
2697}               
2698#endif
2699       
2700
2701#endif
Note: See TracBrowser for help on using the repository browser.