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

Subversion Repositories usb_fpga_2_14

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

powered by: WebSVN 2.1.0

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