Hi Ricardo,

What is going wrong? Can you compile your code? Do you see SNMP packets?

I attached my complete varbind.c file. Maybe it can point you in the right
direction. It has a functions to set u32 values, see
varbind_new_type_gauge().
Don't forget to free the varbind afterwards by calling varbind_free().

Regards,
Harrold




On Mon, 22 Nov 2021 at 19:43, Ricardo Takaki <rtak...@cpqd.com.br> wrote:

> Sorry Harrold, the correct code is listed below. I am not sure if I am
> filling the varbind->value the right way.
> Thanks,
>
> /**
>  * \brief get battery actual capacity
>  * @return
>  */
> u32_t getBatteryActualCapacity(struct snmp_tree_node node){
>
> u32_t batteryActualCapacity = 4;
>
> struct snmp_varbind *varbinds = NULL;
> err_t err;
>
> u32_t varbinds_value = 0;
>
> if(batteryActualCapacity > getBatteryAlarmLowCapacity())
> {
> varbinds_value = 4;
> }
> else{
> varbinds_value = 0;
> }
>
> TPRINTF("getBatteryActualCapacity :: batteryLowCapAgingNotification:
> %d\r\n", varbinds_value);
>
> varbinds->type = SNMP_ASN1_TYPE_UNSIGNED32;
> /arbinds->value = (void*)varbinds_value;
> varbinds->value_len = sizeof(varbinds_value);
>
> err = snmp_send_trap_specific(node.node.oid, varbinds);
>
> TPRINTF("snmp_send_trap_specific() err: %d\r\n", err);
>
> return batteryActualCapacity;
> }
>
> Em seg., 22 de nov. de 2021 às 15:36, Ricardo Takaki <rtak...@cpqd.com.br>
> escreveu:
>
>> Hi Harrold,
>>
>> I have tried your example without success, I am having trouble when
>> configuring the varbinds before sending the trap. Below is part of my code.
>> I actually need to send the batteryActualCapacity parameter as a varbind
>> parameter but I am not sure how to include it into the trap message.
>> Can you please help me with that?
>>
>> Thanks and best regards
>> Ricardo
>>
>> /**
>>  * \brief get battery actual capacity
>>  * @return
>>  */
>> u32_t getBatteryActualCapacity(struct snmp_tree_node node){
>>
>> u32_t batteryActualCapacity = 4;
>>
>> struct snmp_varbind *varbinds = NULL;
>> err_t err;
>>
>> u32_t varbinds_value = 0;
>>
>> if(batteryActualCapacity > getBatteryAlarmLowCapacity())
>> {
>> varbinds_value = 4;
>> }
>> else{
>> varbinds_value = 0;
>> }
>>
>> err = snmp_send_trap_specific(node.node.oid, varbinds);
>>
>> TPRINTF("snmp_send_trap_specific() err: %d\r\n", err);
>>
>> return batteryActualCapacity;
>> }
>>
>>
>> Em sex., 19 de nov. de 2021 às 11:25, Ricardo Takaki <rtak...@cpqd.com.br>
>> escreveu:
>>
>>> Hi Harrold, thanks for the help.
>>> best regards
>>>
>>> Em sex., 19 de nov. de 2021 às 11:05, Harrold Spier <
>>> harrold.sp...@gmail.com> escreveu:
>>>
>>>> Hi  Rtakaki,
>>>>
>>>> Maybe the attache code snippets can help you a little bit. It
>>>> calls snmp_send_trap() at the end, but that function has much in common
>>>> with snmp_send_trap_specific().
>>>>
>>>> The code is extracted from a large application, so it probably need
>>>> some adjustment. Let me know if something is still missing
>>>>
>>>> Best regards,
>>>> Harrold
>>>>
>>>>
>>>> On Fri, 19 Nov 2021 at 13:44, Ricardo Takaki <rtak...@cpqd.com.br>
>>>> wrote:
>>>>
>>>>> Hello all,
>>>>>
>>>>> I am trying to write a code using the snmp_send_trap_specific() lwip
>>>>> function to send a trap notification but I am having trouble filling the
>>>>> function's parameters.
>>>>> I could not see any example in the lwip source code that could help me
>>>>> to use this function.
>>>>> Could you please show me how to use this function?
>>>>> A simple example,  if possible, which uses the specific_trap ad
>>>>> varbind structure would be a great help.
>>>>>
>>>>> Thanks in advance and best regards,
>>>>>
>>>>> rtakaki
>>>>>
>>>>>
>>>>>
>>>>> "Esta mensagem pode conter informações confidenciais ou privilegiadas.
>>>>> Caso não seja o destinatário desta mensagem ou caso ela lhe tenha sido
>>>>> enviada por engano, pedimos a gentileza de eliminar esta mensagem e todos
>>>>> os seus anexos imediatamente. Utilizar, divulgar a quaisquer
>>>>> terceiros ou copiar esta mensagem para qualquer fim é expressamente
>>>>> proibido e punido por lei."
>>>>>
>>>>> "This message may contain privileged or confidential information. If
>>>>> you are not the intended recipient of this message or if you have received
>>>>> it in error, please kindly delete this message and all its attachments
>>>>> immediately. Unauthorized use, disclosure to any third parties or
>>>>> copying of this message is expressly prohibited and may be subject to the
>>>>> penalties of law."
>>>>> _______________________________________________
>>>>> lwip-users mailing list
>>>>> lwip-users@nongnu.org
>>>>> https://lists.nongnu.org/mailman/listinfo/lwip-users
>>>>>
>>>> _______________________________________________
>>>> lwip-users mailing list
>>>> lwip-users@nongnu.org
>>>> https://lists.nongnu.org/mailman/listinfo/lwip-users
>>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>
> --
>
>
> Ricardo Takaki, MSc
>
> CPQD - Diretoria de operações
>
> rtak...@cpqd.com.br
>
> <http://www.cpqd.com.br/>
>
>
>
>
> "Esta mensagem pode conter informações confidenciais ou privilegiadas.
> Caso não seja o destinatário desta mensagem ou caso ela lhe tenha sido
> enviada por engano, pedimos a gentileza de eliminar esta mensagem e todos
> os seus anexos imediatamente. Utilizar, divulgar a quaisquer terceiros ou
> copiar esta mensagem para qualquer fim é expressamente proibido e punido
> por lei."
>
> "This message may contain privileged or confidential information. If you
> are not the intended recipient of this message or if you have received it
> in error, please kindly delete this message and all its attachments
> immediately. Unauthorized use, disclosure to any third parties or copying
> of this message is expressly prohibited and may be subject to the penalties
> of law."
> _______________________________________________
> lwip-users mailing list
> lwip-users@nongnu.org
> https://lists.nongnu.org/mailman/listinfo/lwip-users
>
VARBIND.H:

#ifndef VARBIND_H
#define VARBIND_H

struct snmp_varbind *varbind_new_type_gauge(const struct snmp_obj_id *oid, u32 
value);
struct snmp_varbind *varbind_new_type_integer(const struct snmp_obj_id *oid, 
s32 value);
struct snmp_varbind *varbind_new_type_string(const struct snmp_obj_id *oid, 
const char *s);
struct snmp_varbind *varbind_new_type_null(const struct snmp_obj_id *oid);
struct snmp_varbind *varbind_new_from_id(const struct snmp_obj_id *oid);
void varbind_list_insert(struct snmp_varbind **varbind_list, struct 
snmp_varbind *varbind);
void varbind_list_free(struct snmp_varbind *varbind_list);

#endif /*VARBIND_H*/


VARBIND.C:

#include <lwip/apps/snmp.h>
#include <string.h>
#include "varbind.h"


// Not a lwIP public function, yet.
u8_t snmp_get_node_instance_from_oid(const u32_t *oid, u8_t oid_len, struct 
snmp_node_instance *node_instance);


struct snmp_varbind *varbind_new_type_null(const struct snmp_obj_id *oid)
{
    struct snmp_varbind *varbind;

    // Allocate space for varbind
    varbind = calloc(sizeof(*varbind));
    if (!varbind)
    {
        return NULL;
    }

    varbind->oid = *oid;
    varbind->type = SNMP_ASN1_TYPE_NULL;
    varbind->value = NULL;
    varbind->value_len = 0;

    return varbind;
}

struct snmp_varbind *varbind_new_type_gauge(const struct snmp_obj_id *oid, u32 
value)
{
    struct snmp_varbind *varbind;

    // Allocate space for varbind
    varbind = calloc(sizeof(*varbind));
    if (!varbind)
    {
        return NULL;
    }

    // Allocate space for varbind value
    varbind->value = malloc(sizeof(u32_t));
    if (!varbind->value)
    {
        free(varbind);
        return NULL;
    }

    varbind->oid = *oid;
    varbind->type = SNMP_ASN1_TYPE_GAUGE;
    *(u32_t *)varbind->value = value;
    varbind->value_len = sizeof(u32_t);

    return varbind;
}

struct snmp_varbind *varbind_new_type_integer(const struct snmp_obj_id *oid, 
s32 value)
{
    struct snmp_varbind *varbind;

    // Allocate space for varbind
    varbind = calloc(sizeof(*varbind));
    if (!varbind)
    {
        return NULL;
    }

    // Allocate space for varbind value
    varbind->value = malloc(sizeof(s32_t));
    if (!varbind->value)
    {
        free(varbind);
        return NULL;
    }

    varbind->oid = *oid;
    varbind->type = SNMP_ASN1_TYPE_INTEGER;
    *(s32_t *)varbind->value = value;
    varbind->value_len = sizeof(s32_t);

    return varbind;
}

struct snmp_varbind *varbind_new_type_string(const struct snmp_obj_id *oid, 
const char *s)
{
    struct snmp_varbind *varbind;
    int len = strlen(s);

    // Allocate space for varbind
    varbind = calloc(sizeof(*varbind));
    if (!varbind)
    {
        return NULL;
    }

    // Allocate space for varbind value
    varbind->value = malloc(len);
    if (!varbind->value)
    {
        free(varbind);
        return NULL;
    }

    varbind->oid = *oid;
    varbind->type = SNMP_ASN1_TYPE_OCTET_STRING;
    memcpy(varbind->value, s, len);
    varbind->value_len = len;

    return varbind;
}

struct snmp_varbind *varbind_new_from_id(const struct snmp_obj_id *oid)
{
    struct snmp_varbind *varbind;
    struct snmp_node_instance node_instance;

    // Allocate space for varbind
    varbind = calloc(sizeof(*varbind));
    if (!varbind)
    {
        return NULL;
    }

    // Get oid mode
    if (snmp_get_node_instance_from_oid(oid->id, oid->len, &node_instance))
    {
        free(varbind);
        return NULL;
    }

    // Allocate space for varbind value
    varbind->value = malloc(SNMP_MAX_VALUE_SIZE);
    if (!varbind->value)
    {
        free(varbind);
        return NULL;
    }

    // Set varbind values
    varbind->oid = *oid;
    varbind->type = node_instance.asn1_type;
    varbind->value_len = node_instance.get_value(&node_instance, 
varbind->value);

    return varbind;
}

static void varbind_free(struct snmp_varbind *varbind)
{
    if (varbind->value)
    {
        free(varbind->value);
    }

    free(varbind);
}

void varbind_list_insert(struct snmp_varbind **varbind_list, struct 
snmp_varbind *varbind)
{
    if (!varbind)
    {
        // Nothing to add
        return;
    }

    if (*varbind_list)
    {
        // List not empty
        (*varbind_list)->prev = varbind;
    }

    // Insert to list
    varbind->next = *varbind_list;
    *varbind_list = varbind;
}

void varbind_list_free(struct snmp_varbind *varbind_list)
{
    while (varbind_list)
    {
        struct snmp_varbind *next = varbind_list->next;
        varbind_free(varbind_list);
        varbind_list = next;
    }
}
_______________________________________________
lwip-users mailing list
lwip-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/lwip-users

Reply via email to