comparison arib25v021/arib25/src/td.c @ 0:67e8eca28a80

initial import
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 16 Feb 2009 15:41:49 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:67e8eca28a80
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.1 (2008, 4/9)\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 if(offset == 0x000000006dfd2000){
237 offset = 0x000000006dfd2000;
238 }
239
240 code = b25->put(b25, &sbuf);
241 if(code < 0){
242 fprintf(stderr, "error - failed on ARIB_STD_B25::put() : code=%d\n", code);
243 goto LAST;
244 }
245
246 code = b25->get(b25, &dbuf);
247 if(code < 0){
248 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code);
249 goto LAST;
250 }
251
252 if(dbuf.size > 0){
253 n = _write(dfd, dbuf.data, dbuf.size);
254 if(n != dbuf.size){
255 fprintf(stderr, "error failed on _write(%d)\n", dbuf.size);
256 goto LAST;
257 }
258 }
259
260 offset += sbuf.size;
261 if(opt->verbose != 0){
262 m = (int)(10000*offset/total);
263 fprintf(stderr, "\rprocessing: %2d.%02d%% ", m/100, m%100);
264 }
265 }
266
267 code = b25->flush(b25);
268 if(code < 0){
269 fprintf(stderr, "error - failed on ARIB_STD_B25::flush() : code=%d\n", code);
270 goto LAST;
271 }
272
273 code = b25->get(b25, &dbuf);
274 if(code < 0){
275 fprintf(stderr, "error - failed on ARIB_STD_B25::get() : code=%d\n", code);
276 goto LAST;
277 }
278
279 if(dbuf.size > 0){
280 n = _write(dfd, dbuf.data, dbuf.size);
281 if(n != dbuf.size){
282 fprintf(stderr, "error - failed on _write(%d)\n", dbuf.size);
283 goto LAST;
284 }
285 }
286
287 if(opt->verbose != 0){
288 fprintf(stderr, "\rprocessing: finish \n");
289 fflush(stderr);
290 fflush(stdout);
291 }
292
293 n = b25->get_program_count(b25);
294 if(n < 0){
295 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_count() : code=%d\n", code);
296 goto LAST;
297 }
298 for(i=0;i<n;i++){
299 code = b25->get_program_info(b25, &pgrm, i);
300 if(code < 0){
301 fprintf(stderr, "error - failed on ARIB_STD_B25::get_program_info(%d) : code=%d\n", i, code);
302 goto LAST;
303 }
304 if(pgrm.ecm_unpurchased_count > 0){
305 fprintf(stderr, "warning - unpurchased ECM is detected\n");
306 fprintf(stderr, " channel: %d\n", pgrm.program_number);
307 fprintf(stderr, " unpurchased ECM count: %d\n", pgrm.ecm_unpurchased_count);
308 fprintf(stderr, " last ECM error code: %04x\n", pgrm.last_ecm_error_code);
309 #if defined(WIN32)
310 fprintf(stderr, " undecrypted TS packet: %d\n", pgrm.undecrypted_packet_count);
311 fprintf(stderr, " total TS packet: %d\n", pgrm.total_packet_count);
312 #else
313 fprintf(stderr, " undecrypted TS packet: %"PRId64"\n", pgrm.undecrypted_packet_count);
314 fprintf(stderr, " total TS packet: %"PRId64"\n", pgrm.total_packet_count);
315 #endif
316 }
317 }
318
319 if(opt->power_ctrl != 0){
320 show_bcas_power_on_control_info(bcas);
321 }
322
323 LAST:
324
325 if(sfd >= 0){
326 _close(sfd);
327 sfd = -1;
328 }
329
330 if(dfd >= 0){
331 _close(dfd);
332 dfd = -1;
333 }
334
335 if(b25 != NULL){
336 b25->release(b25);
337 b25 = NULL;
338 }
339
340 if(bcas != NULL){
341 bcas->release(bcas);
342 bcas = NULL;
343 }
344 }
345
346 static void show_bcas_power_on_control_info(B_CAS_CARD *bcas)
347 {
348 int code;
349 int i,w;
350 B_CAS_PWR_ON_CTRL_INFO pwc;
351
352 code = bcas->get_pwr_on_ctrl(bcas, &pwc);
353 if(code < 0){
354 fprintf(stderr, "error - failed on B_CAS_CARD::get_pwr_on_ctrl() : code=%d\n", code);
355 return;
356 }
357
358 if(pwc.count == 0){
359 fprintf(stdout, "no EMM receiving request\n");
360 return;
361 }
362
363 fprintf(stdout, "total %d EMM receiving request\n", pwc.count);
364 for(i=0;i<pwc.count;i++){
365 fprintf(stdout, "+ [%d] : tune ", i);
366 switch(pwc.data[i].network_id){
367 case 4:
368 w = pwc.data[i].transport_id;
369 fprintf(stdout, "BS-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7));
370 break;
371 case 6:
372 case 7:
373 w = pwc.data[i].transport_id;
374 fprintf(stdout, "ND-%d/TS-%d ", ((w >> 4) & 0x1f), (w & 7));
375 break;
376 default:
377 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);
378 break;
379 }
380 fprintf(stdout, "between %04d %02d/%02d ", pwc.data[i].s_yy, pwc.data[i].s_mm, pwc.data[i].s_dd);
381 fprintf(stdout, "to %04d %02d/%02d ", pwc.data[i].l_yy, pwc.data[i].l_mm, pwc.data[i].l_dd);
382 fprintf(stdout, "least %d hours\n", pwc.data[i].hold_time);
383 }
384 }
385