/*************************************************************
/*
/* Program getvalue
/*
/* This program contains an example of how the user can
/* retrieve the value of one or more variables of a
/* process in the KLOE DAQ environment.
/* The update command is sent if the option -u is used.
/* Usage:
/*
/*       getvalue [-u] process@node variable_name[,variable_name...]
/*
/* Created for daqctl v2
/* E. Pasqualucci 9-5-1997
/*
/* Modified for daqctl v2
/* E. Pasqualucci 23-5-1997
/*
/*************************************************************

/* standard include files */

#include <stdio.h>

/* snmp include files */

#include "snmp.h"
#include "asn1.h"
#include "snmp_impl.h"
#include "snmp_api.h"

/* Process template include files */

#include "template.h"
#include "dummy.h"

/* kloe specific include files */

#include "ksnmp.h"
#include "netmap.h"
#include "ksnmplib_protos.h"

/* global variables */

extern char ownpname[];

char command[] = "update ";

char **messages = (char **) NULL;
char **varp = (char **) NULL;
char *variables = (char *) NULL;
char *remote_node = (char *) NULL;
char *process = (char *) NULL;

char **rvar_name = (char **) NULL;

int nvar;
int cmdlen = (int) sizeof (command);
int update = FALSE;

struct snmp_session *sess = (struct snmp_session *) NULL;

void getvalue_help (void)
{
    fprintf (stderr,
             " Usage: getvalue [-u] process@node %s",
             "variable_name[,variable_name...]\n");
    exit (0);
}

void getvalue_init_options (int argc, char **argv)
{
    register int i;
    register int arg;
    char *tmp[2];

    for (i=0 ; i < 2 ; ++i)
        tmp[i] = (char *) NULL;

    for (arg = 1, i = 0; arg < argc; arg++)
        if (argv[arg][0] == '-')
            switch(argv[arg][1])
            {
                case 'h':
                    getvalue_help();
                    break;
                case 'u':
                    update = TRUE;
                    break;
                default:
                    printf ("invalid option: -%c\n", argv[arg][1]);
                    break;
            }
        else
	{
            tmp[i++] = argv[arg];
            if (i > 2)
                printf ("warning: too many parameters\n");
        }

    process = tmp[0];
    variables = tmp[1];

    if (variables == (char *) NULL || process == (char *) NULL)
        getvalue_help();
}

void getvalue_init (void)
{
    register int i = 1;
    int varlen;
    char *newvar;
    char *var = variables;

    while (var = strchr (var, ','))
    {
        ++var;
        ++i;
    }

    varp = (char **) calloc ((size_t) i, sizeof (char *));
    messages = (char **) calloc ((size_t) i, sizeof (char *));

    nvar = i;
    var = variables;

    for (i=0 ; i < nvar ; ++i)
    {
        if (newvar = strchr (var, ','))
            varlen = (int) (newvar - var);
        else
            varlen = strlen (var);

        varp[i] = (char *) calloc (sizeof(varlen) + 1, sizeof (char));
        strncpy (varp[i], var, varlen);

        messages[i] = (char *) calloc (sizeof(command) + sizeof(varlen) + 1,
                                       sizeof(char));
        strcpy (messages[i], command);
        strncat (messages[i], var, varlen);

        var = ++newvar;
    }

    if (!strchr (process, (int) '@'))
    {
        if (Attach_Table() == -1)
        {
            printf("Error in Attach_Table()\n");
            exit(0);
        }
    }
    else
    {
        ksnmp_init();
        init_mib();
    }

    strcpy (ownpname, "getvalue");
}

void getvalue_do (void)
{
    int i;
    byte_seq *bseq;
    struct var_dict_type var;

    if (update)
        for (i=0 ; i < nvar ; ++i)
            if (Send_Message (process, messages[i]) != SUCCESS)
                ErrorSetF (0, "getvalue_do",
                           "Warning: Value of %s not updated",
                           messages[i] + cmdlen - 1);

    for (i=0 ; i < nvar ; ++i)
    {
        Get_Value (process, varp[i], &var);

        printf ("variable name %s\n", var.name);
        printf ("variable type %d\n", var.type);

        switch (var.type)
        {
            case INTEGER:
                printf ("variable value %d\n", *(int *) var.addr);
                break;
            case REAL:
                printf ("variable value %f\n", *(float *) var.addr);
                break;
            case BYTESEQ:
                bseq = (byte_seq *) var.addr;
                printf ("variable size %d\n", bseq->size);
                printf ("variable value (first bytes) %x %x %x %x\n",
                        bseq->btext[0], bseq->btext[1],
                        bseq->btext[2], bseq->btext[3]);
                break;
            case STRING:
                printf ("variable value %s\n", (char *) var.addr);
                break;
        }
    }
}

void getvalue_end (void)
{
    ksnmp_end();
}

main (int argc, char **argv)
{
/*  This is an example of how to retrieve a variable of a process */

    getvalue_init_options (argc, argv);
    getvalue_init();
    getvalue_do();
    getvalue_end();
}