Payout
A thin wrapper around the SSP protocol from ITL
port_win32.h
1 #ifndef BASIC_DEMO6_WIN32_H
2 #define BASIC_DEMO6_WIN32_H
3 
4 #ifdef INCLUDED_PORT
5 #error "Multiple ports included"
6 #endif
7 #define INCLUDED_PORT
8 
9 #include <conio.h>
10 #include <Windows.h>
11 #include <stdio.h>
12 
13 #include "ssp_helpers.h"
14 
15 // windows does not provide usleep
16 #define usleep(x) Sleep(x/1000)
17 
18 // linux port uses a changemode function but windows does not need this
19 #define changemode(x)
20 
21 // some functions which are available under different names
22 #define kbhit _kbhit
23 #define getchar _getch
24 
25 
26 
27 typedef UINT (CALLBACK* LPFNDLLFUNC1)(SSP_COMMAND* cmd);
28 LPFNDLLFUNC1 openPort;
29 typedef UINT (CALLBACK* LPFNDLLFUNC2)(void);
30 LPFNDLLFUNC2 closePort;
31 typedef UINT (CALLBACK* LPFNDLLFUNC3)(SSP_COMMAND* cmd,SSP_COMMAND_INFO* sspInfo);
32 LPFNDLLFUNC3 sspSendCommand;
33 typedef UINT (CALLBACK* LPFNDLLFUNC4)(SSP_KEYS* key, SSP_COMMAND* cmd);
34 LPFNDLLFUNC4 initiateSSPHostKeys;
35 typedef UINT (CALLBACK* LPFNDLLFUNC5)(SSP_KEYS* key);
36 LPFNDLLFUNC5 createSSPHostEncryptionKey;
37 
38 static int open_ssp_port (SSP_COMMAND *sspC) {
39  return openPort(sspC);
40 }
41 static void close_ssp_port () {
42  closePort();
43 }
44 
45 static int init_lib(void)
46 {
47  HINSTANCE hinstLib;
48 
49 
50  // Load DLL file
51  hinstLib = LoadLibrary(TEXT("ITLSSPProc.dll"));
52  if (hinstLib == NULL) {
53  printf("ERROR: unable to load DLL\n");
54  return 0;
55  }
56 
57  openPort = (LPFNDLLFUNC1)GetProcAddress(hinstLib, "OpenSSPComPortUSB");//
58  if (openPort == NULL) {
59  printf("ERROR: unable to find DLL function OpenSSPComPort\n");
60  FreeLibrary(hinstLib);
61  return 0;
62  }
63 
64  closePort = (LPFNDLLFUNC2)GetProcAddress(hinstLib, "CloseSSPComPortUSB");
65  if (closePort == NULL) {
66  printf("ERROR: unable to find DLL function CloseSSPComPort\n");
67  FreeLibrary(hinstLib);
68  return 0;
69  }
70 
71  sspSendCommand = (LPFNDLLFUNC3)GetProcAddress(hinstLib, "SSPSendCommand");
72  if (sspSendCommand == NULL) {
73  printf("ERROR: unable to find DLL function SSPSendCommand\n");
74  FreeLibrary(hinstLib);
75  return 0;
76  }
77  initiateSSPHostKeys = (LPFNDLLFUNC4)GetProcAddress(hinstLib, "InitiateSSPHostKeys");
78  if (initiateSSPHostKeys == NULL) {
79  printf("ERROR: unable to find DLL function initiateSSPHostKeys\n");
80  FreeLibrary(hinstLib);
81  return 0;
82  }
83  createSSPHostEncryptionKey = (LPFNDLLFUNC5)GetProcAddress(hinstLib, "CreateSSPHostEncryptionKey");
84  if (createSSPHostEncryptionKey == NULL) {
85  printf("ERROR: unable to find DLL function createSSPHostEncryptionKey\n");
86  FreeLibrary(hinstLib);
87  return 0;
88  }
89 
90 
91  return 1;
92 
93 }
94 
95 
96 static int SendSSPCommand(SSP_COMMAND* sp,SSP_COMMAND_INFO* si)
97 {
98  int reTries = 0;
99  int i;
100  SSP_COMMAND ssp;
101 
102 
103  /* take a copy for retries */
104  for(i = 0; i < sp->CommandDataLength; i++)
105  ssp.CommandData[i] = sp->CommandData[i];
106  ssp.CommandDataLength = sp->CommandDataLength;
107 
108  while(reTries < sp->RetryLevel){
109 
110  /* dll call */
111  //if(sspSendCommand(&sspC,&sspI) == 0)
112  if(sspSendCommand(sp,si) == 0){
113  sp->ResponseStatus = SSP_RESPONSE_TIMEOUT;
114  return 0;
115  }
116  /* response OK */
117  if(sp->ResponseData[0] == SSP_RESPONSE_OK){
118  sp->ResponseStatus = SSP_RESPONSE_OK;
119  return 255;
120  }
121 
122  /* retry for busy response */
123  if(sp->ResponseData[0] == SSP_RESPONSE_COMMAND_NOT_PROCESSED && sp->ResponseDataLength == 2 && sp->ResponseData[1] == 0x03 /*PAYOUT_BUSY*/){
124  Sleep(500); /* delay for next retry */
125  /* restore command data as it may have been changed by DLL encryption */
126  for(i = 0; i < sp->CommandDataLength; i++)
127  sp->CommandData[i] = ssp.CommandData[i];
128  sp->CommandDataLength = ssp.CommandDataLength;
129  }
130 
131  /* Check for a payout error responses */
132  if(sp->ResponseData[0] == SSP_RESPONSE_COMMAND_NOT_PROCESSED && sp->ResponseDataLength == 2 && sp->ResponseData[1] == 0x01 /*NOT_ENOUGH_VALUE*/){
133  sp->ResponseStatus = SSP_RESPONSE_COMMAND_NOT_PROCESSED;
134  return 0x01;
135  }
136  if(sp->ResponseData[0] == SSP_RESPONSE_COMMAND_NOT_PROCESSED && sp->ResponseDataLength == 2 && sp->ResponseData[1] == 0x02 /*CANNOT_PAY_EXACT_AMOUNT*/){
137  sp->ResponseStatus = SSP_RESPONSE_COMMAND_NOT_PROCESSED;
138  return 0x02;
139  }
140  if(sp->ResponseData[0] == SSP_RESPONSE_COMMAND_NOT_PROCESSED && sp->ResponseDataLength == 2 && sp->ResponseData[1] == 0x04 /*PAYOUT_DISABLED*/){
141  sp->ResponseStatus = SSP_RESPONSE_COMMAND_NOT_PROCESSED;
142  return 0x04;
143  }
144 
145  if(sp->ResponseData[0] == SSP_RESPONSE_KEY_NOT_SET){
146  sp->ResponseStatus = SSP_RESPONSE_KEY_NOT_SET;
147  return 0x01;
148  }
149 
150 
151 
152  reTries++;
153  }
154 
155  if(reTries >= sp->RetryLevel) {
156  sp->ResponseStatus = SSP_RESPONSE_TIMEOUT;
157  return 0;
158  }
159 
160  sp->ResponseStatus = SSP_RESPONSE_OK;
161  return 255;
162 }
163 static int send_ssp_command(SSP_COMMAND *sspC) {
164  SSP_COMMAND_INFO si;
165  return SendSSPCommand(sspC, &si);
166 }
167 
168 
169 
170 static int negotiate_ssp_encryption(SSP_COMMAND *sspC, SSP_FULL_KEY *hostKey)
171 {
172  SSP_KEYS sKey;
173  int i;
174 
175 
176  // DLL call
177  if(initiateSSPHostKeys(&sKey,sspC) == 0){
178  printf("ERROR: Cannot Initiate host keys\n");
179  return 0;
180  }
181 
182  // send sync command to establish coms
183  sspC->EncryptionStatus = 0;
184  sspC->CommandDataLength = 1;
185  sspC->CommandData[0] = SSP_CMD_SYNC;
186  if(send_ssp_command(sspC) == 0){
187  printf("ERROR: Error sending sync command to slave\n");
188  return 0;
189 
190  }
191 
192 
193 
194  // set generator command
195  sspC->EncryptionStatus = 0;
196  sspC->CommandDataLength = 9;
197  sspC->CommandData[0] = SSP_CMD_SET_GENERATOR;
198  for(i = 0; i < 8; i++)
199  sspC->CommandData[i + 1] = (unsigned char)(sKey.Generator >> (i*8));
200  if(send_ssp_command(sspC) == 0){
201  printf("ERROR: Error sending GENERATOR command to slave\n");
202  return 0;
203 
204  }
205  // set modulus command
206  sspC->EncryptionStatus = 0;
207  sspC->CommandDataLength = 9;
208  sspC->CommandData[0] = SSP_CMD_SET_MODULUS;
209  for(i = 0; i < 8; i++)
210  sspC->CommandData[i + 1] = (unsigned char)(sKey.Modulus >> (i*8));
211  if(send_ssp_command(sspC) == 0){
212  printf("ERROR: Error sending MODULUS command to slave\n");
213  return 0;
214 
215  }
216  // req key exchange command
217  sspC->EncryptionStatus = 0;
218  sspC->CommandDataLength = 9;
219  sspC->CommandData[0] = SSP_CMD_KEY_EXCHANGE;
220  for(i = 0; i < 8; i++)
221  sspC->CommandData[i + 1] = (unsigned char)(sKey.HostInter >> (i*8));
222  if(send_ssp_command(sspC) == 0){
223  printf("ERROR: Error sending KEY_EXCHANGE command to slave\n");
224  return 0;
225 
226  }
227  sKey.SlaveInterKey = 0;
228  for(i = 0; i < 8; i++)
229  sKey.SlaveInterKey += ((unsigned __int64)sspC->ResponseData[i + 1]) << (i*8);
230 
231 
232  // DLL call to create our key
233  if(createSSPHostEncryptionKey(&sKey) == 0){
234  printf("ERROR: Cannot Create host keys\n");
235  return 0;
236  }
237 
238  hostKey->EncryptKey = sKey.KeyHost;
239 
240  return 1;
241 
242 }
243 
244 /* DLL defs and references */
245 /*typedef UINT (CALLBACK* LPFNDLLFUNC1)(SSP_COMMAND* cmd);
246 LPFNDLLFUNC1 openPort;
247 typedef UINT (CALLBACK* LPFNDLLFUNC2)(void);
248 LPFNDLLFUNC2 closePort;
249 
250 */
251 
252 #endif
Definition: port_win32_ssp.h:35
Definition: port_win32_ssp.h:67
Definition: port_win32_ssp.h:57
Definition: port_win32_ssp.h:30