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>
42 #include <netinet/in.h>
44 /* DVB Card Drivers */
45 #include <linux/dvb/version.h>
46 #include <linux/dvb/dmx.h>
47 #include <linux/dvb/frontend.h>
48 #include <linux/dvb/ca.h>
50 /* Include dvbpsi headers */
51 #ifdef HAVE_DVBPSI_DR_H
52 # include <dvbpsi/dvbpsi.h>
53 # include <dvbpsi/descriptor.h>
54 # include <dvbpsi/pat.h>
55 # include <dvbpsi/pmt.h>
56 # include <dvbpsi/dr.h>
57 # include <dvbpsi/psi.h>
58 # include <dvbpsi/demux.h>
59 # include <dvbpsi/sdt.h>
62 # include "descriptor.h"
63 # include "tables/pat.h"
64 # include "tables/pmt.h"
65 # include "descriptors/dr.h"
72 # include "vlc_httpd.h"
77 #include <vlc_charset.h>
80 #define HLCI_WAIT_CAM_READY 0
81 #define CAM_PROG_MAX MAX_PROGRAMS
83 static void ResourceManagerOpen( access_t * p_access, int i_session_id );
84 static void ApplicationInformationOpen( access_t * p_access, int i_session_id );
85 static void ConditionalAccessOpen( access_t * p_access, int i_session_id );
86 static void DateTimeOpen( access_t * p_access, int i_session_id );
87 static void MMIOpen( access_t * p_access, int i_session_id );
89 /*****************************************************************************
91 *****************************************************************************/
92 #define SIZE_INDICATOR 0x80
94 static uint8_t *GetLength( uint8_t *p_data, int *pi_length )
96 *pi_length = *p_data++;
98 if ( (*pi_length & SIZE_INDICATOR) != 0 )
100 int l = *pi_length & ~SIZE_INDICATOR;
104 for ( i = 0; i < l; i++ )
105 *pi_length = (*pi_length << 8) | *p_data++;
111 static uint8_t *SetLength( uint8_t *p_data, int i_length )
115 if ( i_length < 128 )
119 else if ( i_length < 256 )
121 *p++ = SIZE_INDICATOR | 0x1;
124 else if ( i_length < 65536 )
126 *p++ = SIZE_INDICATOR | 0x2;
127 *p++ = i_length >> 8;
128 *p++ = i_length & 0xff;
130 else if ( i_length < 16777216 )
132 *p++ = SIZE_INDICATOR | 0x3;
133 *p++ = i_length >> 16;
134 *p++ = (i_length >> 8) & 0xff;
135 *p++ = i_length & 0xff;
139 *p++ = SIZE_INDICATOR | 0x4;
140 *p++ = i_length >> 24;
141 *p++ = (i_length >> 16) & 0xff;
142 *p++ = (i_length >> 8) & 0xff;
143 *p++ = i_length & 0xff;
154 #define MAX_TPDU_SIZE 2048
155 #define MAX_TPDU_DATA (MAX_TPDU_SIZE - 4)
157 #define DATA_INDICATOR 0x80
161 #define T_CREATE_TC 0x82
162 #define T_CTC_REPLY 0x83
163 #define T_DELETE_TC 0x84
164 #define T_DTC_REPLY 0x85
165 #define T_REQUEST_TC 0x86
166 #define T_NEW_TC 0x87
167 #define T_TC_ERROR 0x88
168 #define T_DATA_LAST 0xA0
169 #define T_DATA_MORE 0xA1
171 static void Dump( bool b_outgoing, uint8_t *p_data, int i_size )
176 fprintf(stderr, "%s ", b_outgoing ? "-->" : "<--");
177 for ( i = 0; i < i_size && i < MAX_DUMP; i++)
178 fprintf(stderr, "%02X ", p_data[i]);
179 fprintf(stderr, "%s\n", i_size >= MAX_DUMP ? "..." : "");
181 VLC_UNUSED(b_outgoing); VLC_UNUSED(p_data); VLC_UNUSED(i_size);
185 /*****************************************************************************
187 *****************************************************************************/
188 static int TPDUSend( access_t * p_access, uint8_t i_slot, uint8_t i_tag,
189 const uint8_t *p_content, int i_length )
191 access_sys_t *p_sys = p_access->p_sys;
192 uint8_t i_tcid = i_slot + 1;
193 uint8_t p_data[MAX_TPDU_SIZE];
209 p_data[3] = 1; /* length */
216 p_data[3] = 2; /* length */
218 p_data[5] = p_content[0];
225 /* i_length <= MAX_TPDU_DATA */
226 uint8_t *p = p_data + 3;
227 p = SetLength( p, i_length + 1 );
231 memcpy( p, p_content, i_length );
232 i_size = i_length + (p - p_data);
239 Dump( true, p_data, i_size );
241 if ( write( p_sys->i_ca_handle, p_data, i_size ) != i_size )
243 msg_Err( p_access, "cannot write to CAM device (%m)" );
251 /*****************************************************************************
253 *****************************************************************************/
254 #define CAM_READ_TIMEOUT 3500 // ms
256 static int TPDURecv( access_t * p_access, uint8_t i_slot, uint8_t *pi_tag,
257 uint8_t *p_data, int *pi_size )
259 access_sys_t *p_sys = p_access->p_sys;
260 uint8_t i_tcid = i_slot + 1;
262 struct pollfd pfd[1];
264 pfd[0].fd = p_sys->i_ca_handle;
265 pfd[0].events = POLLIN;
266 if ( !(poll(pfd, 1, CAM_READ_TIMEOUT) > 0 && (pfd[0].revents & POLLIN)) )
268 msg_Err( p_access, "cannot poll from CAM device" );
272 if ( pi_size == NULL )
274 p_data = malloc( MAX_TPDU_SIZE );
279 i_size = read( p_sys->i_ca_handle, p_data, MAX_TPDU_SIZE );
281 if ( i_size >= 0 || errno != EINTR )
287 msg_Err( p_access, "cannot read from CAM device (%d:%m)", i_size );
288 if( pi_size == NULL )
293 if ( p_data[1] != i_tcid )
295 msg_Err( p_access, "invalid read from CAM device (%d instead of %d)",
297 if( pi_size == NULL )
303 p_sys->pb_tc_has_data[i_slot] = (i_size >= 4
304 && p_data[i_size - 4] == T_SB
305 && p_data[i_size - 3] == 2
306 && (p_data[i_size - 1] & DATA_INDICATOR))
309 Dump( false, p_data, i_size );
311 if ( pi_size == NULL )
324 #define ST_SESSION_NUMBER 0x90
325 #define ST_OPEN_SESSION_REQUEST 0x91
326 #define ST_OPEN_SESSION_RESPONSE 0x92
327 #define ST_CREATE_SESSION 0x93
328 #define ST_CREATE_SESSION_RESPONSE 0x94
329 #define ST_CLOSE_SESSION_REQUEST 0x95
330 #define ST_CLOSE_SESSION_RESPONSE 0x96
333 #define SS_NOT_ALLOCATED 0xF0
335 #define RI_RESOURCE_MANAGER 0x00010041
336 #define RI_APPLICATION_INFORMATION 0x00020041
337 #define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
338 #define RI_HOST_CONTROL 0x00200041
339 #define RI_DATE_TIME 0x00240041
340 #define RI_MMI 0x00400041
342 static int ResourceIdToInt( uint8_t *p_data )
344 return ((int)p_data[0] << 24) | ((int)p_data[1] << 16)
345 | ((int)p_data[2] << 8) | p_data[3];
348 /*****************************************************************************
350 *****************************************************************************/
351 static int SPDUSend( access_t * p_access, int i_session_id,
352 uint8_t *p_data, int i_size )
354 access_sys_t *p_sys = p_access->p_sys;
355 uint8_t *p_spdu = malloc( i_size + 4 );
358 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
360 *p++ = ST_SESSION_NUMBER;
362 *p++ = (i_session_id >> 8);
363 *p++ = i_session_id & 0xff;
365 memcpy( p, p_data, i_size );
372 if ( i_size > MAX_TPDU_DATA )
374 if ( TPDUSend( p_access, i_slot, T_DATA_MORE, p,
375 MAX_TPDU_DATA ) != VLC_SUCCESS )
377 msg_Err( p_access, "couldn't send TPDU on session %d",
383 i_size -= MAX_TPDU_DATA;
387 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p, i_size )
390 msg_Err( p_access, "couldn't send TPDU on session %d",
398 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS
401 msg_Err( p_access, "couldn't recv TPDU on session %d",
412 /*****************************************************************************
414 *****************************************************************************/
415 static void SessionOpen( access_t * p_access, uint8_t i_slot,
416 uint8_t *p_spdu, int 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 access_sys_t *p_sys = p_access->p_sys;
553 int i_status = p_spdu[2];
554 int i_resource_id = ResourceIdToInt( &p_spdu[3] );
555 int i_session_id = ((int)p_spdu[7] << 8) | p_spdu[8];
557 if ( i_status != SS_OK )
559 msg_Err( p_access, "SessionCreateResponse: failed to open session %d"
560 " resource=0x%x status=0x%x", i_session_id, i_resource_id,
562 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
566 switch ( i_resource_id )
568 case RI_RESOURCE_MANAGER:
569 ResourceManagerOpen( p_access, i_session_id ); break;
570 case RI_APPLICATION_INFORMATION:
571 ApplicationInformationOpen( p_access, i_session_id ); break;
572 case RI_CONDITIONAL_ACCESS_SUPPORT:
573 ConditionalAccessOpen( p_access, i_session_id ); break;
575 DateTimeOpen( p_access, i_session_id ); break;
577 MMIOpen( p_access, i_session_id ); break;
579 case RI_HOST_CONTROL:
581 msg_Err( p_access, "unknown resource id (0x%x)", i_resource_id );
582 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
586 /*****************************************************************************
588 *****************************************************************************/
589 static void SessionSendClose( access_t * p_access, int i_session_id )
591 access_sys_t *p_sys = p_access->p_sys;
592 uint8_t p_response[16];
594 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
596 p_response[0] = ST_CLOSE_SESSION_REQUEST;
598 p_response[2] = i_session_id >> 8;
599 p_response[3] = i_session_id & 0xff;
601 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 4 ) !=
605 "SessionSendClose: couldn't send TPDU on slot %d", i_slot );
608 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
611 "SessionSendClose: couldn't recv TPDU on slot %d", i_slot );
616 /*****************************************************************************
618 *****************************************************************************/
619 static void SessionClose( access_t * p_access, int i_session_id )
621 access_sys_t *p_sys = p_access->p_sys;
622 uint8_t p_response[16];
624 uint8_t i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
626 if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
627 p_sys->p_sessions[i_session_id - 1].pf_close( p_access, i_session_id );
628 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
630 p_response[0] = ST_CLOSE_SESSION_RESPONSE;
632 p_response[2] = SS_OK;
633 p_response[3] = i_session_id >> 8;
634 p_response[4] = i_session_id & 0xff;
636 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, p_response, 5 ) !=
640 "SessionClose: couldn't send TPDU on slot %d", i_slot );
643 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) != VLC_SUCCESS )
646 "SessionClose: couldn't recv TPDU on slot %d", i_slot );
651 /*****************************************************************************
653 *****************************************************************************/
654 static void SPDUHandle( access_t * p_access, uint8_t i_slot,
655 uint8_t *p_spdu, int i_size )
657 access_sys_t *p_sys = p_access->p_sys;
662 case ST_SESSION_NUMBER:
665 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
666 p_sys->p_sessions[i_session_id - 1].pf_handle( p_access, i_session_id,
667 p_spdu + 4, i_size - 4 );
670 case ST_OPEN_SESSION_REQUEST:
671 if ( i_size != 6 || p_spdu[1] != 0x4 )
673 SessionOpen( p_access, i_slot, p_spdu, i_size );
676 case ST_CREATE_SESSION_RESPONSE:
677 if ( i_size != 9 || p_spdu[1] != 0x7 )
679 SessionCreateResponse( p_access, i_slot, p_spdu, i_size );
682 case ST_CLOSE_SESSION_REQUEST:
683 if ( i_size != 4 || p_spdu[1] != 0x2 )
685 i_session_id = ((int)p_spdu[2] << 8) | p_spdu[3];
686 SessionClose( p_access, i_session_id );
689 case ST_CLOSE_SESSION_RESPONSE:
690 if ( i_size != 5 || p_spdu[1] != 0x3 )
692 i_session_id = ((int)p_spdu[3] << 8) | p_spdu[4];
695 msg_Err( p_access, "closing a session which is not allocated (%d)",
700 if ( p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
701 p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
703 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
708 msg_Err( p_access, "unexpected tag in SPDUHandle (%x)", p_spdu[0] );
718 #define AOT_NONE 0x000000
719 #define AOT_PROFILE_ENQ 0x9F8010
720 #define AOT_PROFILE 0x9F8011
721 #define AOT_PROFILE_CHANGE 0x9F8012
722 #define AOT_APPLICATION_INFO_ENQ 0x9F8020
723 #define AOT_APPLICATION_INFO 0x9F8021
724 #define AOT_ENTER_MENU 0x9F8022
725 #define AOT_CA_INFO_ENQ 0x9F8030
726 #define AOT_CA_INFO 0x9F8031
727 #define AOT_CA_PMT 0x9F8032
728 #define AOT_CA_PMT_REPLY 0x9F8033
729 #define AOT_TUNE 0x9F8400
730 #define AOT_REPLACE 0x9F8401
731 #define AOT_CLEAR_REPLACE 0x9F8402
732 #define AOT_ASK_RELEASE 0x9F8403
733 #define AOT_DATE_TIME_ENQ 0x9F8440
734 #define AOT_DATE_TIME 0x9F8441
735 #define AOT_CLOSE_MMI 0x9F8800
736 #define AOT_DISPLAY_CONTROL 0x9F8801
737 #define AOT_DISPLAY_REPLY 0x9F8802
738 #define AOT_TEXT_LAST 0x9F8803
739 #define AOT_TEXT_MORE 0x9F8804
740 #define AOT_KEYPAD_CONTROL 0x9F8805
741 #define AOT_KEYPRESS 0x9F8806
742 #define AOT_ENQ 0x9F8807
743 #define AOT_ANSW 0x9F8808
744 #define AOT_MENU_LAST 0x9F8809
745 #define AOT_MENU_MORE 0x9F880A
746 #define AOT_MENU_ANSW 0x9F880B
747 #define AOT_LIST_LAST 0x9F880C
748 #define AOT_LIST_MORE 0x9F880D
749 #define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
750 #define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
751 #define AOT_DISPLAY_MESSAGE 0x9F8810
752 #define AOT_SCENE_END_MARK 0x9F8811
753 #define AOT_SCENE_DONE 0x9F8812
754 #define AOT_SCENE_CONTROL 0x9F8813
755 #define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
756 #define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
757 #define AOT_FLUSH_DOWNLOAD 0x9F8816
758 #define AOT_DOWNLOAD_REPLY 0x9F8817
759 #define AOT_COMMS_CMD 0x9F8C00
760 #define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
761 #define AOT_COMMS_REPLY 0x9F8C02
762 #define AOT_COMMS_SEND_LAST 0x9F8C03
763 #define AOT_COMMS_SEND_MORE 0x9F8C04
764 #define AOT_COMMS_RCV_LAST 0x9F8C05
765 #define AOT_COMMS_RCV_MORE 0x9F8C06
767 /*****************************************************************************
769 *****************************************************************************/
770 static int APDUGetTag( const uint8_t *p_apdu, int i_size )
775 for ( i = 0; i < 3; i++ )
776 t = (t << 8) | *p_apdu++;
783 /*****************************************************************************
785 *****************************************************************************/
786 static uint8_t *APDUGetLength( uint8_t *p_apdu, int *pi_size )
788 return GetLength( &p_apdu[3], pi_size );
791 /*****************************************************************************
793 *****************************************************************************/
794 static int APDUSend( access_t * p_access, int i_session_id, int i_tag,
795 uint8_t *p_data, int i_size )
797 access_sys_t *p_sys = p_access->p_sys;
798 uint8_t *p_apdu = malloc( i_size + 12 );
803 *p++ = (i_tag >> 16);
804 *p++ = (i_tag >> 8) & 0xff;
806 p = SetLength( p, i_size );
808 memcpy( p, p_data, i_size );
809 if ( p_sys->i_ca_type == CA_CI_LINK )
811 i_ret = SPDUSend( p_access, i_session_id, p_apdu, i_size + p - p_apdu );
815 if ( i_size + p - p_apdu > 256 )
817 msg_Err( p_access, "CAM: apdu overflow" );
818 i_ret = VLC_EGENERIC;
822 ca_msg.length = i_size + p - p_apdu;
823 if ( i_size == 0 ) ca_msg.length=3;
824 memcpy( ca_msg.msg, p_apdu, i_size + p - p_apdu );
825 i_ret = ioctl(p_sys->i_ca_handle, CA_SEND_MSG, &ca_msg );
828 msg_Err( p_access, "Error sending to CAM: %m" );
829 i_ret = VLC_EGENERIC;
841 /*****************************************************************************
842 * ResourceManagerHandle
843 *****************************************************************************/
844 static void ResourceManagerHandle( access_t * p_access, int i_session_id,
845 uint8_t *p_apdu, int i_size )
847 int i_tag = APDUGetTag( p_apdu, i_size );
851 case AOT_PROFILE_ENQ:
853 int resources[] = { htonl(RI_RESOURCE_MANAGER),
854 htonl(RI_APPLICATION_INFORMATION),
855 htonl(RI_CONDITIONAL_ACCESS_SUPPORT),
859 APDUSend( p_access, i_session_id, AOT_PROFILE, (uint8_t*)resources,
864 APDUSend( p_access, i_session_id, AOT_PROFILE_CHANGE, NULL, 0 );
868 msg_Err( p_access, "unexpected tag in ResourceManagerHandle (0x%x)",
873 /*****************************************************************************
874 * ResourceManagerOpen
875 *****************************************************************************/
876 static void ResourceManagerOpen( access_t * p_access, int i_session_id )
878 access_sys_t *p_sys = p_access->p_sys;
880 msg_Dbg( p_access, "opening ResourceManager session (%d)", i_session_id );
882 p_sys->p_sessions[i_session_id - 1].pf_handle = ResourceManagerHandle;
884 APDUSend( p_access, i_session_id, AOT_PROFILE_ENQ, NULL, 0 );
888 * Application Information
891 /*****************************************************************************
892 * ApplicationInformationEnterMenu
893 *****************************************************************************/
894 static void ApplicationInformationEnterMenu( access_t * p_access,
897 access_sys_t *p_sys = p_access->p_sys;
898 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
900 msg_Dbg( p_access, "entering MMI menus on session %d", i_session_id );
901 APDUSend( p_access, i_session_id, AOT_ENTER_MENU, NULL, 0 );
902 p_sys->pb_slot_mmi_expected[i_slot] = true;
905 /*****************************************************************************
906 * ApplicationInformationHandle
907 *****************************************************************************/
908 static void ApplicationInformationHandle( access_t * p_access, int i_session_id,
909 uint8_t *p_apdu, int i_size )
911 int i_tag = APDUGetTag( p_apdu, i_size );
915 case AOT_APPLICATION_INFO:
917 int i_type, i_manufacturer, i_code;
919 uint8_t *d = APDUGetLength( p_apdu, &l );
922 p_apdu[l + 4] = '\0';
925 i_manufacturer = ((int)d[0] << 8) | d[1];
927 i_code = ((int)d[0] << 8) | d[1];
929 d = GetLength( d, &l );
931 msg_Info( p_access, "CAM: %s, %02X, %04X, %04X",
932 d, i_type, i_manufacturer, i_code );
937 "unexpected tag in ApplicationInformationHandle (0x%x)",
942 /*****************************************************************************
943 * ApplicationInformationOpen
944 *****************************************************************************/
945 static void ApplicationInformationOpen( access_t * p_access, int i_session_id )
947 access_sys_t *p_sys = p_access->p_sys;
949 msg_Dbg( p_access, "opening ApplicationInformation session (%d)", i_session_id );
951 p_sys->p_sessions[i_session_id - 1].pf_handle = ApplicationInformationHandle;
953 APDUSend( p_access, i_session_id, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
960 #define MAX_CASYSTEM_IDS 16
964 uint16_t pi_system_ids[MAX_CASYSTEM_IDS + 1];
967 static bool CheckSystemID( system_ids_t *p_ids, uint16_t i_id )
970 if( !p_ids ) return true;
972 while ( p_ids->pi_system_ids[i] )
974 if ( p_ids->pi_system_ids[i] == i_id )
982 /*****************************************************************************
983 * CAPMTNeedsDescrambling
984 *****************************************************************************/
985 static bool CAPMTNeedsDescrambling( dvbpsi_pmt_t *p_pmt )
987 dvbpsi_descriptor_t *p_dr;
988 dvbpsi_pmt_es_t *p_es;
990 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
992 if( p_dr->i_tag == 0x9 )
998 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1000 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
1001 p_dr = p_dr->p_next )
1003 if( p_dr->i_tag == 0x9 )
1013 /*****************************************************************************
1015 *****************************************************************************/
1016 static int GetCADSize( system_ids_t *p_ids, dvbpsi_descriptor_t *p_dr )
1020 while ( p_dr != NULL )
1022 if( p_dr->i_tag == 0x9 )
1024 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1026 if ( CheckSystemID( p_ids, i_sysid ) )
1027 i_cad_size += p_dr->i_length + 2;
1029 p_dr = p_dr->p_next;
1035 static uint8_t *CAPMTHeader( system_ids_t *p_ids, uint8_t i_list_mgt,
1036 uint16_t i_program_number, uint8_t i_version,
1037 int i_size, dvbpsi_descriptor_t *p_dr,
1043 p_data = malloc( 7 + i_size );
1045 p_data = malloc( 6 );
1047 p_data[0] = i_list_mgt;
1048 p_data[1] = i_program_number >> 8;
1049 p_data[2] = i_program_number & 0xff;
1050 p_data[3] = ((i_version & 0x1f) << 1) | 0x1;
1056 p_data[4] = (i_size + 1) >> 8;
1057 p_data[5] = (i_size + 1) & 0xff;
1061 while ( p_dr != NULL )
1063 if( p_dr->i_tag == 0x9 )
1065 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1067 if ( CheckSystemID( p_ids, i_sysid ) )
1070 p_data[i + 1] = p_dr->i_length;
1071 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1072 // p_data[i+4] &= 0x1f;
1073 i += p_dr->i_length + 2;
1076 p_dr = p_dr->p_next;
1088 static uint8_t *CAPMTES( system_ids_t *p_ids, uint8_t *p_capmt,
1089 int i_capmt_size, uint8_t i_type, uint16_t i_pid,
1090 int i_size, dvbpsi_descriptor_t *p_dr,
1097 p_data = realloc( p_capmt, i_capmt_size + 6 + i_size );
1099 p_data = realloc( p_capmt, i_capmt_size + 5 );
1104 p_data[i + 1] = i_pid >> 8;
1105 p_data[i + 2] = i_pid & 0xff;
1109 p_data[i + 3] = (i_size + 1) >> 8;
1110 p_data[i + 4] = (i_size + 1) & 0xff;
1111 p_data[i + 5] = i_cmd;
1114 while ( p_dr != NULL )
1116 if( p_dr->i_tag == 0x9 )
1118 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
1120 if ( CheckSystemID( p_ids, i_sysid ) )
1123 p_data[i + 1] = p_dr->i_length;
1124 memcpy( &p_data[i + 2], p_dr->p_data, p_dr->i_length );
1125 i += p_dr->i_length + 2;
1128 p_dr = p_dr->p_next;
1140 static uint8_t *CAPMTBuild( access_t * p_access, int i_session_id,
1141 dvbpsi_pmt_t *p_pmt, uint8_t i_list_mgt,
1142 uint8_t i_cmd, int *pi_capmt_size )
1144 access_sys_t *p_sys = p_access->p_sys;
1145 system_ids_t *p_ids =
1146 (system_ids_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1147 dvbpsi_pmt_es_t *p_es;
1148 int i_cad_size, i_cad_program_size;
1151 i_cad_size = i_cad_program_size =
1152 GetCADSize( p_ids, p_pmt->p_first_descriptor );
1153 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1155 i_cad_size += GetCADSize( p_ids, p_es->p_first_descriptor );
1161 "no compatible scrambling system for SID %d on session %d",
1162 p_pmt->i_program_number, i_session_id );
1167 p_capmt = CAPMTHeader( p_ids, i_list_mgt, p_pmt->i_program_number,
1168 p_pmt->i_version, i_cad_program_size,
1169 p_pmt->p_first_descriptor, i_cmd );
1171 if ( i_cad_program_size )
1172 *pi_capmt_size = 7 + i_cad_program_size;
1176 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1178 i_cad_size = GetCADSize( p_ids, p_es->p_first_descriptor );
1180 if ( i_cad_size || i_cad_program_size )
1182 p_capmt = CAPMTES( p_ids, p_capmt, *pi_capmt_size, p_es->i_type,
1183 p_es->i_pid, i_cad_size,
1184 p_es->p_first_descriptor, i_cmd );
1186 *pi_capmt_size += 6 + i_cad_size;
1188 *pi_capmt_size += 5;
1195 /*****************************************************************************
1197 *****************************************************************************/
1198 static void CAPMTFirst( access_t * p_access, int i_session_id,
1199 dvbpsi_pmt_t *p_pmt )
1204 msg_Dbg( p_access, "adding first CAPMT for SID %d on session %d",
1205 p_pmt->i_program_number, i_session_id );
1207 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1208 0x3 /* only */, 0x1 /* ok_descrambling */,
1213 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1218 /*****************************************************************************
1220 *****************************************************************************/
1221 static void CAPMTAdd( access_t * p_access, int i_session_id,
1222 dvbpsi_pmt_t *p_pmt )
1227 if( p_access->p_sys->i_selected_programs >= CAM_PROG_MAX )
1229 msg_Warn( p_access, "Not adding CAPMT for SID %d, too many programs",
1230 p_pmt->i_program_number );
1233 p_access->p_sys->i_selected_programs++;
1234 if( p_access->p_sys->i_selected_programs == 1 )
1236 CAPMTFirst( p_access, i_session_id, p_pmt );
1241 msg_Dbg( p_access, "adding CAPMT for SID %d on session %d",
1242 p_pmt->i_program_number, i_session_id );
1244 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1245 0x4 /* add */, 0x1 /* ok_descrambling */,
1250 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1255 /*****************************************************************************
1257 *****************************************************************************/
1258 static void CAPMTUpdate( access_t * p_access, int i_session_id,
1259 dvbpsi_pmt_t *p_pmt )
1264 msg_Dbg( p_access, "updating CAPMT for SID %d on session %d",
1265 p_pmt->i_program_number, i_session_id );
1267 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1268 0x5 /* update */, 0x1 /* ok_descrambling */,
1273 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1278 /*****************************************************************************
1280 *****************************************************************************/
1281 static void CAPMTDelete( access_t * p_access, int i_session_id,
1282 dvbpsi_pmt_t *p_pmt )
1287 p_access->p_sys->i_selected_programs--;
1288 msg_Dbg( p_access, "deleting CAPMT for SID %d on session %d",
1289 p_pmt->i_program_number, i_session_id );
1291 p_capmt = CAPMTBuild( p_access, i_session_id, p_pmt,
1292 0x5 /* update */, 0x4 /* not selected */,
1297 APDUSend( p_access, i_session_id, AOT_CA_PMT, p_capmt, i_capmt_size );
1302 /*****************************************************************************
1303 * ConditionalAccessHandle
1304 *****************************************************************************/
1305 static void ConditionalAccessHandle( access_t * p_access, int i_session_id,
1306 uint8_t *p_apdu, int i_size )
1308 access_sys_t *p_sys = p_access->p_sys;
1309 system_ids_t *p_ids =
1310 (system_ids_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1311 int i_tag = APDUGetTag( p_apdu, i_size );
1319 uint8_t *d = APDUGetLength( p_apdu, &l );
1320 msg_Dbg( p_access, "CA system IDs supported by the application :" );
1322 for ( i = 0; i < l / 2; i++ )
1324 p_ids->pi_system_ids[i] = ((uint16_t)d[0] << 8) | d[1];
1326 msg_Dbg( p_access, "- 0x%x", p_ids->pi_system_ids[i] );
1328 p_ids->pi_system_ids[i] = 0;
1330 for ( i = 0; i < MAX_PROGRAMS; i++ )
1332 if ( p_sys->pp_selected_programs[i] != NULL )
1334 CAPMTAdd( p_access, i_session_id,
1335 p_sys->pp_selected_programs[i] );
1343 "unexpected tag in ConditionalAccessHandle (0x%x)",
1348 /*****************************************************************************
1349 * ConditionalAccessClose
1350 *****************************************************************************/
1351 static void ConditionalAccessClose( access_t * p_access, int i_session_id )
1353 access_sys_t *p_sys = p_access->p_sys;
1355 msg_Dbg( p_access, "closing ConditionalAccess session (%d)", i_session_id );
1357 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1360 /*****************************************************************************
1361 * ConditionalAccessOpen
1362 *****************************************************************************/
1363 static void ConditionalAccessOpen( access_t * p_access, int i_session_id )
1365 access_sys_t *p_sys = p_access->p_sys;
1367 msg_Dbg( p_access, "opening ConditionalAccess session (%d)", i_session_id );
1369 p_sys->p_sessions[i_session_id - 1].pf_handle = ConditionalAccessHandle;
1370 p_sys->p_sessions[i_session_id - 1].pf_close = ConditionalAccessClose;
1371 p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(system_ids_t));
1372 memset( p_sys->p_sessions[i_session_id - 1].p_sys, 0,
1373 sizeof(system_ids_t) );
1375 APDUSend( p_access, i_session_id, AOT_CA_INFO_ENQ, NULL, 0 );
1388 /*****************************************************************************
1390 *****************************************************************************/
1391 static void DateTimeSend( access_t * p_access, int i_session_id )
1393 access_sys_t *p_sys = p_access->p_sys;
1394 date_time_t *p_date =
1395 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1397 time_t t = time(NULL);
1401 if ( gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc) )
1403 int Y = tm_gmt.tm_year;
1404 int M = tm_gmt.tm_mon + 1;
1405 int D = tm_gmt.tm_mday;
1406 int L = (M == 1 || M == 2) ? 1 : 0;
1407 int MJD = 14956 + D + (int)((Y - L) * 365.25)
1408 + (int)((M + 1 + L * 12) * 30.6001);
1409 uint8_t p_response[7];
1411 #define DEC2BCD(d) (((d / 10) << 4) + (d % 10))
1413 p_response[0] = htons(MJD) >> 8;
1414 p_response[1] = htons(MJD) & 0xff;
1415 p_response[2] = DEC2BCD(tm_gmt.tm_hour);
1416 p_response[3] = DEC2BCD(tm_gmt.tm_min);
1417 p_response[4] = DEC2BCD(tm_gmt.tm_sec);
1418 p_response[5] = htons(tm_loc.tm_gmtoff / 60) >> 8;
1419 p_response[6] = htons(tm_loc.tm_gmtoff / 60) & 0xff;
1421 APDUSend( p_access, i_session_id, AOT_DATE_TIME, p_response, 7 );
1423 p_date->i_last = mdate();
1427 /*****************************************************************************
1429 *****************************************************************************/
1430 static void DateTimeHandle( access_t * p_access, int i_session_id,
1431 uint8_t *p_apdu, int i_size )
1433 access_sys_t *p_sys = p_access->p_sys;
1434 date_time_t *p_date =
1435 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1437 int i_tag = APDUGetTag( p_apdu, i_size );
1441 case AOT_DATE_TIME_ENQ:
1444 const uint8_t *d = APDUGetLength( p_apdu, &l );
1448 p_date->i_interval = *d;
1449 msg_Dbg( p_access, "DateTimeHandle : interval set to %d",
1450 p_date->i_interval );
1453 p_date->i_interval = 0;
1455 DateTimeSend( p_access, i_session_id );
1459 msg_Err( p_access, "unexpected tag in DateTimeHandle (0x%x)", i_tag );
1463 /*****************************************************************************
1465 *****************************************************************************/
1466 static void DateTimeManage( access_t * p_access, int i_session_id )
1468 access_sys_t *p_sys = p_access->p_sys;
1469 date_time_t *p_date =
1470 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1472 if ( p_date->i_interval
1473 && mdate() > p_date->i_last + (mtime_t)p_date->i_interval * 1000000 )
1475 DateTimeSend( p_access, i_session_id );
1479 /*****************************************************************************
1481 *****************************************************************************/
1482 static void DateTimeClose( access_t * p_access, int i_session_id )
1484 access_sys_t *p_sys = p_access->p_sys;
1486 msg_Dbg( p_access, "closing DateTime session (%d)", i_session_id );
1488 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1491 /*****************************************************************************
1493 *****************************************************************************/
1494 static void DateTimeOpen( access_t * p_access, int i_session_id )
1496 access_sys_t *p_sys = p_access->p_sys;
1498 msg_Dbg( p_access, "opening DateTime session (%d)", i_session_id );
1500 p_sys->p_sessions[i_session_id - 1].pf_handle = DateTimeHandle;
1501 p_sys->p_sessions[i_session_id - 1].pf_manage = DateTimeManage;
1502 p_sys->p_sessions[i_session_id - 1].pf_close = DateTimeClose;
1503 p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(date_time_t));
1504 memset( p_sys->p_sessions[i_session_id - 1].p_sys, 0, sizeof(date_time_t) );
1506 DateTimeSend( p_access, i_session_id );
1513 /* Display Control Commands */
1515 #define DCC_SET_MMI_MODE 0x01
1516 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1517 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1518 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1519 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1523 #define MM_HIGH_LEVEL 0x01
1524 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1525 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1527 /* Display Reply IDs */
1529 #define DRI_MMI_MODE_ACK 0x01
1530 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1531 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1532 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1533 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1534 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1535 #define DRI_UNKNOWN_MMI_MODE 0xF1
1536 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1540 #define EF_BLIND 0x01
1544 #define AI_CANCEL 0x00
1545 #define AI_ANSWER 0x01
1549 en50221_mmi_object_t last_object;
1552 /*****************************************************************************
1554 *****************************************************************************/
1555 static void MMISendObject( access_t *p_access, int i_session_id,
1556 en50221_mmi_object_t *p_object )
1558 access_sys_t *p_sys = p_access->p_sys;
1559 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1563 switch ( p_object->i_object_type )
1565 case EN50221_MMI_ANSW:
1567 i_size = 1 + strlen( p_object->u.answ.psz_answ );
1568 p_data = malloc( i_size );
1569 p_data[0] = (p_object->u.answ.b_ok == true) ? 0x1 : 0x0;
1570 strncpy( (char *)&p_data[1], p_object->u.answ.psz_answ, i_size - 1 );
1573 case EN50221_MMI_MENU_ANSW:
1574 i_tag = AOT_MENU_ANSW;
1576 p_data = malloc( i_size );
1577 p_data[0] = p_object->u.menu_answ.i_choice;
1581 msg_Err( p_access, "unknown MMI object %d", p_object->i_object_type );
1585 APDUSend( p_access, i_session_id, i_tag, p_data, i_size );
1588 p_sys->pb_slot_mmi_expected[i_slot] = true;
1591 /*****************************************************************************
1593 *****************************************************************************/
1594 static void MMISendClose( access_t *p_access, int i_session_id )
1596 access_sys_t *p_sys = p_access->p_sys;
1597 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1599 APDUSend( p_access, i_session_id, AOT_CLOSE_MMI, NULL, 0 );
1601 p_sys->pb_slot_mmi_expected[i_slot] = true;
1604 /*****************************************************************************
1606 *****************************************************************************/
1607 static void MMIDisplayReply( access_t *p_access, int i_session_id )
1609 uint8_t p_response[2];
1611 p_response[0] = DRI_MMI_MODE_ACK;
1612 p_response[1] = MM_HIGH_LEVEL;
1614 APDUSend( p_access, i_session_id, AOT_DISPLAY_REPLY, p_response, 2 );
1616 msg_Dbg( p_access, "sending DisplayReply on session (%d)", i_session_id );
1619 /*****************************************************************************
1621 *****************************************************************************/
1622 static char *MMIGetText( access_t *p_access, uint8_t **pp_apdu, int *pi_size )
1624 int i_tag = APDUGetTag( *pp_apdu, *pi_size );
1628 if ( i_tag != AOT_TEXT_LAST )
1630 msg_Err( p_access, "unexpected text tag: %06x", i_tag );
1632 return strdup( "" );
1635 d = APDUGetLength( *pp_apdu, &l );
1640 return dvbsi_to_utf8((char*)d,l);
1643 /*****************************************************************************
1645 *****************************************************************************/
1646 static void MMIHandleEnq( access_t *p_access, int i_session_id,
1647 uint8_t *p_apdu, int i_size )
1649 access_sys_t *p_sys = p_access->p_sys;
1650 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1651 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1653 uint8_t *d = APDUGetLength( p_apdu, &l );
1655 en50221_MMIFree( &p_mmi->last_object );
1656 p_mmi->last_object.i_object_type = EN50221_MMI_ENQ;
1657 p_mmi->last_object.u.enq.b_blind = (*d & 0x1) ? true : false;
1658 d += 2; /* skip answer_text_length because it is not mandatory */
1660 p_mmi->last_object.u.enq.psz_text = malloc( l + 1 );
1661 strncpy( p_mmi->last_object.u.enq.psz_text, (char *)d, l );
1662 p_mmi->last_object.u.enq.psz_text[l] = '\0';
1664 msg_Dbg( p_access, "MMI enq: %s%s", p_mmi->last_object.u.enq.psz_text,
1665 p_mmi->last_object.u.enq.b_blind == true ? " (blind)" : "" );
1666 p_sys->pb_slot_mmi_expected[i_slot] = false;
1667 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1670 /*****************************************************************************
1672 *****************************************************************************/
1673 static void MMIHandleMenu( access_t *p_access, int i_session_id, int i_tag,
1674 uint8_t *p_apdu, int i_size )
1676 access_sys_t *p_sys = p_access->p_sys;
1677 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1678 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1680 uint8_t *d = APDUGetLength( p_apdu, &l );
1682 en50221_MMIFree( &p_mmi->last_object );
1683 p_mmi->last_object.i_object_type = (i_tag == AOT_MENU_LAST) ?
1684 EN50221_MMI_MENU : EN50221_MMI_LIST;
1685 p_mmi->last_object.u.menu.i_choices = 0;
1686 p_mmi->last_object.u.menu.ppsz_choices = NULL;
1690 l--; d++; /* choice_nb */
1692 #define GET_FIELD( x ) \
1695 p_mmi->last_object.u.menu.psz_##x \
1696 = MMIGetText( p_access, &d, &l ); \
1697 msg_Dbg( p_access, "MMI " STRINGIFY( x ) ": %s", \
1698 p_mmi->last_object.u.menu.psz_##x ); \
1702 GET_FIELD( subtitle );
1703 GET_FIELD( bottom );
1708 char *psz_text = MMIGetText( p_access, &d, &l );
1709 TAB_APPEND( p_mmi->last_object.u.menu.i_choices,
1710 p_mmi->last_object.u.menu.ppsz_choices,
1712 msg_Dbg( p_access, "MMI choice: %s", psz_text );
1715 p_sys->pb_slot_mmi_expected[i_slot] = false;
1716 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1719 /*****************************************************************************
1721 *****************************************************************************/
1722 static void MMIHandle( access_t *p_access, int i_session_id,
1723 uint8_t *p_apdu, int i_size )
1725 int i_tag = APDUGetTag( p_apdu, i_size );
1729 case AOT_DISPLAY_CONTROL:
1732 uint8_t *d = APDUGetLength( p_apdu, &l );
1738 case DCC_SET_MMI_MODE:
1739 if ( l == 2 && d[1] == MM_HIGH_LEVEL )
1740 MMIDisplayReply( p_access, i_session_id );
1742 msg_Err( p_access, "unsupported MMI mode %02x", d[1] );
1746 msg_Err( p_access, "unsupported display control command %02x",
1755 MMIHandleEnq( p_access, i_session_id, p_apdu, i_size );
1760 MMIHandleMenu( p_access, i_session_id, i_tag, p_apdu, i_size );
1764 SessionSendClose( p_access, i_session_id );
1768 msg_Err( p_access, "unexpected tag in MMIHandle (0x%x)", i_tag );
1772 /*****************************************************************************
1774 *****************************************************************************/
1775 static void MMIClose( access_t *p_access, int i_session_id )
1777 access_sys_t *p_sys = p_access->p_sys;
1778 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1779 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1781 en50221_MMIFree( &p_mmi->last_object );
1782 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1784 msg_Dbg( p_access, "closing MMI session (%d)", i_session_id );
1785 p_sys->pb_slot_mmi_expected[i_slot] = false;
1786 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1789 /*****************************************************************************
1791 *****************************************************************************/
1792 static void MMIOpen( access_t *p_access, int i_session_id )
1794 access_sys_t *p_sys = p_access->p_sys;
1797 msg_Dbg( p_access, "opening MMI session (%d)", i_session_id );
1799 p_sys->p_sessions[i_session_id - 1].pf_handle = MMIHandle;
1800 p_sys->p_sessions[i_session_id - 1].pf_close = MMIClose;
1801 p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(mmi_t));
1802 p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1803 p_mmi->last_object.i_object_type = EN50221_MMI_NONE;
1811 /*****************************************************************************
1812 * InitSlot: Open the transport layer
1813 *****************************************************************************/
1814 #define MAX_TC_RETRIES 20
1816 static int InitSlot( access_t * p_access, int i_slot )
1818 access_sys_t *p_sys = p_access->p_sys;
1821 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, 0 )
1824 msg_Err( p_access, "en50221_Init: couldn't send TPDU on slot %d",
1826 return VLC_EGENERIC;
1829 /* This is out of the spec */
1830 for ( i = 0; i < MAX_TC_RETRIES; i++ )
1833 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) == VLC_SUCCESS
1834 && i_tag == T_CTC_REPLY )
1836 p_sys->pb_active_slot[i_slot] = true;
1840 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, NULL )
1844 "en50221_Init: couldn't send TPDU on slot %d",
1850 if ( p_sys->pb_active_slot[i_slot] )
1852 p_sys->i_ca_timeout = 100000;
1856 return VLC_EGENERIC;
1861 * External entry points
1864 /*****************************************************************************
1865 * en50221_Init : Initialize the CAM for en50221
1866 *****************************************************************************/
1867 int en50221_Init( access_t * p_access )
1869 access_sys_t *p_sys = p_access->p_sys;
1871 if( p_sys->i_ca_type & CA_CI_LINK )
1874 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1876 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1878 msg_Err( p_access, "en50221_Init: couldn't reset slot %d",
1883 p_sys->i_ca_timeout = 100000;
1884 /* Wait a bit otherwise it doesn't initialize properly... */
1891 struct ca_slot_info info;
1894 /* We don't reset the CAM in that case because it's done by the
1896 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &info ) < 0 )
1898 msg_Err( p_access, "en50221_Init: couldn't get slot info" );
1899 close( p_sys->i_ca_handle );
1900 p_sys->i_ca_handle = 0;
1901 return VLC_EGENERIC;
1903 if( info.flags == 0 )
1905 msg_Err( p_access, "en50221_Init: no CAM inserted" );
1906 close( p_sys->i_ca_handle );
1907 p_sys->i_ca_handle = 0;
1908 return VLC_EGENERIC;
1911 /* Allocate a dummy sessions */
1912 p_sys->p_sessions[ 0 ].i_resource_id = RI_CONDITIONAL_ACCESS_SUPPORT;
1914 /* Get application info to find out which cam we are using and make
1915 sure everything is ready to play */
1918 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1919 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1920 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1921 memset( &ca_msg.msg[3], 0, 253 );
1922 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1923 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1925 msg_Err( p_access, "en50221_Init: failed getting message" );
1926 return VLC_EGENERIC;
1929 #if HLCI_WAIT_CAM_READY
1930 while( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1932 if( !vlc_object_alive (p_access) ) return VLC_EGENERIC;
1934 msg_Dbg( p_access, "CAM: please wait" );
1935 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1937 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1938 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1939 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1940 memset( &ca_msg.msg[3], 0, 253 );
1941 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1943 msg_Err( p_access, "en50221_Init: failed getting message" );
1944 return VLC_EGENERIC;
1946 msg_Dbg( p_access, "en50221_Init: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
1949 if( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1951 msg_Err( p_access, "CAM returns garbage as application info!" );
1952 return VLC_EGENERIC;
1955 msg_Dbg( p_access, "found CAM %s using id 0x%x", &ca_msg.msg[12],
1956 (ca_msg.msg[8]<<8)|ca_msg.msg[9] );
1961 /*****************************************************************************
1962 * en50221_Poll : Poll the CAM for TPDUs
1963 *****************************************************************************/
1964 int en50221_Poll( access_t * p_access )
1966 access_sys_t *p_sys = p_access->p_sys;
1970 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1973 ca_slot_info_t sinfo;
1976 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1978 msg_Err( p_access, "en50221_Poll: couldn't get info on slot %d",
1983 if ( !(sinfo.flags & CA_CI_MODULE_READY) )
1985 if ( p_sys->pb_active_slot[i_slot] )
1987 msg_Dbg( p_access, "en50221_Poll: slot %d has been removed",
1989 p_sys->pb_active_slot[i_slot] = false;
1990 p_sys->pb_slot_mmi_expected[i_slot] = false;
1991 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1993 /* Close all sessions for this slot. */
1994 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS;
1997 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
1998 && p_sys->p_sessions[i_session_id - 1].i_slot
2001 if ( p_sys->p_sessions[i_session_id - 1].pf_close
2004 p_sys->p_sessions[i_session_id - 1].pf_close(
2005 p_access, i_session_id );
2007 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
2014 else if ( !p_sys->pb_active_slot[i_slot] )
2016 InitSlot( p_access, i_slot );
2018 if ( !p_sys->pb_active_slot[i_slot] )
2020 msg_Dbg( p_access, "en50221_Poll: resetting slot %d", i_slot );
2022 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
2024 msg_Err( p_access, "en50221_Poll: couldn't reset slot %d",
2030 msg_Dbg( p_access, "en50221_Poll: slot %d is active",
2034 if ( !p_sys->pb_tc_has_data[i_slot] )
2036 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, NULL, NULL ) !=
2040 "en50221_Poll: couldn't send TPDU on slot %d",
2044 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) !=
2048 "en50221_Poll: couldn't recv TPDU on slot %d",
2054 while ( p_sys->pb_tc_has_data[i_slot] )
2056 uint8_t p_tpdu[MAX_TPDU_SIZE];
2057 int i_size, i_session_size;
2060 if ( TPDUSend( p_access, i_slot, T_RCV, NULL, 0 ) != VLC_SUCCESS )
2063 "en50221_Poll: couldn't send TPDU on slot %d",
2067 if ( TPDURecv( p_access, i_slot, &i_tag, p_tpdu, &i_size ) !=
2071 "en50221_Poll: couldn't recv TPDU on slot %d",
2076 p_session = GetLength( &p_tpdu[3], &i_session_size );
2077 if ( i_session_size <= 1 )
2083 if ( i_tag != T_DATA_LAST )
2086 "en50221_Poll: fragmented TPDU not supported" );
2090 SPDUHandle( p_access, i_slot, p_session, i_session_size );
2094 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2096 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2097 && p_sys->p_sessions[i_session_id - 1].pf_manage )
2099 p_sys->p_sessions[i_session_id - 1].pf_manage( p_access,
2108 /*****************************************************************************
2109 * en50221_SetCAPMT :
2110 *****************************************************************************/
2111 int en50221_SetCAPMT( access_t * p_access, dvbpsi_pmt_t *p_pmt )
2113 access_sys_t *p_sys = p_access->p_sys;
2114 int i, i_session_id;
2115 bool b_update = false;
2116 bool b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
2118 for ( i = 0; i < MAX_PROGRAMS; i++ )
2120 if ( p_sys->pp_selected_programs[i] != NULL
2121 && p_sys->pp_selected_programs[i]->i_program_number
2122 == p_pmt->i_program_number )
2126 if ( !b_needs_descrambling )
2128 dvbpsi_DeletePMT( p_pmt );
2129 p_pmt = p_sys->pp_selected_programs[i];
2130 p_sys->pp_selected_programs[i] = NULL;
2132 else if( p_pmt != p_sys->pp_selected_programs[i] )
2134 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2135 p_sys->pp_selected_programs[i] = p_pmt;
2142 if ( !b_update && b_needs_descrambling )
2144 for ( i = 0; i < MAX_PROGRAMS; i++ )
2146 if ( p_sys->pp_selected_programs[i] == NULL )
2148 p_sys->pp_selected_programs[i] = p_pmt;
2154 if ( b_update || b_needs_descrambling )
2156 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2158 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2159 == RI_CONDITIONAL_ACCESS_SUPPORT )
2161 if ( b_update && b_needs_descrambling )
2162 CAPMTUpdate( p_access, i_session_id, p_pmt );
2163 else if ( b_update )
2164 CAPMTDelete( p_access, i_session_id, p_pmt );
2166 CAPMTAdd( p_access, i_session_id, p_pmt );
2171 if ( !b_needs_descrambling )
2173 dvbpsi_DeletePMT( p_pmt );
2179 /*****************************************************************************
2181 *****************************************************************************/
2182 int en50221_OpenMMI( access_t * p_access, int i_slot )
2184 access_sys_t *p_sys = p_access->p_sys;
2186 if( p_sys->i_ca_type & CA_CI_LINK )
2189 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2191 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2192 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2195 "MMI menu is already opened on slot %d (session=%d)",
2196 i_slot, i_session_id );
2201 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2203 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2204 == RI_APPLICATION_INFORMATION
2205 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2207 ApplicationInformationEnterMenu( p_access, i_session_id );
2212 msg_Err( p_access, "no application information on slot %d", i_slot );
2213 return VLC_EGENERIC;
2217 msg_Err( p_access, "MMI menu not supported" );
2218 return VLC_EGENERIC;
2222 /*****************************************************************************
2223 * en50221_CloseMMI :
2224 *****************************************************************************/
2225 int en50221_CloseMMI( access_t * p_access, int i_slot )
2227 access_sys_t *p_sys = p_access->p_sys;
2229 if( p_sys->i_ca_type & CA_CI_LINK )
2232 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2234 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2235 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2237 MMISendClose( p_access, i_session_id );
2242 msg_Warn( p_access, "closing a non-existing MMI session on slot %d",
2244 return VLC_EGENERIC;
2248 msg_Err( p_access, "MMI menu not supported" );
2249 return VLC_EGENERIC;
2253 /*****************************************************************************
2254 * en50221_GetMMIObject :
2255 *****************************************************************************/
2256 en50221_mmi_object_t *en50221_GetMMIObject( access_t * p_access,
2259 access_sys_t *p_sys = p_access->p_sys;
2262 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
2263 return NULL; /* should not happen */
2265 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2267 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2268 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2271 (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
2272 if ( p_mmi == NULL )
2273 return NULL; /* should not happen */
2274 return &p_mmi->last_object;
2282 /*****************************************************************************
2283 * en50221_SendMMIObject :
2284 *****************************************************************************/
2285 void en50221_SendMMIObject( access_t * p_access, int i_slot,
2286 en50221_mmi_object_t *p_object )
2288 access_sys_t *p_sys = p_access->p_sys;
2291 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2293 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2294 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2296 MMISendObject( p_access, i_session_id, p_object );
2301 msg_Err( p_access, "SendMMIObject when no MMI session is opened !" );
2304 /*****************************************************************************
2306 *****************************************************************************/
2307 void en50221_End( access_t * p_access )
2309 access_sys_t *p_sys = p_access->p_sys;
2310 int i_session_id, i;
2312 for ( i = 0; i < MAX_PROGRAMS; i++ )
2314 if ( p_sys->pp_selected_programs[i] != NULL )
2316 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2320 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2322 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2323 && p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
2325 p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
2330 /* Leave the CAM configured, so that it can be reused in another
2334 static inline void *FixUTF8( char *p )
2340 char *dvbsi_to_utf8( char *psz_instring, size_t i_length )
2342 const char *psz_encoding, *psz_stringstart;
2343 char *psz_outstring, *psz_tmp;
2344 char psz_encbuf[12];
2346 vlc_iconv_t iconv_handle;
2347 if( i_length < 1 ) return NULL;
2348 if( psz_instring[0] < 0 || psz_instring[0] >= 0x20 )
2350 psz_stringstart = psz_instring;
2351 psz_encoding = "ISO_8859-1"; /* should be ISO6937 according to spec, but this seems to be the one used */
2352 } else switch( psz_instring[0] )
2355 psz_stringstart = &psz_instring[1];
2356 psz_encoding = "ISO_8859-5";
2359 psz_stringstart = &psz_instring[1];
2360 psz_encoding = "ISO_8859-6";
2363 psz_stringstart = &psz_instring[1];
2364 psz_encoding = "ISO_8859-7";
2367 psz_stringstart = &psz_instring[1];
2368 psz_encoding = "ISO_8859-8";
2371 psz_stringstart = &psz_instring[1];
2372 psz_encoding = "ISO_8859-9";
2375 psz_stringstart = &psz_instring[1];
2376 psz_encoding = "ISO_8859-10";
2379 psz_stringstart = &psz_instring[1];
2380 psz_encoding = "ISO_8859-11";
2383 psz_stringstart = &psz_instring[1]; /*possibly reserved?*/
2384 psz_encoding = "ISO_8859-12";
2387 psz_stringstart = &psz_instring[1];
2388 psz_encoding = "ISO_8859-13";
2391 psz_stringstart = &psz_instring[1];
2392 psz_encoding = "ISO_8859-14";
2395 psz_stringstart = &psz_instring[1];
2396 psz_encoding = "ISO_8859-15";
2399 if( i_length < 3 || psz_instring[1] != '\0' || psz_instring[2] > 0x0f
2400 || psz_instring[2] == 0 )
2401 return FixUTF8(strndup(psz_instring,i_length));
2402 sprintf( psz_encbuf, "ISO_8859-%d", psz_instring[2] );
2403 psz_stringstart = &psz_instring[3];
2404 psz_encoding = psz_encbuf;
2407 psz_stringstart = &psz_instring[1];
2408 psz_encoding = "UTF-16";
2411 psz_stringstart = &psz_instring[1];
2412 psz_encoding = "KSC5601-1987";
2415 psz_stringstart = &psz_instring[1];
2416 psz_encoding = "GB2312";/*GB-2312-1980 */
2419 psz_stringstart = &psz_instring[1];
2420 psz_encoding = "BIG-5";
2423 return FixUTF8(strndup(&psz_instring[1],i_length-1));
2427 return FixUTF8(strndup(psz_instring,i_length));
2429 iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
2430 i_in = i_length - (psz_stringstart - psz_instring );
2432 psz_outstring = psz_tmp = (char*)malloc( i_out * sizeof(char) + 1 );
2433 vlc_iconv( iconv_handle, &psz_stringstart, &i_in, &psz_tmp, &i_out );
2434 vlc_iconv_close( iconv_handle );
2436 return psz_outstring;