/*
 *
 *     Module Name: start.c
 *	       Version: 1.00
 *   Original Date: 08/23/99	
 *	        Author: Charles Gordon
 *	      Language: Ansi C
 * Compile Options:
 * Compile defines:
 *	     Libraries:
 *    Link Options:
 *
 *    Entry Points: tx_application_define()
 *
 *  Copyrighted (c) by NETsilicon, Inc.  All Rights Reserved.
 *
 * Description.
 * =======================================================================
 *  tx_application_define() is called just before the ThreadX kernel loads.
 *  It is responsible for creating at least one thread to drive the application.
 *  We create a thread that starts up NET+OS.
 *
 *
 *
 * Edit Date/Ver   Edit Description
 * ==============  =======================================================
 *	1/31/07  JZW    Variable cipher_list made static since it is passed to the 
 *                  ssl thread adn the ssl thread will own it. If it is not static
 *                  then if root thread exists, the memory pointed to by cipher_list
 *                  could be reused.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include <ssl.h>
#include <tx_api.h>
#include <hservapi.h>
#include <sysAccess.h>



extern void Send_function_0(unsigned long handle);


/*
 *
 *  Function: void applicationStart (void)
 *
 *  Description:
 *
 *      This routine is responsible for starting the user application.  It should 
 *      create any threads or other resources the application needs.
 *
 *      ThreadX, the NET+OS device drivers, and the TCP/IP stack will be running
 *      when this function is called.
 *
 *  Parameters:
 *
 *      none
 *
 *  Return Values:
 *
 *      none
 *
 */

void applicationStart (void)

{
	int certificate_length;
	char* certificate=NULL;
	/* 
	 * added storage class static to cipher_list. A pointer to cipher_list is passed
	 * to the ssl thread. If this is not static, then as a stack variable, the memory
	 * in which cipher_list is stored could be subject to reuse.
	 */
	static unsigned char  cipher_list[] =	SSL_CIPHER_LIST_HTTPS_DEFAULT;
	int	  cipher_list_len=	SSL_CIPHER_LIST_HTTPS_DEFAULT_LEN;
	char* key=NULL;
	int	  key_data_len;

    void AppSearchURL (unsigned long, char*);
    void AppPreprocessURL (unsigned long, char*);

   /*
    * Register the URL search and preprocess functions and start the 
    * HTTP server
    */

    /*  Add Username Netsilicon password sysadm to the System Access Database   */
    NAsetSysAccess (NASYSACC_ADD, "Netsilicon", "sysadm", NASYSACC_LEVEL_HTTP_R1, 0);

    HSRegisterSearchFunction(AppSearchURL, AppPreprocessURL);

	/* Generate X.509 certificate for use by HTTPS server */
    printf ("Generating certificate.  This may take a few minutes.\n");
	certificate_length = NASSLX509Generate(&certificate, &key, &key_data_len, SSL_CERTIFICATE_KEY_TYPE_RSA, 1024, 1, "NAHTTPS Example Certificate", "020630120000Z", "120630120000Z");

	if(certificate_length <= 0 || key_data_len <= 0)
	{
		printf("Error generating X.509 certificate.\n");
		return;
	}

	/* Start HTTPS and HTTP servers */
    /* HSStartServerSecure(cipher_list, cipher_list_len, certificate, certificate_length, key, key_data_len, HTTP_AND_HTTPS); */
	
	/* OR */
	
	/* naSslSetProxyPortNumber(1234);*/ /* set SSL proxy socket to user specifed value */
 
	
	/* Start HTTPS server only */
	HSStartServerSecure(cipher_list, cipher_list_len, certificate, certificate_length, key, key_data_len, HTTPS_ONLY);
	
    printf ("NAHTTPS Server application is ready.\n");

	tx_thread_suspend(tx_thread_identify());
}

/*
 *
 *  Function: void applicationTcpDown (void)
 *
 *  Description:
 *
 *      This routine will be called by the NET+OS root thread once every 
 *      clock tick while it is waiting for the TCP/IP stack to come up.  
 *      This function can increment a counter everytime it's called to 
 *      keep track of how long we've been waiting for the stack to start.
 *      If we've been waiting too long, then this function can do something
 *      to handle the error.  
 *
 *      This function will not be called once the stack has started.
 *
 *  Parameters:
 *
 *      none
 *
 *  Return Values:
 *
 *      none
 *
 */

void applicationTcpDown (void)

{
    static int ticksPassed = 0;

    ticksPassed++;
/*
 * Code to handle error condition if the stack doesn't come up goes here.
 */
}


