{*******************************************************}
{                                                       }
{       nrCommLib Communication Library for Delphi      }
{       CAPI 2.0 definitions and routines for Win32     }
{       Copyright (c) 1997-2011 Roman Novgorodov        }
{       v9.27                                           }
{       last update: 28 Apr 2005                        }
{                                                       }
{       e-mail:noro@deepsoftware.com                    }
{       DeepSoftware                                    }
{       http://www.deepsoftware.com/                    }
{                                                       }
{*******************************************************}
{$I nrcommlib.inc}
unit nrcapiw32;
interface
uses windows;

Const
  // Type of message
  CAPI_REQ   = $80; // request from application to CAPI
  CAPI_CONF  = $81; // CAPI confirms a request
  CAPI_IND   = $82; // CAPI indicates an incoming event
  CAPI_RESP  = $83; // application confirms reception


 //   Command and SubCommand of MessageHeader
 //   see CAPI_PUT_MESSAGE und CAPI_GET_MESSAGE
  LISTEN_REQ             = $0580;    // Command 0x05, SubCommand 0x80
  LISTEN_CONF            = $0581;
  CONNECT_REQ            = $0280;
  CONNECT_CONF           = $0281;
  CONNECT_IND            = $0282;
  CONNECT_RESP           = $0283;
  CONNECT_ACTIVE_IND     = $0382;
  CONNECT_ACTIVE_RESP    = $0383;
  CONNECT_B3_REQ         = $8280;
  CONNECT_B3_CONF        = $8281;
  CONNECT_B3_IND         = $8282;
  CONNECT_B3_RESP        = $8283;
  CONNECT_B3_ACTIVE_IND  = $8382;
  CONNECT_B3_ACTIVE_RESP = $8383;
  CONNECT_B3_T90_ACTIVE_IND  = $8882;
  CONNECT_B3_T90_ACTIVE_RESP = $8883;
  DATA_B3_REQ            = $8680;
  DATA_B3_CONF           = $8681;
  DATA_B3_IND            = $8682;
  DATA_B3_RESP           = $8683;
  DISCONNECT_B3_REQ      = $8480;
  DISCONNECT_B3_CONF     = $8481;
  DISCONNECT_B3_IND      = $8482;
  DISCONNECT_B3_RESP     = $8483;
  DISCONNECT_REQ         = $0480;
  DISCONNECT_CONF        = $0481;
  DISCONNECT_IND         = $0482;
  DISCONNECT_RESP        = $0483;
  INFO_REQ               = $0880;
  INFO_CONF              = $0881;
  INFO_IND               = $0882;
  INFO_RESP              = $0883;
  ALERT_REQ              = $0180;
  ALERT_CONF             = $0181;
  FACILITY_REQ           = $8080;
  FACILITY_CONF          = $8081;
  FACILITY_IND           = $8082;
  FACILITY_RESP          = $8083;
  RESET_B3_REQ           = $8780;
  RESET_B3_CONF          = $8781;
  RESET_B3_IND           = $8782;
  RESET_B3_RESP          = $8783;
  SELECT_B_PROTOCOL_REQ  = $4180;
  SELECT_B_PROTOCOL_CONF = $4181;

  //Initialization of CAPI
  // max. nr. of B-channels
  defMaxLogicalConnections : dword = 1;
  // max. nr. of unconfirmed B3-Datablocks, CAPI supports max. 7
  defMaxBDataBlocks        : dword = 2;
  // max. Blocksize for B3 Datablocks, CAPI supports max. 2048
  defMaxBDataLen           : dword = 128;

  //consts for use in informative CAPI functions
  //z.B. var Manufacturer : string;
  //     SetLength(Manufacturer, slManufacturer);
  //     Capi_GetManufacturer(PChar(Manufacturer));

  slManufacturer = 64;   // szBuffer f. Capi_GetManufacturer
  slSerialNr     = 8;    // szBuffer f. Capi_GetSerialNumber

  // default value for Controller (i.e. in LISTEN_REQ)
  ctrDefault           = 1;

  // Info-Mask for LISTEN_REQ ----------------------------------------
  imaCause             = $0001;
  imaDateTime          = $0002;
  imaDisplay           = $0004;
  imaUserUserInfo      = $0008;
  imaCallProgression   = $0010;
  imaFacility          = $0020;
  imaCharging          = $0040;
  imaMaskAll           = $007F;
  imaDefault           = imaMaskAll;

  // CIP-Mask for LISTEN_REQ -----------------------------------------
  cipDefault             = $1FFF03FE;  // sets all Bits
  cipAnyMatch            = $00000001;  // in CONNECT_IND look for BC, HLC u. LLC
  cipSpeech              = $00000002;  // speech
  cipUnrestDigitalInfo   = $00000004;  // unrestricted digital information
  cipRestDigitalInfo     = $00000008;  // restricted digital information
  cip31kHzAudio          = $00000010;  // 3.1 kHz audio
  cip70kHzAudio          = $00000020;  // 7.0 kHz audio
  cipVideo               = $00000040;  // video
  cipPacketMode          = $00000080;  // packet mode
  cip56kBits             = $00000100;  // 56 kBit/s rate adaptation
  cipUnrestDigitalInfoTA = $00000200;  // unrestricted digital information with tones/announcements
                                       // 10..15 reserved
  cipTelephony           = $00010000;  // telephony
  cipFaxGroup3           = $00020000;  // fax group 2/3
  cipFaxGroup4Class1     = $00040000;  // fax group 4 class 1
  cipTeletexbm           = $00080000;  // Teletex service (basic and mixed), fax group 4 class 2
  cipTeletexbp           = $00100000;  // Teletex service (basic and processable)
  cipTeletexb            = $00200000;  // Teletex service (basic)
  cipVideotex            = $00400000;  // Videotex
  cipTelex               = $00800000;  // Telex
  cipX400                = $01000000;  // message handling systems according X.400
  cipX200                = $02000000;  // OSI applications according X.200
  cip7kHzTelephony       = $04000000;  // 7 kHz Telephony
  cipF721First           = $08000000;  //  Video Telephony F.721, first connection
  cipF721Second          = $10000000;  //  Video Telephony F.721, second connection

  // CIP-Mask2 for LISTEN_REQ
  cip2Default            = 0;          // reserved

  // CIP Values for CONNECT_IND
  civNoProfile           = 0;         //no predefined profile 
  civSpeech              = 1;         //speech - i.e. ISDN telephone
  civUnrestDigitalInfo   = 2;         //unrestricted digital information 
  civRestDigitalInfo     = 3;         //restricted digital information 
  civ31kHzAudio          = 4;         //3.1 kHz audio, i.e. analog telephone 
  civ70kHzAudio          = 5;         //7.0 kHz audio 
  civVideo               = 6;         //video 
  civPacketMode          = 7;         //packet mode 
  civ56kBits             = 8;         //56 kBit/s rate adaptation 
  civUnrestDigitalInfoTA = 9;         //unrestricted digital information with tones/announcements 
                                      //10..15 reserved 
  civTelephony           = 16;        //telephony 
  civFaxGroup3           = 17;        //fax group 2/3 
  civFaxGroup4Class1     = 18;        //fax group 4 class 1 
  civTeletexbm           = 19;        //Teletex service (basic and mixed), fax group 4 class 2
  civTeletexbp           = 20;        //Teletex service (basic and processable) 
  civTeletexb            = 21;        //Teletex service (basic)
  civVideotex            = 22;        //Videotex 
  civTelex               = 23;        //Telex 
  civX400                = 24;        //message handling systems according X.400 
  civX200                = 25;        //OSI applications according X.200 
  civ7kHzTelephony       = 26;        //7 kHz Telephony
  civF721First           = 27;        //Video Telephony F.721, first connection 
  civF721Second          = 28;        //Video Telephony F.721, second connection 


 //Parameter Reject for CONNECT_RESP -------------------------------
  rjeAcceptCall   = $0000;
  rjeIgnoreCall   = $0001; //other devices can accept the call
  rjeNormalClear  = $0002; //hang up
  rjeUserBusy     = $0003; //busy
  rjeIncompDest   = $0007; //incompatible destination
  rjeUndefined    = $FFFF; //not Capi defined

 //Parameter Reject for CONNECT_B3_RESP, Parameter Reject ----------
  rjeB3AcceptCall   = $0000;    //other devices can accept the call
  rjeB3NormalClear  = $0002;    //hang up

  //TypeOfNumber (i.e. for TCallingPartyNumber)
  // ETS 300 102, 4.5.10, extension Bit = 0 (Capi - Calling Party
  //                                            Number - Byte 0     -----
  tonDefault         = 0;  //Bit 5-7 unknown, Bit 1-4 unknown
  tonInternationalNumberMask = $10;//Bit 5-7 international number
  tonNationalNumberMask      = $20;//Bit 5-7 national number

  //ScreenIndicator (i.e. for TCallingPartyNumber)
  //  ETS 300 102, 4.5.10,
  sidDefault         = $80;//Bit 7+6 presentation allowed,
                           //Bit 1+2 user provided, not screened
  sidNoPresentation  = $A0;//Bit 7+6 presentation restricted,
                           //Bit 1+2 user provided, not screened
  //TypeOfSubAddress (i.e. for TCallingPartySubAddress)
  tosDefault     = $80;
  //Contents for TCallingPartySubAddress
  tscDefault : string[1]    = chr($50);

  //Facility-Selectors (i.e. TFacilityReq)
  fsHandset              = 0;
  fsDTMF                 = 1;
  fsV42bisCompression    = 2;
  fsSupplementaryService = 3;

  //DTMF-Suppport
  //DTMF-Facility-functions
  cDTMFStartListen     = 1;
  cDTMFStopListen      = 2;
  cDTMFSendDigits      = 3;
  //defaults for Facility Request Parameter
  cDTMFDefToneDuration = 40;
  cDTMFDefGapDuration  = 40;
  //DTMF Information in Facility Conf. Parameter
  cDTMFInfoSuccess        = 0;
  cDTMFInfoInvalidDigit   = 1;
  cDTMFInfoUnknownRequest = 2;
  //support for Supplementary Services
  //Supplementary Services functions
  cSSGetSupportedServices  : word = 0;
  cSSListen                : word = 1;

  function capiInstalled:THandle;
  procedure capiGetManufacturer(szBuffer:PChar);
  function capiGetSerialNumber(szBuffer:PChar):dword;
  function capiGetVersion(var CapiMajor,CapiMinor,ManufacturerMajor,ManufacturerMinor:dword):dword;
  function capiGetProfile(pProfile:pointer; CtrlNr:dword):dword;
  function capiRegister(MsgBufferSize, MaxLogConn, MaxBDataBlocks, MaxBDataLen:dword; pApplId:pointer):dword;
  function capiRelease(ApplId:dword):dword;
  function capiPutMessage(ApplId:dword; pCapiMessage:pointer):dword;
  function capiGetMessage(ApplId:dword; var pCapiMessage:pointer):dword;
  function capiWaitForSignal(ApplId:dword):dword;

implementation

Const
  CAPI_DLL_NAME = 'CAPI2032.DLL';
Var
  hCAPIDll : THandle = 0;

function AllocCAPIDll:boolean;
begin
  hCAPIDll := LoadLibrary(CAPI_DLL_NAME);
  Result := hCAPIDll <> 0;
end;

function capiInstalled:THandle;
var p:function:THandle; stdcall;
begin
  Result := INVALID_HANDLE_VALUE;
  if AllocCAPIDll then begin
     @p := GetProcAddress(hCAPIDll, 'CAPI_INSTALLED');
     if @p <> nil then Result := p;
  end;
end;

procedure capiGetManufacturer(szBuffer:PChar);
var p:procedure(szBuffer:PChar); stdcall;
begin
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_GET_MANUFACTURER');
     if @p<>nil then p(szBuffer);
  end;
end;

function capiGetSerialNumber(szBuffer:PChar):dword;
var p:function(szBuffer:PChar):dword; stdcall;
begin
  Result := Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_GET_SERIAL_NUMBER');
     if @p<>nil then Result:=p(szBuffer);
  end;
end;

function capiGetVersion(var CapiMajor,CapiMinor,ManufacturerMajor,ManufacturerMinor:dword):dword;
var p:function(var CapiMajor,CapiMinor,ManufacturerMajor,ManufacturerMinor:dword):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_GET_VERSION');
     if @p<>nil then Result:=p(CapiMajor,CapiMinor,ManufacturerMajor,ManufacturerMinor);
  end;
end;

function capiGetProfile(pProfile:pointer; CtrlNr:dword):dword;
var p:function(pProfile:pointer; CtrlNr:dword):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_GET_PROFILE');
     if @p<>nil then Result:=p(pProfile,CtrlNr);
  end;
end;

function capiRegister(MsgBufferSize, MaxLogConn, MaxBDataBlocks, MaxBDataLen:dword; pApplId:pointer):dword;
var p:function(MsgBufferSize, MaxLogConn, MaxBDataBlocks, MaxBDataLen:dword; pApplId:pointer):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_REGISTER');
     if @p<>nil then Result:=p(MsgBufferSize, MaxLogConn, MaxBDataBlocks, MaxBDataLen,pApplId);
  end;
end;

function capiRelease(ApplId:dword):dword;
var p:function(ApplId:dword):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_RELEASE');
     if @p<>nil then Result:=p(ApplId);
  end;
end;

function capiPutMessage(ApplId:dword; pCapiMessage:pointer):dword;
var p:function(ApplId:dword; pCapiMessage:pointer):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_PUT_MESSAGE');
     if @p<>nil then Result:=p(ApplId,pCapiMessage);
  end;
end;

function capiGetMessage(ApplId:dword; var pCapiMessage:pointer):dword;
var p:function(ApplId:dword; var pCapiMessage:pointer):dword; stdcall;
begin
  Result := Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p := GetProcAddress(hCAPIDll,'CAPI_GET_MESSAGE');
     if @p <> nil then Result:=p(ApplId,pCapiMessage);
  end;
end;

function capiWaitForSignal(ApplId:dword):dword;
var p:function(ApplId:dword):dword; stdcall;
begin
  Result:=Dword(INVALID_HANDLE_VALUE);
  if AllocCAPIDll then begin
     @p:=GetProcAddress(hCAPIDll,'CAPI_WAIT_FOR_SIGNAL');
     if @p<>nil then Result:=p(ApplId);
  end;
end;

initialization

finalization
  if hCAPIDll <> 0 then FreeLibrary(hCAPIDll);

end.
