switch from using RS-232 BREAKs and no flow control to new command mode reset and...
[fleet.git] / src / edu / berkeley / fleet / fpga / Server.java
1 package edu.berkeley.fleet.fpga;
2
3 import gnu.io.*;
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7
8 // FIXME: accept connections, but stall, during programming
9 public class Server {
10     static boolean sign = false;
11
12     static long jarFileTime = 0;
13     static long bitFileTime = 0;
14     static {
15         try {
16             jarFileTime = new File("fleet.jar").lastModified();
17             bitFileTime = new File("build/fpga/main.bit").lastModified();
18         } catch (Exception e) { throw new RuntimeException(e); }
19     }
20
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[] {
25                 "misc/program.sh",
26                 "build/fpga/main.bit"
27             });
28         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
29         String str = null;
30         while((str = br.readLine()) != null) {
31             System.err.println("  " + str);
32         }
33         int ret = proc.waitFor();
34         if (ret != 0) {
35             System.err.println("programming error: " + ret);
36             return;
37         }
38         System.err.println("done programming.");
39         new Thread() {
40             public void run() {
41                 try {
42                     while(true) {
43                         Thread.sleep(500);
44                         if (jarFileTime != new File("fleet.jar").lastModified()) {
45                             System.err.println("jarfile modified; exiting...");
46                             System.exit(0);
47                         }
48                         if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
49                             System.err.println("bitfile modified; exiting...");
50                             System.exit(0);
51                         }
52                     }
53                 } catch (Exception e) { throw new RuntimeException(e); }
54             }
55         }.start();
56         ss = new ServerSocket(3133);
57         while(true) {
58             try {
59                 Socket s = ss.accept();
60                 System.out.println("accept!");
61                 new Handler(s).start();
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66     }
67
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; }
74         public void run() {
75             System.err.println("waiting for Server.class lock...");
76             synchronized(Server.class) {
77                 System.err.println("   (got it)");
78                 try {
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);
85                     try {
86                         _run();
87                     } finally {
88                         sp.close();
89                     }
90                 } catch (Exception e) { throw new RuntimeException(e); }
91             }
92         }
93         public void _run() {
94             try {
95                 final InputStream is = new BufferedInputStream(socket.getInputStream());
96                 byte[] buf = new byte[1024];
97                 StringBuffer sb = new StringBuffer();
98                 while(true) {
99                     int i = is.read();
100                     if (i==-1) return;
101                     if (((char)i)=='\n') break;
102                     sb.append((char)i);
103                 }
104                 System.err.println("login string: " + sb.toString());
105                 if (!sb.toString().startsWith(pass_string)) return;
106                 final OutputStream os = new BufferedOutputStream(socket.getOutputStream());
107
108                 final OutputStream fos = sp.getOutputStream();
109                 final InputStream fis = new BufferedInputStream(sp.getInputStream());
110
111                 System.err.println("sending instructions...");
112                 new Thread() {
113                     public void run() {
114                         try {
115                             while(true) {
116                                 int r = is.read();
117                                 if (r == -1) break;
118                                 fos.write(r);
119                             }
120                             fos.flush();
121                         } catch (Exception e) { throw new RuntimeException(e);
122                         } finally {
123                             System.err.println("closing...");
124                             closed = true;
125                             try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
126                         }
127                     }
128                 }.start();
129
130                 System.err.println("reading back...");
131                 while(true) {
132                     long result = 0;
133                     int val = 0;
134                     boolean hit=false;
135                     for(int i=0; i<6; i++) {
136                         int k = 0;
137                         while(!closed && fis.available()==0) {
138                             if (closed) return;
139                             k++;
140                             if (k >= 100) {
141                                 System.err.println("sleep");
142                                 k = 0;
143                             }
144                             if (k==10) os.flush();
145                             Thread.sleep(10);
146                         }
147                         if (closed) { os.flush(); return; }
148                         val = fis.read();
149                         if (val==-1) break;
150                         System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
151                         os.write((byte)val);
152                         result |= ((long)val) << (i * 8);
153                     }
154                     if (val==-1) break;
155                     System.err.print(result);
156                     System.err.print(" 0x");
157                     System.err.print(Long.toString(result, 16));
158                     System.err.println();
159                 }
160                 os.flush();
161                 System.err.println("done.");
162
163             } catch (Exception e) { throw new RuntimeException(e);
164             } finally {
165                 try {
166                     System.err.println("closing...");
167                     /*
168                     fis.close();
169                     if (fis != null) fis.close();
170                     */
171                 } catch (Throwable t) { t.printStackTrace(); }
172             }
173         }
174     }
175
176 }