📄 checkbasestation.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.usb.checkbasestation;import com.sun.spot.peripheral.Spot;import gnu.io.CommPortIdentifier;import gnu.io.PortInUseException;import gnu.io.SerialPort;import gnu.io.UnsupportedCommOperationException;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintStream;import java.util.Enumeration;import com.sun.spot.peripheral.ota.ISpotClientConstants;/** * NOT COMPLETELY IMPLEMENTED, AND NOT USED BY SPOTWORLD. THIS IS A PLACEHOLDER CLASS FOR NOW. * * The CheckBasestation class determines whether a device connected to a given * serial port is a base station. Due to limitations in the module used to do * the detection, CheckBaseStation must be executed as a separate process. * * @author Christian P黨ringer * */public class CheckBasestation { private static PrintStream infoOut = System.out; private String baseStationPort; private boolean isVerbose = Boolean.getBoolean("verbose"); private InputStream inStream; private BufferedReader in; private OutputStream out; private SerialPort serialPort; private boolean portIsInUse; private CheckBasestation(String port) { super(); this.baseStationPort = port; } public CheckBasestation() { } /* * A spot which is waiting in a bootloader prompt, should not be tested * whether it is a basestation. (and I can't be a running one anyway). * This is because the bytes sent by hostagent can be interpreted as * commands by the bootloader. In fact it is very probably that this * happens as the commands are one byte values, and all non command * values are just thrown away by the bootloader. In most cases the * bootloader would go to a flash modus and then fail after some time, * and thus hang or print a inappropriate error message (like flashing * failed). It's not impossible that parts of the flash are overwritten, * although it's very improbable that this happens. (The serial * communication has a special format (see xmodem.c)) * * As we can't wait any time in checkBaseStation, we only wait a certain * time, and to decrease the number of false negatives, we say for any * occurence of BOOTLOADER_CMD_HEADER (which is *EL*) that the spot is * not a basestation. This means that spots which are currently starting * up are seen as non basestations. * * First we send an invalid command 'X' to the bootloader. If it's * waiting for commands it will print an Error (including the *EL* * string), and we now that it's not a running base station The other * possibility is that the bootloader is in the loop waiting for the * sync command. (that happens when the spot is booting). Then we send a * 'S' (which works like synchronize which are just more 'S') command, * if the bootloader is running the spot will response with a command * list and the *EL*> prompt. In this case we know it's not a * basestation, and we send a RESYNC command to return the state it had * before. * * It would be good if the synchronize command would be more complicated * to avoid doing it by chance. Then the second step could be avoided. */ private boolean detectBootloader() { try { if (isVerbose) System.out.println("detect bootloader"); try { initComms(baseStationPort); portIsInUse = false; } catch (PortInUseException e1) { if (isVerbose) e1.printStackTrace(); portIsInUse = true; return false; } // "X" is an invalid // command, the // bootloader will // echo its commands + *EL* if it is running. sendBootloaderCommand("X"); boolean isOk = false; while (!isOk) { try { System.out.println("."); // this resets the // watchdogcounter. Thread.sleep(50); isOk = true; } catch (InterruptedException e) { } } System.out.println("."); // this resets the watchdogcounter. int bytesAvailable = inStream.available(); byte[] buf = new byte[bytesAvailable]; if (bytesAvailable > 0) { inStream.read(buf); String spotReply = new String(buf); if (isVerbose) System.out.println("spot:" + spotReply); if (spotReply .lastIndexOf(ISpotClientConstants.BOOTLOADER_CMD_HEADER) != -1) { if (isVerbose) System.out .println("Spot is in bootloader prompt, Not a basestation (at least not a running one)"); closeComms(); return false; } } closeComms(); } catch (IOException e) { if (isVerbose) e.printStackTrace(); return false; } // Spot is not in bootloader prompt, thus perhaps a basestation if (isVerbose) System.out.println("OK for scan, not bootloader detectect."); return true; } public boolean checkIsBaseStation(String port) { baseStationPort = port; return checkIsBaseStation(); } /** * Determines whether the device connected to the port is a base * station. * * @return returns true if the device connected to the serial port is a * base station * */ public boolean checkIsBaseStation() { System.setProperty("SERIAL_PORT", baseStationPort); try { if (detectBootloader() == false) return false; // This also resets the watchdogcounter. System.out.println("Testing if spot " + baseStationPort + " responds."); System.out.println("IEEE Address: " + Spot.getInstance().getRadioPolicyManager().getIEEEAddress()); return true; } catch (Exception ex) { ex.printStackTrace(infoOut); } return false; } /** * Initialize the comms connection * * @param portName * Port to use * @throws IOException */ private void initComms(String portName) throws PortInUseException, IOException { CommPortIdentifier portId = null; Enumeration portList; portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { CommPortIdentifier nextPortId = (CommPortIdentifier) portList.nextElement(); if (nextPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (nextPortId.getName().equalsIgnoreCase(portName)) { portId = nextPortId; // break; } } } if (portId == null) { throw new IOException(portName + " not found"); } serialPort = (SerialPort) portId.open("Flasher", 2000); try { serialPort.disableReceiveThreshold(); serialPort.enableReceiveTimeout(3000); serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { throw new IOException(e.getMessage()); } inStream = serialPort.getInputStream(); in = new BufferedReader(new InputStreamReader(inStream)); out = serialPort.getOutputStream(); } /** * Close the comms connection * * @throws IOException */ private void closeComms() throws IOException { inStream.close(); in.close(); out.close(); serialPort.close(); } /* * private String getTargetResponse() throws IOException { String result = * null; while (true) { try { result = in.readLine(); // * System.out.println("[HDIAG] received: " + result); break; } catch * (IOException e) { if (e.getMessage().equals("Underlying input stream * returned zero bytes")) { // do nothing, as it's just a timeout * System.out.println("timeout"); } else { throw e; } } } return result; } */ private void sendBootloaderCommand(String string) throws IOException { out.write(string.getBytes()); } /** * Checks whether the connected device is a base station. * * @param args * The first command line argument is the serial port * which will be checked whether a base station is * connected. */ public static void main(String[] args) { if (args.length == 0) { infoOut.println("Wrong argument format. \n\t usage: CheckBasestation SERIAL_PORT"); System.exit(-1); } String port = args[0]; CheckBasestation checkBasestation = new CheckBasestation(port); if (checkBasestation.checkIsBaseStation())// System.exit(0); System.out.println("IS BASESTATION"); else { if (checkBasestation.portIsInUse) System.out.println("PORT IN USE"); else System.out.println("PORT NOT IN USE"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -