9
9
import com .microchip .mplab .mdbcore .simulator .PeripheralSet ;
10
10
import java .io .BufferedInputStream ;
11
11
import java .io .BufferedOutputStream ;
12
- import java .io .BufferedReader ;
13
- import java .io .BufferedWriter ;
14
12
import java .io .File ;
15
13
import java .util .LinkedList ;
16
14
import java .io .FileInputStream ;
15
+ import java .io .FileNotFoundException ;
17
16
import java .io .IOException ;
18
- import java .io .InputStream ;
19
- import java .io .InputStreamReader ;
20
- import java .io .OutputStreamWriter ;
21
- import java .io .PrintWriter ;
22
17
import java .net .Socket ;
23
18
import org .openide .util .lookup .ServiceProvider ;
24
19
import java .util .Map ;
@@ -30,8 +25,6 @@ public class Spi implements Peripheral {
30
25
String SPI_NUM ; // SPI Name (eg: SPI1, SPI2, etc...)
31
26
String SPI_BUFF ; // Respective SPI BUFFER SFR
32
27
String SPI_STAT ; // SPI STAT buffer
33
- String REQUEST_FILE ; // Request File Path (eg: "~/uartfolder/req"
34
- String RESPONSE_FILE ; // Response File Path (eg: "~/uartfolder/res"
35
28
36
29
static Spi instance ;
37
30
SimulatorDataStore DS ;
@@ -41,9 +34,10 @@ public class Spi implements Peripheral {
41
34
SFR sfrBuff ;
42
35
SFR sfrStat ;
43
36
SFR sfrTX ;
44
- SFRSet sfrs ;
37
+ SpiObserver spiMonitor ;
45
38
46
39
LinkedList <Byte > bytes = new LinkedList <Byte >();
40
+ Socket reqSocket ;
47
41
BufferedOutputStream request ;
48
42
BufferedInputStream response ;
49
43
Yaml yaml = new Yaml ();
@@ -57,28 +51,43 @@ public class Spi implements Peripheral {
57
51
boolean injectedFlag = false ;
58
52
boolean systemBooted = false ;
59
53
String tempStr = "" ;
54
+ FileInputStream conf ;
55
+ Map config ;
60
56
57
+ public Spi () {
58
+ spiMonitor = new SpiObserver ();
59
+ yaml = new Yaml ();
60
+ }
61
+
61
62
@ Override
62
63
public boolean init (SimulatorDataStore DS ) {
63
- // Initialize DS
64
64
this .DS = DS ;
65
-
66
- // Initialize messageHandler
65
+ SFRSet sfrs ;
67
66
messageHandler = DS .getMessageHandler ();
68
67
69
68
// Initialize instance variables
70
69
try {
71
- FileInputStream conf = new FileInputStream (new File ("spiconfig.yml" ));
72
- Map config = (Map ) yaml .load (conf );
70
+ conf = new FileInputStream (new File ("spiconfig.yml" ));
71
+ config = (Map ) yaml .load (conf );
73
72
SPI_NUM = config .get ("spiNum" ).toString ();
74
73
SPI_BUFF = config .get ("spiBuff" ).toString ();
75
74
SPI_STAT = config .get ("spiStat" ).toString ();
76
- REQUEST_FILE = config .get ("requestFile" ).toString ();
77
- RESPONSE_FILE = config .get ("responseFile" ).toString ();
78
- } catch (Exception e ) {
75
+ } catch (FileNotFoundException e ) {
76
+ messageHandler .outputError (e );
77
+ messageHandler .outputMessage ("Are you sure you placed config.yml in the correct folder?" );
78
+ return false ;
79
+ } catch (SecurityException e ) {
80
+ messageHandler .outputError (e );
81
+ return false ;
82
+ } catch (NullPointerException e ) {
79
83
messageHandler .outputError (e );
80
- // return false;
84
+ messageHandler .outputMessage ("Are you sure you have all of the necessary config fields?" );
85
+ return false ;
86
+ } catch (ClassCastException e ) {
87
+ messageHandler .outputError (e );
88
+ return false ;
81
89
}
90
+
82
91
sfrs = DS .getSFRSet ();
83
92
sfrBuff = sfrs .getSFR (SPI_BUFF );
84
93
sfrStat = sfrs .getSFR (SPI_STAT );
@@ -90,32 +99,17 @@ public boolean init(SimulatorDataStore DS) {
90
99
spiPeriph .deInit ();
91
100
periphSet .removePeripheral (spiPeriph );
92
101
}
93
-
94
- // Setup pipes
95
- /* try {
96
- request = new FileOutputStream(REQUEST_FILE);
97
- response = new FileInputStream(RESPONSE_FILE);
98
- } catch (FileNotFoundException e) {
99
- messageHandler.outputMessage("Exception in init: " + e);
100
- return false;
101
- } */
102
102
103
103
// Setup Sockets
104
- try {
105
- Socket reqSocket = new Socket ("localhost" , 5555 );
106
- request = new BufferedOutputStream (reqSocket .getOutputStream ());
107
- response = new BufferedInputStream (reqSocket .getInputStream ());
108
- } catch (Exception e ) {
109
- messageHandler .outputError (e );
110
- }
104
+ if (!openSockets ()) {
105
+ return false ;
106
+ }
111
107
112
108
// Add observers
113
- SpiObserver obs = new SpiObserver ();
114
- sfrBuff .addObserver (obs );
109
+ sfrBuff .addObserver (spiMonitor );
115
110
116
111
messageHandler .outputMessage ("External Peripheral Initialized: SPI" );
117
- instance = this ;
118
-
112
+
119
113
// Add peripheral to list and return true
120
114
DS .getPeripheralSet ().addToActivePeripheralList (this );
121
115
return true ;
@@ -154,16 +148,20 @@ public void reset() {
154
148
155
149
@ Override
156
150
public void update () {
151
+ byte [] bytes ;
152
+ if (spiMonitor .changed ()) {
153
+ output ();
154
+ }
157
155
if (cycleCount > 100000 ) {
158
156
systemBooted = true ;
159
157
}
160
158
try {
161
159
if (sendFlag ) {
162
160
injectedFlag = true ;
163
161
sendFlag = false ;
164
- long readByte = response .read ();
162
+ int readByte = response .read ();
165
163
byte b = (byte ) readByte ;
166
- if (( int ) b == -1 ) {
164
+ if (readByte == -1 ) {
167
165
messageHandler .outputMessage ("End of Stream" );
168
166
System .exit (0 );
169
167
} else {
@@ -206,7 +204,33 @@ public void output() {
206
204
}
207
205
}
208
206
209
- public static Spi get () {
210
- return instance ;
207
+ public boolean openSockets () {
208
+ try {
209
+ reqSocket = new Socket ("localhost" , 5555 );
210
+ } catch (IOException e ) {
211
+ messageHandler .outputError (e );
212
+ messageHandler .outputMessage ("Failed to open socket. Is there an external listener running?" );
213
+ return false ;
214
+ } catch (SecurityException e ) {
215
+ messageHandler .outputError (e );
216
+ return false ;
217
+ } catch (IllegalArgumentException e ) {
218
+ messageHandler .outputError (e );
219
+ messageHandler .outputMessage ("Provided port is outside of valid values (0-65535)." );
220
+ return false ;
221
+ } catch (NullPointerException e ) {
222
+ messageHandler .outputError (e );
223
+ return false ;
224
+ }
225
+
226
+ try {
227
+ request = new BufferedOutputStream (reqSocket .getOutputStream ());
228
+ response = new BufferedInputStream (reqSocket .getInputStream ());
229
+ } catch (IOException e ) {
230
+ messageHandler .outputError (e );
231
+ messageHandler .outputMessage ("Failed to open Req/Res streams." );
232
+ return false ;
233
+ }
234
+ return true ;
211
235
}
212
236
}
0 commit comments