package com.microchip.mplab.ipecmd.common; import com.microchip.crownking.mplabinfo.DeviceSupport; import com.microchip.crownking.mplabinfo.DeviceSupportException; import com.microchip.crownking.mplabinfo.DeviceSupport.Device; import com.microchip.mplab.ipecmd.FilterSystemMessage; import com.microchip.mplab.ipecmd.commandline.CommandCodes; import com.microchip.mplab.ipecmd.errorlog.LoggingOutputStream; import com.microchip.mplab.ipecmd.errorlog.StdOutErrLevel; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Properties; import java.util.ResourceBundle; import java.util.logging.LogManager; import java.util.logging.Logger; public class SharedData { private boolean quitModeFlag = false; private Properties corePeroperties = null; private ResourceBundle bundle = null; private int errorCode = 0; private boolean jtagArgNotUsed; PrintWriter outputstream = null; private int portNumber = 2012; private static SharedData ref; public String getSystemAppLocation() { String wd = System.getProperty("user.home"); if (System.getProperty("os.name").contains("Windows")) { wd = System.getenv("APPDATA").replace("\\", "/") + "/MPLABX IPE/"; } return wd; } public void initOutputData() { String appConfigFilePath = this.getSystemAppLocation() + "socketoutput.ini"; try { File f = new File(appConfigFilePath); if (f.exists()) { f.delete(); } } catch (Exception var4) { } } public void writeOutputData(String dataToGo) { String appConfigFilePath = this.getSystemAppLocation() + "socketoutput.ini"; try { File f = new File(appConfigFilePath); FileOutputStream fos = new FileOutputStream(f, true); if (dataToGo.trim().length() > 0) { fos.write(dataToGo.getBytes()); } fos.close(); } catch (IOException var5) { } } public void setSystemErrorFilter() { if (System.getProperty("java.util.logging.config.file") == null) { LogManager logManager = LogManager.getLogManager(); logManager.reset(); PrintStream stderr = System.err; Logger logger = Logger.getLogger("stderr"); LoggingOutputStream los = new LoggingOutputStream(logger, StdOutErrLevel.STDERR); System.setErr(new PrintStream(los, true)); } } public void setSystemOutFilter() { PrintStream origOut = System.out; PrintStream interceptor = new FilterSystemMessage(origOut); System.setOut(interceptor); } public void setOutputStreamObj(PrintWriter os) { this.outputstream = os; } public void setQuitMode(boolean flag) { this.quitModeFlag = flag; } public void quitAndSendErrorCode(String msg, int errorCode) { this.IPEPrint(msg); this.setLastErrorCode(errorCode); } public void IPEPrint(String msg) { if (!this.quitModeFlag) { System.out.print("IPE:" + msg); if (this.outputstream != null) { this.outputstream.println(msg.trim()); this.outputstream.flush(); } } } public void getPropertyInfo(String msg) { if (!this.quitModeFlag) { System.out.println(msg); } } public boolean verifyDeviceName(String deviceName) { boolean retVal = false; try { DeviceSupport devSupport = DeviceSupport.getInstance(); Device dv = devSupport.getDeviceFromDeviceName(deviceName); if (dv == null) { retVal = false; } else { retVal = true; } } catch (DeviceSupportException var5) { retVal = false; } return retVal; } public boolean isNumeric(String str) { try { int nfe = Integer.parseInt(str); return true; } catch (NumberFormatException var3) { return false; } } public boolean isDouble(String str) { try { double nfe = Double.parseDouble(str); return true; } catch (NumberFormatException var4) { return false; } } public boolean setCoreParameters(String key, String value) { if (this.corePeroperties != null) { this.corePeroperties.setProperty(key, value); return true; } else { return false; } } public Properties getCoreParameters() { return this.corePeroperties; } public long hexToDecimal(String hexString) { long retVal = -1L; try { if (!hexString.startsWith("0x") && !hexString.startsWith("0X")) { retVal = Long.valueOf(hexString, 16); } else { retVal = Long.decode(hexString); } } catch (Exception var5) { retVal = -1L; } return retVal; } public String getStringFromBundle(String keyName) { String retVal = null; return this.bundle.getString(keyName); } public void printScreen(String keyName) { String retVal = null; retVal = this.bundle.getString(keyName); this.IPEPrint(retVal); } public void disableNetworkToolsProperty() { System.getProperties().setProperty("com.microchip.mplab.libs.comm.enableNetworkTools", "false"); } public String getExtention(String fullPath) { if (fullPath.contains(".")) { int dot = fullPath.lastIndexOf("."); return fullPath.substring(dot + 1); } else { return null; } } public String getFileNameOnly(String fileNameOnly) { if (fileNameOnly.contains(".")) { int dot = fileNameOnly.lastIndexOf("."); return fileNameOnly.substring(0, dot); } else { return null; } } public String getVersion() { return this.getStringFromBundle("IPECMD.VERSION"); } public String getShortName(String toolName) { String retValue = ""; if (toolName.contains("Real ICE")) { retValue = "RICE"; } if (toolName.contains("PICkit3")) { retValue = "PK3"; } if (toolName.contains("ICD 3")) { retValue = "ICD3"; } if (toolName.contains("PK3OB")) { retValue = "PKOB"; } if (toolName.contains("PKOB SKDE")) { retValue = "PKOB"; } if (toolName.contains("PKOB4")) { retValue = "PKOB4"; } if (toolName.contains("PICkit 4")) { retValue = "PK4"; } if (toolName.contains("PICkit 5")) { retValue = "PK5"; } if (toolName.contains("ICD 4")) { retValue = "ICD4"; } if (toolName.contains("ICD 5")) { retValue = "ICD5"; } if (toolName.contains("ICE 4")) { retValue = "ICE4"; } if (toolName.contains("Atmel-ICE")) { retValue = "AICE"; } if (toolName.contains("Snap")) { retValue = "SNAP"; } if (toolName.contains("J-32")) { retValue = "J32"; } if (toolName.contains("EDBG")) { retValue = "Edbg"; } if (toolName.contains("MEDBG")) { retValue = "MEdbg"; } if (toolName.contains("NEDBG")) { retValue = "NEdbg"; } if (toolName.contains("PM5 Program Adapter")) { retValue = "PM5PA"; } if (toolName.contains("PM5 S.No")) { retValue = "PM5"; } return retValue; } public String getPlatformToolID(String toolName) { String toolID = null; if (toolName.equalsIgnoreCase("ICD4")) { toolID = "ICD4Tool"; } else if (toolName.equalsIgnoreCase("ICD5")) { toolID = "ICD5Tool"; } else if (toolName.equalsIgnoreCase("ICE4")) { toolID = "ri4hybrid"; } else if (toolName.equalsIgnoreCase("PK4")) { toolID = "pk4hybrid"; } else if (toolName.equalsIgnoreCase("PK5")) { toolID = "PK5Tool"; } else if (toolName.equalsIgnoreCase("SNAP")) { toolID = "snap"; } else if (toolName.equalsIgnoreCase("PKOB4")) { toolID = "pkob4hybrid"; } else if (toolName.equalsIgnoreCase("AICE")) { toolID = "AtmelIceTool"; } else if (toolName.equalsIgnoreCase("EDBG")) { toolID = "EdbgTool"; } else if (toolName.equalsIgnoreCase("MEDBG")) { toolID = "mEdbgTool"; } else if (toolName.equalsIgnoreCase("NEDBG")) { toolID = "nEdbgTool"; } else if (toolName.equalsIgnoreCase("ICD3")) { toolID = "ICD3PlatformTool"; } else if (toolName.equalsIgnoreCase("PK3")) { toolID = "PICkit3PlatformTool"; } else if (toolName.equalsIgnoreCase("PM3")) { toolID = "PM3PlatformTool"; } else if (toolName.equalsIgnoreCase("RICE")) { toolID = "RealICEPlatformTool"; } else if (toolName.equalsIgnoreCase("PKOB")) { toolID = "PK3OBPlatformTool"; } else if (toolName.equalsIgnoreCase("PM5PA")) { toolID = "pm5"; } else if (toolName.equalsIgnoreCase("J32")) { toolID = "j32"; } return toolID; } public void setLastErrorCode(int code) { this.errorCode = code; } public int getLastErrorCode() { return this.errorCode; } public void setPortNumber(int port) { this.portNumber = port; } public int getPortNumber() { return this.portNumber; } public String getSystemAppData() { String wd = System.getProperty("user.home"); File dir = new File(wd + "/.mchp_ipe"); if (!dir.exists()) { dir.mkdirs(); } return wd.replace("\\", "/") + "/.mchp_ipe/"; } public void getNotifyLock(int portNumber) { try { String appLockFilePath = this.getSystemAppData() + portNumber + ".lock"; File f = new File(appLockFilePath); if (f.exists()) { f.delete(); } } catch (Exception var4) { this.outputstream.println("Could not able to delete process file."); this.outputstream.flush(); System.exit(CommandCodes.OP_FALED); } } public void deleteProcessID(int localPortNo) { try { String appLockFilePath = this.getSystemAppData() + localPortNo + ".ini"; File f = new File(appLockFilePath); if (f.exists()) { f.delete(); } } catch (Exception var4) { this.outputstream.println("Could not able to delete process ID file."); this.outputstream.flush(); System.exit(CommandCodes.OP_FALED); } } private SharedData() { this.corePeroperties = new Properties(); this.bundle = ResourceBundle.getBundle("com.microchip.mplab.ipecmd.common.ipeproperty"); this.jtagArgNotUsed = false; } public static SharedData getSingletonObject() { if (ref == null) { ref = new SharedData(); } return ref; } @Override public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } public void setJtagArgNotUsedFlag(boolean val) { this.jtagArgNotUsed = val; } public boolean getJtagArgNotUsedFlag() { return this.jtagArgNotUsed; } }