/**
 * Author : Rishi Gupta
 * 
 * This file is part of 'serial communication manager' library.
 *
 * The 'serial communication manager' is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The 'serial communication manager' is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with serial communication manager. If not, see <http://www.gnu.org/licenses/>.
 */

package test56;

import com.embeddedunveiled.serial.SerialComException;
import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;
import com.embeddedunveiled.serial.ISerialComDataListener;
import com.embeddedunveiled.serial.SerialComDataEvent;

class Data extends Test56 implements ISerialComDataListener{
	@Override
	public void onNewSerialDataAvailable(SerialComDataEvent data) {
		System.out.println("Read from serial port : " + new String(data.getDataBytes()));
		System.out.println("data length : " + data.getDataBytesLength() );
	}
	@Override
	public void onDataListenerError(int arg0) {
		System.out.println("onDataListenerError called " + arg0);
		try {
			scm.unregisterDataListener(dataListener);
			scm.closeComPort(handle);
		} catch (SerialComException e) {
			e.printStackTrace();
		}
	}
}

public class Test56 {
	protected static long handle = 0;
	protected static Data dataListener = null;
	protected static SerialComManager scm = null;
	
	public static void main(String[] args) {
		try {
			scm = new SerialComManager();
			
			String PORT = null;
			String PORT1 = null;
			int osType = SerialComManager.getOSType();
			if(osType == SerialComManager.OS_LINUX) {
				PORT = "/dev/ttyUSB0";
				PORT1 = "/dev/ttyUSB1";
			}else if(osType == SerialComManager.OS_WINDOWS) {
				PORT = "COM51";
				PORT1 = "COM52";
			}else if(osType == SerialComManager.OS_MAC_OS_X) {
				PORT = "/dev/cu.usbserial-A70362A3";
				PORT1 = "/dev/cu.usbserial-A602RDCH";
			}else if(osType == SerialComManager.OS_SOLARIS) {
				PORT = null;
				PORT1 = null;
			}else{
			}

			dataListener = new Data();
			handle = scm.openComPort(PORT, true, true, true);
			scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
			scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
			
			scm.registerDataListener(handle, dataListener);
			
			// remove usb-uart physically from system and see onDataListenerError(int arg0) will be called
			// where recovery policy will come into action.
			while(true);

		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -871,34 +871,207 @@
 .serial.
-SerialComException
+ISerialComEventListener;%0Aimport com.embeddedunveiled.serial.ISerialComPortMonitor;%0Aimport com.embeddedunveiled.serial.SerialComException;%0Aimport com.embeddedunveiled.serial.SerialComLineEvent
 ;%0Aimport
@@ -2005,16 +2005,113 @@
 tener);%0A
+%09%09%09scm.unregisterLineEventListener(eventListener);%0A%09%09%09scm.unregisterPortMonitorListener(handle);%0A
 %09%09%09scm.c
@@ -2200,16 +2200,417 @@
 %7D%0A%09%7D%0A%7D%0A%0A
+class EventListener implements ISerialComEventListener%7B%0A%09@Override%0A%09public void onNewSerialEvent(SerialComLineEvent lineEvent) %7B%0A%09%09System.out.println(%22eventCTS : %22 + lineEvent.getCTS());%0A%09%09System.out.println(%22eventDSR : %22 + lineEvent.getDSR());%0A%09%7D%0A%7D%0A%0Aclass portWatcher implements ISerialComPortMonitor%7B%0A%09@Override%0A%09public void onPortMonitorEvent(int event) %7B%0A%09%09System.out.println(%22==%22 + event);%0A%09%7D%0A%7D%0A%0A
 public c
@@ -2702,16 +2702,70 @@
 = null;%0A
+%09protected static EventListener eventListener = null;%0A
 %09protect
@@ -3424,38 +3424,8 @@
 %09%7D%0A%0A
-%09%09%09dataListener = new Data();%0A
 %09%09%09h
@@ -3665,57 +3665,303 @@
 %0A%09%09%09
-%0A%09%09%09scm.registerDataListener(handle, dataListener
+dataListener = new Data();%0A%09%09%09eventListener = new EventListener();%0A%09%09%09portWatcher pw = new portWatcher();%0A%09%09%09scm.registerDataListener(handle, dataListener);%0A%09%09%09scm.registerLineEventListener(handle, eventListener);%0A%09%09%09scm.registerPortMonitorListener(handle, pw);%0A%09%09%09%0A%09%09%09System.out.println(%22ready%22
 );%0A%09

update test for testing recovery policy