0neblock / Arduino_SNMP

SNMP Agent built with Arduino
MIT License
78 stars 30 forks source link

Pointer to structure for addReadWriteStringHandler results in SNMP no such object #41

Open dmarc1234 opened 1 year ago

dmarc1234 commented 1 year ago

I am trying to point the addReadWriteStringHandler() to a char array within a global structure to allow a GET and SET to be performed on it and I am having real problems getting to work.

I have tried creating a pointer to the structure and dereferencing the structure like this:

SNMPAgent * snmp; snmp = new SNMPAgent("public", "private");

CONFIG_REG *conf = new CONFIG_REG(); snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", (char*) (conf).sensor_name, 40, true);

But get a SNMP not such object reply in iReasoning MIB browser.

The snmp->addReadOnlyStaticStringHandler() works okay so I am at a loss as to why it will not work ?

dmarc1234 commented 1 year ago

That line did come out right above, should be:

snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", (char*)(conf).sensor_name, 40, true);

0neblock commented 1 year ago

Hi @dmarc1234. What is the type of (*conf).sensor_name? If it is a char*, then you'll need to pass the reference of it to addReadWriteStringHandler(). For example: snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", &(*conf).sensor_name, 40, true); You shouldn't need to cast it to a char* unless it's not a char originally.

Let me know if that helps!. Thanks.

dmarc1234 commented 1 year ago

Thank you for your reply.

Yes, (*conf).sensor_name is a char within the CONFIG_REG structure.

I have tried: snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", &(*conf).sensor_name, 40, true); but it will not compile, I need to cast to char to make it compile, so: `snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", (char)&(*conf).client_name, 40, true);` which compiles but still results in a SNMP no such object error.

Also tried a different approach as follows:

CONFIG_REG *config = &configReg;
snmp->addReadWriteStringHandler(".1.3.6.1.4.1.38218.1.3.1.0", (char**)&config->sensor_name, 40, true);

which returns a blank string rather than the no object error.

Really not sure where to go next ?

0neblock commented 1 year ago

@dmarc1234 Can you show me the declaration of the CONFIG_REG type? What data type is sensor_name?

dmarc1234 commented 1 year ago

This is how CONFIG_REG is defined:

// Structure to contain configuration information
typedef struct
{
    char        sensor_name[40];        // Sensor name
    char        sensor_location[40];    // Sensor location
} CONFIG_REG;

and then declared as a global in main.c : CONFIG_REG configReg;

I then create a pointer to it using: CONFIG_REG *config = &configReg;

dmarc1234 commented 1 year ago

@0neblock did you see my message in relation to the structure, still struggling to get this to work.