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("build/fpga/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[] {
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("build/fpga/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 this.sp = new RXTXPort("/dev/ttyS0");
81 sp.setInputBufferSize(0);
82 sp.setOutputBufferSize(0);
83 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
84 //sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
85 sp.setFlowControlMode(0);
91 } catch (Exception e) { throw new RuntimeException(e); }
96 final InputStream is = new BufferedInputStream(socket.getInputStream());
97 byte[] buf = new byte[1024];
98 StringBuffer sb = new StringBuffer();
102 if (((char)i)=='\n') break;
105 System.err.println("login string: " + sb.toString());
106 if (!sb.toString().startsWith(pass_string)) return;
107 final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
109 final OutputStream fos = sp.getOutputStream();
110 final InputStream fis = new BufferedInputStream(sp.getInputStream());
112 final Semaphore sem = new Semaphore(15);
114 System.err.println("sending instructions...");
126 } catch (Exception e) { throw new RuntimeException(e);
128 System.err.println("closing...");
130 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
135 System.err.println("reading back...");
140 for(int i=0; i<6; i++) {
142 while(!closed && fis.available()==0) {
146 System.err.println("sleep");
149 if (k==10) os.flush();
152 if (closed) { os.flush(); return; }
155 if ((val & (1<<6)) == 0) {
157 fos.write( (1<<6) | 1);
160 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
162 result |= ((long)val) << (i * 8);
165 System.err.print(result);
166 System.err.print(" 0x");
167 System.err.print(Long.toString(result, 16));
168 System.err.println();
171 System.err.println("done.");
173 } catch (Exception e) { throw new RuntimeException(e);
176 System.err.println("closing...");
179 if (fis != null) fis.close();
181 } catch (Throwable t) { t.printStackTrace(); }