extra reset code for fpga.Server
[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 public class Server {
9
10     static long jarFileTime = 0;
11     static long bitFileTime = 0;
12     static {
13         try {
14             jarFileTime = new File("fleet.jar").lastModified();
15             bitFileTime = new File("build/fpga/main.bit").lastModified();
16         } catch (Exception e) { throw new RuntimeException(e); }
17     }
18
19     public static ServerSocket ss;
20     public static void main(String[] args) throws Exception {
21         System.err.println("programming...");
22         Process proc = Runtime.getRuntime().exec(new String[] {
23                 "misc/program.sh",
24                 "build/fpga/main.bit"
25             });
26         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
27         String str = null;
28         while((str = br.readLine()) != null) {
29             System.err.println("  " + str);
30         }
31         int ret = proc.waitFor();
32         if (ret != 0) {
33             System.err.println("programming error: " + ret);
34             return;
35         }
36         System.err.println("done programming.");
37         new Thread() {
38             public void run() {
39                 try {
40                     while(true) {
41                         Thread.sleep(500);
42                         if (jarFileTime != new File("fleet.jar").lastModified()) {
43                             System.err.println("jarfile modified; exiting...");
44                             System.exit(0);
45                         }
46                         if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
47                             System.err.println("bitfile modified; exiting...");
48                             System.exit(0);
49                         }
50                     }
51                 } catch (Exception e) { throw new RuntimeException(e); }
52             }
53         }.start();
54         ss = new ServerSocket(3133);
55         while(true) {
56             try {
57                 Socket s = ss.accept();
58                 new Handler(s).start();
59             } catch (Exception e) {
60                 e.printStackTrace();
61             }
62         }
63     }
64
65     public static String pass_string = "password=security_is_for_wimps ";
66
67     static class Handler extends Thread {
68         private Socket socket;
69         boolean closed = false;
70         private SerialPort sp;
71         public Handler(Socket s) { this.socket = s; this.sp = sp; }
72         public void run() {
73             synchronized(Server.class) {
74                 try {
75                     this.sp = new RXTXPort("/dev/ttyS0");
76                     sp.setInputBufferSize(0);
77                     sp.setOutputBufferSize(0);
78                     sp.sendBreak(100);
79                     Thread.sleep(100);
80                     sp.sendBreak(100);
81                     try {
82                         _run();
83                     } finally {
84                         sp.close();
85                     }
86                 } catch (Exception e) { throw new RuntimeException(e); }
87             }
88         }
89         public void _run() {
90             try {
91                 final InputStream is = socket.getInputStream();
92                 byte[] buf = new byte[1024];
93                 StringBuffer sb = new StringBuffer();
94                 while(true) {
95                     int i = is.read();
96                     if (i==-1) return;
97                     if (((char)i)=='\n') break;
98                     sb.append((char)i);
99                 }
100                 System.err.println("login string: " + sb.toString());
101                 if (!sb.toString().startsWith(pass_string)) return;
102
103                 //try { Thread.sleep(2000); } catch(Exception e) { }
104                 final OutputStream os = socket.getOutputStream();
105                 System.err.println("sending instructions...");
106
107                 sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
108                 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
109                 final OutputStream fos = sp.getOutputStream();
110                 final InputStream fis = sp.getInputStream();
111                 while(fis.available() > 0)
112                     is.read();
113
114                 new Thread() {
115                     public void run() {
116                         try {
117                             byte[] buf = new byte[1024];
118                             while(true) {
119                                 int numread = is.read(buf, 0, buf.length);
120                                 if (numread == -1) break;
121                                 fos.write(buf, 0, numread);
122                                 fos.flush();
123                             }
124                         } catch (Exception e) { throw new RuntimeException(e);
125                         } finally {
126                             System.err.println("closing...");
127                             closed = true;
128                             try { fos.close(); } catch (Throwable t) { t.printStackTrace(); }
129                         }
130                     }
131                 }.start();
132
133                 System.err.println("reading back...");
134                 while(true) {
135                     long result = 0;
136                     int val = 0;
137                     for(int i=0; i<6; i++) {
138                         int k = 0;
139                         while(!closed && fis.available()==0) {
140                             if (closed) return;
141                             k++;
142                             if (k >= 100) {
143                                 System.err.println("sleep");
144                                 k = 0;
145                             }
146                             Thread.sleep(10);
147                         }
148                         if (closed) return;
149                         val = fis.read();
150                         if (val==-1) break;
151                         System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
152                         os.write((byte)val);
153                         os.flush();
154                         result |= ((long)val) << (i * 8);
155                     }
156                     if (val==-1) break;
157                     System.err.print(result);
158                     System.err.print(" 0x");
159                     System.err.print(Long.toString(result, 16));
160                     System.err.println();
161                 }
162                 System.err.println("done.");
163
164             } catch (Exception e) { throw new RuntimeException(e);
165             } finally {
166                 try {
167                     System.err.println("closing...");
168                     /*
169                     fis.close();
170                     if (fis != null) fis.close();
171                     */
172                 } catch (Throwable t) { t.printStackTrace(); }
173             }
174         }
175     }
176
177 }