-
Notifications
You must be signed in to change notification settings - Fork 16
/
module-dvbapi-chancache.c
212 lines (180 loc) · 4.67 KB
/
module-dvbapi-chancache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#define MODULE_LOG_PREFIX "dvbapi"
#include "globals.h"
#ifdef HAVE_DVBAPI
#include "ncam-config.h"
#include "ncam-ecm.h"
#include "ncam-string.h"
#include "module-dvbapi.h"
#include "module-dvbapi-chancache.h"
extern DEMUXTYPE demux[MAX_DEMUX];
static LLIST *channel_cache;
void dvbapi_save_channel_cache(void)
{
if(boxtype_is("dbox2")) return; // dont save channelcache on these boxes, they lack resources and will crash!
if (USE_OPENXCAS) // Why?
return;
char fname[256];
int32_t result = 0;
get_config_filename(fname, sizeof(fname), "ncam.ccache");
FILE *file = fopen(fname, "w");
if(!file)
{
cs_log("dvbapi channelcache can't write to file %s", fname);
return;
}
LL_ITER it = ll_iter_create(channel_cache);
struct s_channel_cache *c;
while((c = ll_iter_next(&it)))
{
result = fprintf(file, "%04X,%06X,%04X,%04X,%06X\n", c->caid, c->prid, c->srvid, c->pid, c->chid);
if(result < 0)
{
fclose(file);
result = remove(fname);
if(!result)
{
cs_log("error writing cache -> cache file removed!");
}
else
{
cs_log("error writing cache -> cache file could not be removed either!");
}
return;
}
}
fclose(file);
cs_log("dvbapi channelcache saved to %s", fname);
}
void dvbapi_load_channel_cache(void)
{
if(boxtype_is("dbox2")) return; // dont load channelcache on these boxes, they lack resources and will crash!
if (USE_OPENXCAS) // Why?
return;
char fname[256];
char line[1024];
FILE *file;
struct s_channel_cache *c;
get_config_filename(fname, sizeof(fname), "ncam.ccache");
file = fopen(fname, "r");
if(!file)
{
cs_log_dbg(D_TRACE, "dvbapi channelcache can't read from file %s", fname);
return;
}
int32_t i = 1;
int32_t valid = 0;
char *ptr, *saveptr1 = NULL;
char *split[6];
memset(line, 0, sizeof(line));
while(fgets(line, sizeof(line), file))
{
if(!line[0] || line[0] == '#' || line[0] == ';')
{ continue; }
for(i = 0, ptr = strtok_r(line, ",", &saveptr1); ptr && i < 6 ; ptr = strtok_r(NULL, ",", &saveptr1), i++)
{
split[i] = ptr;
}
valid = (i == 5);
if(valid)
{
if(!cs_malloc(&c, sizeof(struct s_channel_cache)))
{ continue; }
c->caid = a2i(split[0], 4);
c->prid = a2i(split[1], 6);
c->srvid = a2i(split[2], 4);
c->pid = a2i(split[3], 4);
c->chid = a2i(split[4], 6);
if(valid && c->caid != 0)
{
if(!channel_cache)
{
channel_cache = ll_create("channel cache");
}
ll_append(channel_cache, c);
}
else
{
NULLFREE(c);
}
}
}
fclose(file);
cs_log("dvbapi channelcache loaded from %s", fname);
}
struct s_channel_cache *dvbapi_find_channel_cache(int32_t demux_id, int32_t pidindex, int8_t caid_and_prid_only)
{
struct s_ecmpid *p = &demux[demux_id].ECMpids[pidindex];
struct s_channel_cache *c;
LL_ITER it;
if(!channel_cache)
{ channel_cache = ll_create("channel cache"); }
it = ll_iter_create(channel_cache);
while((c = ll_iter_next(&it)))
{
if(caid_and_prid_only)
{
if(p->CAID == c->caid && (p->PROVID == c->prid || p->PROVID == 0)) // PROVID ==0 some provider no provid in PMT table
{ return c; }
}
else
{
if(demux[demux_id].program_number == c->srvid
&& p->CAID == c->caid
&& p->ECM_PID == c->pid
&& (p->PROVID == c->prid || p->PROVID == 0)) // PROVID ==0 some provider no provid in PMT table
{
#ifdef WITH_DEBUG
char buf[ECM_FMT_LEN];
ecmfmt(buf, ECM_FMT_LEN, c->caid, 0, c->prid, c->chid, c->pid, c->srvid, 0, 0, 0, 0, 0, 0, NULL, NULL);
cs_log_dbg(D_DVBAPI, "Demuxer %d found in channel cache: %s", demux_id, buf);
#endif
return c;
}
}
}
return NULL;
}
int32_t dvbapi_edit_channel_cache(int32_t demux_id, int32_t pidindex, uint8_t add)
{
struct s_ecmpid *p = &demux[demux_id].ECMpids[pidindex];
struct s_channel_cache *c;
LL_ITER it;
int32_t count = 0;
if(!channel_cache)
{ channel_cache = ll_create("channel cache"); }
it = ll_iter_create(channel_cache);
while((c = ll_iter_next(&it)))
{
if(demux[demux_id].program_number == c->srvid
&& p->CAID == c->caid
&& p->ECM_PID == c->pid
&& (p->PROVID == c->prid || p->PROVID == 0))
{
if(add && p->CHID == c->chid)
{
return 0; //already added
}
ll_iter_remove_data(&it);
count++;
}
}
if(add)
{
if(!cs_malloc(&c, sizeof(struct s_channel_cache)))
{ return count; }
c->srvid = demux[demux_id].program_number;
c->caid = p->CAID;
c->pid = p->ECM_PID;
c->prid = p->PROVID;
c->chid = p->CHID;
ll_append(channel_cache, c);
#ifdef WITH_DEBUG
char buf[ECM_FMT_LEN];
ecmfmt(buf, ECM_FMT_LEN, c->caid, 0, c->prid, c->chid, c->pid, c->srvid, 0, 0, 0, 0, 0, 0, NULL, NULL);
cs_log_dbg(D_DVBAPI, "Demuxer %d added to channel cache: %s", demux_id, buf);
#endif
count++;
}
return count;
}
#endif