-
Notifications
You must be signed in to change notification settings - Fork 26
/
en50221.c
2502 lines (2140 loc) · 76.9 KB
/
en50221.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* en50221.c : implementation of the transport, session and applications
* layers of EN 50 221
*****************************************************************************
* Copyright (C) 2004-2005, 2010, 2015 VideoLAN
*
* Authors: Christophe Massiot <[email protected]>
* Based on code from libdvbci Copyright (C) 2000 Klaus Schmidinger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "config.h"
#ifdef HAVE_DVB_SUPPORT
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <errno.h>
#include <time.h>
#include <ev.h>
/* DVB Card Drivers */
#include <linux/dvb/version.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/frontend.h>
#include <linux/dvb/ca.h>
#include <bitstream/mpeg/psi.h>
#include <bitstream/dvb/ci.h>
#include <bitstream/dvb/si.h>
#include "dvblast.h"
#include "en50221.h"
#include "comm.h"
#define TAB_APPEND( count, tab, p ) \
if( (count) > 0 ) \
{ \
(tab) = realloc( tab, sizeof( void ** ) * ( (count) + 1 ) ); \
} \
else \
{ \
(tab) = malloc( sizeof( void ** ) ); \
} \
(tab)[count] = (p); \
(count)++
/*****************************************************************************
* Local declarations
*****************************************************************************/
#undef DEBUG_TPDU
#define CAM_INIT_TIMEOUT 15000000 /* 15 s */
#undef HLCI_WAIT_CAM_READY
#define CAPMT_WAIT 100 /* ms */
#define CA_POLL_PERIOD 100000 /* 100 ms */
typedef struct en50221_msg_t
{
uint8_t *p_data;
int i_size;
struct en50221_msg_t *p_next;
} en50221_msg_t;
typedef struct en50221_session_t
{
int i_slot;
int i_resource_id;
void (* pf_handle)( access_t *, int, uint8_t *, int );
void (* pf_close)( access_t *, int );
void (* pf_manage)( access_t *, int );
void *p_sys;
} en50221_session_t;
typedef struct ci_slot_t
{
bool b_active;
bool b_expect_answer;
bool b_has_data;
bool b_mmi_expected;
bool b_mmi_undisplayed;
/* TPDU reception */
en50221_msg_t *p_recv;
/* TPDU emission */
en50221_msg_t *p_send;
en50221_msg_t **pp_send_last;
uint8_t *p;
/* InitSlot timer */
struct ev_timer init_watcher;
/* SPDUSend callback, if p_spdu is not NULL */
/* SessionOpen callback, if not 0 */
int i_pending_session_id;
} ci_slot_t;
int i_ca_handle = 0;
int i_ca_type = -1;
static struct ev_io cam_watcher;
static struct ev_timer slot_watcher;
static int i_nb_slots = 0;
static ci_slot_t p_slots[MAX_CI_SLOTS];
static en50221_session_t p_sessions[MAX_SESSIONS];
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void SessionOpenCb( access_t *p_access, uint8_t i_slot );
static void SPDUHandle( access_t * p_access, uint8_t i_slot,
uint8_t *p_spdu, int i_size );
static void ResourceManagerOpen( access_t * p_access, int i_session_id );
static void ApplicationInformationOpen( access_t * p_access, int i_session_id );
static void ConditionalAccessOpen( access_t * p_access, int i_session_id );
static void DateTimeOpen( access_t * p_access, int i_session_id );
static void ResetSlotCb(struct ev_loop *loop, struct ev_timer *w, int revents);
static void en50221_Read(struct ev_loop *loop, struct ev_io *w, int revents);
static void en50221_Poll(struct ev_loop *loop, struct ev_timer *w, int revents);
static void MMIOpen( access_t * p_access, int i_session_id );
/*****************************************************************************
* Utility functions
*****************************************************************************/
#define SIZE_INDICATOR 0x80
static uint8_t *GetLength( uint8_t *p_data, int *pi_length )
{
*pi_length = *p_data++;
if ( (*pi_length & SIZE_INDICATOR) != 0 )
{
int l = *pi_length & ~SIZE_INDICATOR;
int i;
*pi_length = 0;
for ( i = 0; i < l; i++ )
*pi_length = (*pi_length << 8) | *p_data++;
}
return p_data;
}
static uint8_t *SetLength( uint8_t *p_data, int i_length )
{
uint8_t *p = p_data;
if ( i_length < 128 )
{
*p++ = i_length;
}
else if ( i_length < 256 )
{
*p++ = SIZE_INDICATOR | 0x1;
*p++ = i_length;
}
else if ( i_length < 65536 )
{
*p++ = SIZE_INDICATOR | 0x2;
*p++ = i_length >> 8;
*p++ = i_length & 0xff;
}
else if ( i_length < 16777216 )
{
*p++ = SIZE_INDICATOR | 0x3;
*p++ = i_length >> 16;
*p++ = (i_length >> 8) & 0xff;
*p++ = i_length & 0xff;
}
else
{
*p++ = SIZE_INDICATOR | 0x4;
*p++ = i_length >> 24;
*p++ = (i_length >> 16) & 0xff;
*p++ = (i_length >> 8) & 0xff;
*p++ = i_length & 0xff;
}
return p;
}
/*
* Transport layer
*/
#define MAX_TPDU_SIZE 4096
#define MAX_TPDU_DATA (MAX_TPDU_SIZE - 7)
#define DATA_INDICATOR 0x80
#define T_SB 0x80
#define T_RCV 0x81
#define T_CREATE_TC 0x82
#define T_CTC_REPLY 0x83
#define T_DELETE_TC 0x84
#define T_DTC_REPLY 0x85
#define T_REQUEST_TC 0x86
#define T_NEW_TC 0x87
#define T_TC_ERROR 0x88
#define T_DATA_LAST 0xA0
#define T_DATA_MORE 0xA1
static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
{
#ifdef DEBUG_TPDU
int i;
#define MAX_DUMP 256
fprintf(stderr, "%s ", b_outgoing ? "-->" : "<--");
for ( i = 0; i < i_size && i < MAX_DUMP; i++)
fprintf(stderr, "%02X ", p_data[i]);
fprintf(stderr, "%s\n", i_size >= MAX_DUMP ? "..." : "");
#endif
}
/*****************************************************************************
* TPDUWrite
*****************************************************************************/
static int TPDUWrite( access_t * p_access, uint8_t i_slot )
{
ci_slot_t *p_slot = &p_slots[i_slot];
en50221_msg_t *p_send = p_slot->p_send;
if ( p_slot->b_expect_answer )
msg_Warn( p_access,
"en50221: writing while expecting an answer on slot %u",
i_slot );
if ( p_send == NULL )
{
msg_Warn( p_access, "en50221: no data to write on slot %u !", i_slot );
return -1;
}
p_slot->p_send = p_send->p_next;
if ( p_slot->p_send == NULL )
p_slot->pp_send_last = &p_slot->p_send;
Dump( true, p_send->p_data, p_send->i_size );
if ( write( i_ca_handle, p_send->p_data, p_send->i_size )
!= p_send->i_size )
{
msg_Err( p_access, "en50221: cannot write to CAM device (%m)" );
free( p_send->p_data );
free( p_send );
return -1;
}
free( p_send->p_data );
free( p_send );
p_slot->b_expect_answer = true;
return 0;
}
/*****************************************************************************
* TPDUSend
*****************************************************************************/
static int TPDUSend( access_t * p_access, uint8_t i_slot, uint8_t i_tag,
const uint8_t *p_content, int i_length )
{
ci_slot_t *p_slot = &p_slots[i_slot];
uint8_t i_tcid = i_slot + 1;
en50221_msg_t *p_send = malloc( sizeof(en50221_msg_t) );
uint8_t *p_data = malloc( MAX_TPDU_SIZE );
int i_size;
i_size = 0;
p_data[0] = i_slot;
p_data[1] = i_tcid;
p_data[2] = i_tag;
switch ( i_tag )
{
case T_RCV:
case T_CREATE_TC:
case T_CTC_REPLY:
case T_DELETE_TC:
case T_DTC_REPLY:
case T_REQUEST_TC:
p_data[3] = 1; /* length */
p_data[4] = i_tcid;
i_size = 5;
break;
case T_NEW_TC:
case T_TC_ERROR:
p_data[3] = 2; /* length */
p_data[4] = i_tcid;
p_data[5] = p_content[0];
i_size = 6;
break;
case T_DATA_LAST:
case T_DATA_MORE:
{
/* i_length <= MAX_TPDU_DATA */
uint8_t *p = p_data + 3;
p = SetLength( p, i_length + 1 );
*p++ = i_tcid;
if ( i_length )
memcpy( p, p_content, i_length );
i_size = i_length + (p - p_data);
break;
}
default:
break;
}
p_send->p_data = p_data;
p_send->i_size = i_size;
p_send->p_next = NULL;
*p_slot->pp_send_last = p_send;
p_slot->pp_send_last = &p_send->p_next;
if ( !p_slot->b_expect_answer )
return TPDUWrite( p_access, i_slot );
return 0;
}
/*****************************************************************************
* TPDURecv
*****************************************************************************/
static int TPDURecv( access_t * p_access )
{
ci_slot_t *p_slot;
uint8_t i_tag, i_slot;
uint8_t p_data[MAX_TPDU_SIZE];
int i_size;
bool b_last = false;
do
{
i_size = read( i_ca_handle, p_data, MAX_TPDU_SIZE );
}
while ( i_size < 0 && errno == EINTR );
if ( i_size < 5 )
{
msg_Err( p_access, "en50221: cannot read from CAM device (%d:%m)",
i_size );
return -1;
}
Dump( false, p_data, i_size );
i_slot = p_data[1] - 1;
i_tag = p_data[2];
if ( i_slot >= i_nb_slots )
{
msg_Warn( p_access, "en50221: TPDU is from an unknown slot %u",
i_slot );
return -1;
}
p_slot = &p_slots[i_slot];
p_slot->b_has_data = !!(p_data[i_size - 4] == T_SB
&& p_data[i_size - 3] == 2
&& (p_data[i_size - 1] & DATA_INDICATOR));
p_slot->b_expect_answer = false;
switch ( i_tag )
{
case T_CTC_REPLY:
p_slot->b_active = true;
ev_timer_stop(event_loop, &p_slot->init_watcher);
msg_Dbg( p_access, "CI slot %d is active", i_slot );
break;
case T_SB:
break;
case T_DATA_LAST:
b_last = true;
/* intended pass-through */
case T_DATA_MORE:
{
en50221_msg_t *p_recv;
int i_session_size;
uint8_t *p_session = GetLength( &p_data[3], &i_session_size );
if ( i_session_size <= 1 )
break;
p_session++;
i_session_size--;
if ( p_slot->p_recv == NULL )
{
p_slot->p_recv = malloc( sizeof(en50221_msg_t) );
p_slot->p_recv->p_data = NULL;
p_slot->p_recv->i_size = 0;
}
p_recv = p_slot->p_recv;
p_recv->p_data = realloc( p_recv->p_data,
p_recv->i_size + i_session_size );
memcpy( &p_recv->p_data[ p_recv->i_size ], p_session, i_session_size );
p_recv->i_size += i_session_size;
if ( b_last )
{
SPDUHandle( p_access, i_slot, p_recv->p_data, p_recv->i_size );
free( p_recv->p_data );
free( p_recv );
p_slot->p_recv = NULL;
}
break;
}
default:
msg_Warn( p_access, "en50221: unhandled R_TPDU tag %u slot %u", i_tag,
i_slot );
break;
}
if ( !p_slot->b_expect_answer && p_slot->p_send != NULL )
TPDUWrite( p_access, i_slot );
if ( !p_slot->b_expect_answer && p_slot->i_pending_session_id != 0 )
SessionOpenCb( p_access, i_slot );
if ( !p_slot->b_expect_answer && p_slot->b_has_data )
TPDUSend( p_access, i_slot, T_RCV, NULL, 0 );
return 0;
}
/*
* Session layer
*/
#define ST_SESSION_NUMBER 0x90
#define ST_OPEN_SESSION_REQUEST 0x91
#define ST_OPEN_SESSION_RESPONSE 0x92
#define ST_CREATE_SESSION 0x93
#define ST_CREATE_SESSION_RESPONSE 0x94
#define ST_CLOSE_SESSION_REQUEST 0x95
#define ST_CLOSE_SESSION_RESPONSE 0x96
#define SS_OK 0x00
#define SS_NOT_ALLOCATED 0xF0
#define RI_RESOURCE_MANAGER 0x00010041
#define RI_APPLICATION_INFORMATION 0x00020041
#define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
#define RI_HOST_CONTROL 0x00200041
#define RI_DATE_TIME 0x00240041
#define RI_MMI 0x00400041
static int ResourceIdToInt( uint8_t *p_data )
{
return ((int)p_data[0] << 24) | ((int)p_data[1] << 16)
| ((int)p_data[2] << 8) | p_data[3];
}
/*****************************************************************************
* SPDUSend
*****************************************************************************/
static int SPDUSend( access_t *p_access, int i_session_id,
uint8_t *p_data, int i_size )
{
uint8_t *p_spdu = malloc( i_size + 4 );
uint8_t *p = p_spdu;
uint8_t i_slot = p_sessions[i_session_id - 1].i_slot;
*p++ = ST_SESSION_NUMBER;
*p++ = 0x02;
*p++ = (i_session_id >> 8);
*p++ = i_session_id & 0xff;
memcpy( p, p_data, i_size );
i_size += 4;
p = p_spdu;
while ( i_size > 0 )
{
if ( i_size > MAX_TPDU_DATA )
{
if ( TPDUSend( p_access, i_slot, T_DATA_MORE, p,
MAX_TPDU_DATA ) != 0 )
{
msg_Err( p_access, "couldn't send TPDU on session %d",
i_session_id );
free( p_spdu );
return -1;
}
p += MAX_TPDU_DATA;
i_size -= MAX_TPDU_DATA;
}
else
{
if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p, i_size )
!= 0 )
{
msg_Err( p_access, "couldn't send TPDU on session %d",
i_session_id );
free( p_spdu );
return -1;
}
i_size = 0;
}
}
free( p_spdu );
return 0;
}
/*****************************************************************************
* SessionOpen
*****************************************************************************/
static void SessionOpenCb( access_t *p_access, uint8_t i_slot )
{
ci_slot_t *p_slot = &p_slots[i_slot];
int i_session_id = p_slot->i_pending_session_id;
int i_resource_id = p_sessions[i_session_id - 1].i_resource_id;
p_slot->i_pending_session_id = 0;
switch ( i_resource_id )
{
case RI_RESOURCE_MANAGER:
ResourceManagerOpen( p_access, i_session_id ); break;
case RI_APPLICATION_INFORMATION:
ApplicationInformationOpen( p_access, i_session_id ); break;
case RI_CONDITIONAL_ACCESS_SUPPORT:
ConditionalAccessOpen( p_access, i_session_id ); break;
case RI_DATE_TIME:
DateTimeOpen( p_access, i_session_id ); break;
case RI_MMI:
MMIOpen( p_access, i_session_id ); break;
case RI_HOST_CONTROL:
default:
msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
p_sessions[i_session_id - 1].i_resource_id = 0;
}
}
static void SessionOpen( access_t * p_access, uint8_t i_slot,
uint8_t *p_spdu, int i_size )
{
ci_slot_t *p_slot = &p_slots[i_slot];
int i_session_id;
int i_resource_id = ResourceIdToInt( &p_spdu[2] );
uint8_t p_response[16];
int i_status = SS_NOT_ALLOCATED;
for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
{
if ( !p_sessions[i_session_id - 1].i_resource_id )
break;
}
if ( i_session_id > MAX_SESSIONS )
{
msg_Err( p_access, "too many sessions !" );
return;
}
p_sessions[i_session_id - 1].i_slot = i_slot;
p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
p_sessions[i_session_id - 1].pf_close = NULL;
p_sessions[i_session_id - 1].pf_manage = NULL;
if ( i_resource_id == RI_RESOURCE_MANAGER
|| i_resource_id == RI_APPLICATION_INFORMATION
|| i_resource_id == RI_CONDITIONAL_ACCESS_SUPPORT
|| i_resource_id == RI_DATE_TIME
|| i_resource_id == RI_MMI )
{
i_status = SS_OK;
}
p_response[0] = ST_OPEN_SESSION_RESPONSE;
p_response[1] = 0x7;
p_response[2] = i_status;
p_response[3] = p_spdu[2];
p_response[4] = p_spdu[3];
p_response[5] = p_spdu[4];
p_response[6] = p_spdu[5];
p_response[7] = i_session_id >> 8;
p_response[8] = i_session_id & 0xff;
if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 9 ) != 0 )
{
msg_Err( p_access,
"SessionOpen: couldn't send TPDU on slot %d", i_slot );
return;
}
if ( p_slot->i_pending_session_id != 0 )
msg_Warn( p_access, "overwriting pending session %d",
p_slot->i_pending_session_id );
p_slot->i_pending_session_id = i_session_id;
}
#if 0
/* unused code for the moment - commented out to keep gcc happy */
/*****************************************************************************
* SessionCreate
*****************************************************************************/
static void SessionCreate( access_t * p_access, int i_slot, int i_resource_id )
{
uint8_t p_response[16];
uint8_t i_tag;
int i_session_id;
for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
{
if ( !p_sessions[i_session_id - 1].i_resource_id )
break;
}
if ( i_session_id > MAX_SESSIONS )
{
msg_Err( p_access, "too many sessions !" );
return;
}
p_sessions[i_session_id - 1].i_slot = i_slot;
p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
p_sessions[i_session_id - 1].pf_close = NULL;
p_sessions[i_session_id - 1].pf_manage = NULL;
p_sessions[i_session_id - 1].p_sys = NULL;
p_response[0] = ST_CREATE_SESSION;
p_response[1] = 0x6;
p_response[2] = i_resource_id >> 24;
p_response[3] = (i_resource_id >> 16) & 0xff;
p_response[4] = (i_resource_id >> 8) & 0xff;
p_response[5] = i_resource_id & 0xff;
p_response[6] = i_session_id >> 8;
p_response[7] = i_session_id & 0xff;
if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
0 )
{
msg_Err( p_access,
"SessionCreate: couldn't send TPDU on slot %d", i_slot );
return;
}
}
#endif
/*****************************************************************************
* SessionCreateResponse
*****************************************************************************/
static void SessionCreateResponse( access_t * p_access, uint8_t i_slot,
uint8_t *p_spdu, int i_size )
{
int i_status = p_spdu[2];
int i_resource_id = ResourceIdToInt( &p_spdu[3] );
int i_session_id = ((int)p_spdu[7] << 8) | p_spdu[8];
if ( i_status != SS_OK )
{
msg_Err( p_access, "SessionCreateResponse: failed to open session %d"
" resource=0x%x status=0x%x", i_session_id, i_resource_id,
i_status );
p_sessions[i_session_id - 1].i_resource_id = 0;
return;
}
switch ( i_resource_id )
{
case RI_RESOURCE_MANAGER:
ResourceManagerOpen( p_access, i_session_id ); break;
case RI_APPLICATION_INFORMATION:
ApplicationInformationOpen( p_access, i_session_id ); break;
case RI_CONDITIONAL_ACCESS_SUPPORT:
ConditionalAccessOpen( p_access, i_session_id ); break;
case RI_DATE_TIME:
DateTimeOpen( p_access, i_session_id ); break;
case RI_MMI:
MMIOpen( p_access, i_session_id ); break;
case RI_HOST_CONTROL:
default:
msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
p_sessions[i_session_id - 1].i_resource_id = 0;
}
}
/*****************************************************************************
* SessionSendClose
*****************************************************************************/
static void SessionSendClose( access_t * p_access, int i_session_id )
{
uint8_t p_response[16];
uint8_t i_slot = p_sessions[i_session_id - 1].i_slot;
p_response[0] = ST_CLOSE_SESSION_REQUEST;
p_response[1] = 0x2;
p_response[2] = i_session_id >> 8;
p_response[3] = i_session_id & 0xff;
if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
0 )
{
msg_Err( p_access,
"SessionSendClose: couldn't send TPDU on slot %d", i_slot );
return;
}
}
/*****************************************************************************
* SessionClose
*****************************************************************************/
static void SessionClose( access_t * p_access, int i_session_id )
{
uint8_t p_response[16];
uint8_t i_slot = p_sessions[i_session_id - 1].i_slot;
if ( p_sessions[i_session_id - 1].pf_close != NULL )
p_sessions[i_session_id - 1].pf_close( p_access, i_session_id );
p_sessions[i_session_id - 1].i_resource_id = 0;
p_response[0] = ST_CLOSE_SESSION_RESPONSE;
p_response[1] = 0x3;
p_response[2] = SS_OK;
p_response[3] = i_session_id >> 8;
p_response[4] = i_session_id & 0xff;
if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 5 ) !=
0 )
{
msg_Err( p_access,
"SessionClose: couldn't send TPDU on slot %d", i_slot );
return;
}
}
/*****************************************************************************
* SPDUHandle
*****************************************************************************/
static void SPDUHandle( access_t * p_access, uint8_t i_slot,
uint8_t *p_spdu, int i_size )
{
int i_session_id;
switch ( p_spdu[0] )
{
case ST_SESSION_NUMBER:
if ( i_size <= 4 )
return;
i_session_id = (p_spdu[2] << 8) | p_spdu[3];
if ( i_session_id <= MAX_SESSIONS
&& p_sessions[i_session_id - 1].pf_handle != NULL )
p_sessions[i_session_id - 1].pf_handle( p_access, i_session_id,
p_spdu + 4, i_size - 4 );
break;
case ST_OPEN_SESSION_REQUEST:
if ( i_size != 6 || p_spdu[1] != 0x4 )
return;
SessionOpen( p_access, i_slot, p_spdu, i_size );
break;
case ST_CREATE_SESSION_RESPONSE:
if ( i_size != 9 || p_spdu[1] != 0x7 )
return;
SessionCreateResponse( p_access, i_slot, p_spdu, i_size );
break;
case ST_CLOSE_SESSION_REQUEST:
if ( i_size != 4 || p_spdu[1] != 0x2 )
return;
i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
SessionClose( p_access, i_session_id );
break;
case ST_CLOSE_SESSION_RESPONSE:
if ( i_size != 5 || p_spdu[1] != 0x3 )
return;
i_session_id = ((int)p_spdu[3] << 8) | p_spdu[4];
if ( p_spdu[2] )
{
msg_Err( p_access, "closing a session which is not allocated (%d)",
i_session_id );
}
else
{
if ( p_sessions[i_session_id - 1].pf_close != NULL )
p_sessions[i_session_id - 1].pf_close( p_access,
i_session_id );
p_sessions[i_session_id - 1].i_resource_id = 0;
}
break;
default:
msg_Err( p_access, "unexpected tag in SPDUHandle (%x)", p_spdu[0] );
break;
}
}
/*
* Application layer
*/
#define AOT_NONE 0x000000
#define AOT_PROFILE_ENQ 0x9F8010
#define AOT_PROFILE 0x9F8011
#define AOT_PROFILE_CHANGE 0x9F8012
#define AOT_APPLICATION_INFO_ENQ 0x9F8020
#define AOT_APPLICATION_INFO 0x9F8021
#define AOT_ENTER_MENU 0x9F8022
#define AOT_CA_INFO_ENQ 0x9F8030
#define AOT_CA_INFO 0x9F8031
#define AOT_CA_PMT 0x9F8032
#define AOT_CA_PMT_REPLY 0x9F8033
#define AOT_CA_UPDATE 0x9F8034
#define AOT_TUNE 0x9F8400
#define AOT_REPLACE 0x9F8401
#define AOT_CLEAR_REPLACE 0x9F8402
#define AOT_ASK_RELEASE 0x9F8403
#define AOT_DATE_TIME_ENQ 0x9F8440
#define AOT_DATE_TIME 0x9F8441
#define AOT_CLOSE_MMI 0x9F8800
#define AOT_DISPLAY_CONTROL 0x9F8801
#define AOT_DISPLAY_REPLY 0x9F8802
#define AOT_TEXT_LAST 0x9F8803
#define AOT_TEXT_MORE 0x9F8804
#define AOT_KEYPAD_CONTROL 0x9F8805
#define AOT_KEYPRESS 0x9F8806
#define AOT_ENQ 0x9F8807
#define AOT_ANSW 0x9F8808
#define AOT_MENU_LAST 0x9F8809
#define AOT_MENU_MORE 0x9F880A
#define AOT_MENU_ANSW 0x9F880B
#define AOT_LIST_LAST 0x9F880C
#define AOT_LIST_MORE 0x9F880D
#define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
#define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
#define AOT_DISPLAY_MESSAGE 0x9F8810
#define AOT_SCENE_END_MARK 0x9F8811
#define AOT_SCENE_DONE 0x9F8812
#define AOT_SCENE_CONTROL 0x9F8813
#define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
#define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
#define AOT_FLUSH_DOWNLOAD 0x9F8816
#define AOT_DOWNLOAD_REPLY 0x9F8817
#define AOT_COMMS_CMD 0x9F8C00
#define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
#define AOT_COMMS_REPLY 0x9F8C02
#define AOT_COMMS_SEND_LAST 0x9F8C03
#define AOT_COMMS_SEND_MORE 0x9F8C04
#define AOT_COMMS_RCV_LAST 0x9F8C05
#define AOT_COMMS_RCV_MORE 0x9F8C06
/*****************************************************************************
* APDUGetTag
*****************************************************************************/
static int APDUGetTag( const uint8_t *p_apdu, int i_size )
{
if ( i_size >= 3 )
{
int i, t = 0;
for ( i = 0; i < 3; i++ )
t = (t << 8) | *p_apdu++;
return t;
}
return AOT_NONE;
}
/*****************************************************************************
* APDUGetLength
*****************************************************************************/
static uint8_t *APDUGetLength( uint8_t *p_apdu, int *pi_size )
{
return GetLength( &p_apdu[3], pi_size );
}
/*****************************************************************************
* APDUSend
*****************************************************************************/
static int APDUSend( access_t * p_access, int i_session_id, int i_tag,
uint8_t *p_data, int i_size )
{
uint8_t *p_apdu = malloc( i_size + 12 );
uint8_t *p = p_apdu;
ca_msg_t ca_msg;
int i_ret;
*p++ = (i_tag >> 16);
*p++ = (i_tag >> 8) & 0xff;
*p++ = i_tag & 0xff;
p = SetLength( p, i_size );
if ( i_size )
memcpy( p, p_data, i_size );
if ( i_ca_type == CA_CI_LINK )
{
i_ret = SPDUSend( p_access, i_session_id, p_apdu, i_size + p - p_apdu );
}
else
{
if ( i_size + p - p_apdu > 256 )
{
msg_Err( p_access, "CAM: apdu overflow" );
i_ret = -1;
}
else
{
ca_msg.length = i_size + p - p_apdu;
if ( i_size == 0 ) ca_msg.length=3;
memcpy( ca_msg.msg, p_apdu, i_size + p - p_apdu );
i_ret = ioctl(i_ca_handle, CA_SEND_MSG, &ca_msg );
if ( i_ret < 0 )
{
msg_Err( p_access, "Error sending to CAM: %m" );
i_ret = -1;
}
}
}
free( p_apdu );
return i_ret;
}
/*
* Resource Manager
*/
/*****************************************************************************
* ResourceManagerHandle
*****************************************************************************/
static void ResourceManagerHandle( access_t * p_access, int i_session_id,
uint8_t *p_apdu, int i_size )
{
int i_tag = APDUGetTag( p_apdu, i_size );
switch ( i_tag )
{
case AOT_PROFILE_ENQ:
{
int resources[] = { htonl(RI_RESOURCE_MANAGER),
htonl(RI_APPLICATION_INFORMATION),
htonl(RI_CONDITIONAL_ACCESS_SUPPORT),
htonl(RI_DATE_TIME),
htonl(RI_MMI)
};
APDUSend( p_access, i_session_id, AOT_PROFILE, (uint8_t*)resources,
sizeof(resources) );
break;
}
case AOT_PROFILE:
APDUSend( p_access, i_session_id, AOT_PROFILE_CHANGE, NULL, 0 );
break;
default:
msg_Err( p_access, "unexpected tag in ResourceManagerHandle (0x%x)",
i_tag );
}
}
/*****************************************************************************
* ResourceManagerOpen
*****************************************************************************/
static void ResourceManagerOpen( access_t * p_access, int i_session_id )
{
msg_Dbg( p_access, "opening ResourceManager session (%d)", i_session_id );
p_sessions[i_session_id - 1].pf_handle = ResourceManagerHandle;
APDUSend( p_access, i_session_id, AOT_PROFILE_ENQ, NULL, 0 );
}
/*
* Application Information
*/