To decode a message, follow these steps:
The working buffer can be created by like this:
char bytes[] = { 0x30,0x2D,0x80,0x01,0xFF,0x81,0x01,0x00,0xA2,0x04,0x80,0x02,0x04,0xC0,0x83,0x0E,
0x77,0x77,0x77,0x2E,0x61,0x73,0x6E,0x6C,0x61,0x62,0x2E,0x63,0x6F,0x6D,0x84,0x0F,
0x32,0x30,0x31,0x30,0x30,0x32,0x30,0x32,0x31,0x34,0x30,0x32,0x30,0x32,0x5A };
/*
* Wraps a byte array into a buffer
*/
Buffer* buffer = wrapBuffer(bytes, N(bytes), BASIC_ENCODING_RULES);
Create the message data struct variable, do not fill the content, but initialize the whole memory with zeros:
struct GetRequest myRequest = { 0 };
/* * Do the decoding */ int result = decode_GetRequest(buffer, &myRequest);The return result of the decoding is explained in the same way as of encoding.
The example here just prints out the readable message:
/* * Process the result of the decoding: print out readable information */ print_GetRequest(&myRequest, printf);
The struct specific free routine must be called to free the variable's dynamically allocated memory. This routine doesn't free the variable's memory itself, because it may be allocated statically.
/* * Free the data struct variable's memory */ free_GetRequest(&myRequest);
If this working buffer is created by calling wrapBuffer(), calling asn_free() to free the buffer itself is enough:
/* * Free the working buffer */ asn_free(buffer);But if the working buffer is created by calling allocBuffer(), freeBuffer() should be called to free the buffer's content (byte array) and the buffer itself.
#include "GetRequest.h"
#include "stdio.h"
#define N(arr) sizeof(arr)/sizeof(arr[0])
int main(void) {
char bytes[] = { 0x30,0x2D,0x80,0x01,0xFF,0x81,0x01,0x00,0xA2,0x04,0x80,0x02,0x04,0xC0,0x83,0x0E,
0x77,0x77,0x77,0x2E,0x61,0x73,0x6E,0x6C,0x61,0x62,0x2E,0x63,0x6F,0x6D,0x84,0x0F,
0x32,0x30,0x31,0x30,0x30,0x32,0x30,0x32,0x31,0x34,0x30,0x32,0x30,0x32,0x5A };
/*
* Wraps a byte array into a buffer
*/
Buffer* buffer = wrapBuffer(bytes, N(bytes), BASIC_ENCODING_RULES);
if(buffer!=NULL) {
struct GetRequest myRequest = { 0 };
/*
* Do the decoding
*/
int result = decode_GetRequest(buffer, &myRequest);
if(result==0) {
/*
* Process the result of the decoding: print out readable information
*/
print_GetRequest(&myRequest, printf);
printf("\n");
}
else {
printf("Error Code: %d\n", result);
}
/*
* Free the data struct variable's memory
*/
free_GetRequest(&myRequest);
/*
* Free the working buffer
*/
asn_free(buffer);
}
return 0;
}