memorycache.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 1,455 行 · 第 1/3 页
JAVA
1,455 行
/*--------------------------------------------------------------------------- * Copyright (C) 2001 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.application.file;// importsimport java.util.Vector;import java.util.Enumeration;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.container.PagedMemoryBank;import com.dalsemi.onewire.container.MemoryBank;import com.dalsemi.onewire.container.OTPMemoryBank;import com.dalsemi.onewire.utils.CRC16;import com.dalsemi.onewire.utils.Bit;/** * Class to provide read/write cache services to a 1-Wire memory * device. Writes are only performed when this classes <code>sync()</code> * method is called. Provides page bitmap services for OTP devices. * * <p>Objectives: * <ul> * <li> Cache read/written pages * <li> write only on sync() * <li> write order is oldest to newest. * <li> Collect redirection information when appropriate * </ul> * * @author DS * @version 0.01, 1 June 2001 * @see com.dalsemi.onewire.application.file.OWFile * @see com.dalsemi.onewire.application.file.OWFileDescriptor * @see com.dalsemi.onewire.application.file.OWFileInputStream * @see com.dalsemi.onewire.application.file.OWFileOutputStream */class MemoryCache{ //-------- //-------- Static Final Variables //-------- /** cache pageState's */ private static final int NOT_READ = 0; private static final int READ_CRC = 1; private static final int READ_NO_CRC = 2; private static final int VERIFY = 3; private static final int REDIRECT = 4; private static final int WRITE = 5; /** Flag to indicate the writeLog entry is empty */ private static final int EMPTY = -1; /** Field NONE - flag to indicate last page read is not known */ private static final int NONE = -100; /** Field USED - flag to indicate page bitmap file used */ private static final int USED = 0; /** Field NOT_USED - flag to indicated page bitmap file un-used */ private static final int NOT_USED = 1; /** Enable/disable debug messages */ private static final boolean doDebugMessages = false; //-------- //-------- Variables //-------- /** Field owd - 1-Wire container that containes this memory to cache */ private OneWireContainer[] owd; /** Field cache - 2 dimentional array to contain the cache */ private byte[][] cache; /** Field len - array of lengths of packets found */ private int[] len; /** Field pageState - array of flags to indicate the page has been changed */ private int[] pageState; /** Field banks - vector of memory banks that contain the Filesystem */ private Vector banks; /** Field totalPages - total pages in this Filesystem */ private int totalPages; /** Field lastPageRead - last page read by this cache */ private int lastPageRead; /** Field maxPacketDataLength - maximum data length on a page */ private int maxPacketDataLength; /** Field bankPages - array of the number of pages in vector of memory banks */ private int[] bankPages; /** Field startPages - array of the number of start pages for device list */ private int[] startPages; /** Field writeLog - array to track the order of pages written to the cache */ private int[] writeLog; /** Field tempExtra - temporary buffer used to to read the extra information from a page read */ private byte[] tempExtra; /** Field tempPage - temporary buffer the size of a page */ private byte[] tempPage; /** Field redirect - array of redirection bytes */ private int[] redirect; /** Field owners - vector of classes that are using this cache */ private Vector owners; /** Field openedToWrite - vector of files that have been opened to write on this filesystem */ private Vector openedToWrite; /** Field canRedirect - flag to indicate page redirection information must be gathered */ private boolean canRedirect; /** Field pbmBank - memory bank used for the page bitmap */ private OTPMemoryBank pbmBank; /** Field pbmByteOffset - byte offset into page bitmap buffer */ private int pbmByteOffset; /** Field pbmBitOffset - bit offset into page bitmap buffer */ private int pbmBitOffset; /** Field pbmCache - buffer to cache the page bitmap */ private byte[] pbmCache; /** Field pbmCacheModified - modifified version of the page bitmap */ private byte[] pbmCacheModified; /** Field pbmRead - flag indicating that the page bitmap has been read */ private boolean pbmRead; /** Field lastFreePage - last free page found in the page bitmap */ private int lastFreePage; /** Field lastDevice - last device read/written */ private int lastDevice; /** Field autoOverdrive - flag to indicate if we need to do auto-ovedrive */ private boolean autoOverdrive; //-------- //-------- Constructor //-------- /** * Construct a new memory cache for provided 1-wire container device. * * @param device 1-Wire container */ public MemoryCache(OneWireContainer device) { OneWireContainer[] devices = new OneWireContainer[1]; devices[0] = device; init(devices); } /** * Construct a new memory cache for provided 1-wire container device. * * @param device 1-Wire container */ public MemoryCache(OneWireContainer[] devices) { init(devices); } /** * Initializes this memory cache for provided 1-wire container device(s). * * @param devices 1-Wire container(s) */ private void init(OneWireContainer[] devices) { owd = devices; int mem_size=0; PagedMemoryBank pmb = null; banks = new Vector(1); owners = new Vector(1); openedToWrite = new Vector(1); startPages = new int[owd.length]; lastDevice = 0; // check to see if adapter supports overdrive try { autoOverdrive = devices[0].getAdapter().canOverdrive(); } catch (OneWireException e) { autoOverdrive = false; } //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___Constructor MemoryCache: " + devices[0].getAddressAsString() + " num " + devices.length); // loop through all of the devices in the array totalPages = 0; for (int dev=0; dev < owd.length; dev++) { // check to make sure each device can do Overdrive if (owd[dev].getMaxSpeed() != DSPortAdapter.SPEED_OVERDRIVE) autoOverdrive = false; // record the start page offset for each device startPages[dev] = totalPages; // enumerate through the memory banks and collect the // general purpose banks in a vector for (Enumeration bank_enum = owd[dev].getMemoryBanks(); bank_enum.hasMoreElements(); ) { // get the next memory bank MemoryBank mb = (MemoryBank) bank_enum.nextElement(); // look for pbm memory bank (used in file structure) if (mb.isWriteOnce() && !mb.isGeneralPurposeMemory() && mb.isNonVolatile() && (mb instanceof OTPMemoryBank)) { // if more then 1 device with a OTP then error if (owd.length > 1) { totalPages = 0; return; } // If only 128 bytes then have DS2502 or DS2406 which have bitmap included // in the only status page. All other EPROM devices have a special memory // bank that has 'Bitmap' in the title. if ((mem_size == 128) || (mb.getBankDescription().indexOf("Bitmap") != -1)) { pbmBank = (OTPMemoryBank) mb; if (mem_size == 128) pbmBitOffset = 4; pbmByteOffset = 0; canRedirect = true; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_Paged BitMap MemoryBank: " + mb.getBankDescription() + " with bit offset " + pbmBitOffset); } } // check regular memory bank if (!mb.isGeneralPurposeMemory() ||!mb.isNonVolatile() ||!(mb instanceof PagedMemoryBank)) continue; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_Using MemoryBank: " + mb.getBankDescription()); banks.addElement(mb); mem_size += mb.getSize(); totalPages += ((PagedMemoryBank)mb).getNumberPages(); } } // count total bankPages bankPages = new int [banks.size()]; totalPages = 0; for (int b = 0; b < banks.size(); b++) { pmb = (PagedMemoryBank) banks.elementAt(b); bankPages [b] = pmb.getNumberPages(); totalPages += bankPages [b]; } // create the cache len = new int [totalPages]; pageState = new int [totalPages]; writeLog = new int [totalPages]; redirect = new int [totalPages]; if (pmb != null) { maxPacketDataLength = pmb.getMaxPacketDataLength(); cache = new byte [totalPages][pmb.getPageLength()]; tempPage = new byte [pmb.getPageLength()]; } // initialize some of the flag arrays for (int p = 0; p < totalPages; p++) { pageState [p] = NOT_READ; len [p] = 0; writeLog [p] = EMPTY; } // if getting redirection information, create necessarey arrays if (canRedirect) { tempExtra = new byte [pmb.getExtraInfoLength()]; pbmCache = new byte [pbmBank.getSize()]; pbmCacheModified = new byte [pbmBank.getSize()]; pbmRead = false; } else pbmRead = true; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_Total Pages: " + totalPages + ", get Redirection = " + canRedirect); } /** * Gets the number of pages in this cache * * @return number of pages in the cache */ public int getNumberPages() { return totalPages; } /** * Gets the number of pages in the specified bank number * * @param bankNum bank number to retrieve number of pages * * @return number of pages in the bank */ public int getNumberPagesInBank(int bankNum) { if (totalPages > 0) return bankPages[bankNum]; else return 0; } /** * Gets the page number of the first page on the specified * device. If the device number is not valid then return 0. * * @param deviceNum device number to retrieve page offset * * @return page number of first page on device */ public int getPageOffsetForDevice(int deviceNum) { return startPages[deviceNum]; } /** * Gets the maximum number of bytes for data in each page. * * @return max number of data bytes per page */ public int getMaxPacketDataLength() { return maxPacketDataLength; } /** * Check if this memory device is write-once. If this is true then * the page bitmap facilities in this class will be used. * * @return true if this device is write-once */ public boolean isWriteOnce() { return canRedirect; } /** * Read a page packet. If the page is available in the cache * then return that data. * * @param page page to read * @param readBuf buffer to place the data in * @param offset offset into the read buffer * * @return the number byte in the packet * * @throws OneWireException when the adapter is not setup properly * @throws OneWireIOException when an 1-Wire IO error occures */ public int readPagePacket(int page, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("___readPagePacket (" + page + ") "); // check if have a cache (any memory banks) if (totalPages == 0) throw new OneWireException("1-Wire Filesystem does not have memory"); // check if out of range if (page >= totalPages) throw new OneWireException("Page requested is not in memory space"); // check if doing autoOverdrive (greatly improves multi-device cache speed) if (autoOverdrive) { autoOverdrive = false; DSPortAdapter adapter = owd[0].getAdapter(); adapter.setSpeed(adapter.SPEED_REGULAR); adapter.reset(); adapter.putByte(( byte ) 0x3C); adapter.setSpeed(adapter.SPEED_OVERDRIVE); } // check if need to read the page bitmap for the first time if (!pbmRead) readPageBitMap(); // page NOT cached (maybe redirected) if ((pageState[page] == NOT_READ) || (pageState[page] == READ_NO_CRC) || (redirect [page] != 0)) { //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_Not in cache or redirected, length=" + len [page] + " redirect=" + redirect [page]); // page not cached, so read it int local_page = getLocalPage(page); PagedMemoryBank pmb = getMemoryBankForPage(page); int local_device_page = page - startPages[getDeviceIndex(page)]; //\\//\\//\\//\\//\\//\\//\\// if (doDebugMessages) System.out.println("_Look in MemoryBank " + pmb.getBankDescription()); if (canRedirect) { // don't use multi-bank page reference (would not work with redirect) // loop while page is redirected int loopcnt = 0; for (;;) { // check for redirection if (redirect [page] == 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?