OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [icapif/] [IcapInterface.java] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
/**
2
 *
3
 */
4
package edu.byu.cc.plieber.fpgaenet.icapif;
5
 
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.net.InetAddress;
12
import java.util.ArrayList;
13
 
14
import edu.byu.cc.plieber.fpgaenet.fcp.FCPException;
15
import edu.byu.cc.plieber.fpgaenet.fcp.FCPProtocol;
16
 
17
/**
18
 * @author plieber
19
 *
20
 * @version 0.1
21
 */
22
public class IcapInterface {
23
 
24
        protected FCPProtocol fcpprotocol;
25
 
26
        protected int icapWritePort = 3;
27
        protected int icapReadPort = 4;
28
 
29
        /**
30
         * Create IcapInterface, attached to the given FCP layer, with the default
31
         * FCP ports for the ICAP functions.
32
         *
33
         * @param fcpprotocol
34
         */
35
        public IcapInterface(FCPProtocol fcpprotocol) {
36
                this.init(fcpprotocol, 3, 4);
37
        }
38
 
39
        /**
40
         * Create IcapInterface, attached to the given FCP layer, with the given
41
         * FCP ports for the ICAP functions.
42
         *
43
         * @param fcpprotocol
44
         * @param icapWritePort
45
         * @param icapReadPort
46
         */
47
        public IcapInterface(FCPProtocol fcpprotocol, int icapWritePort,
48
                        int icapReadPort) {
49
                this.init(fcpprotocol, icapWritePort, icapReadPort);
50
        }
51
 
52
        /**
53
         * Initialize this object
54
         * @param fcpprotocol
55
         * @param icapWritePort
56
         * @param icapReadPort
57
         */
58
        private void init(FCPProtocol fcpprotocol, int icapWritePort,
59
                        int icapReadPort) {
60
                this.fcpprotocol = fcpprotocol;
61
                this.icapReadPort = icapReadPort;
62
                this.icapWritePort = icapWritePort;
63
        }
64
 
65
        @Override
66
        public String toString() {
67
                return "ICAP Interface< " + this.fcpprotocol.toString() + " >";
68
        }
69
 
70
        /**
71
         * Send a list of bytes to the ICAP.
72
         * @param bytes
73
         */
74
        public void sendIcapData(java.util.List<Byte> bytes) {
75
                try {
76
                        this.fcpprotocol.send(this.icapWritePort, bytes, bytes.size());
77
                } catch (FCPException e) {
78
                        // TODO Auto-generated catch block
79
                        e.printStackTrace();
80
                }
81
        }
82
 
83
        /**
84
         * Send an array of byte to the ICAP.
85
         * @param bytes
86
         * @throws FCPException
87
         */
88
        public void sendIcapData(byte[] bytes, int numBytes) throws FCPException {
89
                byte[] writereg = new byte[1024];
90
                int offset = 0;
91
                int numRead = 0;
92
                while (offset < numBytes) {
93
                        for (int i = 0; i < 1024 && offset < numBytes; i++, offset++) {
94
                                writereg[i] = bytes[offset];
95
                                numRead++;
96
                        }
97
                        this.fcpprotocol.send(3, writereg, numRead);
98
                }
99
        }
100
 
101
        public void sendIcapFile(String fileName) throws FCPException {
102
                try {
103
                        File file = new File(fileName);
104
                        InputStream is = new FileInputStream(file);
105
                        long length = file.length();
106
                        if (length > Integer.MAX_VALUE) {
107
 
108
                        }
109
 
110
                        byte[] writereg = new byte[1024];
111
                        int offset = 0;
112
                        int numRead = 0;
113
                        while (offset < file.length()
114
                                        && (numRead = is.read(writereg, 0, 1024)) >= 0) {
115
                                offset += numRead;
116
                                try {
117
                                        fcpprotocol.send(3, writereg, numRead);
118
                                } catch (FCPException e) {
119
                                        // TODO Auto-generated catch block
120
                                        e.printStackTrace();
121
                                }
122
                        }
123
                } catch (FileNotFoundException e) {
124
                        throw new FCPException("File not found: " + fileName);
125
                } catch (IOException e) {
126
                        throw new FCPException("Error reading file: " + fileName);
127
                }
128
        }
129
 
130
        /**
131
         * Send a data request for ICAP data from the ICAP controller.
132
         * @param numBytes The number of bytes expected at the next receiveIcapData function call.
133
         * @throws FCPException
134
         */
135
        public void requestIcapData(int numBytes) throws FCPException {
136
                byte[] read4 = new byte[] { (byte) ((numBytes >> 8) & 0xff), (byte) (numBytes & 0xff) };
137
                this.fcpprotocol.send(this.icapReadPort, read4, read4.length);
138
                this.fcpprotocol.sendDataRequest(this.icapReadPort, numBytes);
139
        }
140
 
141
        /**
142
         * Receive a list of bytes from the ICAP.
143
         * @return List of bytes returned from the ICAP.
144
         */
145
        public ArrayList<Byte> receiveIcapData() {
146
                ArrayList<Byte> ret = new ArrayList<Byte>();
147
                byte[] bytes = this.fcpprotocol.getDataResponse();
148
                for (int i = 0; i < bytes.length; i++) {
149
                        ret.add(bytes[i]);
150
                }
151
                return ret;
152
        }
153
 
154
        /**
155
         * Read a certain number of bytes from the ICAP. This function encapsulates calls to both requestIcapData and receiveIcapData.
156
         * @param numBytes The number of bytes expected
157
         * @return List of bytes returned from the ICAP
158
         * @throws FCPException
159
         */
160
        public ArrayList<Byte> read(int numBytes) throws FCPException {
161
                this.requestIcapData(numBytes);
162
                return this.receiveIcapData();
163
        }
164
}

powered by: WebSVN 2.1.0

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