-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathICT_Protocol.cpp
More file actions
360 lines (300 loc) · 11.7 KB
/
Copy pathICT_Protocol.cpp
File metadata and controls
360 lines (300 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <cstdlib>
#include <string.h>
#include "ICT_Protocol.h"
#include "config.h"
ICT_Protocol::ICT_Protocol():commLineState(ICT_Protocol::COMMUNICATION_STATE_UNKNOWN),
deviceInhibitState(ICT_Protocol::DEVICE_STATE_UNKNOWN),
commErrorCounter(0)
{
}
void ICT_Protocol::die(){
Logger::instance().logFatal("Application exit forced.");
port.serialClose();
std::exit(-1);
}
bool ICT_Protocol::portReadByte(unsigned char &data){
int result;
char readData[1024];
memset(readData, 0x00, sizeof(readData));
if((result=port.serialRead(readData, 1))<0){
stringstream ss;
ss<<result;
Logger::instance().logError("Cannot read serial port "+ss.str());
ss.str(string());
die();
}
if(result==0){
commErrorCounter++;
if(commErrorCounter>COMMUNICATION_DEAD_COUNTER){
commErrorCounter=0;
//TODO inform the application.
commLineState=COMMUNICATION_STATE_BROKEN;
}
return false;
}
commLineState=COMMUNICATION_STATE_ALIVE;
data=(unsigned char)readData[0];
#ifdef _DEBUG
stringstream ss;
ss<<std::hex<<static_cast<unsigned>(data);
Logger::instance().logDebug("Received 0x"+ss.str());
ss.str(std::string());
#endif
return true;
}
bool ICT_Protocol::sendMessage(ICT_Protocol::MessageCodesController messageCode){
unsigned char response[1]={(unsigned char)messageCode};
if(port.serialWrite(response, 1)!=E_OK){
Logger::instance().logFatal("Cannot write to the serial port.");
die();
}
stringstream ss;
unsigned char x=response[0];
ss<<std::hex<<static_cast<unsigned>(x);
time(&sendTime);
Logger::instance().logDebug("Sent 0x"+ss.str());
ss.str(std::string());
return true;
}
void ICT_Protocol::parseInput(unsigned char data){
stringstream ss;
unsigned char buf;
string errorString;
bool accept_bill=false;
switch(data){
case (unsigned char)ICT_Protocol::MSG_DEVICE_POWERUP:
case 0x8f:
Logger::instance().logInfo("Power up message received");
sendMessage(ICT_Protocol::MSG_CTRL_PWRUP_COMMFAIL_RESPONSE);
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_COMM_FAILURE:
Logger::instance().logInfo("Communication failure message received");
//TODO report this
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_ESCROW:
if(portReadByte(buf)){
if(buf<0x40 || buf>0x45){
ss<<static_cast<unsigned>(buf);
Logger::instance().logInfo("Escrow message message error. Received data: "+ss.str());
ss.str(string());
sendMessage(ICT_Protocol::MSG_CTRL_BILL_REQUEST_REJECT);
break;
}
}else{
Logger::instance().logInfo("Escrow message not complete");
break;
}
//Here you can check which values are defined in the config, and which will be accepted based on that
switch(buf){
case 0x40:
ss<<ConfigReader::instance().configValues.bill1_value;
Logger::instance().logInfo("First bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill1_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
case 0x41:
ss<<ConfigReader::instance().configValues.bill2_value;
Logger::instance().logInfo("Second bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill2_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
case 0x42:
ss<<ConfigReader::instance().configValues.bill3_value;
Logger::instance().logInfo("Third bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill3_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
case 0x43:
ss<<ConfigReader::instance().configValues.bill4_value;
Logger::instance().logInfo("Fourth bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill4_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
case 0x44:
ss<<ConfigReader::instance().configValues.bill5_value;
Logger::instance().logInfo("Fifth bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill5_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
case 0x45:
ss<<ConfigReader::instance().configValues.bill6_value;
Logger::instance().logInfo("Sixth bill type inserted - "+ss.str());
if(ConfigReader::instance().configValues.bill6_value<1){
Logger::instance().logWarning("Bill type "+ss.str()+" is disabled in the configuration");
}else{
accept_bill=true;
}
ss.str(std::string());
break;
default:
Logger::instance().logError("Unknown bill type inserted");
break;
}
if(accept_bill){
Logger::instance().logInfo("Accepting the bill");
sendMessage(ICT_Protocol::MSG_CTRL_BILL_REQUEST_ACCEPT);
if(portReadByte(buf)){
parseInput(buf);
}else{
Logger::instance().logWarning("No response on accept command");
}
}else{
Logger::instance().logInfo("Rejecting the bill");
sendMessage(ICT_Protocol::MSG_CTRL_BILL_REQUEST_REJECT);
}
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_STATUS_RESP_INHIBIT:
Logger::instance().logDebug("Status reasponse - device is in inhibit state");
deviceInhibitState=DEVICE_STATE_DISABLED;
if(lastDeviceInhibitState!=deviceInhibitState){
//TODO report this to the application
lastDeviceInhibitState=deviceInhibitState;
}
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_STATUS_RESP_ENABLED:
Logger::instance().logDebug("Status reasponse - device is ready to accept the bills");
deviceInhibitState=DEVICE_STATE_ENABLED;
if(lastDeviceInhibitState!=deviceInhibitState){
//TODO report this to the application
lastDeviceInhibitState=deviceInhibitState;
}
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_BILL_ACCEPT_FINISH:
Logger::instance().logDebug("Bill accept finish.");
break;
case (unsigned char)ICT_Protocol::MSG_DEVICE_BILL_ACCEPT_FAILURE:
Logger::instance().logDebug("Bill accept failure");
break;
case 0x20:
errorString="Motor failure";
Logger::instance().logWarning(errorString);
break;
case 0x21:
errorString="Checksum error";
Logger::instance().logWarning(errorString);
break;
case 0x22:
errorString="Bill jam";
Logger::instance().logWarning(errorString);
break;
case 0x23:
errorString="Bill remove";
Logger::instance().logWarning(errorString);
break;
case 0x24:
errorString="Stacker open";
Logger::instance().logWarning(errorString);
break;
case 0x25:
errorString="Sensor problem";
Logger::instance().logWarning(errorString);
break;
case 0x27:
errorString="Bill fish";
Logger::instance().logWarning(errorString);
break;
case 0x28:
errorString="Stacker problem";
Logger::instance().logWarning(errorString);
break;
case 0x29:
errorString="Bill reject";
Logger::instance().logWarning(errorString);
break;
case 0x2A:
errorString="Invalid command";
Logger::instance().logWarning(errorString);
break;
case 0x2E:
errorString="Reserved / Not defined";
Logger::instance().logWarning(errorString);
break;
case 0x2F:
errorString="Error status is exclusion";
Logger::instance().logWarning(errorString);
break;
default:
ss<<static_cast<unsigned>(data);
string err=ss.str();
ss.str(std::string());
Logger::instance().logInfo("Unknown message: "+err);
break;
}
}
bool ICT_Protocol::sendEnableAcceptor()
{
return sendMessage(ICT_Protocol::MSG_CTRL_ENABLE_ACCEPTOR_REQ);
}
bool ICT_Protocol::sendDisableAcceptor()
{
return sendMessage(ICT_Protocol::MSG_CTRL_DISABLE_ACCEPTOR_REQ);
}
bool ICT_Protocol::sendBillAccept()
{
return sendMessage(ICT_Protocol::MSG_CTRL_BILL_REQUEST_ACCEPT);
}
bool ICT_Protocol::sendBillReject()
{
return sendMessage(ICT_Protocol::MSG_CTRL_BILL_REQUEST_REJECT);
}
bool ICT_Protocol::sendResetDevice()
{
return sendMessage(ICT_Protocol::MSG_CTRL_RESET_DEVICE);
}
bool ICT_Protocol::init(string serialPortName)
{
int result=port.serialOpen(serialPortName, 9600, SerialPort::HANDSHAKE_NONE, SerialPort::SERIAL_PARITY_EVEN, SerialPort::STOP_BIT_ONE);
if(result!=E_OK){
Logger::instance().logFatal("Cannot open serial port "+serialPortName);
return false;
}else{
Logger::instance().logInfo("Port "+serialPortName+" opened");
}
commLineState=COMMUNICATION_STATE_UNKNOWN;
deviceInhibitState=DEVICE_STATE_UNKNOWN;
return true;
}
void ICT_Protocol::run(){
time(&sendTime);
currentTime=sendTime+DEVICE_POLL_INTERVAL;
while(1){
unsigned char data;
if(portReadByte(data)){
parseInput(data);
}
if(lastCommLineState!=commLineState){
//TODO report to the application
lastCommLineState=commLineState;
if(commLineState==COMMUNICATION_STATE_ALIVE){
Logger::instance().logInfo("Communication to the device is alive");
}else if(commLineState==COMMUNICATION_STATE_BROKEN){
Logger::instance().logWarning("No communication to the device");
}
}
usleep(5000);
time(¤tTime);
if(difftime(currentTime, sendTime)>DEVICE_POLL_INTERVAL){
sendMessage(ICT_Protocol::MSG_CTRL_STATUS_REQ);
}
}
}