/*
 * White Rabbit SNMP
 * Copyright (C) 2010, CERN.
 *
 * Version:     wrsw_snmpd v1.0
 *
 * Authors:     Miguel Baizán (miguel.baizan@integrasys-sa.com)
 *
 * Description: The ieee8021BridgePortPriorityTable_data_access file contains
 *              the interface to the data in its raw format. These functions are
 *              used to build the row cache or locate the row (depending on the
 *              table access method).
 *              Note: this file originally auto-generated by mib2c using
 *              mib2c.mfd.conf 17337
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* standard Net-SNMP includes */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

#include <sys/ioctl.h>
#include <linux/if.h>

#include <wr_ipc.h>
#include <wrsw_hal.h>
#include <hal_exports.h>

/* include our parent header */
#include "ieee8021BridgePortPriorityTable.h"


#include "ieee8021BridgePortPriorityTable_data_access.h"

/** @ingroup interface
 * @addtogroup data_access data_access: Routines to access data
 *
 * These routines are used to locate the data used to satisfy
 * requests.
 *
 * @{
 */
/**********************************************************************
 **********************************************************************
 ***
 *** Table ieee8021BridgePortPriorityTable
 ***
 **********************************************************************
 **********************************************************************/
/*
 * IEEE8021-BRIDGE-MIB::ieee8021BridgePortPriorityTable is subid 1 of ieee8021BridgePriority.
 * Its status is Current.
 * OID: .1.3.111.2.802.1.1.2.1.3.1, length: 11
*/

/**
 * initialization for ieee8021BridgePortPriorityTable data access
 *
 * This function is called during startup to allow you to
 * allocate any resources you need for the data table.
 *
 * @param ieee8021BridgePortPriorityTable_reg
 *        Pointer to ieee8021BridgePortPriorityTable_registration
 *
 * @retval MFD_SUCCESS : success.
 * @retval MFD_ERROR   : unrecoverable error.
 */
int
ieee8021BridgePortPriorityTable_init_data(ieee8021BridgePortPriorityTable_registration * ieee8021BridgePortPriorityTable_reg)
{
    DEBUGMSGTL(("verbose:ieee8021BridgePortPriorityTable:ieee8021BridgePortPriorityTable_init_data","called\n"));

    /*
     * TODO:303:o: Initialize ieee8021BridgePortPriorityTable data.
     */

    return MFD_SUCCESS;
} /* ieee8021BridgePortPriorityTable_init_data */

/**
 * container overview
 *
 */

/**
 * container initialization
 *
 * @param container_ptr_ptr A pointer to a container pointer. If you
 *        create a custom container, use this parameter to return it
 *        to the MFD helper. If set to NULL, the MFD helper will
 *        allocate a container for you.
 * @param  cache A pointer to a cache structure. You can set the timeout
 *         and other cache flags using this pointer.
 *
 *  This function is called at startup to allow you to customize certain
 *  aspects of the access method. For the most part, it is for advanced
 *  users. The default code should suffice for most cases. If no custom
 *  container is allocated, the MFD code will create one for your.
 *
 *  This is also the place to set up cache behavior. The default, to
 *  simply set the cache timeout, will work well with the default
 *  container. If you are using a custom container, you may want to
 *  look at the cache helper documentation to see if there are any
 *  flags you want to set.
 *
 * @remark
 *  This would also be a good place to do any initialization needed
 *  for you data source. For example, opening a connection to another
 *  process that will supply the data, opening a database, etc.
 */
void
ieee8021BridgePortPriorityTable_container_init(netsnmp_container **container_ptr_ptr,
                             netsnmp_cache *cache)
{
    DEBUGMSGTL(("verbose:ieee8021BridgePortPriorityTable:ieee8021BridgePortPriorityTable_container_init","called\n"));

    if (NULL == container_ptr_ptr) {
        snmp_log(LOG_ERR,"bad container param to ieee8021BridgePortPriorityTable_container_init\n");
        return;
    }

    /*
     * For advanced users, you can use a custom container. If you
     * do not create one, one will be created for you.
     */
    *container_ptr_ptr = NULL;

    if (NULL == cache) {
        snmp_log(LOG_ERR,"bad cache param to ieee8021BridgePortPriorityTable_container_init\n");
        return;
    }

    /*
     * TODO:345:A: Set up ieee8021BridgePortPriorityTable cache properties.
     *
     * Also for advanced users, you can set parameters for the
     * cache. Do not change the magic pointer, as it is used
     * by the MFD helper. To completely disable caching, set
     * cache->enabled to 0.
     */
    cache->timeout = IEEE8021BRIDGEPORTPRIORITYTABLE_CACHE_TIMEOUT; /* seconds */
} /* ieee8021BridgePortPriorityTable_container_init */

/**
 * container shutdown
 *
 * @param container_ptr A pointer to the container.
 *
 *  This function is called at shutdown to allow you to customize certain
 *  aspects of the access method. For the most part, it is for advanced
 *  users. The default code should suffice for most cases.
 *
 *  This function is called before ieee8021BridgePortPriorityTable_container_free().
 *
 * @remark
 *  This would also be a good place to do any cleanup needed
 *  for you data source. For example, closing a connection to another
 *  process that supplied the data, closing a database, etc.
 */
void
ieee8021BridgePortPriorityTable_container_shutdown(netsnmp_container *container_ptr)
{
    DEBUGMSGTL(("verbose:ieee8021BridgePortPriorityTable:ieee8021BridgePortPriorityTable_container_shutdown","called\n"));

    if (NULL == container_ptr) {
        snmp_log(LOG_ERR,"bad params to ieee8021BridgePortPriorityTable_container_shutdown\n");
        return;
    }

} /* ieee8021BridgePortPriorityTable_container_shutdown */

/**
 * load initial data
 *
 * TODO:350:M: Implement ieee8021BridgePortPriorityTable data load
 * This function will also be called by the cache helper to load
 * the container again (after the container free function has been
 * called to free the previous contents).
 *
 * @param container container to which items should be inserted
 *
 * @retval MFD_SUCCESS              : success.
 * @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source
 * @retval MFD_ERROR                : other error.
 *
 *  This function is called to load the index(es) (and data, optionally)
 *  for the every row in the data set.
 *
 * @remark
 *  While loading the data, the only important thing is the indexes.
 *  If access to your data is cheap/fast (e.g. you have a pointer to a
 *  structure in memory), it would make sense to update the data here.
 *  If, however, the accessing the data invovles more work (e.g. parsing
 *  some other existing data, or peforming calculations to derive the data),
 *  then you can limit yourself to setting the indexes and saving any
 *  information you will need later. Then use the saved information in
 *  ieee8021BridgePortPriorityTable_row_prep() for populating data.
 *
 * @note
 *  If you need consistency between rows (like you want statistics
 *  for each row to be from the same time frame), you should set all
 *  data here.
 *
 */
int
ieee8021BridgePortPriorityTable_container_load(netsnmp_container *container)
{
    ieee8021BridgePortPriorityTable_rowreq_ctx *rowreq_ctx;

    wripc_handle_t              hal_ipc;
    hexp_port_list_t            port_list;

    struct wrn_register_req     req;
    struct ifreq                ifr;
    int                         sockfd, i;

    /* Indexes */
    u_long   ieee8021BridgeBasePortComponentId;
    u_long   ieee8021BridgeBasePort;


    DEBUGMSGTL(("ieee8021BridgePortPriorityTable:"
                "ieee8021BridgePortPriorityTable_container_load","called\n"));

    /* Set index value for the Component Id */
    ieee8021BridgeBasePortComponentId = DEFAULT_COMPONENTID;

    /* Create socket interface */
    sockfd = socket(AF_PACKET, SOCK_RAW, 0);
    if (sockfd < 0) {
        snmp_log(LOG_ERR,"socket failed\n");
        return MFD_RESOURCE_UNAVAILABLE;
    }

    /* Connect to HAL to get information of the ports */
    hal_ipc = wripc_connect("wrsw_hal");
    if(hal_ipc < 0) {
        snmp_log(LOG_ERR,"Unable to connect to HAL\n");
        close(sockfd);
        return MFD_ERROR;
    }

    DEBUGMSGTL(("ieee8021BridgePortPriorityTable:"
                "ieee8021BridgePortPriorityTable_container_load",
                "connected to HAL\n"));

    /* Get port list */
    if (wripc_call(hal_ipc, "halexp_query_all_ports", &port_list, 0) < 0) {
        snmp_log(LOG_ERR,"halexp_query_all_ports has not worked\n");
        wripc_close(hal_ipc);
        close(sockfd);
        return MFD_ERROR;
    }

    /* Iterate through port list */
    for (i = 0; i < HAL_MAX_PORTS; i++) {
        DEBUGMSGTL(("ieee8021BridgePortPriorityTable:"
                    "ieee8021BridgePortPriorityTable_container_load",
                    "port %d in port_list is: %s \n",
                    i, port_list.port_names[i]));

        /* Only interested in non null port names*/
        if (port_list.port_names[i][0] != '\0') {
            /* Make custom ioctl to get data */
            ifr.ifr_addr.sa_family = AF_PACKET;
            strncpy(ifr.ifr_name, port_list.port_names[i],
                    sizeof(ifr.ifr_name));

            req.cmd = WRN_ECR_GET_PORTID;
            ifr.ifr_data = &req;

            if (ioctl(sockfd, PRIV_IOCGGETECR, &ifr) < 0) {
                snmp_log(LOG_ERR, "ioctl PRIV_IOCGGETECR failed\n");
            } else {
                ieee8021BridgeBasePort = req.val;
                DEBUGMSGTL(("ieee8021BridgePortPriorityTable:"
                            "ieee8021BridgePortPriorityTable_container_load",
                            "for port %d the PORTID value is: %i \n",
                            i, req.val));

                /* Allocate rowreq context */
                rowreq_ctx =
                    ieee8021BridgePortPriorityTable_allocate_rowreq_ctx(NULL);
                if (NULL == rowreq_ctx) {
                    snmp_log(LOG_ERR, "memory allocation failed\n");
                    wripc_close(hal_ipc);
                    close(sockfd);
                    return MFD_RESOURCE_UNAVAILABLE;
                }

                rowreq_ctx->column_exists_flags =
                    IEEE8021BRIDGEPORTPRIORITYTABLE_IMPLEMENTED_COLS;

                /* Set indexes in the row requets context */
                if (MFD_SUCCESS !=
                    ieee8021BridgePortPriorityTable_indexes_set(rowreq_ctx
                                       , ieee8021BridgeBasePortComponentId
                                       , ieee8021BridgeBasePort)) {
                    snmp_log(LOG_ERR,"error setting index while loading "
                             "ieee8021BridgePortPriorityTable data.\n");
                    ieee8021BridgePortPriorityTable_release_rowreq_ctx(
                        rowreq_ctx);
                    continue;
                }

                /* Setup/save data for ieee8021BridgePortDefaultUserPriority */
                req.cmd = WRN_RFCR_GET_PRIO_VAL;
                ifr.ifr_data = &req;

                if (ioctl(sockfd, PRIV_IOCGGETRFCR, &ifr) < 0) {
                    snmp_log(LOG_ERR, "ioctl PRIV_IOCGGETRFCR failed\n");
                    rowreq_ctx->column_exists_flags &=
                        ~COLUMN_IEEE8021BRIDGEPORTDEFAULTUSERPRIORITY_FLAG;
                } else {
                    rowreq_ctx->data.ieee8021BridgePortDefaultUserPriority =
                        req.val;
                }

                /* Setup/save data for ieee8021BridgePortNumTrafficClasses */
                /* TODO:WR: hardcoded until we get HW support */
                rowreq_ctx->data.ieee8021BridgePortNumTrafficClasses = 8;

                /* insert into table container */
                CONTAINER_INSERT(container, rowreq_ctx);
            }
        } /* Only interested in non null port names*/
    } /* Iterate through port list */

    wripc_close(hal_ipc);
    close(sockfd);
    return MFD_SUCCESS;
} /* ieee8021BridgePortPriorityTable_container_load */


/**
 * container clean up
 *
 * @param container container with all current items
 *
 *  This optional callback is called prior to all
 *  item's being removed from the container. If you
 *  need to do any processing before that, do it here.
 *
 * @note
 *  The MFD helper will take care of releasing all the row contexts.
 *
 */
void
ieee8021BridgePortPriorityTable_container_free(netsnmp_container *container)
{
    DEBUGMSGTL(("verbose:ieee8021BridgePortPriorityTable:ieee8021BridgePortPriorityTable_container_free","called\n"));

    /*
     * TODO:380:M: Free ieee8021BridgePortPriorityTable container data.
     */
} /* ieee8021BridgePortPriorityTable_container_free */

/**
 * prepare row for processing.
 *
 *  When the agent has located the row for a request, this function is
 *  called to prepare the row for processing. If you fully populated
 *  the data context during the index setup phase, you may not need to
 *  do anything.
 *
 * @param rowreq_ctx pointer to a context.
 *
 * @retval MFD_SUCCESS     : success.
 * @retval MFD_ERROR       : other error.
 */
int
ieee8021BridgePortPriorityTable_row_prep( ieee8021BridgePortPriorityTable_rowreq_ctx *rowreq_ctx)
{
    DEBUGMSGTL(("verbose:ieee8021BridgePortPriorityTable:ieee8021BridgePortPriorityTable_row_prep","called\n"));

    netsnmp_assert(NULL != rowreq_ctx);

    /*
     * TODO:390:o: Prepare row for request.
     * If populating row data was delayed, this is the place to
     * fill in the row for this request.
     */

    return MFD_SUCCESS;
} /* ieee8021BridgePortPriorityTable_row_prep */

/** @} */
