📄 upacketbuilder.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.adapter;// importsimport java.util.Vector;import java.util.Enumeration;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.adapter.UAdapterState;import com.dalsemi.onewire.adapter.RawSendPacket;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.utils.Address;import com.dalsemi.onewire.OneWireAccessProvider;/** UPacketBuilder contains the methods to build a communication packet * to the DS2480 based serial adapter. * * @version 0.00, 28 Aug 2000 * @author DS */class UPacketBuilder{ //-------- //-------- Finals //-------- //-------- Misc /** Byte operation */ public static final int OPERATION_BYTE = 0; /** Byte operation */ public static final int OPERATION_SEARCH = 1; /** Max bytes to stream at once */ public static final char MAX_BYTES_STREAMED = 64; //-------- DS9097U function commands /** DS9097U funciton command, single bit */ public static final char FUNCTION_BIT = 0x81; /** DS9097U funciton command, turn search mode on */ public static final char FUNCTION_SEARCHON = 0xB1; /** DS9097U funciton command, turn search mode off */ public static final char FUNCTION_SEARCHOFF = 0xA1; /** DS9097U funciton command, OneWire reset */ public static final char FUNCTION_RESET = 0xC1; /** DS9097U funciton command, 5V pulse imediate */ public static final char FUNCTION_5VPULSE_NOW = 0xED; /** DS9097U funciton command, 12V pulse imediate */ public static final char FUNCTION_12VPULSE_NOW = 0xFD; /** DS9097U funciton command, 5V pulse after next byte */ public static final char FUNCTION_5VPULSE_ARM = 0xEF; /** DS9097U funciton command to stop an ongoing pulse */ public static final char FUNCTION_STOP_PULSE = 0xF1; //-------- DS9097U bit polarity settings for doing bit operations /** DS9097U bit polarity one for function FUNCTION_BIT */ public static final char BIT_ONE = 0x10; /** DS9097U bit polarity zero for function FUNCTION_BIT */ public static final char BIT_ZERO = 0x00; //-------- DS9097U 5V priming values /** DS9097U 5V prime on for function FUNCTION_BIT */ public static final char PRIME5V_TRUE = 0x02; /** DS9097U 5V prime off for function FUNCTION_BIT */ public static final char PRIME5V_FALSE = 0x00; //-------- DS9097U command masks /** DS9097U mask to read or write a configuration parameter */ public static final char CONFIG_MASK = 0x01; /** DS9097U mask to read the OneWire reset response byte */ public static final char RESPONSE_RESET_MASK = 0x03; //-------- DS9097U reset results /** DS9097U OneWire reset result = shorted */ public static final char RESPONSE_RESET_SHORT = 0x00; /** DS9097U OneWire reset result = presence */ public static final char RESPONSE_RESET_PRESENCE = 0x01; /** DS9097U OneWire reset result = alarm */ public static final char RESPONSE_RESET_ALARM = 0x02; /** DS9097U OneWire reset result = no presence */ public static final char RESPONSE_RESET_NOPRESENCE = 0x03; //-------- DS9097U bit interpretation /** DS9097U mask to read bit operation result */ public static final char RESPONSE_BIT_MASK = 0x03; /** DS9097U read bit operation 1 */ public static final char RESPONSE_BIT_ONE = 0x03; /** DS9097U read bit operation 0 */ public static final char RESPONSE_BIT_ZERO = 0x00; /** Enable/disable debug messages */ public static boolean doDebugMessages = false; //-------- //-------- Variables //-------- /** * The current state of the U brick, passed into constructor. */ private UAdapterState uState; /** * The current current count for the number of return bytes from * the packet being created. */ protected int totalReturnLength; /** * Current raw send packet before it is added to the packetsVector */ protected RawSendPacket packet; /** * Vector of raw send packets */ protected Vector packetsVector; /** * Flag to send only 'bit' commands to the DS2480 */ protected boolean bitsOnly; //-------- //-------- Constructors //-------- /** * Constructs a new u packet builder. * * @param startUState the object that contains the U brick state * which is reference when creating packets */ public UPacketBuilder (UAdapterState startUState) { // get a reference to the U state uState = startUState; // create the buffer for the data packet = new RawSendPacket(); // create the vector packetsVector = new Vector(); // restart the packet to initialize restart(); // Default on SunOS to bit-banging bitsOnly = (System.getProperty("os.name").indexOf("SunOS") != -1); // check for a bits only property String bits = OneWireAccessProvider.getProperty("onewire.serial.forcebitsonly"); if (bits != null) { if (bits.indexOf("true") != -1) bitsOnly = true; else if (bits.indexOf("false") != -1) bitsOnly = false; } } //-------- //-------- Packet handling Methods //-------- /** * Reset the packet builder to start a new one. */ public void restart () { // clear the vector list of packets packetsVector.removeAllElements(); // truncate the packet to 0 length packet.buffer.setLength(0); packet.returnLength = 0; // reset the return cound totalReturnLength = 0; } /** * Take the current packet and place it into the vector. This * indicates a place where we need to wait for the results from * DS9097U adapter. */ public void newPacket () { // add the packet packetsVector.addElement(packet); // get a new packet packet = new RawSendPacket(); } /** * Retrieve enumeration of raw send packets * * @return the enumeration of packets */ public Enumeration getPackets () { // put the last packet into the vector if it is non zero if (packet.buffer.length() > 0) newPacket(); return packetsVector.elements(); } //-------- //-------- 1-Wire Network operation append methods //-------- /** Add the command to reset the OneWire at the current speed. * * @return the number offset in the return packet to get the * result of this operation */ public int oneWireReset () { // set to command mode setToCommandMode(); // append the reset command at the current speed packet.buffer.append(( char ) (FUNCTION_RESET | uState.uSpeedMode)); // count this as a return totalReturnLength++; packet.returnLength++; // check if not streaming resets if (!uState.streamResets) newPacket(); // check for 2480 wait on extra bytes packet if (uState.longAlarmCheck && ((uState.uSpeedMode == uState.USPEED_REGULAR) || (uState.uSpeedMode == uState.USPEED_FLEX))) newPacket(); return totalReturnLength - 1; } /** * Append data bytes (read/write) to the packet. * * @param dataBytesValue character array of data bytes * * @return the number offset in the return packet to get the * result of this operation */ public int dataBytes (char dataBytesValue []) { char byte_value; int i,j; // set to data mode if (!bitsOnly) setToDataMode(); // provide debug output if (doDebugMessages) System.out.println("DEBUG: UPacketbuilder-dataBytes[] length " + dataBytesValue.length); // record the current count location int ret_value = totalReturnLength; // check each byte to see if some need duplication for (i = 0; i < dataBytesValue.length; i++) { // convert the rest to OneWireIOExceptions if (bitsOnly) { // change byte to bits byte_value = dataBytesValue [i]; for (j = 0; j < 8; j++) { dataBit(((byte_value & 0x01) == 0x01), false); byte_value >>>= 1; } } else { // append the data packet.buffer.append(dataBytesValue [i]); // provide debug output if (doDebugMessages) System.out.println( "DEBUG: UPacketbuilder-dataBytes[] byte[" + Integer.toHexString(( int ) dataBytesValue [i] & 0x00FF) + "]"); // check for duplicates needed for special characters if ((( char ) (dataBytesValue [i] & 0x00FF) == uState.MODE_COMMAND) || ((( char ) (dataBytesValue [i] & 0x00FF) == uState.MODE_SPECIAL) && (uState.revision == uState.CHIP_VERSION1))) { // duplicate this data byte packet.buffer.append(dataBytesValue [i]); } // add to the return number of bytes totalReturnLength++; packet.returnLength++; // provide debug output if (doDebugMessages) System.out.println( "DEBUG: UPacketbuilder-dataBytes[] returnlength " + packet.returnLength + " bufferLength " + packet.buffer.length()); // check for packet too large or not streaming bytes if ((packet.buffer.length() > MAX_BYTES_STREAMED) ||!uState.streamBytes) newPacket(); } } return ret_value; } /** * Append data bytes (read/write) to the packet. * * @param dataBytesValue byte array of data bytes * @param off offset into the array of data to start * @param len length of data to send / receive starting at 'off' * * @return the number offset in the return packet to get the * result of this operation */ public int dataBytes (byte[] dataBytesValue, int off, int len) { char[] temp_ch = new char [len]; for (int i = 0; i < len; i++) temp_ch [i] = ( char ) dataBytesValue [off + i]; return dataBytes(temp_ch); } /** * Append a data byte (read/write) to the packet. * * @param dataByteValue data byte to append * * @return the number offset in the return packet to get the * result of this operation */ public int dataByte (char dataByteValue) { // contruct a temporary array of characters of lenght 1 // to use the dataBytes method char[] temp_char_array = new char [1]; temp_char_array [0] = dataByteValue; // provide debug output if (doDebugMessages) System.out.println( "DEBUG: UPacketbuilder-dataBytes [" + Integer.toHexString(( int ) dataByteValue & 0x00FF) + "]"); return dataBytes(temp_char_array); } /** * Append a data byte (read/write) to the packet. Do a strong pullup * when the byte is complete * * @param dataByteValue data byte to append * * @return the number offset in the return packet to get the * result of this operation */ public int primedDataByte (byte dataByteValue) { int offset, start_offset = 0; // create a primed data byte by using bits with last one primed for (int i = 0; i < 8; i++) { offset = dataBit(((dataByteValue & 0x01) == 0x01), (i == 7)); dataByteValue >>>= 1; // record the starting offset if (i == 0) start_offset = offset; } return start_offset; } /** * Append a data bit (read/write) to the packet. * * @param dataBit bit to append * @param strong5V true if want strong5V after bit * * @return the number offset in the return packet to get the * result of this operation */ public int dataBit (boolean dataBit, boolean strong5V) { // set to command mode setToCommandMode(); // append the bit with polarity and strong5V options packet.buffer.append(( char ) (FUNCTION_BIT | uState.uSpeedMode | ((dataBit) ? BIT_ONE : BIT_ZERO) | ((strong5V) ? PRIME5V_TRUE : PRIME5V_FALSE))); // add to the return number of bytes totalReturnLength++; packet.returnLength++; // check for packet too large or not streaming bits if ((packet.buffer.length() > MAX_BYTES_STREAMED) ||!uState.streamBits) newPacket(); return (totalReturnLength - 1); } /** * Append a search to the packet. Assume that any reset and search * command have already been appended. This will add only the search * itself. * * @param mState OneWire state * * @return the number offset in the return packet to get the * result of this operation */ public int search (OneWireState mState) { // set to command mode setToCommandMode(); // search mode on packet.buffer.append(( char ) (FUNCTION_SEARCHON | uState.uSpeedMode)); // set to data mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -