-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransmit.c
More file actions
264 lines (228 loc) · 5.69 KB
/
transmit.c
File metadata and controls
264 lines (228 loc) · 5.69 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
/*********************************************************************
L.A.
Prg name : transmit.c
Function : dialog AV8500 <----> Tashlumat
Flat programme, only one function except main ???
Date de creation : 21.02.1999
by A.Dinur & Y.Refaely @ Malam.Systems
***********************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <termio.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <unistd.h>
#define TIMEOUT 50 /* wait TIMEOUT secs */
#define FALSE 0
#define TRUE 1
char flag_timeout; /* timeout flag */
char flag_rec = FALSE;
char buff_rx[600];
char code_rx[5] = '\0';
char buff_tx[600];
char expect_code[600];
char c,c_rx = '\0';
char nl = 0x0a;
char cr = 0x0d;
char result[600];
int size;
int i = 1;
int ttyfd;
int res = 0;
void gest_timeout(); /* timeout function */
void readcom();
void sendmessage(char *messg);
void open_closetashlumat(char *messg);
void sendlogicok();
void sendtashlumat();
char checkresult();
char substr(char *str,int start,int numb,char *outstr);
void transmit (strin, rest)
char* strin;
char* rest;
{
char code[2];
char temp_code[5];
int trans_cod;
FILE *ordfile;
substr ( strin,0,1,code );
trans_cod = atoi ( code );
substr ( strin,1,4,expect_code );
size = strlen ( strin ) -5;
substr ( strin,5,size,buff_tx );
rest[0]=0;
/* ordfile = fopen ("/users/a0elina/proc/ordfd.lis","w"); */
ttyfd = open ("/dev/term/0",O_RDWR);
/* sprintf(result,"%s"," 3NOANSWER"); */
/* strcpy ( rest,result ); */
/* fprintf(ordfile,"ttyfd=%d rest=%s result=%s\n",ttyfd,rest,result); */
/* fprintf(ordfile,"%s\n","1st 3NOANSWER"); */
/* fclose(ordfile); */
/* return; */
if (ttyfd == -1)
{
sprintf(result,"%s"," 3NOANSWER");
strcpy ( rest,result );
return;
}
printf("test");
/* Use Cuid*/
system("/usr/bin/stty raw 4800 cs7 parenb -echo -parodd < /dev/term/0");
switch (trans_cod)
{
case 2: /* Money RETURN */
trans_cod = 1;
break;
case 4: /* Caspomat CLOSE */
trans_cod = 3;
break;
}
switch ( trans_cod )
{
case 1: /* Money PAY */
sendtashlumat();
if ( res == 1 )
{
sleep(2);
sendlogicok();
}
break;
case 3: /* Caspomat OPEN */
open_closetashlumat ( buff_tx );
break;
case 5: /* Caspomat STATUS */
sendtashlumat();
break;
}
if ( flag_rec == FALSE )
sprintf ( result,"%s"," 3NORESPONSE" );
strcpy ( rest,result );
close ( ttyfd );
return ;
}
/**********************************************************************/
/* Set flag timeout */
void gest_timeout()
{
flag_timeout = TRUE;
}
/********************************************************/
/* SEND Tashlumat */
void sendtashlumat()
{
if ( size != 0 )
sendmessage ( buff_tx );
flag_timeout = FALSE;
alarm ( TIMEOUT );
signal ( SIGALRM, *gest_timeout );
i = 0;
do
{
sprintf ( result,"%s"," 0" );
readcom();
code_rx[0] = '\0';
substr ( buff_rx,0,4,code_rx );
res = checkresult ();
}
while ( flag_timeout == FALSE );
}
/****************************************************************/
/* Read communication */
void readcom()
{
buff_rx[0] = '\0';
i = 0;
do
{
read ( ttyfd,&c_rx,1 );
buff_rx[i++] = c_rx;
if ( i > 1 )
flag_rec = TRUE;
}
while ( c_rx != 0x0d && flag_timeout == FALSE );
read ( ttyfd,&c_rx,1 );
buff_rx[--i] = '\0';
}
/**********************************************************************/
/* Send message */
void sendmessage(char *messg)
{
write(ttyfd,messg,strlen(messg));
write(ttyfd,&cr,1);
write(ttyfd,&nl,1);
}
/**********************************************************************/
/* String function */
char substr(char *str,int start,int numb,char *outstr)
{
int i,j = 0;
int ret;
for ( i = 0; i < numb; i++ )
{
if ( str[start+i] == '\0' )
{
return;
}
outstr[j++] = str[start+i];
}
outstr[j] = '\0';
}
/****************************************************************/
/* OPEN or CLOSE Tashlumat */
void open_closetashlumat(char *messg)
{
sendmessage ( messg );
flag_timeout = FALSE;
alarm ( TIMEOUT );
signal ( SIGALRM, *gest_timeout );
do
{
readcom();
code_rx[0] = '\0';
substr(buff_rx,0,4,code_rx);
res = checkresult();
}
while ( flag_timeout == FALSE );
}
/**********************************************************************/
char checkresult()
{
if ( strcmp ( code_rx,expect_code ) == 0 )
{
size = strlen ( buff_rx );
if ( size > 4 )
{
sprintf ( result,"%s"," 1" );
buff_rx[size] = '\0';
strcat ( result,buff_rx );
}
else
{
sprintf ( result,"%s"," 1NOSTRING" );
}
flag_timeout = TRUE;
return 1;
}
else
{
sprintf ( result,"%s"," 4NOCOMPATIBLE" );
return 0;
}
}
/*********************************************************************/
void sendlogicok()
{
char mess1[20] = "000811";
char oper[5];
long i;
substr ( buff_rx,10,4,oper );
strcat ( mess1,oper );
strcat ( mess1,"00" );
strcat ( mess1,oper );
for ( i = 0; i < 1000000 ; i++ );
sendmessage ( mess1 );
}