/*
 *Author: liu_bridge
 *Function: 娑撴彃褰涢幒銉ュ經閺佺増宓侀幒銉︽暪閸滃本甯堕敓?
 *Time 2010.8.25
 * */
package com.tcl.factory.serial;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.util.Log;

//import com.mediatek.tv.TVManager;
//import com.mediatek.tv.common.TVMException;
//import com.mediatek.tv.model.UARTSerialListener;
//import com.mediatek.tv.model.UARTSerialParams;
//import com.mediatek.tv.service.ConfigService;
 
import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;
import com.tcl.factory.utils.Utils;

public class SerialUtils extends SerialPortApplication{
	private final String TAG = this.getClass().getName();
	private static SerialUtils sInstance = null;
	
    public static SerialUtils getInstance() {
        if (sInstance == null) {
            synchronized(SerialUtils.class){
                if (sInstance == null) {
                    sInstance = new SerialUtils();
                }
            }
        }
        return sInstance;
    }
	
	public void onUARTSerialNfy(int ioNotifyCond, int len, byte[] data)
	{
			Debug.v(TAG, "[onUARTSerialNfy]len = "+len);
			DEFS.receiveData=new short[len];
			for (int i = 0; i <len ; ++i)
			{
				DEFS.receiveData[i] = (short)(data[i]&0xff);
			}
			if(mUiSLsner!=null)
				mUiSLsner.onChange(); //lisenner  
//			DEFS.dataComing=true;
	}

	private int cmd_max_size = 4096;
	private byte[] cmd_data = new byte[cmd_max_size];
	private int cmd_len = 0;
	private int cmd_size = 0;//瀹歌尙绮￠幏鐤閻ㄥ嫭鏆熼幑顔笺亣锟?
	private int cmd_head = 0; // AA->AF
	private int cmd_package_status = 0;//閺勵垰鎯侀棁鈧憰浣虹矋锟?

	private int hdcp_cmd_max_size = 4096;
	private byte[] hdcp_cmd_data = new byte[hdcp_cmd_max_size];
	private int hdcp_cmd_len = 0;
	private int hdcp_cmd_size = 0;//瀹歌尙绮￠幏鐤閻ㄥ嫭鏆熼幑顔笺亣锟?
	private int hdcp_cmd_head = 0; // AA->AF
	private int hdcp_cmd_head2 = 0;//FE
	private int hdcp_cmd_package_status = 0;//閺勵垰鎯侀棁鈧憰浣虹矋锟?

	@Override
	protected synchronized void onDataReceived(byte[] cmd, int size) {

		//HDCPKEY
		if(hdcp_cmd_package_status == 0) {
			hdcp_cmd_head = (int)(cmd[0]&0xff);
			hdcp_cmd_head2 = (int)(cmd[1]&0xff);
			if((SerialManager.FACTORY_DEBUG_HEADING_CODE == hdcp_cmd_head) //瀵版鍩屾径瀵哥垳
				&&(SerialManager.FACTORY_HDCP_HEAD_CODE == hdcp_cmd_head2)) { //HDCPKEY
				for(int i=0;i<size;i++){
					hdcp_cmd_data[i] = cmd[i];
				}
				
				hdcp_cmd_len = (int)(((cmd[2]<<8) & 0xff00)|(cmd[3]&0xff)); ////311
				//cmd_len = DEFS.HDCP_LENGTH;
				hdcp_cmd_size = size;
				hdcp_cmd_package_status = 1;
				hdcp_cmd_head = (int)(cmd[0]&0xff);
				
				if(hdcp_cmd_len == hdcp_cmd_size) { //娑撯偓娑擃亜鐣弫瀵告畱閸栧懐娲块幒銉ュ絺锟?
					Debug.v("Factory", "[HDCP]package,cmd[0/1/2/3]="+cmd[0]+"/"+cmd[1]+"/"+cmd[2]+"/"+cmd[3]+";hdcp_cmd_len="+hdcp_cmd_len);
					onUARTSerialNfy(5,cmd_len,cmd);
					hdcp_cmd_package_status = 0;
					hdcp_cmd_len = 0;
					hdcp_cmd_size = 0;
					hdcp_cmd_head = 0;
				}
				return ;
			}
		}
		else {
			if((SerialManager.FACTORY_DEBUG_HEADING_CODE == hdcp_cmd_head) //瀵版鍩屾径瀵哥垳
				&&(SerialManager.FACTORY_HDCP_HEAD_CODE == hdcp_cmd_head2)) { //HDCPKEY
				
				if(hdcp_cmd_len>hdcp_cmd_size) {//缂佸嫬锟?
					for(int i=0;i<size;i++){
						hdcp_cmd_data[hdcp_cmd_size+i] = cmd[i];
					}
					hdcp_cmd_size+=size;
					Debug.v("Factory", "[HDCP]repackage,hdcp_cmd_size="+hdcp_cmd_size);
				}
				else {//缂佸嫬瀵橀崙娲晩閿涘苯顦叉担?
					Debug.v("Factory", "[HDCP]repackage Error,cmd_size/cmd_len="+hdcp_cmd_size+"/"+hdcp_cmd_len);
					hdcp_cmd_package_status = 0;
					hdcp_cmd_len = 0;
					hdcp_cmd_size = 0;
					hdcp_cmd_head = 0;
					return;
				}
				
				if(hdcp_cmd_len == hdcp_cmd_size) {//濮濓絿锟?
					hdcp_cmd_package_status = 0;
					Debug.v("Factory", "[HDCP]repackage ok,hdcp_cmd_size="+hdcp_cmd_size);
					onUARTSerialNfy(5,hdcp_cmd_len,hdcp_cmd_data);
				}
				else if(hdcp_cmd_len < hdcp_cmd_size){//閺佺増宓侀張澶婎樋閿涘苯褰查懗钘夊毉锟?
					hdcp_cmd_package_status = 0;
					Debug.v("Factory", "[HDCP]repackage warn,hdcp_cmd_size="+hdcp_cmd_size);
					hdcp_cmd_len = 0;
					hdcp_cmd_size = 0;
					hdcp_cmd_head = 0;
					//onUARTSerialNfy(5,hdcp_cmd_len,hdcp_cmd_data);
				}
				else {//缂佈呯敾缂佸嫬锟?
					hdcp_cmd_package_status = 1;
				}
				
				return;
			}
		}
		
		
		if(cmd_package_status == 0) {//閺傛澘瀵樺鈧慨?
			if(size>=5){
				cmd_package_status = 0;
				cmd_len = 0;
				cmd_size = 0;
				cmd_head = 0;
				
				cmd_head = (int)(cmd[0]&0xff);

				if(SerialManager.FACTORY_DEBUG_HEADING_CODE == cmd_head) { //瀵版鍩屾径瀵哥垳
					cmd_len = (int)(cmd[1]&0xff);

					if(cmd_len == size) { //娑撯偓娑擃亜鐣弫瀵告畱閸栧懐娲块幒銉ュ絺锟?
						Debug.v("Factory", "package,cmd[0/1]="+cmd[0]+"/"+cmd[1]+";cmd_len="+cmd_len);
						onUARTSerialNfy(5,cmd_len,cmd);
						cmd_package_status = 0;
						cmd_len = 0;
						cmd_size = 0;
						cmd_head = 0;
					}
					else if(cmd_len > size){ //闂団偓鐟曚胶绮嶉崠?
						for(int i=0;i<size;i++){
							cmd_data[i] = cmd[i];
						}
						cmd_size = size;
						cmd_package_status = 1;
						cmd_len = (int)(cmd[1]&0xff);
						cmd_head = (int)(cmd[0]&0xff);

						Debug.v("Factory", "repackage,cmd[0/1]="+cmd[0]+"/"+cmd[1]+";cmd_len="+cmd_len);
					}
					else{//婢舵矮閲滈崠鍛存付鐟曚礁顦╅悶?						onUARTSerialNfy(5,cmd_len,cmd);
						cmd_package_status = 0;
						cmd_len = 0;
						cmd_size = 0;
						cmd_head = 0;
					}
				}
				else {//閸栧懎锟?娑撳秵顒滅涵?婢跺秳锟?
					Debug.v("Factory", "[Serial]Head discorrect = "+cmd_head);
					cmd_package_status = 0;
					cmd_len = 0;
					cmd_size = 0;
					cmd_head = 0;
				}
			}
			else {//鐏忓繋锟?鐎涙濡惃鍕瘶,婢跺秳锟?
				Debug.v("Factory", "[Serial]size discorrect = "+size);
				cmd_package_status = 0;
				cmd_len = 0;
				cmd_size = 0;
				cmd_head = 0;
			}
		}
		else {
			
			if(cmd_len>cmd_size) {//缂佸嫬锟?
				for(int i=0;i<size;i++){
					cmd_data[cmd_size+i] = cmd[i];
				}
				cmd_size+=size;
				Debug.v("Factory", "repackage,cmd_size="+cmd_size);
			}
			else {//缂佸嫬瀵橀崙娲晩閿涘苯顦叉担?
				Debug.v("Factory", "repackage Error,cmd_size/cmd_len="+cmd_size+"/"+cmd_len);
				cmd_package_status = 0;
				cmd_len = 0;
				cmd_size = 0;
				cmd_head = 0;
				return;
			}
			
			if(cmd_len == cmd_size) {//濮濓絿锟?
				cmd_package_status = 0;
				Debug.v("Factory", "repackage ok,cmd_size="+cmd_size);
				onUARTSerialNfy(5,cmd_len,cmd_data);
			}
			else if(cmd_len < cmd_size){//閺佺増宓侀張澶婎樋閿涘苯顦挎担娆戞畱閺佺増宓侀弰顖氭儊闂団偓鐟曚礁顦╅悶?
				cmd_package_status = 0;
				Debug.v("Factory", "repackage warn,cmd_size="+cmd_size);
				onUARTSerialNfy(5,cmd_len,cmd_data);
			}
			else {//缂佈呯敾缂佸嫬锟?
				cmd_package_status = 1;
			}
		}
	}

	@Override
	public void OpenUartSerial() {
		DEFS.uartOpenStatus=DEFS.Enable;
		System.out.println("[OpenUartSerial]");
		
		ThirdParty.mFactoryAdapter.openSerialPort();
		super.OpenUartSerial();
	}

	@Override
	public void CloseUARTSerial() {
		DEFS.uartOpenStatus=DEFS.Disable; 
		System.out.println("[CloseUARTSerial");
		
		ThirdParty.mFactoryAdapter.closeSerialPort();
		super.CloseUARTSerial();
	}
	
	public void outPutUartData(short[] uartSerialData){
		byte[] outData=new byte[uartSerialData.length];
		for(int i=0;i<uartSerialData.length;i++) {
			outData[i]=(byte)(uartSerialData[i] & 0xff);
			//Debug.v(TAG, "" + outData[i]);
		}
		mSerialPort.write(outData,outData.length);
//		try {
//			mOutputStream.write(outData);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	public  interface UiSerialListener{
		void  onChange();
	}
	private UiSerialListener  mUiSLsner = null;

	public void  addListener(UiSerialListener mLsner){
		mUiSLsner=mLsner;
	}
}

