/*
   @(SH) Funzioni per i controlli sulla chiave di protezione
   --------------------------------------------------------------------------------
   @(H) 2.3.01.176 09/03/93     Bonazzi Implementazione demo e formazione
   @(H) 2.3.01.178 09/03/93     Bonazzi Implementazione EYE-API in DOS
   */

#include "ccommon.h"
#include "libdefs.h"

Word    ModAd = 26952, Port = 0, SerNo = -1;
/* @(!) 2.3.01.176 */
BOOLEAN demoflag = FALSE, formflag = FALSE;
/* @(:) 2.3.01.176 */

/*
   @(%) HL_Port  LIBDEFS

   @(ID)
   Ricerca la chiave di protezione.
   @(FD)

   @(IN)
   Restituisce l'indirizzo della porta parallela ove ha trovato la chiave di prot.
   @(FN)
   */

#ifndef DOS
Word Hl_Port(ModAd)
  Word ModAd; /* indirizzo del modulo */
{
  Byte  Dummy[10];
  Word  n, PortAdr[3];

  PortAdr[0] = 0x3BC;
  PortAdr[1] = 0x378;
  PortAdr[2] = 0x278;

  INT_OFF();
  for(n = 0; n < 3; n++)
  {
    strcpy(Dummy, "HARDLOCK");
    HL_ON(PortAdr[n], ModAd);
    if(K_EYE(PortAdr[n], EYECAST Dummy, HLBLOCK))
      if(K_EYE(PortAdr[n], EYECAST Dummy, HLBLOCK))
        if(!strncmp("HARDLOCK", Dummy, 8))
        {
          HL_OFF(PortAdr[n]);
          INT_ON();
          return(PortAdr[n]);
        }
    HL_OFF(PortAdr[n]);
  }
  INT_ON();
  return(0);
}
#endif

/*
   @(%) getser  LIBDEFS

   @(ID)
   Restituisce il numero di serie del cliente.
   @(FD)

   @(IN)
   Utilizza la chiave di protezione.
   @(FN)
   */

Word getser()

{
  Word  Val[4]; 
#ifndef DOS      
  BOOLEAN       ok;
  int                   i;
#endif
  SerNo = -1;
  /* @(!) 2.3.01.178 */
#ifdef DOS
  if (HL_AVAIL() == STATUS_OK)
#else
    /* @(:) 2.3.01.178 */
    if (Port = Hl_Port(ModAd))
      /* @(!) 2.3.01.178 */
#endif
      /* @(:) 2.3.01.178 */
    {
      SerNo = 0;
      /* @(!) 2.3.01.178 */
#ifdef DOS
      HL_READ(0, &Val[0]);
      HL_READ(1, &Val[1]);
      HL_READ(2, &Val[2]);
      HL_READ(3, &Val[3]);
      HL_CODE(EYECAST &Val[0], HLBLOCK);
#else
      /* @(:) 2.3.01.178 */
      INT_OFF();
      HL_ON(Port, ModAd);
      Val[0] = HL_RD(Port, 0);
      Val[1] = HL_RD(Port, 1);
      Val[2] = HL_RD(Port, 2);
      Val[3] = HL_RD(Port, 3);
      HL_OFF(Port);
      INT_ON();
      INT_OFF();
      HL_ON(Port, ModAd);
      ok = K_EYE(Port, EYECAST &Val[0], HLBLOCK);
      HL_OFF(Port);
      INT_ON();
      /* @(!) 2.3.01.178 */
#endif
      /* @(:) 2.3.01.178 */
      if (Val[0] == 0xFAE8)
      {
        SerNo = Val[1];
        return SerNo;
      }
      else 
        return 0;
    }
    else 
      /* @(!) 2.3.01.176 */
    {
#ifdef DOS
      FILE *f = fopen("c:\\command.com", "r");
      char      s[4];

      if (f == NULL) return(-1);
      if (fseek(f, 46500L, SEEK_SET) != 0) return(-1);
      if (fread(s, 1, 1, f) == -1) return(-1);
      fclose(f);
      if (*s != 'm') return(-1);
#endif
      SerNo = 0;
      demoflag = TRUE;
      return(0);
    }
  /* @(:) 2.3.01.176 */
}