/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * U.S. Government Rights - Commercial software. Government users are subject
 * to the Sun Microsystems, Inc. standard license agreement and applicable
 * provisions of the FAR and its supplements.
 *
 *
 * This distribution may include materials developed by third parties. Sun,
 * Sun Microsystems, the Sun logo and Solaris are trademarks or registered
 * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
 */


/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.iterate.conf,v 1.1.1.1 2003/03/26 18:12:29 pcarroll Exp $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "demo_module_3.h"


me1ContactEntry* testhead ; 
void  construct_table(){
    int total = 10, i;
    me1ContactEntry* prevPtr = 0;
    /* Too lazy, so I make an ordered list */
    for (i=1; i<=total; i++) {
	me1ContactEntry* ptr = (me1ContactEntry *) malloc(sizeof(me1ContactEntry)); 
	ptr->me1FloorNumber = i % 3 + 1;
	ptr->me1RoomNumber = ptr->me1FloorNumber + i + ptr->me1FloorNumber*1000; 
	strcpy(ptr->me1Name, "John Doe ");
	ptr->me1Extension = ptr->me1RoomNumber % 10 + 8000;
	ptr->pNext = NULL;
	if (prevPtr == NULL) {
	    testhead =  prevPtr = ptr;
	}
	    prevPtr->pNext = ptr;
	    prevPtr = ptr;


    }
}
/** Initialize the me1ContactInfoTable table by defining its contents and how it's structured */
void
initialize_table_me1ContactInfoTable(void)
{
    static oid me1ContactInfoTable_oid[] = {1,3,6,1,4,1,42,2,2,4,4,1,3};
    netsnmp_table_registration_info *table_info;
    netsnmp_handler_registration *my_handler;
    netsnmp_iterator_info *iinfo;

    /* create the table structure itself */
    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);

    /* if your table is read only, it's easiest to change the
       HANDLER_CAN_RWRITE definition below to HANDLER_CAN_RONLY */
    my_handler = netsnmp_create_handler_registration("me1ContactInfoTable",
                                             me1ContactInfoTable_handler,
                                             me1ContactInfoTable_oid,
                                             OID_LENGTH(me1ContactInfoTable_oid),
                                             HANDLER_CAN_RWRITE);
            
    if (!my_handler || !table_info || !iinfo)
        return; /* mallocs failed */

    /***************************************************
     * Setting up the table's definition
     */
    netsnmp_table_helper_add_indexes(table_info,
                                  ASN_INTEGER, /* index: me1FloorNumber */
                                  ASN_INTEGER, /* index: me1RoomNumber */
                             0);

    table_info->min_column = 1;
    table_info->max_column = 4;

    /* iterator access routines */
    iinfo->get_first_data_point = me1ContactInfoTable_get_first_data_point;
    iinfo->get_next_data_point = me1ContactInfoTable_get_next_data_point;

    iinfo->table_reginfo = table_info;

    /***************************************************
     * registering the table with the master agent
     */
    DEBUGMSGTL(("initialize_table_me1ContactInfoTable",
                "Registering table me1ContactInfoTable as a table iterator\n"));		 
    netsnmp_register_table_iterator(my_handler, iinfo);
}

/** Initializes the demo_module_3 module */
void
init_demo_module_3(void)
{

  /* here we initialize all the tables we're planning on supporting */
    initialize_table_me1ContactInfoTable();
    construct_table();
}

/** returns the first data point within the me1ContactInfoTable table data.

    Set the my_loop_context variable to the first data point structure
    of your choice (from which you can find the next one).  This could
    be anything from the first node in a linked list, to an integer
    pointer containing the beginning of an array variable.

    Set the my_data_context variable to something to be returned to
    you later that will provide you with the data to return in a given
    row.  This could be the same pointer as what my_loop_context is
    set to, or something different.

    The put_index_data variable contains a list of snmp variable
    bindings, one for each index in your table.  Set the values of
    each appropriately according to the data matching the first row
    and return the put_index_data variable at the end of the function.
*/
netsnmp_variable_list *
me1ContactInfoTable_get_first_data_point(void **my_loop_context, void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{

    netsnmp_variable_list *vptr;

    me1ContactEntry* firstNode = testhead;
    if (!firstNode) {
	printf("The head is NULL ***********\n");
	return NULL;
    }
    *my_loop_context = firstNode;
    *my_data_context = firstNode;

    vptr = put_index_data;
    
    snmp_set_var_value(vptr, (u_char *) &firstNode->me1FloorNumber, sizeof(firstNode->me1FloorNumber));
    vptr = vptr->next_variable;
    snmp_set_var_value(vptr, (u_char *) &firstNode->me1RoomNumber, sizeof(firstNode->me1RoomNumber));
    vptr = vptr->next_variable;

    return put_index_data;
}

/** functionally the same as me1ContactInfoTable_get_first_data_point, but
   my_loop_context has already been set to a previous value and should
   be updated to the next in the list.  For example, if it was a
   linked list, you might want to cast it and the return
   my_loop_context->next.  The my_data_context pointer should be set
   to something you need later and the indexes in put_index_data
   updated again. */

netsnmp_variable_list *
me1ContactInfoTable_get_next_data_point(void **my_loop_context, void **my_data_context,
                         netsnmp_variable_list *put_index_data,
                         netsnmp_iterator_info *mydata)
{

    netsnmp_variable_list *vptr;

    me1ContactEntry* nextNode = (me1ContactEntry*) *my_loop_context;
    nextNode = nextNode->pNext;

    if (!nextNode) {
   /*     printf("No data returned in get_next\n"); */
	return NULL;
    }
    *my_loop_context = nextNode;
    *my_data_context = nextNode;

    vptr = put_index_data;
    
    snmp_set_var_value(vptr, (u_char *) &nextNode->me1FloorNumber, sizeof(nextNode->me1FloorNumber));
    vptr = vptr->next_variable;
    snmp_set_var_value(vptr, (u_char *) &nextNode->me1RoomNumber, sizeof(nextNode->me1RoomNumber));
    vptr = vptr->next_variable;

    return put_index_data;
}

/** handles requests for the me1ContactInfoTable table, if anything else needs to be done */
int
me1ContactInfoTable_handler(
    netsnmp_mib_handler               *handler,
    netsnmp_handler_registration      *reginfo,
    netsnmp_agent_request_info        *reqinfo,
    netsnmp_request_info              *requests) {

    netsnmp_request_info *request;
    netsnmp_table_request_info *table_info;
    netsnmp_variable_list *var;
    me1ContactEntry* data;
    
    for(request = requests; request; request = request->next) {
        var = request->requestvb;
        if (request->processed != 0)
            continue;

        /* perform anything here that you need to do before each
           request is processed. */

        /* the following extracts the my_data_context pointer set in
           the loop functions above.  You can then use the results to
           help return data for the columns of the me1ContactInfoTable table in question */
        data = (me1ContactEntry *) netsnmp_extract_iterator_context(request);
        if ( data == NULL) {
            if (reqinfo->mode == MODE_GET) {
                netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
                continue;
            }
            /* XXX: no row existed, if you support creation and this is a
               set, start dealing with it here, else continue */
        }

        /* extracts the information about the table from the request */
        table_info = netsnmp_extract_table_info(request);
        /* table_info->colnum contains the column number requested */
        /* table_info->indexes contains a linked list of snmp variable
           bindings for the indexes of the table.  Values in the list
           have been set corresponding to the indexes of the
           request */
        if (table_info==NULL) {
            continue;
        }

        switch(reqinfo->mode) {
            /* the table_iterator helper should change all GETNEXTs
               into GETs for you automatically, so you don't have to
               worry about the GETNEXT case.  Only GETs and SETs need
               to be dealt with here */
            case MODE_GET:
                switch(table_info->colnum) {
                    case COLUMN_ME1FLOORNUMBER:
                        snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1FloorNumber,sizeof(data->me1FloorNumber));
                        break;

                    case COLUMN_ME1ROOMNUMBER:
                        snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1RoomNumber, sizeof(data->me1RoomNumber));
                        break;

                    case COLUMN_ME1NAME:
                        snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) data->me1Name, strlen(data->me1Name));
                        break;

                    case COLUMN_ME1EXTENSION:
                        snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1Extension, sizeof(data->me1Extension));
                        break;

                    default:
                        /* We shouldn't get here */
                        snmp_log(LOG_ERR, "problem encountered in me1ContactInfoTable_handler: unknown column\n");
                }
                break;

            case MODE_SET_RESERVE1:
                /* set handling... */

            default:
                snmp_log(LOG_ERR, "problem encountered in me1ContactInfoTable_handler: unsupported mode\n");
        }
    }
    return SNMP_ERR_NOERROR;
}
