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");
76 sp.setInputBufferSize(0);
77 sp.setOutputBufferSize(0);
78 sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
79 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
85 } catch (Exception e) { throw new RuntimeException(e); }
90 final InputStream is = socket.getInputStream();
91 byte[] buf = new byte[1024];
92 StringBuffer sb = new StringBuffer();
96 if (((char)i)=='\n') break;
99 System.err.println("login string: " + sb.toString());
100 if (!sb.toString().startsWith(pass_string)) return;
102 //try { Thread.sleep(500); } catch(Exception e) { }
103 final OutputStream os = socket.getOutputStream();
105 System.out.println("sending breaks");
106 for(int i=0; i<2; i++) {
110 System.out.println("done sending breaks");
112 final OutputStream fos = sp.getOutputStream();
113 final InputStream fis = sp.getInputStream();
115 System.err.println("sending instructions...");
119 byte[] buf = new byte[1024];
121 int numread = is.read(buf, 0, buf.length);
122 if (numread == -1) break;
123 fos.write(buf, 0, numread);
126 } catch (Exception e) { throw new RuntimeException(e);
128 System.err.println("closing...");
130 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
137 for(int j=0; j<3; j++)
141 System.out.println("leader byte: 0x" + Integer.toString(i, 16) + " '"+((char)i)+"'");
142 if (((char)last) == 'o' && ((char)i) == 'k') break;
147 System.err.println("reading back...");
151 for(int i=0; i<6; i++) {
153 while(!closed && fis.available()==0) {
157 System.err.println("sleep");
165 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
168 result |= ((long)val) << (i * 8);
171 System.err.print(result);
172 System.err.print(" 0x");
173 System.err.print(Long.toString(result, 16));
174 System.err.println();
176 System.err.println("done.");
178 } catch (Exception e) { throw new RuntimeException(e);
181 System.err.println("closing...");
184 if (fis != null) fis.close();
186 } catch (Throwable t) { t.printStackTrace(); }