/*
 *  Copyright (c) 1996-2007 Digi International Inc., All Rights Reserved
 *
 *  This software contains proprietary and confidential information of Digi
 *  International Inc.  By accepting transfer of this copy, Recipient agrees
 *  to retain this software in confidence, to prevent disclosure to others,
 *  and to make no use of this software other than that for which it was
 *  delivered.  This is an unpublished copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *
 *  Restricted Rights Legend
 *
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 * Description.
 * =======================================================================
 * applicationStart() is the entry point for user applications.  This
 * This function will be called after the kernel has started and the
 * TCP/IP stack has loaded.  applicationTcpDown() is called periodically
 * after the kernel has started while the system is waiting for the
 * TCP/IP stack to start.  
 *                                                                        
 *                                                                        
 * Edit Date/Ver   Edit Description
 * ==============  ===================================================
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tx_api.h>
#include <http_awsapi.h>
#include <sysAccess.h>
#include "appconf.h"
#include "server.h"     /*  For username/password */




/*
 *
 *  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.
 */
}




/*
 *
 *  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)
{
    HS_SERVER_PARAMETER *appParams;
    int result;

    appParams = (HS_SERVER_PARAMETER *)malloc(sizeof(HS_SERVER_PARAMETER));
    if (appParams == NULL)
    {
        printf("Out of memory to continue HTTP AWS Server Application.\n");

    }
    else
    {
        printf ("Start the HTTP AWS Server Application.\n");

        naHSDefaultSetting(appParams);

        appParams->ServerStartDate.tm_mday       = 3;
        appParams->ServerStartDate.tm_mon        = 11;
        appParams->ServerStartDate.tm_year       = 2006;
        appParams->connections                   = 6;
        appParams->persistent_timeout            = (60 * 3);
        appParams->session_timeout               = (60 * 5);
        appParams->http_buffer_size              = 8096;

        /*  Insecure account digi password sysadm can be used anywhere to access HTTP Realm 1 and FTP */
        NAsetSysAccess (NASYSACC_ADD, APP_USERNAME, APP_PASSWORD, NASYSACC_LEVEL_HTTP_R1 | NASYSACC_LEVEL_RW, NULL);

        /*  Secure account Safe can only be used through the Secure Gateway -- this account's password will never be exposed!   */
        NAsetSysAccess (NASYSACC_ADD, "user", "password", NASYSACC_LEVEL_HTTP_R1 | NASYSACC_LEVEL_RW, NULL);

        result = NAHSStartServer(*appParams);
        if (result != 0)
        {
            printf("root: NAHSStartServer failure[%d].\n", result);
        }
    }
    if (appParams != NULL) free(appParams);
}


