📄 contikimotetypedialog.java
字号:
public void mouseClicked(MouseEvent e) { if (e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e)) { popup.show(taskOutput, e.getX(), e.getY()); } } }); progressPanel.add(BorderLayout.CENTER, new JScrollPane(taskOutput)); progressPanel.add(BorderLayout.NORTH, progressBar); progressPanel.add(BorderLayout.SOUTH, button); progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); progressPanel.setVisible(true); progressDialog.getContentPane().add(progressPanel); progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); progressDialog.setSize(500, 300); progressDialog.getRootPane().setDefaultButton(button); progressDialog.setVisible(true); // Create temp output directory if not already exists if (!ContikiMoteType.tempOutputDirectory.exists()) { ContikiMoteType.tempOutputDirectory.mkdir(); } // Parse selected sensors Vector<String> sensors = new Vector<String>(); for (Component checkBox : sensorPanel.getComponents()) { if (((JCheckBox) checkBox).isSelected()) { sensors.add(((JCheckBox) checkBox).getText()); } } // Parse selected core interfaces Vector<String> coreInterfaces = new Vector<String>(); for (Component checkBox : coreInterfacePanel.getComponents()) { if (((JCheckBox) checkBox).isSelected()) { coreInterfaces.add(((JCheckBox) checkBox).getText()); } } // Parse selected user processes Vector<String> userProcesses = new Vector<String>(); for (Component checkBox : processPanel.getComponents()) { if (((JCheckBox) checkBox).isSelected()) { ContikiProcess process = (ContikiProcess) ((JCheckBox) checkBox).getClientProperty("process"); userProcesses.add(process.getProcessName()); } } // Generate Contiki main file try { generateSourceFile(textID.getText(), sensors, coreInterfaces, userProcesses); } catch (Exception e) { libraryCreatedOK = false; progressBar.setBackground(Color.ORANGE); progressBar.setString(e.getMessage()); progressBar.setIndeterminate(false); progressBar.setValue(0); createButton.setEnabled(libraryCreatedOK); return; } // Test compile shared library progressBar.setString("..compiling.."); final File contikiDir = new File(textContikiDir.getText()); final String identifier = textID.getText(); File libFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.librarySuffix); File mapFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.mapSuffix); File depFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.dependSuffix); if (libFile.exists()) { libFile.delete(); } if (depFile.exists()) { depFile.delete(); } if (mapFile.exists()) { mapFile.delete(); } // Add all project directories compilationFiles = (Vector<File>) myGUI.getProjectDirs().clone(); if (moteTypeProjectDirs == null || moteTypeProjectDirs.isEmpty()) { compilationFiles.add(new File(textCoreDir.getText(), "testapps")); } else { compilationFiles.addAll(moteTypeProjectDirs); } // Add source files from project configs String[] projectSourceFiles = newMoteTypeConfig.getStringArrayValue(ContikiMoteType.class, "C_SOURCES"); for (String projectSourceFile : projectSourceFiles) { if (!projectSourceFile.equals("")) { File file = new File(projectSourceFile); if (file.getParent() != null) { // Find which project directory added this file File projectDir = newMoteTypeConfig.getUserProjectDefining( ContikiMoteType.class, "C_SOURCES", projectSourceFile); if (projectDir != null) { // We found a project directory; add it to path compilationFiles.add(new File(projectDir.getPath(), file.getParent())); } } compilationFiles.add(new File(file.getName())); } } // Add selected process source files for (Component checkBox : processPanel.getComponents()) { if (((JCheckBox) checkBox).isSelected()) { ContikiProcess process = (ContikiProcess) ((JCheckBox) checkBox).getClientProperty("process"); if (process.getSourceFile() != null) { compilationFiles.add(process.getSourceFile().getParentFile()); compilationFiles.add(process.getSourceFile()); } } } compilationSucceded = ContikiMoteTypeDialog.compileLibrary(identifier, contikiDir, compilationFiles, symbolsCheckBox.isSelected(), (ContikiMoteType.CommunicationStack) commStackComboBox.getSelectedItem(), taskOutput.getInputStream(MessageList.NORMAL), taskOutput.getInputStream(MessageList.ERROR)); if (!compilationSucceded) { if (libFile.exists()) { libFile.delete(); } if (depFile.exists()) { depFile.delete(); } if (mapFile.exists()) { mapFile.delete(); } libraryCreatedOK = false; } else { libraryCreatedOK = true; if (!libFile.exists() || !depFile.exists()) { /* TODO Check if map file is really needed */ logger.fatal("Not all needed files could be located"); libraryCreatedOK = false; } } if (libraryCreatedOK) { button.setText("Compilation succeeded!"); progressBar.setBackground(Color.GREEN); progressBar.setString("compilation succeded"); button.grabFocus(); myDialog.getRootPane().setDefaultButton(createButton); } else { button.setText("Compilation failed!"); progressBar.setBackground(Color.ORANGE); progressBar.setString("compilation failed"); myDialog.getRootPane().setDefaultButton(testButton); } progressBar.setIndeterminate(false); progressBar.setValue(0); createButton.setEnabled(libraryCreatedOK); } /** * Generates new source file by reading default source template and replacing * fields with sensors, core interfaces and processes. Also includes default * processes from GUI external configuration. * * @param id * Mote type ID (decides name of new source file) * @param sensors * Names of sensors * @param coreInterfaces * Names of core interfaces * @param userProcesses * Names of user processes * @return New filename * @throws Exception * If any error occurs */ public static String generateSourceFile(String id, Vector<String> sensors, Vector<String> coreInterfaces, Vector<String> userProcesses) throws Exception { // SENSORS String sensorString = ""; String externSensorDefs = ""; for (String sensor : sensors) { if (!sensorString.equals("")) { sensorString += ", "; } sensorString += "&" + sensor; externSensorDefs += "extern const struct sensors_sensor " + sensor + ";\n"; } if (!sensorString.equals("")) { sensorString = "SENSORS(" + sensorString + ");"; } else { sensorString = "SENSORS(NULL);"; } // CORE INTERFACES String interfaceString = ""; String externInterfaceDefs = ""; for (String coreInterface : coreInterfaces) { if (!interfaceString.equals("")) { interfaceString += ", "; } interfaceString += "&" + coreInterface; externInterfaceDefs += "SIM_INTERFACE_NAME(" + coreInterface + ");\n"; } if (!interfaceString.equals("")) { interfaceString = "SIM_INTERFACES(" + interfaceString + ");"; } else { interfaceString = "SIM_INTERFACES(NULL);"; } // PROCESSES (including any default processes) String userProcessString = ""; String externProcessDefs = ""; for (String process : userProcesses) { if (!userProcessString.equals("")) { userProcessString += ", "; } userProcessString += "&" + process; externProcessDefs += "PROCESS_NAME(" + process + ");\n"; } String defaultProcessString = ""; String defaultProcesses[] = GUI.getExternalToolsSetting( "CONTIKI_STANDARD_PROCESSES").split(";"); for (String process : defaultProcesses) { if (!defaultProcessString.equals("")) { defaultProcessString += ", "; } defaultProcessString += "&" + process; } if (userProcessString.equals("")) { logger .warn("No application processes specified! Sure you don't want any?"); } String processString; if (!defaultProcessString.equals("")) { processString = "PROCINIT(" + defaultProcessString + ");"; } else { processString = "PROCINIT(NULL);"; } if (!userProcessString.equals("")) { processString += "\nAUTOSTART_PROCESSES(" + userProcessString + ");"; } else { processString += "\nAUTOSTART_PROCESSES(NULL);"; } // CHECK JNI CLASS AVAILABILITY String libString = CoreComm.getAvailableClassName(); if (libString == null) { logger.fatal("No more libraries can be loaded!"); throw new MoteTypeCreationException("Maximum number of mote types already exist"); } // GENERATE NEW FILE BufferedWriter destFile = null; BufferedReader sourceFile = null; String destFilename = null; try { Reader reader; String mainTemplate = GUI .getExternalToolsSetting("CONTIKI_MAIN_TEMPLATE_FILENAME"); if ((new File(mainTemplate)).exists()) { reader = new FileReader(mainTemplate); } else { InputStream input = ContikiMoteTypeDialog.class .getResourceAsStream('/' + mainTemplate); if (input == null) { throw new FileNotFoundException(mainTemplate + " not found"); } reader = new InputStreamReader(input); } sourceFile = new BufferedReader(reader); destFilename = ContikiMoteType.tempOutputDirectory.getPath() + File.separatorChar + id + ".c"; destFile = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFilename))); // Replace special fields in template String line; while ((line = sourceFile.readLine()) != null) { line = line .replaceFirst("\\[PROCESS_DEFINITIONS\\]", externProcessDefs); line = line.replaceFirst("\\[PROCESS_ARRAY\\]", processString); line = line.replaceFirst("\\[SENSOR_DEFINITIONS\\]", externSensorDefs); line = line.replaceFirst("\\[SENSOR_ARRAY\\]", sensorString); line = line.replaceFirst("\\[INTERFACE_DEFINITIONS\\]", externInterfaceDefs); line = line.replaceFirst("\\[INTERFACE_ARRAY\\]", interfaceString); line = line.replaceFirst("\\[CLASS_NAME\\]", libString); destFile.write(line + "\n"); } destFile.close(); sourceFile.close(); } catch (Exception e) { try { if (destFile != null) { destFile.close(); } if (sourceFile != null) { sourceFile.close(); } } catch (Exception e2) { } // Forward exception throw e; } return destFilename; } /** * Compiles a mote type shared library using the standard Contiki makefile. * * @param identifier * Mote type identifier * @param contikiDir * Contiki base directory * @param sourceFiles * Source files and directories to include in compilation * @param includeSymbols * Generate and include symbols in library file * @param outputStream * Output stream from compilation (optional) * @param errorStream * Error stream from compilation (optional) * @return True if compilation succeded, false otherwise */ public static boolean compileLibrary(String identifier, File contikiDir, Vector<File> sourceFiles, boolean includeSymbols, ContikiMoteType.CommunicationStack commStack, final PrintStream outputStream, final PrintStream errorStream) { File libFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.librarySuffix); File mapFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.mapSuffix); File arFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.dependSuffix);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -