Server custom data type variable mapping fails

50 views
Skip to first unread message

PorcineJim

unread,
Jan 15, 2024, 12:56:07 PMJan 15
to open62541
I'm trying to implement a structure data type. I've taken open62541/examples/custom_datatype/server_types_custom.c and stripped out all bu the "opt" type. Without further modification it compiles and runs fine. As soon as I modify the definition of the Opt structure, the program fails. Sometimes it segfaults, currently it compiles and runs but the memory mapping of the variables is wrong. The opt struct is defined as follows:
</code>
typedef struct {
    UA_Int16 aint;
    UA_Float bfloat;
    UA_Float cfloat;
} Opt;

static UA_DataTypeMember Opt_members[3] = {
    /* a */
    {
        UA_TYPENAME("aint")                                      /* .memberName */
        &UA_TYPES[UA_TYPES_INT16],                            /* .memberType */
        0,                                                    /* .padding */
        false,                                                /* .isArray */
        false                                                 /* .isOptional */
    },
    /* b */
    {
        UA_TYPENAME("bfloat")                                      /* .memberName */
        &UA_TYPES[UA_TYPES_FLOAT],                            /* .memberType */
        offsetof(Opt,bfloat) - offsetof(Opt,aint) - sizeof(UA_Int16), /* .padding */
        false,                                                /* .isArray */
        false                                                  /* .isOptional */
//       true                                                  /* .isOptional */
    },
    /* c */
    {
        UA_TYPENAME("cfloat")                                      /* .memberName */
        &UA_TYPES[UA_TYPES_FLOAT],                            /* .memberType */
        offsetof(Opt,cfloat) - offsetof(Opt,bfloat) - sizeof(UA_Float),   /* .padding */
        false,                                                /* .isArray */
        false                                                  /* .isOptional */
    }
};
</code>
Then the values are inserted later. 
<code>
static void
addOptStructVariable(UA_Server *server) {
  Opt o ;
    memset(&o, 0, sizeof(Opt));
    o.aint = 3;
    o.bfloat = 42.1;
    o.cfloat = (UA_Float) 120.10;

    UA_VariableAttributes vattr = UA_VariableAttributes_default;
    vattr.description = UA_LOCALIZEDTEXT("en-US", "Current Values");
    vattr.displayName = UA_LOCALIZEDTEXT("en-US", "Current Values");
    vattr.dataType = OptType.typeId;
    vattr.valueRank = UA_VALUERANK_SCALAR;
   
    //    Opt o;
    //    memset(&o, 0, sizeof(Opt));
    //    updateValues( &o ) ;
    UA_Variant_setScalar(&vattr.value, &o, &OptType);
   
    UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "Current.Value"),
                              UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
                              UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
                              UA_QUALIFIEDNAME(1, "Current Values"),
                              optstructVariableTypeId, vattr, NULL, NULL);
   UA_clear(&o, &OptType);
   //   updateValues(server) ;
}
When I examine the server with uaexpert, the three variable are 0, 2.75506e-40, and 42.1. So it looks to me as if the padding is not working correctly. I've attached my two files "opt_copy.c" and "opt_copy.h"
Any suggestions are welcome.

Regards, Jim
opt_copy.h
opt_copy.c

PorcineJim

unread,
Jan 15, 2024, 1:08:31 PMJan 15
to open62541
ARRG - I attached the wrong files!
opt_copy.h
opt_copy.c

Julius Pfrommer

unread,
Jan 27, 2024, 5:39:10 PMJan 27
to open62541
Probably the “type kind” should be “structure”.
And not structure with optional fields.
The optional fields are indicated in a 32bit bit field at the beginning of the binary encoding.

Regards 
Reply all
Reply to author
Forward
0 new messages