components/net-snmp/sun/sdk/demo/demo_module_3/demo_module_3.c
changeset 252 ee0fb1eabcbf
equal deleted inserted replaced
251:f527656d334f 252:ee0fb1eabcbf
       
     1 /*
       
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  *
       
     4  * U.S. Government Rights - Commercial software. Government users are subject
       
     5  * to the Sun Microsystems, Inc. standard license agreement and applicable
       
     6  * provisions of the FAR and its supplements.
       
     7  *
       
     8  *
       
     9  * This distribution may include materials developed by third parties. Sun,
       
    10  * Sun Microsystems, the Sun logo and Solaris are trademarks or registered
       
    11  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
       
    12  */
       
    13 
       
    14 
       
    15 /*
       
    16  * Note: this file originally auto-generated by mib2c using
       
    17  *        : mib2c.iterate.conf,v 1.1.1.1 2003/03/26 18:12:29 pcarroll Exp $
       
    18  */
       
    19 
       
    20 #include <net-snmp/net-snmp-config.h>
       
    21 #include <net-snmp/net-snmp-includes.h>
       
    22 #include <net-snmp/agent/net-snmp-agent-includes.h>
       
    23 #include "demo_module_3.h"
       
    24 
       
    25 
       
    26 me1ContactEntry* testhead ; 
       
    27 void  construct_table(){
       
    28     int total = 10, i;
       
    29     me1ContactEntry* prevPtr = 0;
       
    30     /* Too lazy, so I make an ordered list */
       
    31     for (i=1; i<=total; i++) {
       
    32 	me1ContactEntry* ptr = (me1ContactEntry *) malloc(sizeof(me1ContactEntry)); 
       
    33 	ptr->me1FloorNumber = i % 3 + 1;
       
    34 	ptr->me1RoomNumber = ptr->me1FloorNumber + i + ptr->me1FloorNumber*1000; 
       
    35 	strcpy(ptr->me1Name, "John Doe ");
       
    36 	ptr->me1Extension = ptr->me1RoomNumber % 10 + 8000;
       
    37 	ptr->pNext = NULL;
       
    38 	if (prevPtr == NULL) {
       
    39 	    testhead =  prevPtr = ptr;
       
    40 	}
       
    41 	    prevPtr->pNext = ptr;
       
    42 	    prevPtr = ptr;
       
    43 
       
    44 
       
    45     }
       
    46 }
       
    47 /** Initialize the me1ContactInfoTable table by defining its contents and how it's structured */
       
    48 void
       
    49 initialize_table_me1ContactInfoTable(void)
       
    50 {
       
    51     static oid me1ContactInfoTable_oid[] = {1,3,6,1,4,1,42,2,2,4,4,1,3};
       
    52     netsnmp_table_registration_info *table_info;
       
    53     netsnmp_handler_registration *my_handler;
       
    54     netsnmp_iterator_info *iinfo;
       
    55 
       
    56     /* create the table structure itself */
       
    57     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
       
    58     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
       
    59 
       
    60     /* if your table is read only, it's easiest to change the
       
    61        HANDLER_CAN_RWRITE definition below to HANDLER_CAN_RONLY */
       
    62     my_handler = netsnmp_create_handler_registration("me1ContactInfoTable",
       
    63                                              me1ContactInfoTable_handler,
       
    64                                              me1ContactInfoTable_oid,
       
    65                                              OID_LENGTH(me1ContactInfoTable_oid),
       
    66                                              HANDLER_CAN_RWRITE);
       
    67             
       
    68     if (!my_handler || !table_info || !iinfo)
       
    69         return; /* mallocs failed */
       
    70 
       
    71     /***************************************************
       
    72      * Setting up the table's definition
       
    73      */
       
    74     netsnmp_table_helper_add_indexes(table_info,
       
    75                                   ASN_INTEGER, /* index: me1FloorNumber */
       
    76                                   ASN_INTEGER, /* index: me1RoomNumber */
       
    77                              0);
       
    78 
       
    79     table_info->min_column = 1;
       
    80     table_info->max_column = 4;
       
    81 
       
    82     /* iterator access routines */
       
    83     iinfo->get_first_data_point = me1ContactInfoTable_get_first_data_point;
       
    84     iinfo->get_next_data_point = me1ContactInfoTable_get_next_data_point;
       
    85 
       
    86     iinfo->table_reginfo = table_info;
       
    87 
       
    88     /***************************************************
       
    89      * registering the table with the master agent
       
    90      */
       
    91     DEBUGMSGTL(("initialize_table_me1ContactInfoTable",
       
    92                 "Registering table me1ContactInfoTable as a table iterator\n"));		 
       
    93     netsnmp_register_table_iterator(my_handler, iinfo);
       
    94 }
       
    95 
       
    96 /** Initializes the demo_module_3 module */
       
    97 void
       
    98 init_demo_module_3(void)
       
    99 {
       
   100 
       
   101   /* here we initialize all the tables we're planning on supporting */
       
   102     initialize_table_me1ContactInfoTable();
       
   103     construct_table();
       
   104 }
       
   105 
       
   106 /** returns the first data point within the me1ContactInfoTable table data.
       
   107 
       
   108     Set the my_loop_context variable to the first data point structure
       
   109     of your choice (from which you can find the next one).  This could
       
   110     be anything from the first node in a linked list, to an integer
       
   111     pointer containing the beginning of an array variable.
       
   112 
       
   113     Set the my_data_context variable to something to be returned to
       
   114     you later that will provide you with the data to return in a given
       
   115     row.  This could be the same pointer as what my_loop_context is
       
   116     set to, or something different.
       
   117 
       
   118     The put_index_data variable contains a list of snmp variable
       
   119     bindings, one for each index in your table.  Set the values of
       
   120     each appropriately according to the data matching the first row
       
   121     and return the put_index_data variable at the end of the function.
       
   122 */
       
   123 netsnmp_variable_list *
       
   124 me1ContactInfoTable_get_first_data_point(void **my_loop_context, void **my_data_context,
       
   125                           netsnmp_variable_list *put_index_data,
       
   126                           netsnmp_iterator_info *mydata)
       
   127 {
       
   128 
       
   129     netsnmp_variable_list *vptr;
       
   130 
       
   131     me1ContactEntry* firstNode = testhead;
       
   132     if (!firstNode) {
       
   133 	printf("The head is NULL ***********\n");
       
   134 	return NULL;
       
   135     }
       
   136     *my_loop_context = firstNode;
       
   137     *my_data_context = firstNode;
       
   138 
       
   139     vptr = put_index_data;
       
   140     
       
   141     snmp_set_var_value(vptr, (u_char *) &firstNode->me1FloorNumber, sizeof(firstNode->me1FloorNumber));
       
   142     vptr = vptr->next_variable;
       
   143     snmp_set_var_value(vptr, (u_char *) &firstNode->me1RoomNumber, sizeof(firstNode->me1RoomNumber));
       
   144     vptr = vptr->next_variable;
       
   145 
       
   146     return put_index_data;
       
   147 }
       
   148 
       
   149 /** functionally the same as me1ContactInfoTable_get_first_data_point, but
       
   150    my_loop_context has already been set to a previous value and should
       
   151    be updated to the next in the list.  For example, if it was a
       
   152    linked list, you might want to cast it and the return
       
   153    my_loop_context->next.  The my_data_context pointer should be set
       
   154    to something you need later and the indexes in put_index_data
       
   155    updated again. */
       
   156 
       
   157 netsnmp_variable_list *
       
   158 me1ContactInfoTable_get_next_data_point(void **my_loop_context, void **my_data_context,
       
   159                          netsnmp_variable_list *put_index_data,
       
   160                          netsnmp_iterator_info *mydata)
       
   161 {
       
   162 
       
   163     netsnmp_variable_list *vptr;
       
   164 
       
   165     me1ContactEntry* nextNode = (me1ContactEntry*) *my_loop_context;
       
   166     nextNode = nextNode->pNext;
       
   167 
       
   168     if (!nextNode) {
       
   169    /*     printf("No data returned in get_next\n"); */
       
   170 	return NULL;
       
   171     }
       
   172     *my_loop_context = nextNode;
       
   173     *my_data_context = nextNode;
       
   174 
       
   175     vptr = put_index_data;
       
   176     
       
   177     snmp_set_var_value(vptr, (u_char *) &nextNode->me1FloorNumber, sizeof(nextNode->me1FloorNumber));
       
   178     vptr = vptr->next_variable;
       
   179     snmp_set_var_value(vptr, (u_char *) &nextNode->me1RoomNumber, sizeof(nextNode->me1RoomNumber));
       
   180     vptr = vptr->next_variable;
       
   181 
       
   182     return put_index_data;
       
   183 }
       
   184 
       
   185 /** handles requests for the me1ContactInfoTable table, if anything else needs to be done */
       
   186 int
       
   187 me1ContactInfoTable_handler(
       
   188     netsnmp_mib_handler               *handler,
       
   189     netsnmp_handler_registration      *reginfo,
       
   190     netsnmp_agent_request_info        *reqinfo,
       
   191     netsnmp_request_info              *requests) {
       
   192 
       
   193     netsnmp_request_info *request;
       
   194     netsnmp_table_request_info *table_info;
       
   195     netsnmp_variable_list *var;
       
   196     me1ContactEntry* data;
       
   197     
       
   198     for(request = requests; request; request = request->next) {
       
   199         var = request->requestvb;
       
   200         if (request->processed != 0)
       
   201             continue;
       
   202 
       
   203         /* perform anything here that you need to do before each
       
   204            request is processed. */
       
   205 
       
   206         /* the following extracts the my_data_context pointer set in
       
   207            the loop functions above.  You can then use the results to
       
   208            help return data for the columns of the me1ContactInfoTable table in question */
       
   209         data = (me1ContactEntry *) netsnmp_extract_iterator_context(request);
       
   210         if ( data == NULL) {
       
   211             if (reqinfo->mode == MODE_GET) {
       
   212                 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
       
   213                 continue;
       
   214             }
       
   215             /* XXX: no row existed, if you support creation and this is a
       
   216                set, start dealing with it here, else continue */
       
   217         }
       
   218 
       
   219         /* extracts the information about the table from the request */
       
   220         table_info = netsnmp_extract_table_info(request);
       
   221         /* table_info->colnum contains the column number requested */
       
   222         /* table_info->indexes contains a linked list of snmp variable
       
   223            bindings for the indexes of the table.  Values in the list
       
   224            have been set corresponding to the indexes of the
       
   225            request */
       
   226         if (table_info==NULL) {
       
   227             continue;
       
   228         }
       
   229 
       
   230         switch(reqinfo->mode) {
       
   231             /* the table_iterator helper should change all GETNEXTs
       
   232                into GETs for you automatically, so you don't have to
       
   233                worry about the GETNEXT case.  Only GETs and SETs need
       
   234                to be dealt with here */
       
   235             case MODE_GET:
       
   236                 switch(table_info->colnum) {
       
   237                     case COLUMN_ME1FLOORNUMBER:
       
   238                         snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1FloorNumber,sizeof(data->me1FloorNumber));
       
   239                         break;
       
   240 
       
   241                     case COLUMN_ME1ROOMNUMBER:
       
   242                         snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1RoomNumber, sizeof(data->me1RoomNumber));
       
   243                         break;
       
   244 
       
   245                     case COLUMN_ME1NAME:
       
   246                         snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) data->me1Name, strlen(data->me1Name));
       
   247                         break;
       
   248 
       
   249                     case COLUMN_ME1EXTENSION:
       
   250                         snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &data->me1Extension, sizeof(data->me1Extension));
       
   251                         break;
       
   252 
       
   253                     default:
       
   254                         /* We shouldn't get here */
       
   255                         snmp_log(LOG_ERR, "problem encountered in me1ContactInfoTable_handler: unknown column\n");
       
   256                 }
       
   257                 break;
       
   258 
       
   259             case MODE_SET_RESERVE1:
       
   260                 /* set handling... */
       
   261 
       
   262             default:
       
   263                 snmp_log(LOG_ERR, "problem encountered in me1ContactInfoTable_handler: unsupported mode\n");
       
   264         }
       
   265     }
       
   266     return SNMP_ERR_NOERROR;
       
   267 }