source: titan/titan/frontenddev.h @ 43109

Last change on this file since 43109 was 43109, checked in by gost, 5 years ago

fix

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