⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cas_demo.c

📁 EMU_FTAbox_ref441328_code_source ali m3327
💻 C
📖 第 1 页 / 共 2 页
字号:
					result = 0;
					goto exit_cas_sc_t0;
				}

				continue;
			}
			else if (LEN > 5)
			{
				if (sc_is_inserted(sc_id))
				{
					result = sc_write(sc_id, &Buf[5 + counter], Buf[4] - counter);
				}
				else
				{
					result = 0;
					goto exit_cas_sc_t0;
				}

				if (sc_is_inserted(sc_id))
				{
					result = sc_read(sc_id, buf, 1, &read_bytes);
    				if (result == 0)
    					goto exit_cas_sc_t0;
				}
				else
				{
					result = 0;
					goto exit_cas_sc_t0;
				}
			}
			done = 0;
			continue;
		}
		else if (((INS ^ ACK) == 0xff) || ((INS ^ ACK) == 0xfe))
		{
			if (sc_is_inserted(sc_id))
			{
				result = sc_write(sc_id, &Buf[5 + counter], 1);
			}
			else
			{
				result = 0;
				goto exit_cas_sc_t0;
			}

			counter++;
			if (sc_is_inserted(sc_id))
			{
				result = sc_read(sc_id, buf, 1, &read_bytes);
    			if (result == 0)
    				goto exit_cas_sc_t0;
			}
			else
			{
				result = 0;
				goto exit_cas_sc_t0;
			}

			continue;
		}
		else
		{
			done = 1;
			result = 0;

			goto exit_cas_sc_t0;
		}
	} while (done = 1);

	if (sc_is_inserted(sc_id))
	{
		result = sc_read(sc_id, buf, 1, &read_bytes);
    	if (result == 0)
    		goto exit_cas_sc_t0;
	}
	else
	{
		result = 0;
		goto exit_cas_sc_t0;
	}

	*SW2 = buf[0];
exit_cas_sc_t0:
	return(result);
}

int cas_sc_t1 (int sci_id, unsigned char *Data, int Length, unsigned char *SW1, unsigned char *SW2)
{
	/* for nagra */
	/* not used now */
	return 0;
}


int cas_sc_t14(int sc_id, unsigned char *Data, int Length, unsigned char *SW1, unsigned char *SW2)
{
	int read_bytes;
	int total_read_bytes = 0;
	int length;
	int retry_count;
	int result = 0;

	if (cas_get_debug_level() >= 1)
	{
		cas_print("\r\n-> ");
		cas_print_buf(Data, Length);
	}

	length = Data[5] + 6 + 1;
	Data[length - 1] = gen_t14_checksum(Data, length);

	result = sc_write(sc_id, Data, length);

	cas_sleep(10);

	if (result == 1)
	{
		total_read_bytes = 0;
		retry_count = 0;
		do
		{
			read_bytes = 0;
			result = sc_read(sc_id, Data + total_read_bytes, 8, &read_bytes);
			total_read_bytes += read_bytes;
			retry_count++;
			cas_sleep(1);
		}
		while ((total_read_bytes < 8) && (retry_count < 3));

		if (total_read_bytes >= 8)
		{
			length = Data[7] + 1;

			retry_count = 0;
			do
			{
				read_bytes = 0;
				result = sc_read(sc_id, Data + total_read_bytes, length, &read_bytes);
				total_read_bytes += read_bytes;

    			retry_count++;
				cas_sleep(1);
			}
			while ((length > (total_read_bytes - 8)) && (retry_count < 3));
		}

		if (cas_get_debug_level() >= 1)
		{
			cas_print("\r\n<- ");
			cas_print_buf(Data, total_read_bytes);
		}

		if (total_read_bytes >= 8)
		{
			*SW1 = Data[2];
			*SW2 = Data[3];
		}
	}
	else
	{
		return 1;
	}

	return (!is_t14_checksum(Data, total_read_bytes));
}


#if 1

int main()
{
	unsigned char n2_cat_data[] = {0x01,0xb0,0x1b,0xff,0xff,0xc1,0x00,0x00,0x09,0x04,0x18,0x00,0xe0,0x20,0x09,0x04,0x18,0x01,0xe1,0x20,0x09,0x04,0x18,0x10,0xe1,0x20,0x27,0xd8,0x79,0xc2};

	unsigned char n2_ecm_data[] = {0x81,0x30,0x47,0x07,0x45,0x01,0x01,0x86,0x00,0x08,0x3a,0x8f,0x5b,0x0e,0xc4,0xd1,0x9d,0x32,0x48,0x9c,0x44,0x48,0x36,0x5f,0x0e,0x7e,0xc0,0x4f,0x91,0x2a,0x00,0x64,0xbf,0x51,0x5f,0x8d,0x24,0x76,0x2a,0x1e,0x7f,0x7f,0x5d,0x64,0x16,0x03,0xf5,0x43,0xd9,0x3e,0x55,0x3d,0x26,0xd3,0x7a,0xb6,0xbc,0xb7,0xc1,0x54,0xe1,0x7e,0xf2,0xd4,0x1b,0xb0,0x75,0x65,0xc7,0x04,0x61,0xed,0xf3,0x6d};
	
	unsigned char n2_emm_data[] = {0x82,0x70,0x6C,0x00,0x00,0x00,0x00,0x00,0x04,0x65,0x09,0x01,0x82,0x00,0x10,0x1E,0x11,0xAD,0xEE,0x49,0x9C,0xB4,0x5F,0x24,0x15,0x04,0xB6,0x2F,0x4D,0x4A,0xA5,0x46,0xFF,0x4F,0x35,0x33,0xA7,0xB2,0xAC,0x3C,0x69,0x25,0x1C,0xFA,0xB4,0xD4,0x9D,0x41,0xDF,0x8D,0x13,0x21,0xB3,0x18,0xD2,0xCB,0xD6,0x6A,0x27,0xA2,0x34,0x59,0xFF,0x96,0x3F,0xFF,0xA3,0x77,0x82,0x25,0xEA,0x0C,0x7D,0x2B,0xD3,0xAB,0xB0,0xD9,0x60,0x14,0xDE,0x9F,0x7D,0x19,0xDC,0xD4,0x7E,0xEF,0x77,0x85,0x83,0x59,0xAB,0x60,0x0A,0x92,0x89,0x76,0x55,0xF1,0x11,0xEC,0x4E,0xF7,0xB2,0xB1,0x11,0xC5,0x64,0x92,0x30};
	unsigned char n2_polsat_emm_data[] = {0x82, 0x70, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00,0x04, 0x65, 0x71, 0x01, 0x82, 0x00, 0x98, 0xb9,0xff, 0x1e, 0x99, 0xee, 0x7d, 0x59, 0x88, 0x9b,0xdc, 0x71, 0x14, 0x77, 0x46, 0x41, 0x36, 0xc7,0x41, 0x99, 0xe0, 0x71, 0xc6, 0xb3, 0xab, 0xd5,0x42, 0x41, 0xfc, 0xa9, 0xdd, 0xb7, 0xfc, 0x02,0x9d, 0x74, 0x29, 0x7e, 0x57, 0x61, 0xfb, 0x12,0x7c, 0xd3, 0x57, 0x42, 0x71, 0x03, 0xdd, 0x3b,0x61, 0x44, 0x3c, 0x7c, 0xc1, 0x03, 0x31, 0x7a,0xe3, 0xe7, 0xb8, 0x9a, 0xef, 0x92, 0x1f, 0x35,0x05, 0xd3, 0x5c, 0x10, 0xd4, 0xc6, 0xc3, 0x20,0x39, 0x8e, 0x90, 0x0e, 0xe8, 0x8d, 0x66, 0x9b,0x83, 0xfb, 0xd5, 0xfc, 0x92, 0x30, 0x85, 0x0a,0xd1, 0x6f, 0xb4, 0xa7, 0x53, 0x17, 0x48,};



	unsigned char irdeto_ecm_data[] = {0x80,0x70,0x26,0xfb,0x00,0x02,0x00,0x1f,0x00,0x06,0x00,0x1d,0x78,0x12,0x06,0x12,0xd5,0x78,0xdc,0x31,0xa3,0xa7,0xdb,0x20,0xf6,0x0c,0xd8,0x59,0xf9,0xc2,0x55,0x14,0x40,0x02,0x0b,0x81,0xf2,0xdd,0xec,0x2f,0x3d};
	unsigned char via_ecm_data[] = {0x81,0x70,0x63,0x01,0x90,0x03,0x00,0x7c,0x0b,0xdf,0x3a,0x82,0xb5,0x83,0x3b,0x8f,0x0e,0x3a,0x3b,0xb0,0x39,0xdb,0x3a,0xff,0x30,0xdb,0x3f,0xf6,0xd6,0x9e,0x73,0x64,0x9c,0x22,0x6b,0x39,0x1b,0x00,0x0e,0x9d,0x77,0x56,0x42,0xe5,0xf6,0x69,0x29,0x55,0xd2,0xfb,0x2c,0x27,0xa2,0x04,0x7a,0x3d,0xc9,0xa0,0x32,0x71,0x27,0x21,0x3b,0xcd,0xf9,0x35,0x44,0x6e,0x3a,0xe2,0x03,0x33,0x37,0x32,0xea,0x10,0x9b,0x4e,0x81,0x5e,0xd0,0x93,0xd3,0xb7,0x23,0xa9,0x76,0xd3,0x45,0x0e,0x1d,0xf8,0xf0,0x08,0x76,0xfb,0xbb,0xd7,0xd8,0x9e,0x9e,0xf4};
	unsigned char seca_ecm_data[] = {0x81,0x70,0x61,0x00,0x65,0x00,0x00,0x9d,0x10,0x01,0xf3,0x51,0xaa,0x49,0x33,0x07,0x4e,0x00,0xcc,0x19,0xe2,0x65,0xc1,0x70,0x5f,0x55,0x7a,0x60,0x90,0x28,0x5c,0x46,0xd8,0x0b,0xfd,0xd5,0xe5,0xf8,0x2c,0x90,0x84,0xf5,0x0b18,0xe7,0x6a,0x0c,0xf2,0x70,0x5a,0x57,0x9e,0xfe,0xe5,0x79,0x98,0x32,0x19,0x93,0xfd,0x6f,0x34,0x7b,0x98,0x6c,0xf4,0xd7,0x5e,0xea,0xc5,0x3f,0x44,0x93,0xc4,0xee,0xde,0x4f,0xc4,0x10,0xea,0x13,0xf3,0x1c,0xfa,0x33,0x9d,0xbf,0x43,0x78,0x8c,0x4e,0x48,0xc4,0x94,0x93,0xd4,0x89,0xa3,0xdf,0x2e};
	//unsigned char cw_ecm_data[] = {0x80,0x70,0x6a,0x81,0xff,0x28,0x00,0x65,0x83,0x01,0xc1,0x85,0x48,0x0b,0x95,0x2d,0x86,0x62,0x02,0xcb,0x9f,0x82,0xa6,0xf5,0x9f,0x1f,0x93,0x32,0x7a,0xfa,0x30,0x6d,0x83,0x75,0xec,0xcf,0x8d,0xda,0xf4,0xde,0x3c,0xc3,0x0e,0x06,0x31,0x72,0x8c,0xa9,0x3f,0xde,0x02,0x8b,0x1f,0x32,0xfb,0x98,0xe6,0xc6,0x6a,0xb3,0xda,0xbd,0xb2,0x69,0x6f,0xca,0xa0,0x53,0x6b,0x07,0xbc,0x20,0x8b,0x76,0x4c,0xcc,0xa3,0xf5,0x3c,0x42,0x24,0x6c,0x52,0x64,0x44,0x86,0x08,0xe5,0x6b,0xb0,0x9e,0x2b,0x2d,0xbb,0xd2,0x8e,0x02,0x20,0x99,0xdf,0x08,0xf2,0x0f,0x55,0xf2,0x85,0x73,0x02,0x26};
	unsigned char cw_ecm_data[] = {0x81,0x70,0x84,0x81,0xff,0x28,0x00,0x7f,0x83,0x01,0xc1,0x85,0x4c,0xa5,0x60,0x5e,0x05,0xc7,0x31,0x49,0xba,0x09,0x67,0x45,0x59,0x4d,0x22,0x8b,0x45,0xf2,0x4e,0x78,0xa8,0x7a,0xc6,0x8b,0xd3,0xf7,0x00,0xde,0x39,0xcd,0x59,0xfd,0x7d,0xbe,0x6c,0x97,0xaf,0xc5,0xbb,0xb4,0x76,0xd4,0xe6,0x8a,0xa2,0xd2,0xc1,0x55,0xce,0xdf,0xa7,0xa4,0x9f,0x00,0x8b,0x96,0xf5,0x6b,0x1e,0xcb,0x5a,0xf6,0x43,0x0f,0xd4,0x71,0x11,0x3f,0xde,0x44,0x63,0x77,0xb5,0x36,0xd9,0x5e,0xce,0x86,0x08,0x11,0xf0,0xc2,0x82,0x6b,0x41,0x5c,0x6e,0x8e,0x02,0x20,0x99,0xd6,0x02,0x44,0x50,0xd7,0x10,0x59,0x41,0x50,0x49,0x20,0x4b,0x52,0x45,0x44,0x49,0x20,0x42,0x49,0x5a,0x20,0x54,0xdf,0x08,0xb3,0x3c,0x2f,0x62,0x9f,0xc3,0xa2,0xca};
		
	unsigned char sfi_cat_desc[] = {0x13, 0x01, 0x20, 0x14, 0x03, 0x00, 0x94, 0x00};
	unsigned char sfi_emm_data[] = {0x8e,0x70,0x2c,0x02,0x8c,0xb3,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x4b,0x91,0x50,0x6e,0xd5,0x0f,0x3f,0x6f};

	unsigned char dcw[16];
	int result;

	/* You MUST call this funtion once at booting */
	/* debug_level = 0 : no debug print */
	/* debug_level = 1 : simple debug print */
	/* debug_level = 2 : detail debug print */
	cas_initialize(2);





	/* You MUST call this funtion every channel changing */
	/* channel 0 : for live TV */
	/* channel 1 : for PVR recording */
	/* channel 2 : for other */
	cas_reset_ecm_channel(0);


	/* You MUST call this funtion if biss channel */
	/* program_number should be program number of biss channel */
	result = cas_handle_biss(4, dcw);
	if (result)
	{
		/* dcw : 18 AA 00 C2 C2 D3 28 BD 18 AA 00 C2 C2 D3 28 BD */
		/* set dcw */
		cas_set_dcw(dcw);
	}

	cas_handle_emm(n2_polsat_emm_data, 0x1801);

	/* You MUST call this funtion every cat data */
	cas_handle_cat(n2_cat_data, sizeof(n2_cat_data));


	/* You MUST call this funtion every ecm data */
	/* to get dcw */
	/* channel 0 : for live TV */
	/* channel 1 : for PVR recording */
	/* channel 2 : for other */
	result = cas_handle_ecm(0, n2_ecm_data, 0x100, 0x1801, dcw);
	if (result)
	{
		if (cas_get_debug_level() >= 1)
		{
			if (result&CAS_ECM_OK)
				cas_print("dcw from patch\n");
			if (result&CAS_ECM_SC0_OK)
				cas_print("dcw from first sc\n");
			if (result&CAS_ECM_SC1_OK)
				cas_print("dcw from second sc\n");
		}

		/* dcw : 5D 42 2E CD 27 9F AA 70 84 DD C4 25 66 24 30 BA */
		/* set dcw */
		cas_set_dcw(dcw);
	}


	result = cas_handle_ecm(0, irdeto_ecm_data, 0x100, 0x1762, dcw);
	if (result)
	{
		/* dcw : 76 8F 23 36 32 FF 12 4F 65 33 F1 86 B8 7F D4 B0 */
		cas_set_dcw(dcw);
	}
	
	result = cas_handle_ecm(0, via_ecm_data, 0x100, 0x500, dcw);
	if (result)
	{
		/* dcw : E6 D5 13 5E 23 CC A1 E9 77 02 61 6E 42 5E 6E A0 */
		cas_set_dcw(dcw);
	}

	result = cas_handle_ecm(0, seca_ecm_data, 0x100, 0x100, dcw);
	if (result)
	{
		/* dcw : E6 D5 13 5E 23 CC A1 E9 77 02 61 6E 42 5E 6E A0 */
		cas_set_dcw(dcw);
	}

	result = cas_handle_ecm(0, cw_ecm_data, 0x100, 0x0D03, dcw);
	if (result)
	{
		/* dcw : 20 8b d3 8c f6 93 a4 85 65 46 84 bd 5f 73 20 70 */
		cas_set_dcw(dcw);
	}
	

	/* You MUST call this funtion every emm data */
	/* to update card & key */
	cas_handle_emm(n2_emm_data, 0x1801);
	/* cas_add_key_db */
	/* 0A 4C 4D A0 9A 68 AC 8F 8B 0D C0 74 3B 70 35 59 */

	/* handling smart card */
	{
		unsigned char irdeto_atr[] = {0x3b,0x9f,0x21,0x0e,0x49,0x52,0x44,0x45,0x54,0x4f,0x20,0x41,0x43,0x53,0x03,0x84,0x55,0xff,0x80,0x6d};

		/* You MUST call this funtion when card is inserted */
		cas_sc_inserted(0,  irdeto_atr, sizeof(irdeto_atr));

		/* You MUST call this funtion when card is extracted */
		cas_sc_extracted(0);
	}

	/* cas_handle_pmt_ca_desc/cas_handle_cat_ca_desc are funtions for ca descriptor */
	/* there are too many ecm/emm pid */
	/* you can check valid ecm/emm pid with these funtions */
	/* you MUST call these function when pmt/cat is arrived */
	{
		unsigned char pmt_ca_desc_data[] = {0x09,0x0f,0x05,0x00,0x03,0x03,0x10,0x01,0x00,0x13,0x01,0x20,0x14,0x03,0x02,0x12,0x00};
		unsigned char cat_ca_desc_data[] = {0x09,0x0c,0x05,0x00,0xe3,0xe8,0x13,0x01,0x20,0x14,0x03,0x02,0x00,0x00};
		unsigned short caid;
		unsigned short ecm_pid;
		unsigned short emm_pid;
		

		/* If you want to check valid ecm pid, */
		/* You can call this funtion every pmt descriptor */
		caid = ((unsigned short)pmt_ca_desc_data[2]<<8) | pmt_ca_desc_data[3];
		ecm_pid = ((unsigned short)pmt_ca_desc_data[4]<<8) | pmt_ca_desc_data[5];
		result = cas_handle_pmt_ca_desc(caid, ecm_pid, pmt_ca_desc_data+6, pmt_ca_desc_data[1]-4);
		if (result)
		{
			cas_print("valid ecm pid\n");

			/* start ecm */
		}
		else
		{
			cas_print("invalid ecm pid\n");
		}

		/* If you want to check valid emm pid, */
		/* You can call this funtion every cat descriptor */
		caid = ((unsigned short)cat_ca_desc_data[2]<<8) | cat_ca_desc_data[3];
		emm_pid = ((unsigned short)cat_ca_desc_data[4]<<8) | cat_ca_desc_data[5];
		result = cas_handle_cat_ca_desc(caid, emm_pid, cat_ca_desc_data+6, cat_ca_desc_data[1]-4);
		if (result)
		{
			cas_print("valid emm pid\n");

			/* start emm */
		}
		else
		{
			cas_print("invalid emm pid\n");
		}
	}

	/* handling key database */
	{
		cas_key_db key_db;
		int count;
		int i;

		/* if user want to reset the key database, then call this function */
		cas_reset_key_db();

		/* to get maximum key count */
		count = cas_get_max_key_count();
		
		for(i=0; i <count; i++)
		{
			/* to get key data */
			result = cas_get_key_db(i, &key_db);

			if (result)
			{
				if (cas_get_debug_level() >= 2)
				{
					/* to get provider name */
					cas_print(cas_get_provider_name(key_db.provider, key_db.ca_id));
					cas_print(" : ");
					cas_print_buf(key_db.key, max_key_size[key_db.key_type]);			
				}
			}
		}

		/* to add key */
		{
			unsigned char key[] = {0x03,0xA8,0x57,0x61,0x0B,0x5F,0x12,0x69};
			
			key_db.ca_id = 0x500;
			key_db.index = 0x8;
			key_db.key_type = VIA_KEY8;
			key_db.provider = 0x7a00;
			memcpy(key_db.key, key, max_key_size[key_db.key_type]);

			cas_add_key_db(count, &key_db);
		}

		/* to update key */
		{
			unsigned char key[] = {0xF7,0xCF,0xC6,0x42,0xB9,0x84,0xCB,0xD1};

			cas_get_key_db(20, &key_db);
			
			memcpy(key_db.key, key, max_key_size[key_db.key_type]);
			cas_update_key_db(20, &key_db);
		}
		
		/* to delete key */
		{
			cas_del_key_db(20);
		}
	}


	return 0;
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -