OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_13/usb_fpga_2_13/trunk

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [java/] [DeviceServer/] [DeviceServer.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   DeviceServer for the ZTEX USB-FPGA Modules
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
20
    ZTEX device server
21
*/
22
 
23
import java.io.*;
24
import java.util.*;
25
import java.text.*;
26
import java.net.*;
27
 
28
import com.sun.net.httpserver.*;
29
 
30
import ch.ntb.usb.*;
31
 
32
import ztex.*;
33
 
34
// *****************************************************************************
35
// ******* ErrorBuffer *********************************************************
36
// *****************************************************************************
37
class ErrorBuffer {
38
    private static final int bufsize = 128;
39
    private static String cids[] = new String[bufsize];
40
    private static StringBuilder messages[] = new StringBuilder[bufsize];
41
    private static int id[] = new int[bufsize];
42
    private static boolean initialized = false;
43
    private static int idcnt = 0;
44
 
45
    private static void initialize() {
46
        if ( ! initialized ) {
47
            for ( int i=0; i<bufsize; i++ )
48
                id[i]=-1;
49
            initialized = true;
50
        }
51
    }
52
 
53
    public static void add (String cid, StringBuilder message) {
54
        if (cid==null) return;
55
        initialize();
56
        int j=0, k=-1;
57
        for ( int i=0; i<bufsize; i++ ) {
58
            if ( id[i]<id[j] ) j=i;
59
            if ( id[i]>=0 && cid.equals(cids[i]) ) k=i;
60
        }
61
        if ( k>=0 ) {
62
            messages[k].append(message);
63
        }
64
        else {
65
            id[j] = idcnt;
66
            idcnt ++;
67
            messages[j] = message;
68
            cids[j] = cid;
69
        }
70
    }
71
 
72
    public static StringBuilder get (String cid) {
73
        if (cid==null) return null;
74
        initialize();
75
        for ( int i=0; i<bufsize; i++ ) {
76
            if ( id[i]>=0 && cid.equals(cids[i]) ) {
77
                id[i] = -1;
78
                return messages[i];
79
            }
80
        }
81
        return null;
82
    }
83
}
84
 
85
// *****************************************************************************
86
// ******* NonBlockingBufferedInputStream **************************************
87
// *****************************************************************************
88
class NonBlockingBufferedInputStream extends BufferedInputStream {
89
    private final int timeout = 1000;
90
    private final int delay = 10;
91
 
92
    NonBlockingBufferedInputStream(InputStream in) {
93
        super(in);
94
    }
95
 
96
    public int read( byte[] b, int off, int len) throws IOException {
97
        int cnt=0, a=1;
98
        while ( len > 0 && a>0 ) {
99
            a=available();
100
            int to=0;
101
            while ( a<1 && to<timeout ) {
102
                try {
103
                    Thread.sleep( delay );
104
                }
105
                catch ( InterruptedException e) {
106
                }
107
                a=available();
108
                to+=delay;
109
            }
110
            if (a > len ) a=len;
111
            if ( a > 0 ) super.read(b, off, a);
112
            off+=a;
113
            len-=a;
114
            cnt+=a;
115
        }
116
        return cnt;
117
    }
118
 
119
    public void close() {
120
    }
121
 
122
}
123
 
124
// *****************************************************************************
125
// ******* SocketThread ********************************************************
126
// *****************************************************************************
127
class SocketThread extends Thread {
128
    private Socket socket;
129
    private PrintStream printer = null;
130
    private BufferedOutputStream binOut = null;
131
    private NonBlockingBufferedInputStream in = null;
132
 
133
// ******* SocketThread ********************************************************
134
    public SocketThread ( Socket s ) {
135
        socket = s;
136
        DeviceServer.addSocket(socket);
137
        start();
138
    }
139
 
140
// ******* out *****************************************************************
141
    private BufferedOutputStream binOut() throws IOException {
142
        if ( binOut == null ) binOut = new BufferedOutputStream( socket.getOutputStream() );
143
        if ( printer != null ) {
144
            printer.flush();
145
            printer = new PrintStream( binOut );
146
        }
147
        return binOut;
148
    }
149
 
150
// ******* writer **************************************************************
151
    private PrintStream printer() throws IOException {
152
        if ( printer == null ) printer = new PrintStream( binOut==null ? socket.getOutputStream() : binOut, true);
153
        return printer;
154
    }
155
 
156
// ******* printHelp ***********************************************************
157
    private void printHelp ( String cmd ) throws IOException  {
158
        boolean all = cmd.equalsIgnoreCase("all");
159
        PrintStream out = printer();
160
        boolean b = false;
161
        if ( all || cmd.equalsIgnoreCase("help") ) {
162
            out.println( "Supported commands:\n" +
163
                         "  scan     Scan buses\n" +
164
                         "  info     Print device capabilities\n" +
165
                         "  upload   Upload firmware\n" +
166
                         "  config   Configure FPGA\n" +
167
                         "  read     Read data from given endpoint\n" +
168
                         "  write    Write data to given endpoint\n" +
169
                         "  errors   Returns errors\n" +
170
                         "  help     Help\n" +
171
                         "  quit     Quit Device Server\n" +
172
                         "\n" +
173
                         "See help <command>|all  for detailed info\n" );
174
            b=true;
175
        }
176
 
177
        if ( all || cmd.equalsIgnoreCase("scan") ) {
178
            out.println( "[<cid:>]scan [-bin]\n" +
179
                         "  (Re)scan buses and returns the device list. If <cid> and -bin are specified\n" +
180
                         "  errors are stored and can be read out using \"errors <cid>\". If -bin is not\n" +
181
                         "  specified errors are returned directly.\n" +
182
                         "    -bin   print it in (computer friendly) binary format\n"
183
                        );
184
            b=true;
185
        }
186
 
187
        if ( all || cmd.equalsIgnoreCase("info") ) {
188
            out.println( "info <bus index> <device number>\n" +
189
                         "  Returns device capabilities.\n"
190
                        );
191
            b=true;
192
        }
193
 
194
        if ( all || cmd.equalsIgnoreCase("upload") ) {
195
            out.println( "upload <bus index> <device number> [-v] [-nv] [-e] [-f]\n" +
196
                         "  Upload firmware to USB controller. Returns errors, if any.\n" +
197
                         "    -v   upload to volatile memory (default if neither -nv nor -env is given)\n" +
198
                         "    -nv  upload to non-volatile memory\n" +
199
                         "    -e   erase / disable firmware in non-volatile memory\n" +
200
                         "    -f   force upload of incompatible firmware\n"
201
                        );
202
            b=true;
203
        }
204
 
205
        if ( all || cmd.equalsIgnoreCase("config") ) {
206
            out.println( "config <bus index> <device number> [-v] [-nv] [-e] [-f]\n" +
207
                         "  Configure FPGA. Returns errors, if any.\n" +
208
                         "    -v    upload to volatile memory (default if -nv is not given)\n" +
209
                         "    -nv   upload to non-volatile memory\n" +
210
                         "    -e    erase / disable bitstream in non-volatile memory\n" +
211
                         "    -f    force upload if already configured\n"
212
                        );
213
            b=true;
214
        }
215
 
216
        if ( all || cmd.equalsIgnoreCase("read") ) {
217
            out.println( "[<cid>:]read <bus index> <device number> <ep> [<max. bytes>]\n" +
218
                         "  Read data from endpoint and returns them. If <max. bytes> if not specified\n" +
219
                         "  data is read until end. If <cid> is specified errors are stored and can be\n" +
220
                         "  read out using \"errors <cid>\" \n"
221
                        );
222
            b=true;
223
        }
224
 
225
        if ( all || cmd.equalsIgnoreCase("write") ) {
226
            out.println( "write <bus number> <device number> <ep>\n" +
227
                         "  write data to endpoint. Returns errors, if any.\n"
228
                       );
229
            b=true;
230
        }
231
 
232
        if ( all || cmd.equalsIgnoreCase("errors") ) {
233
            out.println( "errors <cid>\n" +
234
                         "  Returns errors stored under <cid>.\n"
235
                       );
236
            b=true;
237
        }
238
 
239
        if ( all || cmd.equalsIgnoreCase("quit") ) {
240
            out.println( "quit\n" +
241
                         "  Quit Device Server\n"
242
                       );
243
            b=true;
244
        }
245
 
246
        if ( ! b )  {
247
            out.println( "No help available for command " + cmd + "\n");
248
        }
249
    }
250
 
251
// ******* str2bin *************************************************************
252
    private static void str2bin( String s, byte buf[], int start, int len ) {
253
        byte bytes[] = null;
254
        int l = 0;
255
        if ( s != null ) {
256
            bytes = s.getBytes();
257
            l = Math.min(bytes.length,len);
258
        }
259
        for ( int i=0; i<l; i++ )
260
            buf[start+i]=bytes[i];
261
        for ( int i=l; i<len; i++ )
262
            buf[start+i]=0;
263
    }
264
 
265
// ******* scan ****************************************************************
266
    private void scan ( boolean bin ) throws IOException  {
267
        DeviceServer.scanUSB();
268
        int n = DeviceServer.numberOfDevices();
269
        if ( bin ) {
270
            byte buf[] = new byte[7+15+64+64];
271
            BufferedOutputStream out = binOut();
272
            if ( n>255 ) n=255;
273
            out.write(n);
274
            for ( int i=0; i<n; i++ ) {
275
                try {
276
                    ZtexDevice1 dev = DeviceServer.device(i);
277
                    buf[0] = (byte) DeviceServer.busIdx(i);
278
                    buf[1] = (byte) DeviceServer.devNum(i);
279
                    buf[2] = (byte) (dev.valid() ? 1 : 0);
280
                    buf[3] = (byte) dev.productId(0);
281
                    buf[4] = (byte) dev.productId(1);
282
                    buf[5] = (byte) dev.productId(2);
283
                    buf[6] = (byte) dev.productId(3);
284
                    str2bin( dev.snString(), buf,7,15);
285
                    str2bin( dev.manufacturerString(), buf,22,64);
286
                    str2bin( dev.productString(), buf,86,64);
287
                    out.write(buf);
288
                }
289
                catch ( IndexOutOfBoundsException e ) {
290
                }
291
            }
292
            out.flush();
293
        }
294
        else {
295
            PrintStream out = printer();
296
            if ( n<1 ) {
297
                out.println("(No devices)");
298
            }
299
            else {
300
                out.println("# <busIdx>:<devNum>        <busName>       <product ID'S>  <serial number string>  <manufacturer string>   <product name>");
301
            }
302
            for ( int i=0; i<n; i++ ) {
303
                try {
304
                    ZtexDevice1 dev = DeviceServer.device(i);
305
                    out.println(DeviceServer.busIdx(i) + ":" + DeviceServer.devNum(i)
306
                            + " " + dev.dev().getBus().getDirname()
307
                            + ( dev.valid() ? ( "       " + ZtexDevice1.byteArrayString(dev.productId()) ) : "  (unconfigured)" )
308
                            + " \"" + ( dev.snString() == null ? "" : dev.snString() ) + "\""
309
                            + " \"" + ( dev.manufacturerString() == null ? "" : dev.manufacturerString() ) + "\""
310
                            + " \"" + ( dev.productString() == null ? "" : dev.productString() ) + "\""  );
311
 
312
                }
313
                catch ( IndexOutOfBoundsException e ) {
314
                }
315
            }
316
 
317
        }
318
    }
319
 
320
// ******* info ****************************************************************
321
    private void info ( int busIdx, int devNum ) throws IOException, Exception  {
322
        ZtexDevice1 dev = DeviceServer.findDevice(busIdx,devNum);
323
        EPDescriptorVector eps = DeviceServer.getEps(busIdx,devNum);
324
        if ( dev == null ) throw new Exception("Device " + busIdx + ":" + devNum + " not found");
325
        Ztex1v1 ztex = new Ztex1v1(dev);
326
        PrintStream out = printer();
327
        out.println("Bus name: " + dev.dev().getBus().getDirname() );
328
        out.println("Device Number: " + devNum );
329
        out.println("USB ID's: " + Integer.toHexString(dev.usbVendorId()) + ":" + Integer.toHexString(dev.usbProductId()) );
330
        out.println("Product ID's: " + ( dev.valid() ? ( ZtexDevice1.byteArrayString(dev.productId()) ) : "(unconfigured)" ) );
331
        out.println("Firmware version: " + ( dev.valid() ? (dev.fwVersion() & 255) : "" ) );
332
        out.println("Serial Number String: " + ( dev.snString() == null ? "" : dev.snString() ) );
333
        out.println("Manufacturer String: " + ( dev.manufacturerString() == null ? "" : dev.manufacturerString() ) );
334
        out.println("Product String: " + ( dev.productString() == null ? "" : dev.productString() ) );
335
        String s = ztex.capabilityInfo("\nCapability: ");
336
        if ( s.length()>0 ) out.println("Capability: " + s);
337
        if ( ztex.config != null ) {
338
            out.println("ZTEX Product: " + ztex.config.getName());
339
            out.println("FPGA: " + ztex.config.getFpga());
340
            if (ztex.config.getRamSize()>0)  out.println("RAM: " + (ztex.config.getRamSize() >> 20) + " MByte " + ztex.config.getRamType());
341
        }
342
        s = ztex.flashInfo(); if ( s.length()>0 ) out.println("Flash: " + s);
343
        try {
344
            s = ztex.getFpgaConfigurationStr();
345
            out.println("FPGA State: " + s);
346
        } catch ( Exception e ) {
347
        }
348
        if ( eps!=null ) {
349
            for ( int i=0; i<eps.size(); i++ ) {
350
                EPDescriptor ep = eps.elementAt(i);
351
                out.println("Endpoint: "+ep.num()+" "+(ep.in() ? "read" : "write"));
352
            }
353
        }
354
    }
355
 
356
// ******* run *****************************************************************
357
    public void run () {
358
        final int bufSize = 512;
359
        final int maxArgs = 32;
360
 
361
        byte buf[] = new byte[bufSize];
362
        String args[] = new String[maxArgs];
363
        int bufN=0, argsN=0;
364
        String cid="", cid2=null;
365
        boolean noErrors = false;
366
 
367
        try {
368
            in = new NonBlockingBufferedInputStream( socket.getInputStream() );
369
 
370
            // read command and args        
371
            int b = 0;
372
            do {
373
                b = in.read();
374
                if ( b <= 32 ) {
375
                    if ( bufN > 0 ) {
376
                        if ( argsN >= maxArgs ) throw new Exception("Error reading command: Argument buffer overflow");
377
                        args[argsN] = new String(buf,0,bufN);
378
                        argsN+=1;
379
                        bufN=0;
380
                    }
381
                }
382
                else {
383
                    if ( bufN >= bufSize ) throw new Exception("Error reading command: Buffer overflow");
384
                    buf[bufN] = (byte) b;
385
                    bufN+=1;
386
                }
387
            } while (b!=10 && b>0);
388
 
389
            if ( argsN == 0 ) throw new Exception ("Command missed");
390
        }
391
        catch (Exception e) {
392
//          DeviceServer.error("Error: "+e.getLocalizedMessage() );
393
            try {
394
                printer().println("Error: "+e.getLocalizedMessage());
395
            }
396
            catch (IOException f) {
397
                DeviceServer.error("Error: "+e.getLocalizedMessage() );
398
            }
399
        }
400
 
401
        StringBuilder messages = new StringBuilder();
402
        if ( args[0].indexOf(':') > 0 ) {
403
            int i = args[0].lastIndexOf(':');
404
            cid = args[0].substring(0,i);
405
            args[0] = args[0].substring(i+1);
406
        }
407
 
408
        // process commands
409
        try {
410
            // quit
411
            if ( args[0].equalsIgnoreCase("quit") ) {
412
                DeviceServer.quit = true;
413
            }
414
            // help [<command>]
415
            else if ( args[0].equalsIgnoreCase("help") ) {
416
                if ( argsN < 2 ) printHelp("help");
417
                for ( int i=1; i<argsN; i++ ) {
418
                    printHelp( args[i] );
419
                }
420
            }
421
            // [<cid>:]scan [-bin]
422
            else if ( args[0].equalsIgnoreCase("scan") ) {
423
                if ( argsN > 2 ) throw new Exception("scan: to much parameters" );
424
                if ( argsN==2 && ! args[1].equalsIgnoreCase("-bin") ) throw new Exception("scan: invalid parameter: " + args[1] );
425
                if ( argsN == 2 ) noErrors = true;
426
                scan( argsN==2 );
427
            }
428
            // info <bus index> <device number>
429
            else if ( args[0].equalsIgnoreCase("info") ) {
430
                if ( argsN !=3 ) throw new Exception("info: invalid number of parameters" );
431
                info( Integer.valueOf(args[1]), Integer.valueOf(args[2]) );
432
            }
433
            // upload <bus index> <device number> [-v] [-nv] [-e] [-f] 
434
            // config <bus index> <device number> [-v] [-nv] [-e] [-f]
435
            else if ( args[0].equalsIgnoreCase("upload") || args[0].equalsIgnoreCase("config") ) {
436
                if ( argsN<3 ) throw new Exception(args[0]+": to less parameters" );
437
                boolean vola=false, nonvola=false, erase=false, force=false;
438
                for ( int i=3; i<argsN; i++) {
439
                    if ("-v".equalsIgnoreCase(args[i])) vola=true;
440
                    else if ("-nv".equalsIgnoreCase(args[i])) nonvola=true;
441
                    else if ("-e".equalsIgnoreCase(args[i])) erase=true;
442
                    else if ("-f".equalsIgnoreCase(args[i])) force=true;
443
                    else throw new Exception("Invalid parameter: "+args[i]);
444
                }
445
                int busIdx=Integer.valueOf(args[1]);
446
                int devNum=Integer.valueOf(args[2]);
447
                ZtexDevice1 dev = DeviceServer.findDevice(busIdx, devNum);
448
                if ( dev == null ) throw new Exception("Device " + busIdx + ":" + devNum + " not found");
449
                Ztex1v1 ztex = new Ztex1v1(dev);
450
 
451
                if ( args[0].equalsIgnoreCase("upload")) {
452
                    DeviceServer.loadFirmware ( ztex, messages, in, IPPermissions.toString( socket.getInetAddress() ), force, vola, nonvola, erase );
453
                    int ndn = ztex.dev().dev().getDevnum();
454
                    if ( ndn != devNum ) {
455
                        messages.append("Device re-numerated: " + busIdx + ":" + devNum + " -> " + busIdx + ":" + ndn + "\n");
456
                        DeviceServer.scanUSB();
457
                    }
458
                }
459
                else {
460
                    DeviceServer.loadBitstream ( ztex, messages, in, IPPermissions.toString( socket.getInetAddress() ), force, vola, nonvola, erase );
461
                }
462
            }
463
            // write <bus number> <device number> <ep> 
464
            else if ( args[0].equalsIgnoreCase("write") ) {
465
                if ( argsN !=4 ) throw new Exception("write: invalid number of parameters" );
466
                int busIdx=Integer.valueOf(args[1]);
467
                int devNum=Integer.valueOf(args[2]);
468
                ZtexDevice1 dev = DeviceServer.findDevice(busIdx, devNum);
469
                if ( dev == null ) throw new Exception("Device " + busIdx + ":" + devNum + " not found");
470
                Ztex1v1 ztex = new Ztex1v1(dev);
471
                EPDescriptorVector eps = DeviceServer.getEps(busIdx,devNum);
472
                try {
473
                    DeviceServer.epUpload (ztex, eps.find(Integer.valueOf(args[3])), in, messages);
474
                }
475
                finally {
476
                    DeviceServer.release(ztex);
477
                }
478
            }
479
            // [<cid>:]read <bus index> <device number> <ep> [<max. bytes>]
480
            else if ( args[0].equalsIgnoreCase("read") ) {
481
                noErrors = true;
482
                if ( argsN<4 || argsN>5 ) throw new Exception("read: invalid number of parameters" );
483
                int busIdx=Integer.valueOf(args[1]);
484
                int devNum=Integer.valueOf(args[2]);
485
                ZtexDevice1 dev = DeviceServer.findDevice(busIdx, devNum);
486
                if ( dev == null ) throw new Exception("Device " + busIdx + ":" + devNum + " not found");
487
                Ztex1v1 ztex = new Ztex1v1(dev);
488
                EPDescriptorVector eps = DeviceServer.getEps(busIdx,devNum);
489
                int max_size = argsN==5 ? Integer.valueOf(args[4]) : Integer.MAX_VALUE;
490
                try {
491
                    DeviceServer.epDownload (ztex, eps.find(Integer.valueOf(args[3])), binOut(), max_size, messages);
492
                    binOut.flush();
493
                }
494
                finally {
495
                    DeviceServer.release(ztex);
496
                }
497
            }
498
            // error <cid>
499
            else if ( args[0].equalsIgnoreCase("errors") ) {
500
                cid2 = cid;
501
                cid = null;
502
                if ( argsN > 2 ) throw new Exception("errors: to much parameters" );
503
                if ( argsN == 2 ) cid2 = args[1];
504
                messages = ErrorBuffer.get(cid2);
505
                if (messages != null) printer().print( messages );
506
                messages = new StringBuilder();
507
            }
508
            else {
509
                throw new Exception("Invalid command: "+args[0] );
510
            }
511
        }
512
        catch ( IOException e) {
513
            DeviceServer.error("Error: "+e.getLocalizedMessage() );
514
        }
515
        catch (NumberFormatException e) {
516
            messages.append("Error: Number expected: "+e.getLocalizedMessage()+"\n");
517
        }
518
        catch (Exception e) {
519
            messages.append("Error: "+e.getLocalizedMessage()+"\n");
520
        }
521
 
522
        try {
523
            if ( messages != null && messages.length()>0 ) {
524
                if ( ! noErrors ) printer().print(messages);
525
                ErrorBuffer.add(cid,messages);
526
            }
527
        }
528
        catch ( IOException e) {
529
            DeviceServer.error("Error2: "+e.getLocalizedMessage() );
530
        }
531
 
532
 
533
        try {
534
            socket.getInputStream().close();
535
        }
536
        catch (Exception e) {
537
            DeviceServer.error("Error closing input stream: "+e.getLocalizedMessage() );
538
        }
539
 
540
        try {
541
            if ( binOut!=null ) binOut.close();
542
            else if ( printer != null ) printer.close();
543
        }
544
        catch (Exception e) {
545
            DeviceServer.error("Error closing output stream: "+e.getLocalizedMessage() );
546
        }
547
 
548
        DeviceServer.removeSocket(socket);
549
        try {
550
            socket.close();
551
        }
552
        catch (Exception e) {
553
            DeviceServer.error("Error closing output socket: "+e.getLocalizedMessage() );
554
        }
555
    }
556
}
557
 
558
 
559
// *****************************************************************************
560
// ******* MultipartFormDataReader ********************************************
561
// *****************************************************************************
562
class MultipartFormDataReader {
563
    private final byte eol[] = { 13, 10 };
564
    private InputStream in;
565
    private byte sep[] = null;
566
    private boolean eof = false;
567
    public String name = "";
568
    public String fileName = "";
569
 
570
// ******* readTo **************************************************************
571
    private boolean readTo ( OutputStream out, byte s[] ) {
572
        byte buf[] = new byte[s.length];
573
        int eq = 0;
574
        while ( eq<s.length && !eof ) {
575
            int i = 0;
576
            try {
577
                i = in.read();
578
                eof = i<0;
579
            }
580
            catch ( IOException e ) {
581
                eof = true;
582
            }
583
            if ( !eof ) {
584
                buf[eq] = (byte) i;
585
                if ( buf[eq] == s[eq] ) {
586
                    eq++;
587
                }
588
                else {
589
                    try {
590
                        if ( out != null ) out.write(buf,0,eq+1);
591
                    }
592
                    catch ( IOException e ) {
593
                    }
594
                    eq=0;
595
                }
596
            }
597
        }
598
        return !eof;
599
    }
600
 
601
// ******* MultiPartFormDataReader *********************************************
602
    MultipartFormDataReader ( InputStream in_ ) {
603
        in = in_;
604
        do {
605
            try {
606
                if ( in.read() == 45 && in.read() == 45 ) {
607
                    ByteArrayOutputStream buf = new ByteArrayOutputStream();
608
                    buf.write(eol,0,eol.length);
609
                    buf.write(45);
610
                    buf.write(45);
611
                    readTo( buf, eol );
612
                    sep = buf.toByteArray();
613
//                  System.out.println("sep: -->" + new String(sep) + "<--");
614
                }
615
                else {
616
                    readTo( null, eol );
617
                }
618
            }
619
            catch ( IOException e ) {
620
                eof = true;
621
            }
622
        } while ( sep == null && !eof );
623
    }
624
 
625
// ******* readField ***********************************************************
626
    public boolean readField ( OutputStream data ) {
627
        if ( sep == null ) return false;
628
        ByteArrayOutputStream lineBuf = new ByteArrayOutputStream();
629
        String line;
630
        name = "";
631
        fileName = "";
632
        do {
633
            readTo ( lineBuf, eol );
634
            line = lineBuf.toString();
635
//          System.out.println("line: "+line);
636
            int i=0;
637
            while ( i<line.length() && line.codePointAt(i) <= 32 ) i++;
638
            if ( line.length()>=i+19 && line.substring(i,i+19).equalsIgnoreCase("Content-Disposition") ) {
639
                String tokens[] = line.split(";");
640
                for ( int j=1; j<tokens.length; j++ ) {
641
                    String t = tokens[j];
642
                    i=0;
643
                    while ( t.codePointAt(i) <= 32 && i < t.length() ) i++;
644
                    String s=t.substring(i,i+5);
645
                    if ( s.equalsIgnoreCase("name ") || s.equalsIgnoreCase("name=") ) {
646
                        int a = t.indexOf("\"");
647
                        int z = t.lastIndexOf("\"");
648
                        if ( a>0 && z>a ) name=t.substring(a+1,z);
649
                    }
650
                    s=t.substring(i,i+9);
651
                    if ( s.equalsIgnoreCase("filename ") || s.equalsIgnoreCase("filename=") ) {
652
                        int a = t.indexOf("\"");
653
                        int z = t.lastIndexOf("\"");
654
                        if ( a>0 && z>a ) fileName=t.substring(a+1,z);
655
                    }
656
                }
657
//              System.out.println("name: "+name);
658
//              System.out.println("filename: "+fileName);
659
            }
660
            lineBuf.reset();
661
        } while ( line.length()>0 && !eof );
662
        if ( ! eof ) readTo( data, sep );
663
        boolean result = !eof;
664
        try {
665
            in.read();
666
            in.read();
667
        }
668
        catch ( IOException e ) {
669
            eof = true;
670
        }
671
        return result;
672
    }
673
}
674
 
675
 
676
// *****************************************************************************
677
// ******* ZtexHttpHandler *****************************************************
678
// *****************************************************************************
679
class ZtexHttpHandler implements HttpHandler {
680
 
681
// ******* htmlHeader **********************************************************
682
    private StringBuilder htmlHeader ( String title )  {
683
        StringBuilder sb = new StringBuilder();
684
        sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
685
        sb.append("<html>\n");
686
        sb.append("<head>\n");
687
        sb.append("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
688
        sb.append("  <meta http-equiv=\"Content-Language\" content=\"en\">\n");
689
        sb.append("  <meta name=\"author\" content=\"ZTEX GmbH\">\n");
690
        sb.append("<title>" + title + "</title>\n");
691
        sb.append("<style type=\"text/css\">\n");
692
        sb.append("body { background-color:#f0f0f0; color:#202020; font-family:Helvetica,Helv,sans-serif; font-size:11pt}\n");
693
        sb.append("a:link { color:#2020a0; }\n");
694
        sb.append("a:visited { color:#a02020; }\n");
695
        sb.append("a:active { color:#208020; }\n");
696
        sb.append("a.topmenu { color:#ffffff; font-size:12pt; text-decoration:none; font-weight:bold }\n");
697
        sb.append("a.topmenu:link { color:#ffffff; }\n");
698
        sb.append("a.topmenu:visited { color:#ffffff; }\n");
699
        sb.append("a.topmenu:hover { color:#202020; }\n");
700
        sb.append("</style>\n");
701
        sb.append("</head>\n");
702
        sb.append("<body>\n");
703
        sb.append("<center><table border=0 bgcolor=\"#7870a0\" cellpadding=2 cellspacing=0><tr><td>\n");
704
        sb.append("<table border=0 bgcolor=\"#eae6ff\" cellpadding=5 cellspacing=10>\n");
705
        sb.append("  <tr><th bgcolor=\"#cac4ec\">\n");
706
        sb.append("    <span style=\"font-size:125%\">" + title + "</span>\n");
707
        sb.append("  </th></tr>\n");
708
        sb.append("  <tr><td align=center>\n");
709
        return sb;
710
    }
711
 
712
// ******* heading *************************************************************
713
    private StringBuilder heading ( StringBuilder sb, String s )  {
714
        sb.append ( "</td></tr>\n");
715
        sb.append("  <tr><th bgcolor=\"#cac4ec\">\n");
716
        sb.append("    <span style=\"font-size:125%\">" + s + "</span>\n");
717
        sb.append("  </th></tr>\n");
718
        sb.append("  <tr><td align=center>\n");
719
        return sb;
720
    }
721
 
722
// ******* htmlConvert *********************************************************
723
    private byte[] htmlConvert ( StringBuilder sb )  {
724
        sb.append ( "</td></tr>\n");
725
        sb.append ( "</table>\n");
726
        sb.append ( "</td></tr></table><center>\n");
727
        sb.append ( "<p>\n");
728
        sb.append ( "<hr>\n");
729
        sb.append ( "<center>\n");
730
        sb.append ( "  <a href=\"http://www.ztex.de/\">[ZTEX Homepage]</a>&nbsp;\n");
731
        sb.append ( "  <a href=\"http://wiki.ztex.de/\">[ZTEX Wiki]</a>&nbsp;\n");
732
        sb.append ( "  <span style=\"font-size:80%\">&#169; ZTEX GmbH</span>\n");
733
        sb.append ( "</center>\n");
734
        sb.append ( "</body></html>" );
735
        return sb.toString().getBytes();
736
    }
737
 
738
// ******* test ****************************************************************
739
    private byte[] test (HttpExchange t) throws IOException {
740
        InputStream in = new BufferedInputStream( t.getRequestBody() );
741
        System.out.println("Request Body: " + in.available() + "Bytes");
742
        int i;
743
        do {
744
            i = in.read();
745
            if ( i>=0 ) System.out.print((char)i);
746
        } while (i>=0);
747
 
748
        Headers h = t.getResponseHeaders();
749
        h.add("Content-Type", "text/html;Charset=iso-8859-1");
750
        StringBuilder sb = htmlHeader ("Test");
751
        sb.append ("<form action=\"test\" method=\"post\" enctype=\"multipart/form-data\">\n");
752
        sb.append ("  <p>W&auml;hlen Sie eine Textdatei (txt, html usw.) von Ihrem Rechner aus:<br>\n");
753
        sb.append ("    <input name=\"Datei\" type=\"file\" size=\"50\" maxlength=\"100000\" >\n");
754
        sb.append ("  </p>\n");
755
        sb.append ("  <input type=\"checkbox\" name=\"upload_to\" value=\"v\">Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
756
        sb.append ("  <input type=\"checkbox\" name=\"upload_to\" value=\"v\">Non-Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
757
        sb.append ("  <input type=\"submit\" value=\"Submit\">\n");
758
        sb.append ("</form>\n");
759
 
760
        return htmlConvert(sb);
761
    }
762
 
763
// ******* test2 ***************************************************************
764
    private byte[] test2 (HttpExchange t) throws IOException {
765
        MultipartFormDataReader form = new MultipartFormDataReader( new BufferedInputStream( t.getRequestBody() ) );
766
        ByteArrayOutputStream data = new ByteArrayOutputStream();
767
        while ( form.readField( data ) ) {
768
            System.out.println( "Name=\"" + form.name + "\"" );
769
            System.out.println( "Filename=\"" + form.fileName + "\"" );
770
            System.out.println( "Data -->" + data + "<--" );
771
            data.reset();
772
        }
773
 
774
        Headers h = t.getResponseHeaders();
775
        h.add("Content-Type", "text/html;Charset=iso-8859-1");
776
        StringBuilder sb = htmlHeader ("Test2");
777
        sb.append ("<form action=\"test2\" method=\"post\" enctype=\"multipart/form-data\">\n");
778
        sb.append ("  <p>W&auml;hlen Sie eine Textdatei (txt, html usw.) von Ihrem Rechner aus:<br>\n");
779
        sb.append ("    <input name=\"Datei\" type=\"file\" size=\"50\" maxlength=\"100000\" >\n");
780
        sb.append ("  </p>\n");
781
        sb.append ("  <input type=\"checkbox\" name=\"upload_to\" value=\"v\">Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
782
        sb.append ("  <input type=\"checkbox\" name=\"upload_to\" value=\"nv\">Non-Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
783
        sb.append ("  <input type=\"submit\" value=\"Submit\">\n");
784
        sb.append ("</form>\n");
785
 
786
        return htmlConvert(sb);
787
    }
788
 
789
// ******* scan ****************************************************************
790
    private byte[] scan (HttpExchange t) {
791
        DeviceServer.scanUSB();
792
        int n = DeviceServer.numberOfDevices();
793
        Headers h = t.getResponseHeaders();
794
        h.add("Content-Type", "text/html;Charset=iso-8859-1");
795
        StringBuilder sb = htmlHeader ("Device overview");
796
        sb.append ("<table border=\"0\" bgcolor=\"#808080\" cellspacing=1 cellpadding=4>\n");
797
        sb.append ("  <tr>\n");
798
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Device Link / <br> &lt;Bus Index&gt;:&lt;Device Number&gt;</td>\n");
799
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Bus Name</td>\n");
800
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Product ID's</td>\n");
801
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Serial Number String</td>\n");
802
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Manufacturer String</td>\n");
803
        sb.append ("    <td align=center bgcolor=\"#e0e0e0\">Product String</td>\n");
804
        sb.append ("  </tr>\n");
805
        if ( n<1 ) {
806
            sb.append("<tr><td align=center bgcolor=\"#f0f0f0\" colspan=6>(No devices)</td>");
807
        } else {
808
            for ( int i=0; i<n; i++ ) {
809
                try {
810
                    ZtexDevice1 dev = DeviceServer.device(i);
811
                    sb.append("    <tr>\n");
812
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\"><a href=\"" + DeviceServer.busIdx(i) + ":" + DeviceServer.devNum(i) + "\">" + DeviceServer.busIdx(i) + ":" + DeviceServer.devNum(i) + "</a></td>\n");
813
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\">" + dev.dev().getBus().getDirname() + "</td>\n");
814
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\">" + ( dev.valid() ? ( ZtexDevice1.byteArrayString(dev.productId()) ) : "(unconfigured)" ) + "</td>\n");
815
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\">" + ( dev.snString() == null ? "" : dev.snString() ) + "</td>\n");
816
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\">" + ( dev.manufacturerString() == null ? "" : dev.manufacturerString() ) + "</td>\n");
817
                    sb.append("    <td align=center bgcolor=\"#f0f0f0\">" + ( dev.productString() == null ? "" : dev.productString() ) + "</td>\n");
818
                    sb.append("  </tr>\n");
819
                }
820
                catch ( IndexOutOfBoundsException e ) {
821
                }
822
            }
823
        }
824
        sb.append ("</table>\n");
825
        sb.append ("<p>\n");
826
        sb.append ("<a href=\"/scan\"><button>Re-Scan</button></a>\n");
827
        return htmlConvert(sb);
828
    }
829
 
830
// ******* device **************************************************************
831
    private byte[] device ( HttpExchange t, int busIdx, int devNum, int epnum, ZtexDevice1 dev ) {
832
 
833
        StringBuilder messages = new StringBuilder();
834
        EPDescriptorVector eps = DeviceServer.getEps(busIdx,devNum);
835
        Headers h = t.getResponseHeaders();
836
 
837
        // ***********
838
        // * request *
839
        // ***********
840
        boolean fw_force = false;
841
        boolean fw_upload_v = false;
842
        boolean fw_upload_nv = false;
843
        boolean fw_erase = false;
844
        ByteArrayInputStream fw_data = null;
845
        String fw_data_name = null;
846
 
847
        boolean bs_force = false;
848
        boolean bs_upload_v = false;
849
        boolean bs_upload_nv = false;
850
        boolean bs_erase = false;
851
        byte bs_data[] = null;
852
        String bs_data_name = null;
853
 
854
        ByteArrayInputStream ep_data = null;
855
        String ep_data_name = null;
856
        int ep_data_num = -1;
857
        int ep_down_size = -1;
858
 
859
        MultipartFormDataReader form = new MultipartFormDataReader( new BufferedInputStream( t.getRequestBody() ) );
860
        ByteArrayOutputStream data = new ByteArrayOutputStream();
861
        while ( form.readField( data ) ) {
862
/*          System.out.println( "Name=\"" + form.name + "\"" );
863
            System.out.println( "Filename=\"" + form.fileName + "\"" );
864
            System.out.println( "Data -->" + data + "<--" ); */
865
            if ( data.size()>0 ) {
866
                if ( form.name.equalsIgnoreCase("fw_force" ) ) fw_force=true;
867
                else if ( form.name.equalsIgnoreCase("fw_upload_v" ) ) fw_upload_v=true;
868
                else if ( form.name.equalsIgnoreCase("fw_upload_nv" ) ) fw_upload_nv=true;
869
                else if ( form.name.equalsIgnoreCase("fw_erase" ) ) fw_erase=true;
870
                else if ( form.name.equalsIgnoreCase("fw_data" ) ) {
871
                    fw_data = new ByteArrayInputStream(data.toByteArray());
872
                    fw_data_name = IPPermissions.toString( t.getRemoteAddress().getAddress() ) + ":" + form.fileName;
873
                }
874
                else if ( form.name.equalsIgnoreCase("bs_force" ) ) bs_force=true;
875
                else if ( form.name.equalsIgnoreCase("bs_upload_v" ) ) bs_upload_v=true;
876
                else if ( form.name.equalsIgnoreCase("bs_upload_nv" ) ) bs_upload_nv=true;
877
                else if ( form.name.equalsIgnoreCase("bs_erase" ) ) bs_erase=true;
878
                else if ( form.name.equalsIgnoreCase("bs_data" ) ) {
879
                    bs_data = data.toByteArray();
880
                    bs_data_name = IPPermissions.toString( t.getRemoteAddress().getAddress() ) + ":" + form.fileName;
881
                }
882
                else if ( form.name.equalsIgnoreCase("ep_down_size" ) ) {
883
                    try {
884
                        ep_down_size = Integer.valueOf(data.toString());
885
                    }
886
                    catch (Exception e) {
887
                        ep_down_size = -1;
888
                    }
889
//                  System.out.println(ep_down_size);
890
                }
891
                else {
892
                    for ( int i=0; eps!=null && i<eps.size(); i++ ) {
893
                        EPDescriptor ep = eps.elementAt(i);
894
                        if ( ! ep.in() && form.name.equalsIgnoreCase("ep_"+ep.num()+"_data" ) ) {
895
                            ep_data = new ByteArrayInputStream(data.toByteArray());
896
                            ep_data_name = IPPermissions.toString( t.getRemoteAddress().getAddress() ) + ":" + form.fileName;
897
                            ep_data_num = ep.num();
898
                        }
899
                    }
900
                }
901
                data.reset();
902
            }
903
        }
904
 
905
        // **********
906
        // * action *
907
        // **********
908
        Ztex1v1 ztex = null;
909
        try {
910
            ztex = new Ztex1v1(dev);
911
        } catch ( Exception e ) {
912
            ztex = null;
913
            messages.append( "Error: " + e.getLocalizedMessage() + "\n");
914
        }
915
 
916
        int oldDevNum = devNum;
917
        try {
918
            DeviceServer.loadFirmware ( ztex, messages, fw_data, fw_data_name, fw_force, fw_upload_v, fw_upload_nv, fw_erase );
919
            if ( ztex != null ) {
920
                devNum = ztex.dev().dev().getDevnum();
921
                if ( devNum != oldDevNum ) {
922
                    messages.append("Device re-numerated: " + busIdx + ":" + oldDevNum + " -> " + busIdx + ":" + devNum + "\n");
923
                    DeviceServer.scanUSB();
924
                    eps = DeviceServer.getEps(busIdx,devNum);
925
                }
926
            }
927
        } catch ( Exception e ) {
928
            messages.append( "Error: " + e.getLocalizedMessage() + '\n' );
929
        }
930
 
931
        try {
932
            if ( ztex != null ) DeviceServer.loadBitstream ( ztex, messages, bs_data, bs_data_name, bs_force, bs_upload_v, bs_upload_nv, bs_erase );
933
        } catch ( Exception e ) {
934
            messages.append( "Error: " + e.getLocalizedMessage() + '\n' );
935
        }
936
 
937
        try {
938
            if ( ep_data != null ) DeviceServer.epUpload (ztex, eps.find(ep_data_num), ep_data, messages);
939
        } catch ( Exception e ) {
940
            messages.append( "Error: " + e.getLocalizedMessage() + '\n' );
941
        }
942
 
943
        try {
944
            if ( epnum>0 ){
945
                ByteArrayOutputStream out = new ByteArrayOutputStream();
946
                DeviceServer.epDownload (ztex, eps.find(epnum), out, ep_down_size, messages);
947
                h.add("Content-Type", "application/octet-stream");
948
                return out.toByteArray();
949
            }
950
        } catch ( Exception e ) {
951
            messages.append( "Error: " + e.getLocalizedMessage() + '\n' );
952
        }
953
 
954
        DeviceServer.release (ztex);
955
 
956
        // ************
957
        // * response *
958
        // ************
959
        h.add("Content-Type", "text/html;Charset=iso-8859-1");
960
        StringBuilder sb = htmlHeader ("Device " + busIdx + ":" + devNum + ( devNum!=oldDevNum ? ( " (was " + busIdx + ":" + oldDevNum +")" ) : "" ) );
961
 
962
        // info 
963
        sb.append ("<table border=\"0\" bgcolor=\"#808080\" cellspacing=1 cellpadding=4>\n");
964
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Bus name: </td><td align=left bgcolor=\"#f0f0f0\">" + dev.dev().getBus().getDirname() + "</td></tr>\n");
965
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Device Number: </td><td align=left bgcolor=\"#f0f0f0\">" + devNum + "</td></tr>\n");
966
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> USB ID's: </td><td align=left bgcolor=\"#f0f0f0\">" + Integer.toHexString(dev.usbVendorId()) + ":" + Integer.toHexString(dev.usbProductId()) + "</td></tr>\n");
967
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Product ID's: </td><td align=left bgcolor=\"#f0f0f0\">" + ( dev.valid() ? ( ZtexDevice1.byteArrayString(dev.productId()) ) : "(unconfigured)" ) + "</td></tr>\n");
968
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Firmware version: </td><td align=left bgcolor=\"#f0f0f0\">" + ( dev.valid() ? (dev.fwVersion() & 255) : "" ) + "</td></tr>\n");
969
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Serial Number String: </td><td align=left bgcolor=\"#f0f0f0\">" + ( dev.snString() == null ? "" : dev.snString() ) + "</td></tr>\n");
970
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Manufacturer String: </td><td align=left bgcolor=\"#f0f0f0\">" + ( dev.manufacturerString() == null ? "" : dev.manufacturerString() ) + "</td></tr>\n");
971
        sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Product String: </td><td align=left bgcolor=\"#f0f0f0\">" + ( dev.productString() == null ? "" : dev.productString() ) + "</td></tr>\n");
972
        if ( ztex != null ) {
973
            String s = ztex.capabilityInfo(", ");
974
            if ( s.length()>0 ) sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Capabilities: </td><td align=left bgcolor=\"#f0f0f0\">" + s + "</td></tr>\n");
975
            if ( ztex.config != null ) {
976
                sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> ZTEX Product: </td><td align=left bgcolor=\"#f0f0f0\">" + ztex.config.getName() + "</td></tr>\n");
977
                sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> FPGA: </td><td align=left bgcolor=\"#f0f0f0\">" + ztex.config.getFpga() + "</td></tr>\n");
978
                if (ztex.config.getRamSize()>0) sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> RAM: </td><td align=left bgcolor=\"#f0f0f0\">" + (ztex.config.getRamSize() >> 20) + " MByte " + ztex.config.getRamType() + "</td></tr>\n");
979
                s = ztex.flashInfo(); if ( s.length()>0 ) sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> Flash: </td><td align=left bgcolor=\"#f0f0f0\">" + s + "</td></tr>\n");
980
            }
981
            try {
982
                s = ztex.getFpgaConfigurationStr();
983
                sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> FPGA State: </td><td align=left bgcolor=\"#f0f0f0\">" + s + "</td></tr>\n");
984
            } catch ( Exception e ) {
985
            }
986
        }
987
        sb.append ("</table>\n");
988
        sb.append ("<p><a href=\"/\"><button>Device Overview</button></a>\n");
989
 
990
        // firmware
991
        heading(sb,"Firmware Upload");
992
        sb.append ("<form action=\"" + busIdx + ":" + devNum + "\" method=\"post\" enctype=\"multipart/form-data\">\n");
993
        sb.append ("  <div align=left>\n");
994
        sb.append ("    Firmware file: <input name=\"fw_data\" type=\"file\" size=\"70\" accept=\".ihx\" maxlength=\"5000000\"><p>\n");
995
        sb.append ("    <input type=\"checkbox\" name=\"fw_upload_v\" value=\"x\" " + ( fw_upload_v ? "checked" : "" ) + ">Upload to volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
996
//        try {
997
//          if ( ztex != null ) {
998
//              ztex.checkCapability(ztex.CAPABILITY_EEPROM);
999
                sb.append ("    <input type=\"checkbox\" name=\"fw_upload_nv\" value=\"x\"" + ( fw_upload_nv ? "checked" : "" ) + ">Upload to non-Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
1000
                sb.append ("    <input type=\"checkbox\" name=\"fw_erase\" value=\"x\"" + ( fw_erase ? "checked" : "" ) + ">Erase firmware in non-volatile memory");
1001
//          }
1002
//      }
1003
//      catch ( Exception a ) {
1004
//      }
1005
        sb.append ("    <input type=\"checkbox\" name=\"fw_force\" value=\"x\"" + ( fw_force ? "checked" : "" ) + ">Enforce upload<p>");
1006
        sb.append ("    (Before firmware can be loaded into non-volatile memory some firmware must be installed in volatile memory.)<p>\n");
1007
        sb.append ("  </div>\n");
1008
        sb.append ("  <input type=\"submit\" value=\"Submit\">\n");
1009
        sb.append ("</form>\n");
1010
 
1011
        // bitstream
1012
        try {
1013
            if ( ztex == null ) throw new Exception();
1014
            ztex.checkCapability(ztex.CAPABILITY_FPGA);
1015
            heading(sb,"Bitstream Upload");
1016
            sb.append ("<form action=\"" + busIdx + ":" + devNum + "\" method=\"post\" enctype=\"multipart/form-data\">\n");
1017
            sb.append ("  <div align=left>\n");
1018
            sb.append ("    Bitstream file: <input name=\"bs_data\" type=\"file\" size=\"70\" accept=\".ihx\" maxlength=\"5000000\"><p>\n");
1019
            sb.append ("        <input type=\"checkbox\" name=\"bs_upload_v\" value=\"x\" " + ( bs_upload_v ? "checked" : "" ) + ">Upload to volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
1020
            try {
1021
                if ( ztex != null && ztex.flashEnabled() ) {
1022
                    sb.append ("    <input type=\"checkbox\" name=\"bs_upload_nv\" value=\"x\"" + ( bs_upload_nv ? "checked" : "" ) + ">Upload to non-Volatile Memory &nbsp;&nbsp;&nbsp;&nbsp;\n");
1023
                    sb.append ("    <input type=\"checkbox\" name=\"bs_erase\" value=\"x\"" + ( bs_erase ? "checked" : "" ) + ">Erase bitstream in non-volatile memory");
1024
                }
1025
            }
1026
            catch ( Exception a ) {
1027
            }
1028
            sb.append ("    <input type=\"checkbox\" name=\"bs_force\" value=\"x\"" + ( bs_force ? "checked" : "" ) + ">Enforce upload<p>");
1029
            sb.append ("  </div>\n");
1030
            sb.append ("  <input type=\"submit\" value=\"Submit\">\n");
1031
            sb.append ("</form>\n");
1032
        }
1033
        catch ( Exception a ) {
1034
        }
1035
 
1036
        // endpoints
1037
        if ( eps!=null && eps.size()>0) {
1038
            heading(sb,"Endpoints");
1039
            sb.append ("<table border=\"0\" bgcolor=\"#808080\" cellspacing=1 cellpadding=12>\n");
1040
            for (int i=0; i<eps.size(); i++ ) {
1041
                EPDescriptor ep = eps.elementAt(i);
1042
                if ( ep.in() ) {
1043
                    sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> IN EP " + ep.num() +":</td>  <td align=left bgcolor=\"#f0f0f0\">"
1044
                        + "<form action=\"" + busIdx + ":" + devNum + ":" + ep.num() + "\" method=\"post\" enctype=\"multipart/form-data\">"
1045
                        + "Maximum size: <input type=\"text\" name=\"ep_down_size\" value=\"1000000\" size=12 maxlength=11>"
1046
                        + "&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Download\">"
1047
                        + "</form></td></tr>\n" );
1048
                } else {
1049
                    sb.append("  <tr><td align=left bgcolor=\"#e0e0e0\"> OUT EP " + ep.num() +":</td>  <td align=left bgcolor=\"#f0f0f0\">"
1050
                        + "<form action=\"" + busIdx + ":" + devNum + "\" method=\"post\" enctype=\"multipart/form-data\">"
1051
                        + "File: <input name=\"ep_"+ep.num()+"_data\" type=\"file\" size=\"60\" accept=\"\" maxlength=\"50000000\">"
1052
                        + "&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Upload\">"
1053
                        + "</form></td></tr>\n" );
1054
                }
1055
            }
1056
            sb.append ("</table>\n");
1057
        }
1058
 
1059
        // messages
1060
        if ( messages.length() > 0 ) {
1061
            heading(sb,"Messages");
1062
            sb.append ("<div align=left><pre>\n");
1063
            sb.append(messages);
1064
            sb.append ("</pre></div>");
1065
        }
1066
 
1067
        return htmlConvert(sb);
1068
    }
1069
 
1070
// ******* handle **************************************************************
1071
    public void handle(HttpExchange t) throws IOException {
1072
        String path = t.getRequestURI().getPath();
1073
        int responseCode = 200;
1074
        byte buf[] = {};
1075
        if ( path.charAt(0) != '/' ) path = '/' + path;
1076
        int rcvd = t.getRequestBody().available();
1077
 
1078
        if ( ! DeviceServer.httpPermissions().checkAddress( t.getRemoteAddress().getAddress() ) ) {
1079
            responseCode = 400;
1080
            StringBuilder sb = htmlHeader ("400 Bad Request");
1081
            sb.append("Access denied" );
1082
            buf = htmlConvert(sb);
1083
        }
1084
/*      else if ( path.equalsIgnoreCase("/test") ) {
1085
            buf = test(t);
1086
        }
1087
        else if ( path.equalsIgnoreCase("/test2") ) {
1088
            buf = test2(t);
1089
        } */
1090
        else if ( path.equalsIgnoreCase("/") || path.equalsIgnoreCase("/scan") ) {
1091
            buf = scan(t);
1092
        }
1093
        else if ( path.indexOf(':') > 0 ) {
1094
            try {
1095
                int i = path.indexOf(':');
1096
                int j = path.lastIndexOf(':');
1097
                if (j<=i) j=path.length();
1098
                int busIdx = Integer.valueOf(path.substring(1,i));
1099
                int devNum = Integer.valueOf(path.substring(i+1,j ));
1100
                int epNum = j < path.length() ? Integer.valueOf(path.substring(j+1)) : -1;
1101
                ZtexDevice1 dev = DeviceServer.findDevice(busIdx,devNum);
1102
                if ( dev == null ) throw new Exception();
1103
                buf = device(t, busIdx, devNum, epNum, dev);
1104
            }
1105
            catch ( Exception e ) {
1106
                responseCode = 400;
1107
                StringBuilder sb = htmlHeader ("400 Bad Request");
1108
                sb.append("Invalid device path: " + path );
1109
                sb.append ("<p>\n");
1110
                sb.append ("<a href=\"/\"><button>Device Overview</button></a>\n");
1111
                buf = htmlConvert(sb);
1112
            }
1113
        }
1114
        else {
1115
            responseCode = 404;
1116
            StringBuilder sb = htmlHeader ("404 Not Found");
1117
            sb.append("Invalid path: " + path );
1118
            sb.append ("<p>\n");
1119
            sb.append ("<a href=\"/\"><button>Device Overview</button></a>\n");
1120
            buf = htmlConvert(sb);
1121
        }
1122
        DeviceServer.info( "Connection from " + IPPermissions.toString( t.getRemoteAddress().getAddress() ) + ": " + path + ": " + responseCode + ": received " + rcvd + " bytes,  sent " + buf.length + " bytes" );
1123
        t.sendResponseHeaders(responseCode, buf.length);
1124
        OutputStream os = t.getResponseBody();
1125
        os.write(buf);
1126
        os.close();
1127
    }
1128
}
1129
 
1130
 
1131
// *****************************************************************************
1132
// ******* IPPermissionList ****************************************************
1133
// *****************************************************************************
1134
class IPPermissions {
1135
    private byte ip[][] = { { 127, 0, 0, 1 } };
1136
    private boolean deny[] = { false };
1137
    private int mask[] = { 32 };
1138
 
1139
    public IPPermissions(String adrs) throws UnknownHostException,IllegalArgumentException {
1140
        String strs[] = adrs.split(",");
1141
        ip = new byte[strs.length][];
1142
        deny = new boolean[strs.length];
1143
        mask = new int[strs.length];
1144
        for (int i=0; i<strs.length; i++ ) {
1145
            if ( strs[i].length()==0 ) throw new IllegalArgumentException( "Invalid address format at position " + (i+1) + ": empty string");
1146
            deny[i] = strs[i].charAt(0) == '-';
1147
            int start = deny[i] ? 1 : 0;
1148
            int end = strs[i].lastIndexOf("/");
1149
            if ( end < 0 ) end = strs[i].length();
1150
            ip[i] = InetAddress.getByName(strs[i].substring(start,end)).getAddress();
1151
            try {
1152
                mask[i] = ( end+1 < strs[i].length() ) ? Integer.parseInt(strs[i].substring(end+1)) : ip[i].length*8;
1153
            }
1154
            catch (Exception e) {
1155
                throw new IllegalArgumentException("Invalid mask format at position " + (i+1) + ": `" + strs[i].substring(end+1) + "'" );
1156
            }
1157
        }
1158
    }
1159
 
1160
    public IPPermissions() {
1161
    }
1162
 
1163
    public boolean checkAddress ( byte rip[]) {
1164
        boolean allow = false;
1165
        for ( int i=0; i<ip.length; i++ ) {
1166
            if ( ip[i].length == rip.length ) {
1167
                boolean eq = true;
1168
                for ( int j=0; j<rip.length; j++ ) {
1169
                    int k = Math.max( (j+1)*8-mask[i], 0);
1170
                    eq = eq && ( (ip[i][j] & 255)>>k == (rip[j] & 255)>>k );
1171
                }
1172
                if ( eq ) allow = ! deny[i];
1173
            }
1174
        }
1175
        return allow;
1176
    }
1177
 
1178
    public boolean checkAddress ( InetAddress adr ) {
1179
        return checkAddress( adr.getAddress() );
1180
    }
1181
 
1182
    public static String toString(byte ip[]) {
1183
        StringBuilder sb = new StringBuilder();
1184
        if ( ip.length<6 || (ip.length & 1) != 0 ) {
1185
            for (int i=0; i<ip.length; i++ ) {
1186
                if (i>0) sb.append('.');
1187
                sb.append(ip[i] & 255);
1188
            }
1189
        }
1190
        else {
1191
            for (int i=0; i+1<ip.length; i+=2 ) {
1192
                if (i>0) sb.append(':');
1193
                sb.append(Integer.toString( ((ip[i] & 255)<<8) | (ip[i+1] & 255), 16 ) );
1194
            }
1195
        }
1196
 
1197
        return sb.toString();
1198
    }
1199
 
1200
    public static String toString(InetAddress adr) {
1201
        return toString( adr.getAddress() );
1202
    }
1203
 
1204
    public String toString() {
1205
        StringBuilder sb = new StringBuilder();
1206
        for (int i=0; i<ip.length; i++ ) {
1207
            if (i>0) sb.append(',');
1208
            if (deny[i]) sb.append('-');
1209
            sb.append(toString(ip[i])+"/"+mask[i]);
1210
        }
1211
        return sb.toString();
1212
    }
1213
}
1214
 
1215
// *****************************************************************************
1216
// ******* EPDescriptor ********************************************************
1217
// *****************************************************************************
1218
class EPDescriptor {
1219
    private boolean in, bulk;
1220
    public int num;
1221
    public int size;
1222
 
1223
    public EPDescriptor ( boolean p_in, int p_num, boolean p_bulk, int p_size ) {
1224
        in = p_in;
1225
        num = p_num;
1226
        bulk = p_bulk;
1227
        size = p_size;
1228
//      System.out.println((in ? "IN" : "OUT" ) + " EP "+num + ": " + (bulk ? "BULK" : "INT" ) + ", " + size);
1229
    }
1230
 
1231
    public boolean in () {
1232
        return in;
1233
    }
1234
 
1235
    public int num () {
1236
        return num;
1237
    }
1238
 
1239
    public boolean bulk () {
1240
        return bulk;
1241
    }
1242
 
1243
    public int size () {
1244
        return size;
1245
    }
1246
}
1247
 
1248
// *****************************************************************************
1249
// ******* EPDescriptorVector **************************************************
1250
// *****************************************************************************
1251
class EPDescriptorVector extends Vector<EPDescriptor> {
1252
    public EPDescriptor find(int num) {
1253
        for (int i=0; i<size(); i++) {
1254
            if ( elementAt(i).num() == num ) return elementAt(i);
1255
        }
1256
        return null;
1257
    }
1258
}
1259
 
1260
// *****************************************************************************
1261
// ******* DeviceServer ********************************************************
1262
// *****************************************************************************
1263
class DeviceServer {
1264
    public static final int maxConnections = 128;
1265
    public final static SimpleDateFormat msgDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
1266
 
1267
    public static int usbVendorId = ZtexDevice1.ztexVendorId;
1268
    public static int usbProductId = -1;
1269
    public static boolean cypress = true;
1270
    public static int httpPort = 9080;
1271
    public static int socketPort = 9081;
1272
    public static boolean quit = false;
1273
 
1274
    private static Vector<Socket> socketVector = new Vector<Socket>();
1275
    private static boolean verbose = false;
1276
    private static boolean quiet = false;
1277
    private static PrintStream logFile = null;
1278
    private static PrintStream log2File = null;
1279
 
1280
    private static IPPermissions httpPermissions = new IPPermissions();
1281
    private static IPPermissions socketPermissions = new IPPermissions();
1282
    private static String httpBind = null, socketBind = null;
1283
 
1284
    private static ZtexScanBus1 scanBus;
1285
    private static int busIdx[];
1286
    private static int devNum[];
1287
    private static int confNum[];
1288
    private static EPDescriptorVector eps[];
1289
    private static Vector<String> dirnameDB = new Vector<String>();
1290
 
1291
// ******* addSocket ***********************************************************
1292
    public synchronized static void addSocket( Socket socket ) {
1293
        info( "Connection from " + IPPermissions.toString( socket.getInetAddress() ) + " established" );
1294
        socketVector.addElement(socket);
1295
    }
1296
 
1297
// ******* removeSocket ********************************************************
1298
    public synchronized static void removeSocket(Socket socket) {
1299
        info( "Connection from " + IPPermissions.toString( socket.getInetAddress() ) + " closed" );
1300
        socketVector.remove(socket);
1301
    }
1302
 
1303
// ******* httpPermissions *****************************************************
1304
    public static IPPermissions httpPermissions() {
1305
        return httpPermissions;
1306
    }
1307
 
1308
// ******* sleep ***************************************************************
1309
    public static void sleep(int ms) {
1310
        try {
1311
            Thread.sleep(ms);
1312
        }
1313
        catch ( InterruptedException e ) {
1314
        }
1315
    }
1316
 
1317
// ******* info ****************************************************************
1318
    public synchronized static void info (String msg) {
1319
        if ( verbose ) System.err.println( msg );
1320
        if ( log2File != null ) log2File.println( msgDateFormat.format(new Date()) + ": " + msg );
1321
    }
1322
 
1323
// ******* error ***************************************************************
1324
    public synchronized static void error (String msg) {
1325
        if ( ! quiet ) System.err.println( msg );
1326
        if ( logFile != null ) logFile.println( msgDateFormat.format(new Date()) + ": " + msg );
1327
        if ( log2File != null ) log2File.println( msgDateFormat.format(new Date()) + ": " + msg );
1328
    }
1329
 
1330
// ******* getDirnameNum *******************************************************
1331
    public static int getDirnameIdx ( String dirname ) {
1332
        if ( dirname == null ) return -1;
1333
        for ( int i=0; i<dirnameDB.size(); i++ ) {
1334
            if ( dirname.equals(dirnameDB.elementAt(i)) ) return i;
1335
        }
1336
        dirnameDB.add(dirname);
1337
        info("Found bus \"" +dirname + "\": assigned bus index " + (dirnameDB.size()-1));
1338
        return dirnameDB.size()-1;
1339
    }
1340
 
1341
// ******* scanUSB *************************************************************
1342
    public synchronized static void scanUSB () {
1343
        info("Scanning USB ...");
1344
        scanBus = new ZtexScanBus1( usbVendorId, usbProductId, cypress, false, 1 );
1345
        int n = scanBus.numberOfDevices();
1346
        if ( n > 0 ) {
1347
            busIdx = new int[n];
1348
            devNum = new int[n];
1349
            confNum = new int[n];
1350
            eps = new EPDescriptorVector[n];
1351
            for ( int i=0; i<n; i++ ) {
1352
                Usb_Device dev = scanBus.device(i).dev();
1353
                busIdx[i] = getDirnameIdx( dev.getBus().getDirname() );
1354
                devNum[i] = dev.getDevnum();
1355
                confNum[i] = -1;
1356
                eps[i] = new EPDescriptorVector();
1357
                try {
1358
                    if ( dev.getDescriptor().getBNumConfigurations() < 1 ) throw new Exception();
1359
                    Usb_Config_Descriptor conf = dev.getConfig()[0];
1360
                    confNum[i] = conf.getBConfigurationValue();
1361
                    if ( conf.getBNumInterfaces() < 1 ) throw new Exception();
1362
                    Usb_Interface iface = conf.getInterface()[0];
1363
                    if ( iface.getNumAltsetting() < 1 ) throw new Exception();
1364
                    Usb_Interface_Descriptor desc = iface.getAltsetting()[0];
1365
                    if ( desc.getBNumEndpoints() < 1 ) throw new Exception();
1366
                    Usb_Endpoint_Descriptor epd[] = desc.getEndpoint();
1367
                    for ( int j=0; j<epd.length; j++ ) {
1368
                        int t = epd[j].getBmAttributes() & Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_MASK;
1369
                        if ( t == Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_BULK || t == Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_INTERRUPT )
1370
                            eps[i].addElement(new EPDescriptor(
1371
                                    (epd[j].getBEndpointAddress() & Usb_Endpoint_Descriptor.USB_ENDPOINT_DIR_MASK) != 0,
1372
                                    epd[j].getBEndpointAddress() & Usb_Endpoint_Descriptor.USB_ENDPOINT_ADDRESS_MASK,
1373
                                    t == Usb_Endpoint_Descriptor.USB_ENDPOINT_TYPE_BULK,
1374
                                    epd[j].getWMaxPacketSize()
1375
                                ) );
1376
                    }
1377
                }
1378
                catch (Exception e) {
1379
                }
1380
            }
1381
        }
1382
    }
1383
 
1384
// ******* loadFirmware ********************************************************
1385
    public synchronized static void loadFirmware ( Ztex1v1 ztex, StringBuilder messages, InputStream in, String inName, boolean force, boolean toVolatile, boolean toNonVolatile, boolean eraseEeprom ) throws Exception {
1386
        if ( ztex == null ) return;
1387
        eraseEeprom = eraseEeprom && (! toNonVolatile );
1388
        if ( toVolatile || toNonVolatile ) {
1389
            if ( in == null ) throw new Exception("No firmware defined.");
1390
            ZtexIhxFile1 ihxFile = new ZtexIhxFile1( in, inName );
1391
            if ( toVolatile ) {
1392
                long i = ztex.uploadFirmware( ihxFile, force );
1393
                if ( messages != null ) messages.append("Firmware uploaded to volatile memory: "+i+"ms\n");
1394
                }
1395
            if ( toNonVolatile ) {
1396
                 long i = ztex.eepromUpload( ihxFile, force );
1397
                if ( messages != null ) messages.append("Firmware uploaded to non-volatile memory: "+i+"ms\n");
1398
            }
1399
        }
1400
        if ( eraseEeprom ) {
1401
            ztex.eepromDisable();
1402
            if ( messages != null ) messages.append("Firmware in non-volatile memory disabled\n");
1403
        }
1404
    }
1405
 
1406
// ******* loadBitstream *******************************************************
1407
    public synchronized static void loadBitstream ( Ztex1v1 ztex, StringBuilder messages, byte[] buf, String inName, boolean force, boolean toVolatile, boolean toNonVolatile, boolean eraseFlash ) throws Exception {
1408
        if ( ztex == null ) return;
1409
        eraseFlash = eraseFlash && (! toNonVolatile );
1410
        if ( toVolatile || toNonVolatile ) {
1411
            if ( buf == null ) throw new Exception("No firmware defined.");
1412
            if ( toVolatile ) {
1413
                long i = ztex.configureFpga( new ByteArrayInputStream(buf), force, -1 );
1414
                if ( messages != null ) messages.append("Bitstream uploaded to volatile memory: "+i+"ms\n");
1415
                }
1416
            if ( toNonVolatile ) {
1417
                long i = ztex.flashUploadBitstream( new ByteArrayInputStream(buf), -1 );
1418
                if ( messages != null ) messages.append("Bitstream uploaded to non-volatile memory: "+i+"ms\n");
1419
            }
1420
        }
1421
        if ( eraseFlash ) {
1422
            ztex.flashResetBitstream();
1423
            if ( messages != null ) messages.append("Bitstream in non-volatile memory disabled\n");
1424
        }
1425
    }
1426
 
1427
    public synchronized static void loadBitstream ( Ztex1v1 ztex, StringBuilder messages, InputStream in, String inName, boolean force, boolean toVolatile, boolean toNonVolatile, boolean eraseFlash ) throws Exception {
1428
        byte buf[] = new byte[65536];
1429
        ByteArrayOutputStream out = new ByteArrayOutputStream();
1430
        int i;
1431
        do {
1432
            i=in.read(buf);
1433
            if (i>0) out.write(buf,0,i);
1434
        } while (i>0);
1435
        loadBitstream(ztex, messages, out.toByteArray(), inName, force, toVolatile, toNonVolatile, eraseFlash);
1436
    }
1437
 
1438
// ******* claim ***************************************************************
1439
    public synchronized static void claim ( Ztex1v1 ztex, StringBuilder messages ) {
1440
        int c = 1;
1441
        for (int i=0; i<scanBus.numberOfDevices(); i++ ) {
1442
            if ( scanBus.device(i) == ztex.dev() ) {
1443
                c=confNum[i];
1444
            }
1445
        }
1446
        try {
1447
            ztex.setConfiguration(c);
1448
        }
1449
        catch ( UsbException e ) {
1450
            if (messages!=null) messages.append("Warning: "+e.getLocalizedMessage()+'\n');
1451
        }
1452
 
1453
        try {
1454
            ztex.claimInterface(0);
1455
        }
1456
        catch ( UsbException e ) {
1457
            if (messages!=null) messages.append("Warning: "+e.getLocalizedMessage()+'\n');
1458
        }
1459
    }
1460
 
1461
// ******* release *************************************************************
1462
    public synchronized static void release ( Ztex1v1 ztex ) {
1463
        if (ztex!=null) ztex.releaseInterface(0);
1464
    }
1465
 
1466
// ******* epUpload ************************************************************
1467
    public synchronized static void epUpload ( Ztex1v1 ztex, EPDescriptor ep, InputStream in, StringBuilder messages ) throws Exception {
1468
        if ( ztex == null ) return;
1469
        if ( ep == null || ep.in() ) throw new UsbException(ztex.dev().dev(), "No valid endpoint defined");
1470
 
1471
        claim(ztex,messages);
1472
 
1473
        int bufSize = ep.num()==1 ? 64 : 256*1024;
1474
        byte buf[] = new byte[bufSize];
1475
        int r,i;
1476
        do {
1477
            i = r = Math.max(in.read(buf),0);
1478
            if ( i>0 ) i = ep.bulk() ? LibusbJava.usb_bulk_write(ztex.handle(), ep.num, buf, r, 1000) : LibusbJava.usb_interrupt_write(ztex.handle(), ep.num, buf, r, 1000);
1479
        } while (r>0 && r==i);
1480
 
1481
        if (i<0) throw new UsbException("Write error: " + LibusbJava.usb_strerror());
1482
        if ( r!=i ) throw new UsbException("Write error: wrote " + i + " bytes instead of " + r + " bytes");
1483
    }
1484
 
1485
// ******* epDownload **********************************************************
1486
    public synchronized static void epDownload ( Ztex1v1 ztex, EPDescriptor ep, OutputStream out, int maxSize, StringBuilder messages ) throws Exception {
1487
        if ( ztex == null ) return;
1488
        if ( ep == null || ! ep.in() ) throw new UsbException(ztex.dev().dev(), "No valid endpoint defined");
1489
        if ( maxSize < 1 ) maxSize = Integer.MAX_VALUE;
1490
 
1491
        claim(ztex,messages);
1492
 
1493
        int bufSize = ep.num()==1 ? 64 : 256*1024;
1494
        byte buf[] = new byte[bufSize];
1495
        int r,i;
1496
        int j=0;
1497
        do {
1498
            r = Math.min(bufSize,maxSize);
1499
            maxSize-=r;
1500
            i = ep.bulk() ? LibusbJava.usb_bulk_read(ztex.handle(), 0x80 | ep.num, buf, r, j==0 ? 5000 : 1000) : LibusbJava.usb_interrupt_read(ztex.handle(), 0x80 | ep.num, buf, r, 1000);
1501
            if (i>0) out.write(buf,0,i);
1502
//          System.out.println("r: "+i);
1503
            j++;
1504
        } while (maxSize>0 && r==i);
1505
 
1506
        if (i<0) throw new UsbException("Read error: " + LibusbJava.usb_strerror());
1507
    }
1508
 
1509
// ******* numberOfDevices *****************************************************
1510
    public synchronized static int numberOfDevices() {
1511
        return scanBus.numberOfDevices();
1512
    }
1513
 
1514
// ******* device **************************************************************
1515
    public synchronized static ZtexDevice1 device (int i) throws IndexOutOfBoundsException {
1516
        return scanBus.device(i);
1517
    }
1518
 
1519
// ******* findDevice **********************************************************
1520
    public synchronized static ZtexDevice1 findDevice (int b, int d) {
1521
        int n = numberOfDevices();
1522
        for ( int i=0; i<n; i++ ) {
1523
            try {
1524
                if ( busIdx[i]==b && devNum[i]==d ) return scanBus.device(i);
1525
            } catch ( IndexOutOfBoundsException e ) {
1526
            }
1527
        }
1528
        return null;
1529
    }
1530
 
1531
// ******* busIdx **************************************************************
1532
    public synchronized static int busIdx (int i) throws IndexOutOfBoundsException {
1533
        if ( i<0 || i>=busIdx.length) throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (busIdx.length-1) );
1534
        return busIdx[i];
1535
    }
1536
 
1537
// ******* devNum **************************************************************
1538
    public synchronized static int devNum (int i) throws IndexOutOfBoundsException {
1539
        if ( i<0 || i>=devNum.length) throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (devNum.length-1) );
1540
        return devNum[i];
1541
    }
1542
 
1543
// ******* getEps  *************************************************************
1544
    public synchronized static EPDescriptorVector getEps (int b, int d) {
1545
        int n = numberOfDevices();
1546
        for ( int i=0; i<n; i++ ) {
1547
            try {
1548
                if ( busIdx[i]==b && devNum[i]==d ) return eps[i];
1549
            } catch ( IndexOutOfBoundsException e ) {
1550
            }
1551
        }
1552
        return null;
1553
    }
1554
 
1555
// ******* main ****************************************************************
1556
    public static void main (String args[]) {
1557
        LibusbJava.usb_init();
1558
 
1559
        final String helpMsg = new String (
1560
                        "Global parameters:\n"+
1561
                        "    -nc              Do not scan for Cypress EZ-USB devices without ZTEX firmware\n"+
1562
                        "    -id <VID> <PID>  Scan for devices with given Vendor ID and Product ID\n"+
1563
                        "    -sp <port>       Port number for the socket interface (default: 9081; <0: disabled)\n"+
1564
                        "    -hp <port>       Port number for the HTTP interface (default: 9080; <0: disabled)\n"+
1565
                        "    -sa [-]<address>[/<mask>][,...]  Allow (permit if '-' is given) HTTP connection from this address(es),\n"+
1566
                        "                     <mask> 24 is equivalent to 255.255.255.0, default: 127.0.0.1\n"+
1567
                        "    -ha [-]<address>[/<mask>][,...]  Allow (permit if '-' is given) HTTP connection from this address(es),\n"+
1568
                        "                     <mask> 24 is equivalent to 255.255.255.0, default: 127.0.0.1\n"+
1569
                        "    -sb <address>    Bind socket server to this address (default: listen on all interfaces)\n"+
1570
                        "    -hb <address>    Bind HTTP server to this address (default: listen on all interfaces)\n"+
1571
                        "    -v               Be verbose\n"+
1572
                        "    -q               Be quiet\n"+
1573
                        "    -l               Log file\n"+
1574
                        "    -l2              Verbose log file\n"+
1575
                        "    -h               Help" );
1576
 
1577
// process parameters
1578
        try {
1579
 
1580
            for (int i=0; i<args.length; i++ ) {
1581
                if ( args[i].equals("-nc") ) {
1582
                    cypress = false;
1583
                }
1584
                else if ( args[i].equals("-id") ) {
1585
                    i++;
1586
                    try {
1587
                        if (i>=args.length)
1588
                            throw new Exception();
1589
                        usbVendorId = Integer.decode( args[i] );
1590
                    }
1591
                    catch (Exception e) {
1592
                        System.err.println("Error: Vendor ID expected after -id");
1593
                        System.err.println(helpMsg);
1594
                        System.exit(1);
1595
                    }
1596
                    i++;
1597
                    try {
1598
                        if (i>=args.length)
1599
                            throw new Exception();
1600
                        usbProductId = Integer.decode( args[i] );
1601
                    }
1602
                    catch (Exception e) {
1603
                        System.err.println("Error: Product ID expected after -id <VID>");
1604
                        System.err.println(helpMsg);
1605
                        System.exit(1);
1606
                    }
1607
                }
1608
                else if ( args[i].equals("-hp") ) {
1609
                    i++;
1610
                    try {
1611
                        if (i>=args.length)
1612
                            throw new Exception();
1613
                        httpPort = Integer.parseInt( args[i] );
1614
                    }
1615
                    catch (Exception e) {
1616
                        System.err.println("Error: Port number expected after -hp");
1617
                        System.err.println(helpMsg);
1618
                        System.exit(1);
1619
                    }
1620
                }
1621
                else if ( args[i].equals("-sp") ) {
1622
                    i++;
1623
                    try {
1624
                        if (i>=args.length)
1625
                            throw new Exception();
1626
                        socketPort = Integer.parseInt( args[i] );
1627
                    }
1628
                    catch (Exception e) {
1629
                        System.err.println("Error: Port number expected after -sp");
1630
                        System.err.println(helpMsg);
1631
                        System.exit(1);
1632
                    }
1633
                }
1634
                else if ( args[i].equals("-ha") ) {
1635
                    i++;
1636
                    try {
1637
                        if (i>=args.length)
1638
                            throw new Exception("Argument expected after -ha");
1639
                        httpPermissions = new IPPermissions( args[i] );
1640
                    }
1641
                    catch (Exception e) {
1642
                        System.err.println("Error parsing HTTP permissions:");
1643
                        System.exit(1);
1644
                    }
1645
                }
1646
                else if ( args[i].equals("-sa") ) {
1647
                    i++;
1648
                    try {
1649
                        if (i>=args.length)
1650
                            throw new Exception("Argument expected after -hs");
1651
                        socketPermissions = new IPPermissions( args[i] );
1652
                    }
1653
                    catch (Exception e) {
1654
                        System.err.println("Error parsing socket permissions:");
1655
                        System.exit(1);
1656
                    }
1657
                }
1658
                else if ( args[i].equals("-hb") ) {
1659
                    i++;
1660
                    try {
1661
                        if (i>=args.length)
1662
                            throw new Exception("Argument expected after -hb");
1663
                        httpBind = args[i];
1664
                    }
1665
                    catch (Exception e) {
1666
                        System.err.println("Error parsing HTTP permissions:");
1667
                        System.exit(1);
1668
                    }
1669
                }
1670
                else if ( args[i].equals("-sb") ) {
1671
                    i++;
1672
                    try {
1673
                        if (i>=args.length)
1674
                            throw new Exception("Argument expected after -sb");
1675
                        socketBind = args[i];
1676
                    }
1677
                    catch (Exception e) {
1678
                        System.err.println("Error parsing HTTP permissions:");
1679
                        System.exit(1);
1680
                    }
1681
                }
1682
                else if ( args[i].equals("-hb") ) {
1683
                    i++;
1684
                    try {
1685
                        if (i>=args.length)
1686
                            throw new Exception("Argument expected after -hb");
1687
                        httpBind = args[i];
1688
                    }
1689
                    catch (Exception e) {
1690
                        System.err.println("Error parsing HTTP permissions:");
1691
                        System.exit(1);
1692
                    }
1693
                }
1694
                else if ( args[i].equals("-v") ) {
1695
                    verbose = true;
1696
                }
1697
                else if ( args[i].equals("-q") ) {
1698
                    quiet = true;
1699
                }
1700
                else if ( args[i].equals("-l") ) {
1701
                    i++;
1702
                    if (i>=args.length) {
1703
                        System.err.println("Error: File name expected after `-l'");
1704
                        System.err.println(helpMsg);
1705
                        System.exit(1);
1706
                    }
1707
                    try {
1708
                        logFile = new PrintStream ( new FileOutputStream ( args[i], true ), true );
1709
                    }
1710
                    catch (Exception e) {
1711
                        System.err.println("Error: File name expected after `-l': "+e.getLocalizedMessage() );
1712
                        System.err.println(helpMsg);
1713
                        System.exit(1);
1714
                    }
1715
                }
1716
                else if ( args[i].equals("-l2") ) {
1717
                    i++;
1718
                    if (i>=args.length) {
1719
                        System.err.println("Error: File name expected after `-l2'");
1720
                        System.err.println(helpMsg);
1721
                        System.exit(1);
1722
                    }
1723
                    try {
1724
                        log2File = new PrintStream ( new FileOutputStream ( args[i], true ), true );
1725
                    }
1726
                    catch (Exception e) {
1727
                        System.err.println("Error: File name expected after `-l2': "+e.getLocalizedMessage() );
1728
                        System.err.println(helpMsg);
1729
                        System.exit(1);
1730
                    }
1731
                }
1732
                else if ( args[i].equals("-h") ) {
1733
                    System.err.println(helpMsg);
1734
                    System.exit(0);
1735
                }
1736
                else {
1737
                    System.err.println("Error: Invalid option: `"+args[i]+"'");
1738
                    System.err.println(helpMsg);
1739
                    System.exit(1);
1740
                }
1741
            }
1742
 
1743
            if ( httpPort < 0 && socketPort < 0 ) {
1744
                error("neither HTTP nor socket interface enabled: exiting");
1745
                System.exit(0);
1746
            }
1747
 
1748
// init USB stuff
1749
            LibusbJava.usb_init();
1750
            LibusbJava.usb_find_busses();
1751
            Usb_Bus bus = LibusbJava.usb_get_busses();
1752
            while ( bus != null ) {
1753
                getDirnameIdx(bus.getDirname());
1754
                bus = bus.getNext();
1755
            }
1756
            scanUSB();
1757
 
1758
// start http server
1759
            HttpServer httpServer = null;
1760
            if ( httpPort > 0 ) {
1761
                error ( "Listening for http connections at port " + httpPort + " from addresses " + httpPermissions ); // not really an error
1762
                httpServer = HttpServer.create( ( httpBind == null ) ? new InetSocketAddress(httpPort) : new InetSocketAddress(InetAddress.getByName(httpBind),httpPort), 0);
1763
                httpServer.createContext("/", new ZtexHttpHandler());
1764
                httpServer.setExecutor(null);
1765
                httpServer.start();
1766
            }
1767
 
1768
// run socket server
1769
            if ( socketPort > 0 ) {
1770
                error ( "Listening for socket connections at port " + socketPort + " from addresses " + socketPermissions ); // not really an error
1771
                ServerSocket ss = (socketBind == null) ?  new ServerSocket ( socketPort, 20 ) : new ServerSocket ( socketPort, 20,  InetAddress.getByName(socketBind));
1772
                ss.setSoTimeout(500);
1773
                while ( ! quit ) {
1774
                    if ( socketVector.size() < maxConnections ) {
1775
                        try {
1776
                            Socket cs = ss.accept();
1777
                            if ( socketPermissions.checkAddress( cs.getInetAddress() ) ) {
1778
                                new SocketThread( cs );
1779
                            }
1780
                            else {
1781
                                info( "Connection from " + IPPermissions.toString( cs.getInetAddress() ) + " refused" );
1782
                            }
1783
                        }
1784
                        catch ( SocketTimeoutException e ) {
1785
                        }
1786
                    }
1787
                    else {
1788
                        sleep(1000);
1789
                    }
1790
                }
1791
            }
1792
            else {
1793
                while ( ! quit ) {
1794
                    sleep(1000);
1795
                }
1796
            }
1797
 
1798
// stop http server
1799
            if ( httpServer!=null ) httpServer.stop(1);
1800
 
1801
        }
1802
        catch (Exception e) {
1803
            error("Error: "+e.getLocalizedMessage() );
1804
        }
1805
   }
1806
 
1807
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.