using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles; 

namespace RogerMfReaderExample.RUD3
{
    class rud3
    {
        internal const UInt32 RUD3_OK = 0x00;
        internal const UInt32 RUD3_ERROR = 0x01;
        internal const UInt32 RUD3_DEVICE_NOT_FOUND = 0x02;
        internal const UInt32 RUD3_DEVICE_CANNOT_OPEN = 0x04;
        internal const UInt32 RUD3_INVALID_BUFFER_SIZE = 0x05;
        internal const UInt32 RUD3_EPSO_ERROR = 0x06;
        internal const UInt32 RUD3_USB_HID_ERROR = 0x07;
        internal const UInt32 RUD3_TIMEOUT = 0x08;
        internal const UInt32 RUD3_CARD_NOT_FOUND = 0x09;
        internal const UInt32 RUD3_DEVICE_CANNOT_CLOSE = 0x0A;
        internal const UInt32 RUD3_WRITE_ERROR = 0x0B;
        internal const UInt32 RUD3_READ_ERROR = 0x0C;

        internal const UInt32 FTD_UNKNOW = 0x00;
        internal const UInt32 FTD_RUD3 = 0x03;

        public const UInt16 ALL_DEVICES = 0x0000;
        public const UInt16 RUD_3 = 0x0001;
        public const UInt16 RUD_2 = 0x0006;
        public const UInt16 RUD_4 = 0x000D;

        // ******************************************************************************
        // API functions
        // ******************************************************************************

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3LibVer(StringBuilder Version, Int32 Length);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetDeviceCount(ref UInt32 DeviceCount, UInt16 DeviceType);

        static public UInt32 rud3GetDeviceCount(ref UInt32 DeviceCount)
        {
            return rud3GetDeviceCount(ref DeviceCount, ALL_DEVICES);
        }

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetSerialNumber(UInt32 DeviceCount, UInt16 DeviceType, StringBuilder SerialNumber);

        static public UInt32 rud3GetSerialNumber(UInt32 DeviceCount, StringBuilder SerialNumber)
        {
            return rud3GetSerialNumber(DeviceCount, ALL_DEVICES, SerialNumber);
        }

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetIndex(StringBuilder SerialNumber, UInt32 DeviceCount);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetDescriptionBySerialNumber(StringBuilder SerialNumber, StringBuilder Description);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetDescriptionByIndex(UInt32 DeviceIndex, StringBuilder Description);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetPort(StringBuilder SerialNumber, StringBuilder Port);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3GetDeviceType(StringBuilder SerialNumber, UInt32 DeviceType);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3Open(ref IntPtr RudHandle, StringBuilder SerialNumber);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3Close(ref IntPtr RudHandle);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3Write(ref IntPtr RudHandle, String Buff, UInt32 BuffLength, out UInt32 NumberOfByteWriten, UInt32 TimeOut);

        [DllImport("RogerRud3.dll", CallingConvention = CallingConvention.Cdecl)]
        static public extern UInt32 rud3Read(ref IntPtr RudHandle, StringBuilder Buff, UInt32 BuffLength, out UInt32 NumberOfByteReaden, UInt32 TimeOut);

    }
}

