1 package edu.berkeley.fleet.fpga;
10 static long jarFileTime = 0;
11 static long bitFileTime = 0;
14 jarFileTime = new File("fleet.jar").lastModified();
15 bitFileTime = new File("build/fpga/main.bit").lastModified();
16 } catch (Exception e) { throw new RuntimeException(e); }
19 public static ServerSocket ss;
20 public static void main(String[] args) throws Exception {
21 System.err.println("programming...");
22 Process proc = Runtime.getRuntime().exec(new String[] {
26 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
28 while((str = br.readLine()) != null) {
29 System.err.println(" " + str);
31 int ret = proc.waitFor();
33 System.err.println("programming error: " + ret);
36 System.err.println("done programming.");
42 if (jarFileTime != new File("fleet.jar").lastModified()) {
43 System.err.println("jarfile modified; exiting...");
46 if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
47 System.err.println("bitfile modified; exiting...");
51 } catch (Exception e) { throw new RuntimeException(e); }
54 ss = new ServerSocket(3133);
57 Socket s = ss.accept();
58 new Handler(s).start();
59 } catch (Exception e) {
65 public static String pass_string = "password=security_is_for_wimps ";
67 static class Handler extends Thread {
68 private Socket socket;
69 boolean closed = false;
70 private SerialPort sp;
71 public Handler(Socket s) { this.socket = s; this.sp = sp; }
73 synchronized(Server.class) {
75 this.sp = new RXTXPort("/dev/ttyS0");
82 } catch (Exception e) { throw new RuntimeException(e); }
87 final InputStream is = socket.getInputStream();
88 byte[] buf = new byte[1024];
89 StringBuffer sb = new StringBuffer();
93 if (((char)i)=='\n') break;
96 System.err.println("login string: " + sb.toString());
97 if (!sb.toString().startsWith(pass_string)) return;
99 //try { Thread.sleep(2000); } catch(Exception e) { }
100 final OutputStream os = socket.getOutputStream();
101 System.err.println("sending instructions...");
103 sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
104 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
105 final OutputStream fos = sp.getOutputStream();
106 final InputStream fis = sp.getInputStream();
111 byte[] buf = new byte[1024];
113 int numread = is.read(buf, 0, buf.length);
114 if (numread == -1) break;
115 fos.write(buf, 0, numread);
118 } catch (Exception e) { throw new RuntimeException(e);
120 System.err.println("closing...");
122 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
127 System.err.println("reading back...");
131 for(int i=0; i<6; i++) {
133 while(!closed && fis.available()==0) {
137 System.err.println("sleep");
145 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
148 result |= ((long)val) << (i * 8);
151 System.err.print(result);
152 System.err.print(" 0x");
153 System.err.print(Long.toString(result, 16));
154 System.err.println();
156 System.err.println("done.");
158 } catch (Exception e) { throw new RuntimeException(e);
161 System.err.println("closing...");
164 if (fis != null) fis.close();
166 } catch (Throwable t) { t.printStackTrace(); }