/*
 *
 *     Module Name: root.c
 *	       Version: 1.00
 *   Original Date: 08/23/99	
 *	        Author: Contact NetSilicon technical support for information about this application.
 *	      Language: Ansi C
 * Compile Options:
 * Compile defines:
 *	     Libraries:
 *    Link Options:
 *
 *    Entry Points: applicationStart(), applicationTcpDown()
 *
 *  Copyrighted (c) by NETsilicon, Inc.  All Rights Reserved.
 *
 * 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 <tx_api.h>
#include <sockapi.h>
#include <snmpapi.h>
#include <sysAccess.h>
#include "cli.h"



#include "appconf.h"
#include "snmpv3api.h"
#include "boardParams.h"
#include "narmapi.h"


/*
 * Set this to 1 to run the system POST tests during startup.
 */
const int APP_POST = 0;

 

/*
 * Set this to 1 to run the manufacturing burn in tests.
 */
int APP_BURN_IN_TEST = 0; 




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


NaSnmpNotificationTarget_t notificationTargets[] = 
{
    {"10.52.35.113", 162, AF_INET, "public", NA_SNMP_MPMODEL_V1},
    {"10.52.35.113", 162, AF_INET, "public", NA_SNMP_MPMODEL_V2C},
    {"10.52.35.113", 162, AF_INET, "public", NA_SNMP_MPMODEL_V3}
};

/*
 * Set up to trap targets for all traps.
 */
NaStatus setupTraps(void)
{
    NaStatus result = NA_SUCCESS;

    result = naSetNotificationUserName("Trap User");
    if (result != NA_SUCCESS)
        goto exit;

    result = naSetNotificationContextName("");
    if (result != NA_SUCCESS)
        goto exit;

#ifdef DISABLE_COLD_START_TRAP
    result = naSnmpDisableColdStartTrap();
    if (result != NA_SUCCESS)
        goto exit;
#endif
    result = naSnmpSetNotificationTargets(notificationTargets, 
                            sizeof notificationTargets / sizeof notificationTargets[0]);

exit: 
    return result;
}
/*
 *
 *  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)

{
	static devBoardParamsType nvram;        /* declared static because too large for stack*/
    
    printf ("Adding SNMPv1 public and private communities.\n");
    /*
     * The SNMP agent will automatically create community table security entries for the
     * public and private communities and map them to publicuser and privateuser USM
     * users respectively. The password parameter to the 
     * NAsetSysAccess() function sets the community string.
     *
     * Note that SNMPv1 is inherently unsecure.  You may want to disable all
     * access through SNMPv1 protocol in shipping products.
     */
    /*  Add Username private/private to the System Access Database   */
    NAsetSysAccess (NASYSACC_ADD, "private", "private", NASYSACC_LEVEL_SNMP_RW, NULL);

    /*  Add Username public/public to the System Access Database   */
    NAsetSysAccess (NASYSACC_ADD, "public", "public", NASYSACC_LEVEL_SNMP_R, NULL);

    /*
     * Read the NVRAM parameters to see how many times the agent has been rebooted.
     * Update that number and write it back.  Of course you are NOT going to use
     * our NVRAM structure WHICH IS FOR DEVELOPMENT BOARDS ONLY.  You will store information
     * in NVRAM in your own way as needed for your application.
     */
    customizeReadDevBoardParams(&nvram);
    /*
     * Range check the value.  It must be between NA_SNMP_MIN_BOOTS and NA_SNMP_MAX_BOOTS_TIME.
     * If it is less than NA_SNMP_MIN_BOOTS make it NA_SNMP_MIN_BOOTS.  If it is less than
     * NA_SNMP_MAX_BOOTS_TIME then increment it.  If it is equal to NA_SNMP_MAX_BOOTS_TIME,
     * then the RFC says it should not be incremented, but just left there until someone
     * resets it back to 1.  
     */
    if (nvram.snmpEngineBoots < NA_SNMP_MIN_BOOTS)
    {
        nvram.snmpEngineBoots = NA_SNMP_MIN_BOOTS;
    }
    else if (nvram.snmpEngineBoots < NA_SNMP_MAX_BOOTS_TIME)
    {
        nvram.snmpEngineBoots++;
    }
    else
    {
        nvram.snmpEngineBoots = NA_SNMP_MAX_BOOTS_TIME;
    }
    /*
     * Be sure we write the number of reboots back to NVRAM so that it gets incremented
     * everytime we reboot.  This is required by the RFC.
     */
    customizeWriteDevBoardParams(&nvram);
    
    /*
     * Number of boots should be set before the agent starts.  If you don't call
     * this function, then the value 1 is used instead.
     */
    naSnmpSetEngineBoots(nvram.snmpEngineBoots);
    
    /*
     * The SNMP engine ID MUST be set before the agent starts if you do
     * not intend to use the default one.  This is because it is used to
     * hash authentication and privacy keys.  We will use the default one,
     * so we don't need to set it.  The default engine ID is based on the
     * unit's MAC address.
     */
    

    /*
     * Set the destination address for authentication failure traps.  
     */
    setupTraps();
    
    /*
     * At this point we are ready to start the SNMP agent by calling snmpd_load(), and 
     * that's what you would do in most applications.  However, this application provides
     * a command line interface that lets you fool around with the SNMPv3 API.  So, we
     * start the CLI instead.  You can start the agent by typing "start" at the command
     * prompt.
     */
    startCli();
    
}



