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

📄 irlapframer.java

📁 JRed is a 100% Java implementation of the IrDA infrared communications protocols. JRed lets you beam
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
**************************************************************************
** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/irlap/IrLAPFramer.java,v 1.2 2000/07/30 05:57:57 mpatters Exp $
**
** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
**
** This file is part of JRed, a 100% Java implementation of the IrDA
** infrared communications protocols.
**
** This file may be distributed under the terms of the Synchrona Public
** License as defined by Synchrona, Inc. and appearing in the file
** LICENSE included in the packaging of this file. The Synchrona Public
** License is based on the Q Public License as defined by Troll Tech AS
** of Norway; it differs only in its use of the courts of Florida, USA
** rather than those of Oslo, Norway.
**************************************************************************
*/
package com.synchrona.jred.irlap;

import com.synchrona.util.Log;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.comm.SerialPort;

public class IrLAPFramer implements Runnable {
	private static final byte COMMAND_DISCONNECT          = (byte) 0x43;
	private static final byte BROADCAST_ADDRESS           = (byte) 0xFE;
	private static final int  COMM_PARAMETER_ARRAY_LENGTH = 21;
	private static final byte FINAL_SLOT                  = (byte) 0xFF;
	private static final byte INFORMATION_FORMAT          = (byte) 0x00;
	private static final byte IRLAP_VERSION               = (byte) 0x00;
	private static final byte MASK_CR_BIT                 = (byte) 0x01;
	private static final byte MASK_NR_BITS                = (byte) 0xE0;
	private static final byte MASK_NS_BITS                = (byte) 0x0E;
	private static final byte MASK_PF_BIT                 = (byte) 0x10;
	private static final byte MASK_SUPERVISORY_CONTROLS   = (byte) 0x0F;
	private static final byte RR_CONTROL                  = (byte) 0x01;
	private static final byte SNRM_COMMAND                = (byte) 0x83;
	private static final byte SUPERVISORY_FORMAT          = (byte) 0x01;
	private static final byte UA_RESPONSE                 = (byte) 0x63;
	private static final byte UNNUMBERED_FORMAT           = (byte) 0x03;
	private static final byte XID_COMMAND                 = (byte) 0x2F;
	private static final byte XID_FORMAT                  = (byte) 0x01;
	private static final byte XID_RESPONSE                = (byte) 0xAF;

	private byte []              m_ayCommParameters;
	private byte []              m_ayOutputFrame;
	private boolean              m_bDoLogging;
	private iIrLAPFramerListener m_listener;
	private Log                  m_log;
	private SerialPort           m_port;
	private iFrameWrapper        m_wrapper;

	public IrLAPFramer(Log log, SerialPort port) throws Exception {
		m_ayCommParameters = new byte[COMM_PARAMETER_ARRAY_LENGTH];
		m_ayOutputFrame    = new byte[AsyncFrameWrapper.MAX_FRAME_LENGTH];
		m_bDoLogging       = false;
		m_listener         = null;
		m_log              = log;
		m_port             = port;

		// default IrLAP serial parameters
		m_port.setSerialPortParams(Integer.getInteger("portSpeed", 9600).intValue(),
		                           SerialPort.DATABITS_8,
		                           SerialPort.STOPBITS_1,
		                           SerialPort.PARITY_NONE);
		// readFrame() needs to block while data is unavailable
		m_port.disableReceiveThreshold();
		m_port.disableReceiveTimeout();

		m_port.setRTS(true);
		m_port.setDTR(false);

		m_wrapper = new AsyncFrameWrapper(log,
		                                  new DataInputStream(port.getInputStream()),
		                                  new DataOutputStream(port.getOutputStream()));

	}

	public void addIrLAPFramerListener(iIrLAPFramerListener listener) throws Exception {
		if ( null != m_listener ) {
			throw new Exception("This IrLAPFramer already has a listener.");
		} else {
			m_listener = listener;
		}
	}

	public void initialize() {
		m_port.setRTS(true);
		m_port.setDTR(false);
	}

	public void run() {
		boolean bContinue = true;
		byte [] ayFrame   = new byte[AsyncFrameWrapper.MAX_FRAME_LENGTH];

		while ( bContinue ) {
			try {
				int nBytesRead = readFrame(ayFrame);
				if ( nBytesRead > 0 ) {
					dispatchFrame(ayFrame, nBytesRead);
				}
			} catch ( Exception e ) {
				System.err.println(e);
				e.printStackTrace(System.err);
			}
		}
	}

	public void sendI(byte yConnection, int nNr, int nNs, boolean bFinal, byte [] ayData, int nOffset, int nLength) throws Exception {
		m_log.debug("IrLAPFramer", "sendI Nr=" + nNr + " Ns=" + nNs);
		int nPosition = 0;

		m_ayOutputFrame[nPosition++] = (byte) (yConnection << 1);
		m_ayOutputFrame[nPosition++] = (byte) ((nNr << 5 ) | (bFinal ? 0x10 : 0x00) | (nNs << 1));
		for ( int i = 0; i < nLength; i++ ) {
			m_ayOutputFrame[nPosition++] = ayData[nOffset + i];
		}
		
		m_wrapper.send(m_ayOutputFrame, 0, nPosition);
	}

	public void sendRR(byte yConnection, int nNr, boolean bCommand, boolean bFinal) throws Exception {
		m_log.debug("IrLAPFramer", "sendRR");
		int nPosition = 0;

		m_ayOutputFrame[nPosition++] = (byte) ((yConnection << 1) | ( bCommand ? 0x01 : 0x00 ));
		m_ayOutputFrame[nPosition++] = (byte) ((nNr << 5 ) | (bFinal ? 0x10 : 0x00) | RR_CONTROL);

		m_wrapper.send(m_ayOutputFrame, 0, nPosition);
	}

	public void sendSNRM(int nSource, int nDestination, byte yConnection) throws Exception {
		m_log.debug("IrLAPFramer", "sendSNRM");
		int nPosition = 0;

		m_ayOutputFrame[nPosition++] = (byte) (BROADCAST_ADDRESS | MASK_CR_BIT);
		m_ayOutputFrame[nPosition++] = (byte) (SNRM_COMMAND | MASK_PF_BIT);

		nPosition = intToBytes(m_ayOutputFrame, nPosition, nSource);
		nPosition = intToBytes(m_ayOutputFrame, nPosition, nDestination);

		m_ayOutputFrame[nPosition++] = (byte) ((yConnection << 1) & 0xFE);

		m_ayOutputFrame[nPosition++] = (byte) 0x01; // (1) baud rate
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x02; // 9600 bps

		m_ayOutputFrame[nPosition++] = (byte) 0x82; // (2) max turnaround time
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length

		m_ayOutputFrame[nPosition++] = (byte) 0x83; // (3) data size
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // 500ms

		m_ayOutputFrame[nPosition++] = (byte) 0x84; // (4) window size
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // stop-and-wait; 1 frame/window

		m_ayOutputFrame[nPosition++] = (byte) 0x85; // (5) additional BOF's
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // 48 BOF's @ 115200

		m_ayOutputFrame[nPosition++] = (byte) 0x86; // (6) min turnaround time
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // 3 seconds

		m_ayOutputFrame[nPosition++] = (byte) 0x08; // (7) link disconnect time
		m_ayOutputFrame[nPosition++] = (byte) 0x01; // data is 1 byte in length
		m_ayOutputFrame[nPosition++] = (byte) 0x80; // 40 seconds

		m_wrapper.send(m_ayOutputFrame, 0, nPosition);
	}

	public void sendUA(int nSource, int nDestination, byte yConnection, boolean bSendParameters) throws Exception {
		m_log.debug("IrLAPFramer", "sendUA");

		int nPosition = 0;

		m_ayOutputFrame[nPosition++] = (byte) ((yConnection << 1) & 0xFE);
		m_ayOutputFrame[nPosition++] = (byte) (UA_RESPONSE | MASK_PF_BIT);

		nPosition = intToBytes(m_ayOutputFrame, nPosition, nSource);
		nPosition = intToBytes(m_ayOutputFrame, nPosition, nDestination);

		if ( bSendParameters ) {
			for ( int i =0; i < m_ayCommParameters.length; i++ ) {
				m_ayOutputFrame[nPosition++] = m_ayCommParameters[i];
			}
		}

		m_wrapper.send(m_ayOutputFrame, 0, nPosition);
	}

	public void sendXID(int nSource, int nDestination, boolean bCommand, byte ySlot, byte [] ayDiscoveryInfo) throws Exception {
		m_log.debug("IrLAPFramer", "sendXID");
		int nPosition = 0;
		
		if ( bCommand ) {
			m_ayOutputFrame[nPosition++] = (byte) (BROADCAST_ADDRESS | MASK_CR_BIT);
			m_ayOutputFrame[nPosition++] = (byte) (XID_COMMAND | MASK_PF_BIT);
		} else {
			m_ayOutputFrame[nPosition++] = BROADCAST_ADDRESS;
			m_ayOutputFrame[nPosition++] = (byte) (XID_RESPONSE | MASK_PF_BIT);
		}
		m_ayOutputFrame[nPosition++] = XID_FORMAT;
	
		m_ayOutputFrame[nPosition++] = (byte) ((nSource & 0xFF000000) >>> 24);
		m_ayOutputFrame[nPosition++] = (byte) ((nSource & 0x00FF0000) >>> 16);
		m_ayOutputFrame[nPosition++] = (byte) ((nSource & 0x0000FF00) >>> 8);
		m_ayOutputFrame[nPosition++] = (byte) ((nSource & 0x000000FF));
	
		m_ayOutputFrame[nPosition++] = (byte) ((nDestination & 0xFF000000) >>> 24);
		m_ayOutputFrame[nPosition++] = (byte) ((nDestination & 0x00FF0000) >>> 16);
		m_ayOutputFrame[nPosition++] = (byte) ((nDestination & 0x0000FF00) >>> 8);
		m_ayOutputFrame[nPosition++] = (byte) ((nDestination & 0x000000FF));
	
		// Flag field tells how many slots per discovery; 0x01 means 6 and
		// is what PalmOS devices do.
		m_ayOutputFrame[nPosition++] = (byte) 0x01;
		m_ayOutputFrame[nPosition++] = ySlot;

⌨️ 快捷键说明

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