-3
MyTest DEFINITIONS ::=
BEGIN
    Client ::= CHOICE {
    clientInt INTEGER,
    clientStr OCTET STRING,
    clientOID OBJECT IDENTIFIER
}

END

  Client_t *newClient;          //new client struct
  newClient = calloc(1, sizeof(Client_t));
  asn_enc_rval_t encode_rtn;
  printf("input integer: ");
  scanf("%d", &((*newClient).choice.clientInt));

  encode_rtn = der_encode_to_buffer(&asn_DEF_Client, newClient,
                       send_buffer, BUFFER_SIZE);
  if (encode_rtn.encoded == -1){
        printf("Error while encoding  %s: %s\n", encode_rtn.failed_type->name, strerror(errno));
        exit(1);
   }

Hi, there. This code can be compiled, but I always get "Client: Error 0" after I input an integer. What exactly is error 0? Thank you.

What i'm trying to do is encode the integer using BER to a serial sequence of bytes and store in the send buffer. I start using asn1 compiler today. The only doc I read is Using the Open Source ASN.1 Compiler, and it doesn't provide much information. I will appreciate if you can provide me useful information.

The documentation for the compiler is http://lionet.info/asn1c/documentation.html

LeonF
  • 423
  • 6
  • 14
  • 1
    Read the documentation for `der_encode_to_buffer` – M.M Mar 06 '15 at 00:46
  • Please update your question with links to the ASN.1 compiler and the documentation you refer to. There are multiple ASN.1 compilers available; we shouldn't have to spend time working out which one you are using. Since you don't show the declaration of `encode_rtn`, it isn't obvious what you are up to. It isn't clear how you are telling the `der_encode_to_buffer()` function that your data is in the `clientInt` element of the message. – Jonathan Leffler Mar 06 '15 at 02:20
  • This code does not print "Client: Error 0", and DER APIs are not system calls and therefore do not set `errno`. So printing it is futile, and asking what error 0 is ditto. – user207421 Mar 06 '15 at 02:35

1 Answers1

1

Didn't get exactly the same error as you. However, you're missing selecting the choice:

  Client_t *newClient;          //new client struct
  newClient = calloc(1, sizeof(Client_t));
  asn_enc_rval_t encode_rtn;
  printf("input integer: ");
  scanf("%d", &((*newClient).choice.clientInt));
  /* CHANGE: select clientInt on the CHOICE */
  (*newClient).present = Client_PR_clientInt;      

  encode_rtn = der_encode_to_buffer(&asn_DEF_Client, newClient,
                       send_buffer, BUFFER_SIZE);
  if (encode_rtn.encoded == -1){
        printf("Error while encoding  %s: %s\n", encode_rtn.failed_type->name, strerror(errno));
        exit(1);
  }

With that change I didn't get any error.

jsantander
  • 4,972
  • 16
  • 27