/*
 * Copyright Notice:
 * Copyright Treck Incorporated  1997 - 2004.
 * Copyright Elmic Incorporated  1997 - 2004.
 * No portions or the complete contents of this file may be copied or
 * distributed in any form (including but not limited to printed or electronic
 * forms) without the expressed written consent of Treck Inc OR Elmic Systems.
 * Copyright laws and International Treaties protect the contents of this
 * file. Unauthorized use is prohibited. All rights reserved.
 *
 * Description:
 *
 * Filename: example_local.c
 * Generated by The Treck MIB compiler - version 1.23.
 * Date Created: Wed Aug 23 20:19:55 2006
 */


#include "trdefs.h"
#include "trasn1.h"
#include "trsnmp.h"
#include "trimpl.h"
#include "trmib.h"
#include "trconsts.h"
#ifdef TM_SNMP_AGENT_V3
#include "trsnmpv3.h"
#include "trdeskey.h"
#endif /* TM_SNMP_AGENT_V3 */
#include "trregstr.h"
#include "example_local.h"
#include "example_var.h"

/* Include you local .h files here */

static struct group example_group;
static int example_group_initialized = 0;

void example_group_init()
{
    memset(&example_group, 0x0, sizeof example_group);
    example_group.greenledon = 1; /* TRUE */
    example_group.yellowledon = 2; /* FALSE */
    strcpy((char *)example_group.bigboss, "Bruce");
    example_group.bigboss_len = strlen((char *)example_group.bigboss);
}

static char *name[] =
{
    "Walpole",
    "Compton",
    "Pelham",
    "Pelham-Holles",
    "Cavendish",
    "William Pitt - the Elder",
    "Pelham-Holles",
    "Stuart",
    "Grenville",
    "Rockingham",
    "William Pitt - the Elder",
    "Grafton",
    "Lord North",
    "Rockingham",
    "Shelburne",
    "Bentinck",
    "William Pitt - the Younger",
    "Addington",
    "William Pitt - the Younger",
    "Grenville",
    "Bentinck",
    "Perceval",
    "Jenkinson",
    "Canning",
    "Robinson",
    "Wellington",
    "Grey",
    "Lamb",
    "Peel",
    "Lamb",
    "Peel",
    "Russell",
    "Derby",
    "Aberdeen",
    "Palmerston",
    "Derby",
    "Palmerston",
    "Russell",
    "Derby",
    "Disraeli",
    "Gladstone",
    "Disraeli",
    "Gladstone",
    "Salisbury",
    "Gladstone",
    "Salisbury",
    "Gladstone",
    "Rosebery",
    "Salisbury",
    "Balfour",
    "Campbell-Bannerman",
    "Asquith",
    "George",
    "Law",
    "Baldwin",
    "MacDonald",
    "Baldwin",
    "MacDonald",
    "Baldwin",
    "Chamberlain",
    "Churchill",
    "Atlee",
    "Churchill",
    "Eden",
    "Macmillan",
    "Douglas-Home",
    "Wilson",
    "Heath",
    "Wilson",
    "Callaghan",
    "Thatcher",
    "Major",
    "Blair"
};

static char example_table[sizeof name / sizeof (char *)][MAX_STRING_LEN];
static int example_table_initialized = 0;
static int example_table_entries = sizeof name / sizeof (char *);

void example_table_init()
{
    int j;

    for (j = 0; j < example_table_entries; j++)
    {
        if (strlen(name[j]) < MAX_STRING_LEN)
            strcpy(&example_table[j][0], name[j]);
    }
}


/********************************************************************
* tfgroup_get
*
* PARAMETERS:
*   what    - one of the #define's in the xxx_var.h file
*   length  - pointer to the value length
*
* RETURNS:
*   Pointer to the variable's value on success, NULL on failure
*
* NOTES:
*
*********************************************************************/
long *tfgroup_get(u_char what, int *length)
{
    /* Insert your code here */
    static long * data_ptr = NULL;

    *length = 0;
    if (!example_group_initialized)
    {
        example_group_init();
        example_group_initialized = 1;
    }

    switch(what)
    {
        case GREENLEDON:
            *length = sizeof(ttS32Bit);
            data_ptr = &example_group.greenledon;
            break;
        case YELLOWLEDON:
            *length = sizeof(ttS32Bit);
            data_ptr = &example_group.yellowledon;
            break;
        case BIGBOSS:
            *length = example_group.bigboss_len;
            data_ptr = (long *)example_group.bigboss;
        default:
            break;
    }
    return data_ptr;

    /* Following 2 lines are for dummy return */
#if 0
    static long dumb_return = 7;
    *length = 4;
    return &dumb_return;
#endif
}

/********************************************************************
* tfwrite_group
*
* PARAMETERS:
*   what          - one of the #define's in the xxx_var.h file.
*   buffer        - pointer to the value to write
*   buffer_length - length of the value
*
* RETURNS:
*   OK on success, FAIL on failure
*
* NOTES:
*
*********************************************************************/
int tfwrite_group(
    int what, u_char *buffer, int buffer_length)
{
    /* Insert your code here */
    int retValue = OK;

    if (buffer == NULL || buffer_length == 0)
    {
        retValue = FALSE;
        goto exit;
    }
    if (!example_group_initialized)
    {
        example_group_init();
        example_group_initialized = 1;
    }

    switch(what)
    {
        case GREENLEDON:
            if (buffer_length != sizeof(ttS32Bit))
            {
                retValue = FALSE;
                goto exit;
            }
            memcpy(&example_group.greenledon, buffer, buffer_length);
            break;
        case YELLOWLEDON:
            if (buffer_length != sizeof(ttS32Bit))
            {
                retValue = FALSE;
                goto exit;
            }
            memcpy(&example_group.yellowledon, buffer, buffer_length);
            break;
        case BIGBOSS:
            if (buffer_length > MAX_STRING_LEN)
            {
                retValue = FALSE;
                goto exit;
            }
            memcpy(example_group.bigboss, buffer, buffer_length);
            example_group.bigboss_len = buffer_length;
            break;
        default:
            return FALSE;
    }

exit:
    return retValue;
}

/********************************************************************
* tfsimpletable_entry_get
*
* PARAMETERS:
*   exact   - 1 if the exact value is retrieved; 0 otherwise
*   entry0  - pointer to a struct simpletable_entry
*
* RETURNS:
*   1 on success, 0 on failure
*
* NOTES:
*   This function is called with the pointer to 
*   struct simpletable_entry
*   On entry the indices are set, to allow to identify the row,
*   and all other fields are bzero'd. An entry may only be used
*   to identify the row (in case of a row with multiple index or in case
*   the table has an external object index.) The indexes (indices) may or
*   may not be valid row index (indices).
*   If exact == 1 and the row is found, fill in ALL fields in the row
*   entry and return 1.
*   If exact == 1 and the row is not found, return 0.
*   If exact == 0 and there is a row lexicographically following
*   the given one, fill in ALL fields from that row, including
*   index (indices) and return 1.
*   If exact == 0 and there are no rows lexicographically following
*   the given one, return 0
*
*********************************************************************/
int tfsimpletable_entry_get(int exact,
     struct simpletable_entry *entry0)
{
    /* Insert your code here */
    int retValue = 1;

    if (entry0 == NULL)
    {
        retValue = 0;
        goto exit;
    }

    if (!example_table_initialized)
    {
        example_table_init();
        example_table_initialized = 1;
    }

    if (!exact)
    {
        if (entry0->index >= example_table_entries ||
            !entry0->index)
        {
            retValue = 0;
            goto exit;
        }

        if (entry0->index < 0)
            entry0->index = 1;
        else
            entry0->index++;
    }
    else
    {
        if (entry0->index > example_table_entries ||
            entry0->index < 1)
        {
            retValue = 0;
            goto exit;
        }
    }
    entry0->index_len = sizeof(ttS32Bit);
    strcpy((char *)entry0->name, &example_table[entry0->index - 1][0]);
    entry0->name_len = strlen(&example_table[entry0->index - 1][0]);

exit:
    return retValue;
}

/********************************************************************
* tfwrite_simpletable_entry
*
* PARAMETERS:
*   index0        - table of index (per RFC)
*   what          - one of the #define's in the xxx_var.h file.
*                   (table's column)
*   value         - pointer to the value to write
*   value_length  - length of the value
*
* RETURNS:
*   OK on success, FAIL on failure
*
* NOTES:
*
*********************************************************************/
int tfwrite_simpletable_entry(
    int index0, 
    int what, u_char *value, int value_length)
{
    /* Insert your code here */
    int retValue = OK;

    if (index0 > example_table_entries ||
        index0 < 1 ||
        value == NULL ||
        !value_length ||
        value_length > MAX_STRING_LEN)
    {
        /* check for valid function arguments */
        retValue = FAIL;
        goto exit;
    }

    memcpy(&example_table[index0 - 1][0], (const char *)value, value_length);
    example_table[index0 - 1][value_length] = '\0'; /* null terminate the string */

exit:
    return retValue;
}

