-
Notifications
You must be signed in to change notification settings - Fork 8
/
MdbBillValidator.cpp
276 lines (233 loc) · 5.04 KB
/
MdbBillValidator.cpp
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
#include "MdbBillValidator.h"
MdbMaster master;
bool _setupInfoRetrieved;
unsigned int _featureLevel;
unsigned int _countryCode;
unsigned int _billScaleFactor;
unsigned int _decimalPlaces;
unsigned int _stackerCapacity;
unsigned int _billSecurityLevels;
bool _escrowEnabled;
unsigned char _billTypeCredit[16];
MdbBillValidator::MdbBillValidator()
{
master = MdbMaster();
_setupInfoRetrieved = false;
}
// Send an acknowledgement.
void MdbBillValidator::SendAck()
{
master.SendAck();
}
// Send a retransmit request.
void MdbBillValidator::SendRet()
{
master.SendRet();
}
// Send a negative acknowledgement.
void MdbBillValidator::SendNak()
{
master.SendNak();
}
// Send a reset command to the bill validator. There is no response to
// this command.
void MdbBillValidator::SoftReset()
{
master.SendCommand(BILL_ADDR, RESET);
_setupInfoRetrieved = false;
}
int MdbBillValidator::GetSetup()
{
master.SendCommand(BILL_ADDR, SETUP);
unsigned char response[MAX_MSG_SIZE];
unsigned int numBytesReturned;
master.GetResponse(response, &numBytesReturned);
if (response[0] == NAK || numBytesReturned == 0)
{
// Negative Acknowledgment
return -1;
}
// Make sure the number of bytes returned is in the correct range.
// There should be between 11 and 27 (inclusive bytes) depending on
// how many bill types are supported.
if (numBytesReturned < 11 || numBytesReturned > 27)
{
return -2;
}
// Read and save the setup information.
_featureLevel = response[0];
_countryCode = (((unsigned int) response[1]) << 8) | ((unsigned int) response[2]);
_billScaleFactor = (((unsigned int) response[3]) << 8) | ((unsigned int) response[4]);
_decimalPlaces = response[5];
_stackerCapacity = (((unsigned int) response[6]) << 8) | ((unsigned int) response[7]);
_billSecurityLevels = (((unsigned int) response[8]) << 8) | ((unsigned int) response[9]);
_escrowEnabled = response[10] == 0xFF;
// Get the value of each bill type.
for (int i = 0; i < 16; i++)
{
_billTypeCredit[i] = response[11 + i];
}
_setupInfoRetrieved = true;
return 0;
}
int MdbBillValidator::SetSecurity(unsigned int securitySettings)
{
unsigned char securityBits[2];
securityBits[0] = securitySettings && 0xFF;
securityBits[1] = (securitySettings >> 8) && 0xFF;
master.SendCommand(BILL_ADDR, SECURITY, securityBits, 2);
unsigned char response[MAX_MSG_SIZE];
unsigned int numBytesReturned;
master.GetResponse(response, &numBytesReturned);
if (numBytesReturned == 0)
{
// No data returned.
return -1;
}
if (numBytesReturned > 1)
{
// The data received doesn't make any sense...
return -2;
}
if (response[0] == NAK)
{
// Negative acknowledgement received.
return -3;
}
if (response[0] == ACK)
{
return 0;
}
// Unidentifiable response. Needs more debugging.
return -4;
}
int MdbBillValidator::Poll()
{
master.SendCommand(BILL_ADDR, POLL);
unsigned char response[MAX_MSG_SIZE];
unsigned int numBytesReturned = 0;
master.GetResponse(response, &numBytesReturned);
if (response[0] == NAK || numBytesReturned == 0)
{
// Negative Acknowledgment
return -1;
}
return 0;
}
// Getter method for retrieving the feature level of the currently
// connected bill acceptor/validator. This method must be called after
// setup has already been called, otherwise the feature level will not
// be known and this method will return -1 to indicate an error.
unsigned int MdbBillValidator::FeatureLevel()
{
if(_setupInfoRetrieved)
{
return _featureLevel;
}
else
{
return -1;
}
}
unsigned int MdbBillValidator::CountryCode()
{
if(_setupInfoRetrieved)
{
return _countryCode;
}
else
{
return -1;
}
}
unsigned int MdbBillValidator::BillScaleFactor()
{
if(_setupInfoRetrieved)
{
return _billScaleFactor;
}
else
{
return -1;
}
}
unsigned int MdbBillValidator::DecimalPlaces()
{
if(_setupInfoRetrieved)
{
return _decimalPlaces;
}
else
{
return -1;
}
}
unsigned int MdbBillValidator::StackerCapacity()
{
if(_setupInfoRetrieved)
{
return _stackerCapacity;
}
else
{
return -1;
}
}
unsigned int MdbBillValidator::BillSecurityLevels()
{
if(_setupInfoRetrieved)
{
return _billSecurityLevels;
}
else
{
return -1;
}
}
bool MdbBillValidator::EscrowEnabled()
{
if(_setupInfoRetrieved)
{
return _escrowEnabled;
}
else
{
return false;
}
}
unsigned char* MdbBillValidator::BillTypeCredit()
{
if(_setupInfoRetrieved)
{
return _billTypeCredit;
}
else
{
return NULL;
}
}
String MdbBillValidator::ToString()
{
String result;
if (_setupInfoRetrieved)
{
result = "Feature Level: " + String(_featureLevel)
+ "\nCountryCode: " + String(_countryCode)
+ "\nBillScaleFactor: " + String(_billScaleFactor)
+ "\nDecimalPlaces: " + String(_decimalPlaces)
+ "\nStackerCapacity: " + String(_stackerCapacity)
+ "\nBillSecurityLevels: " + String(_billSecurityLevels)
+ "\nEscrowEnabled: " + String(_escrowEnabled)
+ "\nBillTypes:";
// Get each bill type.
//for (int i = 0; i < 16; i++)
//{
//result += " " + _billTypeCredit[i];
//}
}
else
{
result = "Configuration of bill validator unknown. Run GetSetup()";
}
return result;
}