package com.microchip.mplab.ipelib.common; import com.microchip.crownking.Pair; import com.microchip.crownking.edc.PIC; import com.microchip.crownking.mplabinfo.FamilyDefinitions.Family; import com.microchip.mplab.crownkingx.xPIC; import com.microchip.mplab.ipelib.IPELibrary; import com.microchip.mplab.mdbcore.RI4Tool.families.FamilyCreator; import com.microchip.mplab.mdbcore.RI4Tool.families.RI4Family; import com.microchip.mplab.mdbcore.RI4Tool.families.RI4FamilyInterface; import com.microchip.mplab.mdbcore.RealICETool.utils.RIExceptions; import com.microchip.mplab.mdbcore.assemblies.Assembly; import com.microchip.mplab.mdbcore.assemblies.AssemblyFactory; import com.microchip.mplab.mdbcore.debugger.Debugger; import com.microchip.mplab.mdbcore.debugger.SDCardInterface; import com.microchip.mplab.mdbcore.sqtpenv.SQTPEnvironment; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.Properties; import java.util.prefs.Preferences; import org.openide.util.Exceptions; import org.openide.util.NbBundle; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class SharedData { private Debugger mdb = null; private Assembly mdbSession = null; private AssemblyFactory assemblyFactory = null; private SDCardInterface sdCardInterface = null; private SQTPEnvironment SQTPEnv = null; private boolean debugBit_Set = false; private boolean tool_connected = false; public boolean makePK2Work = false; private Preferences userPreferences = null; private Properties coreProperties = null; private byte[] pgmMemBytes = new byte[0]; private byte[] userIDMemBytes = new byte[0]; private byte[] bootMemBytes = new byte[0]; private byte[] testMemBytes = new byte[0]; private byte[] auxMemBytes = new byte[0]; private byte[] eeMemBytes = new byte[0]; private byte[] flashData = new byte[0]; private boolean requiresUI = false; private boolean userResponded = false; private int userResponse = 0; private Pair EDataRange = null; private boolean isInvalidHexFile = false; private boolean isOTPSetForCECDevice = false; private static String bundle; private static final String OS_NAME = System.getProperty("os.name", "unknown"); private static final String OS_VERSION = System.getProperty("os.version", "unknown"); private static final String OS_ARCH = System.getProperty("os.arch", "unknown"); private static final String SERVER = "mplab-metrics.s3-website-us-west-2.amazonaws.com"; private static final String PRODUCT = "/ipe/"; private static SharedData ref; public void setOTPForCECDevice(boolean flag) { this.isOTPSetForCECDevice = flag; } public boolean getOTPForCECDevice() { return this.isOTPSetForCECDevice; } public void setInvalidHexFlag(boolean flag) { this.isInvalidHexFile = flag; } public boolean getInvalidHexFlag() { return this.isInvalidHexFile; } public void setDebugBitFlag(boolean flag) { this.debugBit_Set = flag; } public boolean getDebugBitFlag() { return this.debugBit_Set; } public void setUIMode(boolean flag) { this.requiresUI = flag; } public boolean getUIMode() { return this.requiresUI; } public void setUserResponseFlag(boolean flag) { this.userResponded = flag; } public boolean getUserResponseFlag() { return this.userResponded; } public void setUserResponseCode(int code) { this.userResponse = code; } public int getUserResponseCode() { return this.userResponse; } public void setSQTPEnvironment(SQTPEnvironment se) { this.SQTPEnv = se; } public SQTPEnvironment getSQTPEnvironment() { return this.SQTPEnv; } public void setassemblyFactory(AssemblyFactory af) { this.assemblyFactory = af; } public AssemblyFactory getassemblyFactory() { return this.assemblyFactory; } public void setAssembly(Assembly Session) { this.mdbSession = Session; } public Assembly getAssemblySession() { return this.mdbSession; } public boolean isAssemblySessionActive() { boolean retVal = false; if (this.mdbSession != null) { retVal = true; } return retVal; } public Debugger getDebugger() { return this.mdb; } public void setDebugger(Debugger dbg) { this.mdb = dbg; if (this.mdb != null) { this.tool_connected = this.mdb.IsConnected(); } } public boolean setUserCoreProperties(Properties properties) { boolean retVal = false; if (this.mdbSession != null && this.assemblyFactory != null) { this.assemblyFactory.SetToolProperties(this.mdbSession, properties); retVal = true; } else { retVal = false; } return retVal; } public xPIC getDeviceName() { return this.mdbSession != null ? this.mdbSession.GetDevice() : null; } public long getMSBLSB32(byte msb1, byte msb2, byte lsb1, byte lsb2) { long result = 0L; long tempMSB1 = 0L; long tempLSB1 = 0L; long tempMSB2 = 0L; long tempLSB2 = 0L; String s = null; s = String.format("%x", msb1); tempMSB1 = Integer.valueOf(s, 16).longValue(); tempMSB1 *= 16777216L; s = String.format("%x", msb2); tempMSB2 = Integer.valueOf(s, 16).longValue(); tempMSB2 *= 65536L; s = String.format("%x", lsb1); tempLSB1 = Integer.valueOf(s, 16).longValue(); tempLSB1 *= 256L; s = String.format("%x", lsb2); tempLSB2 = Integer.valueOf(s, 16).longValue(); return tempMSB1 + tempMSB2 + tempLSB1 + tempLSB2; } public String getMSBLSB(byte msb, byte lsb) { int result = 0; int tempMSB = 0; int tempLSB = 0; String s = null; s = String.format("%02X", msb); tempMSB = Integer.valueOf(s, 16); tempMSB *= 256; return s.concat(String.format("%02X", lsb)); } public int getMSBLSB_Integer(byte msb, byte lsb) { int result = 0; int tempMSB = 0; int tempLSB = 0; String s = null; s = String.format("%x", msb); tempMSB = Integer.valueOf(s, 16); tempMSB *= 256; s = String.format("%x", lsb); tempLSB = Integer.valueOf(s, 16); return tempMSB + tempLSB; } public int getByteasInt(byte bite) { int result = 0; String s = null; s = String.format("%x", bite); return Integer.valueOf(s, 16); } public boolean setPgmMemBytes(long memSize) { if (memSize != -1L) { this.pgmMemBytes = new byte[(int)memSize]; return true; } else { return false; } } public boolean setPgmMemBytes(byte[] buffer) { System.arraycopy(buffer, 0, this.pgmMemBytes, 0, buffer.length); return true; } public byte[] getPgmMemBytes() { return this.pgmMemBytes; } public boolean setEEMemBytes(long eeMemSize) { if (eeMemSize != -1L) { this.eeMemBytes = new byte[(int)eeMemSize]; return true; } else { return false; } } public boolean setEEMemBytes(byte[] buffer) { System.arraycopy(buffer, 0, this.eeMemBytes, 0, buffer.length); return true; } 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 void setMemRange(Pair eEDataRange) { this.EDataRange = eEDataRange; } public void setSDCard(SDCardInterface sdInt) { this.sdCardInterface = sdInt; } public SDCardInterface getSDCardInterface() { return this.mdbSession == null ? null : (SDCardInterface)this.mdbSession.getLookup().lookup(SDCardInterface.class); } public boolean setUserIDMemBytes(long memSize) { if (memSize != -1L) { this.userIDMemBytes = new byte[(int)memSize]; return true; } else { return false; } } public String getUserID(byte msb1, byte msb2, byte lsb1, byte lsb2) { String s = null; String specifier = ""; if (this.getAssemblySession().GetDevice().getFamily() == Family.Baseline) { specifier = "%X"; } else { specifier = "%02X"; } s = String.format(specifier, msb1); s = s.concat(String.format(specifier, msb2)); s = s.concat(String.format(specifier, lsb1)); return s.concat(String.format(specifier, lsb2)); } public boolean setFlashBytes(int siz) { if (siz != -1) { this.flashData = new byte[siz]; return true; } else { return false; } } public boolean setFlashBytes(byte[] buffer) { System.arraycopy(buffer, 0, this.flashData, 0, buffer.length); return true; } public boolean setBootFlashBytes(byte[] buffer) { System.arraycopy(buffer, 0, this.bootMemBytes, 0, buffer.length); return true; } public boolean setBootFlashBytes(long memSize) { if (memSize != -1L) { this.bootMemBytes = new byte[(int)memSize]; return true; } else { return false; } } public boolean setAuxMemBytes(long memSz) { if (memSz != -1L) { this.auxMemBytes = new byte[(int)memSz * 2]; return true; } else { return false; } } public boolean setAuxMemBytes(byte[] buffer) { System.arraycopy(buffer, 0, this.auxMemBytes, 0, buffer.length); return true; } public String getUserID(byte byt) { String s = null; String specifier = ""; if (this.getAssemblySession().GetDevice().getFamily() == Family.Baseline) { specifier = "%X"; } else { specifier = "%02X"; } return String.format(specifier, byt); } public String getNodeAttributeText(String FullNodePath, String Item, PIC pic) { String RetStr = null; Document doc = pic.getDoc(); Node node = pic.walk(doc, FullNodePath); if (node != null) { NamedNodeMap NNM = node.getAttributes(); if (NNM != null) { node = NNM.getNamedItem(Item); if (node != null) { RetStr = node.getNodeValue(); } } } return RetStr; } public boolean isConnected() { return this.mdb == null ? false : this.mdb.IsConnected(); } public String[] getDevice_Tool() { return new String[]{this.mdbSession.GetDeviceName(), this.mdbSession.GetToolName()}; } public void setUserParameters(Preferences usrProperties) { this.userPreferences = usrProperties; } public Preferences getUserParameters() { return this.userPreferences; } public String getUserParameters(String Parameter) { return this.userPreferences.get(Parameter, ""); } public void setCoreParameters(Properties coreP) { this.coreProperties = coreP; } public Properties getCoreParameters() { return this.coreProperties; } public void setIPEMetricsToServer(String IPEVersion, String toolInfo, String devInfo) { try { String USER_AGENT = "MPLABXIPE/" + IPEVersion; StringBuilder sb = new StringBuilder(); boolean hasUserPermission = false; URL url = null; String urlString = null; String queryString = ""; String modulePermission = System.getProperty("statistics.connect", "true"); if (!modulePermission.equalsIgnoreCase("false")) { hasUserPermission = true; } if (hasUserPermission) { if ("".equals(devInfo)) { devInfo = "NA"; } if ("".equals(toolInfo)) { toolInfo = "NA"; } sb.append("").append("tool=").append(toolInfo); sb.append("&").append("device=").append(devInfo); sb.append("&").append("os=").append(this.getOsInfo()); sb.append("&").append("ver=").append(OS_VERSION); sb.append("&").append("os_arch=").append(OS_ARCH); queryString = sb.toString(); URI uri; if (queryString != null && !queryString.isEmpty()) { uri = new URI("http", "mplab-metrics.s3-website-us-west-2.amazonaws.com", "/ipe/", queryString, null); } else { uri = new URI("http", "mplab-metrics.s3-website-us-west-2.amazonaws.com", "/ipe/", null, null); } urlString = uri.toASCIIString(); url = new URL(urlString); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuilder response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println("\n" + response.toString()); } } catch (MalformedURLException var17) { } catch (IOException var18) { } catch (URISyntaxException var19) { } catch (Exception var20) { } } public RI4FamilyInterface getRI4FamilyInstance() { RI4FamilyInterface ri4Family = null; FamilyCreator FC = new FamilyCreator(getSingletonObject().getAssemblySession().GetDevice()); try { ri4Family = FC.GetFamily(); } catch (RIExceptions var4) { Exceptions.printStackTrace(var4); } if (ri4Family == null) { ri4Family = new RI4Family(); } return ri4Family; } public String getOsInfo() { String os; if (OS_NAME.startsWith("Windows")) { os = OS_NAME.replace(" ", "_"); } else { os = OS_NAME.replace(" ", "") + "_" + OS_VERSION; } return os; } private static String getString(String s) { if (bundle == null) { bundle = NbBundle.getMessage(IPELibrary.class, "ReadConfigMemoryActionName"); } return bundle; } SharedData() { } public static SharedData getSingletonObject() { if (ref == null) { ref = new SharedData(); } return ref; } @Override public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } public static enum PredefinedType implements SharedData.Type { READ_CONFIG_MEMORY("ReadConfigMemory"), UPLOAD_IMAGE("UploadImage"), READ_EXECUTION_MEMORY("ReadExecutionMemory"); private final String localizedName; private PredefinedType(String resourceNamePrefix) { this.localizedName = SharedData.getString(resourceNamePrefix + "ActionName"); } @Override public String getLocalizedName() { return this.localizedName; } @Override public void setLocalizedName(String name) { throw new UnsupportedOperationException(); } } public interface Type { int ordinal(); String name(); String getLocalizedName(); void setLocalizedName(String var1); } }