Formatter

From RifidiWiki

Jump to: navigation, search


The formatter has two important methods:

  1. decode(): The decode method figures out what kind of command the incoming message is, and turns the raw command into an arrayList of objects, where the first thing in the object is the identification of the command type that will be used to look up the commandHandler in the reader.xml. The second argument is the command itself.
  2. encode() The decode method performs any operations on the responses that need to happen on the way out.

In the Symbol Formatter, the third byte is the command type, and is used to look up the handler method to use:

public ArrayList<Object> decode(byte[] arg) {
    if (arg == null || arg.length < 4) {
        return null;
    }
    ArrayList<Object> retVal = new ArrayList<Object>();
    retVal.add(ByteAndHexConvertingUtility.toHexString(arg[3]));
    retVal.add(arg);
    
    return retVal;
	}

On the way out, a 0x01 needs to be prepended to a response, and the CRC needs to be calculated.

public ArrayList<Object> encode(ArrayList<Object> arg) {

	ArrayList<Object> retVal = new ArrayList<Object>();

	for (Object o : arg) {
		byte[] command = (byte[]) o;

		/*
		 * Create outgoing array list that is 3 slots bigger than incoming
		 * one: one for start of frame and two for crc bytes
		 */
		byte[] outgoingCommand = new byte[command.length + 3];
		for (int i = 0; i < command.length; i++) {
			outgoingCommand[i + 1] = command[i];
		}

		/* Add Start of Frame Byte */
		outgoingCommand[0] = 0x01;

		// Calculate CRC
		int crc = CRC16.calculateCRC(command, 0xBEEF,CRC16.XR400_CRC_TABLE, true);
		byte[] crcBytes = ByteAndHexConvertingUtility.intToByteArray(crc, 2);

		// put crc in outgoingCommand, LSB first
		outgoingCommand[outgoingCommand.length - 2] = crcBytes[1];
		outgoingCommand[outgoingCommand.length - 1] = crcBytes[0];
			
		logger.debug(ByteAndHexConvertingUtility.toHexString(outgoingCommand));
			
			
		retVal.add(outgoingCommand);

	}

	return retVal;
}
Personal tools