source: titan/plugins/readerconfig/readerconfig.h @ 33077

Last change on this file since 33077 was 33077, checked in by obi, 8 years ago

REVERT READERCONFIG_H

File size: 11.9 KB
Line 
1#ifndef READERCONFIG_H
2#define READERCONFIG_H
3
4struct oscam
5{
6        char* enable;
7        char* label;
8        char* device;
9        char* rest;
10        struct oscam* next;
11};
12struct oscam* oscam = NULL;
13
14struct oscam* addoscam(char *enable, char* label, char* device, char* rest, struct oscam* last)
15{
16        struct oscam *newnode = NULL, *prev = NULL, *node = oscam;
17
18        newnode = (struct oscam*)calloc(1, sizeof(struct oscam));
19        if(newnode == NULL)
20        {
21                err("no memory");
22                return NULL;
23        }
24
25        if(last != NULL)
26        {
27                last->enable = ostrcat(enable, NULL, 0, 0);
28                last->label = ostrcat(label, NULL, 0, 0);
29                last->device = ostrcat(device, NULL, 0, 0);
30        }
31        newnode->rest = ostrcat(rest, NULL, 0, 0);
32
33        if(last == NULL)
34        {
35                while(node != NULL)
36                {
37                        prev = node;
38                        node = node->next;
39                }
40        }
41        else
42        {
43                prev = last;
44                node = last->next;
45        }
46
47        if(prev == NULL)
48                oscam = newnode;
49        else
50                prev->next = newnode;
51
52        newnode->next = node;
53
54        return newnode;
55}
56
57int readoscam(const char* filename)
58{
59        debug(1000, "in");
60        FILE *fd = NULL;
61        char *fileline = NULL;
62        char *enable = NULL, *label = NULL, *device = NULL, *rest = NULL;
63        int linecount = 0, len = 0;
64        struct oscam* last = NULL;
65
66        fileline = malloc(MINMALLOC);
67        if(fileline == NULL)
68        {
69                err("no memory");
70                return 1;
71        }
72
73        fd = fopen(filename, "r");
74        if(fd == NULL)
75        {
76                perr("can't open %s", filename);
77                free(fileline);
78                return 1;
79        }
80
81        while(fgets(fileline, MINMALLOC, fd) != NULL)
82        {
83                len = strlen(fileline) - 1;
84                if(len >= 0 && fileline[len] == '\n')
85                        fileline[len] = '\0';
86                len--;
87                if(len >= 0 && fileline[len] == '\r')
88                        fileline[len] = '\0';
89
90                linecount++;
91
92                fileline = strstrip(fileline);
93
94                if(ostrstrcase(fileline, "enable") == fileline)
95                {
96                        char* pos = strchr(fileline, '=');
97                        if(pos != NULL)
98                        {
99                                pos++;
100                                free(enable); enable = NULL;
101                                enable = ostrcat(strstrip(pos), NULL, 0, 0);
102                        }
103                }
104                else if(ostrstrcase(fileline, "Label") == fileline)
105                {
106                        char* pos = strchr(fileline, '=');
107                        if(pos != NULL)
108                        {
109                                pos++;
110                                free(label); label = NULL;
111                                label = ostrcat(strstrip(pos), NULL, 0, 0);
112                        }
113                }
114                else if(ostrstrcase(fileline, "device") == fileline)
115                {
116                        char* pos = strchr(fileline, '=');
117                        if(pos != NULL)
118                        {
119                                pos++;
120                                free(device); device = NULL;
121                                device = ostrcat(strstrip(pos), NULL, 0, 0);
122                        }
123                }
124                else
125                {
126                        rest = ostrcat(rest, fileline, 1, 0);
127                        rest = ostrcat(rest, "\n", 1, 0);
128                }
129
130                if(ostrstrcase(fileline, "[reader]") == fileline)
131                {
132                        //add to struct
133                        last = addoscam(enable, label, device, rest, last);
134
135                        free(enable); enable = NULL;
136                        free(label); label = NULL;
137                        free(device); device = NULL;
138                        free(rest); rest = NULL;
139                }
140        }
141
142        //add to struct
143        last = addoscam(enable, label, device, rest, last);
144
145        free(enable); enable = NULL;
146        free(label); label = NULL;
147        free(device); device = NULL;
148        free(rest); rest = NULL;
149
150        free(fileline);
151        fclose(fd);
152        return 0;
153}
154
155int deloscam(struct oscam* onode)
156{
157        int ret = 1;
158        struct oscam *node = oscam, *prev = oscam;
159
160        while(node != NULL)
161        {
162                if(onode == node)
163                {
164                        ret = 0;
165                        if(node == oscam)
166                                oscam = node->next;
167                        else
168                                prev->next = node->next;
169
170                        free(node->enable);
171                        node->enable = NULL;
172
173                        free(node->label);
174                        node->label = NULL;
175
176                        free(node->device);
177                        node->device = NULL;
178
179                        free(node->rest);
180                        node->rest = NULL;
181
182                        free(node);
183                        node = NULL;
184
185                        break;
186                }
187
188                prev = node;
189                node = node->next;
190        }
191
192        return ret;
193}
194
195void freeoscam()
196{
197        struct oscam *node = oscam, *prev = oscam;
198
199        while(node != NULL)
200        {
201                prev = node;
202                node = node->next;
203                if(prev != NULL)
204                        deloscam(prev);
205        }
206}
207
208int writeoscam(const char *filename)
209{
210        debug(1000, "in");
211        FILE *fd = NULL;
212        struct oscam *node = oscam;
213        int ret1 = 0, ret2 = 0, ret3 = 0, ret4 = 0;
214
215        fd = fopen(filename, "w");
216        if(fd == NULL)
217        {
218                perr("can't open %s", filename);
219                return 1;
220        }
221
222        while(node != NULL)
223        {
224                if(node->rest != NULL) ret1 = fprintf(fd, "%s", node->rest);
225                if(node->enable != NULL) ret2 = fprintf(fd, "enable = %s\n", node->enable);
226                if(node->label != NULL) ret3 = fprintf(fd, "Label = %s\n", node->label);
227                if(node->device != NULL) ret4 = fprintf(fd, "device = %s\n", node->device);
228                if(ret1 < 0 || ret2 < 0 || ret3 < 0 || ret4 < 0)
229                {
230                        perr("writting file %s", filename);
231                }
232                node = node->next;
233        }
234
235        fclose(fd);
236        return 0;
237}
238
239void screenoscamconfig(struct oscam* node, char* file)
240{
241        int rcret = -1;
242        struct skin* oscamconfig = getscreen("readerconfig");
243        struct skin* listbox = getscreennode(oscamconfig, "listbox");
244        struct skin* enable = getscreennode(oscamconfig, "enable");
245        struct skin* device = getscreennode(oscamconfig, "device");
246        struct skin* menutitle = getscreennode(oscamconfig, "menutitle");
247        struct skin* tmp = NULL;
248
249        if(node == NULL) return;
250
251        listbox->aktline = 1;
252        listbox->aktpage = -1;
253
254        addchoicebox(enable, "0", _("no"));
255        addchoicebox(enable, "1", _("yes"));
256        setchoiceboxselection(enable, node->enable);
257
258        if(checkbox("ATEMIO-NEMESIS") == 1)
259        {
260                addchoicebox(device, "/dev/sci1", _("first slot"));
261                addchoicebox(device, "/dev/sci0", _("second slot"));
262        }
263        else
264        {
265                addchoicebox(device, "/dev/sci0", _("first slot"));
266                addchoicebox(device, "/dev/sci1", _("second slot"));
267        }
268        setchoiceboxselection(device, node->device);
269
270        if(node->device == NULL || (ostrcmp(node->device, "/dev/sci0") != 0 && ostrcmp(node->device, "/dev/sci1") != 0))
271                device->hidden = YES;
272        else
273                device->hidden = NO;
274
275        char* tmpstr = NULL;
276        tmpstr = ostrcat(_("Reader Configuration"), " : ", 0, 0);
277        tmpstr = ostrcat(tmpstr, file, 1, 0);
278        changetext(menutitle, tmpstr);
279        free(tmpstr), tmpstr = NULL;
280
281        drawscreen(oscamconfig, 0, 0);
282        addscreenrc(oscamconfig, listbox);
283               
284        tmp = listbox->select;
285        while(1)
286        {
287                addscreenrc(oscamconfig, tmp);
288                rcret = waitrc(oscamconfig, 0, 0);
289                tmp = listbox->select;
290
291                if(rcret == getrcconfigint("rcexit", NULL)) break;
292                if(rcret == getrcconfigint("rcok", NULL))
293                {
294                        free(node->enable);
295                        node->enable = ostrcat(enable->ret, NULL, 0, 0);
296
297                        if(node->device != NULL && (ostrcmp(node->device, "/dev/sci0") == 0 || ostrcmp(node->device, "/dev/sci1") == 0))
298                        {
299                                free(node->device);
300                                node->device = ostrcat(device->ret, NULL, 0, 0);
301                        }
302                        break;
303                }
304        }
305
306        delownerrc(oscamconfig);
307        clearscreen(oscamconfig);
308}
309
310char* getoscamconfig()
311{
312        int count = 0;
313        char* tmpstr = NULL;
314        struct menulist* mlist = NULL, *mbox = NULL, *tmpmlist = NULL;
315       
316        if(file_exist("/mnt/swapextensions/keys/oscam.server"))
317        {
318                tmpmlist = addmenulist(&mlist, _("Use config from Flash (permanent)"), NULL, NULL, 0, 0);
319                changemenulistparam(tmpmlist, "/mnt/swapextensions", NULL, NULL, NULL);
320                free(tmpstr); tmpstr = NULL;
321                tmpstr = ostrcat("/mnt/swapextensions", NULL, 0, 0);
322                count++;
323        }
324       
325        if(file_exist("/var/keys/oscam.server"))
326        {
327                tmpmlist = addmenulist(&mlist, _("Use config from Flash (temporary)"), NULL, NULL, 0, 0);
328                changemenulistparam(tmpmlist, "/var", NULL, NULL, NULL);
329                free(tmpstr); tmpstr = NULL;
330                tmpstr = ostrcat("/var", NULL, 0, 0);
331                count++;
332        }
333       
334        if(file_exist("/var/swap/keys/oscam.server"))
335        {
336                tmpmlist = addmenulist(&mlist, _("Use config from Stick or HDD"), NULL, NULL, 0, 0);
337                changemenulistparam(tmpmlist, "/var/swap", NULL, NULL, NULL);
338                free(tmpstr); tmpstr = NULL;
339                tmpstr = ostrcat("/var/swap", NULL, 0, 0);
340                count++;
341        }
342       
343        if(count > 1)
344        {
345                free(tmpstr); tmpstr = NULL;
346                mbox = menulistbox(mlist, NULL, _("Reader Configuration"), _("Choose your Config File from the following list"), NULL, NULL, 0, 0);
347                if(mbox != NULL)
348                        tmpstr = ostrcat(mbox->param, NULL, 0, 0);
349        }
350        else
351                textbox(_("Message"), _("No config file found."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0);
352       
353        freemenulist(mlist, 0); mlist = NULL;
354        return tmpstr;
355}
356
357void screenoscam(char* cfgfile)
358{
359        int rcret = -1;
360        struct skin* skinoscam = getscreen("reader");
361        struct skin* listbox = getscreennode(skinoscam, "listbox");
362        struct skin* menutitle = getscreennode(skinoscam, "menutitle");
363        struct skin* b2 = getscreennode(skinoscam, "b2");
364
365        struct skin* tmp = NULL;
366        struct oscam* node = NULL;
367        char* tmpstr = NULL, *file = NULL, *cmd = NULL, *dvbapi = NULL;
368
369        if(cfgfile == NULL)
370        {
371                tmpstr = getoscamconfig();     
372                if(tmpstr == NULL) return;
373                dvbapi = ostrcat(tmpstr, "/keys/oscam.dvbapi", 0, 0);
374                file = ostrcat(tmpstr, "/keys/oscam.server", 0, 0);
375                free(tmpstr), tmpstr = NULL;
376        }
377        else
378        {
379                cmd = ostrcat("/sbin/emu.sh keydir ", cfgfile, 0, 0);
380                tmpstr = string_newline(command(cmd));
381                dvbapi = ostrcat(tmpstr, "/oscam.dvbapi", 0, 0);
382                file = ostrcat(tmpstr, "/oscam.server", 0, 0);
383                free(cmd), cmd = NULL;
384                free(tmpstr), tmpstr = NULL;
385        }
386       
387        readoscam(file);
388        listbox->aktline = 1;
389        listbox->aktpage = -1;
390
391        tmpstr = ostrcat(_("Reader Selection"), " : ", 0, 0);
392        tmpstr = ostrcat(tmpstr, file, 1, 0);
393        changetext(menutitle, tmpstr);
394        free(tmpstr), tmpstr = NULL;
395
396        if(file_exist(dvbapi))
397                changetext(b2, _("Dvbapi (enabled)"));
398        else
399                changetext(b2, _("Dvbapi (disabled)"));
400
401start:
402
403        tmp = NULL;
404        node = oscam;
405        delmarkedscreennodes(skinoscam, 1);
406        delownerrc(skinoscam);
407
408        while(node != NULL)
409        {
410                if(node->label == NULL || node->enable == NULL)
411                {
412                        node = node->next;
413                        continue;
414                }
415                tmp = addlistbox(skinoscam, listbox, tmp, 1);
416                if(tmp != NULL)
417                {
418                        tmpstr = ostrcat(node->label, NULL, 0, 0);
419                        tmpstr = ostrcat(tmpstr, " -> ", 1, 0);
420                        if(ostrcmp(node->enable, "0") == 0)
421                                tmpstr = ostrcat(tmpstr, _("off"), 1, 0);
422                        else
423                        {
424                                tmpstr = ostrcat(tmpstr, _("on"), 1, 0);
425                                tmpstr = ostrcat(tmpstr, " (", 1, 0);
426                                tmpstr = ostrcat(tmpstr, _("Reader active"), 1, 0);
427                                tmpstr = ostrcat(tmpstr, ")", 1, 0);
428                        }
429
430                        changetext(tmp, tmpstr);
431                        free(tmpstr); tmpstr = NULL;
432                        tmp->del = 1;
433                        tmp->handle = (char*)node;
434                }
435                node = node->next;
436        }
437
438        drawscreen(skinoscam, 0, 0);
439        addscreenrc(skinoscam, listbox);
440
441        while(1)
442        {
443                rcret = waitrc(skinoscam, 0, 0);
444
445                if(rcret == getrcconfigint("rcexit", NULL)) break;
446                if(rcret == getrcconfigint("rcok", NULL))
447                {
448                        //write oscam
449                        if(writeoscam(file) == 0)
450                        {
451                                textbox(_("Message"), _("Oscam config written to medium !"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
452                                if(textbox(_("Message"), _("Restart Oscam ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 5, 0) == 1)
453                                {
454                                        startinternreader(0);
455                                        char* cmd = NULL;
456                                        cmd = ostrcat("emu.sh restart" , NULL, 0, 0);
457                                        system(cmd);
458                                        free(cmd);
459                                }
460                                drawscreen(skinoscam, 0, 0);
461                                break;
462                        }
463                }
464                if(rcret == getrcconfigint("rcred", NULL))
465                {
466                        if(listbox->select != NULL && listbox->select->handle != NULL)
467                        {
468                                screenoscamconfig((struct oscam*)listbox->select->handle, file);
469                                goto start;
470                        }
471                }
472                if(rcret == getrcconfigint("rcgreen", NULL))
473                {
474                        int ret = 1;
475                       
476                        tmpstr = ostrcat(dvbapi, ".disable", 0, 0);
477                        if(tmpstr != NULL)
478                        {
479                                if(file_exist(dvbapi) == 1)
480                                {
481                                        ret = rename(dvbapi, tmpstr);
482                                        free(tmpstr); tmpstr = NULL;
483                                        tmpstr = ostrcat(tmpstr, _("Oscam dvbapi config disabled !"), 0, 0);
484                                        changetext(b2, _("Dvbapi (disabled)"));
485                                }
486                                else if(file_exist(tmpstr) == 1)
487                                {
488                                        ret = rename(tmpstr, dvbapi);
489                                        free(tmpstr); tmpstr = NULL;
490                                        tmpstr = ostrcat(tmpstr, _("Oscam dvbapi config enabled !"), 0, 0);
491                                        changetext(b2, _("Dvbapi (enabled)"));
492                                }
493                        }
494                       
495                        if(ret == 0)
496                        {
497                                textbox(_("Message"), tmpstr, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
498                                if(textbox(_("Message"), _("Restart Oscam ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 5, 0) == 1)
499                                {
500                                        startinternreader(0);
501                                        char* cmd = NULL;
502                                        cmd = ostrcat("emu.sh restart" , NULL, 0, 0);
503                                        system(cmd);
504                                        free(cmd);
505                                }                       
506                        }
507                        free(tmpstr); tmpstr = NULL;
508                        drawscreen(skinoscam, 0, 0);   
509                }
510        }
511
512        startinternreader(1);
513
514        free(dvbapi); dvbapi = NULL;
515        free(file); file = NULL;
516        delmarkedscreennodes(skinoscam, 1);
517        freeoscam();
518        delownerrc(skinoscam);
519        clearscreen(skinoscam);
520}
521
522#endif
Note: See TracBrowser for help on using the repository browser.