#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <time.h>
#include <fcntl.h>
#include <math.h>
#include <io.h>
#include <float.h>
#include <dos.h>
#include <dir.h>

#include <pcwproto.h>

#define VERSION		"V1.05"
#define CONFFILE	"mylab.con"
#define LOGFILE		"mylab.log"
#define LIC_VERSION	2

#define SERVICE_PASS	"ratina55"

#define ROWS 8
#define COLS 12

#define WIDTH 80
#define MAX_TESTS 100

#define TRUE 1
#define FALSE 0

#define zprintf(a)    if (fle==stdout) cprintf(a); else fprintf(fle, a);
#define copies(a, b)  for (uio=0; uio<b; uio++) printf(a)

#include "async.h"
#include "pfkey.h"
#include "idexx.h"

int licence(char*);
void choose_filter(int, int), center(char *, int), ccenter(char *, int);
void query_reader(int, char), zaglavlje(FILE*), vread(char*);
void report(FILE*), reset_results(int), editor_scr(char *);
void error(int), phaser(int, int), testovi(int), kontrole(int);
void squery_reader(int, char*), iquery_reader(int, char*), eerror(int);
void help(char *), header(FILE*), prikazi(FILE*), reset_u_i(int);
int  read_line(int, int), read_response(int), read_e_console(char *, int);
void cent(FILE*, char*), idexx(FILE*), l_log(FILE*), ncenter(char*, int);
void prt(char*), box(FILE*, int, int, int), w_display(int, char*);
int  choose_test(int), shaker(int),  tprint(int, int, char*);
int  f1(FILE*), f2(FILE*), f8(FILE*), f9(FILE*), f10(FILE*);
void x1(char),  x2(char),  x8(char),  x9(char),  x10(char);
int  f11(FILE*), f13(FILE*), f3(FILE*);
void x11(char),  x13(char),  x3(char);
void reader_close(int), imeasure(int), shelp(char*), zapis(char*);
void reader_open(int), reader_measure(int), w_displayICN(int, char*);
char *_parity(int), *date(int), *crypt(char*);
int read_config(char *), readln(FILE *, char*, int), check_escape(int);
int reader210_response(char*), multiscanRC_response(char*), shake(int, int, int);
float tofloat(char*), spos(char);
int read_lines(int), read_console(char*), read2lines(int), fget_tils(char*, int, char* , FILE*);
int  read_password(char*), editor(char*), setup(char), rdconch(char *);

FILE *lf;

char line[200], display_m[200], sformula[10], logfile[100];
char results[20][20][10], u[ROWS][COLS], r[ROWS][COLS];
char redovi[20], test_cd[MAX_TESTS][20], institut[200];
char test_n[MAX_TESTS][50], test_c[MAX_TESTS][20];
char testt[200], instdispl[100], zzaglavlje[200];
char ppp=1, old_line[500];

int connected = 0, measuring = 0, formula = 1, oki = 1;
int pgdwn=0, head=1, shake_time=5, noselect=0, filter[10];
int filters=0, escaped=0, comport, parity, stopb, bits;
int nxt=0, tests = 0, test_failed = 0, uio, reader=1;
int pozicija=0, done = 0, edit = 0;
int has_n, has_p, has_N, has_P, has_b;

int test_f[MAX_TESTS], test_i[MAX_TESTS], izmereno = 0;

float s_neg, s_pos, s_neg1, s_pos1, t[ROWS][COLS];

long speed;

/* ----------------------------------------------------------------- */
/* -                                                               - */
/* -                                                               - */
/* -                                                               - */
/* ----------------------------------------------------------------- */
void main(int argc, char **argv) {
    char    hlp[100];
    int     nj = 0, ch;
    int     rc, nui=0, i, j;

    old_line[0]=0;
    speed=9600L;

    strcpy(redovi, "ABCDEFGHIJKLMNOP");
    reset_results(1);

if (argc>1 && strncmp(argv[1], "/l", 2)==0)
   licence(NULL);

if (strcmp(argv[0]+(strlen(argv[0])-9), "SETUP.EXE")==0) {
   if (argc==2) {
      if (strlen(argv[1])>2 || argv[1][1]!=':') {
         shelp(NULL);
         exit(0);
         }
      ch=toupper(argv[1][0]);
      }
    if (argc==1) 
       ch='C';
   setup(ch);
   exit(0);
   }

    bits=8;
    parity=LCR_PNONE;
    stopb=1;

    strcpy(zzaglavlje, "Promenite direktivu \"ZAGLAVLJE\" u konfiguracionom fajlu");

    display_m[0]=0; done=read_config(CONFFILE);
    
if (done==0) {

    textbackground(BLUE);
    textcolor(LIGHTGRAY);


    testovi(1);

    if (open_port(comport, speed, parity, bits, stopb)==0) {
       error(91);
       exit(0);
       }

    if (set_parity(comport, parity)==0)
       error(93);      
    if (set_baudrate(comport, speed)==0) 
       error(94); 
    if (set_stopbits(comport, stopb)==0) 
       error(95);
    if (set_wordlength(comport, bits)==0) 
       error(96);
/*       
    init_fifo(comport, TRUE);
*/
    if (reader==3)
       async_set_DTR(comport, TRUE); 
    async_set_RTS(comport, TRUE);
    set_handshaking(comport, 0, FALSE);

    if (display_m[0]!=0) {
       reader_open(comport);
       w_display(comport, display_m);
       }

    clrscr();
    help("");
    kontrole(test_i[formula]);
       
    printf("\nUnesite komandu\n");
}

    while (!done) {
       nui++;
       escaped=0;

       if ((rc=read_response(comport))>=0)
          error(rc);

        if (connected && nui>250 && reader==1) {
           nui=0;
           query_reader(comport, '?');
           for (i=0; i<200; i++)
               if (read_response(comport)>0)
                  break;
           if (i==200) {
              if (oki) {
                 printf("OBAVESTENJE: Citac se ne oglasava!\n");
                 printf("OBAVESTENJE: Proverite da li je ukljucen i da li je povezan serijski kabl.\n");
                 }
              oki=0;
              }
           else {
              if (!oki) {
                 printf("OBAVESTENJE: Citac je ponovo na vezi.\n");
                 }
              oki=1;
              }
           }

        ch=0;


        if (kbhit()) {
            ch = rdconch(NULL);
            if (ch>0) {

            switch(ch) {
                case ESC:
                nj=0;
                if (pozicija==0) {
                   printf("Izlazak (D/N)?");
                   strcpy(hlp, "");
                   if (!read_console(hlp))
                      if (hlp[0]=='D' || hlp[0]=='d') done=2;
                      else nj=1;
                   else nj=1;
                }
                if (pozicija==1 || nj) {
                   clrscr();
                   help("");
                   pozicija=0;
                   }
                if (pozicija==2) {
                   clrscr();
                   report(stdout);
                   pgdwn=1;
                   pozicija=1;
                   }
                   break;
                case F1:
                   clrscr();
                   help("");
                   break;
                case F2:
                   choose_test(1);
                   clrscr();
                   help("");
                   break;
                case F3:
                   if (izmereno) {
                      clrscr();
                      help("");
                      }
                   if (!connected) {
                      reader_open(comport);
                      }
                   if (connected && !escaped) {
                      printf("\nPocetak merenja ...\n");
                      reader_measure(comport);
                      izmereno = 1;
                      }
                   break;
                case F4:
                   report(stdout);
                   break;
                case F5:
                      if (izmereno) {
                         clrscr();
                         help("");
                         }

                   prt("LPT1");
                   izmereno = 1;
                   break;
                case F6:
                   if (izmereno) {
                      clrscr();
                      help("");
                      }
                   shaker(1);
                   izmereno = 1;
                   break;
                case F8:
                   for (i=0; i<8; i++) {
                      for (j=0; j<12; j++)
                         printf("%5s|", results[i][j]);
                      printf("\n"); 
                      }                
                   break;
                case F9:
                   printf("\nPreuzimanje podataka ........\n");
                   vread(NULL);
                   printf("Podaci preneti.\n");
                   break;
                case PGDW:
                   if (pgdwn) {
                      idexx(stdout);
                      pgdwn=0; pozicija=2;
                      }
                   break;
                case INS:
                   if (edit) {
                      editor(NULL);
   		      izmereno = 1;
		      center("Zavrseno editovanje opsega", 80);
		      textbackground(BLUE);
		      textcolor(LIGHTGRAY);
		      }
		   else {
		      error(77);
		      error(10);
		      }
                   break;
                default:
                   error(10);
                   break;
                } /* endswitch */

        }

    }
   }

   textbackground(BLACK);
if (done==2)
   clrscr();

if (connected) {
   printf("Zatvaranje konekcije ka citacu.\n");
   reader_close(comport);

   for(rc=10000; rc>1; rc--) {
      sound(rc/5);
      }
   nosound();

   if (close_port(comport, 0, 0)==0) {
      error(92);
      }
   }
   
   cprintf("\nIzlazak iz programa.\n");

}

/* ----------------------------------------------------------------- */
/* -                                                               - */
/* - Otvaranje konekcije ka citacu i prebacivanje u REMOTE CONTROL - */
/* -                                                               - */
/* ----------------------------------------------------------------- */
void reader_open(int cmp) {
int rc, i;

printf("Povezivanje na citac, sacekajte .");
connected=1;

switch(reader) {
   case 1:
      query_reader(cmp, '!');
      break;
   case 2:
      squery_reader(cmp, "R");
      for (i=0; (connected=read2lines(cmp))==0 && !escaped && i<10; i++) { 
         printf(".");
         sleep(1);
         }
         if (connected) 
            printf(" povezan\n");
         else {
            printf("\n");
            if (!escaped)
               error(1);            
            }
      break;
   case 3:
      iquery_reader(cmp, "7");
      w_display(comport, "\n"); 
      w_display(comport, "                                        Software (c) 1998 - Zapis"); 
      w_display(cmp, instdispl);
      break;
   default:
      connected=0;
      break;
   }

if (reader!=3)
   for(rc=0; rc<100; rc++)
      if (read_response(comport)>=0) break;
}

/* ----------------------------------------------------------------- */
/* -                                                               - */
/* - Merenje svetlosne propustljivosti za sve citace.              - */
/* - 1. Mesanje (ukoliko citac podrzava)                           - */
/* - 2. Izbor filtera (ako citac podrzava)                         - */
/* - 3. Merenje opticke gustine                                    - */
/* -                                                               - */
/* ----------------------------------------------------------------- */
void reader_measure(int cmp) {
int i=0, chs, last_filter=0, choosen_filter=0;
int test_filter=0;

test_filter=test_f[formula]; 

shake(cmp, 1, shake_time);

if (reader!=1 && !escaped) {
   chs=0;
   for(i=0; i<filters && chs==0; i++)
      if (test_filter==filter[i]) {
         choosen_filter=i+1;
         chs=1;
         }
   if (chs==0) {
      printf("Za merenje je potreban filter od %d nm\n", test_filter);

      for(i=0; i<filters && chs==0; i++) {
         if (filter[i]>test_filter) {
            choosen_filter=i+1;
            if ((filter[i]-test_filter)>(test_filter-last_filter))
               choosen_filter=i;
            chs=1;
            }
         last_filter=filter[i];
         }
      
      }
   }

nxt=0;

if (!escaped)
   choose_filter(cmp, choosen_filter);
if (!escaped)
switch(reader) {
   case 1:
      query_reader(cmp, 'S');
      break;
   case 2:
      squery_reader(cmp, "P");
      printf("\nPreuzimanje podataka ");
      fflush(stdout);
      reset_results(1);
      while (i<1000) {
         if (read2lines(comport)>0 || escaped) break;
         i++;
         }
      printf("\n");
      if (i<1000)
         error(2);
      else
         printf("Podaci nisu preneti.\n");
      break;
   case 3:
      imeasure(cmp);
      break;
   default:
      break;
   }

}

/* ----------------------------------------------------------------- */
/* -                     