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);
84 sp.setFlowControlMode(0);
90 } catch (Exception e) { throw new RuntimeException(e); }
95 final InputStream is = new BufferedInputStream(socket.getInputStream());
96 byte[] buf = new byte[1024];
97 StringBuffer sb = new StringBuffer();
101 if (((char)i)=='\n') break;
104 System.err.println("login string: " + sb.toString());
105 if (!sb.toString().startsWith(pass_string)) return;
106 final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
108 final OutputStream fos = sp.getOutputStream();
109 final InputStream fis = new BufferedInputStream(sp.getInputStream());
111 System.err.println("sending instructions...");
121 } catch (Exception e) { throw new RuntimeException(e);
123 System.err.println("closing...");
125 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
130 System.err.println("reading back...");
135 for(int i=0; i<6; i++) {
137 while(!closed && fis.available()==0) {
141 System.err.println("sleep");
144 if (k==10) os.flush();
147 if (closed) { os.flush(); return; }
150 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
152 result |= ((long)val) << (i * 8);
155 System.err.print(result);
156 System.err.print(" 0x");
157 System.err.print(Long.toString(result, 16));
158 System.err.println();
161 System.err.println("done.");
163 } catch (Exception e) { throw new RuntimeException(e);
166 System.err.println("closing...");
169 if (fis != null) fis.close();
171 } catch (Throwable t) { t.printStackTrace(); }