-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt_attiva.c
389 lines (328 loc) · 10.5 KB
/
pt_attiva.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef LINT
static char sccs_pt_attiva_id[] = "@(#)pt_attiva.c Version 1.1 Date 00/02/21 Hour 12:06:39";
#endif
/*---------------------------------------------------------------------------
pt_attiva.c: funzione per la gestione dell'interfacciamento
tra il S.I.A. e il sistema TV GuestLink
Parametri in input - tty, speed, parity, databit, stopbit
---------------------------------------------------------------------------*/
#include "dls_include.h"
#include "paytv.h"
#include <sys/wait.h>
#include <pwd.h>
pid_t dbpid;
unsigned int time_to_sleep=80;
int *sia_status;
int main(int argc, char *argv[])
{
int i,lun,dls_qr,dls_qw;
char pt_bcc(const char*);
char c;
char *scarto;
char pt_stringa[MAXSTRLEN];
struct passwd *dls_passwd;
struct dls_msg {
long mtype;
char mtext[MAXSTRLEN];
};
struct dls_msg dls_msgstruct;
if(argc < 6) {
fprintf(stderr,"Usage: %s tty, speed, parity, databit, stopbit\n",argv[0]);
exit(1);
}
if((dls_shmid = shmget((key_t)PT_SHM,sizeof(sia_status),IPC_EXCL | IPC_CREAT | 0666)) == ERROR) {
if(errno != EEXIST) {
perror("Cannot create Shared Memory ! reason");
exit(1);
}
else {
if((dls_shmid = shmget((key_t)PT_SHM,sizeof(sia_status),0666)) == ERROR) {
perror("Cannot get shared memory identifier reason");
exit(1);
}
}
}
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
perror("Cannot attache shared memory segment to my process reason");
exit(1);
}
sia_status = (int *)shm_pointer;
*sia_status = FALSE;
/*
* Apro il device e ne setto le caratteristiche
*/
if(dls_opentty(argv) == ERROR)
exit(1);
dls_passwd = getpwnam("informix");
if(setgid(dls_passwd->pw_gid) == ERROR) {
perror("Can't set group id (informix) reason");
exit(1);
}
if(setuid(dls_passwd->pw_uid) == ERROR) {
perror("Can't set user id (informix) reason");
exit(1);
}
/*
* Creo semaforo e setto valido
*/
semshm = semget((key_t)PT_SEM,1,IPC_CREAT|0660);
if(semshm == ERROR) {
perror("Cannot create semaphore");
exit(1);
}
(void)semctl(semshm,0,SETVAL,1);
/*
* Creo coda di messaggi READ
*/
if((dls_qr = msgget((key_t)PT_MSGQR,IPC_CREAT | 0660)) == ERROR) {
perror("Can't create message queue ID (READ)");
exit(1);
}
/*
* Creo coda di messaggi WRITE
*/
if((dls_qw = msgget((key_t)PT_MSGQW,IPC_CREAT | 0660)) == ERROR) {
perror("Can't create message queue ID (WRITE)");
exit(1);
}
/*
* Forko e Leggo/Scrivo
*/
if((dbpid = fork()) == 0) {
/*
* Leggo il device e valorizzo la coda di messaggi
*/
i = 0;
while(read(dls_fdtty,&c,1) > 0) {
if(c == PT_ACK) {
pt_stringa[0] = c;
pt_stringa[1] = '\0';
dls_msgstruct.mtype = 1L;
(void)strcpy(dls_msgstruct.mtext,pt_stringa);
if(msgsnd(dls_qr,(void *)&dls_msgstruct,sizeof(pt_stringa),0) == ERROR) {
perror("Can't send message queue");
exit(1);
}
i = 0;
continue;
}
if(c == PT_ENQ) {
pt_stringa[0] = c;
pt_stringa[1] = '\0';
dls_msgstruct.mtype = 1L;
(void)strcpy(dls_msgstruct.mtext,pt_stringa);
if(msgsnd(dls_qr,(void *)&dls_msgstruct,sizeof(pt_stringa),0) == ERROR) {
perror("Can't send message queue");
exit(1);
}
i = 0;
continue;
}
if(c == PT_NCK) {
pt_stringa[0] = c;
pt_stringa[1] = '\0';
dls_msgstruct.mtype = 1L;
(void)strcpy(dls_msgstruct.mtext,pt_stringa);
if(msgsnd(dls_qr,(void *)&dls_msgstruct,sizeof(pt_stringa),0) == ERROR) {
perror("Can't send message queue");
exit(1);
}
i = 0;
continue;
}
if(c == PT_ETX) {
pt_stringa[i] = '\0';
scarto = (char *)malloc(sizeof(char));
read(dls_fdtty,scarto,1);
free(scarto);
dls_msgstruct.mtype = 1L;
(void)strcpy(dls_msgstruct.mtext,pt_stringa);
if(msgsnd(dls_qr,(void *)&dls_msgstruct,sizeof(pt_stringa),0) == ERROR) {
perror("Can't send message queue");
exit(1);
}
i = 0;
continue;
}
pt_stringa[i] = c;
i++;
}
}
else { /* Processo Padre */
while(TRUE) {
if(msgrcv(dls_qw,(void *)&dls_msgstruct,sizeof(pt_stringa),1L,0) == ERROR) { /* Errore fatale */
perror("Fatal error while receiving !");
exit(1);
}
lun = strlen(dls_msgstruct.mtext);
dls_msgstruct.mtext[lun] = pt_bcc(&dls_msgstruct.mtext[1]);
lun++;
dls_msgstruct.mtext[lun] = '\0';
if(write(dls_fdtty,dls_msgstruct.mtext,lun) == ERROR ) {
perror("Can't write to device");
exit(1);
}
}
}
}
int dls_opentty(char *argv[])
/*---------------------------------------------------------------------------
dls_opentty: funzione che apre e setta il device
---------------------------------------------------------------------------*/
{
char tmp[20];
struct termio dls_termio;
if((dls_device=(IOCONTROL *)malloc(sizeof(IOCONTROL)))==(IOCONTROL *)NULL) {
perror("Can't allocate memory");
return(ERROR);
}
strcpy(dls_device->dls_tty,argv[1]);
dls_device->dls_speed = atoi(argv[2]);
strcpy(dls_device->dls_parity,argv[3]);
dls_device->dls_bitdata = atoi(argv[4]);
dls_device->dls_bitstop = atoi(argv[5]);
(void)strcpy(tmp,(char *)dls_device->dls_tty);
/*
* Aggiungo suffisso "/dev/" se non presente nel parametro
*/
if((strstr((char *)dls_device->dls_tty,"/dev/")) == (char *)NULL)
(void)sprintf((char *)dls_device->dls_tty,"/dev/%s",tmp);
/*
* Apertura porta per lettura sospensiva
*/
if((dls_fdtty = open((char *)dls_device->dls_tty,O_RDWR)) == ERROR) {
perror("Can't open device");
return(ERROR);
}
/*
* Apertura porta OK !
* Configuro il device con i parametri ricevuti (velocita', parita', ecc..)
*/
if ( ioctl(dls_fdtty,TCGETA,&dls_copia) == ERROR ) {
perror("Can't get terminal characteristics");
return(ERROR);
}
memcpy((void *)&dls_termio,(void *)&dls_copia,sizeof(struct termio));
dls_termio.c_cflag &= ~CBAUD;
dls_termio.c_cflag &= ~CSIZE;
dls_termio.c_iflag &= ~IXANY;
dls_termio.c_iflag &= ~IXOFF;
dls_termio.c_iflag &= ~IXON;
dls_termio.c_iflag |= IXON | IXOFF;
switch(dls_device->dls_speed) {
case 50:
dls_termio.c_cflag |= B50;
break;
case 75:
dls_termio.c_cflag |= B75;
break;
case 110:
dls_termio.c_cflag |= B110;
break;
case 134:
dls_termio.c_cflag |= B134;
break;
case 150:
dls_termio.c_cflag |= B150;
break;
case 200:
dls_termio.c_cflag |= B200;
break;
case 300:
dls_termio.c_cflag |= B300;
break;
case 600:
dls_termio.c_cflag |= B600;
break;
case 1200:
dls_termio.c_cflag |= B1200;
break;
case 2400:
dls_termio.c_cflag |= B2400;
break;
case 4800:
dls_termio.c_cflag |= B4800;
break;
case 9600:
dls_termio.c_cflag |= B9600;
break;
case 19200:
dls_termio.c_cflag |= B19200;
break;
default:
fprintf(stderr,"Invalid speed -> %d\n",dls_device->dls_speed);
return(ERROR);
break;
}
/*
* Set parita'
*/
switch(dls_device->dls_parity[0]) {
case 'E':
dls_termio.c_cflag &= ~PARODD;
dls_termio.c_cflag |= PARENB;
break;
case 'N':
dls_termio.c_cflag &= ~PARENB;
dls_termio.c_cflag &= ~PARODD;
break;
case 'O':
dls_termio.c_cflag |= PARENB;
dls_termio.c_cflag |= PARODD;
break;
default:
fprintf(stderr,"Invalid parity -> %s\n",dls_device->dls_parity);
return(ERROR);
break;
}
/*
* Set bit di data
*/
switch(dls_device->dls_bitdata) {
case 7:
dls_termio.c_cflag |= CS7;
break;
case 8:
dls_termio.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Invalid bit data -> %d\n",dls_device->dls_bitdata);
return(ERROR);
break;
}
/*
* Set bit di stop
*/
switch(dls_device->dls_bitstop) {
case 1:
dls_termio.c_cflag &= ~CSTOPB;
break;
case 2:
dls_termio.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Invalid bit stop -> %d\n",dls_device->dls_bitstop);
return(ERROR);
break;
}
dls_termio.c_lflag &= ~ECHO;
dls_termio.c_lflag &= ~ICANON;
dls_termio.c_cc[VTIME] = 0;
dls_termio.c_cc[VMIN] = 1;
if ( ioctl(dls_fdtty,TCSETA,&dls_termio) == ERROR ) {
perror("Can't set terminal characteristics");
return(ERROR);
}
return(0);
}
char pt_bcc(const char *t)
/*------------------------------------------------------------------------------
pt_bcc - Calcola il codice di controllo di una stringa generica
null-terminata per NOKIA GuestLink
------------------------------------------------------------------------------*/
{
register char bcc;
bcc = 0;
while (*t) { bcc = bcc ^ *t++; }
return(bcc);
}