1 package edu.berkeley.fleet.fpga;
8 // FIXME: accept connections, but stall, during programming
10 static boolean sign = false;
12 static long jarFileTime = 0;
13 static long bitFileTime = 0;
16 jarFileTime = new File("fleet.jar").lastModified();
17 bitFileTime = new File("build/fpga/main.bit").lastModified();
18 } catch (Exception e) { throw new RuntimeException(e); }
21 public static ServerSocket ss;
22 public static void main(String[] args) throws Exception {
23 System.err.println("programming...");
24 Process proc = Runtime.getRuntime().exec(new String[] {
28 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
30 while((str = br.readLine()) != null) {
31 System.err.println(" " + str);
33 int ret = proc.waitFor();
35 System.err.println("programming error: " + ret);
38 System.err.println("done programming.");
44 if (jarFileTime != new File("fleet.jar").lastModified()) {
45 System.err.println("jarfile modified; exiting...");
48 if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
49 System.err.println("bitfile modified; exiting...");
53 } catch (Exception e) { throw new RuntimeException(e); }
56 ss = new ServerSocket(3133);
59 Socket s = ss.accept();
60 System.out.println("accept!");
61 new Handler(s).start();
62 } catch (Exception e) {
68 public static String pass_string = "password=security_is_for_wimps ";
69 static class Handler extends Thread {
70 private Socket socket;
71 boolean closed = false;
72 private SerialPort sp;
73 public Handler(Socket s) { this.socket = s; this.sp = sp; }
75 System.err.println("waiting for Server.class lock...");
76 synchronized(Server.class) {
77 System.err.println(" (got it)");
79 this.sp = new RXTXPort("/dev/ttyS0");
80 sp.setInputBufferSize(0);
81 sp.setOutputBufferSize(0);
82 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
83 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
89 } catch (Exception e) { throw new RuntimeException(e); }
94 final InputStream is = new BufferedInputStream(socket.getInputStream());
95 byte[] buf = new byte[1024];
96 StringBuffer sb = new StringBuffer();
100 if (((char)i)=='\n') break;
103 System.err.println("login string: " + sb.toString());
104 if (!sb.toString().startsWith(pass_string)) return;
105 final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
106 System.err.println("sending breaks...");
109 final OutputStream fos = sp.getOutputStream();
110 final InputStream fis = new BufferedInputStream(sp.getInputStream());
113 System.err.println("sending instructions...");
128 } catch (Exception e) { throw new RuntimeException(e);
130 System.err.println("closing...");
132 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
138 System.err.println("reading back...");
143 for(int i=0; i<6; i++) {
145 while(!closed && fis.available()==0) {
149 System.err.println("sleep");
152 if (k==10) os.flush();
155 if (closed) { os.flush(); return; }
158 System.err.println("leader byte: 0x" + Integer.toString(val, 16) + " '"+((char)val)+"'");
162 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
164 result |= ((long)val) << (i * 8);
167 System.err.print(result);
168 System.err.print(" 0x");
169 System.err.print(Long.toString(result, 16));
170 System.err.println();
173 System.err.println("done.");
175 } catch (Exception e) { throw new RuntimeException(e);
178 System.err.println("closing...");
181 if (fis != null) fis.close();
183 } catch (Throwable t) { t.printStackTrace(); }