major overhaul of FPGA code to support both ML509 and Bee2 at the same time
[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 import java.util.concurrent.Semaphore;
8
9 // FIXME: accept connections, but stall, during programming
10 public class Server {
11     static boolean sign = false;
12
13     static long jarFileTime = 0;
14     static long bitFileTime = 0;
15     static {
16         try {
17             jarFileTime = new File("fleet.jar").lastModified();
18             bitFileTime = new File("./main.bit").lastModified();
19         } catch (Exception e) { throw new RuntimeException(e); }
20     }
21
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[] {
26                 "./misc/program.sh",
27                 "./build/ml509.small/main.bit"
28             });
29         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
30         String str = null;
31         while((str = br.readLine()) != null) {
32             System.err.println("  " + str);
33         }
34         int ret = proc.waitFor();
35         if (ret != 0) {
36             System.err.println("programming error: " + ret);
37             return;
38         }
39         System.err.println("done programming.");
40         new Thread() {
41             public void run() {
42                 try {
43                     while(true) {
44                         Thread.sleep(500);
45                         if (jarFileTime != new File("fleet.jar").lastModified()) {
46                             System.err.println("jarfile modified; exiting...");
47                             System.exit(0);
48                         }
49                         if (bitFileTime != new File("./main.bit").lastModified()) {
50                             System.err.println("bitfile modified; exiting...");
51                             System.exit(0);
52                         }
53                     }
54                 } catch (Exception e) { throw new RuntimeException(e); }
55             }
56         }.start();
57         ss = new ServerSocket(3133);
58         while(true) {
59             try {
60                 Socket s = ss.accept();
61                 System.out.println("accept!");
62                 new Handler(s).start();
63             } catch (Exception e) {
64                 e.printStackTrace();
65             }
66         }
67     }
68
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; }
75         public void run() {
76             System.err.println("waiting for Server.class lock...");
77             synchronized(Server.class) {
78                 System.err.println("   (got it)");
79                 try {
80                     if (new File("/dev/cu.usbserial-FTCBWI2P").exists())
81                         this.sp = new RXTXPort("/dev/cu.usbserial-FTCBWI2P");
82                     else
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);
90                     try {
91                         _run();
92                     } finally {
93                         sp.close();
94                     }
95                 } catch (Exception e) { throw new RuntimeException(e); }
96             }
97         }
98         public void _run() {
99             try {
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());
104
105                 sp.sendBreak(100);
106
107                 // read login string
108                 byte[] buf = new byte[1024];
109                 StringBuffer sb = new StringBuffer();
110                 while(true) {
111                     int i = is.read();
112                     if (i==-1) return;
113                     if (((char)i)=='\n') break;
114                     sb.append((char)i);
115                 }
116                 System.err.println("login string: " + sb.toString());
117                 if (!sb.toString().startsWith(pass_string)) return;
118
119                 System.err.println("sending instructions...");
120                 new Thread() {
121                     public void run() {
122                         try {
123                             while(true) {
124                                 int r = is.read();
125                                 if (r == -1) break;
126                                 System.err.println("write: 0x"+Integer.toString(r & 0xff, 16));
127                                 synchronized(fos) {
128                                     fos.write(r);
129                                     if (is.available()==0) fos.flush();
130                                 }
131                             }
132                             synchronized(fos) {
133                                 fos.flush();
134                             }
135                         } catch (Exception e) { throw new RuntimeException(e);
136                         } finally {
137                             System.err.println("closing...");
138                             closed = true;
139                             try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
140                         }
141                     }
142                 }.start();
143
144                 System.err.println("reading back...");
145                 while(true) {
146                     int val = 0;
147                     for(int i=0; i<6; i++) {
148                         int k = 0;
149                         while (fis.available()==0) {
150                             if (closed) return;
151                             Thread.sleep(10);
152                         }
153                         val = fis.read();
154                         if (val==-1) break;
155                         if ((val & (3<<6)) == 0) {
156                             synchronized(fos) {
157                                 fos.write( (1<<6) | 1);
158                             }
159                         }
160                         System.err.println("read: 0x"+Integer.toString(val & 0xff, 16));
161                         os.write((byte)val);
162                     }
163                     if (val==-1) break;
164                 }
165                 os.flush();
166                 System.err.println("done.");
167
168             } catch (Exception e) { throw new RuntimeException(e);
169             } finally {
170                 try {
171                     System.err.println("closing...");
172                 } catch (Throwable t) { t.printStackTrace(); }
173             }
174         }
175     }
176
177 }