-
Notifications
You must be signed in to change notification settings - Fork 44
/
MDB_MasterThroughPC.ino
277 lines (260 loc) · 6.77 KB
/
MDB_MasterThroughPC.ino
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
/*
* MDB Vending Machine Controller emulation environment
* for Level 01 Cashless Devices
* Author: Novoselov I.E.
* Email: [email protected]
*/
#include <SoftwareSerial.h>
#include <USART.h>
#include <MDB.h>
// SoftwareSerial pins for debugging
#define RX_DEBUG_PIN 3
#define TX_DEBUG_PIN 2
// Debug object
SoftwareSerial Debug(RX_DEBUG_PIN, TX_DEBUG_PIN);
void setup() {
Debug.begin(9600);
USART_Init(9600);
sei();
}
void loop() {
uint8_t recComm = 0;
uint8_t bytesAvailable = 0;
char new_session[] = "=====NEW SESSION=====";
if (bytesAvailable = Debug.available())
{
recComm = Debug.read();
Debug.println();
Debug.println(new_session);
Debug.print(F("Bytes available : 0d"));
Debug.println(bytesAvailable, DEC);
Debug.print(F("Incoming command : 0x"));
Debug.println(recComm, HEX);
}
switch(recComm)
{
case 0x30 : vmcReset(); break;
case 0x31 : vmcSetup(); break;
case 0x32 : vmcPoll(); break;
case 0x33 : vmcVend(); break;
case 0x34 : vmcReader(); break;
case 0x37 : vmcExpansion(); break;
default : break;
}
}
/*
* Emulates Reset and Initialize command
*/
void vmcReset(void)
{
uint16_t reply_reset = 0;
Debug.println(F("VMC_RESET Call test"));
MDB_Send(VMC_RESET);
while (true)
if (MDB_DataCount() > 0)
{
Debug.print(F("MDB_DataCount : "));
Debug.println(MDB_DataCount());
MDB_Read(&reply_reset);
Debug.print(F("Cashless reply : "));
Debug.print(F("0b"));
Debug.println(reply_reset, HEX);
break;
}
}
/*
* Emulates 2 SETUP commands in a row:
* SETUP - Reader Config Data, then
* SETUP - Max / Min Prices
*/
void vmcSetup(void)
{
uint8_t i; // counter
uint8_t checksum;
uint16_t commd_setup_H[6] = {VMC_SETUP, 0x00, 0x01, 0x0A, 0x04, 0x01};
uint16_t commd_setup_L[6] = {VMC_SETUP, 0x01, 0xF1, 0xF2, 0xF3, 0xF4};
uint16_t reply_setup_H[9];
uint16_t reply_setup_L;
Debug.println(F("VMC_SETUP Call test"));
// Stage 1 -- Reader Config Data
checksum = calcChecksum(commd_setup_H, 6);
for (i = 0; i < 6; ++i)
MDB_Send(commd_setup_H[i]);
MDB_Send(checksum);
Debug.print(F("Reader Config Checksum : 0x"));
Debug.println(checksum, HEX);
while (true)
if (MDB_DataCount() > 8)
{
for (i = 0; i < 9; ++i)
MDB_Read(reply_setup_H + i);
break;
}
Debug.print(F("Incoming Reader Config Data :"));
for (i = 0; i < 9; ++i)
{
Debug.print(F(" 0x"));
// write() cannot hold 16-bit ints, but print() can
Debug.print(reply_setup_H[i], HEX);
// Debug.print(reply_setup_H[i] >> 8, HEX);
// Debug.print(F("_0x"));
// Debug.print(reply_setup_H[i] & 0xFF, HEX);
}
Debug.println();
// Stage 2 -- Max / Min Prices
checksum = calcChecksum(commd_setup_L, 6);
for (i = 0; i < 6; ++i)
MDB_Send(commd_setup_L[i]);
MDB_Send(checksum);
while (true)
{
if (MDB_DataCount() == 0)
continue;
else
{
MDB_Read(&reply_setup_L);
break;
}
}
Debug.print(F("Incoming Max / Min Prices reply : "));
Debug.print(F("0x"));
Debug.print(reply_setup_L, HEX);
Debug.println();
}
/*
* Emulates POLL command
*/
void vmcPoll()
{
uint8_t i; // counter
uint16_t reply;
uint16_t comm_poll = VMC_POLL;
Debug.println(F("VMC_POLL Call test"));
MDB_Send(comm_poll);
Debug.print(F("Incoming data :"));
while (true)
{
if (MDB_DataCount() > 0)
{
MDB_Read(&reply);
Debug.print(F(" 0x"));
Debug.print(reply, HEX);
}
}
}
/*
* Emulates VEND command
*/
void vmcVend()
{
}
/*
* Emulates READER command
*/
void vmcReader()
{
uint8_t i; // counter
uint8_t checksum = 0;
uint16_t reply[2];
uint16_t comm_reader_0[2] = {VMC_READER, 0x00};
uint16_t comm_reader_1[2] = {VMC_READER, 0x01};
uint16_t comm_reader_2[2] = {VMC_READER, 0x02};
Debug.println(F("VMC_READER Call test"));
// Test 1 -- READER Disable
checksum = calcChecksum(comm_reader_0, 2);
for (i = 0; i < 2; ++i)
MDB_Send(comm_reader_0[i]);
MDB_Send(checksum);
// Wait for reply
while (true)
if (MDB_DataCount() > 0)
break;
Debug.print(F("READER Disable reply :"));
for (i = 0; i < 1; ++i)
{
MDB_Read(&reply[i]);
Debug.print(F(" 0x"));
Debug.print(reply[i], HEX);
}
Debug.println();
// Test 2 -- READER Enable
checksum = calcChecksum(comm_reader_1, 2);
for (i = 0; i < 2; ++i)
MDB_Send(comm_reader_1[i]);
MDB_Send(checksum);
// Wait for reply
while (true)
if (MDB_DataCount() > 0)
break;
Debug.print(F("READER Enable reply :"));
for (i = 0; i < 1; ++i)
{
MDB_Read(&reply[i]);
Debug.print(F(" 0x"));
Debug.print(reply[i], HEX);
}
Debug.println();
// Test 3 -- READER Cancelled
checksum = calcChecksum(comm_reader_2, 2);
for (i = 0; i < 2; ++i)
MDB_Send(comm_reader_2[i]);
MDB_Send(checksum);
// Wait for reply
while (true)
if (MDB_DataCount() > 1)
break;
Debug.print(F("READER Cancel reply :"));
for (i = 0; i < 2; ++i)
{
MDB_Read(&reply[i]);
Debug.print(F(" 0x"));
Debug.print(reply[i], HEX);
}
Debug.println();
}
/*
* Emulates EXPANSION command
*/
void vmcExpansion()
{
uint8_t i; // counter
uint8_t checksum = VMC_EXPANSION + VMC_EXPANSION_REQUEST_ID;
uint16_t expSend[32];
uint16_t periphID[31];
expSend[0] = VMC_EXPANSION;
expSend[1] = VMC_EXPANSION_REQUEST_ID;
// Make 29 data elements + 1 checksum
for (i = 2; i < 31; ++i)
{
expSend[i] = 0x30;
checksum += expSend[i];
}
expSend[31] = checksum;
// Send data
for (i = 0; i < 32; ++i)
MDB_Send(expSend[i]);
Debug.print(111);
while (true)
if (MDB_DataCount() > 30)
{
for (i = 0; i < 31; ++i)
MDB_Read(periphID + i);
break;
}
Debug.print(222);
Debug.print(F("Incoming Expansion Request ID data :"));
for (i = 0; i < 31; ++i)
{
Debug.print(F(" 0x"));
Debug.print(periphID[i], HEX);
}
Debug.println();
}
uint8_t calcChecksum(uint16_t *array, uint8_t arr_size)
{
uint8_t retVal = 0x00;
uint8_t i;
for (i = 0; i < arr_size; ++i)
retVal += *(array + i);
return retVal;
}