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"
68 # include "tables/sdt.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 = calloc( 1, sizeof(system_ids_t) );
1373 APDUSend( p_access, i_session_id, AOT_CA_INFO_ENQ, NULL, 0 );
1386 /*****************************************************************************
1388 *****************************************************************************/
1389 static void DateTimeSend( access_t * p_access, int i_session_id )
1391 access_sys_t *p_sys = p_access->p_sys;
1392 date_time_t *p_date =
1393 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1395 time_t t = time(NULL);
1399 if ( gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc) )
1401 int Y = tm_gmt.tm_year;
1402 int M = tm_gmt.tm_mon + 1;
1403 int D = tm_gmt.tm_mday;
1404 int L = (M == 1 || M == 2) ? 1 : 0;
1405 int MJD = 14956 + D + (int)((Y - L) * 365.25)
1406 + (int)((M + 1 + L * 12) * 30.6001);
1407 uint8_t p_response[7];
1409 #define DEC2BCD(d) (((d / 10) << 4) + (d % 10))
1411 p_response[0] = htons(MJD) >> 8;
1412 p_response[1] = htons(MJD) & 0xff;
1413 p_response[2] = DEC2BCD(tm_gmt.tm_hour);
1414 p_response[3] = DEC2BCD(tm_gmt.tm_min);
1415 p_response[4] = DEC2BCD(tm_gmt.tm_sec);
1416 p_response[5] = htons(tm_loc.tm_gmtoff / 60) >> 8;
1417 p_response[6] = htons(tm_loc.tm_gmtoff / 60) & 0xff;
1419 APDUSend( p_access, i_session_id, AOT_DATE_TIME, p_response, 7 );
1421 p_date->i_last = mdate();
1425 /*****************************************************************************
1427 *****************************************************************************/
1428 static void DateTimeHandle( access_t * p_access, int i_session_id,
1429 uint8_t *p_apdu, int i_size )
1431 access_sys_t *p_sys = p_access->p_sys;
1432 date_time_t *p_date =
1433 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1435 int i_tag = APDUGetTag( p_apdu, i_size );
1439 case AOT_DATE_TIME_ENQ:
1442 const uint8_t *d = APDUGetLength( p_apdu, &l );
1446 p_date->i_interval = *d;
1447 msg_Dbg( p_access, "DateTimeHandle : interval set to %d",
1448 p_date->i_interval );
1451 p_date->i_interval = 0;
1453 DateTimeSend( p_access, i_session_id );
1457 msg_Err( p_access, "unexpected tag in DateTimeHandle (0x%x)", i_tag );
1461 /*****************************************************************************
1463 *****************************************************************************/
1464 static void DateTimeManage( access_t * p_access, int i_session_id )
1466 access_sys_t *p_sys = p_access->p_sys;
1467 date_time_t *p_date =
1468 (date_time_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1470 if ( p_date->i_interval
1471 && mdate() > p_date->i_last + (mtime_t)p_date->i_interval * 1000000 )
1473 DateTimeSend( p_access, i_session_id );
1477 /*****************************************************************************
1479 *****************************************************************************/
1480 static void DateTimeClose( access_t * p_access, int i_session_id )
1482 access_sys_t *p_sys = p_access->p_sys;
1484 msg_Dbg( p_access, "closing DateTime session (%d)", i_session_id );
1486 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1489 /*****************************************************************************
1491 *****************************************************************************/
1492 static void DateTimeOpen( access_t * p_access, int i_session_id )
1494 access_sys_t *p_sys = p_access->p_sys;
1496 msg_Dbg( p_access, "opening DateTime session (%d)", i_session_id );
1498 p_sys->p_sessions[i_session_id - 1].pf_handle = DateTimeHandle;
1499 p_sys->p_sessions[i_session_id - 1].pf_manage = DateTimeManage;
1500 p_sys->p_sessions[i_session_id - 1].pf_close = DateTimeClose;
1501 p_sys->p_sessions[i_session_id - 1].p_sys = calloc( 1, sizeof(date_time_t) );
1503 DateTimeSend( p_access, i_session_id );
1510 /* Display Control Commands */
1512 #define DCC_SET_MMI_MODE 0x01
1513 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1514 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1515 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1516 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1520 #define MM_HIGH_LEVEL 0x01
1521 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1522 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1524 /* Display Reply IDs */
1526 #define DRI_MMI_MODE_ACK 0x01
1527 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1528 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1529 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1530 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1531 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1532 #define DRI_UNKNOWN_MMI_MODE 0xF1
1533 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1537 #define EF_BLIND 0x01
1541 #define AI_CANCEL 0x00
1542 #define AI_ANSWER 0x01
1546 en50221_mmi_object_t last_object;
1549 /*****************************************************************************
1551 *****************************************************************************/
1552 static void MMISendObject( access_t *p_access, int i_session_id,
1553 en50221_mmi_object_t *p_object )
1555 access_sys_t *p_sys = p_access->p_sys;
1556 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1560 switch ( p_object->i_object_type )
1562 case EN50221_MMI_ANSW:
1564 i_size = 1 + strlen( p_object->u.answ.psz_answ );
1565 p_data = malloc( i_size );
1566 p_data[0] = (p_object->u.answ.b_ok == true) ? 0x1 : 0x0;
1567 strncpy( (char *)&p_data[1], p_object->u.answ.psz_answ, i_size - 1 );
1570 case EN50221_MMI_MENU_ANSW:
1571 i_tag = AOT_MENU_ANSW;
1573 p_data = malloc( i_size );
1574 p_data[0] = p_object->u.menu_answ.i_choice;
1578 msg_Err( p_access, "unknown MMI object %d", p_object->i_object_type );
1582 APDUSend( p_access, i_session_id, i_tag, p_data, i_size );
1585 p_sys->pb_slot_mmi_expected[i_slot] = true;
1588 /*****************************************************************************
1590 *****************************************************************************/
1591 static void MMISendClose( access_t *p_access, int i_session_id )
1593 access_sys_t *p_sys = p_access->p_sys;
1594 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1596 APDUSend( p_access, i_session_id, AOT_CLOSE_MMI, NULL, 0 );
1598 p_sys->pb_slot_mmi_expected[i_slot] = true;
1601 /*****************************************************************************
1603 *****************************************************************************/
1604 static void MMIDisplayReply( access_t *p_access, int i_session_id )
1606 uint8_t p_response[2];
1608 p_response[0] = DRI_MMI_MODE_ACK;
1609 p_response[1] = MM_HIGH_LEVEL;
1611 APDUSend( p_access, i_session_id, AOT_DISPLAY_REPLY, p_response, 2 );
1613 msg_Dbg( p_access, "sending DisplayReply on session (%d)", i_session_id );
1616 /*****************************************************************************
1618 *****************************************************************************/
1619 static char *MMIGetText( access_t *p_access, uint8_t **pp_apdu, int *pi_size )
1621 int i_tag = APDUGetTag( *pp_apdu, *pi_size );
1625 if ( i_tag != AOT_TEXT_LAST )
1627 msg_Err( p_access, "unexpected text tag: %06x", i_tag );
1629 return strdup( "" );
1632 d = APDUGetLength( *pp_apdu, &l );
1637 return dvbsi_to_utf8((char*)d,l);
1640 /*****************************************************************************
1642 *****************************************************************************/
1643 static void MMIHandleEnq( access_t *p_access, int i_session_id,
1644 uint8_t *p_apdu, int i_size )
1646 access_sys_t *p_sys = p_access->p_sys;
1647 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1648 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1650 uint8_t *d = APDUGetLength( p_apdu, &l );
1652 en50221_MMIFree( &p_mmi->last_object );
1653 p_mmi->last_object.i_object_type = EN50221_MMI_ENQ;
1654 p_mmi->last_object.u.enq.b_blind = (*d & 0x1) ? true : false;
1655 d += 2; /* skip answer_text_length because it is not mandatory */
1657 p_mmi->last_object.u.enq.psz_text = malloc( l + 1 );
1658 strncpy( p_mmi->last_object.u.enq.psz_text, (char *)d, l );
1659 p_mmi->last_object.u.enq.psz_text[l] = '\0';
1661 msg_Dbg( p_access, "MMI enq: %s%s", p_mmi->last_object.u.enq.psz_text,
1662 p_mmi->last_object.u.enq.b_blind == true ? " (blind)" : "" );
1663 p_sys->pb_slot_mmi_expected[i_slot] = false;
1664 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1667 /*****************************************************************************
1669 *****************************************************************************/
1670 static void MMIHandleMenu( access_t *p_access, int i_session_id, int i_tag,
1671 uint8_t *p_apdu, int i_size )
1673 access_sys_t *p_sys = p_access->p_sys;
1674 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1675 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1677 uint8_t *d = APDUGetLength( p_apdu, &l );
1679 en50221_MMIFree( &p_mmi->last_object );
1680 p_mmi->last_object.i_object_type = (i_tag == AOT_MENU_LAST) ?
1681 EN50221_MMI_MENU : EN50221_MMI_LIST;
1682 p_mmi->last_object.u.menu.i_choices = 0;
1683 p_mmi->last_object.u.menu.ppsz_choices = NULL;
1687 l--; d++; /* choice_nb */
1689 #define GET_FIELD( x ) \
1692 p_mmi->last_object.u.menu.psz_##x \
1693 = MMIGetText( p_access, &d, &l ); \
1694 msg_Dbg( p_access, "MMI " STRINGIFY( x ) ": %s", \
1695 p_mmi->last_object.u.menu.psz_##x ); \
1699 GET_FIELD( subtitle );
1700 GET_FIELD( bottom );
1705 char *psz_text = MMIGetText( p_access, &d, &l );
1706 TAB_APPEND( p_mmi->last_object.u.menu.i_choices,
1707 p_mmi->last_object.u.menu.ppsz_choices,
1709 msg_Dbg( p_access, "MMI choice: %s", psz_text );
1712 p_sys->pb_slot_mmi_expected[i_slot] = false;
1713 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1716 /*****************************************************************************
1718 *****************************************************************************/
1719 static void MMIHandle( access_t *p_access, int i_session_id,
1720 uint8_t *p_apdu, int i_size )
1722 int i_tag = APDUGetTag( p_apdu, i_size );
1726 case AOT_DISPLAY_CONTROL:
1729 uint8_t *d = APDUGetLength( p_apdu, &l );
1735 case DCC_SET_MMI_MODE:
1736 if ( l == 2 && d[1] == MM_HIGH_LEVEL )
1737 MMIDisplayReply( p_access, i_session_id );
1739 msg_Err( p_access, "unsupported MMI mode %02x", d[1] );
1743 msg_Err( p_access, "unsupported display control command %02x",
1752 MMIHandleEnq( p_access, i_session_id, p_apdu, i_size );
1757 MMIHandleMenu( p_access, i_session_id, i_tag, p_apdu, i_size );
1761 SessionSendClose( p_access, i_session_id );
1765 msg_Err( p_access, "unexpected tag in MMIHandle (0x%x)", i_tag );
1769 /*****************************************************************************
1771 *****************************************************************************/
1772 static void MMIClose( access_t *p_access, int i_session_id )
1774 access_sys_t *p_sys = p_access->p_sys;
1775 int i_slot = p_sys->p_sessions[i_session_id - 1].i_slot;
1776 mmi_t *p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1778 en50221_MMIFree( &p_mmi->last_object );
1779 free( p_sys->p_sessions[i_session_id - 1].p_sys );
1781 msg_Dbg( p_access, "closing MMI session (%d)", i_session_id );
1782 p_sys->pb_slot_mmi_expected[i_slot] = false;
1783 p_sys->pb_slot_mmi_undisplayed[i_slot] = true;
1786 /*****************************************************************************
1788 *****************************************************************************/
1789 static void MMIOpen( access_t *p_access, int i_session_id )
1791 access_sys_t *p_sys = p_access->p_sys;
1794 msg_Dbg( p_access, "opening MMI session (%d)", i_session_id );
1796 p_sys->p_sessions[i_session_id - 1].pf_handle = MMIHandle;
1797 p_sys->p_sessions[i_session_id - 1].pf_close = MMIClose;
1798 p_sys->p_sessions[i_session_id - 1].p_sys = malloc(sizeof(mmi_t));
1799 p_mmi = (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
1800 p_mmi->last_object.i_object_type = EN50221_MMI_NONE;
1808 /*****************************************************************************
1809 * InitSlot: Open the transport layer
1810 *****************************************************************************/
1811 #define MAX_TC_RETRIES 20
1813 static int InitSlot( access_t * p_access, int i_slot )
1815 access_sys_t *p_sys = p_access->p_sys;
1818 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, 0 )
1821 msg_Err( p_access, "en50221_Init: couldn't send TPDU on slot %d",
1823 return VLC_EGENERIC;
1826 /* This is out of the spec */
1827 for ( i = 0; i < MAX_TC_RETRIES; i++ )
1830 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) == VLC_SUCCESS
1831 && i_tag == T_CTC_REPLY )
1833 p_sys->pb_active_slot[i_slot] = true;
1837 if ( TPDUSend( p_access, i_slot, T_CREATE_TC, NULL, NULL )
1841 "en50221_Init: couldn't send TPDU on slot %d",
1847 if ( p_sys->pb_active_slot[i_slot] )
1849 p_sys->i_ca_timeout = 100000;
1853 return VLC_EGENERIC;
1858 * External entry points
1861 /*****************************************************************************
1862 * en50221_Init : Initialize the CAM for en50221
1863 *****************************************************************************/
1864 int en50221_Init( access_t * p_access )
1866 access_sys_t *p_sys = p_access->p_sys;
1868 if( p_sys->i_ca_type & CA_CI_LINK )
1871 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1873 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
1875 msg_Err( p_access, "en50221_Init: couldn't reset slot %d",
1880 p_sys->i_ca_timeout = 100000;
1881 /* Wait a bit otherwise it doesn't initialize properly... */
1888 struct ca_slot_info info;
1891 /* We don't reset the CAM in that case because it's done by the
1893 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &info ) < 0 )
1895 msg_Err( p_access, "en50221_Init: couldn't get slot info" );
1896 close( p_sys->i_ca_handle );
1897 p_sys->i_ca_handle = 0;
1898 return VLC_EGENERIC;
1900 if( info.flags == 0 )
1902 msg_Err( p_access, "en50221_Init: no CAM inserted" );
1903 close( p_sys->i_ca_handle );
1904 p_sys->i_ca_handle = 0;
1905 return VLC_EGENERIC;
1908 /* Allocate a dummy sessions */
1909 p_sys->p_sessions[ 0 ].i_resource_id = RI_CONDITIONAL_ACCESS_SUPPORT;
1911 /* Get application info to find out which cam we are using and make
1912 sure everything is ready to play */
1915 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1916 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1917 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1918 memset( &ca_msg.msg[3], 0, 253 );
1919 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1920 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1922 msg_Err( p_access, "en50221_Init: failed getting message" );
1923 return VLC_EGENERIC;
1926 #if HLCI_WAIT_CAM_READY
1927 while( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1929 if( !vlc_object_alive (p_access) ) return VLC_EGENERIC;
1931 msg_Dbg( p_access, "CAM: please wait" );
1932 APDUSend( p_access, 1, AOT_APPLICATION_INFO_ENQ, NULL, 0 );
1934 ca_msg.msg[0] = ( AOT_APPLICATION_INFO & 0xFF0000 ) >> 16;
1935 ca_msg.msg[1] = ( AOT_APPLICATION_INFO & 0x00FF00 ) >> 8;
1936 ca_msg.msg[2] = ( AOT_APPLICATION_INFO & 0x0000FF ) >> 0;
1937 memset( &ca_msg.msg[3], 0, 253 );
1938 if ( ioctl( p_sys->i_ca_handle, CA_GET_MSG, &ca_msg ) < 0 )
1940 msg_Err( p_access, "en50221_Init: failed getting message" );
1941 return VLC_EGENERIC;
1943 msg_Dbg( p_access, "en50221_Init: Got length: %d, tag: 0x%x", ca_msg.length, APDUGetTag( ca_msg.msg, ca_msg.length ) );
1946 if( ca_msg.msg[8] == 0xff && ca_msg.msg[9] == 0xff )
1948 msg_Err( p_access, "CAM returns garbage as application info!" );
1949 return VLC_EGENERIC;
1952 msg_Dbg( p_access, "found CAM %s using id 0x%x", &ca_msg.msg[12],
1953 (ca_msg.msg[8]<<8)|ca_msg.msg[9] );
1958 /*****************************************************************************
1959 * en50221_Poll : Poll the CAM for TPDUs
1960 *****************************************************************************/
1961 int en50221_Poll( access_t * p_access )
1963 access_sys_t *p_sys = p_access->p_sys;
1967 for ( i_slot = 0; i_slot < p_sys->i_nb_slots; i_slot++ )
1970 ca_slot_info_t sinfo;
1973 if ( ioctl( p_sys->i_ca_handle, CA_GET_SLOT_INFO, &sinfo ) != 0 )
1975 msg_Err( p_access, "en50221_Poll: couldn't get info on slot %d",
1980 if ( !(sinfo.flags & CA_CI_MODULE_READY) )
1982 if ( p_sys->pb_active_slot[i_slot] )
1984 msg_Dbg( p_access, "en50221_Poll: slot %d has been removed",
1986 p_sys->pb_active_slot[i_slot] = false;
1987 p_sys->pb_slot_mmi_expected[i_slot] = false;
1988 p_sys->pb_slot_mmi_undisplayed[i_slot] = false;
1990 /* Close all sessions for this slot. */
1991 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS;
1994 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
1995 && p_sys->p_sessions[i_session_id - 1].i_slot
1998 if ( p_sys->p_sessions[i_session_id - 1].pf_close
2001 p_sys->p_sessions[i_session_id - 1].pf_close(
2002 p_access, i_session_id );
2004 p_sys->p_sessions[i_session_id - 1].i_resource_id = 0;
2011 else if ( !p_sys->pb_active_slot[i_slot] )
2013 InitSlot( p_access, i_slot );
2015 if ( !p_sys->pb_active_slot[i_slot] )
2017 msg_Dbg( p_access, "en50221_Poll: resetting slot %d", i_slot );
2019 if ( ioctl( p_sys->i_ca_handle, CA_RESET, 1 << i_slot) != 0 )
2021 msg_Err( p_access, "en50221_Poll: couldn't reset slot %d",
2027 msg_Dbg( p_access, "en50221_Poll: slot %d is active",
2031 if ( !p_sys->pb_tc_has_data[i_slot] )
2033 if ( TPDUSend( p_access, i_slot, T_DATA_LAST, NULL, NULL ) !=
2037 "en50221_Poll: couldn't send TPDU on slot %d",
2041 if ( TPDURecv( p_access, i_slot, &i_tag, NULL, NULL ) !=
2045 "en50221_Poll: couldn't recv TPDU on slot %d",
2051 while ( p_sys->pb_tc_has_data[i_slot] )
2053 uint8_t p_tpdu[MAX_TPDU_SIZE];
2054 int i_size, i_session_size;
2057 if ( TPDUSend( p_access, i_slot, T_RCV, NULL, 0 ) != VLC_SUCCESS )
2060 "en50221_Poll: couldn't send TPDU on slot %d",
2064 if ( TPDURecv( p_access, i_slot, &i_tag, p_tpdu, &i_size ) !=
2068 "en50221_Poll: couldn't recv TPDU on slot %d",
2073 p_session = GetLength( &p_tpdu[3], &i_session_size );
2074 if ( i_session_size <= 1 )
2080 if ( i_tag != T_DATA_LAST )
2083 "en50221_Poll: fragmented TPDU not supported" );
2087 SPDUHandle( p_access, i_slot, p_session, i_session_size );
2091 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2093 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2094 && p_sys->p_sessions[i_session_id - 1].pf_manage )
2096 p_sys->p_sessions[i_session_id - 1].pf_manage( p_access,
2105 /*****************************************************************************
2106 * en50221_SetCAPMT :
2107 *****************************************************************************/
2108 int en50221_SetCAPMT( access_t * p_access, dvbpsi_pmt_t *p_pmt )
2110 access_sys_t *p_sys = p_access->p_sys;
2111 int i, i_session_id;
2112 bool b_update = false;
2113 bool b_needs_descrambling = CAPMTNeedsDescrambling( p_pmt );
2115 for ( i = 0; i < MAX_PROGRAMS; i++ )
2117 if ( p_sys->pp_selected_programs[i] != NULL
2118 && p_sys->pp_selected_programs[i]->i_program_number
2119 == p_pmt->i_program_number )
2123 if ( !b_needs_descrambling )
2125 dvbpsi_DeletePMT( p_pmt );
2126 p_pmt = p_sys->pp_selected_programs[i];
2127 p_sys->pp_selected_programs[i] = NULL;
2129 else if( p_pmt != p_sys->pp_selected_programs[i] )
2131 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2132 p_sys->pp_selected_programs[i] = p_pmt;
2139 if ( !b_update && b_needs_descrambling )
2141 for ( i = 0; i < MAX_PROGRAMS; i++ )
2143 if ( p_sys->pp_selected_programs[i] == NULL )
2145 p_sys->pp_selected_programs[i] = p_pmt;
2151 if ( b_update || b_needs_descrambling )
2153 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2155 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2156 == RI_CONDITIONAL_ACCESS_SUPPORT )
2158 if ( b_update && b_needs_descrambling )
2159 CAPMTUpdate( p_access, i_session_id, p_pmt );
2160 else if ( b_update )
2161 CAPMTDelete( p_access, i_session_id, p_pmt );
2163 CAPMTAdd( p_access, i_session_id, p_pmt );
2168 if ( !b_needs_descrambling )
2170 dvbpsi_DeletePMT( p_pmt );
2176 /*****************************************************************************
2178 *****************************************************************************/
2179 int en50221_OpenMMI( access_t * p_access, int i_slot )
2181 access_sys_t *p_sys = p_access->p_sys;
2183 if( p_sys->i_ca_type & CA_CI_LINK )
2186 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2188 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2189 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2192 "MMI menu is already opened on slot %d (session=%d)",
2193 i_slot, i_session_id );
2198 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2200 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2201 == RI_APPLICATION_INFORMATION
2202 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2204 ApplicationInformationEnterMenu( p_access, i_session_id );
2209 msg_Err( p_access, "no application information on slot %d", i_slot );
2210 return VLC_EGENERIC;
2214 msg_Err( p_access, "MMI menu not supported" );
2215 return VLC_EGENERIC;
2219 /*****************************************************************************
2220 * en50221_CloseMMI :
2221 *****************************************************************************/
2222 int en50221_CloseMMI( access_t * p_access, int i_slot )
2224 access_sys_t *p_sys = p_access->p_sys;
2226 if( p_sys->i_ca_type & CA_CI_LINK )
2229 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2231 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2232 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2234 MMISendClose( p_access, i_session_id );
2239 msg_Warn( p_access, "closing a non-existing MMI session on slot %d",
2241 return VLC_EGENERIC;
2245 msg_Err( p_access, "MMI menu not supported" );
2246 return VLC_EGENERIC;
2250 /*****************************************************************************
2251 * en50221_GetMMIObject :
2252 *****************************************************************************/
2253 en50221_mmi_object_t *en50221_GetMMIObject( access_t * p_access,
2256 access_sys_t *p_sys = p_access->p_sys;
2259 if ( p_sys->pb_slot_mmi_expected[i_slot] == true )
2260 return NULL; /* should not happen */
2262 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2264 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2265 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2268 (mmi_t *)p_sys->p_sessions[i_session_id - 1].p_sys;
2269 if ( p_mmi == NULL )
2270 return NULL; /* should not happen */
2271 return &p_mmi->last_object;
2279 /*****************************************************************************
2280 * en50221_SendMMIObject :
2281 *****************************************************************************/
2282 void en50221_SendMMIObject( access_t * p_access, int i_slot,
2283 en50221_mmi_object_t *p_object )
2285 access_sys_t *p_sys = p_access->p_sys;
2288 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2290 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id == RI_MMI
2291 && p_sys->p_sessions[i_session_id - 1].i_slot == i_slot )
2293 MMISendObject( p_access, i_session_id, p_object );
2298 msg_Err( p_access, "SendMMIObject when no MMI session is opened !" );
2301 /*****************************************************************************
2303 *****************************************************************************/
2304 void en50221_End( access_t * p_access )
2306 access_sys_t *p_sys = p_access->p_sys;
2307 int i_session_id, i;
2309 for ( i = 0; i < MAX_PROGRAMS; i++ )
2311 if ( p_sys->pp_selected_programs[i] != NULL )
2313 dvbpsi_DeletePMT( p_sys->pp_selected_programs[i] );
2317 for ( i_session_id = 1; i_session_id <= MAX_SESSIONS; i_session_id++ )
2319 if ( p_sys->p_sessions[i_session_id - 1].i_resource_id
2320 && p_sys->p_sessions[i_session_id - 1].pf_close != NULL )
2322 p_sys->p_sessions[i_session_id - 1].pf_close( p_access,
2327 /* Leave the CAM configured, so that it can be reused in another
2331 static inline void *FixUTF8( char *p )
2337 char *dvbsi_to_utf8( char *psz_instring, size_t i_length )
2339 const char *psz_encoding, *psz_stringstart;
2340 char *psz_outstring, *psz_tmp;
2341 char psz_encbuf[12];
2343 vlc_iconv_t iconv_handle;
2344 if( i_length < 1 ) return NULL;
2345 if( psz_instring[0] < 0 || psz_instring[0] >= 0x20 )
2347 psz_stringstart = psz_instring;
2348 psz_encoding = "ISO_8859-1"; /* should be ISO6937 according to spec, but this seems to be the one used */
2349 } else switch( psz_instring[0] )
2352 psz_stringstart = &psz_instring[1];
2353 psz_encoding = "ISO_8859-5";
2356 psz_stringstart = &psz_instring[1];
2357 psz_encoding = "ISO_8859-6";
2360 psz_stringstart = &psz_instring[1];
2361 psz_encoding = "ISO_8859-7";
2364 psz_stringstart = &psz_instring[1];
2365 psz_encoding = "ISO_8859-8";
2368 psz_stringstart = &psz_instring[1];
2369 psz_encoding = "ISO_8859-9";
2372 psz_stringstart = &psz_instring[1];
2373 psz_encoding = "ISO_8859-10";
2376 psz_stringstart = &psz_instring[1];
2377 psz_encoding = "ISO_8859-11";
2380 psz_stringstart = &psz_instring[1]; /*possibly reserved?*/
2381 psz_encoding = "ISO_8859-12";
2384 psz_stringstart = &psz_instring[1];
2385 psz_encoding = "ISO_8859-13";
2388 psz_stringstart = &psz_instring[1];
2389 psz_encoding = "ISO_8859-14";
2392 psz_stringstart = &psz_instring[1];
2393 psz_encoding = "ISO_8859-15";
2396 if( i_length < 3 || psz_instring[1] != '\0' || psz_instring[2] > 0x0f
2397 || psz_instring[2] == 0 )
2398 return FixUTF8(strndup(psz_instring,i_length));
2399 sprintf( psz_encbuf, "ISO_8859-%d", psz_instring[2] );
2400 psz_stringstart = &psz_instring[3];
2401 psz_encoding = psz_encbuf;
2404 psz_stringstart = &psz_instring[1];
2405 psz_encoding = "UTF-16";
2408 psz_stringstart = &psz_instring[1];
2409 psz_encoding = "KSC5601-1987";
2412 psz_stringstart = &psz_instring[1];
2413 psz_encoding = "GB2312";/*GB-2312-1980 */
2416 psz_stringstart = &psz_instring[1];
2417 psz_encoding = "BIG-5";
2420 return FixUTF8(strndup(&psz_instring[1],i_length-1));
2424 return FixUTF8(strndup(psz_instring,i_length));
2426 iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
2427 i_in = i_length - (psz_stringstart - psz_instring );
2429 psz_outstring = psz_tmp = (char*)malloc( i_out + 1 );
2430 vlc_iconv( iconv_handle, &psz_stringstart, &i_in, &psz_tmp, &i_out );
2431 vlc_iconv_close( iconv_handle );
2433 return psz_outstring;