/*=====================================================================
  ==============    MATRIX Software Protection System    ==============
  ==============                                         ==============
  ==============        Mac-OSX: Example for C/C++       ==============
  =====================================================================
  ==============          Copyright (c) TDi GmbH         ==============
  =====================================================================
  ============== Abstract: Open USB Dongle and show info ==============
  =====================================================================*/

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

#include "mxapi.h"

static
short
get_major (long v)
{
  v >>= 16;
  return (short) v;
}

static
short
get_minor (long v)
{
  return (short) v;
}


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

  s = Init_MatrixAPI();
  if (s != 0)
    {
      fprintf (stderr,"Init_MatrixAPI() failed with %d\n",s);
      return 1;
    }
  
  s = Dongle_Find();
  if (s == 0)
    {
      fprintf (stderr,"No dongles connected\n");
      return 1;
    }

  if (s < 0)
    {
      fprintf (stderr,"Dongle_Find() failed with %d\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%d.\n",s);
      break;
    default:
      fprintf (stderr,"Unexpected return value %d 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:   %ld\n", model);
  printf ("Version: %hd.%hd\n", get_major(version), get_minor(version));
  printf ("Memory:  %hd\n", memsize);
  
  return 0;
}



