
/**
 	@file camessage.h	
	@brief CRTVCore's ca command 
	@author l2sw1
	@date 2009.02.10
	*/

#ifndef __CAMESSAGE_H_
#define __CAMESSAGE_H_

#include "jlike_fake.h"

#define TVCORE_PROCESS_ID			0x04
#define TVSDK_PROCESS_ID			33

#define PACKET_BUFFER_SIZE 			(1024*16)
#define CONVERT_BUF_SIZE			PACKET_BUFFER_SIZE
#define QWMSG_BUFFER_SIZE 			(PACKET_BUFFER_SIZE-5)
#define SAVE_P_BUFF_SIZE 			(PACKET_BUFFER_SIZE * 10)


////////////////  CMD  ////////////////////////////////////////////

#define TVSDK_CMD_NONE 0x00
#define TVSDK_CMD_STARTED           0xB6    
#define TVSDK_CMD_STOP              0xB7
#define TVSDK_CMD              		0xBB

///////////////////////////////////////////////////////////
/* TVSDK SUB CMD */
#define TVSDK_PUT_DETAILE_EIT       0x10
#define TVSDK_PUT_EPG_LIST          0x11
#define TVSDK_ADD_EPG_INFO         	0x12
#define TVSDK_GET_GMTOFFSET			0x13
#define TVSDK_SYNC_CRTVCORE			0x14
#define TVSDK_GET_SYSTEM_LANGUAGE	0x15

#define TVSDK_LOCKTUNER             0x20
#define TVSDK_CHECKLOCKTUNER        0x21

#define TVSDK_PID_FILTER_START      0x30
#define TVSDK_PID_FILTER_STOP      	0x31

#define TVSDK_GET_EITMEMORYSTATUS	0x40
#define TVSDK_SET_EITMEMORY         0x41

#define TVSDK_CMD_CHUP				0x50				
#define TVSDK_CMD_CHDOWN			0x51				
#define TVSDK_CMD_CHNUM             0x52

#define TVSDK_SC_SMALL_MSG          0x69
#define TVSDK_SC_BIG_MSG            0x6A


///////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

typedef struct {
	HX_UINT16 L;
	HX_UINT16 D;
	HX_UINT16 S;
	HX_UINT8 F;
	HX_UINT8 P;
	HX_INT32 stamp;
	HX_INT32 section;
} JLIKE_PACKED packet_header_st;

typedef struct {
	packet_header_st header;
	HX_INT8 buf[PACKET_BUFFER_SIZE];
} JLIKE_PACKED packet_st;

typedef struct {
	HX_UINT8 cmd;
	HX_UINT8 ver;
	HX_UINT16 size;
	HX_UINT8 cnt;  
	HX_INT8 data[QWMSG_BUFFER_SIZE]; /* 1019 = 1024 - 5*/
} JLIKE_PACKED qw_msg_st;

typedef struct {
	unsigned short L;
	unsigned short D;
	unsigned short S;
	unsigned char  F;
	unsigned char  P;
	unsigned long stamp;	// msec time-stamp
	int section;			// section id
	int sid;				// session id
	int cmd;				// command id
	union {
		struct {
			int onoff;
		} sleep;
		struct {
			char buf[1024 - (16 + 4 + 4)];
		} none;
	} u;
} app_msg_t;

typedef struct {
	unsigned short L;
	unsigned short D;
	unsigned short S;
	unsigned char  F;
	unsigned char  P;
	unsigned long stamp;    // msec time-stamp
	int section;            // section id
	int sid;                // session id
	int cmd;                // command id
	union {
		// OK/STRING
		struct {
			int len;
			char data[256];
		} string;
		struct {
			int onoff;
		} mode;
		struct {
			int value;
		} state;
	} u;
} ir_msg_t;

enum {
	IR_MSG_NONE = 0,        // 0
	IR_MSG_OK,              // 1
	IR_MSG_FAN,             // 2
	IR_MSG_STRING,          // 3
	IR_MSG_MUTE,            // 4
	IR_MSG_ICON_NETWORK,    // 5
	IR_MSG_ICON_ACTION,     // 6
	IR_MSG_SLEEP,           // 7
	IR_MSG_LAST
};


#ifdef __cplusplus
extern "C" {
#endif

	HX_STATUS send_msg_to_ca(HX_UINT8 cmd, HX_INT8 *buf, HX_UINT16 size);
	HX_UINT8 get_command(HX_INT8 *buf, HX_INT16 *len);
	HX_UINT8 peek_command(HX_VOID);
#if 1
	void __cmd_msg_set(unsigned char cmd);
	void* __cmd_msg_get_buffer(int size);
	HX_UINT16 __cmd_msg_buffer_size(void);
	void __cmd_msg_replace_bytes(void *src, void *data, int size);
	void __cmd_msg_add_byte(char val);
	void __cmd_msg_add_short(short val);
	void __cmd_msg_add_int(int val);
	void __cmd_msg_add_long(long val);
	void __cmd_msg_add_string(char *str, int size);
//	int string_to_wstring(char *str, size_t size, char *out_wstr, size_t out_size);
//	void __cmd_msg_add_wstring(char *str, int size);
	void __cmd_msg_add_bytes(void *data, int size);
	void __cmd_msg_send(void);
#endif
	HX_STATUS init_cont(HX_UINT16 id);
	HX_STATUS set_dest_id(HX_UINT16 id);
	HX_STATUS dest_cont(void);

#ifdef __cplusplus
};
#endif

#endif	// ndef __CAMESSAGE_H_
