1 /*****************************************************************************
2 * en50221.c : implementation of the transport, session and applications
4 *****************************************************************************
5 * Copyright (C) 2004-2005 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_charset.h>
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
38 #include <netinet/in.h>
40 /* DVB Card Drivers */
41 #include <linux/dvb/ca.h>
43 /* Include dvbpsi headers */
44 # include <dvbpsi/dvbpsi.h>
45 # include <dvbpsi/descriptor.h>
46 # include <dvbpsi/pat.h>
47 # include <dvbpsi/pmt.h>
48 # include <dvbpsi/dr.h>
49 # include <dvbpsi/psi.h>
50 # include <dvbpsi/demux.h>
51 # include <dvbpsi/sdt.h>
55 # include <vlc_httpd.h>
58 #include "../demux/dvb-text.h"
59 #include "dtv/en50221.h"
61 typedef struct en50221_session_t
65 void (* pf_handle)( cam_t *, int, uint8_t *, int );
66 void (* pf_close)( cam_t *, int );
67 void (* pf_manage)( cam_t *, int );
71 #define EN50221_MMI_NONE 0
72 #define EN50221_MMI_ENQ 1
73 #define EN50221_MMI_ANSW 2
74 #define EN50221_MMI_MENU 3
75 #define EN50221_MMI_MENU_ANSW 4
76 #define EN50221_MMI_LIST 5
78 typedef struct en50221_mmi_object_t
98 char *psz_title, *psz_subtitle, *psz_bottom;
101 } menu; /* menu and list are the same */
112 #define HLCI_WAIT_CAM_READY 0
113 #define CAM_PROG_MAX MAX_PROGRAMS
114 //#define CAPMT_WAIT 100 /* uncomment this for slow CAMs */
116 static void ResourceManagerOpen( cam_t *, unsigned i_session_id );
117 static void ApplicationInformationOpen( cam_t *, unsigned i_session_id );
118 static void ConditionalAccessOpen( cam_t *, unsigned i_session_id );
119 static void DateTimeOpen( cam_t *, unsigned i_session_id );
120 static void MMIOpen( cam_t *, unsigned i_session_id );
122 #define MAX_CI_SLOTS 16
123 #define MAX_SESSIONS 32
124 #define MAX_PROGRAMS 24
131 mtime_t i_timeout, i_next_event;
134 bool pb_active_slot[MAX_CI_SLOTS];
135 bool pb_tc_has_data[MAX_CI_SLOTS];
136 bool pb_slot_mmi_expected[MAX_CI_SLOTS];
137 bool pb_slot_mmi_undisplayed[MAX_CI_SLOTS];
138 en50221_session_t p_sessions[MAX_SESSIONS];
140 dvbpsi_pmt_t *pp_selected_programs[MAX_PROGRAMS];
141 int i_selected_programs;
144 /*****************************************************************************
146 *****************************************************************************/
147 #define SIZE_INDICATOR 0x80
149 static uint8_t *GetLength( uint8_t *p_data, int *pi_length )
151 *pi_length = *p_data++;
153 if ( (*pi_length & SIZE_INDICATOR) != 0 )
155 int l = *pi_length & ~SIZE_INDICATOR;
159 for ( i = 0; i < l; i++ )
160 *pi_length = (*pi_length << 8) | *p_data++;
166 static uint8_t *SetLength( uint8_t *p_data, int i_length )
170 if ( i_length < 128 )
174 else if ( i_length < 256 )
176 *p++ = SIZE_INDICATOR | 0x1;
179 else if ( i_length < 65536 )
181 *p++ = SIZE_INDICATOR | 0x2;
182 *p++ = i_length >> 8;
183 *p++ = i_length & 0xff;
185 else if ( i_length < 16777216 )
187 *p++ = SIZE_INDICATOR | 0x3;
188 *p++ = i_length >> 16;
189 *p++ = (i_length >> 8) & 0xff;
190 *p++ = i_length & 0xff;
194 *p++ = SIZE_INDICATOR | 0x4;
195 *p++ = i_length >> 24;
196 *p++ = (i_length >> 16) & 0xff;
197 *p++ = (i_length >> 8) & 0xff;
198 *p++ = i_length & 0xff;
209 #define MAX_TPDU_SIZE 4096
210 #define MAX_TPDU_DATA (MAX_TPDU_SIZE - 4)
212 #define DATA_INDICATOR 0x80
216 #define T_CREATE_TC 0x82
217 #define T_CTC_REPLY 0x83
218 #define T_DELETE_TC 0x84
219 #define T_DTC_REPLY 0x85
220 #define T_REQUEST_TC 0x86
221 #define T_NEW_TC 0x87
222 #define T_TC_ERROR 0x88
223 #define T_DATA_LAST 0xA0
224 #define T_DATA_MORE 0xA1
226 static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
231 fprintf(stderr, "%s ", b_outgoing ? "-->" : "<--");
232 for ( i = 0; i < i_size && i < MAX_DUMP; i++)
233 fprintf(stderr, "%02X ", p_data[i]);
234 fprintf(stderr, "%s\n", i_size >= MAX_DUMP ? "..." : "");
236 VLC_UNUSED(b_outgoing); VLC_UNUSED(p_data); VLC_UNUSED(i_size);
240 /*****************************************************************************
242 *****************************************************************************/
243 static int TPDUSend( cam_t * p_cam, uint8_t i_slot, uint8_t i_tag,
244 const uint8_t *p_content, int i_length )
246 uint8_t i_tcid = i_slot + 1;
247 uint8_t p_data[MAX_TPDU_SIZE];
263 p_data[3] = 1; /* length */
270 p_data[3] = 2; /* length */
272 p_data[5] = p_content[0];
279 /* i_length <= MAX_TPDU_DATA */
280 uint8_t *p = p_data + 3;
281 p = SetLength( p, i_length + 1 );
285 memcpy( p, p_content, i_length );
286 i_size = i_length + (p - p_data);
293 Dump( true, p_data, i_size );
295 if ( write( p_cam->fd, p_data, i_size ) != i_size )
297 msg_Err( p_cam->obj, "cannot write to CAM device (%m)" );
305 /*****************************************************************************
307 *****************************************************************************/
308 #define CAM_READ_TIMEOUT 3500 // ms
310 static int TPDURecv( cam_t *p_cam, uint8_t i_slot, uint8_t *pi_tag,
311 uint8_t *p_data, int *pi_size )
313 uint8_t i_tcid = i_slot + 1;
315 struct pollfd pfd[1];
317 pfd[0].fd = p_cam->fd;
318 pfd[0].events = POLLIN;
320 while( poll(pfd, 1, CAM_READ_TIMEOUT ) == -1 )
323 msg_Err( p_cam->obj, "poll error: %m" );
327 if ( !(pfd[0].revents & POLLIN) )
329 msg_Err( p_cam->obj, "CAM device poll time-out" );
333 if ( pi_size == NULL )
335 p_data = xmalloc( MAX_TPDU_SIZE );
340 i_size = read( p_cam->fd, p_data, MAX_TPDU_SIZE );
342 if ( i_size >= 0 || errno != EINTR )
348 msg_Err( p_cam->obj, "cannot read from CAM device (%d:%m)", i_size );
349 if( pi_size == NULL )
354 if ( p_data[1] != i_tcid )
356 msg_Err( p_cam->obj, "invalid read from CAM device (%d instead of %d)",
358 if( pi_size == NULL )
364 p_cam->pb_tc_has_data[i_slot] = (i_size >= 4
365 && p_data[i_size - 4] == T_SB
366 && p_data[i_size - 3] == 2
367 && (p_data[i_size - 1] & DATA_INDICATOR))
370 Dump( false, p_data, i_size );
372 if ( pi_size == NULL )
385 #define ST_SESSION_NUMBER 0x90
386 #define ST_OPEN_SESSION_REQUEST 0x91
387 #define ST_OPEN_SESSION_RESPONSE 0x92
388 #define ST_CREATE_SESSION 0x93
389 #define ST_CREATE_SESSION_RESPONSE 0x94
390 #define ST_CLOSE_SESSION_REQUEST 0x95
391 #define ST_CLOSE_SESSION_RESPONSE 0x96
394 #define SS_NOT_ALLOCATED 0xF0
396 #define RI_RESOURCE_MANAGER 0x00010041
397 #define RI_APPLICATION_INFORMATION 0x00020041
398 #define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
399 #define RI_HOST_CONTROL 0x00200041
400 #define RI_DATE_TIME 0x00240041
401 #define RI_MMI 0x00400041
403 static int ResourceIdToInt( uint8_t *p_data )
405 return ((int)p_data[0] << 24) | ((int)p_data[1] << 16)
406 | ((int)p_data[2] << 8) | p_data[3];
409 /*****************************************************************************
411 *****************************************************************************/
412 static int SPDUSend( cam_t * p_cam, int i_session_id,
413 uint8_t *p_data, int i_size )
415 uint8_t *p_spdu = xmalloc( i_size + 4 );
418 uint8_t i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
420 *p++ = ST_SESSION_NUMBER;
422 *p++ = (i_session_id >> 8);
423 *p++ = i_session_id & 0xff;
425 memcpy( p, p_data, i_size );
432 if ( i_size > MAX_TPDU_DATA )
434 if ( TPDUSend( p_cam, i_slot, T_DATA_MORE, p,
435 MAX_TPDU_DATA ) != VLC_SUCCESS )
437 msg_Err( p_cam->obj, "couldn't send TPDU on session %d",
443 i_size -= MAX_TPDU_DATA;
447 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, p, i_size )
450 msg_Err( p_cam->obj, "couldn't send TPDU on session %d",
458 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS
461 msg_Err( p_cam->obj, "couldn't recv TPDU on session %d",
472 /*****************************************************************************
474 *****************************************************************************/
475 static void SessionOpen( cam_t * p_cam, uint8_t i_slot,
476 uint8_t *p_spdu, int i_size )
478 VLC_UNUSED( i_size );
481 int i_resource_id = ResourceIdToInt( &p_spdu[2] );
482 uint8_t p_response[16];
483 int i_status = SS_NOT_ALLOCATED;
486 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
488 if ( !p_cam->p_sessions[i_session_id - 1].i_resource_id )
491 if ( i_session_id > MAX_SESSIONS )
493 msg_Err( p_cam->obj, "too many sessions !" );
496 p_cam->p_sessions[i_session_id - 1].i_slot = i_slot;
497 p_cam->p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
498 p_cam->p_sessions[i_session_id - 1].pf_close = NULL;
499 p_cam->p_sessions[i_session_id - 1].pf_manage = NULL;
501 if ( i_resource_id == RI_RESOURCE_MANAGER
502 || i_resource_id == RI_APPLICATION_INFORMATION
503 || i_resource_id == RI_CONDITIONAL_ACCESS_SUPPORT
504 || i_resource_id == RI_DATE_TIME
505 || i_resource_id == RI_MMI )
510 p_response[0] = ST_OPEN_SESSION_RESPONSE;
512 p_response[2] = i_status;
513 p_response[3] = p_spdu[2];
514 p_response[4] = p_spdu[3];
515 p_response[5] = p_spdu[4];
516 p_response[6] = p_spdu[5];
517 p_response[7] = i_session_id >> 8;
518 p_response[8] = i_session_id & 0xff;
520 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, p_response, 9 ) !=
524 "SessionOpen: couldn't send TPDU on slot %d", i_slot );
527 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
530 "SessionOpen: couldn't recv TPDU on slot %d", i_slot );
534 switch ( i_resource_id )
536 case RI_RESOURCE_MANAGER:
537 ResourceManagerOpen( p_cam, i_session_id ); break;
538 case RI_APPLICATION_INFORMATION:
539 ApplicationInformationOpen( p_cam, i_session_id ); break;
540 case RI_CONDITIONAL_ACCESS_SUPPORT:
541 ConditionalAccessOpen( p_cam, i_session_id ); break;
543 DateTimeOpen( p_cam, i_session_id ); break;
545 MMIOpen( p_cam, i_session_id ); break;
547 case RI_HOST_CONTROL:
549 msg_Err( p_cam->obj, "unknown resource id (0x%x)", i_resource_id );
550 p_cam->p_sessions[i_session_id - 1].i_resource_id = 0;
555 /* unused code for the moment - commented out to keep gcc happy */
556 /*****************************************************************************
558 *****************************************************************************/
559 static void SessionCreate( cam_t * p_cam, int i_slot, int i_resource_id )
561 uint8_t p_response[16];
565 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
567 if ( !p_cam->p_sessions[i_session_id - 1].i_resource_id )
570 if ( i_session_id == MAX_SESSIONS )
572 msg_Err( p_cam->obj, "too many sessions !" );
575 p_cam->p_sessions[i_session_id - 1].i_slot = i_slot;
576 p_cam->p_sessions[i_session_id - 1].i_resource_id = i_resource_id;
577 p_cam->p_sessions[i_session_id - 1].pf_close = NULL;
578 p_cam->p_sessions[i_session_id - 1].pf_manage = NULL;
579 p_cam->p_sessions[i_session_id - 1].p_sys = NULL;
581 p_response[0] = ST_CREATE_SESSION;
583 SetDWBE( &p_resource[2], i_resource_id );
584 SetWBE( &p_response[6]. i_session_id);
586 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, p_response, 4 ) !=
590 "SessionCreate: couldn't send TPDU on slot %d", i_slot );
593 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
596 "SessionCreate: couldn't recv TPDU on slot %d", i_slot );
602 /*****************************************************************************
603 * SessionCreateResponse
604 *****************************************************************************/
605 static void SessionCreateResponse( cam_t * p_cam, uint8_t i_slot,
606 uint8_t *p_spdu, int i_size )
608 VLC_UNUSED( i_size );
609 VLC_UNUSED( i_slot );
611 int i_status = p_spdu[2];
612 int i_resource_id = ResourceIdToInt( &p_spdu[3] );
613 int i_session_id = ((int)p_spdu[7] << 8) | p_spdu[8];
615 if ( i_status != SS_OK )
617 msg_Err( p_cam->obj, "SessionCreateResponse: failed to open session %d"
618 " resource=0x%x status=0x%x", i_session_id, i_resource_id,
620 p_cam->p_sessions[i_session_id - 1].i_resource_id = 0;
624 switch ( i_resource_id )
626 case RI_RESOURCE_MANAGER:
627 ResourceManagerOpen( p_cam, i_session_id ); break;
628 case RI_APPLICATION_INFORMATION:
629 ApplicationInformationOpen( p_cam, i_session_id ); break;
630 case RI_CONDITIONAL_ACCESS_SUPPORT:
631 ConditionalAccessOpen( p_cam, i_session_id ); break;
633 DateTimeOpen( p_cam, i_session_id ); break;
635 MMIOpen( p_cam, i_session_id ); break;
637 case RI_HOST_CONTROL:
639 msg_Err( p_cam->obj, "unknown resource id (0x%x)", i_resource_id );
640 p_cam->p_sessions[i_session_id - 1].i_resource_id = 0;
644 /*****************************************************************************
646 *****************************************************************************/
647 static void SessionSendClose( cam_t *p_cam, int i_session_id )
649 uint8_t p_response[16];
651 uint8_t i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
653 p_response[0] = ST_CLOSE_SESSION_REQUEST;
655 SetWBE( &p_response[2], i_session_id );
657 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, p_response, 4 ) !=
661 "SessionSendClose: couldn't send TPDU on slot %d", i_slot );
664 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
667 "SessionSendClose: couldn't recv TPDU on slot %d", i_slot );
672 /*****************************************************************************
674 *****************************************************************************/
675 static void SessionClose( cam_t * p_cam, int i_session_id )
677 uint8_t p_response[16];
679 uint8_t i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
681 if ( p_cam->p_sessions[i_session_id - 1].pf_close != NULL )
682 p_cam->p_sessions[i_session_id - 1].pf_close( p_cam, i_session_id );
683 p_cam->p_sessions[i_session_id - 1].i_resource_id = 0;
685 p_response[0] = ST_CLOSE_SESSION_RESPONSE;
687 p_response[2] = SS_OK;
688 p_response[3] = i_session_id >> 8;
689 p_response[4] = i_session_id & 0xff;
691 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, p_response, 5 ) !=
695 "SessionClose: couldn't send TPDU on slot %d", i_slot );
698 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
701 "SessionClose: couldn't recv TPDU on slot %d", i_slot );
706 /*****************************************************************************
708 *****************************************************************************/
709 static void SPDUHandle( cam_t * p_cam, uint8_t i_slot,
710 uint8_t *p_spdu, int i_size )
716 case ST_SESSION_NUMBER:
719 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
720 p_cam->p_sessions[i_session_id - 1].pf_handle( p_cam, i_session_id,
721 p_spdu + 4, i_size - 4 );
724 case ST_OPEN_SESSION_REQUEST:
725 if ( i_size != 6 || p_spdu[1] != 0x4 )
727 SessionOpen( p_cam, i_slot, p_spdu, i_size );
730 case ST_CREATE_SESSION_RESPONSE:
731 if ( i_size != 9 || p_spdu[1] != 0x7 )
733 SessionCreateResponse( p_cam, i_slot, p_spdu, i_size );
736 case ST_CLOSE_SESSION_REQUEST:
737 if ( i_size != 4 || p_spdu[1] != 0x2 )
739 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
740 SessionClose( p_cam, i_session_id );
743 case ST_CLOSE_SESSION_RESPONSE:
744 if ( i_size != 5 || p_spdu[1] != 0x3 )
746 i_session_id = ((int)p_spdu[3] << 8) | p_spdu[4];
750 "closing a session which is not allocated (%d)",
755 if ( p_cam->p_sessions[i_session_id - 1].pf_close != NULL )
756 p_cam->p_sessions[i_session_id - 1].pf_close( p_cam,
758 p_cam->p_sessions[i_session_id - 1].i_resource_id = 0;
763 msg_Err( p_cam->obj, "unexpected tag in SPDUHandle (%x)", p_spdu[0] );
773 #define AOT_NONE 0x000000
774 #define AOT_PROFILE_ENQ 0x9F8010
775 #define AOT_PROFILE 0x9F8011
776 #define AOT_PROFILE_CHANGE 0x9F8012
777 #define AOT_APPLICATION_INFO_ENQ 0x9F8020
778 #define AOT_APPLICATION_INFO 0x9F8021
779 #define AOT_ENTER_MENU 0x9F8022
780 #define AOT_CA_INFO_ENQ 0x9F8030
781 #define AOT_CA_INFO 0x9F8031
782 #define AOT_CA_PMT 0x9F8032
783 #define AOT_CA_PMT_REPLY 0x9F8033
784 #define AOT_TUNE 0x9F8400
785 #define AOT_REPLACE 0x9F8401
786 #define AOT_CLEAR_REPLACE 0x9F8402
787 #define AOT_ASK_RELEASE 0x9F8403
788 #define AOT_DATE_TIME_ENQ 0x9F8440
789 #define AOT_DATE_TIME 0x9F8441
790 #define AOT_CLOSE_MMI 0x9F8800
791 #define AOT_DISPLAY_CONTROL 0x9F8801
792 #define AOT_DISPLAY_REPLY 0x9F8802
793 #define AOT_TEXT_LAST 0x9F8803
794 #define AOT_TEXT_MORE 0x9F8804
795 #define AOT_KEYPAD_CONTROL 0x9F8805
796 #define AOT_KEYPRESS 0x9F8806
797 #define AOT_ENQ 0x9F8807
798 #define AOT_ANSW 0x9F8808
799 #define AOT_MENU_LAST 0x9F8809
800 #define AOT_MENU_MORE 0x9F880A
801 #define AOT_MENU_ANSW 0x9F880B
802 #define AOT_LIST_LAST 0x9F880C
803 #define AOT_LIST_MORE 0x9F880D
804 #define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
805 #define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
806 #define AOT_DISPLAY_MESSAGE 0x9F8810
807 #define AOT_SCENE_END_MARK 0x9F8811
808 #define AOT_SCENE_DONE 0x9F8812
809 #define AOT_SCENE_CONTROL 0x9F8813
810 #define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
811 #define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
812 #define AOT_FLUSH_DOWNLOAD 0x9F8816
813 #define AOT_DOWNLOAD_REPLY 0x9F8817
814 #define AOT_COMMS_CMD 0x9F8C00
815 #define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
816 #define AOT_COMMS_REPLY 0x9F8C02
817 #define AOT_COMMS_SEND_LAST 0x9F8C03
818 #define AOT_COMMS_SEND_MORE 0x9F8C04
819 #define AOT_COMMS_RCV_LAST 0x9F8C05
820 #define AOT_COMMS_RCV_MORE 0x9F8C06
822 /*****************************************************************************
824 *****************************************************************************/
825 static int APDUGetTag( const uint8_t *p_apdu, int i_size )
830 for ( i = 0; i < 3; i++ )
831 t = (t << 8) | *p_apdu++;
838 /*****************************************************************************
840 *****************************************************************************/
841 static uint8_t *APDUGetLength( uint8_t *p_apdu, int *pi_size )
843 return GetLength( &p_apdu[3], pi_size );
846 /*****************************************************************************
848 *****************************************************************************/
849 static int APDUSend( cam_t * p_cam, int i_session_id, int i_tag,
850 uint8_t *p_data, int i_size )
852 uint8_t *p_apdu = xmalloc( i_size + 12 );
857 *p++ = (i_tag >> 16);
858 *p++ = (i_tag >> 8) & 0xff;
860 p = SetLength( p, i_size );
862 memcpy( p, p_data, i_size );
863 if ( p_cam->i_ca_type == CA_CI_LINK )
865 i_ret = SPDUSend( p_cam, i_session_id, p_apdu, i_size + p - p_apdu );
869 if ( i_size + p - p_apdu > 256 )
871 msg_Err( p_cam->obj, "CAM: apdu overflow" );
872 i_ret = VLC_EGENERIC;
876 ca_msg.length = i_size + p - p_apdu;
877 if ( i_size == 0 ) ca_msg.length=3;
878 memcpy( ca_msg.msg, p_apdu, i_size + p - p_apdu );
879 i_ret = ioctl( p_cam->fd, CA_SEND_MSG, &ca_msg );
882 msg_Err( p_cam->obj, "Error sending to CAM: %m" );
883 i_ret = VLC_EGENERIC;
895 /*****************************************************************************
896 * ResourceManagerHandle
897 *****************************************************************************/
898 static void ResourceManagerHandle( cam_t * p_cam, int i_session_id,
899 uint8_t *p_apdu, int i_size )
901 int i_tag = APDUGetTag( p_apdu, i_size );
905 case AOT_PROFILE_ENQ:
907 int resources[] = { htonl(RI_RESOURCE_MANAGER),
908 htonl(RI_APPLICATION_INFORMATION),
909 htonl(RI_CONDITIONAL_ACCESS_SUPPORT),
913 APDUSend( p_cam, i_session_id, AOT_PROFILE, (uint8_t*)resources,
918 APDUSend( p_cam, i_session_id, AOT_PROFILE_CHANGE, NULL, 0 );
922 msg_Err( p_cam->obj, "unexpected tag in ResourceManagerHandle (0x%x)",
927 /*****************************************************************************
928 * ResourceManagerOpen
929 *****************************************************************************/
930 static void ResourceManagerOpen( cam_t * p_cam, unsigned i_session_id )
932 msg_Dbg( p_cam->obj, "opening ResourceManager session (%u)",
934 p_cam->p_sessions[i_session_id - 1].pf_handle = ResourceManagerHandle;
935 APDUSend( p_cam, i_session_id, AOT_PROFILE_ENQ, NULL, 0 );
939 * Application Information
943 /*****************************************************************************
944 * ApplicationInformationEnterMenu
945 *****************************************************************************/
946 static void ApplicationInformationEnterMenu( cam_t * p_cam, int i_session_id )
948 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
950 msg_Dbg( p_cam->obj, "entering MMI menus on session %d", i_session_id );
951 APDUSend( p_cam, i_session_id, AOT_ENTER_MENU, NULL, 0 );
952 p_cam->pb_slot_mmi_expected[i_slot] = true;
956 /*****************************************************************************
957 * ApplicationInformationHandle
958 *****************************************************************************/
959 static void ApplicationInformationHandle( cam_t * p_cam, int i_session_id,
960 uint8_t *p_apdu, int i_size )
962 VLC_UNUSED(i_session_id);
963 int i_tag = APDUGetTag( p_apdu, i_size );
967 case AOT_APPLICATION_INFO:
969 int i_type, i_manufacturer, i_code;
971 uint8_t *d = APDUGetLength( p_apdu, &l );
974 p_apdu[l + 4] = '\0';
977 i_manufacturer = ((int)d[0] << 8) | d[1];
979 i_code = ((int)d[0] << 8) | d[1];
981 d = GetLength( d, &l );
983 msg_Info( p_cam->obj, "CAM: %s, %02X, %04X, %04X",
984 d, i_type, i_manufacturer, i_code );
989 "unexpected tag in ApplicationInformationHandle (0x%x)",
994 /*****************************************************************************
995 * ApplicationInformationOpen
996 *****************************************************************************/
997 static void ApplicationInformationOpen( cam_t * p_cam, unsigned i_session_id )
999 msg_Dbg( p_cam->obj, "opening ApplicationInformation session (%u)",
1001 p_cam->p_sessions[i_session_id - 1].pf_handle =
1002 ApplicationInformationHandle;
1003 APDUSend( p_cam, i_session_id, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1007 * Conditional Access
1010 #define MAX_CASYSTEM_IDS 64
1014 uint16_t pi_system_ids[MAX_CASYSTEM_IDS + 1];
1017 static bool CheckSystemID( system_ids_t *p_ids, uint16_t i_id )
1020 if( !p_ids ) return true; /* dummy session for high-level CI intf */
1022 while ( p_ids->pi_system_ids[i] )
1024 if ( p_ids->pi_system_ids[i] == i_id )
1032 /*****************************************************************************
1033 * CAPMTNeedsDescrambling
1034 *****************************************************************************/
1035 static bool CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
1037 dvbpsi_descriptor_t *p_dr;
1038 dvbpsi_pmt_es_t *p_es;
1040 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1042 if( p_dr->i_tag == 0x9 )
1048 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1050 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
1051 p_dr = p_dr->p_next )
1053 if( p_dr->i_tag == 0x9 )
1063 /*****************************************************************************
1065 *****************************************************************************/
1066 static int GetCADSize( system_ids_t *p_ids, dvbpsi_descriptor_t *p_dr )
1070 while ( p_dr != NULL )
1072 if( p_dr->i_tag == 0x9 )
1074 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1076 if ( CheckSystemID( p_ids, i_sysid ) )
1077 i_cad_size += p_dr->i_length + 2;
1079 p_dr = p_dr->p_next;
1085 static uint8_t *CAPMTHeader( system_ids_t *p_ids, uint8_t i_list_mgt,
1086 uint16_t i_program_number, uint8_t i_version,
1087 int i_size, dvbpsi_descriptor_t *p_dr,
1093 p_data = xmalloc( 7 + i_size );
1095 p_data = xmalloc( 6 );
1097 p_data[0] = i_list_mgt;
1098 p_data[1] = i_program_number >> 8;
1099 p_data[2] = i_program_number & 0xff;
1100 p_data[3] = ((i_version & 0x1f) << 1) | 0x1;
1106 p_data[4] = (i_size + 1) >> 8;
1107 p_data[5] = (i_size + 1) & 0xff;
1111 while ( p_dr != NULL )
1113 if( p_dr->i_tag == 0x9 )
1115 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1117 if ( CheckSystemID( p_ids, i_sysid ) )
1120 p_data[i + 1] = p_dr->i_length;
1121 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1122 // p_data[i+4] &= 0x1f;
1123 i += p_dr->i_length + 2;
1126 p_dr = p_dr->p_next;
1138 static uint8_t *CAPMTES( system_ids_t *p_ids, uint8_t *p_capmt,
1139 int i_capmt_size, uint8_t i_type, uint16_t i_pid,
1140 int i_size, dvbpsi_descriptor_t *p_dr,
1147 p_data = xrealloc( p_capmt, i_capmt_size + 6 + i_size );
1149 p_data = xrealloc( p_capmt, i_capmt_size + 5 );
1154 p_data[i + 1] = i_pid >> 8;
1155 p_data[i + 2] = i_pid & 0xff;
1159 p_data[i + 3] = (i_size + 1) >> 8;
1160 p_data[i + 4] = (i_size + 1) & 0xff;
1161 p_data[i + 5] = i_cmd;
1164 while ( p_dr != NULL )
1166 if( p_dr->i_tag == 0x9 )
1168 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1170 if ( CheckSystemID( p_ids, i_sysid ) )
1173 p_data[i + 1] = p_dr->i_length;
1174 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1175 i += p_dr->i_length + 2;
1178 p_dr = p_dr->p_next;
1190 static uint8_t *CAPMTBuild( cam_t * p_cam, int i_session_id,
1191 dvbpsi_pmt_t *p_pmt, uint8_t i_list_mgt,
1192 uint8_t i_cmd, int *pi_capmt_size )
1194 system_ids_t *p_ids =
1195 (system_ids_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1196 dvbpsi_pmt_es_t *p_es;
1197 int i_cad_size, i_cad_program_size;
1200 i_cad_size = i_cad_program_size =
1201 GetCADSize( p_ids, p_pmt->p_first_descriptor );
1202 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1204 i_cad_size += GetCADSize( p_ids, p_es->p_first_descriptor );
1209 msg_Warn( p_cam->obj,
1210 "no compatible scrambling system for SID %d on session %d",
1211 p_pmt->i_program_number, i_session_id );
1216 p_capmt = CAPMTHeader( p_ids, i_list_mgt, p_pmt->i_program_number,
1217 p_pmt->i_version, i_cad_program_size,
1218 p_pmt->p_first_descriptor, i_cmd );
1220 if ( i_cad_program_size )
1221 *pi_capmt_size = 7 + i_cad_program_size;
1225 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1227 i_cad_size = GetCADSize( p_ids, p_es->p_first_descriptor );
1229 if ( i_cad_size || i_cad_program_size )
1231 p_capmt = CAPMTES( p_ids, p_capmt, *pi_capmt_size, p_es->i_type,
1232 p_es->i_pid, i_cad_size,
1233 p_es->p_first_descriptor, i_cmd );
1235 *pi_capmt_size += 6 + i_cad_size;
1237 *pi_capmt_size += 5;
1244 /*****************************************************************************
1246 *****************************************************************************/
1247 static void CAPMTFirst( cam_t * p_cam, int i_session_id,
1248 dvbpsi_pmt_t *p_pmt )
1253 msg_Dbg( p_cam->obj, "adding first CAPMT for SID %d on session %d",
1254 p_pmt->i_program_number, i_session_id );
1256 p_capmt = CAPMTBuild( p_cam, i_session_id, p_pmt,
1257 0x3 /* only */, 0x1 /* ok_descrambling */,
1262 APDUSend( p_cam, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1267 /*****************************************************************************
1269 *****************************************************************************/
1270 static void CAPMTAdd( cam_t * p_cam, int i_session_id,
1271 dvbpsi_pmt_t *p_pmt )
1276 if( p_cam->i_selected_programs >= CAM_PROG_MAX )
1278 msg_Warn( p_cam->obj, "Not adding CAPMT for SID %d, too many programs",
1279 p_pmt->i_program_number );
1282 p_cam->i_selected_programs++;
1283 if( p_cam->i_selected_programs == 1 )
1285 CAPMTFirst( p_cam, i_session_id, p_pmt );
1290 msleep( CAPMT_WAIT * 1000 );
1293 msg_Dbg( p_cam->obj, "adding CAPMT for SID %d on session %d",
1294 p_pmt->i_program_number, i_session_id );
1296 p_capmt = CAPMTBuild( p_cam, i_session_id, p_pmt,
1297 0x4 /* add */, 0x1 /* ok_descrambling */,
1302 APDUSend( p_cam, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1307 /*****************************************************************************
1309 *****************************************************************************/
1310 static void CAPMTUpdate( cam_t * p_cam, int i_session_id,
1311 dvbpsi_pmt_t *p_pmt )
1316 msg_Dbg( p_cam->obj, "updating CAPMT for SID %d on session %d",
1317 p_pmt->i_program_number, i_session_id );
1319 p_capmt = CAPMTBuild( p_cam, i_session_id, p_pmt,
1320 0x5 /* update */, 0x1 /* ok_descrambling */,
1325 APDUSend( p_cam, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1330 /*****************************************************************************
1332 *****************************************************************************/
1333 static void CAPMTDelete( cam_t * p_cam, int i_session_id,
1334 dvbpsi_pmt_t *p_pmt )
1339 p_cam->i_selected_programs--;
1340 msg_Dbg( p_cam->obj, "deleting CAPMT for SID %d on session %d",
1341 p_pmt->i_program_number, i_session_id );
1343 p_capmt = CAPMTBuild( p_cam, i_session_id, p_pmt,
1344 0x5 /* update */, 0x4 /* not selected */,
1349 APDUSend( p_cam, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1354 /*****************************************************************************
1355 * ConditionalAccessHandle
1356 *****************************************************************************/
1357 static void ConditionalAccessHandle( cam_t * p_cam, int i_session_id,
1358 uint8_t *p_apdu, int i_size )
1360 system_ids_t *p_ids =
1361 (system_ids_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1362 int i_tag = APDUGetTag( p_apdu, i_size );
1370 uint8_t *d = APDUGetLength( p_apdu, &l );
1371 msg_Dbg( p_cam->obj, "CA system IDs supported by the application :" );
1373 for ( i = 0; i < l / 2; i++ )
1375 p_ids->pi_system_ids[i] = ((uint16_t)d[0] << 8) | d[1];
1377 msg_Dbg( p_cam->obj, "- 0x%x", p_ids->pi_system_ids[i] );
1379 p_ids->pi_system_ids[i] = 0;
1381 for ( i = 0; i < MAX_PROGRAMS; i++ )
1383 if ( p_cam->pp_selected_programs[i] != NULL )
1385 CAPMTAdd( p_cam, i_session_id,
1386 p_cam->pp_selected_programs[i] );
1393 msg_Err( p_cam->obj,
1394 "unexpected tag in ConditionalAccessHandle (0x%x)",
1399 /*****************************************************************************
1400 * ConditionalAccessClose
1401 *****************************************************************************/
1402 static void ConditionalAccessClose( cam_t * p_cam, int i_session_id )
1404 msg_Dbg( p_cam->obj, "closing ConditionalAccess session (%d)",
1406 free( p_cam->p_sessions[i_session_id - 1].p_sys );
1409 /*****************************************************************************
1410 * ConditionalAccessOpen
1411 *****************************************************************************/
1412 static void ConditionalAccessOpen( cam_t * p_cam, unsigned i_session_id )
1414 msg_Dbg( p_cam->obj, "opening ConditionalAccess session (%u)",
1416 p_cam->p_sessions[i_session_id - 1].pf_handle = ConditionalAccessHandle;
1417 p_cam->p_sessions[i_session_id - 1].pf_close = ConditionalAccessClose;
1418 p_cam->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(system_ids_t) );
1420 APDUSend( p_cam, i_session_id, AOT_CA_INFO_ENQ, NULL, 0 );
1433 /*****************************************************************************
1435 *****************************************************************************/
1436 static void DateTimeSend( cam_t * p_cam, int i_session_id )
1438 date_time_t *p_date =
1439 (date_time_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1441 time_t t = time(NULL);
1445 if ( gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc) )
1447 int Y = tm_gmt.tm_year;
1448 int M = tm_gmt.tm_mon + 1;
1449 int D = tm_gmt.tm_mday;
1450 int L = (M == 1 || M == 2) ? 1 : 0;
1451 int MJD = 14956 + D + (int)((Y - L) * 365.25)
1452 + (int)((M + 1 + L * 12) * 30.6001);
1453 uint8_t p_response[7];
1455 #define DEC2BCD(d) (((d / 10) << 4) + (d % 10))
1457 SetWBE( &p_response[0], MJD );
1458 p_response[2] = DEC2BCD(tm_gmt.tm_hour);
1459 p_response[3] = DEC2BCD(tm_gmt.tm_min);
1460 p_response[4] = DEC2BCD(tm_gmt.tm_sec);
1461 SetWBE( &p_response[5], tm_loc.tm_gmtoff / 60 );
1463 APDUSend( p_cam, i_session_id, AOT_DATE_TIME, p_response, 7 );
1465 p_date->i_last = mdate();
1469 /*****************************************************************************
1471 *****************************************************************************/
1472 static void DateTimeHandle( cam_t *p_cam, int i_session_id,
1473 uint8_t *p_apdu, int i_size )
1475 date_time_t *p_date =
1476 (date_time_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1478 int i_tag = APDUGetTag( p_apdu, i_size );
1482 case AOT_DATE_TIME_ENQ:
1485 const uint8_t *d = APDUGetLength( p_apdu, &l );
1489 p_date->i_interval = *d;
1490 msg_Dbg( p_cam->obj, "DateTimeHandle : interval set to %d",
1491 p_date->i_interval );
1494 p_date->i_interval = 0;
1496 DateTimeSend( p_cam, i_session_id );
1500 msg_Err( p_cam->obj, "unexpected tag in DateTimeHandle (0x%x)",
1505 /*****************************************************************************
1507 *****************************************************************************/
1508 static void DateTimeManage( cam_t * p_cam, int i_session_id )
1510 date_time_t *p_date =
1511 (date_time_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1513 if ( p_date->i_interval
1514 && mdate() > p_date->i_last + (mtime_t)p_date->i_interval * 1000000 )
1516 DateTimeSend( p_cam, i_session_id );
1520 /*****************************************************************************
1522 *****************************************************************************/
1523 static void DateTimeClose( cam_t * p_cam, int i_session_id )
1525 msg_Dbg( p_cam->obj, "closing DateTime session (%d)", i_session_id );
1527 free( p_cam->p_sessions[i_session_id - 1].p_sys );
1530 /*****************************************************************************
1532 *****************************************************************************/
1533 static void DateTimeOpen( cam_t * p_cam, unsigned i_session_id )
1535 msg_Dbg( p_cam->obj, "opening DateTime session (%u)", i_session_id );
1537 p_cam->p_sessions[i_session_id - 1].pf_handle = DateTimeHandle;
1538 p_cam->p_sessions[i_session_id - 1].pf_manage = DateTimeManage;
1539 p_cam->p_sessions[i_session_id - 1].pf_close = DateTimeClose;
1540 p_cam->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(date_time_t) );
1542 DateTimeSend( p_cam, i_session_id );
1549 /* Display Control Commands */
1551 #define DCC_SET_MMI_MODE 0x01
1552 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1553 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1554 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1555 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1559 #define MM_HIGH_LEVEL 0x01
1560 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1561 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1563 /* Display Reply IDs */
1565 #define DRI_MMI_MODE_ACK 0x01
1566 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1567 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1568 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1569 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1570 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1571 #define DRI_UNKNOWN_MMI_MODE 0xF1
1572 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1576 #define EF_BLIND 0x01
1580 #define AI_CANCEL 0x00
1581 #define AI_ANSWER 0x01
1583 static void MMIFree( mmi_t *p_object )
1585 switch ( p_object->i_object_type )
1587 case EN50221_MMI_ENQ:
1588 FREENULL( p_object->u.enq.psz_text );
1591 case EN50221_MMI_ANSW:
1592 if ( p_object->u.answ.b_ok )
1594 FREENULL( p_object->u.answ.psz_answ );
1598 case EN50221_MMI_MENU:
1599 case EN50221_MMI_LIST:
1600 FREENULL( p_object->u.menu.psz_title );
1601 FREENULL( p_object->u.menu.psz_subtitle );
1602 FREENULL( p_object->u.menu.psz_bottom );
1603 for ( int i = 0; i < p_object->u.menu.i_choices; i++ )
1605 free( p_object->u.menu.ppsz_choices[i] );
1607 FREENULL( p_object->u.menu.ppsz_choices );
1616 /*****************************************************************************
1618 *****************************************************************************/
1619 static void MMISendObject( cam_t *p_cam, int i_session_id,
1622 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
1626 switch ( p_object->i_object_type )
1628 case EN50221_MMI_ANSW:
1630 i_size = 1 + strlen( p_object->u.answ.psz_answ );
1631 p_data = xmalloc( i_size );
1632 p_data[0] = p_object->u.answ.b_ok ? 0x1 : 0x0;
1633 strncpy( (char *)&p_data[1], p_object->u.answ.psz_answ, i_size - 1 );
1636 case EN50221_MMI_MENU_ANSW:
1637 i_tag = AOT_MENU_ANSW;
1639 p_data = xmalloc( i_size );
1640 p_data[0] = p_object->u.menu_answ.i_choice;
1644 msg_Err( p_cam->obj, "unknown MMI object %d", p_object->i_object_type );
1648 APDUSend( p_cam, i_session_id, i_tag, p_data, i_size );
1651 p_cam->pb_slot_mmi_expected[i_slot] = true;
1654 /*****************************************************************************
1656 *****************************************************************************/
1657 static void MMISendClose( cam_t *p_cam, int i_session_id )
1659 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
1661 APDUSend( p_cam, i_session_id, AOT_CLOSE_MMI, NULL, 0 );
1663 p_cam->pb_slot_mmi_expected[i_slot] = true;
1667 /*****************************************************************************
1669 *****************************************************************************/
1670 static void MMIDisplayReply( cam_t *p_cam, int i_session_id )
1672 uint8_t p_response[2];
1674 p_response[0] = DRI_MMI_MODE_ACK;
1675 p_response[1] = MM_HIGH_LEVEL;
1677 APDUSend( p_cam, i_session_id, AOT_DISPLAY_REPLY, p_response, 2 );
1679 msg_Dbg( p_cam->obj, "sending DisplayReply on session (%d)", i_session_id );
1682 /*****************************************************************************
1684 *****************************************************************************/
1685 static char *MMIGetText( cam_t *p_cam, uint8_t **pp_apdu, int *pi_size )
1687 int i_tag = APDUGetTag( *pp_apdu, *pi_size );
1691 if ( i_tag != AOT_TEXT_LAST )
1693 msg_Err( p_cam->obj, "unexpected text tag: %06x", i_tag );
1695 return strdup( "" );
1698 d = APDUGetLength( *pp_apdu, &l );
1703 return vlc_from_EIT(d,l);
1706 /*****************************************************************************
1708 *****************************************************************************/
1709 static void MMIHandleEnq( cam_t *p_cam, int i_session_id,
1710 uint8_t *p_apdu, int i_size )
1712 VLC_UNUSED( i_size );
1714 mmi_t *p_mmi = (mmi_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1715 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
1717 uint8_t *d = APDUGetLength( p_apdu, &l );
1720 p_mmi->i_object_type = EN50221_MMI_ENQ;
1721 p_mmi->u.enq.b_blind = (*d & 0x1) ? true : false;
1722 d += 2; /* skip answer_text_length because it is not mandatory */
1724 p_mmi->u.enq.psz_text = xmalloc( l + 1 );
1725 strncpy( p_mmi->u.enq.psz_text, (char *)d, l );
1726 p_mmi->u.enq.psz_text[l] = '\0';
1728 msg_Dbg( p_cam->obj, "MMI enq: %s%s", p_mmi->u.enq.psz_text,
1729 p_mmi->u.enq.b_blind ? " (blind)" : "" );
1730 p_cam->pb_slot_mmi_expected[i_slot] = false;
1731 p_cam->pb_slot_mmi_undisplayed[i_slot] = true;
1734 /*****************************************************************************
1736 *****************************************************************************/
1737 static void MMIHandleMenu( cam_t *p_cam, int i_session_id, int i_tag,
1738 uint8_t *p_apdu, int i_size )
1742 mmi_t *p_mmi = (mmi_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1743 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
1745 uint8_t *d = APDUGetLength( p_apdu, &l );
1748 p_mmi->i_object_type = (i_tag == AOT_MENU_LAST) ?
1749 EN50221_MMI_MENU : EN50221_MMI_LIST;
1750 p_mmi->u.menu.i_choices = 0;
1751 p_mmi->u.menu.ppsz_choices = NULL;
1755 l--; d++; /* choice_nb */
1757 #define GET_FIELD( x ) \
1760 p_mmi->u.menu.psz_##x = MMIGetText( p_cam, &d, &l ); \
1761 msg_Dbg( p_cam->obj, "MMI " STRINGIFY( x ) ": %s", \
1762 p_mmi->u.menu.psz_##x ); \
1766 GET_FIELD( subtitle );
1767 GET_FIELD( bottom );
1772 char *psz_text = MMIGetText( p_cam, &d, &l );
1773 TAB_APPEND( p_mmi->u.menu.i_choices,
1774 p_mmi->u.menu.ppsz_choices,
1776 msg_Dbg( p_cam->obj, "MMI choice: %s", psz_text );
1779 p_cam->pb_slot_mmi_expected[i_slot] = false;
1780 p_cam->pb_slot_mmi_undisplayed[i_slot] = true;
1783 /*****************************************************************************
1785 *****************************************************************************/
1786 static void MMIHandle( cam_t *p_cam, int i_session_id,
1787 uint8_t *p_apdu, int i_size )
1789 int i_tag = APDUGetTag( p_apdu, i_size );
1793 case AOT_DISPLAY_CONTROL:
1796 uint8_t *d = APDUGetLength( p_apdu, &l );
1802 case DCC_SET_MMI_MODE:
1803 if ( l == 2 && d[1] == MM_HIGH_LEVEL )
1804 MMIDisplayReply( p_cam, i_session_id );
1806 msg_Err( p_cam->obj, "unsupported MMI mode %02x", d[1] );
1810 msg_Err( p_cam->obj, "unsupported display control command %02x",
1819 MMIHandleEnq( p_cam, i_session_id, p_apdu, i_size );
1824 MMIHandleMenu( p_cam, i_session_id, i_tag, p_apdu, i_size );
1828 SessionSendClose( p_cam, i_session_id );
1832 msg_Err( p_cam->obj, "unexpected tag in MMIHandle (0x%x)", i_tag );
1836 /*****************************************************************************
1838 *****************************************************************************/
1839 static void MMIClose( cam_t *p_cam, int i_session_id )
1841 int i_slot = p_cam->p_sessions[i_session_id - 1].i_slot;
1842 mmi_t *p_mmi = (mmi_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1845 free( p_cam->p_sessions[i_session_id - 1].p_sys );
1847 msg_Dbg( p_cam->obj, "closing MMI session (%d)", i_session_id );
1848 p_cam->pb_slot_mmi_expected[i_slot] = false;
1849 p_cam->pb_slot_mmi_undisplayed[i_slot] = true;
1852 /*****************************************************************************
1854 *****************************************************************************/
1855 static void MMIOpen( cam_t *p_cam, unsigned i_session_id )
1859 msg_Dbg( p_cam->obj, "opening MMI session (%u)", i_session_id );
1861 p_cam->p_sessions[i_session_id - 1].pf_handle = MMIHandle;
1862 p_cam->p_sessions[i_session_id - 1].pf_close = MMIClose;
1863 p_cam->p_sessions[i_session_id - 1].p_sys = xmalloc(sizeof(mmi_t));
1864 p_mmi = (mmi_t *)p_cam->p_sessions[i_session_id - 1].p_sys;
1865 p_mmi->i_object_type = EN50221_MMI_NONE;
1873 /*****************************************************************************
1874 * InitSlot: Open the transport layer
1875 *****************************************************************************/
1876 #define MAX_TC_RETRIES 20
1878 static int InitSlot( cam_t * p_cam, int i_slot )
1880 if ( TPDUSend( p_cam, i_slot, T_CREATE_TC, NULL, 0 ) != VLC_SUCCESS )
1882 msg_Err( p_cam->obj, "en50221_Init: couldn't send TPDU on slot %d",
1884 return VLC_EGENERIC;
1887 /* This is out of the spec */
1888 for ( int i = 0; i < MAX_TC_RETRIES; i++ )
1891 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) == VLC_SUCCESS
1892 && i_tag == T_CTC_REPLY )
1894 p_cam->pb_active_slot[i_slot] = true;
1898 if ( TPDUSend( p_cam, i_slot, T_CREATE_TC, NULL, 0 )
1901 msg_Err( p_cam->obj,
1902 "en50221_Init: couldn't send TPDU on slot %d",
1908 if ( p_cam->pb_active_slot[i_slot] )
1910 p_cam->i_timeout = CLOCK_FREQ / 10;
1914 return VLC_EGENERIC;
1919 * External entry points
1922 /*****************************************************************************
1923 * en50221_Init : Initialize the CAM for en50221
1924 *****************************************************************************/
1925 cam_t *en50221_Init( vlc_object_t *obj, int fd )
1929 memset( &caps, 0, sizeof( caps ));
1930 if( ioctl( fd, CA_GET_CAP, &caps ) < 0 )
1932 msg_Err( obj, "CAMInit: ioctl() error getting CAM capabilities" );
1936 /* Output CA capabilities */
1937 msg_Dbg( obj, "CA interface with %d slot(s)", caps.slot_num );
1938 if( caps.slot_type & CA_CI )
1939 msg_Dbg( obj, " CI high level interface type" );
1940 if( caps.slot_type & CA_CI_LINK )
1941 msg_Dbg( obj, " CI link layer level interface type" );
1942 if( caps.slot_type & CA_CI_PHYS )
1943 msg_Dbg( obj, " CI physical layer level interface type (not supported) " );
1944 if( caps.slot_type & CA_DESCR )
1945 msg_Dbg( obj, " built-in descrambler detected" );
1946 if( caps.slot_type & CA_SC )
1947 msg_Dbg( obj, " simple smart card interface" );
1949 msg_Dbg( obj, "%d available descrambler(s) (keys)", caps.descr_num );
1950 if( caps.descr_type & CA_ECD )
1951 msg_Dbg( obj, " ECD scrambling system supported" );
1952 if( caps.descr_type & CA_NDS )
1953 msg_Dbg( obj, " NDS scrambling system supported" );
1954 if( caps.descr_type & CA_DSS )
1955 msg_Dbg( obj, " DSS scrambling system supported" );
1957 if( caps.slot_num == 0 )
1959 msg_Err( obj, "CAM module without slots" );
1963 cam_t *p_cam = calloc( 1, sizeof( *p_cam ) );
1964 if( unlikely(p_cam == NULL) )
1970 if( caps.slot_type & CA_CI_LINK )
1972 p_cam->i_ca_type = CA_CI_LINK;
1974 for ( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
1976 if ( ioctl( p_cam->fd, CA_RESET, 1 << i_slot) != 0 )
1978 msg_Err( p_cam->obj, "en50221_Init: couldn't reset slot %d",
1983 p_cam->i_timeout = CLOCK_FREQ / 10;
1984 /* Wait a bit otherwise it doesn't initialize properly... */
1985 msleep( CLOCK_FREQ / 10 );
1986 p_cam->i_next_event = 0;
1989 if( caps.slot_type & CA_CI )
1991 p_cam->i_ca_type = CA_CI;
1993 struct ca_slot_info info;
1995 /* We don't reset the CAM in that case because it's done by the
1997 if ( ioctl( fd, CA_GET_SLOT_INFO, &info ) < 0 )
1999 msg_Err( obj, "cannot get slot info: %m" );
2002 if( info.flags == 0 )
2004 msg_Err( obj, "no CAM inserted" );
2008 /* Allocate a dummy sessions */
2009 p_cam->p_sessions[ 0 ].i_resource_id = RI_CONDITIONAL_ACCESS_SUPPORT;
2011 /* Get application info to find out which cam we are using and make
2012 sure everything is ready to play */
2015 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
2016 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
2017 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
2018 memset( &ca_msg.msg[3], 0, 253 );
2019 APDUSend( p_cam, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
2020 if ( ioctl( fd, CA_GET_MSG, &ca_msg ) < 0 )
2022 msg_Err( obj, "en50221_Init: failed getting message" );
2026 #if HLCI_WAIT_CAM_READY
2027 while( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
2029 if( !vlc_object_alive (obj) )
2032 msg_Dbg( obj, "CAM: please wait" );
2033 APDUSend( p_cam, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
2035 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
2036 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
2037 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
2038 memset( &ca_msg.msg[3], 0, 253 );
2039 if ( ioctl( fd, CA_GET_MSG, &ca_msg ) < 0 )
2041 msg_Err( obj, "en50221_Init: failed getting message" );
2044 msg_Dbg( p_cam->obj, "en50221_Init: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
2047 if( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
2049 msg_Err( obj, "CAM returns garbage as application info!" );
2053 msg_Dbg( obj, "found CAM %s using id 0x%x", &ca_msg.msg[12],
2054 (ca_msg.msg[8]<<8)|ca_msg.msg[9] );
2058 msg_Err( obj, "CAM interface incompatible" );
2069 /*****************************************************************************
2070 * en50221_Poll : Poll the CAM for TPDUs
2071 *****************************************************************************/
2072 void en50221_Poll( cam_t * p_cam )
2074 switch( p_cam->i_ca_type )
2077 if( mdate() > p_cam->i_next_event )
2085 for ( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
2088 ca_slot_info_t sinfo;
2091 if ( ioctl( p_cam->fd, CA_GET_SLOT_INFO, &sinfo ) != 0 )
2093 msg_Err( p_cam->obj, "en50221_Poll: couldn't get info on slot %d",
2098 if ( !(sinfo.flags & CA_CI_MODULE_READY) )
2100 if ( p_cam->pb_active_slot[i_slot] )
2102 msg_Dbg( p_cam->obj, "en50221_Poll: slot %d has been removed",
2104 p_cam->pb_active_slot[i_slot] = false;
2105 p_cam->pb_slot_mmi_expected[i_slot] = false;
2106 p_cam->pb_slot_mmi_undisplayed[i_slot] = false;
2108 /* Close all sessions for this slot. */
2109 for ( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2111 if ( p_cam->p_sessions[i - 1].i_resource_id
2112 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2114 if ( p_cam->p_sessions[i - 1].pf_close != NULL )
2116 p_cam->p_sessions[i - 1].pf_close( p_cam, i );
2118 p_cam->p_sessions[i - 1].i_resource_id = 0;
2125 else if ( !p_cam->pb_active_slot[i_slot] )
2127 InitSlot( p_cam, i_slot );
2129 if ( !p_cam->pb_active_slot[i_slot] )
2131 msg_Dbg( p_cam->obj, "en50221_Poll: resetting slot %d", i_slot );
2133 if ( ioctl( p_cam->fd, CA_RESET, 1 << i_slot) != 0 )
2135 msg_Err( p_cam->obj, "en50221_Poll: couldn't reset slot %d",
2141 msg_Dbg( p_cam->obj, "en50221_Poll: slot %d is active",
2145 if ( !p_cam->pb_tc_has_data[i_slot] )
2147 if ( TPDUSend( p_cam, i_slot, T_DATA_LAST, NULL, 0 ) !=
2150 msg_Err( p_cam->obj,
2151 "en50221_Poll: couldn't send TPDU on slot %d",
2155 if ( TPDURecv( p_cam, i_slot, &i_tag, NULL, NULL ) !=
2158 msg_Err( p_cam->obj,
2159 "en50221_Poll: couldn't recv TPDU on slot %d",
2165 while ( p_cam->pb_tc_has_data[i_slot] )
2167 uint8_t p_tpdu[MAX_TPDU_SIZE];
2168 int i_size, i_session_size;
2171 if ( TPDUSend( p_cam, i_slot, T_RCV, NULL, 0 ) != VLC_SUCCESS )
2173 msg_Err( p_cam->obj,
2174 "en50221_Poll: couldn't send TPDU on slot %d",
2178 if ( TPDURecv( p_cam, i_slot, &i_tag, p_tpdu, &i_size ) !=
2181 msg_Err( p_cam->obj,
2182 "en50221_Poll: couldn't recv TPDU on slot %d",
2187 p_session = GetLength( &p_tpdu[3], &i_session_size );
2188 if ( i_session_size <= 1 )
2194 if ( i_tag != T_DATA_LAST )
2196 msg_Err( p_cam->obj,
2197 "en50221_Poll: fragmented TPDU not supported" );
2201 SPDUHandle( p_cam, i_slot, p_session, i_session_size );
2205 for ( int i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2207 if ( p_cam->p_sessions[i_session_id - 1].i_resource_id
2208 && p_cam->p_sessions[i_session_id - 1].pf_manage )
2210 p_cam->p_sessions[i_session_id - 1].pf_manage( p_cam,
2215 p_cam->i_next_event = mdate() + p_cam->i_timeout;
2219 /*****************************************************************************
2220 * en50221_SetCAPMT :
2221 *****************************************************************************/
2222 int en50221_SetCAPMT( cam_t * p_cam, dvbpsi_pmt_t *p_pmt )
2224 bool b_update = false;
2225 bool b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
2227 for ( unsigned i = 0; i < MAX_PROGRAMS; i++ )
2229 if ( p_cam->pp_selected_programs[i] != NULL
2230 && p_cam->pp_selected_programs[i]->i_program_number
2231 == p_pmt->i_program_number )
2235 if ( !b_needs_descrambling )
2237 dvbpsi_DeletePMT( p_pmt );
2238 p_pmt = p_cam->pp_selected_programs[i];
2239 p_cam->pp_selected_programs[i] = NULL;
2241 else if( p_pmt != p_cam->pp_selected_programs[i] )
2243 dvbpsi_DeletePMT( p_cam->pp_selected_programs[i] );
2244 p_cam->pp_selected_programs[i] = p_pmt;
2251 if ( !b_update && b_needs_descrambling )
2253 for ( unsigned i = 0; i < MAX_PROGRAMS; i++ )
2255 if ( p_cam->pp_selected_programs[i] == NULL )
2257 p_cam->pp_selected_programs[i] = p_pmt;
2263 if ( b_update || b_needs_descrambling )
2265 for ( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2267 if ( p_cam->p_sessions[i - 1].i_resource_id
2268 == RI_CONDITIONAL_ACCESS_SUPPORT )
2270 if ( b_update && b_needs_descrambling )
2271 CAPMTUpdate( p_cam, i, p_pmt );
2272 else if ( b_update )
2273 CAPMTDelete( p_cam, i, p_pmt );
2275 CAPMTAdd( p_cam, i, p_pmt );
2280 if ( !b_needs_descrambling )
2282 dvbpsi_DeletePMT( p_pmt );
2289 /*****************************************************************************
2291 *****************************************************************************/
2292 static int en50221_OpenMMI( cam_t * p_cam, unsigned i_slot )
2294 if( p_cam->i_ca_type & CA_CI_LINK )
2296 for ( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2298 if ( p_cam->p_sessions[i - 1].i_resource_id == RI_MMI
2299 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2301 msg_Dbg( p_cam->obj,
2302 "MMI menu is already opened on slot %d (session=%u)",
2308 for ( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2310 if ( p_cam->p_sessions[i - 1].i_resource_id
2311 == RI_APPLICATION_INFORMATION
2312 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2314 ApplicationInformationEnterMenu( p_cam, i );
2319 msg_Err( p_cam->obj, "no application information on slot %d", i_slot );
2320 return VLC_EGENERIC;
2324 msg_Err( p_cam->obj, "MMI menu not supported" );
2325 return VLC_EGENERIC;
2329 /*****************************************************************************
2330 * en50221_CloseMMI :
2331 *****************************************************************************/
2332 static int en50221_CloseMMI( cam_t * p_cam, unsigned i_slot )
2334 if( p_cam->i_ca_type & CA_CI_LINK )
2336 for( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2338 if ( p_cam->p_sessions[i - 1].i_resource_id == RI_MMI
2339 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2341 MMISendClose( p_cam, i );
2346 msg_Warn( p_cam->obj, "closing a non-existing MMI session on slot %d",
2348 return VLC_EGENERIC;
2352 msg_Err( p_cam->obj, "MMI menu not supported" );
2353 return VLC_EGENERIC;
2357 /*****************************************************************************
2358 * en50221_GetMMIObject :
2359 *****************************************************************************/
2360 static mmi_t *en50221_GetMMIObject( cam_t * p_cam, unsigned i_slot )
2362 if( p_cam->pb_slot_mmi_expected[i_slot] )
2363 return NULL; /* should not happen */
2365 for( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2367 if ( p_cam->p_sessions[i - 1].i_resource_id == RI_MMI
2368 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2371 (mmi_t *)p_cam->p_sessions[i - 1].p_sys;
2372 if ( p_mmi == NULL )
2373 return NULL; /* should not happen */
2382 /*****************************************************************************
2383 * en50221_SendMMIObject :
2384 *****************************************************************************/
2385 static void en50221_SendMMIObject( cam_t * p_cam, unsigned i_slot,
2388 for( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2390 if ( p_cam->p_sessions[i - 1].i_resource_id == RI_MMI
2391 && p_cam->p_sessions[i - 1].i_slot == i_slot )
2393 MMISendObject( p_cam, i, p_object );
2398 msg_Err( p_cam->obj, "SendMMIObject when no MMI session is opened !" );
2401 char *en50221_Status( cam_t *p_cam, char *psz_request )
2403 if( psz_request != NULL && *psz_request )
2405 /* Check if we have an undisplayed MMI message : in that case we ignore
2406 * the user input to avoid confusing the CAM. */
2407 for ( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
2409 if ( p_cam->pb_slot_mmi_undisplayed[i_slot] )
2412 msg_Dbg( p_cam->obj,
2413 "ignoring user request because of a new MMI object" );
2419 if( psz_request != NULL && *psz_request )
2421 /* We have a mission to accomplish. */
2423 char psz_value[255];
2427 if ( HTTPExtractValue( psz_request, "slot", psz_value,
2428 sizeof(psz_value) ) == NULL )
2430 return strdup( "invalid request parameter\n" );
2432 i_slot = atoi(psz_value);
2434 if ( HTTPExtractValue( psz_request, "open", psz_value,
2435 sizeof(psz_value) ) != NULL )
2437 en50221_OpenMMI( p_cam, i_slot );
2441 if ( HTTPExtractValue( psz_request, "close", psz_value,
2442 sizeof(psz_value) ) != NULL )
2444 en50221_CloseMMI( p_cam, i_slot );
2448 if ( HTTPExtractValue( psz_request, "cancel", psz_value,
2449 sizeof(psz_value) ) == NULL )
2454 if ( HTTPExtractValue( psz_request, "type", psz_value,
2455 sizeof(psz_value) ) == NULL )
2457 return strdup( "invalid request parameter\n" );
2460 if ( !strcmp( psz_value, "enq" ) )
2462 mmi_object.i_object_type = EN50221_MMI_ANSW;
2463 mmi_object.u.answ.b_ok = b_ok;
2466 mmi_object.u.answ.psz_answ = strdup("");
2470 if ( HTTPExtractValue( psz_request, "answ", psz_value,
2471 sizeof(psz_value) ) == NULL )
2473 return strdup( "invalid request parameter\n" );
2476 mmi_object.u.answ.psz_answ = strdup(psz_value);
2481 mmi_object.i_object_type = EN50221_MMI_MENU_ANSW;
2484 mmi_object.u.menu_answ.i_choice = 0;
2488 if ( HTTPExtractValue( psz_request, "choice", psz_value,
2489 sizeof(psz_value) ) == NULL )
2490 mmi_object.u.menu_answ.i_choice = 0;
2492 mmi_object.u.menu_answ.i_choice = atoi(psz_value);
2496 en50221_SendMMIObject( p_cam, i_slot, &mmi_object );
2500 /* Check that we have all necessary MMI information. */
2501 for( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
2503 if ( p_cam->pb_slot_mmi_expected[i_slot] )
2509 FILE *p = open_memstream( &buf, &len );
2510 if( unlikely(p == NULL) )
2515 if( ioctl( p_cam->fd, CA_GET_CAP, &caps ) < 0 )
2517 fprintf( p, "ioctl CA_GET_CAP failed (%m)\n" );
2521 /* Output CA capabilities */
2522 fprintf( p, "CA interface with %d %s, type:\n<table>", caps.slot_num,
2523 caps.slot_num == 1 ? "slot" : "slots" );
2524 #define CHECK_CAPS( x, s ) \
2525 if ( caps.slot_type & (CA_##x) ) \
2526 fprintf( p, "<tr><td>%s</td></tr>\n", s )
2528 CHECK_CAPS( CI, "CI high level interface" );
2529 CHECK_CAPS( CI_LINK, "CI link layer level interface" );
2530 CHECK_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
2531 CHECK_CAPS( DESCR, "built-in descrambler" );
2532 CHECK_CAPS( SC, "simple smartcard interface" );
2535 fprintf( p, "</table>%d available %s\n<table>", caps.descr_num,
2536 caps.descr_num == 1 ? "descrambler (key)" : "descramblers (keys)" );
2537 #define CHECK_DESC( x ) \
2538 if ( caps.descr_type & (CA_##x) ) \
2539 fprintf( p, "<tr><td>%s</td></tr>", STRINGIFY(x) )
2546 fputs( "</table>", p );
2548 for( unsigned i_slot = 0; i_slot < p_cam->i_nb_slots; i_slot++ )
2550 ca_slot_info_t sinfo;
2552 p_cam->pb_slot_mmi_undisplayed[i_slot] = false;
2553 fprintf( p, "<p>CA slot #%d: ", i_slot );
2556 if ( ioctl( p_cam->fd, CA_GET_SLOT_INFO, &sinfo ) < 0 )
2558 fprintf( p, "ioctl CA_GET_SLOT_INFO failed (%m)<br>\n" );
2562 #define CHECK_TYPE( x, s ) \
2563 if ( sinfo.type & (CA_##x) ) \
2566 CHECK_TYPE( CI, "high level, " );
2567 CHECK_TYPE( CI_LINK, "link layer level, " );
2568 CHECK_TYPE( CI_PHYS, "physical layer level, " );
2571 if ( sinfo.flags & CA_CI_MODULE_READY )
2573 mmi_t *p_object = en50221_GetMMIObject( p_cam, i_slot );
2575 fputs( "module present and ready<p>\n", p );
2576 fputs( "<form action=index.html method=get>\n", p );
2577 fprintf( p, "<input type=hidden name=slot value=\"%d\">\n",
2580 if ( p_object == NULL )
2582 fputs( "<input type=submit name=open"
2583 " value=\"Open session\">\n", p );
2587 switch ( p_object->i_object_type )
2589 case EN50221_MMI_ENQ:
2590 fputs( "<input type=hidden name=type value=enq>\n", p );
2591 fprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
2592 p_object->u.enq.psz_text );
2593 fprintf( p, "<tr><td><input type=%s name=answ>"
2595 p_object->u.enq.b_blind ? "password" : "text" );
2598 case EN50221_MMI_MENU:
2599 fputs( "<input type=hidden name=type value=menu>\n", p );
2600 fprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
2601 p_object->u.menu.psz_title );
2602 fprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
2603 p_object->u.menu.psz_subtitle );
2604 for ( int i = 0; i < p_object->u.menu.i_choices; i++ )
2605 fprintf( p, "<input type=radio name=choice"
2606 " value=\"%d\">%s<br>\n", i + 1,
2607 p_object->u.menu.ppsz_choices[i] );
2608 fprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
2609 p_object->u.menu.psz_bottom );
2612 case EN50221_MMI_LIST:
2613 fputs( "<input type=hidden name=type value=menu>\n", p );
2614 fputs( "<input type=hidden name=choice value=0>\n", p );
2615 fprintf( p, "<table border=1><tr><th>%s</th></tr>\n",
2616 p_object->u.menu.psz_title );
2617 fprintf( p, "<tr><td>%s</td></tr><tr><td>\n",
2618 p_object->u.menu.psz_subtitle );
2619 for ( int i = 0; i < p_object->u.menu.i_choices; i++ )
2620 fprintf( p, "%s<br>\n",
2621 p_object->u.menu.ppsz_choices[i] );
2622 fprintf( p, "</td></tr><tr><td>%s</td></tr>\n",
2623 p_object->u.menu.psz_bottom );
2627 fputs( "<table><tr><th>Unknown MMI object type</th></tr>\n", p );
2630 fputs( "</table><p><input type=submit name=ok value=\"OK\">\n", p );
2631 fputs( "<input type=submit name=cancel value=\"Cancel\">\n", p );
2632 fputs( "<input type=submit name=close value=\"Close Session\">\n", p );
2634 fputs( "</form>\n", p );
2636 else if ( sinfo.flags & CA_CI_MODULE_PRESENT )
2637 fputs( "module present, not ready<br>\n", p );
2639 fputs( "module not present<br>\n", p );
2648 /*****************************************************************************
2650 *****************************************************************************/
2651 void en50221_End( cam_t * p_cam )
2653 for( unsigned i = 0; i < MAX_PROGRAMS; i++ )
2655 if( p_cam->pp_selected_programs[i] != NULL )
2657 dvbpsi_DeletePMT( p_cam->pp_selected_programs[i] );
2661 for( unsigned i = 1; i <= MAX_SESSIONS; i++ )
2663 if( p_cam->p_sessions[i - 1].i_resource_id
2664 && p_cam->p_sessions[i - 1].pf_close != NULL )
2666 p_cam->p_sessions[i - 1].pf_close( p_cam, i );