/*************************************************************
// WTX C API Tool Sample Code for Windows                   //
// C.A Leddy, 02/15/01                                      //
*************************************************************/
/*************************************************************
// Code is provided free for any use, just leave above      //
// header on any derived source                             // 
*************************************************************/
/*************************************************************
// Tornado 1.0.1, Win NT 4.0 SP6a, Visual Studio C++ 6.0    //
// target vxworks 5.3.1                                     //		
*************************************************************/

#include "stdafx.h"
// not shown in example
#include "host.h"
#include "wtx.h"
#include <signal.h>
#include <stdio.h>

HWTX wtxh;

// WTX housecleaning when quit
void wtxAppTerminate(void)
	{
	wtxToolDetach(wtxh);
	wtxTerminate(wtxh);
	exit(0);
	}

// signal terminate call
void wtxAppSigHandler(int sig)
	{
	wtxAppTerminate();
	}

// install signal handlers
void wtxAppSigInit(void)
	{
	// modified windriver code for windows 32 api:
	signal(SIGABRT, &wtxAppSigHandler);
	signal(SIGFPE, &wtxAppSigHandler);
	signal(SIGILL, &wtxAppSigHandler);
	signal(SIGINT, &wtxAppSigHandler);
	signal(SIGSEGV, &wtxAppSigHandler);
	signal(SIGTERM, &wtxAppSigHandler);
	}

int main(int argc, char* argv[])
	{
	STATUS stats;
	int count=0;
	TGT_ADDR_T t_add;
    	// get server name from command line
	if (argc != 2)
		{
		printf("Error: Usage -> wtxcom targetservername\n");
		exit(0);
		}

	// set up signals like in the example
	// trap errors and terminate properly
	wtxAppSigInit();
	printf("Signals Initialized\n");

	// initialize WTX protocol
	if (wtxInitialize(&wtxh)!= OK)
		{
		printf("Error: WTX API did not Initialize\n");
		exit(0);
		}
	else
		printf("WTX Initialized\n");

	// check registry
	if (wtxProbe(wtxh)!= OK)
		{
		printf("Warning: Tornado Registry Not Found\n");
		printf("     add the environment variable 'WIND-REGISTRY' \npointing
to the registry to 				correct this!\n");
		}
	else
		printf("Tornado Registry Located\n");

	// attached to server on command line with my tool
	stats=wtxToolAttach(wtxh, argv[1], "ChrisTool");
	if (stats != OK)
		{
		printf("Error: WTX did not Attach to target #%s#\n",argv[1]);
		wtxTerminate(wtxh);
		exit(0);
		}
	else
		{
		printf("WTX Tool Attached\n");
		}

	// allocate memory for an integer on target
	t_add=wtxMemAlign(wtxh, 4, 4);
	if (t_add == NULL)
		{
		printf("Error: WTX could not allocate Memory for Integer\n");
		wtxAppTerminate();
		}
	else
		{
		printf("WTX Allocated Memory for Int\n");
		}

	 //add the symbol to target so I can check it on the target shell
	stats=wtxSymAdd(wtxh, "addedsymbol", t_add, V_INT32);
	if (stats != OK)
		{
		printf("Error: WTX did add symbol");
		wtxAppTerminate();
		}
	else
		{
		printf("added 'addedsymbol' to symbol table\n");
		}

	// we can terminate with a CTRL-C (signal handler will catch it)
	// or we dump with this call when done
	wtxAppTerminate();
	return 0;
	}


