📄 irlapframer.java
字号:
m_ayOutputFrame[nPosition++] = IRLAP_VERSION;
m_log.debug("IrLAPFramer", "FINAL_SLOT " + FINAL_SLOT + " ySlot " + ySlot + " disco length " + ayDiscoveryInfo.length);
// Plug in discovery hints
if ( FINAL_SLOT == ySlot ) {
for ( int i = 0; i < ayDiscoveryInfo.length; i++ ) {
m_ayOutputFrame[nPosition++] = ayDiscoveryInfo[i];
}
}
m_wrapper.send(m_ayOutputFrame, 0, nPosition);
}
public void setConnectionParameters(byte [] ayParameters) throws Exception {
m_log.debug("IrLAPFramer", "[setConnectionParameters] do nothing for now");
int i = 0;
m_ayCommParameters[i++] = (byte) 0x01; // (1) baud rate
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x02; // 9600 bps
m_ayCommParameters[i++] = (byte) 0x82; // (2) max turnaround time
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x83; // (3) data size
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x01; // 500ms
m_ayCommParameters[i++] = (byte) 0x84; // (4) window size
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x01; // stop-and-wait; 1 frame/window
m_ayCommParameters[i++] = (byte) 0x85; // (5) additional BOF's
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x01; // 48 BOF's @ 115200
m_ayCommParameters[i++] = (byte) 0x86; // (6) min turnaround time
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x01; // 3 seconds
m_ayCommParameters[i++] = (byte) 0x08; // (7) link disconnect time
m_ayCommParameters[i++] = (byte) 0x01; // data is 1 byte in length
m_ayCommParameters[i++] = (byte) 0x80; // 40 seconds
}
//---------------------------------------------------------------
// End public methods
//---------------------------------------------------------------
private int bytesToInt(byte [] ayBytes, int nOffset) {
return ((ayBytes[nOffset ] << 24) & 0xFF000000)
| ((ayBytes[nOffset + 1] << 16) & 0x00FF0000)
| ((ayBytes[nOffset + 2] << 8) & 0x0000FF00)
| ( ayBytes[nOffset + 3] & 0x000000FF);
}
private void dispatchFrame(byte [] ayFrame, int nLength) throws Exception {
// 0 bit of address field indicates whether this frame is a
// command (bit 0 = 1) or a response (bit 0 = 0)
byte yConnection = (byte) (ayFrame[0] >> 1);
boolean bCommand = ((ayFrame[0] & MASK_CR_BIT) != 0) ? true : false;
boolean bPoll = ((ayFrame[1] & MASK_PF_BIT) != 0) ? true : false;
byte yControl = ayFrame[1];
if ( UNNUMBERED_FORMAT == (yControl & UNNUMBERED_FORMAT) ) {
switch ( yControl & ~MASK_PF_BIT ) {
case SNRM_COMMAND:
dispatchSNRM(ayFrame, bCommand, nLength);
break;
case XID_COMMAND: // fall through
case XID_RESPONSE:
dispatchXID(ayFrame, bCommand, nLength);
break;
case COMMAND_DISCONNECT:
dispatchDISC(yConnection, bCommand, bPoll, ayFrame, nLength);
break;
default:
m_log.debug("IrLAPFramer", "Unimplemented control: " + (yControl & ~MASK_PF_BIT));
break;
}
} else if ( SUPERVISORY_FORMAT == (yControl & SUPERVISORY_FORMAT) ) {
int nNr = (yControl & MASK_NR_BITS) >> 5;
switch ( yControl & MASK_SUPERVISORY_CONTROLS ) {
case RR_CONTROL:
dispatchRR(yConnection, nNr, bCommand, bPoll);
break;
default:
m_log.debug("IrLAPFramer", "Unimplemented supervisory control: " + (yControl & MASK_SUPERVISORY_CONTROLS));
break;
}
} else if ( INFORMATION_FORMAT == (yControl & INFORMATION_FORMAT) ) {
int nNr = (yControl & MASK_NR_BITS) >> 5;
int nNs = (yControl & MASK_NS_BITS) >> 1;
dispatchInformation(yConnection, bCommand, bPoll, nNs, nNr, ayFrame, nLength);
}
}
private void dispatchDISC(byte yConnection, boolean bCommand, boolean bPoll, byte [] ayFrame, int nLength) throws Exception {
m_log.debug("IrLAPFramer", "dispatchDISC");
if ( m_listener != null ) {
m_listener.handleDisconnect(yConnection, bCommand, bPoll);
}
}
private void dispatchInformation(byte yConnection, boolean bCommand, boolean bPoll, int nNs, int nNr, byte [] ayFrame, int nLength) throws Exception {
m_log.debug("IrLAPFramer", "dispatchInformation");
if ( m_listener != null ) {
byte [] ayData = new byte[nLength - 2];
for ( int nSource = 2, nDestination = 0; nSource < nLength; nSource++, nDestination++ ) {
ayData[nDestination] = ayFrame[nSource];
}
m_listener.handleData(yConnection, bCommand, nNs, nNr, bPoll, ayData);
}
}
private void dispatchRR(byte yConnection, int nNr, boolean bCommand, boolean bPoll) throws Exception {
m_log.debug("IrLAPFramer", "dispatchRR");
if ( null != m_listener ) {
m_listener.handleRR(yConnection, nNr, bCommand, bPoll);
}
}
private void dispatchSNRM(byte [] ayFrame, boolean bCommand, int nLength) throws Exception {
m_log.debug("IrLAPFramer", "dispatchSNRM");
if ( null != m_listener ) {
int nSource = bytesToInt(ayFrame, 2);
int nDestination = bytesToInt(ayFrame, 6);
byte yConnection = (byte) (ayFrame[10] >>> 1);
byte [] ayParameters = new byte[nLength - 11];
for ( int i = 0, j = 11; j < nLength; i++, j++ ) {
ayParameters[i] = ayFrame[j];
}
m_listener.handleSNRM(nSource, nDestination, yConnection, ayParameters);
}
}
private void dispatchXID(byte [] ayFrame, boolean bCommand, int nLength) throws Exception {
if ( (nLength < 14) || (ayFrame.length < 14) ) {
throw new Exception("XID frames must be at least 14 bytes long. (length=" + nLength + ",array length=" + ayFrame.length);
}
int i = 3; // skip past address, XID command, and format
/*
if ( ayFrame[i++] != XID_FORMAT ) {
//throw new Exception("XID format identifier is supposed to be " + XID_FORMAT + ".");
m_log.debug("dispatchXID", "XID format identifier is supposed to be " + XID_FORMAT + ".");
}
*/
int nSource = bytesToInt(ayFrame, i);
i += 4;
m_log.debug("dispatchXID", "source: " + nSource);
int nDestination = bytesToInt(ayFrame, i);
i += 4;
m_log.debug("dispatchXID", "destination: " + nDestination);
// TBD: Decode flags. These are okay for PalmOS.
byte yFlags = ayFrame[i++];
byte yNumSlots = 6;
byte ySlot = ayFrame[i++];
byte yVersion = ayFrame[i++];
byte [] ayHints = null;
m_log.debug("dispatchXID", "ySlot: " + ySlot);
if ( !bCommand || FINAL_SLOT == ySlot ) {
m_log.debug("IrLAPFramer", "nLength " + nLength + " i " + i);
int nHintLength = nLength - i;
if ( nHintLength > 0 ) {
ayHints = new byte[nLength - i];
for ( int j = 0; j < ayHints.length; j++, i++ ) {
ayHints[j] = ayFrame[i];
}
}
}
if ( null != m_listener ) {
m_listener.handleXID(nSource, nDestination, yNumSlots, ySlot, bCommand, ayHints);
}
}
/**
* Write an integer in network order to the given array of bytes at
* the given offset.
* @return Returns the offset plus the size of an integer (4 bytes)
*/
private int intToBytes(byte [] ayBytes, int nOffset, int nValue) throws Exception {
if ( nOffset < 0 ) {
throw new Exception("Negative offset.");
}
if ( (ayBytes.length - nOffset) < 4 ) {
throw new Exception("Writing values at this offset would overflow the array bounds.");
}
ayBytes[nOffset++] = (byte) ((nValue & 0xFF000000) >>> 24);
ayBytes[nOffset++] = (byte) ((nValue & 0x00FF0000) >>> 16);
ayBytes[nOffset++] = (byte) ((nValue & 0x0000FF00) >>> 8);
ayBytes[nOffset++] = (byte) ((nValue & 0x000000FF));
return nOffset;
}
private int readFrame(byte [] ayFrame) throws Exception {
int nBytesRead = m_wrapper.receive(ayFrame, 0, ayFrame.length);
return nBytesRead;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -