 1Content-type: application/x-Unknown; charset=UTF8package nmc.sidekick;

// 200205161514
// 200206121652

import java.io.*;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import com.ajile.drivers.spi.*;
import javax.comm.SerialPort;
import javax.comm.CommPortIdentifier;
import com.ajile.jem.rawJEM;


public class UartLoopbackTest {

    public static int UartBkpts = 0;
    public static void UartBkptHere() {
        UartBkpts++;
    }

    protected IoResource channel[] = new IoResource[64];
    /**
     * Starts the application.
     * @param args an array of command-line arguments
     */
    public static void main(java.lang.String[] args) {

        DebugMessage.println("This is the UartLoopbackTest running.");
        UartLoopbackTest ult = new UartLoopbackTest();

        ult.initializeLoopbackPlatform();
        ult.doLoopback();

        ult.cutTxPower(0);
        ult.cutTxPower(1);


    }

    static int[] mcrAddrs = {
        (0x00800010 + 0*0x40),  // com0   class=XR16L758 name=XR16L758_0
        (0x00800010 + 1*0x40),  // com1   class=XR16L758 name=XR16L758_0
        (0x00800010 + 2*0x40),  // com2   class=XR16L758 name=XR16L758_0
        (0x00800010 + 3*0x40),  // com3   class=XR16L758 name=XR16L758_0
        (0x00800010 + 4*0x40),  // com4   class=XR16L758 name=XR16L758_0
        (0x00800010 + 5*0x40),  // com5   class=XR16L758 name=XR16L758_0
        (0x00800010 + 6*0x40),  // com6   class=XR16L758 name=XR16L758_0
        (0x00800010 + 7*0x40),  // com7   class=XR16L758 name=XR16L758_0
 
        0x00900010,             // com8   driver=Generic16551 name=com8
        0x00a00010,             // com9   driver=Generic16551 name=com9
 
        0x09000010,             // com10  driver=??aj100Duart? name=com10
        0x09000810,             // com11  driver=??aj100Duart? name=com11
    };

    static protected void doExternalLoopback(int ch) {
	doLoopback(ch, 0);
    }

    static protected void doInternalLoopback(int ch) {
	doLoopback(ch, 1);
    }

    // bitval = 0 for external loopback, 1 for internal loopback, other values bad
    static protected void doLoopback(int ch, int bitval) {
	if (bitval != 0)
	    bitval = 1;
        UartBkptHere();
        DebugMessage.println("Trying doInternalLoopback(" + ch + ")");
        if (ch < 0 || ch > mcrAddrs.length) {
            DebugMessage.println("no such port as com" + ch);
            return;
        }
        if (mcrAddrs[ch] == 0) {
            DebugMessage.println("no such port as com" + ch);
            return;
        }

        CommPortIdentifier cpi = null;
        SerialPort serialPort = null;
        OutputStream ostream = null;
        InputStream istream = null;
        String cpiString = "com" + ch;
        DebugMessage.println("cpiString = \"" + cpiString + "\"");
        try {
            cpi = CommPortIdentifier.getPortIdentifier(cpiString);
        } catch (javax.comm.NoSuchPortException e1) {
            DebugMessage.println("Exception on opening port: " + e1);
            return;
        }
        try {
            serialPort = (SerialPort)cpi.open("xy", 0);
            ostream = serialPort.getOutputStream();
            istream = serialPort.getInputStream();

        } catch (javax.comm.PortInUseException e2) {
            DebugMessage.println("PortInUseException: " + e2);
            return;
        } catch (java.io.IOException e3) {
            DebugMessage.println("Exception on opening port: " + e3);
            return;
        }

        int addr = mcrAddrs[ch];
        UartBkptHere();
        rawJEM.setField(bitval, addr, 4, 1);
        rawJEM.setField(bitval, addr, 4, 1);
        UartBkptHere();


        try {

            byte[] outgoing = "hubbahubba".getBytes();
            ostream.write(outgoing);
            int bytesWritten = outgoing.length;
            DebugMessage.println("Seem to have succeeded in writing byte(s)");

            int a = 0;
            int loopctr = 0;
            while (a < bytesWritten) {
                a = istream.available();
                if (loopctr % 500 == 0) {
                    DebugMessage.println("Tried " + loopctr + " times, "
                                       + a
                                       + " available for reading");
                }
                // if we're not getting anything, report failure
                if (loopctr++ > 10000)
                    break;
            }
            DebugMessage.println("Just got " + a + " bytes to read");
            if (loopctr > 10000) {
                DebugMessage.println("loopback test FAILED for port " + cpiString);
            } else {
                byte[] b = new byte[a];
                istream.read(b);
                String s = new String(b, 0, a);
                DebugMessage.println("Seem to have succeeded in reading " + s);
            }

        } catch (java.io.IOException e) {
            DebugMessage.println("doLoopback got IOException: " + e);
        }

        if (serialPort != null)
            serialPort.close();

        DebugMessage.println("DONE with loopback test");

    }

    protected void doLoopback() {

        DpaBoard.DpaChannel dc =
            (DpaBoard.DpaChannel)channel[0].getResourceType(IoResourceType.DPA_BOARD);
        SerialPort sp = dc.instrumentPort;
        if (sp != null) {
            DebugMessage.println("GOT sp");
        } else {
            DebugMessage.println("BADBADBAD");
        }

        try {

            InputStream istream = sp.getInputStream();
            OutputStream ostream = sp.getOutputStream();
            byte[] outgoing = "hubbahubba".getBytes();
            ostream.write(outgoing);
            int bytesWritten = outgoing.length;
            DebugMessage.println("Seem to have succeeded in writing byte(s)");

            int a = 0;
            int loopctr = 0;
            while (a < bytesWritten) {
                a = istream.available();
                if (loopctr % 500 == 0) {
                    DebugMessage.println("Tried " + loopctr + " times, "
                                       + a
                                       + " available for reading");
                }
                loopctr++;
            }
            DebugMessage.println("Just got " + a + " bytes to read");

            byte[] b = new byte[a];
            istream.read(b);
            String s = new String(b, 0, a);
            DebugMessage.println("Seem to have succeeded in reading " + s);

        } catch (java.io.IOException e) {
            DebugMessage.println("doLoopback got IOException: " + e);
        }


        DebugMessage.println("DONE with loopback test");
    }


    private void cutTxPower(int n) {
        if (channel[n] == null) {
            DebugMessage.println("channel[" + n + "] is null!");
            return;
        }
        DpaBoard.DpaChannel dc =
            (DpaBoard.DpaChannel)channel[n].getResourceType(IoResourceType.DPA_BOARD);
        dc.channelCtrlReg.setTxLoPower();
        dc.channelCtrlReg.write();
    }

    protected void initializeLoopbackPlatform() {

	/*
	 *  This is all the platform-specific configuration stuff.
	 *  Everything except the association between the instruments
	 *  and the port is likely to remain hardwired.  The association
	 *  of instruments to serial lines must be configurable.
	 */

	DebugMessage.println("Starting 'initializeLoopbackPlatform()'");
	DpaBoard board = new DpaBoard(SpiMaster.SPI_SLAVE_SELECT_0);
	// the next line should not work
	DpaBoard.DpaChannel aChannel = board.getLeftChannel();
	DebugMessage.println("Finished creating left dpa channel");
	aChannel.initializeChannel();
	DebugMessage.println("Finished initializing left dpa channel");
	SerialPort cereal = null;
        String serialPortName = "com10";
	try {
	    cereal = openSerialPort(serialPortName);
            aChannel.setSerialPort(cereal);
            DebugMessage.println("Finished creating the serial port on "
                               + serialPortName);
	} catch (IOException e) {
	    DebugMessage.println("could not open '"
                               + serialPortName
                               + "': " + e);
	}

	channel[0] = aChannel;
	DebugMessage.println("Finished setting up left channel");
	DebugMessage.println("Finished setting up left channel"
                           + " and wiring it to "
                           + serialPortName);


	aChannel = board.getRightChannel();
	DebugMessage.println("Finished creating right dpa channel");
	aChannel.initializeChannel();
	DebugMessage.println("Finished initializing right dpa channel");

	channel[1] = aChannel;
	DebugMessage.println("Finished setting up right channel");
    }

    protected SerialPort openSerialPort(String portName) throws IOException {
	CommPortIdentifier cpi;
	SerialPort serialPort = null;

            try
            {
                cpi = CommPortIdentifier.getPortIdentifier(portName);
                serialPort = (SerialPort)cpi.open("xyz", 0);

                DebugMessage.println( "Succeeded in opening port named: "
                                    + portName);
            }
            catch ( javax.comm.NoSuchPortException e )
            {
                DebugMessage.println( "Unable to locate " + portName );
            }
            catch ( javax.comm.PortInUseException e )
            {
                DebugMessage.println( "Port " + portName + " is already in use!" );
            }
            catch ( Exception e )
            {
                DebugMessage.println( "Some other exception happened to "
                                    + portName
                                    + ".  Exception: "
                                    + e );
            }
	return serialPort;
    }
}
