1 package edu.berkeley.fleet.fpga;
10 public static ServerSocket ss;
11 public static void main(String[] args) throws Exception {
12 System.err.println("programming...");
13 Process proc = Runtime.getRuntime().exec(new String[] {
14 "/afs/megacz.com/work/ml410/program.sh",
15 "/afs/megacz.com/work/ml410/main.bit"
17 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
19 while((str = br.readLine()) != null) {
20 System.err.println(" " + str);
22 int ret = proc.waitFor();
24 System.err.println("programming error: " + ret);
27 System.err.println("done programming.");
28 ss = new ServerSocket(3133);
31 Socket s = ss.accept();
32 new Handler(s).start();
33 } catch (Exception e) {
39 public static String pass_string = "password=security_is_for_wimps ";
41 static class Handler extends Thread {
42 private Socket socket;
43 boolean closed = false;
44 private SerialPort sp;
45 public Handler(Socket s) { this.socket = s; this.sp = sp; }
47 synchronized(Server.class) {
49 this.sp = new RXTXPort("/dev/ttyS0");
56 } catch (Exception e) { throw new RuntimeException(e); }
61 final InputStream is = socket.getInputStream();
62 byte[] buf = new byte[1024];
63 StringBuffer sb = new StringBuffer();
67 if (((char)i)=='\n') break;
70 System.err.println("login string: " + sb.toString());
71 if (!sb.toString().startsWith(pass_string)) return;
73 //try { Thread.sleep(2000); } catch(Exception e) { }
74 final OutputStream os = socket.getOutputStream();
75 System.err.println("sending instructions...");
77 sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
78 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
79 final OutputStream fos = sp.getOutputStream();
80 final InputStream fis = sp.getInputStream();
85 byte[] buf = new byte[1024];
87 int numread = is.read(buf, 0, buf.length);
88 if (numread == -1) break;
89 fos.write(buf, 0, numread);
92 } catch (Exception e) { throw new RuntimeException(e);
94 System.err.println("closing...");
96 try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
101 System.err.println("reading back...");
105 for(int i=0; i<6; i++) {
107 while(!closed && fis.available()==0) {
111 System.err.println("sleep");
119 System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
122 result |= ((long)val) << (i * 8);
125 System.err.print(result);
126 System.err.print(" 0x");
127 System.err.print(Long.toString(result, 16));
128 System.err.println();
130 System.err.println("done.");
132 } catch (Exception e) { throw new RuntimeException(e);
135 System.err.println("closing...");
138 if (fis != null) fis.close();
140 } catch (Throwable t) { t.printStackTrace(); }