1 /*****************************************************************************
2 * en50221.c : implementation of the transport, session and applications
4 *****************************************************************************
5 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Based on code from libdvbci Copyright (C) 2000 Klaus Schmidinger
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_access.h>
32 #include <sys/ioctl.h>
35 #include <sys/types.h>
39 #include <netinet/in.h>
41 /* DVB Card Drivers */
42 #include <linux/dvb/version.h>
43 #include <linux/dvb/dmx.h>
44 #include <linux/dvb/frontend.h>
45 #include <linux/dvb/ca.h>
47 /* Include dvbpsi headers */
48 #ifdef HAVE_DVBPSI_DR_H
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/descriptor.h>
51 # include <dvbpsi/pat.h>
52 # include <dvbpsi/pmt.h>
53 # include <dvbpsi/dr.h>
54 # include <dvbpsi/psi.h>
55 # include <dvbpsi/demux.h>
56 # include <dvbpsi/sdt.h>
59 # include "descriptor.h"
60 # include "tables/pat.h"
61 # include "tables/pmt.h"
62 # include "descriptors/dr.h"
65 # include "tables/sdt.h"
69 # include <vlc_httpd.h>
74 #include <vlc_charset.h>
77 #define HLCI_WAIT_CAM_READY 0
78 #define CAM_PROG_MAX MAX_PROGRAMS
79 //#define CAPMT_WAIT 100 /* uncomment this for slow CAMs */
81 static void ResourceManagerOpen( access_t * p_access, int i_session_id );
82 static void ApplicationInformationOpen( access_t * p_access, int i_session_id );
83 static void ConditionalAccessOpen( access_t * p_access, int i_session_id );
84 static void DateTimeOpen( access_t * p_access, int i_session_id );
85 static void MMIOpen( access_t * p_access, int i_session_id );
87 /*****************************************************************************
89 *****************************************************************************/
90 #define SIZE_INDICATOR 0x80
92 static uint8_t *GetLength( uint8_t *p_data, int *pi_length )
94 *pi_length = *p_data++;
96 if ( (*pi_length & SIZE_INDICATOR) != 0 )
98 int l = *pi_length & ~SIZE_INDICATOR;
102 for ( i = 0; i < l; i++ )
103 *pi_length = (*pi_length << 8) | *p_data++;
109 static uint8_t *SetLength( uint8_t *p_data, int i_length )
113 if ( i_length < 128 )
117 else if ( i_length < 256 )
119 *p++ = SIZE_INDICATOR | 0x1;
122 else if ( i_length < 65536 )
124 *p++ = SIZE_INDICATOR | 0x2;
125 *p++ = i_length >> 8;
126 *p++ = i_length & 0xff;
128 else if ( i_length < 16777216 )
130 *p++ = SIZE_INDICATOR | 0x3;
131 *p++ = i_length >> 16;
132 *p++ = (i_length >> 8) & 0xff;
133 *p++ = i_length & 0xff;
137 *p++ = SIZE_INDICATOR | 0x4;
138 *p++ = i_length >> 24;
139 *p++ = (i_length >> 16) & 0xff;
140 *p++ = (i_length >> 8) & 0xff;
141 *p++ = i_length & 0xff;
152 #define MAX_TPDU_SIZE 4096
153 #define MAX_TPDU_DATA (MAX_TPDU_SIZE - 4)
155 #define DATA_INDICATOR 0x80
159 #define T_CREATE_TC 0x82
160 #define T_CTC_REPLY 0x83
161 #define T_DELETE_TC 0x84
162 #define T_DTC_REPLY 0x85
163 #define T_REQUEST_TC 0x86
164 #define T_NEW_TC 0x87
165 #define T_TC_ERROR 0x88
166 #define T_DATA_LAST 0xA0
167 #define T_DATA_MORE 0xA1
169 static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
174 fprintf(stderr, "%s ", b_outgoing ? "-->" : "<--");
175 for ( i = 0; i < i_size && i < MAX_DUMP; i++)
176 fprintf(stderr, "%02X ", p_data[i]);
177 fprintf(stderr, "%s\n", i_size >= MAX_DUMP ? "..." : "");
179 VLC_UNUSED(b_outgoing); VLC_UNUSED(p_data); VLC_UNUSED(i_size);
183 /*****************************************************************************
185 *****************************************************************************/
186 static int TPDUSend( access_t * p_access, uint8_t i_slot, uint8_t i_tag,
187 const uint8_t *p_content, int i_length )
189 access_sys_t *p_sys = p_access->p_sys;
190 uint8_t i_tcid = i_slot + 1;
191 uint8_t p_data[MAX_TPDU_SIZE];
207 p_data[3] = 1; /* length */
214 p_data[3] = 2; /* length */
216 p_data[5] = p_content[0];
223 /* i_length <= MAX_TPDU_DATA */
224 uint8_t *p = p_data + 3;
225 p = SetLength( p, i_length + 1 );
229 memcpy( p, p_content, i_length );
230 i_size = i_length + (p - p_data);
237 Dump( true, p_data, i_size );
239 if ( write( p_sys->i_ca_handle, p_data, i_size ) != i_size )
241 msg_Err( p_access, "cannot write to CAM device (%m)" );
249 /*****************************************************************************
251 *****************************************************************************/
252 #define CAM_READ_TIMEOUT 3500 // ms
254 static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
255 uint8_t *p_data, int *pi_size )
257 access_sys_t *p_sys = p_access->p_sys;
258 uint8_t i_tcid = i_slot + 1;
260 struct pollfd pfd[1];
262 pfd[0].fd = p_sys->i_ca_handle;
263 pfd[0].events = POLLIN;
264 if ( !(poll(pfd, 1, CAM_READ_TIMEOUT) > 0 && (pfd[0].revents & POLLIN)) )
266 msg_Err( p_access, "cannot poll from CAM device" );
270 if ( pi_size == NULL )
272 p_data = xmalloc( MAX_TPDU_SIZE );
277 i_size = read( p_sys->i_ca_handle, p_data, MAX_TPDU_SIZE );
279 if ( i_size >= 0 || errno != EINTR )
285 msg_Err( p_access, "cannot read from CAM device (%d:%m)", i_size );
286 if( pi_size == NULL )
291 if ( p_data[1] != i_tcid )
293 msg_Err( p_access, "invalid read from CAM device (%d instead of %d)",
295 if( pi_size == NULL )
301 p_sys->pb_tc_has_data[i_slot] = (i_size >= 4
302 && p_data[i_size - 4] == T_SB
303 && p_data[i_size - 3] == 2
304 && (p_data[i_size - 1] & DATA_INDICATOR))
307 Dump( false, p_data, i_size );
309 if ( pi_size == NULL )
322 #define ST_SESSION_NUMBER 0x90
323 #define ST_OPEN_SESSION_REQUEST 0x91
324 #define ST_OPEN_SESSION_RESPONSE 0x92
325 #define ST_CREATE_SESSION 0x93
326 #define ST_CREATE_SESSION_RESPONSE 0x94
327 #define ST_CLOSE_SESSION_REQUEST 0x95
328 #define ST_CLOSE_SESSION_RESPONSE 0x96
331 #define SS_NOT_ALLOCATED 0xF0
333 #define RI_RESOURCE_MANAGER 0x00010041
334 #define RI_APPLICATION_INFORMATION 0x00020041
335 #define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
336 #define RI_HOST_CONTROL 0x00200041
337 #define RI_DATE_TIME 0x00240041
338 #define RI_MMI 0x00400041
340 static int ResourceIdToInt( uint8_t *p_data )
342 return ((int)p_data[0] << 24) | ((int)p_data[1] << 16)
343 | ((int)p_data[2] << 8) | p_data[3];
346 /*****************************************************************************
348 *****************************************************************************/
349 static int SPDUSend( access_t * p_access, int i_session_id,
350 uint8_t *p_data, int i_size )
352 access_sys_t *p_sys = p_access->p_sys;
353 uint8_t *p_spdu = xmalloc( i_size + 4 );
356 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
358 *p++ = ST_SESSION_NUMBER;
360 *p++ = (i_session_id >> 8);
361 *p++ = i_session_id & 0xff;
363 memcpy( p, p_data, i_size );
370 if ( i_size > MAX_TPDU_DATA )
372 if ( TPDUSend( p_access, i_slot, T_DATA_MORE, p,
373 MAX_TPDU_DATA ) != VLC_SUCCESS )
375 msg_Err( p_access, "couldn't send TPDU on session %d",
381 i_size -= MAX_TPDU_DATA;
385 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p, i_size )
388 msg_Err( p_access, "couldn't send TPDU on session %d",
396 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS
399 msg_Err( p_access, "couldn't recv TPDU on session %d",
410 /*****************************************************************************
412 *****************************************************************************/
413 static void SessionOpen( access_t * p_access, uint8_t i_slot,
414 uint8_t *p_spdu, int i_size )
416 VLC_UNUSED( i_size );
418 access_sys_t *p_sys = p_access->p_sys;
420 int i_resource_id = ResourceIdToInt( &p_spdu[2] );
421 uint8_t p_response[16];
422 int i_status = SS_NOT_ALLOCATED;
425 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
427 if ( !p_sys->p_sessions[i_session_id - 1].i_resource_id )
430 if ( i_session_id > MAX_SESSIONS )
432 msg_Err( p_access, "too many sessions !" );
435 p_sys->p_sessions[i_session_id - 1].i_slot = i_slot;
436 p_sys->p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
437 p_sys->p_sessions[i_session_id - 1].pf_close = NULL;
438 p_sys->p_sessions[i_session_id - 1].pf_manage = NULL;
440 if ( i_resource_id == RI_RESOURCE_MANAGER
441 || i_resource_id == RI_APPLICATION_INFORMATION
442 || i_resource_id == RI_CONDITIONAL_ACCESS_SUPPORT
443 || i_resource_id == RI_DATE_TIME
444 || i_resource_id == RI_MMI )
449 p_response[0] = ST_OPEN_SESSION_RESPONSE;
451 p_response[2] = i_status;
452 p_response[3] = p_spdu[2];
453 p_response[4] = p_spdu[3];
454 p_response[5] = p_spdu[4];
455 p_response[6] = p_spdu[5];
456 p_response[7] = i_session_id >> 8;
457 p_response[8] = i_session_id & 0xff;
459 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 9 ) !=
463 "SessionOpen: couldn't send TPDU on slot %d", i_slot );
466 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
469 "SessionOpen: couldn't recv TPDU on slot %d", i_slot );
473 switch ( i_resource_id )
475 case RI_RESOURCE_MANAGER:
476 ResourceManagerOpen( p_access, i_session_id ); break;
477 case RI_APPLICATION_INFORMATION:
478 ApplicationInformationOpen( p_access, i_session_id ); break;
479 case RI_CONDITIONAL_ACCESS_SUPPORT:
480 ConditionalAccessOpen( p_access, i_session_id ); break;
482 DateTimeOpen( p_access, i_session_id ); break;
484 MMIOpen( p_access, i_session_id ); break;
486 case RI_HOST_CONTROL:
488 msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
489 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
494 /* unused code for the moment - commented out to keep gcc happy */
495 /*****************************************************************************
497 *****************************************************************************/
498 static void SessionCreate( access_t * p_access, int i_slot, int i_resource_id )
500 access_sys_t *p_sys = p_access->p_sys;
501 uint8_t p_response[16];
505 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
507 if ( !p_sys->p_sessions[i_session_id - 1].i_resource_id )
510 if ( i_session_id == MAX_SESSIONS )
512 msg_Err( p_access, "too many sessions !" );
515 p_sys->p_sessions[i_session_id - 1].i_slot = i_slot;
516 p_sys->p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
517 p_sys->p_sessions[i_session_id - 1].pf_close = NULL;
518 p_sys->p_sessions[i_session_id - 1].pf_manage = NULL;
519 p_sys->p_sessions[i_session_id - 1].p_sys = NULL;
521 p_response[0] = ST_CREATE_SESSION;
523 p_response[2] = i_resource_id >> 24;
524 p_response[3] = (i_resource_id >> 16) & 0xff;
525 p_response[4] = (i_resource_id >> 8) & 0xff;
526 p_response[5] = i_resource_id & 0xff;
527 p_response[6] = i_session_id >> 8;
528 p_response[7] = i_session_id & 0xff;
530 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
534 "SessionCreate: couldn't send TPDU on slot %d", i_slot );
537 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
540 "SessionCreate: couldn't recv TPDU on slot %d", i_slot );
546 /*****************************************************************************
547 * SessionCreateResponse
548 *****************************************************************************/
549 static void SessionCreateResponse( access_t * p_access, uint8_t i_slot,
550 uint8_t *p_spdu, int i_size )
552 VLC_UNUSED( i_size );
553 VLC_UNUSED( i_slot );
555 access_sys_t *p_sys = p_access->p_sys;
556 int i_status = p_spdu[2];
557 int i_resource_id = ResourceIdToInt( &p_spdu[3] );
558 int i_session_id = ((int)p_spdu[7] << 8) | p_spdu[8];
560 if ( i_status != SS_OK )
562 msg_Err( p_access, "SessionCreateResponse: failed to open session %d"
563 " resource=0x%x status=0x%x", i_session_id, i_resource_id,
565 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
569 switch ( i_resource_id )
571 case RI_RESOURCE_MANAGER:
572 ResourceManagerOpen( p_access, i_session_id ); break;
573 case RI_APPLICATION_INFORMATION:
574 ApplicationInformationOpen( p_access, i_session_id ); break;
575 case RI_CONDITIONAL_ACCESS_SUPPORT:
576 ConditionalAccessOpen( p_access, i_session_id ); break;
578 DateTimeOpen( p_access, i_session_id ); break;
580 MMIOpen( p_access, i_session_id ); break;
582 case RI_HOST_CONTROL:
584 msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
585 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
589 /*****************************************************************************
591 *****************************************************************************/
592 static void SessionSendClose( access_t * p_access, int i_session_id )
594 access_sys_t *p_sys = p_access->p_sys;
595 uint8_t p_response[16];
597 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
599 p_response[0] = ST_CLOSE_SESSION_REQUEST;
601 p_response[2] = i_session_id >> 8;
602 p_response[3] = i_session_id & 0xff;
604 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
608 "SessionSendClose: couldn't send TPDU on slot %d", i_slot );
611 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
614 "SessionSendClose: couldn't recv TPDU on slot %d", i_slot );
619 /*****************************************************************************
621 *****************************************************************************/
622 static void SessionClose( access_t * p_access, int i_session_id )
624 access_sys_t *p_sys = p_access->p_sys;
625 uint8_t p_response[16];
627 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
629 if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
630 p_sys->p_sessions[i_session_id - 1].pf_close( p_access, i_session_id );
631 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
633 p_response[0] = ST_CLOSE_SESSION_RESPONSE;
635 p_response[2] = SS_OK;
636 p_response[3] = i_session_id >> 8;
637 p_response[4] = i_session_id & 0xff;
639 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 5 ) !=
643 "SessionClose: couldn't send TPDU on slot %d", i_slot );
646 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
649 "SessionClose: couldn't recv TPDU on slot %d", i_slot );
654 /*****************************************************************************
656 *****************************************************************************/
657 static void SPDUHandle( access_t * p_access, uint8_t i_slot,
658 uint8_t *p_spdu, int i_size )
660 access_sys_t *p_sys = p_access->p_sys;
665 case ST_SESSION_NUMBER:
668 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
669 p_sys->p_sessions[i_session_id - 1].pf_handle( p_access, i_session_id,
670 p_spdu + 4, i_size - 4 );
673 case ST_OPEN_SESSION_REQUEST:
674 if ( i_size != 6 || p_spdu[1] != 0x4 )
676 SessionOpen( p_access, i_slot, p_spdu, i_size );
679 case ST_CREATE_SESSION_RESPONSE:
680 if ( i_size != 9 || p_spdu[1] != 0x7 )
682 SessionCreateResponse( p_access, i_slot, p_spdu, i_size );
685 case ST_CLOSE_SESSION_REQUEST:
686 if ( i_size != 4 || p_spdu[1] != 0x2 )
688 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
689 SessionClose( p_access, i_session_id );
692 case ST_CLOSE_SESSION_RESPONSE:
693 if ( i_size != 5 || p_spdu[1] != 0x3 )
695 i_session_id = ((int)p_spdu[3] << 8) | p_spdu[4];
698 msg_Err( p_access, "closing a session which is not allocated (%d)",
703 if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
704 p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
706 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
711 msg_Err( p_access, "unexpected tag in SPDUHandle (%x)", p_spdu[0] );
721 #define AOT_NONE 0x000000
722 #define AOT_PROFILE_ENQ 0x9F8010
723 #define AOT_PROFILE 0x9F8011
724 #define AOT_PROFILE_CHANGE 0x9F8012
725 #define AOT_APPLICATION_INFO_ENQ 0x9F8020
726 #define AOT_APPLICATION_INFO 0x9F8021
727 #define AOT_ENTER_MENU 0x9F8022
728 #define AOT_CA_INFO_ENQ 0x9F8030
729 #define AOT_CA_INFO 0x9F8031
730 #define AOT_CA_PMT 0x9F8032
731 #define AOT_CA_PMT_REPLY 0x9F8033
732 #define AOT_TUNE 0x9F8400
733 #define AOT_REPLACE 0x9F8401
734 #define AOT_CLEAR_REPLACE 0x9F8402
735 #define AOT_ASK_RELEASE 0x9F8403
736 #define AOT_DATE_TIME_ENQ 0x9F8440
737 #define AOT_DATE_TIME 0x9F8441
738 #define AOT_CLOSE_MMI 0x9F8800
739 #define AOT_DISPLAY_CONTROL 0x9F8801
740 #define AOT_DISPLAY_REPLY 0x9F8802
741 #define AOT_TEXT_LAST 0x9F8803
742 #define AOT_TEXT_MORE 0x9F8804
743 #define AOT_KEYPAD_CONTROL 0x9F8805
744 #define AOT_KEYPRESS 0x9F8806
745 #define AOT_ENQ 0x9F8807
746 #define AOT_ANSW 0x9F8808
747 #define AOT_MENU_LAST 0x9F8809
748 #define AOT_MENU_MORE 0x9F880A
749 #define AOT_MENU_ANSW 0x9F880B
750 #define AOT_LIST_LAST 0x9F880C
751 #define AOT_LIST_MORE 0x9F880D
752 #define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
753 #define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
754 #define AOT_DISPLAY_MESSAGE 0x9F8810
755 #define AOT_SCENE_END_MARK 0x9F8811
756 #define AOT_SCENE_DONE 0x9F8812
757 #define AOT_SCENE_CONTROL 0x9F8813
758 #define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
759 #define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
760 #define AOT_FLUSH_DOWNLOAD 0x9F8816
761 #define AOT_DOWNLOAD_REPLY 0x9F8817
762 #define AOT_COMMS_CMD 0x9F8C00
763 #define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
764 #define AOT_COMMS_REPLY 0x9F8C02
765 #define AOT_COMMS_SEND_LAST 0x9F8C03
766 #define AOT_COMMS_SEND_MORE 0x9F8C04
767 #define AOT_COMMS_RCV_LAST 0x9F8C05
768 #define AOT_COMMS_RCV_MORE 0x9F8C06
770 /*****************************************************************************
772 *****************************************************************************/
773 static int APDUGetTag( const uint8_t *p_apdu, int i_size )
778 for ( i = 0; i < 3; i++ )
779 t = (t << 8) | *p_apdu++;
786 /*****************************************************************************
788 *****************************************************************************/
789 static uint8_t *APDUGetLength( uint8_t *p_apdu, int *pi_size )
791 return GetLength( &p_apdu[3], pi_size );
794 /*****************************************************************************
796 *****************************************************************************/
797 static int APDUSend( access_t * p_access, int i_session_id, int i_tag,
798 uint8_t *p_data, int i_size )
800 access_sys_t *p_sys = p_access->p_sys;
801 uint8_t *p_apdu = xmalloc( i_size + 12 );
806 *p++ = (i_tag >> 16);
807 *p++ = (i_tag >> 8) & 0xff;
809 p = SetLength( p, i_size );
811 memcpy( p, p_data, i_size );
812 if ( p_sys->i_ca_type == CA_CI_LINK )
814 i_ret = SPDUSend( p_access, i_session_id, p_apdu, i_size + p - p_apdu );
818 if ( i_size + p - p_apdu > 256 )
820 msg_Err( p_access, "CAM: apdu overflow" );
821 i_ret = VLC_EGENERIC;
825 ca_msg.length = i_size + p - p_apdu;
826 if ( i_size == 0 ) ca_msg.length=3;
827 memcpy( ca_msg.msg, p_apdu, i_size + p - p_apdu );
828 i_ret = ioctl(p_sys->i_ca_handle, CA_SEND_MSG, &ca_msg );
831 msg_Err( p_access, "Error sending to CAM: %m" );
832 i_ret = VLC_EGENERIC;
844 /*****************************************************************************
845 * ResourceManagerHandle
846 *****************************************************************************/
847 static void ResourceManagerHandle( access_t * p_access, int i_session_id,
848 uint8_t *p_apdu, int i_size )
850 int i_tag = APDUGetTag( p_apdu, i_size );
854 case AOT_PROFILE_ENQ:
856 int resources[] = { htonl(RI_RESOURCE_MANAGER),
857 htonl(RI_APPLICATION_INFORMATION),
858 htonl(RI_CONDITIONAL_ACCESS_SUPPORT),
862 APDUSend( p_access, i_session_id, AOT_PROFILE, (uint8_t*)resources,
867 APDUSend( p_access, i_session_id, AOT_PROFILE_CHANGE, NULL, 0 );
871 msg_Err( p_access, "unexpected tag in ResourceManagerHandle (0x%x)",
876 /*****************************************************************************
877 * ResourceManagerOpen
878 *****************************************************************************/
879 static void ResourceManagerOpen( access_t * p_access, int i_session_id )
881 access_sys_t *p_sys = p_access->p_sys;
883 msg_Dbg( p_access, "opening ResourceManager session (%d)", i_session_id );
885 p_sys->p_sessions[i_session_id - 1].pf_handle = ResourceManagerHandle;
887 APDUSend( p_access, i_session_id, AOT_PROFILE_ENQ, NULL, 0 );
891 * Application Information
894 /*****************************************************************************
895 * ApplicationInformationEnterMenu
896 *****************************************************************************/
897 static void ApplicationInformationEnterMenu( access_t * p_access,
900 access_sys_t *p_sys = p_access->p_sys;
901 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
903 msg_Dbg( p_access, "entering MMI menus on session %d", i_session_id );
904 APDUSend( p_access, i_session_id, AOT_ENTER_MENU, NULL, 0 );
905 p_sys->pb_slot_mmi_expected[i_slot] = true;
908 /*****************************************************************************
909 * ApplicationInformationHandle
910 *****************************************************************************/
911 static void ApplicationInformationHandle( access_t * p_access, int i_session_id,
912 uint8_t *p_apdu, int i_size )
914 VLC_UNUSED(i_session_id);
915 int i_tag = APDUGetTag( p_apdu, i_size );
919 case AOT_APPLICATION_INFO:
921 int i_type, i_manufacturer, i_code;
923 uint8_t *d = APDUGetLength( p_apdu, &l );
926 p_apdu[l + 4] = '\0';
929 i_manufacturer = ((int)d[0] << 8) | d[1];
931 i_code = ((int)d[0] << 8) | d[1];
933 d = GetLength( d, &l );
935 msg_Info( p_access, "CAM: %s, %02X, %04X, %04X",
936 d, i_type, i_manufacturer, i_code );
941 "unexpected tag in ApplicationInformationHandle (0x%x)",
946 /*****************************************************************************
947 * ApplicationInformationOpen
948 *****************************************************************************/
949 static void ApplicationInformationOpen( access_t * p_access, int i_session_id )
951 access_sys_t *p_sys = p_access->p_sys;
953 msg_Dbg( p_access, "opening ApplicationInformation session (%d)", i_session_id );
955 p_sys->p_sessions[i_session_id - 1].pf_handle = ApplicationInformationHandle;
957 APDUSend( p_access, i_session_id, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
964 #define MAX_CASYSTEM_IDS 64
968 uint16_t pi_system_ids[MAX_CASYSTEM_IDS + 1];
971 static bool CheckSystemID( system_ids_t *p_ids, uint16_t i_id )
974 if( !p_ids ) return true; /* dummy session for high-level CI intf */
976 while ( p_ids->pi_system_ids[i] )
978 if ( p_ids->pi_system_ids[i] == i_id )
986 /*****************************************************************************
987 * CAPMTNeedsDescrambling
988 *****************************************************************************/
989 static bool CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
991 dvbpsi_descriptor_t *p_dr;
992 dvbpsi_pmt_es_t *p_es;
994 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
996 if( p_dr->i_tag == 0x9 )
1002 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1004 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
1005 p_dr = p_dr->p_next )
1007 if( p_dr->i_tag == 0x9 )
1017 /*****************************************************************************
1019 *****************************************************************************/
1020 static int GetCADSize( system_ids_t *p_ids, dvbpsi_descriptor_t *p_dr )
1024 while ( p_dr != NULL )
1026 if( p_dr->i_tag == 0x9 )
1028 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1030 if ( CheckSystemID( p_ids, i_sysid ) )
1031 i_cad_size += p_dr->i_length + 2;
1033 p_dr = p_dr->p_next;
1039 static uint8_t *CAPMTHeader( system_ids_t *p_ids, uint8_t i_list_mgt,
1040 uint16_t i_program_number, uint8_t i_version,
1041 int i_size, dvbpsi_descriptor_t *p_dr,
1047 p_data = xmalloc( 7 + i_size );
1049 p_data = xmalloc( 6 );
1051 p_data[0] = i_list_mgt;
1052 p_data[1] = i_program_number >> 8;
1053 p_data[2] = i_program_number & 0xff;
1054 p_data[3] = ((i_version & 0x1f) << 1) | 0x1;
1060 p_data[4] = (i_size + 1) >> 8;
1061 p_data[5] = (i_size + 1) & 0xff;
1065 while ( p_dr != NULL )
1067 if( p_dr->i_tag == 0x9 )
1069 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1071 if ( CheckSystemID( p_ids, i_sysid ) )
1074 p_data[i + 1] = p_dr->i_length;
1075 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1076 // p_data[i+4] &= 0x1f;
1077 i += p_dr->i_length + 2;
1080 p_dr = p_dr->p_next;
1092 static uint8_t *CAPMTES( system_ids_t *p_ids, uint8_t *p_capmt,
1093 int i_capmt_size, uint8_t i_type, uint16_t i_pid,
1094 int i_size, dvbpsi_descriptor_t *p_dr,
1101 p_data = xrealloc( p_capmt, i_capmt_size + 6 + i_size );
1103 p_data = xrealloc( p_capmt, i_capmt_size + 5 );
1108 p_data[i + 1] = i_pid >> 8;
1109 p_data[i + 2] = i_pid & 0xff;
1113 p_data[i + 3] = (i_size + 1) >> 8;
1114 p_data[i + 4] = (i_size + 1) & 0xff;
1115 p_data[i + 5] = i_cmd;
1118 while ( p_dr != NULL )
1120 if( p_dr->i_tag == 0x9 )
1122 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1124 if ( CheckSystemID( p_ids, i_sysid ) )
1127 p_data[i + 1] = p_dr->i_length;
1128 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1129 i += p_dr->i_length + 2;
1132 p_dr = p_dr->p_next;
1144 static uint8_t *CAPMTBuild( access_t * p_access, int i_session_id,
1145 dvbpsi_pmt_t *p_pmt, uint8_t i_list_mgt,
1146 uint8_t i_cmd, int *pi_capmt_size )
1148 access_sys_t *p_sys = p_access->p_sys;
1149 system_ids_t *p_ids =
1150 (system_ids_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1151 dvbpsi_pmt_es_t *p_es;
1152 int i_cad_size, i_cad_program_size;
1155 i_cad_size = i_cad_program_size =
1156 GetCADSize( p_ids, p_pmt->p_first_descriptor );
1157 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1159 i_cad_size += GetCADSize( p_ids, p_es->p_first_descriptor );
1165 "no compatible scrambling system for SID %d on session %d",
1166 p_pmt->i_program_number, i_session_id );
1171 p_capmt = CAPMTHeader( p_ids, i_list_mgt, p_pmt->i_program_number,
1172 p_pmt->i_version, i_cad_program_size,
1173 p_pmt->p_first_descriptor, i_cmd );
1175 if ( i_cad_program_size )
1176 *pi_capmt_size = 7 + i_cad_program_size;
1180 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1182 i_cad_size = GetCADSize( p_ids, p_es->p_first_descriptor );
1184 if ( i_cad_size || i_cad_program_size )
1186 p_capmt = CAPMTES( p_ids, p_capmt, *pi_capmt_size, p_es->i_type,
1187 p_es->i_pid, i_cad_size,
1188 p_es->p_first_descriptor, i_cmd );
1190 *pi_capmt_size += 6 + i_cad_size;
1192 *pi_capmt_size += 5;
1199 /*****************************************************************************
1201 *****************************************************************************/
1202 static void CAPMTFirst( access_t * p_access, int i_session_id,
1203 dvbpsi_pmt_t *p_pmt )
1208 msg_Dbg( p_access, "adding first CAPMT for SID %d on session %d",
1209 p_pmt->i_program_number, i_session_id );
1211 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1212 0x3 /* only */, 0x1 /* ok_descrambling */,
1217 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1222 /*****************************************************************************
1224 *****************************************************************************/
1225 static void CAPMTAdd( access_t * p_access, int i_session_id,
1226 dvbpsi_pmt_t *p_pmt )
1231 if( p_access->p_sys->i_selected_programs >= CAM_PROG_MAX )
1233 msg_Warn( p_access, "Not adding CAPMT for SID %d, too many programs",
1234 p_pmt->i_program_number );
1237 p_access->p_sys->i_selected_programs++;
1238 if( p_access->p_sys->i_selected_programs == 1 )
1240 CAPMTFirst( p_access, i_session_id, p_pmt );
1245 msleep( CAPMT_WAIT * 1000 );
1248 msg_Dbg( p_access, "adding CAPMT for SID %d on session %d",
1249 p_pmt->i_program_number, i_session_id );
1251 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1252 0x4 /* add */, 0x1 /* ok_descrambling */,
1257 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1262 /*****************************************************************************
1264 *****************************************************************************/
1265 static void CAPMTUpdate( access_t * p_access, int i_session_id,
1266 dvbpsi_pmt_t *p_pmt )
1271 msg_Dbg( p_access, "updating CAPMT for SID %d on session %d",
1272 p_pmt->i_program_number, i_session_id );
1274 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1275 0x5 /* update */, 0x1 /* ok_descrambling */,
1280 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1285 /*****************************************************************************
1287 *****************************************************************************/
1288 static void CAPMTDelete( access_t * p_access, int i_session_id,
1289 dvbpsi_pmt_t *p_pmt )
1294 p_access->p_sys->i_selected_programs--;
1295 msg_Dbg( p_access, "deleting CAPMT for SID %d on session %d",
1296 p_pmt->i_program_number, i_session_id );
1298 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1299 0x5 /* update */, 0x4 /* not selected */,
1304 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1309 /*****************************************************************************
1310 * ConditionalAccessHandle
1311 *****************************************************************************/
1312 static void ConditionalAccessHandle( access_t * p_access, int i_session_id,
1313 uint8_t *p_apdu, int i_size )
1315 access_sys_t *p_sys = p_access->p_sys;
1316 system_ids_t *p_ids =
1317 (system_ids_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1318 int i_tag = APDUGetTag( p_apdu, i_size );
1326 uint8_t *d = APDUGetLength( p_apdu, &l );
1327 msg_Dbg( p_access, "CA system IDs supported by the application :" );
1329 for ( i = 0; i < l / 2; i++ )
1331 p_ids->pi_system_ids[i] = ((uint16_t)d[0] << 8) | d[1];
1333 msg_Dbg( p_access, "- 0x%x", p_ids->pi_system_ids[i] );
1335 p_ids->pi_system_ids[i] = 0;
1337 for ( i = 0; i < MAX_PROGRAMS; i++ )
1339 if ( p_sys->pp_selected_programs[i] != NULL )
1341 CAPMTAdd( p_access, i_session_id,
1342 p_sys->pp_selected_programs[i] );
1350 "unexpected tag in ConditionalAccessHandle (0x%x)",
1355 /*****************************************************************************
1356 * ConditionalAccessClose
1357 *****************************************************************************/
1358 static void ConditionalAccessClose( access_t * p_access, int i_session_id )
1360 access_sys_t *p_sys = p_access->p_sys;
1362 msg_Dbg( p_access, "closing ConditionalAccess session (%d)", i_session_id );
1364 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1367 /*****************************************************************************
1368 * ConditionalAccessOpen
1369 *****************************************************************************/
1370 static void ConditionalAccessOpen( access_t * p_access, int i_session_id )
1372 access_sys_t *p_sys = p_access->p_sys;
1374 msg_Dbg( p_access, "opening ConditionalAccess session (%d)", i_session_id );
1376 p_sys->p_sessions[i_session_id - 1].pf_handle = ConditionalAccessHandle;
1377 p_sys->p_sessions[i_session_id - 1].pf_close = ConditionalAccessClose;
1378 p_sys->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(system_ids_t) );
1380 APDUSend( p_access, i_session_id, AOT_CA_INFO_ENQ, NULL, 0 );
1393 /*****************************************************************************
1395 *****************************************************************************/
1396 static void DateTimeSend( access_t * p_access, int i_session_id )
1398 access_sys_t *p_sys = p_access->p_sys;
1399 date_time_t *p_date =
1400 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1402 time_t t = time(NULL);
1406 if ( gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc) )
1408 int Y = tm_gmt.tm_year;
1409 int M = tm_gmt.tm_mon + 1;
1410 int D = tm_gmt.tm_mday;
1411 int L = (M == 1 || M == 2) ? 1 : 0;
1412 int MJD = 14956 + D + (int)((Y - L) * 365.25)
1413 + (int)((M + 1 + L * 12) * 30.6001);
1414 uint8_t p_response[7];
1416 #define DEC2BCD(d) (((d / 10) << 4) + (d % 10))
1418 p_response[0] = htons(MJD) >> 8;
1419 p_response[1] = htons(MJD) & 0xff;
1420 p_response[2] = DEC2BCD(tm_gmt.tm_hour);
1421 p_response[3] = DEC2BCD(tm_gmt.tm_min);
1422 p_response[4] = DEC2BCD(tm_gmt.tm_sec);
1423 p_response[5] = htons(tm_loc.tm_gmtoff / 60) >> 8;
1424 p_response[6] = htons(tm_loc.tm_gmtoff / 60) & 0xff;
1426 APDUSend( p_access, i_session_id, AOT_DATE_TIME, p_response, 7 );
1428 p_date->i_last = mdate();
1432 /*****************************************************************************
1434 *****************************************************************************/
1435 static void DateTimeHandle( access_t * p_access, int i_session_id,
1436 uint8_t *p_apdu, int i_size )
1438 access_sys_t *p_sys = p_access->p_sys;
1439 date_time_t *p_date =
1440 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1442 int i_tag = APDUGetTag( p_apdu, i_size );
1446 case AOT_DATE_TIME_ENQ:
1449 const uint8_t *d = APDUGetLength( p_apdu, &l );
1453 p_date->i_interval = *d;
1454 msg_Dbg( p_access, "DateTimeHandle : interval set to %d",
1455 p_date->i_interval );
1458 p_date->i_interval = 0;
1460 DateTimeSend( p_access, i_session_id );
1464 msg_Err( p_access, "unexpected tag in DateTimeHandle (0x%x)", i_tag );
1468 /*****************************************************************************
1470 *****************************************************************************/
1471 static void DateTimeManage( access_t * p_access, int i_session_id )
1473 access_sys_t *p_sys = p_access->p_sys;
1474 date_time_t *p_date =
1475 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1477 if ( p_date->i_interval
1478 && mdate() > p_date->i_last + (mtime_t)p_date->i_interval * 1000000 )
1480 DateTimeSend( p_access, i_session_id );
1484 /*****************************************************************************
1486 *****************************************************************************/
1487 static void DateTimeClose( access_t * p_access, int i_session_id )
1489 access_sys_t *p_sys = p_access->p_sys;
1491 msg_Dbg( p_access, "closing DateTime session (%d)", i_session_id );
1493 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1496 /*****************************************************************************
1498 *****************************************************************************/
1499 static void DateTimeOpen( access_t * p_access, int i_session_id )
1501 access_sys_t *p_sys = p_access->p_sys;
1503 msg_Dbg( p_access, "opening DateTime session (%d)", i_session_id );
1505 p_sys->p_sessions[i_session_id - 1].pf_handle = DateTimeHandle;
1506 p_sys->p_sessions[i_session_id - 1].pf_manage = DateTimeManage;
1507 p_sys->p_sessions[i_session_id - 1].pf_close = DateTimeClose;
1508 p_sys->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(date_time_t) );
1510 DateTimeSend( p_access, i_session_id );
1517 /* Display Control Commands */
1519 #define DCC_SET_MMI_MODE 0x01
1520 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1521 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1522 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1523 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1527 #define MM_HIGH_LEVEL 0x01
1528 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1529 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1531 /* Display Reply IDs */
1533 #define DRI_MMI_MODE_ACK 0x01
1534 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1535 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1536 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1537 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1538 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1539 #define DRI_UNKNOWN_MMI_MODE 0xF1
1540 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1544 #define EF_BLIND 0x01
1548 #define AI_CANCEL 0x00
1549 #define AI_ANSWER 0x01
1553 en50221_mmi_object_t last_object;
1556 /*****************************************************************************
1558 *****************************************************************************/
1559 static void MMISendObject( access_t *p_access, int i_session_id,
1560 en50221_mmi_object_t *p_object )
1562 access_sys_t *p_sys = p_access->p_sys;
1563 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1567 switch ( p_object->i_object_type )
1569 case EN50221_MMI_ANSW:
1571 i_size = 1 + strlen( p_object->u.answ.psz_answ );
1572 p_data = xmalloc( i_size );
1573 p_data[0] = (p_object->u.answ.b_ok == true) ? 0x1 : 0x0;
1574 strncpy( (char *)&p_data[1], p_object->u.answ.psz_answ, i_size - 1 );
1577 case EN50221_MMI_MENU_ANSW:
1578 i_tag = AOT_MENU_ANSW;
1580 p_data = xmalloc( i_size );
1581 p_data[0] = p_object->u.menu_answ.i_choice;
1585 msg_Err( p_access, "unknown MMI object %d", p_object->i_object_type );
1589 APDUSend( p_access, i_session_id, i_tag, p_data, i_size );
1592 p_sys->pb_slot_mmi_expected[i_slot] = true;
1595 /*****************************************************************************
1597 *****************************************************************************/
1598 static void MMISendClose( access_t *p_access, int i_session_id )
1600 access_sys_t *p_sys = p_access->p_sys;
1601 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1603 APDUSend( p_access, i_session_id, AOT_CLOSE_MMI, NULL, 0 );
1605 p_sys->pb_slot_mmi_expected[i_slot] = true;
1608 /*****************************************************************************
1610 *****************************************************************************/
1611 static void MMIDisplayReply( access_t *p_access, int i_session_id )
1613 uint8_t p_response[2];
1615 p_response[0] = DRI_MMI_MODE_ACK;
1616 p_response[1] = MM_HIGH_LEVEL;
1618 APDUSend( p_access, i_session_id, AOT_DISPLAY_REPLY, p_response, 2 );
1620 msg_Dbg( p_access, "sending DisplayReply on session (%d)", i_session_id );
1623 /*****************************************************************************
1625 *****************************************************************************/
1626 static char *MMIGetText( access_t *p_access, uint8_t **pp_apdu, int *pi_size )
1628 int i_tag = APDUGetTag( *pp_apdu, *pi_size );
1632 if ( i_tag != AOT_TEXT_LAST )
1634 msg_Err( p_access, "unexpected text tag: %06x", i_tag );
1636 return strdup( "" );
1639 d = APDUGetLength( *pp_apdu, &l );
1644 return dvbsi_to_utf8((char*)d,l);
1647 /*****************************************************************************
1649 *****************************************************************************/
1650 static void MMIHandleEnq( access_t *p_access, int i_session_id,
1651 uint8_t *p_apdu, int i_size )
1653 VLC_UNUSED( i_size );
1655 access_sys_t *p_sys = p_access->p_sys;
1656 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1657 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1659 uint8_t *d = APDUGetLength( p_apdu, &l );
1661 en50221_MMIFree( &p_mmi->last_object );
1662 p_mmi->last_object.i_object_type = EN50221_MMI_ENQ;
1663 p_mmi->last_object.u.enq.b_blind = (*d & 0x1) ? true : false;
1664 d += 2; /* skip answer_text_length because it is not mandatory */
1666 p_mmi->last_object.u.enq.psz_text = xmalloc( l + 1 );
1667 strncpy( p_mmi->last_object.u.enq.psz_text, (char *)d, l );
1668 p_mmi->last_object.u.enq.psz_text[l] = '\0';
1670 msg_Dbg( p_access, "MMI enq: %s%s", p_mmi->last_object.u.enq.psz_text,
1671 p_mmi->last_object.u.enq.b_blind == true ? " (blind)" : "" );
1672 p_sys->pb_slot_mmi_expected[i_slot] = false;
1673 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1676 /*****************************************************************************
1678 *****************************************************************************/
1679 static void MMIHandleMenu( access_t *p_access, int i_session_id, int i_tag,
1680 uint8_t *p_apdu, int i_size )
1683 access_sys_t *p_sys = p_access->p_sys;
1684 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1685 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1687 uint8_t *d = APDUGetLength( p_apdu, &l );
1689 en50221_MMIFree( &p_mmi->last_object );
1690 p_mmi->last_object.i_object_type = (i_tag == AOT_MENU_LAST) ?
1691 EN50221_MMI_MENU : EN50221_MMI_LIST;
1692 p_mmi->last_object.u.menu.i_choices = 0;
1693 p_mmi->last_object.u.menu.ppsz_choices = NULL;
1697 l--; d++; /* choice_nb */
1699 #define GET_FIELD( x ) \
1702 p_mmi->last_object.u.menu.psz_##x \
1703 = MMIGetText( p_access, &d, &l ); \
1704 msg_Dbg( p_access, "MMI " STRINGIFY( x ) ": %s", \
1705 p_mmi->last_object.u.menu.psz_##x ); \
1709 GET_FIELD( subtitle );
1710 GET_FIELD( bottom );
1715 char *psz_text = MMIGetText( p_access, &d, &l );
1716 TAB_APPEND( p_mmi->last_object.u.menu.i_choices,
1717 p_mmi->last_object.u.menu.ppsz_choices,
1719 msg_Dbg( p_access, "MMI choice: %s", psz_text );
1722 p_sys->pb_slot_mmi_expected[i_slot] = false;
1723 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1726 /*****************************************************************************
1728 *****************************************************************************/
1729 static void MMIHandle( access_t *p_access, int i_session_id,
1730 uint8_t *p_apdu, int i_size )
1732 int i_tag = APDUGetTag( p_apdu, i_size );
1736 case AOT_DISPLAY_CONTROL:
1739 uint8_t *d = APDUGetLength( p_apdu, &l );
1745 case DCC_SET_MMI_MODE:
1746 if ( l == 2 && d[1] == MM_HIGH_LEVEL )
1747 MMIDisplayReply( p_access, i_session_id );
1749 msg_Err( p_access, "unsupported MMI mode %02x", d[1] );
1753 msg_Err( p_access, "unsupported display control command %02x",
1762 MMIHandleEnq( p_access, i_session_id, p_apdu, i_size );
1767 MMIHandleMenu( p_access, i_session_id, i_tag, p_apdu, i_size );
1771 SessionSendClose( p_access, i_session_id );
1775 msg_Err( p_access, "unexpected tag in MMIHandle (0x%x)", i_tag );
1779 /*****************************************************************************
1781 *****************************************************************************/
1782 static void MMIClose( access_t *p_access, int i_session_id )
1784 access_sys_t *p_sys = p_access->p_sys;
1785 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1786 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1788 en50221_MMIFree( &p_mmi->last_object );
1789 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1791 msg_Dbg( p_access, "closing MMI session (%d)", i_session_id );
1792 p_sys->pb_slot_mmi_expected[i_slot] = false;
1793 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1796 /*****************************************************************************
1798 *****************************************************************************/
1799 static void MMIOpen( access_t *p_access, int i_session_id )
1801 access_sys_t *p_sys = p_access->p_sys;
1804 msg_Dbg( p_access, "opening MMI session (%d)", i_session_id );
1806 p_sys->p_sessions[i_session_id - 1].pf_handle = MMIHandle;
1807 p_sys->p_sessions[i_session_id - 1].pf_close = MMIClose;
1808 p_sys->p_sessions[i_session_id - 1].p_sys = xmalloc(sizeof(mmi_t));
1809 p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1810 p_mmi->last_object.i_object_type = EN50221_MMI_NONE;
1818 /*****************************************************************************
1819 * InitSlot: Open the transport layer
1820 *****************************************************************************/
1821 #define MAX_TC_RETRIES 20
1823 static int InitSlot( access_t * p_access, int i_slot )
1825 access_sys_t *p_sys = p_access->p_sys;
1828 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, 0 )
1831 msg_Err( p_access, "en50221_Init: couldn't send TPDU on slot %d",
1833 return VLC_EGENERIC;
1836 /* This is out of the spec */
1837 for ( i = 0; i < MAX_TC_RETRIES; i++ )
1840 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) == VLC_SUCCESS
1841 && i_tag == T_CTC_REPLY )
1843 p_sys->pb_active_slot[i_slot] = true;
1847 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, 0 )
1851 "en50221_Init: couldn't send TPDU on slot %d",
1857 if ( p_sys->pb_active_slot[i_slot] )
1859 p_sys->i_ca_timeout = 100000;
1863 return VLC_EGENERIC;
1868 * External entry points
1871 /*****************************************************************************
1872 * en50221_Init : Initialize the CAM for en50221
1873 *****************************************************************************/
1874 int en50221_Init( access_t * p_access )
1876 access_sys_t *p_sys = p_access->p_sys;
1878 if( p_sys->i_ca_type & CA_CI_LINK )
1881 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1883 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1885 msg_Err( p_access, "en50221_Init: couldn't reset slot %d",
1890 p_sys->i_ca_timeout = 100000;
1891 /* Wait a bit otherwise it doesn't initialize properly... */
1898 struct ca_slot_info info;
1901 /* We don't reset the CAM in that case because it's done by the
1903 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &info ) < 0 )
1905 msg_Err( p_access, "en50221_Init: couldn't get slot info" );
1906 close( p_sys->i_ca_handle );
1907 p_sys->i_ca_handle = 0;
1908 return VLC_EGENERIC;
1910 if( info.flags == 0 )
1912 msg_Err( p_access, "en50221_Init: no CAM inserted" );
1913 close( p_sys->i_ca_handle );
1914 p_sys->i_ca_handle = 0;
1915 return VLC_EGENERIC;
1918 /* Allocate a dummy sessions */
1919 p_sys->p_sessions[ 0 ].i_resource_id = RI_CONDITIONAL_ACCESS_SUPPORT;
1921 /* Get application info to find out which cam we are using and make
1922 sure everything is ready to play */
1925 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1926 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1927 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1928 memset( &ca_msg.msg[3], 0, 253 );
1929 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1930 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1932 msg_Err( p_access, "en50221_Init: failed getting message" );
1933 return VLC_EGENERIC;
1936 #if HLCI_WAIT_CAM_READY
1937 while( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1939 if( !vlc_object_alive (p_access) ) return VLC_EGENERIC;
1941 msg_Dbg( p_access, "CAM: please wait" );
1942 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1944 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1945 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1946 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1947 memset( &ca_msg.msg[3], 0, 253 );
1948 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1950 msg_Err( p_access, "en50221_Init: failed getting message" );
1951 return VLC_EGENERIC;
1953 msg_Dbg( p_access, "en50221_Init: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
1956 if( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1958 msg_Err( p_access, "CAM returns garbage as application info!" );
1959 return VLC_EGENERIC;
1962 msg_Dbg( p_access, "found CAM %s using id 0x%x", &ca_msg.msg[12],
1963 (ca_msg.msg[8]<<8)|ca_msg.msg[9] );
1968 /*****************************************************************************
1969 * en50221_Poll : Poll the CAM for TPDUs
1970 *****************************************************************************/
1971 int en50221_Poll( access_t * p_access )
1973 access_sys_t *p_sys = p_access->p_sys;
1977 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1980 ca_slot_info_t sinfo;
1983 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1985 msg_Err( p_access, "en50221_Poll: couldn't get info on slot %d",
1990 if ( !(sinfo.flags & CA_CI_MODULE_READY) )
1992 if ( p_sys->pb_active_slot[i_slot] )
1994 msg_Dbg( p_access, "en50221_Poll: slot %d has been removed",
1996 p_sys->pb_active_slot[i_slot] = false;
1997 p_sys->pb_slot_mmi_expected[i_slot] = false;
1998 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
2000 /* Close all sessions for this slot. */
2001 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS;
2004 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2005 && p_sys->p_sessions[i_session_id - 1].i_slot
2008 if ( p_sys->p_sessions[i_session_id - 1].pf_close
2011 p_sys->p_sessions[i_session_id - 1].pf_close(
2012 p_access, i_session_id );
2014 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
2021 else if ( !p_sys->pb_active_slot[i_slot] )
2023 InitSlot( p_access, i_slot );
2025 if ( !p_sys->pb_active_slot[i_slot] )
2027 msg_Dbg( p_access, "en50221_Poll: resetting slot %d", i_slot );
2029 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
2031 msg_Err( p_access, "en50221_Poll: couldn't reset slot %d",
2037 msg_Dbg( p_access, "en50221_Poll: slot %d is active",
2041 if ( !p_sys->pb_tc_has_data[i_slot] )
2043 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, NULL, 0 ) !=
2047 "en50221_Poll: couldn't send TPDU on slot %d",
2051 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) !=
2055 "en50221_Poll: couldn't recv TPDU on slot %d",
2061 while ( p_sys->pb_tc_has_data[i_slot] )
2063 uint8_t p_tpdu[MAX_TPDU_SIZE];
2064 int i_size, i_session_size;
2067 if ( TPDUSend( p_access, i_slot, T_RCV, NULL, 0 ) != VLC_SUCCESS )
2070 "en50221_Poll: couldn't send TPDU on slot %d",
2074 if ( TPDURecv( p_access, i_slot, &i_tag, p_tpdu, &i_size ) !=
2078 "en50221_Poll: couldn't recv TPDU on slot %d",
2083 p_session = GetLength( &p_tpdu[3], &i_session_size );
2084 if ( i_session_size <= 1 )
2090 if ( i_tag != T_DATA_LAST )
2093 "en50221_Poll: fragmented TPDU not supported" );
2097 SPDUHandle( p_access, i_slot, p_session, i_session_size );
2101 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2103 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2104 && p_sys->p_sessions[i_session_id - 1].pf_manage )
2106 p_sys->p_sessions[i_session_id - 1].pf_manage( p_access,
2115 /*****************************************************************************
2116 * en50221_SetCAPMT :
2117 *****************************************************************************/
2118 int en50221_SetCAPMT( access_t * p_access, dvbpsi_pmt_t *p_pmt )
2120 access_sys_t *p_sys = p_access->p_sys;
2121 int i, i_session_id;
2122 bool b_update = false;
2123 bool b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
2125 for ( i = 0; i < MAX_PROGRAMS; i++ )
2127 if ( p_sys->pp_selected_programs[i] != NULL
2128 && p_sys->pp_selected_programs[i]->i_program_number
2129 == p_pmt->i_program_number )
2133 if ( !b_needs_descrambling )
2135 dvbpsi_DeletePMT( p_pmt );
2136 p_pmt = p_sys->pp_selected_programs[i];
2137 p_sys->pp_selected_programs[i] = NULL;
2139 else if( p_pmt != p_sys->pp_selected_programs[i] )
2141 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2142 p_sys->pp_selected_programs[i] = p_pmt;
2149 if ( !b_update && b_needs_descrambling )
2151 for ( i = 0; i < MAX_PROGRAMS; i++ )
2153 if ( p_sys->pp_selected_programs[i] == NULL )
2155 p_sys->pp_selected_programs[i] = p_pmt;
2161 if ( b_update || b_needs_descrambling )
2163 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2165 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2166 == RI_CONDITIONAL_ACCESS_SUPPORT )
2168 if ( b_update && b_needs_descrambling )
2169 CAPMTUpdate( p_access, i_session_id, p_pmt );
2170 else if ( b_update )
2171 CAPMTDelete( p_access, i_session_id, p_pmt );
2173 CAPMTAdd( p_access, i_session_id, p_pmt );
2178 if ( !b_needs_descrambling )
2180 dvbpsi_DeletePMT( p_pmt );
2186 /*****************************************************************************
2188 *****************************************************************************/
2189 int en50221_OpenMMI( access_t * p_access, int i_slot )
2191 access_sys_t *p_sys = p_access->p_sys;
2193 if( p_sys->i_ca_type & CA_CI_LINK )
2196 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2198 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2199 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2202 "MMI menu is already opened on slot %d (session=%d)",
2203 i_slot, i_session_id );
2208 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2210 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2211 == RI_APPLICATION_INFORMATION
2212 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2214 ApplicationInformationEnterMenu( p_access, i_session_id );
2219 msg_Err( p_access, "no application information on slot %d", i_slot );
2220 return VLC_EGENERIC;
2224 msg_Err( p_access, "MMI menu not supported" );
2225 return VLC_EGENERIC;
2229 /*****************************************************************************
2230 * en50221_CloseMMI :
2231 *****************************************************************************/
2232 int en50221_CloseMMI( access_t * p_access, int i_slot )
2234 access_sys_t *p_sys = p_access->p_sys;
2236 if( p_sys->i_ca_type & CA_CI_LINK )
2239 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2241 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2242 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2244 MMISendClose( p_access, i_session_id );
2249 msg_Warn( p_access, "closing a non-existing MMI session on slot %d",
2251 return VLC_EGENERIC;
2255 msg_Err( p_access, "MMI menu not supported" );
2256 return VLC_EGENERIC;
2260 /*****************************************************************************
2261 * en50221_GetMMIObject :
2262 *****************************************************************************/
2263 en50221_mmi_object_t *en50221_GetMMIObject( access_t * p_access,
2266 access_sys_t *p_sys = p_access->p_sys;
2269 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
2270 return NULL; /* should not happen */
2272 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2274 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2275 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2278 (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
2279 if ( p_mmi == NULL )
2280 return NULL; /* should not happen */
2281 return &p_mmi->last_object;
2289 /*****************************************************************************
2290 * en50221_SendMMIObject :
2291 *****************************************************************************/
2292 void en50221_SendMMIObject( access_t * p_access, int i_slot,
2293 en50221_mmi_object_t *p_object )
2295 access_sys_t *p_sys = p_access->p_sys;
2298 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2300 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2301 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2303 MMISendObject( p_access, i_session_id, p_object );
2308 msg_Err( p_access, "SendMMIObject when no MMI session is opened !" );
2311 /*****************************************************************************
2313 *****************************************************************************/
2314 void en50221_End( access_t * p_access )
2316 access_sys_t *p_sys = p_access->p_sys;
2317 int i_session_id, i;
2319 for ( i = 0; i < MAX_PROGRAMS; i++ )
2321 if ( p_sys->pp_selected_programs[i] != NULL )
2323 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2327 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2329 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2330 && p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
2332 p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
2337 /* Leave the CAM configured, so that it can be reused in another
2341 static inline void *FixUTF8( char *p )
2347 char *dvbsi_to_utf8( const char *psz_instring, size_t i_length )
2349 const char *psz_encoding, *psz_stringstart;
2350 char *psz_outstring, *psz_tmp;
2351 char psz_encbuf[12];
2353 vlc_iconv_t iconv_handle;
2354 if( i_length < 1 ) return NULL;
2355 if( psz_instring[0] < 0 || psz_instring[0] >= 0x20 )
2357 psz_stringstart = psz_instring;
2358 psz_encoding = "ISO_8859-1"; /* should be ISO6937 according to spec, but this seems to be the one used */
2359 } else switch( psz_instring[0] )
2362 psz_stringstart = &psz_instring[1];
2363 psz_encoding = "ISO_8859-5";
2366 psz_stringstart = &psz_instring[1];
2367 psz_encoding = "ISO_8859-6";
2370 psz_stringstart = &psz_instring[1];
2371 psz_encoding = "ISO_8859-7";
2374 psz_stringstart = &psz_instring[1];
2375 psz_encoding = "ISO_8859-8";
2378 psz_stringstart = &psz_instring[1];
2379 psz_encoding = "ISO_8859-9";
2382 psz_stringstart = &psz_instring[1];
2383 psz_encoding = "ISO_8859-10";
2386 psz_stringstart = &psz_instring[1];
2387 psz_encoding = "ISO_8859-11";
2390 psz_stringstart = &psz_instring[1]; /*possibly reserved?*/
2391 psz_encoding = "ISO_8859-12";
2394 psz_stringstart = &psz_instring[1];
2395 psz_encoding = "ISO_8859-13";
2398 psz_stringstart = &psz_instring[1];
2399 psz_encoding = "ISO_8859-14";
2402 psz_stringstart = &psz_instring[1];
2403 psz_encoding = "ISO_8859-15";
2406 if( i_length < 3 || psz_instring[1] != '\0' || psz_instring[2] > 0x0f
2407 || psz_instring[2] == 0 )
2408 return FixUTF8(strndup(psz_instring,i_length));
2409 sprintf( psz_encbuf, "ISO_8859-%d", psz_instring[2] );
2410 psz_stringstart = &psz_instring[3];
2411 psz_encoding = psz_encbuf;
2414 psz_stringstart = &psz_instring[1];
2415 psz_encoding = "UTF-16";
2418 psz_stringstart = &psz_instring[1];
2419 psz_encoding = "KSC5601-1987";
2422 psz_stringstart = &psz_instring[1];
2423 psz_encoding = "GB2312";/*GB-2312-1980 */
2426 psz_stringstart = &psz_instring[1];
2427 psz_encoding = "BIG-5";
2430 return FixUTF8(strndup(&psz_instring[1],i_length-1));
2434 return FixUTF8(strndup(psz_instring,i_length));
2436 iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
2437 i_in = i_length - (psz_stringstart - psz_instring );
2439 psz_outstring = psz_tmp = (char*)xmalloc( i_out + 1 );
2440 vlc_iconv( iconv_handle, &psz_stringstart, &i_in, &psz_tmp, &i_out );
2441 vlc_iconv_close( iconv_handle );
2443 return psz_outstring;