Mercurial > pt1
comparison arib25/src/td.c @ 53:41a5481db56f
rearrange arib25 directory
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Sun, 13 Sep 2009 02:11:33 +0900 |
parents | arib25v023/arib25/src/td.c@6801fe7e04ff |
children | 080414aa8f21 |
comparison
equal
deleted
inserted
replaced
52:db7af62da709 | 53:41a5481db56f |
---|---|
1 #include <stdlib.h> | |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 | |
5 #include <fcntl.h> | |
6 #include <sys/stat.h> | |
7 #include <sys/types.h> | |
8 | |
9 #if defined(WIN32) | |
10 #include <io.h> | |
11 #include <windows.h> | |
12 #include <crtdbg.h> | |
13 #else | |
14 #define __STDC_FORMAT_MACROS | |
15 #include <inttypes.h> | |
16 #include <unistd.h> | |
17 #endif | |
18 | |
19 #include "arib_std_b25.h" | |
20 #include "b_cas_card.h" | |
21 | |
22 typedef struct { | |
23 int32_t round; | |
24 int32_t strip; | |
25 int32_t emm; | |
26 int32_t verbose; | |
27 int32_t power_ctrl; | |
28 } OPTION; | |
29 | |
30 static void show_usage(); | |
31 static int parse_arg(OPTION *dst, int argc, char **argv); | |
32 static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt); | |
33 static void show_bcas_power_on_control_info(B_CAS_CARD *bcas); | |
34 | |
35 int main(int argc, char **argv) | |
36 { | |
37 int n; | |
38 OPTION opt; | |
39 | |
40 #if defined(WIN32) | |
41 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); | |
42 _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); | |
43 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); | |
44 _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT ); | |
45 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE ); | |
46 _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT ); | |
47 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_DELAY_FREE_MEM_DF|_CRTDBG_CHECK_ALWAYS_DF|_CRTDBG_LEAK_CHECK_DF); | |
48 #endif | |
49 | |
50 n = parse_arg(&opt, argc, argv); | |
51 if(n+2 > argc){ | |
52 show_usage(); | |
53 exit(EXIT_FAILURE); | |
54 } | |
55 | |
56 test_arib_std_b25(argv[n+0], argv[n+1], &opt); | |
57 | |
58 #if defined(WIN32) | |
59 _CrtDumpMemoryLeaks(); | |
60 #endif | |
61 | |
62 return EXIT_SUCCESS; | |
63 } | |
64 | |
65 static void show_usage() | |
66 { | |
67 fprintf(stderr, "b25 - ARIB STD-B25 test program ver. 0.2.3 (2008, 12/30)\n"); | |
68 fprintf(stderr, "usage: b25 [options] src.m2t dst.m2t\n"); | |
69 fprintf(stderr, "options:\n"); | |
70 fprintf(stderr, " -r round (integer, default=4)\n"); | |
71 fprintf(stderr, " -s strip\n"); | |
72 fprintf(stderr, " 0: keep null(padding) stream (default)\n"); | |
73 fprintf(stderr, " 1: strip null stream\n"); | |
74 fprintf(stderr, " -m EMM\n"); | |
75 fprintf(stderr, " 0: ignore EMM (default)\n"); | |
76 fprintf(stderr, " 1: send EMM to B-CAS card\n"); | |
77 fprintf(stderr, " -p power_on_control_info\n"); | |
78 fprintf(stderr, " 0: do nothing additionaly\n"); | |
79 fprintf(stderr, " 1: show B-CAS EMM receiving request (default)\n"); | |
80 fprintf(stderr, " -v verbose\n"); | |
81 fprintf(stderr, " 0: silent\n"); | |
82 fprintf(stderr, " 1: show processing status (default)\n"); | |
83 fprintf(stderr, "\n"); | |
84 } | |
85 | |
86 static int parse_arg(OPTION *dst, int argc, char **argv) | |
87 { | |
88 int i; | |
89 | |
90 dst->round = 4; | |
91 dst->strip = 0; | |
92 dst->emm = 0; | |
93 dst->power_ctrl = 1; | |
94 dst->verbose = 1; | |
95 | |
96 for(i=1;i<argc;i++){ | |
97 if(argv[i][0] != '-'){ | |
98 break; | |
99 } | |
100 switch(argv[i][1]){ | |
101 case 'm': | |
102 if(argv[i][2]){ | |
103 dst->emm = atoi(argv[i]+2); | |
104 }else{ | |
105 dst->emm = atoi(argv[i+1]); | |
106 i += 1; | |
107 } | |
108 break; | |
109 case 'p': | |
110 if(argv[i][2]){ | |
111 dst->power_ctrl = atoi(argv[i]+2); | |
112 }else{ | |
113 dst->power_ctrl = atoi(argv[i+1]); | |
114 i += 1; | |
115 } | |
116 break; | |
117 case 'r': | |
118 if(argv[i][2]){ | |
119 dst->round = atoi(argv[i]+2); | |
120 }else{ | |
121 dst->round = atoi(argv[i+1]); | |
122 i += 1; | |
123 } | |
124 break; | |
125 case 's': | |
126 if(argv[i][2]){ | |
127 dst->strip = atoi(argv[i]+2); | |
128 }else{ | |
129 dst->strip = atoi(argv[i+1]); | |
130 i += 1; | |
131 } | |
132 break; | |
133 case 'v': | |
134 if(argv[i][2]){ | |
135 dst->verbose = atoi(argv[i]+2); | |
136 }else{ | |
137 dst->verbose = atoi(argv[i+1]); | |
138 i += 1; | |
139 } | |
140 break; | |
141 default: | |
142 fprintf(stderr, "error - unknown option '-%c'\n", argv[i][1]); | |
143 return argc; | |
144 } | |
145 } | |
146 | |
147 return i; | |
148 } | |
149 | |
150 static void test_arib_std_b25(const char *src, const char *dst, OPTION *opt) | |
151 { | |
152 int code,i,n,m; | |
153 int sfd,dfd; | |
154 | |
155 int64_t total; | |
156 int64_t offset; | |
157 | |
158 ARIB_STD_B25 *b25; | |
159 B_CAS_CARD *bcas; | |
160 | |
161 ARIB_STD_B25_PROGRAM_INFO pgrm; | |
162 | |
163 uint8_t data[8*1024]; | |
164 | |
165 ARIB_STD_B25_BUFFER sbuf; | |
166 ARIB_STD_B25_BUFFER dbuf; | |
167 | |
168 sfd = -1; | |
169 dfd = -1; | |
170 b25 = NULL; | |
171 bcas = NULL; | |
172 | |
173 sfd = _open(src, _O_BINARY|_O_RDONLY|_O_SEQUENTIAL); | |
174 if(sfd < 0){ | |
175 fprintf(stderr, "error - failed on _open(%s) [src]\n", src); | |
176 goto LAST; | |
177 } | |
178 | |
179 _lseeki64(sfd, 0, SEEK_END); | |
180 total = _telli64(sfd); | |
181 _lseeki64(sfd, 0, SEEK_SET); | |
182 | |
183 b25 = create_arib_std_b25(); | |
184 if(b25 == NULL){ | |
185 fprintf(stderr, "error - failed on create_arib_std_b25()\n"); | |
186 goto LAST; | |
187 } | |
188 | |
189 code = b25->set_multi2_round(b25, opt->round); | |
190 if(code < 0){ | |
191 fprintf(stderr, "error - failed on ARIB_STD_B25::set_multi2_round() : code=%d\n", code); | |
192 goto LAST; | |
193 } | |
194 | |
195 code = b25->set_strip(b25, opt->strip); | |
196 if(code < 0){ | |
197 fprintf(stderr, "error - failed on ARIB_STD_B25::set_strip() : code=%d\n", code); | |
198 goto LAST; | |
199 } | |
200 | |
201 code = b25->set_emm_proc(b25, opt->emm); | |
202 if(code < 0){ | |
203 fprintf(stderr, "error - failed on ARIB_STD_B25::set_emm_proc() : code=%d\n", code); | |
204 goto LAST; | |
205 } | |
206 | |
207 bcas = create_b_cas_card(); | |
208 if(bcas == NULL){ | |
209 fprintf(stderr, "error - failed on create_b_cas_card()\n"); | |
210 goto LAST; | |
211 } | |
212 | |
213 code = bcas->init(bcas); | |
214 if(code < 0){ | |
215 fprintf(stderr, "error - failed on B_CAS_CARD::init() : code=%d\n", code); | |
216 goto LAST; | |
217 } | |
218 | |
219 code = b25->set_b_cas_card(b25, bcas); | |
220 if(code < 0){ | |
221 fprintf(stderr, "error - failed on ARIB_STD_B25::set_b_cas_card() : code=%d\n", code); | |
222 goto LAST; | |
223 } | |
224 | |
225 dfd = _open(dst, _O_BINARY|_O_WRONLY|_O_SEQUENTIAL|_O_CREAT|_O_TRUNC, _S_IREAD|_S_IWRITE); | |
226 if(dfd < 0){ | |
227 fprintf(stderr, "error - failed on _open(%s) [dst]\n", dst); | |
228 goto LAST; | |
229 } | |
230 | |
231 offset = 0; | |
232 while( (n = _read(sfd, data, sizeof(data))) > 0 ){ | |
233 sbuf.data = data; | |
234 sbuf.size = n; | |
235 | |
236 code = b25->put(b25, &sbuf); | |
237 if(code < 0){ | |
238 fprintf(stderr, "error - failed on ARIB_STD_B25::put() : code=%d\n", code); | |
239 goto LAST; | |
240 } | |
241 | |
242 code = b25->get(b25, &dbuf); | |
243 if(code < 0){ | |
244 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code); | |
245 goto LAST; | |
246 } | |
247 | |
248 if(dbuf.size > 0){ | |
249 n = _write(dfd, dbuf.data, dbuf.size); | |
250 if(n != dbuf.size){ | |
251 fprintf(stderr, "error failed on _write(%d)\n", dbuf.size); | |
252 goto LAST; | |
253 } | |
254 } | |
255 | |
256 offset += sbuf.size; | |
257 if(opt->verbose != 0){ | |
258 m = (int)(10000*offset/total); | |
259 fprintf(stderr, "\rprocessing: %2d.%02d%% ", m/100, m%100); | |
260 } | |
261 } | |
262 | |
263 code = b25->flush(b25); | |
264 if(code < 0){ | |
265 fprintf(stderr, "error - failed on ARIB_STD_B25::flush() : code=%d\n", code); | |
266 goto LAST; | |
267 } | |
268 | |
269 code = b25->get(b25, &dbuf); | |
270 if(code < 0){ | |
271 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code); | |
272 goto LAST; | |
273 } | |
274 | |
275 if(dbuf.size > 0){ | |
276 n = _write(dfd, dbuf.data, dbuf.size); | |
277 if(n != dbuf.size){ | |
278 fprintf(stderr, "error - failed on _write(%d)\n", dbuf.size); | |
279 goto LAST; | |
280 } | |
281 } | |
282 | |
283 if(opt->verbose != 0){ | |
284 fprintf(stderr, "\rprocessing: finish \n"); | |
285 fflush(stderr); | |
286 fflush(stdout); | |
287 } | |
288 | |
289 n = b25->get_program_count(b25); | |
290 if(n < 0){ | |
291 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_count() : code=%d\n", code); | |
292 goto LAST; | |
293 } | |
294 for(i=0;i<n;i++){ | |
295 code = b25->get_program_info(b25, &pgrm, i); | |
296 if(code < 0){ | |
297 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_info(%d) : code=%d\n", i, code); | |
298 goto LAST; | |
299 } | |
300 if(pgrm.ecm_unpurchased_count > 0){ | |
301 fprintf(stderr, "warning - unpurchased ECM is detected\n"); | |
302 fprintf(stderr, " channel: %d\n", pgrm.program_number); | |
303 fprintf(stderr, " unpurchased ECM count: %d\n", pgrm.ecm_unpurchased_count); | |
304 fprintf(stderr, " last ECM error code: %04x\n", pgrm.last_ecm_error_code); | |
305 #if defined(WIN32) | |
306 fprintf(stderr, " undecrypted TS packet: %d\n", pgrm.undecrypted_packet_count); | |
307 fprintf(stderr, " total TS packet: %d\n", pgrm.total_packet_count); | |
308 #else | |
309 fprintf(stderr, " undecrypted TS packet: %"PRId64"\n", pgrm.undecrypted_packet_count); | |
310 fprintf(stderr, " total TS packet: %"PRId64"\n", pgrm.total_packet_count); | |
311 #endif | |
312 } | |
313 } | |
314 | |
315 if(opt->power_ctrl != 0){ | |
316 show_bcas_power_on_control_info(bcas); | |
317 } | |
318 | |
319 LAST: | |
320 | |
321 if(sfd >= 0){ | |
322 _close(sfd); | |
323 sfd = -1; | |
324 } | |
325 | |
326 if(dfd >= 0){ | |
327 _close(dfd); | |
328 dfd = -1; | |
329 } | |
330 | |
331 if(b25 != NULL){ | |
332 b25->release(b25); | |
333 b25 = NULL; | |
334 } | |
335 | |
336 if(bcas != NULL){ | |
337 bcas->release(bcas); | |
338 bcas = NULL; | |
339 } | |
340 } | |
341 | |
342 static void show_bcas_power_on_control_info(B_CAS_CARD *bcas) | |
343 { | |
344 int code; | |
345 int i,w; | |
346 B_CAS_PWR_ON_CTRL_INFO pwc; | |
347 | |
348 code = bcas->get_pwr_on_ctrl(bcas, &pwc); | |
349 if(code < 0){ | |
350 fprintf(stderr, "error - failed on B_CAS_CARD::get_pwr_on_ctrl() : code=%d\n", code); | |
351 return; | |
352 } | |
353 | |
354 if(pwc.count == 0){ | |
355 fprintf(stdout, "no EMM receiving request\n"); | |
356 return; | |
357 } | |
358 | |
359 fprintf(stdout, "total %d EMM receiving request\n", pwc.count); | |
360 for(i=0;i<pwc.count;i++){ | |
361 fprintf(stdout, "+ [%d] : tune ", i); | |
362 switch(pwc.data[i].network_id){ | |
363 case 4: | |
364 w = pwc.data[i].transport_id; | |
365 fprintf(stdout, "BS-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7)); | |
366 break; | |
367 case 6: | |
368 case 7: | |
369 w = pwc.data[i].transport_id; | |
370 fprintf(stdout, "ND-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7)); | |
371 break; | |
372 default: | |
373 fprintf(stdout, "unknown(b:0x%02x,n:0x%04x,t:0x%04x) ", pwc.data[i].broadcaster_group_id, pwc.data[i].network_id, pwc.data[i].transport_id); | |
374 break; | |
375 } | |
376 fprintf(stdout, "between %04d %02d/%02d ", pwc.data[i].s_yy, pwc.data[i].s_mm, pwc.data[i].s_dd); | |
377 fprintf(stdout, "to %04d %02d/%02d ", pwc.data[i].l_yy, pwc.data[i].l_mm, pwc.data[i].l_dd); | |
378 fprintf(stdout, "least %d hours\n", pwc.data[i].hold_time); | |
379 } | |
380 } | |
381 |