ASN.1 multiple records with multiple record definitions

Papa Tana papa.tana101@REDACTED
Tue Jun 30 09:13:27 CEST 2020


Hello,

One recommended me to add a field so that I can identify the type of
Record what I am storing.

Then, I changed my People.asn definition as below:

People DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

  RecordType ::= CHOICE {
	person	Person,
	contact	Contact
  }

  Person ::= SEQUENCE {
    name PrintableString,
    location INTEGER {home(0),field(1),roving(2)},
    age INTEGER OPTIONAL
  }

	Contact ::= SEQUENCE {
		name VisibleString,
		phone NumericString
	}

END

And I can add 4 records:
-> Person
->Contact
->Contact
->Person

{ok, Bin1} = 'People':encode('RecordType', {'person', {'Person', "John
Smith", 5,38}} ).
{ok, Bin2} = 'People':encode('RecordType', {'contact', {'Contact',
"Vanessa", "123 456"}} ).
{ok, Bin3} = 'People':encode('RecordType', {'contact', {'Contact',
"Alicia", "987 654"}} ).
{ok, Bin4} = 'People':encode('RecordType', {'person', {'Person',
"Mercator", 0,32}} ).

file:write(F,Bin1).
file:write(F,Bin2).
file:write(F,Bin3).
file:write(F,Bin4).

But when I'm reading the file, and decode, it only returns the first
Person, sounds like the 3 upcoming records are ignored:

{ok, F} = file:read_file("CDR-people.dat").
{ok, Txt} = file:read_file("CDR-people.dat").
'People':decode('RecordType', Txt).

>>> {ok,{person,{'Person',"John Smith",5,38}}}


I'm struggling, I really don't figure out where am I doing it wrong,
can anyone help?

Kind Regards,

2020-06-29 15:22 UTC+03:00, Papa Tana <papa.tana101@REDACTED>:
> Hello World!
>
> I came across this issue as well
> (http://erlang.org/pipermail/erlang-questions/2014-February/077119.html)
> and thanks to this, I am now able to decode multiple records using the
> same definition.
>
> Now, the problem is as below:
>
> My People.asn file is:
>
> People DEFINITIONS AUTOMATIC TAGS ::=
> BEGIN
>   Person ::= SEQUENCE {
>     name PrintableString,
>     location INTEGER {home(0),field(1),roving(2)},
>     age INTEGER OPTIONAL
>   }
>   Contact ::= SEQUENCE {
>     name VisibleString,
>     phone NumericString
>   }
> END
>
> Compilation is ok:
>
>> asn1ct:compile("People", [ber]).
>
> Let's check:
>
>> rr('People').
>
> Now, I am adding a 'Person' record, then a 'Contact', a 'Contact'
> again, and a 'Person' as next, and so on "randomly":
>
>> {ok, F} = file:open("CDR-people.dat", [write]).
>> {ok, Bin1} = 'People':encode('Person', {'Person', "John Smith", 2, 32}).
>> {ok, Bin2} = 'People':encode('Contact', {'Contact', "Alicia", "261
>> 5687454121"}).
>> {ok, Bin3} = 'People':encode('Contact', {'Contact', "Alexa", "974
>> 3487454121"}).
>> {ok, Bin4} = 'People':encode('Person', {'Person', "Rob Dummy", 2, 38}).
>> {ok, Bin5} = 'People':encode('Person', {'Person', "Anderson", 2, 34}).
>> file:write(F,Bin1).
>> file:write(F,Bin2).
>> file:write(F,Bin3).
>> file:write(F,Bin4).
>> file:write(F,Bin5).
>> file:close(F).
>
> As expected, when I want to decode this simple binary file as below:
>
> {ok, F} = file:read_file("CDR-people.dat").
>
>
> {ok, A, B}  = 'People':decode('Person', F).
> {ok, C, D}  = 'People':decode('Person', B).
> {ok, E, G}  = 'People':decode('Contact', D).
>
> => It gives me an error, because the second record is a 'Contact', not
> a 'Person'.
>
> How can I resolve this please?
>
> The fact is that, I have some CDR file which contains several records
> inside them, and those records are added randomly by the peer-end, so
> I don't always know what would be the next content.
>
> Am I missing something?
>
>
> Thank you so much,
>
> Kind Regards,
>


More information about the erlang-questions mailing list