PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/collect-view/src/org/contikios/contiki/collect/MoteFinder.java

https://gitlab.com/rdeterre/contiki
Java | 226 lines | 152 code | 21 blank | 53 comment | 33 complexity | be33af67e0112ddb9c0ee5ea6bf71e14 MD5 | raw file
  1. /*
  2. * Copyright (c) 2008, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. *
  30. * -----------------------------------------------------------------
  31. *
  32. * Motelist
  33. *
  34. * Authors : Joakim Eriksson, Niclas Finne
  35. * Created : 4 jul 2008
  36. * Updated : $Date: 2010/11/03 14:53:05 $
  37. * $Revision: 1.1 $
  38. */
  39. package org.contikios.contiki.collect;
  40. import java.awt.Component;
  41. import java.io.BufferedReader;
  42. import java.io.IOException;
  43. import java.io.InputStreamReader;
  44. import java.util.ArrayList;
  45. import java.util.regex.Matcher;
  46. import java.util.regex.Pattern;
  47. import javax.swing.JOptionPane;
  48. /**
  49. *
  50. */
  51. public class MoteFinder {
  52. public static final String MOTELIST_WINDOWS = "./tools/motelist-windows.exe";
  53. public static final String MOTELIST_LINUX = "./tools/motelist-linux";
  54. public static final String MOTELIST_MACOS = "./tools/motelist-macos";
  55. private final Pattern motePattern;
  56. private final boolean isWindows;
  57. private final boolean isMacos;
  58. private Process moteListProcess;
  59. // private boolean hasVerifiedProcess;
  60. private ArrayList<String> comList = new ArrayList<String>();
  61. private ArrayList<String> moteList = new ArrayList<String>();
  62. public MoteFinder() {
  63. String osName = System.getProperty("os.name", "").toLowerCase();
  64. isWindows = osName.startsWith("win");
  65. isMacos = osName.startsWith("mac");
  66. motePattern = Pattern.compile("\\s(COM|/dev/[a-zA-Z]+|/dev/tty.usbserial-)(\\d+|[A-Z0-9]+)\\s");
  67. }
  68. public String[] getMotes() throws IOException {
  69. searchForMotes();
  70. return getMoteList();
  71. }
  72. public String[] getComPorts() throws IOException {
  73. searchForMotes();
  74. return getComList();
  75. }
  76. private void searchForMotes() throws IOException {
  77. comList.clear();
  78. moteList.clear();
  79. // hasVerifiedProcess = false;
  80. /* Connect to COM using external serialdump application */
  81. String fullCommand;
  82. if (isWindows) {
  83. fullCommand = MOTELIST_WINDOWS;
  84. } else if (isMacos) {
  85. fullCommand = MOTELIST_MACOS;
  86. } else {
  87. fullCommand = MOTELIST_LINUX;
  88. }
  89. try {
  90. String[] cmd = new String[] { fullCommand };
  91. moteListProcess = Runtime.getRuntime().exec(cmd);
  92. final BufferedReader input = new BufferedReader(new InputStreamReader(moteListProcess.getInputStream()));
  93. final BufferedReader err = new BufferedReader(new InputStreamReader(moteListProcess.getErrorStream()));
  94. /* Start thread listening on stdout */
  95. Thread readInput = new Thread(new Runnable() {
  96. public void run() {
  97. String line;
  98. try {
  99. while ((line = input.readLine()) != null) {
  100. parseIncomingLine(line);
  101. }
  102. input.close();
  103. } catch (IOException e) {
  104. System.err.println("Exception when reading from motelist");
  105. e.printStackTrace();
  106. }
  107. }
  108. }, "read motelist thread");
  109. /* Start thread listening on stderr */
  110. Thread readError = new Thread(new Runnable() {
  111. public void run() {
  112. String line;
  113. try {
  114. while ((line = err.readLine()) != null) {
  115. System.err.println("Motelist error stream> " + line);
  116. }
  117. err.close();
  118. } catch (IOException e) {
  119. System.err.println("Exception when reading from motelist error stream: " + e);
  120. }
  121. }
  122. }, "read motelist error stream thread");
  123. readInput.start();
  124. readError.start();
  125. // Wait for the motelist program to finish executing
  126. readInput.join();
  127. } catch (Exception e) {
  128. throw (IOException) new IOException("Failed to execute '" + fullCommand + "'").initCause(e);
  129. }
  130. }
  131. private String[] getComList() {
  132. return comList.toArray(new String[comList.size()]);
  133. }
  134. private String[] getMoteList() {
  135. return moteList.toArray(new String[moteList.size()]);
  136. }
  137. public void close() {
  138. if (moteListProcess != null) {
  139. moteListProcess.destroy();
  140. moteListProcess = null;
  141. }
  142. }
  143. protected void parseIncomingLine(String line) {
  144. if (line.contains("No devices found") || line.startsWith("Reference")) {
  145. // No Sky connected or title before connected motes
  146. // hasVerifiedProcess = true;
  147. } else if (line.startsWith("-------")) {
  148. // Separator
  149. } else {
  150. Matcher matcher = motePattern.matcher(line);
  151. if (matcher.find()) {
  152. String dev = matcher.group(1);
  153. String no = matcher.group(2);
  154. String comPort = dev + no;
  155. String moteID = comPort;
  156. if (isWindows) {
  157. // Special handling of mote id under Windows
  158. int moteNumber = Integer.parseInt(no);
  159. moteID = Integer.toString(moteNumber - 1);
  160. }
  161. comList.add(comPort);
  162. moteList.add(moteID);
  163. } else {
  164. System.err.println("Motelist> " + line);
  165. }
  166. }
  167. }
  168. public static String selectComPort(Component parent) {
  169. MoteFinder finder = new MoteFinder();
  170. try {
  171. String[] motes = finder.getComPorts();
  172. if (motes == null || motes.length == 0) {
  173. JOptionPane.showMessageDialog(parent, "Could not find any connected motes.", "No mote found", JOptionPane.ERROR_MESSAGE);
  174. return null;
  175. } else if (motes.length == 1) {
  176. // Only one node found
  177. return motes[0];
  178. } else {
  179. // Several motes found
  180. return (String) JOptionPane.showInputDialog(
  181. parent, "Found multiple connected motes. Please select serial port:",
  182. "Select serial port", JOptionPane.QUESTION_MESSAGE, null, motes, motes[0]);
  183. }
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. JOptionPane.showMessageDialog(parent, "Failed to search for connected motes:\n" + e, "Error", JOptionPane.ERROR_MESSAGE);
  187. return null;
  188. } finally {
  189. finder.close();
  190. }
  191. }
  192. public static void main(String[] args) throws IOException {
  193. MoteFinder finder = new MoteFinder();
  194. String[] comPorts = args.length > 0 && "-v".equals(args[0]) ?
  195. finder.getMotes() : finder.getComPorts();
  196. finder.close();
  197. if (comPorts == null || comPorts.length == 0) {
  198. System.out.println("No motes connected");
  199. } else {
  200. for(String port: comPorts) {
  201. System.out.println("Found Sky at " + port);
  202. }
  203. }
  204. }
  205. }