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);
86 } catch (Exception e) { throw new RuntimeException(e); }
91 final InputStream is = socket.getInputStream();
92 byte[] buf = new byte[1024];
93 StringBuffer sb = new StringBuffer();
97 if (((char)i)=='\n') break;
100 System.err.println("login string: " + sb.toString());
101 if (!sb.toString().startsWith(pass_string)) return;
103 //try { Thread.sleep(2000); } catch(Exception e) { }
104 final OutputStream os = socket.getOutputStream();
105 System.err.println("sending instructions...");
107 sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
108 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
109 final OutputStream fos = sp.getOutputStream();
110 final InputStream fis = sp.getInputStream();
111 while(fis.available() > 0)
117 byte[] buf = new byte[1024];
119 int numread = is.read(buf, 0, buf.length);
120 if (numread == -1) break;
121 fos.write(buf, 0, numread);
124 } catch (Exception e) { throw new RuntimeException(e);
126 System.err.println("closing...");
128 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
135 for(int j=0; j<3; j++)
139 System.out.println("leader byte: 0x" + Integer.toString(i, 16) + " '"+((char)i)+"'");
140 if (((char)last) == 'o' && ((char)i) == 'k') break;
145 System.err.println("reading back...");
149 for(int i=0; i<6; i++) {
151 while(!closed && fis.available()==0) {
155 System.err.println("sleep");
163 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
166 result |= ((long)val) << (i * 8);
169 System.err.print(result);
170 System.err.print(" 0x");
171 System.err.print(Long.toString(result, 16));
172 System.err.println();
174 System.err.println("done.");
176 } catch (Exception e) { throw new RuntimeException(e);
179 System.err.println("closing...");
182 if (fis != null) fis.close();
184 } catch (Throwable t) { t.printStackTrace(); }