/
enlace.c
1101 lines (895 loc) · 32.8 KB
/
enlace.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Fichero: enlace.c
** Autores:
** Alvaro Valiente Herrero
** Diego Sanchez Moreno
** Usuario: i8112600
*/
#include "enlace.h"
#include "fisico.h"
#include "windows.h"
TIPO_F_INICIO_PUERTO f_inicio_puerto;
TIPO_F_ESCRIBIR f_escribir;
TIPO_F_PUEDO_ESCRIBIR f_puedo_escribir;
TIPO_F_LEER f_leer;
TIPO_F_ESPERAR_LEER f_esperar_leer;
TIPO_F_PUEDO_LEER f_puedo_leer;
TIPO_F_ESPERAR_ESCRIBIR f_esperar_escribir;
DWORD loadDll(void)
{
mydll=LoadLibrary(dllname);
if (mydll==NULL)
{
PERROR("LoadLibrary");
return 1;
}
f_inicio_puerto=(TIPO_F_INICIO_PUERTO)GetProcAddress(mydll,"f_inicio_puerto");
if (f_inicio_puerto==NULL)
{
PERROR("GetProcAddress:f_inicio_puerto");
return 1;
}
f_escribir=(TIPO_F_ESCRIBIR)GetProcAddress(mydll,"f_escribir");
if (f_escribir==NULL)
{
PERROR("GetProcAddress:f_escribir");
return 1;
}
f_leer=(TIPO_F_LEER)GetProcAddress(mydll,"f_leer");
if (f_leer==NULL)
{
PERROR("GetProcAddress:f_leer");
return 1;
}
f_puedo_escribir=(TIPO_F_PUEDO_ESCRIBIR)GetProcAddress(mydll,"f_puedo_escribir");
if (f_puedo_escribir==NULL)
{
PERROR("GetProcAddress:f_puedo_escribir");
return 1;
}
f_puedo_leer=(TIPO_F_PUEDO_LEER)GetProcAddress(mydll,"f_puedo_leer");
if (f_puedo_leer==NULL)
{
PERROR("GetProcAddress:f_puedo_leer");
return 1;
}
f_esperar_leer=(TIPO_F_ESPERAR_LEER)GetProcAddress(mydll,"f_esperar_leer");
if (f_esperar_leer==NULL)
{
PERROR("GetProcAddress:f_esperar_leer");
return 1;
}
f_esperar_escribir=(TIPO_F_ESPERAR_ESCRIBIR)GetProcAddress(mydll,"f_esperar_escribir");
if (f_esperar_escribir==NULL)
{
PERROR("GetProcAddress:f_esperar_escribir");
return 1;
}
return 0;
}
DWORD liberateDll(void)
{
FreeLibrary(mydll);
return 0;
}
DWORD initiateSideI(int errors, int velocity, int mode)
{
side = 0;//al lado izquierdo le corresponde el 0
maxTrys = 5;
timeout = 4000;
nSecuenceNext = 0;
fI = (struct Frame*) malloc(sizeof(Frame));
//iniciamos el puerto
f_inicio_puerto('I',NULL,"1234",velocity,NULL,errors);
//esto nos devuelve el manejador del output de pantalla
hstdoutI=GetStdHandle(STD_OUTPUT_HANDLE);
//hilo que esperara la lectura de tramas y de alguna manera avisara
if((waitingI = CreateThread(NULL,0,waiting_for,fI,0,NULL)) == NULL)
{
fprintf(stderr,"\nError making wating thread\n");
return 1;
}
//semaforo que indicara si hay datos para leer
side_I_data_ready = CreateSemaphore(NULL,0,1,"semaphoreI");
//semaforo para indicar que se ha producido la conexion
conexion = CreateSemaphore(NULL,0,1,"semaphoreConexion");
return 0;
}
DWORD initiateSideD(int errors,int velocity ,int mode)
{
side = 1;//al lado derecho le corresponde el 1
maxTrys = 5;
timeout = 4000;
nSecuenceWait = 0;
fD = (struct Frame*) malloc(sizeof(Frame));
//iniciamos el puerto
f_inicio_puerto('D',NULL,"1234",velocity,NULL,errors);
//esto nos devuelve el manejador del output de pantalla
hstdoutD=GetStdHandle(STD_OUTPUT_HANDLE);
//hilo que esperara la lectura de tramas y de alguna manera avisara
if((waitingD = CreateThread(NULL,0,waiting_for,fD,0,NULL)) == NULL)
{
fprintf(stderr,"\nError making wating thread\n");
return 1;
}
//semaforo que indicara si hay datos para leer
side_D_data_ready = CreateSemaphore(NULL,0,1,"semaphoreD");
return 0;
}
DWORD makeFrame(int type, unsigned char *data)
{
int ret,i,j,k;
struct Frame *f;
unsigned char *in_morse;
unsigned short crc;
//la cabecera sera igual para todos
char head[5] = {DLE,SYN,DLE,SYN,DLE};
in_morse = (unsigned char*) malloc(sizeof(unsigned char)*8);
f = (struct Frame*) malloc(sizeof(Frame));
f->length = 0;
//ponemos la cabecera que sera igual para todos indistintamente
for(i=0;i<5;i++)
{
f->body[i]= head[i];
f->length++;
}
switch(type)//dependiendo del tipo de trama...
{
case enqFrame://ENQ
f->body[i] = ENQ;
i++;
f->length++;
f->type = enqFrame;
break;
case ackFrame://ACK
f->body[i] = ACK;
i++;
f->length++;
f->type = ackFrame;
break;
case eotFrame://EOT
f->body[i] = EOT;
i++;
f->length++;
f->type = eotFrame;
break;
case dataFrame://DATA
f->body[i] = SOH;//inicio cabecera de datos
i++;
f->length++;
if(nSecuenceNext == 0)//numero de sencuencia
{
f->body[i] = '0';
}
else
{
f->body[i] = '1';
}
f->nSecuence = nSecuenceNext;
i++;
f->length++;
f->body[i] = DLE;//DLE
i++;
f->length++;
f->body[i] = STX;//inicio de datos
i++;
f->length++;
for(j=0;j< strlen(data);j++)//datos de la trama
{
f->body[i] = data[j];
if(data[j] == DLE)//si aparece DLE en el texto, este mismo se duplica
{
f->body[i+1] = DLE;
i++;
f->length++;
}
i++;
f->length++;
}
f->body[i] = DLE;//DLE anterior a ETX
i++;
f->length++;
f->body[i] = ETX;//fin de datos
i++;
f->length++;
//codigo de control de errores CRC
strcat(data,"\0");
crc =icrc(0,(unsigned char *)data,strlen(data), 255, -1);
f->body[i]=LOBYTE(crc);
f->body[i+1]=HIBYTE(crc);
f->length = f->length +2;
f->type = dataFrame;
break;
case morseFrame:
f->body[i] = SOH;//inicio cabecera de datos
i++;
f->length++;
if(nSecuenceWait == 0)//numero de sencuencia
{
f->body[i] = '0';
}
else
{
f->body[i] = '1';
}
f->nSecuence = nSecuenceWait;
i++;
f->length++;
f->body[i] = DLE;//DLE
i++;
f->length++;
f->body[i] = STX;//inicio de datos
i++;
f->length++;
//datos en morse
for(j=0;j<(strlen(data));j++)
{
morse(data[j],in_morse);//llama la funcion que traduce a morse
for(k=0;k<strlen(in_morse);k++)
{
f->body[i] = in_morse[k];
if(data[j] == DLE)//si aparece DLE en el texto, este mismo se duplica
{
f->body[i+1] = DLE;
i++;
f->length++;
}
i++;
f->length++;
}
//caracter de separacion entre cada palabra de morse
f->body[i] = '#';
i++;
f->length++;
}
f->body[i] = DLE;//DLE anterior a ETX
i++;
f->length++;
f->body[i] = ETX;//fin de datos
i++;
f->length++;
//codigo de control de errores CRC
catchData(f);
crc =icrc(0,(unsigned char *)f->data,strlen(f->data), 255, -1);
f->body[i]=LOBYTE(crc);
f->body[i+1]=HIBYTE(crc);
f->length = f->length +2;
f->type = dataFrame;
break;
}
//trama creada, procedemos a enviarla
ret = sendFrame(*f);
return ret;//devolvemos lo que nos devuelve la funcion de enviar la trama
}
DWORD sendFrame(struct Frame f) {
int ret,i,u;
for(i=0;i<f.length;i++)//hasta la longitud de la trama
{
if(f_esperar_escribir(INFINITE))//esperamos hasta que podamos escribir
{
ret=f_escribir(f.body[i]);//escribimos un caracter
if(ret == -1){break;}//si retorna -1 paramos
switch(ret)
{
case 0://sin problemas al enviar
break;
case -1://error de sobreescritura
return 1;
break;
}
}
}
//si esta activo el modo debug, mostramos la trama enviada
if(mode == 1)
{
printf("\n-------------------------------SEND------------------------------------\n");
for(u=0;u<f.length;u++)
{
printData(f.body[u]);
printf(" ");
}
printf("\n");
}
else
{
if(side == 1)//solo si es el lado del receptor y no esta en modo debug
{
if(f.type == dataFrame)
{
catchData(&f);
printf("\nSend: %s",f.data);
}
}
}
return ret;
}
DWORD WINAPI waiting_for(LPVOID p)
{
int ret,first = 1;
int i=0;
unsigned char c;
int flag;
unsigned short crc;
int u;
struct Frame *f;
f = (struct Frame*) p;
f->length =0;
f->type = 0;
if(side == 0)//LADO IZQUIERDO (EMISOR)
{
while(1)
{
flag = 0;
while(flag ==0)
{
if(first == 1)//es el primer caracter, por tanto esperamos hasta que exista uno en el buffer que poder leer
{
f_esperar_leer(INFINITE);
clearFrame(f);//aprovechamos para limpiar la trama
i=0;
}
else
{
f_esperar_leer(800);//este ya no es el primer caracter
}
ret=f_leer(&c);
first= 0;
switch (ret)
{
case 0://sin problemas al leer
fI->body[i] = c;
fI->length++;
i++;
break;
case -1://sobreescritura
if(mode == 1){fprintf(stderr,"Error de sobrescritura al leer\n");}
break;
case -2://no queda nada que leer
//evaluamos la trama
fI->type = evaluateFrame(fI);
if(mode == 1)
{
printf("\n-------------------------------RECEIVED------------------------------------\n");
if(fI->type != -1)
{
SetConsoleTextAttribute(hstdoutI, FOREGROUND_GREEN); //Letra verde para trama correcta
}
else
{
SetConsoleTextAttribute(hstdoutI, FOREGROUND_RED); //Letra roja para trama incorrecta
}
for(u=0;u<fI->length;u++)
{
printData(fI->body[u]);
printf(" ");
}
printf("\n");
SetConsoleTextAttribute(hstdoutI, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); //Letra blanca
}
//indicamos que hay una trama lista
ReleaseSemaphore(side_I_data_ready,1,NULL);
//reiniciamos los valores de control
first=1;
flag = 1;
break;
case -3:
fprintf(stderr,"\nComunication off\n");
conexionOK = 0;
ExitThread(1);
break;
}
}
}
}
else//LADO DERECHO (RECEPTOR)
{
while(1)
{
flag = 0;
while(flag ==0)
{
if(first == 1)//es el primer caracter, por tanto esperamos hasta que exista uno en el buffer que poder leer
{
f_esperar_leer(INFINITE);
clearFrame(f);//aprovechamos para limpiar la trama
i=0;
}
else
{
f_esperar_leer(800);//no es el primera caracter
}
ret=f_leer(&c);
first = 0;
switch (ret)
{
case 0://sin problemas al leer
fD->body[i] = c;
fD->length++;
i++;
break;
case -1://sobreescritura
if(mode == 1){fprintf(stderr,"Read overrrun error\n");}
break;
case -2://no queda nada que leer
//evaluamos la trama
fD->type = evaluateFrame(fD);
if(mode == 1)
{
printf("\n-------------------------------RECEIVED------------------------------------\n");
if(fD->type != -1)
{
SetConsoleTextAttribute(hstdoutD, FOREGROUND_GREEN); //Letra verde para trama correcta
}
else
{
SetConsoleTextAttribute(hstdoutD, FOREGROUND_RED); //Letra roja para trama incorrecta
}
for(u=0;u<fD->length;u++)
{
printData(fD->body[u]);
printf(" ");
}
printf("\n");
SetConsoleTextAttribute(hstdoutD, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); //Letra blanca
}
else
{
//para que cuando no este en modo debug muestre los datos que recibe
if(fD->type == dataFrame)
{
catchData(fD);
printf("\nReceived: %s",fD->data);
}
}
//indicamos que hay una trama lista
ReleaseSemaphore(side_D_data_ready,1,NULL);
//reiniciamos los valores de control
first=1;
flag = 1;
break;
case -3://la comunicacion se ha cortado
fprintf(stderr,"\nComunication offn\n");
conexionOK = 0;
fD->type = -5;
ExitThread(1);
break;
}
}
}
}
}
DWORD evaluateFrame(struct Frame *f)
{
int i;
unsigned short crc;
char head[5] = {DLE,SYN,DLE,SYN,DLE};
for(i=0;i<5;i++)//la cabecera sera siempre la misma para todos
{
if(f->body[i] != head[i]){return -1;}
}
if(f->body[i] == ENQ){f->type = enqFrame; return enqFrame;}//ENQ
if(f->body[i] == ACK){f->type = ackFrame; return ackFrame;}//ACK
if(f->body[i] == EOT){f->type = eotFrame; return eotFrame;}//EOT
if(f->body[i] != SOH){return -1;};//SOH
i++;
if( (f->body[i] != '0' && f->body[i] != '1'))//numero de secuencia(es un caracter, hay que tratarlo como tal)
{
return -1;
}
else
{
if(f->body[i] == '0')
{
f->nSecuence = 0;
}
else
{
f->nSecuence = 1;
}
}
i++;
if(f->body[i] != DLE){return -1;};//DLE
i++;
if(f->body[i] == STX)//DATOS
{
i++;
while(f->body[i] != DLE && f->body[i+1] != ETX)
{
// DLE dentro de data
if(f->body[i] == DLE)
{
if(f->body[i+1] != DLE){return -1;}
}
i++;
if(i == MTU){return -1;}//no llega nunca el ETX, la trama esta mal
}
if(f->body[i] != DLE){return -1;}//DLE
i++;
if(f->body[i] == ETX)//ETX
{
//CRC
catchData(f);
crc =icrc(0,(unsigned char *)f->data,strlen(f->data), 255, -1);
if(LOBYTE(crc)!=(unsigned char)f->body[i+1]){return -1;}
if(HIBYTE(crc) !=(unsigned char)f->body[i+2]){return -1;}
f->type = dataFrame;
return dataFrame;
}
}
return -1;
}
DWORD L_CONNECT_request(long source, long destination, int priority)//envia ENQ
{
struct Frame *f;
int trys = 0;
trysStadistics = 0;
received=0;
f = (struct Frame*) malloc(sizeof(Frame));
if(mode == 1){printf("\nL_CONNECT_request\n");}
while(trys != maxTrys)
{
//actualizamos la variable maximo de trys intentados
if(trysStadistics<trys) {trysStadistics=trys;}
//creamos y enviamos la trama ENQ
makeFrame(enqFrame, NULL);
//esperamos hasta que haya algo para leer(quizas sea ACK o quizas no)
WaitForSingleObject(side_I_data_ready,timeout);
//actualizamos las recibidas
received++;
switch(fI->type)
{
case ackFrame:
//actualizamos las correctas
correct++;
return 0;
break;
default:
trys++;
break;
}
}
//si finalmente se han agotado los intentos es que no hemos conseguido contactar cn el receptor
return 1;
}
DWORD L_CONNECT_indication(long source, long destination, int priority)//comprueba que le ha llegado ENQ
{
do
{
//esperamos a que nos indique que hay datos para leer
WaitForSingleObject(side_D_data_ready,timeout);
//actualizamos las recibidas
received++;
switch(fD->type)
{
case enqFrame:
//actualizamos las correctas
correct++;
if(mode == 1){printf("\nL_CONNECT_indication\n");}
return 0;
break;
case eotFrame:
//actualizamos las correctas
correct++;
return 1;
break;
case -5://se ha cortado la comunicacion
return 1;
break;
default://lo que ha llegado es incorrecto de lo que se esperaba
return 2;
break;
}
}while(conexionOK != 1);
}
DWORD L_CONNECT_response(long source, long destination, int priority)//envia ACK
{
int ret;
HANDLE c;
if(mode == 1){printf("\nL_CONNECT_response\n");}
//abrimos el semaforo que indicara que la conexion se ha realizado
c = OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"semaphoreConexion");
//creamos y enviamos la trama ACK
makeFrame(ackFrame,NULL);
//esperamos hasta que nos confirmen la conexion
ret = WaitForSingleObject(c,timeout);
//si no se ha complido el timeout significa que hemos conseguido conectarnos
if(ret != WAIT_TIMEOUT){conexionOK = 1;}
return 0;
}
DWORD L_CONNECT_confirm(long source, long destination, int priority)//comprueba que le ha llegado ACK
{
if(mode == 1){printf("\nL_CONNECT_confirm\n");}
//indicamos que se ha realizado la conexion
conexionOK = 1;
//confirmamos al emisor que hemso conectado
ReleaseSemaphore(conexion,1,NULL);
if(mode ==1){printf("\nConnected\n");}
return 0;
}
DWORD L_DATA_request(long source, long destination, char* data)//envia los datos
{
int ret;
int trys = 0;
trysStadistics = 0;
if(mode == 1){printf("\nL_DATA_request\n");}
if(side == 0)//si es el lado izquierdo seran datos normales (EMISOR)
{
makeFrame(dataFrame,data);
while(trys != maxTrys && conexionOK == 1)
{
//actualizamos los trys
if(trysStadistics<trys){trysStadistics=trys;}
//esperamos a que haya datos listos
ret = WaitForSingleObject(side_I_data_ready,timeout);
if(ret == WAIT_TIMEOUT)//se ha cumplido el timeout, tenemos que reenviar la trama
{
trys++;
makeFrame(dataFrame,data);
}
else//ha llegado un dato al buffer, vemos que es
{
//actualizamos recibidos
received++;
switch(fI->type)
{
case ackFrame:
//actualizamos correctas
correct++;
//nos ha llegado confirmacion de que ha llegado el dato, cmbiamos la trama esperada
if(nSecuenceNext == 0)
{
nSecuenceNext = 1;
}
else
{
nSecuenceNext = 0;
}
break;
case dataFrame:
//actualizamos correctas
correct++;
//cmbiamos el T-siguiente y salimos para llamar a indication
if(nSecuenceNext == 0)
{
nSecuenceNext = 1;
}
else
{
nSecuenceNext = 0;
}
return 0;
break;
}
}
}
return 1;
}
else//si es el lado derecho tiene que traducirlos a morse(RECEPTOR)
{
if(data == NULL)//en esta llamada no tiene datos que enviar, solo espera por ellos
{
//esperamos por los datos
ret = WaitForSingleObject(side_D_data_ready,timeout);
//antes de hacer cualquier cosa comprobamos si seguimos conectados
if(conexionOK == 0){return 1;}
//se ha cumplido el timeout, no ha llegado nada
if(ret == WAIT_TIMEOUT){return 3;}
//actualizamos los recibidos
received++;
switch(fD->type)
{
case dataFrame:
//actualizamos correctas
correct++;
//el numero de secuencia de la trama es el mismo que el esperado, por tanto lo cambiamos
if(fD->nSecuence == nSecuenceWait)
{
if(nSecuenceWait == 0)
{
nSecuenceWait = 1;
}
else
{
nSecuenceWait = 0;
}
return 0;
}
else
{
//no es el mismo, por tanto tenemos que reenviar el dato anterior
return 0;
}
break;
case eotFrame:
//actualizamos correctas
correct++;
return 1;
break;
default://no ha llegado nada correcto
return 3;
break;
}
}
else
{
if(conexionOK == 0){return 1;}//si por cualquier casual se corta la conexion
//tenemos que enviar el dato en morse
makeFrame(morseFrame,data);
return 2;
}
}
}
DWORD L_DATA_indication(long source, long destination)
{
if(mode == 1){printf("\nL_DATA_indication\n");}
if(side == 0)//nos llega un dato correcto en el emisor, por tanto, lo obtenemos
{
catchData(fI);
}
else//nos llega un dato correcto en el receptor y por tanto tenemos que mandar ACK y obtenerlo
{
makeFrame(ackFrame,NULL);
catchData(fD);
}
return 0;
}
DWORD L_DISCONNECT_request(long source, long destination)
{
int ret;
if(mode == 1){printf("\nL_DISCONNECT_request\n");}
ret = makeFrame(eotFrame, NULL);
//estadisticas
printStadistics();
//para que no se cierre la tuberia antes de enviar
Sleep(2000);
//cerramos todos los ipcs
liberateDll();
CloseHandle(side_I_data_ready);
CloseHandle(conexion);
CloseHandle(waitingI);
printf("\nDisconnect\n");
return 0;
}
DWORD L_DISCONNECT_indication(long source, long destination, int reason)
{
if(mode == 1){printf("\nL_DISCONNECT_indication\n");}
//estadisticas
printStadistics();
//cerramos todos los ipcs
liberateDll();
CloseHandle(side_D_data_ready);
CloseHandle(waitingD);
printf("\nDisconnect\n");
return 0;
}
DWORD printData(char c)
{
switch(c)
{
case 0x01:
printf("SOH ");
break;
case 0x02:
printf("STX ");
break;
case 0x03:
printf("ETX ");
break;
case 0x04:
printf("EOT ");
break;
case 0x05:
printf("ENQ ");
break;
case 0x06:
printf("ACK ");
break;
case 0x10:
printf("DLE ");
break;
case 0x15:
printf("NAK ");
break;
case 0x16:
printf("SYN ");
break;
case 0x17:
printf("ETB ");
break;
case '0':
printf("0");
break;
case '1':
printf("1");
break;
default:
printf("%c ",c);