/*=====================================================================
  ===============          MATRIX32 API               =================
  =====================================================================
  ===============   (Linux API for LPT/USB Dongle)    =================
  =====================================================================
  ===============       Copyright (c) TDi GmbH        =================
  =====================================================================
  ========  Abstract:  Open LPT/USB Dongle and show info   ============
  =====================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "mxapi.h"

static
_mxINT16
get_major (_mxINT32 v)
{
  v >>= 16;
  return (_mxINT16) v;
}

static
_mxINT16
get_minor (_mxINT32 v)
{
  return (_mxINT16) v;
}


int
main (int argc, char **argv)
{
  _mxINT16 s;
  _mxINT16 p;
  _mxINT32 version;
  _mxINT32 model;
  _mxINT16 memsize;

  s = Init_MatrixAPI();
  if (s != 0)
    {
      fprintf (stderr,"Init_MatrixAPI() failed with %" PRId16 "\n",s);
      if (s == -20)
      {
        fprintf (stderr,"-20 = LPT Driver Access Error\n");
      }
    }
  
  s = Dongle_Find();
  if (s == 0)
    {
      fprintf (stderr,"No dongles connected\n");
      return 1;
    }

  if (s < 0)
    {
      fprintf (stderr,"Dongle_Find() failed with %"PRId16"\n",s);
      return 1;
    }

  switch (s)
    {
    case 'U':
      fprintf (stdout,"There is a MXSPS dongle connected to USB.\n");
      break;
    case 1:
    case 2:
    case 3:
      fprintf (stdout,"There is a MXSPS dongle at LPT%"PRId16".\n",s);
      break;
    default:
      fprintf (stderr,"Unexpected return value %"PRId16" for Dongle_Find()\n",s);
    }

  p = s; // remember the port we have a dongle connected to

  // print Dongle Version, Model and MemSize
  model   = Dongle_Model   (1, p);
  version = Dongle_Version (1, p);
  memsize = Dongle_MemSize (1, p);

  printf ("Model:   %" PRId32 "\n", (int) model);
  printf ("Version: %" PRId16 ".%" PRId16 "\n", get_major(version), get_minor(version));
  printf ("Memory:  %" PRId16 "\n", memsize);
  
  return 0;
}



