/* zentrales Header-File fuer geskoctrl */
/* ------------------------------------ */

/*	Zur Anpassung des 'L7_EXP.H' an Linux	*/
#ifdef LINUX
 #include <asm/types.h>
#endif
#ifndef LINUX
 #include <sys/types.h>
#endif

/* typedef	__u8	byte; */
/* typedef	__u16	word; */
/* typedef	__u32	dword; */

#define byte	unsigned char
#define word	unsigned short
#define dword unsigned long

#define ATT __attribute__ ((packed))

#define __near
#define __far
#define __fastcall
#define __loadds

#define	QUEUE_ANZ		200
#define	POOL_ANZ		5
#define	TASK_ANZ		20


/* --- Globale Definitionen --- */
#define SosPrintf printf

#define NO_ERROR        0
#define ERROR          -1

#define FALSE           0
#define TRUE            1

#define TIMER_UP        1
#define TIMER_DOWN      0

#define MODE_ERROR      1
#define MODE_GT         2
#define MODE_CONF       3
#define MODE_GESP_CONF  4
#define MODE_GESP_GT    5

#define FW_NONE					0
#define FW_F108					1
#define FW_I416					2
#define FW_A108					3	
#define FW_A416					4

#define FW_D            1
#define FW_A            2

#define FW_ADR_BEGIN		0x40000
#define FW_ADR_END      0xBFFFF

#define FAMILY          1
#define OFFICE          2
#define FAMILY_OLD			3
#define OFFICE_OLD			4

#define NO_TYP          0
#define FAMILY_104      1
#define FAMILY_105      2
#define FAMILY_108      3
#define FAMILY_115      4
#define FAMILY_116      5
#define OFFICE_1008     6
#define OFFICE_1016     7
#define OFFICE_1106     8
#define OFFICE_1114     9
#define OFFICE_1108     10
#define OFFICE_1116     11
#define OFFICE_2108     12
#define OFFICE_2116     13
#define FAMILY_4        14
#define FAMILY_8        15
#define OFFICE_206      16
#define OFFICE_209      17

#define COM_NUMBER      1
#define COM_DEVICE      2

#define BLOCK_READ_BEFEHL			0
#define BLOCK_WRITE_BEFEHL		1
#define BLOCK_REPAIR_FLASH    8
#define BLOCK_DESTROY_FLASH   9

#define PROG_ADRESS   0x40000
#define CONF_ADRESS   0xB0000

#define TIMEOUT													/* Programm mit Timeout? */

/* defines fuer Timeout-Zeiten der seriellen Schnittstelle - in sec.!!! */
#define SHORT_TIMEOUT_1       1
#define SHORT_TIMEOUT_2       3
#define LONG_TIMEOUT_1        10
#define LONG_TIMEOUT_2        15
#define LONG_TIMEOUT_3        25
#define LONG_TIMEOUT_4        30
#define RESET_TIMEOUT         9
#define FW_DOWNLOAD_TIMEOUT   3
#define SERIAL_FAIL_TIMEOUT		0.5
#define SERIAL_TIMEOUT        SHORT_TIMEOUT_2

/* defines fuer Gespraechsdaten */
//#define GET_GSPDATA_GT
#define GET_GSPDATA_CONF
#define GSPDATA_NEW       1
#define GSPDATA_APPEND    2

/* defines fuer Timeouts... */
#define NONE              0
#define SERIAL_IN         1
#define SERIAL_OUT        2
#define SERIAL_BLOCK_IN   3
#define SERIAL_BLOCK_OUT  4

#define MAX_TRYS          5

#define LF                (char)10
#define CR                (char)13
#define CR_TASTE          0x0A

/* ein paar defines für serielle Schnittstelle - Modem Status Register (MSR) */
#define MSR_DCTS          0x01                   /* MSR - delta clear to send (DCTS) */
#define MSR_CTS           0x10                   /* MSR - clear to send (CTS) */
#define MSR_DSR           0x20								   /* MSR - data set ready (DSR) */
#define VALUE_MSR_DSR     32

/* defines fuer Logging */
#define LOG_CONSOLE       1
#define LOG_FILE          2
#define LOG_CONSOLE_FILE  3

/* defines fuer diverse Debug-Meldungen */
//#define DEBUG
//#define DEBUG_SERIAL
//#define DEBUG_SERIAL_MSR
//#define DEBUG_DATA
//#define DEBUG_CRC
//#define DEBUG_ERROR
//#define DEBUG_TIMEOUT


/* Struktur fuer Kommandodaten */
typedef struct cmd_defs
{
   unsigned char	befehl[0x10];
   int param_anz;
   long param[3];
   int data_len;
   unsigned char  daten[0x100];
} cmdtype;


/* Inlude-Dateien von Gesko */
#include "sys_i416.h"
#include "glob_def.h"
#include "l7_exp.h"


/* Prototypen-Definitionen */
int ConfPbxPutAndGet ( cmdtype, unsigned char *, unsigned char * , int);
int GTPbxPutAndGet ( cmdtype, unsigned char *, int );

short WriteBlockToPabx ( unsigned long, unsigned short, unsigned char * );
short ReadBlockFromPabx ( unsigned long, unsigned long, unsigned char * );
int SwitchToBlockmode ( int );

int GespConfPbxPutAndGet ( int );
int GespGtPbxPutAndGet ( int );
void ShowGespDat ( unsigned char *, FILE * );

int GetL7EinrBlock ( EINRICHT_BLOCK );
int GetL7MsnBlock ( MSN_BLOCK );
int GetL7TlnBlock ( TLN_BLOCK );
int GetL7IntPortTab ( INT_PORT_TAB );

int GTFirmwareDownload ( int, unsigned char *);
int GTFirmwareUpload ( int, unsigned char *);

int ResetPbx ( void );

int PbxOpen ( void );
int SetBaudRate( int );
int SetPcBaudRate( int );
void SetDevice ( int com , char *device, int mode );
void SetComTimeout ( int );
int SerialLogging ( int );
void CheckBaudRate ( int );

int SerialOut ( unsigned char *, int );
int SerialIn ( char *, char );
int SerialInBlock ( char *, int );

void ReadFirmwareVersion ( unsigned char *, unsigned char *, int );

int DestroyFlashSignature ( void );
int RepairFlashSignature ( void );

void HelpText ( void );
void CopyrightText ( void );

void ReadPbxData ( void );
void GetHardwareInfo (void );
int GetAnlageTyp ( void ); 

void StartTimer( long,  long );
void TimerEnd ( int );

int CheckStartProgram ( void );
void EndeProgrammSignal ( int );

unsigned short CalcCrc ( unsigned char *, register unsigned short );
short MakeHeader ( short, unsigned long, short, unsigned char * );

long HexToWert ( unsigned char * );

word L0_TM_NibbleWahlziffer ( byte *, byte *, word );
word L0_TM_DecompWahlziffer ( byte *, byte *, word );

int MakeCfgFile ( EINRICHT_BLOCK );
int MakeMsnFile ( MSN_BLOCK );
int MakeTlnFile ( TLN_BLOCK );
int MakeTabFile ( INT_PORT_TAB );

int GetL7EinrBlock ( EINRICHT_BLOCK );
int GetL7MsnBlock ( MSN_BLOCK );
int GetL7TlnBlock  ( TLN_BLOCK );
int GetL7IntPortTab ( INT_PORT_TAB );

