#include <exec/types.h>
#include <exec/nodes.h>

#include <exec/execbase.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <dos/rdargs.h>
#include <dos/dostags.h>
#include <dos/datetime.h>
#include <devices/serial.h>
#include <intuition/intuitionbase.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <clib/exec_protos.h>
extern struct ExecBase *SysBase;
#include <pragmas/exec_libcall_lib.h>

#include <clib/dos_protos.h>
extern struct DosLibrary *DOSBase;
#include <pragmas/dos_lib.h>

#include <clib/timer_protos.h>
#define TimerBase (TimerIO.tr_node.io_Device)
#include <pragmas/timer_lib.h>

#include <clib/icon_protos.h>
extern struct Library *IconBase;
#include <pragmas/icon_lib.h>

#include <clib/intuition_protos.h>
#include <pragmas/intuition_lib.h>

#include <clib/locale_protos.h>
#define LocaleBase LocaleInfo.li_LocaleBase
#include <pragmas/locale_lib.h>

#include <clib/alib_protos.h>

#include <stdio.h>
#define USE_BUILTIN_MATH TRUE
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include <dos.h>	/* for __emit() prototype */

#define CATCOMP_NUMBERS
#include "CyberX10_Strings.h"

#include "OwnDevUnit.h"

#define SECSINDAY (86400)
#define SECSINHOUR (3600)
#define SECSINMINUTE (60)

#define BIG_BUF_SIZE_BASE (1024)
#define BIG_BUF_SIZE_XTRA (16)
#define BIG_BUF_SIZE (BIG_BUF_SIZE_BASE + BIG_BUF_SIZE_XTRA)

/* some magic returns from the serial input routine */
#define TIMEOUT (0xFFFF)
#define EMPTY (0xFFFE)

/* a magic assign for the name of the program */
#define PROGNAME GetString(&LocaleInfo, MSG_PROGNAME)

/* exports from Timer.c */
extern struct timerequest TimerIO;
extern ULONG TimerSigMask;

/* exports from MyPrintf.c */
extern BOOL ErrorsToEzReq;

/* exports from CyberX10.c */
extern const UBYTE HouseCode[16];
extern struct LocaleInfo LocaleInfo;
extern struct Locale *DefaultLocale;

void __stdargs kprintf(STRPTR fmt, ...);

/* prototypes for functions in CyberX10.c */

int __regargs main(char *cmdptr, int cmdlen, struct WBStartup *WBMsg);
void DoInstance(STRPTR ArgLine, STRPTR ArgHelp);
void X10TurnOn(STRPTR *Targets);
void X10TurnOff(STRPTR *Targets);
void X10Dim(STRPTR *Targets, ULONG DimLevel);
void OnOffDimHandler(STRPTR *Targets, ULONG DimLevel, ULONG Function, LONG Msg);
void X10SetBaseHC(STRPTR BaseHC);
void X10SetClock(void);
void X10Diagnostic(void);
int  X10GetHC(UBYTE Char);
BOOL X10SendDirectCmd(int HC, int Unit, ULONG Function, ULONG DimLevel);
BOOL X10WaitForAck(BOOL WaitForUploadReport);

/* prototypes for functions in X10UpDown.c */

void X10Download(STRPTR filename);
void X10Upload(STRPTR filename);

/* prototypes for functions in MyPrintf.c */

void __stdargs ErrorMsg(LONG, ...);
void __stdargs MyPrintf(LONG, ...);

/* prototypes for functions in WBtoCLIargs.c */

STRPTR WBtoCLIargs(struct WBArg * WBArg, STRPTR ArgTemplate);

/* prototypes for functions in Timer.c */

BOOL OpenTimer(void);
void CloseTimer(void);

/* prototypes for functions in Serial.c */

BOOL OpenSerial(STRPTR Device, ULONG Unit, BOOL AttemptMode);
void CloseSerial(void);
void ClearSerial(void);
void SerWrite(STRPTR Buf, ULONG Len);
UWORD SerGetRawChar(ULONG timeout);

/* prototypes for functions in CyberX10Strings. */
STRPTR __asm GetString(register __a0 struct LocaleInfo *li, register __d0 ULONG id);

/* extern references to our version and revision numbers */

extern ULONG __far Version;
extern ULONG __far Revision;
extern UBYTE __far VersionID[];
