unit slcan;

interface

uses Windows;

const
   SLCAN_PROPERTY_INDEX_LINKNAME       = 0;
   SLCAN_PROPERTY_INDEX_INSTANCEID     = 1;
   SLCAN_PROPERTY_INDEX_DEVICEDESC     = 2;
   SLCAN_PROPERTY_INDEX_FRIENDLYNAME   = 3;
   SLCAN_PROPERTY_INDEX_PHOBJECTNAME   = 4;
   SLCAN_PROPERTY_INDEX_MFG            = 5;
   SLCAN_PROPERTY_INDEX_LOCATIONINFO   = 6;
   SLCAN_PROPERTY_INDEX_ENUMERATOR     = 7;
   SLCAN_PROPERTY_INDEX_CLASS          = 8;
   SLCAN_PROPERTY_INDEX_CLASSGUID      = 9;
   SLCAN_PROPERTY_INDEX_SERVICE        = 10;
   SLCAN_PROPERTY_INDEX_DRIVER         = 11;
   SLCAN_PROPERTY_INDEX_PORTNAME       = 12;
   SLCAN_PROPERTY_INDEX_PRODUCT        = 13;
   SLCAN_PROPERTY_INDEX_MANUFACTURER   = 14;
   SLCAN_PROPERTY_INDEX_CONFIGURATION  = 15;
   SLCAN_PROPERTY_INDEX_INTERFACE      = 16;
   SLCAN_PROPERTY_INDEX_SERIAL         = 17;
   SLCAN_PROPERTY_INDEX_ALIAS          = 18;
   SLCAN_PROPERTY_INDEX_CHANNELLINK    = 19;
   SLCAN_PROPERTY_INDEX_SERIALID       = 20;

   SLCAN_MODE_CONFIG       = $00;
   SLCAN_MODE_NORMAL       = $01;
   SLCAN_MODE_LISTENONLY   = $02;
   SLCAN_MODE_LOOPBACK     = $03;
   SLCAN_MODE_SLEEP        = $04;

   SLCAN_BR_CIA_1000K      = $8000;
   SLCAN_BR_CIA_800K       = $8001;
   SLCAN_BR_CIA_500K       = $8002;
   SLCAN_BR_CIA_250K       = $8003;
   SLCAN_BR_CIA_125K       = $8004;
   SLCAN_BR_CIA_50K        = $8005;
   SLCAN_BR_CIA_20K        = $8006;
   SLCAN_BR_CIA_10K        = $8007;
   SLCAN_BR_400K           = $8008;
   SLCAN_BR_200K           = $8009;
   SLCAN_BR_100K           = $800A;
   SLCAN_BR_83333          = $800B;
   SLCAN_BR_33333          = $800C;
   SLCAN_BR_25K            = $800D;
   SLCAN_BR_5K             = $800E;
   SLCAN_BR_30K            = $800F;
   SLCAN_BR_300K           = $8010;
   SLCAN_BR_LASTINDEX = SLCAN_BR_33333;

   SLCAN_CAP_MODE_NORMAL         = $01;
   SLCAN_CAP_MODE_LISTEN_ONLY    = $02;
   SLCAN_CAP_MODE_LOOP_BACK      = $04;
   SLCAN_CAP_MODE_SLEEP          = $08;

   SLCAN_CAP_TXMODE_ONE_SHOT     = $01;
   SLCAN_CAP_TXMODE_TIME_STAMP   = $02;

   SLCAN_CAP_CONTR_EXTERNAL = $00;
   SLCAN_CAP_CONTR_MCP2515  = $01;
   SLCAN_CAP_CONTR_SJA1000  = $02;

   SLCAN_CAP_CONTR_INTERNAL = $80;
   SLCAN_CAP_CONTR_LPC      = $81;
   SLCAN_CAP_CONTR_STM32    = $82;
   SLCAN_CAP_CONTR_STM8     = $83;
   SLCAN_CAP_CONTR_PIC      = $84;
   SLCAN_CAP_CONTR_PIC_ECAN = $85;

   SLCAN_CAP_PHYS_HS    = $01;
   SLCAN_CAP_PHYS_LS    = $02;
   SLCAN_CAP_PHYS_SW    = $04;
   SLCAN_CAP_PHYS_J1708 = $08;
   SLCAN_CAP_PHYS_LIN   = $10;
   SLCAN_CAP_PHYS_KLINE = $20;

   SLCAN_CAP_PHYS_LOAD = $01;

   SLCAN_CAP_BITRATE_INDEX  = $01;
   SLCAN_CAP_BITRATE_CUSTOM = $02;
   SLCAN_CAP_BITRATE_AUTO   = $04;

   SLCAN_EVT_LEVEL_RX_MSG     = 0;
   SLCAN_EVT_LEVEL_TIME_STAMP = 1;
   SLCAN_EVT_LEVEL_TX_MSG     = 2;
   SLCAN_EVT_LEVEL_BUS_STATE  = 3;
   SLCAN_EVT_LEVEL_COUNTS     = 4;
   SLCAN_EVT_LEVEL_ERRORS     = 5;

   SLCAN_EVT_TYPE_RX                = $0;
   SLCAN_EVT_TYPE_START_TX          = $1;
   SLCAN_EVT_TYPE_END_TX            = $2;
   SLCAN_EVT_TYPE_ABORT_TX          = $3;
   SLCAN_EVT_TYPE_BUS_STATE         = $4;
   SLCAN_EVT_TYPE_ERROR_COUNTS      = $5;
   SLCAN_EVT_TYPE_BUS_ERROR         = $6;
   SLCAN_EVT_TYPE_ARBITRATION       = $7;
   SLCAN_EVT_TYPE_STAMP_INC         = $F;

   SLCAN_BUS_STATE_ERROR_ACTIVE      = $00;
   SLCAN_BUS_STATE_ERROR_ACTIVE_WARN = $01;
   SLCAN_BUS_STATE_ERROR_PASSIVE     = $02;
   SLCAN_BUS_STATE_BUSOFF            = $03;

   SLCAN_MES_INFO_EXT     = $01;
   SLCAN_MES_INFO_RTR     = $02;
   SLCAN_MES_INFO_ONESHOT = $04;

   SLCAN_DEVOP_CREATE        = $00000000;
   SLCAN_DEVOP_CREATEHANDLE  = $00000001;
   SLCAN_DEVOP_OPEN          = $00000002;
   SLCAN_DEVOP_CLOSE         = $00000003;
   SLCAN_DEVOP_DESTROYHANDLE = $00000004;
   SLCAN_DEVOP_DESTROY       = $00000005;
   SLCAN_DEVOP_INFO          = $00000006;
   SLCAN_DEVOP_USER          = $00000007;

   SLCAN_INVALID_HANDLE_ERROR          = $E0001001;
   SLCAN_DEVICE_INVALID_HANDLE_ERROR   = $E0001120;
   SLCAN_HANDLE_INIT_ERROR             = $E0001017;
   SLCAN_DEVICE_NOTOPEN_ERROR          = $E0001121;

   SLCAN_EVT_ERR_TYPE_BIT              = $00;
   SLCAN_EVT_ERR_TYPE_FORM             = $01;
   SLCAN_EVT_ERR_TYPE_STUFF            = $02;
   SLCAN_EVT_ERR_TYPE_OTHER            = $03;

   SLCAN_EVT_ERR_DIR_TX                = $00;
   SLCAN_EVT_ERR_DIR_RX                = $01;

   SLCAN_EVT_ERR_FRAME_SOF             = $03;
   SLCAN_EVT_ERR_FRAME_ID28_ID21       = $02;
   SLCAN_EVT_ERR_FRAME_ID20_ID18       = $06;
   SLCAN_EVT_ERR_FRAME_SRTR            = $04;
   SLCAN_EVT_ERR_FRAME_IDE             = $05;
   SLCAN_EVT_ERR_FRAME_ID17_ID13       = $07;
   SLCAN_EVT_ERR_FRAME_ID12_ID5        = $0F;
   SLCAN_EVT_ERR_FRAME_ID4_ID0         = $0E;
   SLCAN_EVT_ERR_FRAME_RTR             = $0C;
   SLCAN_EVT_ERR_FRAME_RSRV0           = $0D;
   SLCAN_EVT_ERR_FRAME_RSRV1           = $09;
   SLCAN_EVT_ERR_FRAME_DLC             = $0B;
   SLCAN_EVT_ERR_FRAME_DATA            = $0A;
   SLCAN_EVT_ERR_FRAME_CRC_SEQ         = $08;
   SLCAN_EVT_ERR_FRAME_CRC_DEL         = $18;
   SLCAN_EVT_ERR_FRAME_ACK_SLOT        = $19;
   SLCAN_EVT_ERR_FRAME_ACK_DEL         = $1B;
   SLCAN_EVT_ERR_FRAME_EOF             = $1A;
   SLCAN_EVT_ERR_FRAME_INTER           = $12;
   SLCAN_EVT_ERR_FRAME_AER_FLAG        = $11;
   SLCAN_EVT_ERR_FRAME_PER_FLAG        = $16;
   SLCAN_EVT_ERR_FRAME_TDB             = $13;
   SLCAN_EVT_ERR_FRAME_ERR_DEL         = $17;
   SLCAN_EVT_ERR_FRAME_OVER_FLAG       = $1C;

   SLCAN_TX_STATUS_OK                  =$00;
   SLCAN_TX_STATUS_TIMEOUT             =$01;
   SLCAN_TX_STATUS_BUSOFF              =$02;
   SLCAN_TX_STATUS_ABORT               =$03;
   SLCAN_TX_STATUS_NOT_ENA             =$04;
   SLCAN_TX_STATUS_ERROR_ONE_SHOT      =$05;
   SLCAN_TX_STATUS_INVALID_MODE        =$06;
   SLCAN_TX_STATUS_UNKNOWN             =$0F;

   SLCAN_PURGE_TX_ABORT                =$01;
   SLCAN_PURGE_RX_ABORT                =$02;
   SLCAN_PURGE_TX_CLEAR                =$04;
   SLCAN_PURGE_RX_CLEAR                =$08;
 type
//{$IFDEF CompilerVersion}
//{$IF CompilerVersion >= 16 }
//TSlCanDevice = NativeUInt;
//{$ELSE}
//TSlCanDevice = longword;
//{$ENDIF}
//{$ELSE}
TSlCanDevice = longword;
//{$ENDIF}


PSlCanCapabilities = ^TSlCanCapabilities;
TSlCanCapabilities = packed record
   bModes:byte;
   bTXModes:byte;
   bMaxEventLevel:byte;
   bController:byte;
   bPhysical:byte;
   bPhysicalLoad:byte;
   bBitrates:byte;
   bAdvancedModes:byte;
   dwCanBaseClk:longword;
   dwTimeStampClk:longword;
   wMaxBrp:word;
end;

PSlCanState = ^TSlCanState;
TSlCanState = packed record
   States: array [0 .. 1] of byte;
   ErrCountRX:byte;
   ErrCountTX:byte;
end;

PSlCanMessage = ^TSlCanMessage;
TSlCanMessage = packed record
   Info:byte;
   ID:longword;
   DataCount:byte;
   Data: array [0 .. 7] of byte;
end;

PSlCanTxMessage = ^TSlCanTxMessage;
TSlCanTxMessage = packed record
   dwDelay:longint;
   Msg:TSlCanMessage;
end;


PSlCanEvent = ^TSlCanEvent;
TSlCanEvent = packed record
   EventType:byte;
   TimeStampLo:longword;
   case integer of
   0:(Msg:TSlCanMessage);
   1:(TimeStamp: array [0 .. 1] of longword);
   2:(TimeStamp64: UInt64);
   3:(BusMode:byte;
      bDummy1:byte;
      ErrCountRX:byte;
      ErrCountTX:byte;
      ErrType:byte;
      ErrDir:byte;
      ErrFrame:byte;
      LostArbitration:byte);
end;

PSlCanBitRate = ^TSlCanBitRate;
TSlCanBitRate = packed record
   BRP:word;
   TSEG1:byte;
   TSEG2:byte;
   SJW:byte;
   SAM:byte;
end;

PSlCanTimeStamp = ^TSlCanTimeStamp;
TSlCanTimeStamp = packed record
   case integer of
   0:(Value:int64);
   1:(dwValue: array [0 .. 1] of longword);
   2:(wValue: array [0 .. 3] of word);
   3:(bValue: array [0 .. 7] of byte);
end;

TSlCan_DeviceCallBack = procedure(
   hDevice:       TSlCanDevice;
   dwIndex:       longword;
   Operation:     longword;
   pContext:      pointer;
   dwContextSize: longword
   );stdcall;

TSlCan_DeviceListCallBack = procedure(
   hDevice:       TSlCanDevice;
   dwIndex:       longword;
   pContext:      pointer;
   dwContextSize: longword
   );stdcall;

function SlCan_Load(
   DeviceProc:       TSlCan_DeviceCallBack;
   DeviceListProc:   TSlCan_DeviceListCallBack
   ):longbool;stdcall;

function SlCan_Free(
   bDoCallBack:longbool
   ):longbool;stdcall;

function SlCan_Update:longbool;stdcall;

function SlCan_GetDeviceCount:longword;stdcall;

function SlCan_GetDevice(
   dwIndex:longword
   ):TSlCanDevice;stdcall;

function SlCan_DeviceGetHandle(
   hDevice:TSlCanDevice
   ):THandle;stdcall;

function SlCan_DeviceGetProperty(
   hDevice: TSlCanDevice;
   dwIndex: longword;
   pBuf:    pAnsiChar;
   dwSize:  longword
   ):longword;stdcall;

function SlCan_DeviceGetPropertyW(
   hDevice: TSlCanDevice;
   dwIndex: longword;
   pBuf:    PWideChar;
   dwSize:  longword
   ):longword;stdcall;

function SlCan_DeviceGetRegKey(
   hDevice: TSlCanDevice;
   dwIndex: longword
   ):HKEY;stdcall;

function SlCan_DeviceSetContext(
   hDevice:TSlCanDevice;
   pBuf:     pointer;
   dwBufSize:longword
   ):pointer;stdcall;

function SlCan_DeviceGetContext(hDevice:TSlCanDevice):pointer;stdcall;

function SlCan_DeviceGetContextSize(
   hDevice: TSlCanDevice
   ):longword;stdcall;

function SlCan_DeviceSetAlias(
   hDevice: TSlCanDevice;
   pBuf:    PAnsiChar
   ):longbool;stdcall;

function SlCan_DeviceGetAlias(
   hDevice:TSlCanDevice;
   pBuf:    PAnsiChar;
   dwSize:  longword
   ):longword;stdcall;

function SlCan_DeviceSetAliasW(
   hDevice: TSlCanDevice;
   pBuf:    PWideChar
   ):longbool;stdcall;

function SlCan_DeviceGetAliasW(
   hDevice: TSlCanDevice;
   pBuf:    PWideChar;
   dwSize:  longword
   ):longword;stdcall;

function SlCan_DeviceGetCapabilities(
   hDevice: TSlCanDevice;
   pCap:    PSlCanCapabilities
   ):longbool;stdcall;

function SlCan_DeviceOpen(
   hDevice:TSlCanDevice
   ):longbool;stdcall;

function SlCan_DeviceClose(
   hDevice:TSlCanDevice
   ):longbool;stdcall;

function SlCan_DeviceSetMode(
   hDevice: TSlCanDevice;
   dwMode:  longword
   ):longbool;stdcall;

function SlCan_DeviceGetMode(
   hDevice: TSlCanDevice;
   pdwMode: Plongword
   ):longbool;stdcall;

function SlCan_DeviceGetState(
   hDevice:TSlCanDevice;
   pState: PSlCanState
   ):longbool;stdcall;

function SlCan_DeviceSetTXTimeOut(
   hDevice: TSlCanDevice;
   dwValue: longword):longbool;stdcall;

function SlCan_DeviceGetTXTimeOut(
   hDevice: TSlCanDevice;
   pdwValue:Plongword
   ):longbool;stdcall;

function SlCan_DeviceGetBitRate(
   hDevice: TSlCanDevice;
   pBitRate:PSlCanBitRate
   ):longbool;stdcall;

function SlCan_DeviceSetBitRate(
   hDevice: TSlCanDevice;
   pBitRate:PSlCanBitRate
   ):longbool;stdcall;

function SlCan_DeviceEnaRec(
   hDevice: TSlCanDevice;
   bValue:  byte
   ):longbool;stdcall;

function SlCan_DeviceSetLatency(
   hDevice: TSlCanDevice;
   bValue:  byte
   ):longbool;stdcall;

function SlCan_DeviceGetLatency(
   hDevice: TSlCanDevice;
   pbValue: pbyte
   ):longbool;stdcall;

function SlCan_DevicePurge(
   hDevice:TSlCanDevice;
   bValue:  byte
   ):longbool;stdcall;

function SlCan_DeviceSetEventLevel(
   hDevice:TSlCanDevice;
   bValue:  byte
   ):longbool;stdcall;

function SlCan_DeviceGetEventLevel(
   hDevice:TSlCanDevice;
   pbValue: pbyte
   ):longbool;stdcall;

function SlCan_DeviceSetStartTimeStamp(
   hDevice: TSlCanDevice;
   pValue:  PSlCanTimeStamp
   ):longbool;stdcall;

function SlCan_DeviceGetStartTimeStamp(
   hDevice: TSlCanDevice;
   pValue:  PSlCanTimeStamp
   ):longbool;stdcall;

function SlCan_DeviceGetTimeStamp(
   hDevice: TSlCanDevice;
   pValue:  PSlCanTimeStamp
   ):longbool;stdcall;

function SlCan_DeviceSetTimeStampPeriod(
   hDevice: TSlCanDevice;
   lValue: longint
   ):longbool;stdcall;

function SlCan_DeviceGetTimeStampPeriod(
   hDevice: TSlCanDevice;
   plValue: PLongint
   ):longbool;stdcall;

function SlCan_DeviceSetExMode(
   hDevice: TSlCanDevice;
   bMode:   byte
   ):longbool;stdcall;

function SlCan_DeviceGetExMode(
   hDevice:TSlCanDevice;
   pbMode:  pbyte
   ):longbool;stdcall;


function SlCan_DeviceWriteMessages(
   hDevice: TSlCanDevice;
   pMsg:    PSlCanMessage;
   Count:   longword;
   pStatus: pbyte
   ):longbool;stdcall;

function SlCan_DeviceWriteMessagesEx(
   hDevice: TSlCanDevice;
   pMsg:    PSlCanTxMessage;
   Count:   longword;
   pStatus: pbyte;
   pdwCount:Plongword
   ):longbool;stdcall;

function SlCan_DeviceReadMessages(
   hDevice:  TSlCanDevice;
   dwTimeOut:longword;
   pMsg:     PSlCanMessage;
   Count:    longword;
   pCount:   Plongword
   ):longbool;stdcall;

function SlCan_DeviceReadEvents(
   hDevice:  TSlCanDevice;
   dwTimeOut:longword;
   pEvent:   PSlCanEvent;
   Count:    longword;
   pCount:   Plongword
   ):longbool;stdcall;

implementation

const
   Dll = 'slcan.dll';

function SlCan_Load;stdcall;external Dll;
function SlCan_Free;stdcall;external Dll;
function SlCan_Update;stdcall;external Dll;
function SlCan_GetDeviceCount;stdcall;external Dll;
function SlCan_GetDevice;stdcall;external Dll;

function SlCan_DeviceGetHandle;stdcall;external Dll;
function SlCan_DeviceOpen;stdcall;external Dll;
function SlCan_DeviceClose;stdcall;external Dll;

function SlCan_DeviceGetProperty;stdcall;external Dll;
function SlCan_DeviceGetPropertyW;stdcall;external Dll;
function SlCan_DeviceGetRegKey;stdcall;external Dll;
function SlCan_DeviceSetAlias;stdcall;external Dll;
function SlCan_DeviceGetAlias;stdcall;external Dll;
function SlCan_DeviceSetAliasW;stdcall;external Dll;
function SlCan_DeviceGetAliasW;stdcall;external Dll;
function SlCan_DeviceGetCapabilities;stdcall;external Dll;

function SlCan_DeviceSetContext;stdcall;external Dll;
function SlCan_DeviceGetContext;stdcall;external Dll;
function SlCan_DeviceGetContextSize;stdcall;external Dll;

function SlCan_DeviceSetMode;stdcall;external Dll;
function SlCan_DeviceGetMode;stdcall;external Dll;
function SlCan_DeviceGetState;stdcall;external Dll;
function SlCan_DeviceSetTXTimeOut;stdcall;external Dll;
function SlCan_DeviceGetTXTimeOut;stdcall;external Dll;

function SlCan_DeviceGetBitRate;stdcall;external Dll;
function SlCan_DeviceSetBitRate;stdcall;external Dll;
function SlCan_DeviceEnaRec;stdcall;external Dll;
function SlCan_DeviceSetLatency;stdcall;external Dll;
function SlCan_DeviceGetLatency;stdcall;external Dll;
function SlCan_DevicePurge;stdcall;external Dll;
function SlCan_DeviceSetEventLevel;stdcall;external Dll;
function SlCan_DeviceGetEventLevel;stdcall;external Dll;
function SlCan_DeviceSetStartTimeStamp;stdcall;external Dll;
function SlCan_DeviceGetStartTimeStamp;stdcall;external Dll;
function SlCan_DeviceGetTimeStamp;stdcall;external Dll;
function SlCan_DeviceSetTimeStampPeriod;stdcall;external Dll;
function SlCan_DeviceGetTimeStampPeriod;stdcall;external Dll;
function SlCan_DeviceSetExMode;stdcall;external Dll;
function SlCan_DeviceGetExMode;stdcall;external Dll;

function SlCan_DeviceWriteMessages;stdcall;external Dll;
function SlCan_DeviceWriteMessagesEx;stdcall;external Dll;
function SlCan_DeviceReadMessages;stdcall;external Dll;
function SlCan_DeviceReadEvents;stdcall;external Dll;

end.
