1 package edu.berkeley.fleet.fpga;
7 import java.util.concurrent.Semaphore;
9 // FIXME: accept connections, but stall, during programming
11 static boolean sign = false;
13 static long jarFileTime = 0;
14 static long bitFileTime = 0;
17 jarFileTime = new File("fleet.jar").lastModified();
18 bitFileTime = new File("./main.bit").lastModified();
19 } catch (Exception e) { throw new RuntimeException(e); }
22 public static ServerSocket ss;
23 public static void main(String[] args) throws Exception {
24 System.err.println("programming...");
25 Process proc = Runtime.getRuntime().exec(new String[] {
27 "./build/ml509.small/main.bit"
29 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
31 while((str = br.readLine()) != null) {
32 System.err.println(" " + str);
34 int ret = proc.waitFor();
36 System.err.println("programming error: " + ret);
39 System.err.println("done programming.");
45 if (jarFileTime != new File("fleet.jar").lastModified()) {
46 System.err.println("jarfile modified; exiting...");
49 if (bitFileTime != new File("./main.bit").lastModified()) {
50 System.err.println("bitfile modified; exiting...");
54 } catch (Exception e) { throw new RuntimeException(e); }
57 ss = new ServerSocket(3133);
60 Socket s = ss.accept();
61 System.out.println("accept!");
62 new Handler(s).start();
63 } catch (Exception e) {
69 public static String pass_string = "password=security_is_for_wimps ";
70 static class Handler extends Thread {
71 private Socket socket;
72 boolean closed = false;
73 private SerialPort sp;
74 public Handler(Socket s) { this.socket = s; this.sp = sp; }
76 System.err.println("waiting for Server.class lock...");
77 synchronized(Server.class) {
78 System.err.println(" (got it)");
80 if (new File("/dev/cu.usbserial-FTCBWI2P").exists())
81 this.sp = new RXTXPort("/dev/cu.usbserial-FTCBWI2P");
83 this.sp = new RXTXPort("/dev/ttyS0");
84 sp.setInputBufferSize(0);
85 sp.setOutputBufferSize(0);
86 //sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
87 sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
88 //sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
89 sp.setFlowControlMode(0);
95 } catch (Exception e) { throw new RuntimeException(e); }
100 final InputStream is = new BufferedInputStream(socket.getInputStream());
101 final OutputStream os = socket.getOutputStream();
102 final OutputStream fos = sp.getOutputStream();
103 final InputStream fis = new BufferedInputStream(sp.getInputStream());
108 byte[] buf = new byte[1024];
109 StringBuffer sb = new StringBuffer();
113 if (((char)i)=='\n') break;
116 System.err.println("login string: " + sb.toString());
117 if (!sb.toString().startsWith(pass_string)) return;
119 System.err.println("sending instructions...");
126 System.err.println("write: 0x"+Integer.toString(r & 0xff, 16));
129 if (is.available()==0) fos.flush();
135 } catch (Exception e) { throw new RuntimeException(e);
137 System.err.println("closing...");
139 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
144 System.err.println("reading back...");
147 for(int i=0; i<6; i++) {
149 while (fis.available()==0) {
155 if ((val & (3<<6)) == 0) {
157 fos.write( (1<<6) | 1);
160 System.err.println("read: 0x"+Integer.toString(val & 0xff, 16));
166 System.err.println("done.");
168 } catch (Exception e) { throw new RuntimeException(e);
171 System.err.println("closing...");
172 } catch (Throwable t) { t.printStackTrace(); }