asn1cpp icon indicating copy to clipboard operation
asn1cpp copied to clipboard

How to use pushList with a sequence of choice?

Open carlosrisma opened this issue 3 years ago • 2 comments

Hi,

I'm making use of asn1cpp for a project and so far everything has been working perfectly. Recently, I have encountered an issue when trying to create a sequence of CHOICE that looks like this:

ISO14823Attributes::= SEQUENCE (SIZE(1..8,...)) OF CHOICE{
	dtm DTM, 	-- Date/Time/Period	
	edt	EDT,	-- Exemption status of Date/Time/Period	
	dfl	DFL,	-- Directional Flow of Lane	
	ved	VED, 	-- Vehicle Dimensions
	spe	SPE, 	-- Speed
	roi	ROI,	-- Rate of Incline
	dbv	DBV,	-- Distance Between Vehicles
	ddd	DDD 	-- Destination/Direction/Distance
	}

With this sequence the asn1c generated ISO14823Attributes.h defines the sequence as: A_SEQUENCE_OF(ISO14823Attributes__Member) list; and with the ISO14823Attributes__Member struct defined in the same .h file(I'm attaching the file for better understanding). The issue is that when trying to create a sequence of the member struct with asn1cpp::makeSeq(ISO14823Attributes__Member) I get an error because it doesn't have an asn type descriptor. On the other hand, a problem arises when trying to manually set the fields inside the member struct and then trying to do sequenceof::pushList() like this:

auto attr = asn1cpp::makeSeq(ISO14823Attributes);

ISO14823Attributes__Member attr_m;
attr_m.present = ISO14823Attributes__Member_PR_spe;
attr_m.choice.spe = spe_content;

asn1ccp::sequenceOf::pushList(*attr,attr_m);

The error that I get when trying to build says that there is no match for the call to (asn1cpp::Impl::Setter<ISO14823Attributes__Member, void>) (ISO14823Attributes__Member*&, const ISO14823Attributes__Member&).

In an attempt to solve the issue, I added a new macro based on pushList that avoids the use of asn1cpp::setterField() like it is done in asn1cpp::setOf::adderElement() that look like this:

 template <typename T, typename V>
        bool adderChoice(T & field, const V & value) {
            V *ptr = static_cast<V*>(calloc(1,sizeof(V)));

            if(!ptr) {
                return false;
            }

            *ptr=value;

            return ASN_SET_ADD(&field, ptr) == 0;
        } 

With this, I was able to successfully create the sequence but another error comes up when the destructor of the Seq<ISO14823Attributes> object is destroyed and the line def_->op->free_struct(def_, seq_, ASFM_FREE_EVERYTHING); in Seq.hpp, is called. As far as I understand this issue has to do with the fact that avoiding the use of asn1cpp::setterField() also means avoiding the use of the View object that takes care of some memory handling but I don't fully understand how it works.

I would like to ask if there is something I'm missing or if there is any other way to treat this kind of structure "SEQUENCE OF CHOICE". I also let you the .asn containing this structure in case it serves of any help.

Thank you very much in advance for your time and help.
files.zip

carlosrisma avatar Sep 21 '21 10:09 carlosrisma

Oh boy, it's been a while since I've given a look at ASN1 stuff. I am definitely a bit rusty on this.

As a first thing, I believe that the library as it currently is does not support CHOICE. I do not remember the details, but I think we had some problems figuring out a smooth interface for them that we would be sure would not leak. I'm not 100% sure of this, maybe it's instead something else we don't support, but I'm just saying that just in case.

I'm not sure what line are you referencing with ASFM_FREE_EVERYTHING; I cannot find it in the library nor in the generated ASN1 code (I did not download your zip, I'm just using your definition with the fields set as integers).

A View IIRC shouldn't handle memory at all, it is just a viewing wrapper that allows you to interact with certain fields without managing their memory. Seq is the class that generally handles ownership and destruction of resources (aside from some helper methods like clear and so on).

Svalorzen avatar Sep 21 '21 11:09 Svalorzen

Hey, did you manage to make any progress on this issue?

Svalorzen avatar Oct 13 '21 10:10 Svalorzen