
#if !defined (xapilib_)
  #define xapilib_

#ifdef XAPIDLL_EXPORTS
#define XAPI_API __declspec(dllexport)
#else
#ifdef XAPIDLL_IMPORTS
#define XAPI_API __declspec(dllimport)
#else
#define XAPI_API
#endif
#endif

#define Xapi_default_protocol_port                           9875
  // Xymphony port can be changed via "Ethernet/Basic/Companion signalling port"

#define Xapi_default_cmdr_port                               9876
  // Xymphony port can be changed via "Ethernet/Cmdr/XtoolsMonitor(UDP)Cmdr(TCP)port"

#define Xapi_default_adjunct_port                            9870
  // Xymphony port can be changed via "Ethernet/Adjunct/Adjunct server port"

#define XAPILNK_WM                                           49000
#define XAPILNK_WPARAM_STATE_CHANGE                          1
#define XAPILNK_WPARAM_DIGIT_FROM_CALLCONTROL                2
#define XAPILNK_WPARAM_DIGIT_TO_CALLCONTROL                  3
#define XAPILNK_WPARAM_CALLING_LINE_ID                       4
#define XAPILNK_WPARAM_DISPLAY                               5
#define XAPILNK_WPARAM_BDP_STATUS                            6
#define XAPILNK_WPARAM_TRANSFERRED_CALLS                     7
#define XAPILNK_WPARAM_SUSPENDED_CALLS                       8

#define XAPILNK_WPARAM_CMDR_STATE_CHANGE                     100
#define XAPILNK_WPARAM_CMDR_RECORD                           101

#define XAPILNK_WPARAM_ADJUNCT_STATE_CHANGE                  200
#define XAPILNK_WPARAM_ADJUNCT_RECORD                        201

#define XAPILNK_WPARAM_PERIODICTIMER                         1000
  // priodic callback, xapi instance posts this message every second

typedef struct {
    unsigned RegistrationState;
      #define RegistrationState_registering                  0
      #define RegistrationState_registered                   1
      #define RegistrationState_registration_failed          2
    const char * registration_failed_cause;

    unsigned char PortControlState;
      #define PortControlState_s00_null_state                0
      #define PortControlState_s01_call_initiated	     1
      #define PortControlState_s02_overlap_sending	     2
      #define PortControlState_s03_outgoing_call_proceeding  3
      #define PortControlState_s04_call_delivered	     4
      #define PortControlState_s06_call_present		     6
      #define PortControlState_s07_call_received	     7
      #define PortControlState_s08_connect_request	     8
      #define PortControlState_s09_incoming_call_proceeding  9
      #define PortControlState_s10_active		     10
      #define PortControlState_s11_disconnect_request	     11
      #define PortControlState_s12_disconnect_indication     12
      #define PortControlState_s15_suspend_request	     15
      #define PortControlState_s17_resume_request	     17
      #define PortControlState_s19_release_request	     19
      #define PortControlState_s22_call_abort                22
      #define PortControlState_s25_overlap_receiving         25
      #define PortControlState_s26_og_h323_transit           26
      #define PortControlState_s27_ic_h323_admited           27
      #define PortControlState_s28_ic_multi_station_call     28
      #define PortControlState_s29_continuity_check 	     29
      #define PortControlState_s30_held			     30
      #define PortControlState_s31_v52pstn                   31

    unsigned ChargePulses;
    unsigned PendingCalls;

    // boolean XAPI_Client_ call enable indicator flags
    unsigned char NewcallEnabled;
    unsigned char DigitsEnabled;
    unsigned char DeflectEnabled;
    unsigned char AnswerEnabled;
    unsigned char ClearEnabled;
    unsigned char HoldEnabled;
    unsigned char RetrieveEnabled;
    unsigned char CallbEnabled;
    unsigned char SuspendEnabled;
    unsigned char ConferenceEnabled;
    unsigned char TransferEnabled;
    unsigned char RecordStartEnabled;
    unsigned char RecordStopEnabled;
    unsigned char MailboxEnabled;
    unsigned char FunctionEnabled;
      // as TransferUndo, Resume

    // user status flags
    unsigned char Suspended;
    unsigned char RemoteHold;
    unsigned char RemoteConf;
} XAPI_stateS;

typedef struct {
    unsigned access_code;
    unsigned Status;
      #define Bdp_status_off                                  0
      #define Bdp_status_fast_flash                           1
      #define Bdp_status_slow_flash                           2
      #define Bdp_status_on                                   3
} BdpStatusS;

typedef void *HXAPI;
typedef XAPI_stateS *PXAPI;


typedef struct {
    unsigned ConnectionState;
      #define Cmdr_ConnectionState_connecting                  0
      #define Cmdr_ConnectionState_connected                   1
      #define Cmdr_ConnectionState_disconnected                2
    const char * disconnection_cause;
} XAPI_cmdr_stateS;

typedef struct {
  unsigned ForwardingUsed                                    : 8;
    #define Fu_LastNumberRedial                              0x01
    #define Fu_UserSpeedDialMemory                           0x02
    #define Fu_HotLine                                       0x04
    #define Fu_UserHotLine                                   0x05
    #define Fu_RedialOnIncompleteDialling                    0x06
    #define Fu_RedialOnDestinationBusy                       0x07
    #define Fu_RedialOnAlertingTimeout                       0x08

    #define Fu_Deflected                                     0x21
    #define Fu_UserUnconditionalForwarding                   0x22
    #define Fu_UserBusyForwarding                            0x23
    #define Fu_UserNoanswerForwarding                        0x24
    #define Fu_SysUnconditionalForwarding                    0x25
    #define Fu_SysBusyForwarding                             0x26
    #define Fu_SysNoanswerForwarding                         0x27
    #define Fu_H323XferToUser                                0x28
    #define Fu_H323XferToDigitSequence                       0x29

    #define Fu_CallXfer                                      0x40
    #define Fu_CallBack                                      0x41
    #define Fu_UserRouteForwarding                           0x42
    #define Fu_CallXferBusy                                  0x43
    #define Fu_CallXferAlrting                               0x44
    #define Fu_AltRouting                                    0x45

  unsigned  IncomingChan                                   : 5;
    // applicable to CCS signalling E1
  unsigned  OutgoingChan                                   : 5;
    // applicable to CCS signalling E1

  unsigned  ChargingStarted                                : 1;
  unsigned  IncomingTieline                                : 1;
  unsigned  OutgoingTieline                                : 1;
  unsigned  NumberOfStarsReported                          : 4;
} XAPI_cmdr_flagS;

typedef struct {
  unsigned short second;
  unsigned short minute;
  unsigned short hour;
  unsigned short day;
  unsigned short month;
  unsigned short year;
}  XAPI_timeS;

typedef struct {
    unsigned char type;
      // A - Call records
      // B - System related (information and warning) records
      // C - User operated Service records
      // D - Malicious call records
      // E - Room service reports (for hotel applications)

    XAPI_timeS time;
      // record generation time

    unsigned duration;
      // duration of call in seconds

    unsigned cport;
      // access code of user to be charged

    unsigned pport;
      // access code of actual port used to make call
      // may be different than cport if the user used passwod login service

    unsigned uport;
      // B party engaged in call

    union {
        struct { // applicable to A - Call records
          unsigned char rdata [32];
            // called party digits dialled by the user
          unsigned char xdata [32];
            // translated called party digits after routing analysis
          unsigned char cdata [32];
            // caller id
        } digits;
        unsigned char info [96]; // applicable to B, C, D, E records
    };

    unsigned rpuls;
      // charge pulses received from line

    unsigned ppuls;
      // charge pulses generated by Xymphony call control

    unsigned crefa;
      // call reference A, may be used to group records belonging to the same call

    unsigned crefb;
      // call reference B, may be used to group records belonging to the same call

    unsigned char ripadr [48];
      // ipadr of charged user, applicable to voip users

    union {
        XAPI_cmdr_flagS Cflg;
        unsigned  flags;
      };

    unsigned char Xid [16];
      // for future use

    unsigned  char xipadr [48];
      // ipadr of xymphony generating the record

} XAPI_cmdr_recordS;

typedef void *HCMDR;
typedef XAPI_cmdr_stateS *PCMDR;

typedef struct {
    unsigned ConnectionState;
      #define Adjunct_ConnectionState_connecting                     0
      #define Adjunct_ConnectionState_connected                      1
      #define Adjunct_ConnectionState_disconnected                   2
    const char * disconnection_cause;
} XAPI_adjunct_stateS;

typedef struct {
    unsigned refno;
      // reference number of the request

    XAPI_timeS time;
      // record generation time

    unsigned cport;
      // access code of user to be charged

    unsigned pport;
      // access code of actual port used to make call
      // may be different than cport if the user used passwod login service

    unsigned char rdata [32];
      // called party digits dialled by the user
      // during overlap sending, this field may be empty

    unsigned char cdata [32];
      // caller id

} XAPI_adjunct_recordS;

typedef void *HADJUNCT;
typedef XAPI_adjunct_stateS *PADJUNCT;

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

XAPI_API unsigned XAPI_Stack_initialize (unsigned short multicast_port);
// Should be the first xapi call to the library
// If already initialized, all started clients are stopped
// Return value nonzero if initialization fails
// Parameters
// multicast_port: multicast port address, should be selected as "Ethernet/Basic/Companion signalling port"

XAPI_API void XAPI_Stack_release (void);
// final call to library before terminating program

//
// Companion client api functions
//
XAPI_API HXAPI XAPI_Client_start (void * parent, unsigned access_code);
// Return value handle to use with client api calls, 0 if request fails
// Parameters
// parent: HWND to post messages
XAPI_API HXAPI XAPI_Client_start_Thread (unsigned idThread, unsigned access_code);
// Return value handle to use with client api calls, 0 if request fails
// Parameters
// idThread: thread id of parent to PostThreadMessage
// can be used inplace of XAPI_Client_start, if HWND is not available

XAPI_API unsigned XAPI_Client_stop (HXAPI handle);
// Stopped handle should not be used in further api calls
XAPI_API unsigned XAPI_Client_newcall (HXAPI handle, const char * pdigit);
XAPI_API unsigned XAPI_Client_digits (HXAPI handle, const char * pdigit);
// valid digits are 0-9, *, # DTMF a-d digits are also accepted
XAPI_API unsigned XAPI_Client_deflect (HXAPI handle, const char * pdigit);
XAPI_API unsigned XAPI_Client_answer (HXAPI handle);
XAPI_API unsigned XAPI_Client_clear (HXAPI handle);
XAPI_API unsigned XAPI_Client_hold (HXAPI handle);
XAPI_API unsigned XAPI_Client_retrieve (HXAPI handle);
XAPI_API unsigned XAPI_Client_callb (HXAPI handle);
XAPI_API unsigned XAPI_Client_suspend (HXAPI handle);
XAPI_API unsigned XAPI_Client_conference (HXAPI handle);
XAPI_API unsigned XAPI_Client_transfer (HXAPI handle);
XAPI_API unsigned XAPI_Client_record_start (HXAPI handle);
XAPI_API unsigned XAPI_Client_record_stop (HXAPI handle);
XAPI_API unsigned XAPI_Client_mailbox (HXAPI handle);
XAPI_API unsigned XAPI_Client_transfer_undo (HXAPI handle, const char * transfer_index);
XAPI_API unsigned XAPI_Client_bdp_register (HXAPI handle, unsigned access_code, unsigned char add);
XAPI_API unsigned XAPI_Client_bdp_intrude (HXAPI handle, unsigned access_code);
XAPI_API unsigned XAPI_Client_bdp_answer (HXAPI handle, unsigned access_code);
XAPI_API unsigned XAPI_Client_resume (HXAPI handle, unsigned access_code);
// Return value nonzero if XAPI refuses request, Accepted requests are sent to the server
//    the outcome of the request should be monitored in state changes
// Parameters
// hadle: HXAPI received from XAPI_Client_start
// pdigit: null terminated digit string
// access_code: Xymphony access code of user, 100 encoded as 0xfffff100 (User/Common properties/AccessCode)
// transfer_index: null terminated transfer index, received from XAPILNK_WPARAM_TRANSFERRED_CALLS message
// add: boolean flag


//
// Cmdr api functions
//
XAPI_API HCMDR XAPI_Cmdr_start (void * parent, const char * ip_adr, unsigned short port);
// Return value: handle to use with cmdr api calls, 0 if request fails
// Parameters
// parent: HWND to post messages
// ip_adr: Xymphony ip address (Ethernet/Basic/IP address)
// port: Xymphony cmdr port (Ethernet/Cmdr/XtoolsMonitor(UDP)Cmdr(TCP)port)

XAPI_API unsigned XAPI_Cmdr_stop (HCMDR handle);
// Return value nonzero if XAPI refuses request
// Stopped handle should not be used in further api calls

//
// Adjunct api functions
//
XAPI_API HADJUNCT XAPI_Adjunct_start (void * parent, const char * ip_adr, unsigned short port);
// Return value: handle to use with adjunct api calls, 0 if request fails
// Parameters
// parent: HWND to post messages
// ip_adr: Xymphony ip address (Ethernet/Basic/IP address)
// port: Xymphony adjunct port (Ethernet/Adjunct/Adjunct server port)

XAPI_API unsigned XAPI_Adjunct_response (HADJUNCT handle, unsigned refno, const char * pdigit, unsigned charge_pulse);
// Return value nonzero if XAPI refuses request
// Parameters
// refno: reference number of the request
// pdigit: null terminated digit string to be used
// duration: permitted number of charge pulses to cut the call, reset to 0 for normal operation

XAPI_API unsigned XAPI_Adjunct_stop (HADJUNCT handle);
// Return value nonzero if XAPI refuses request
// Stopped handle should not be used in further api calls

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // xapilib_