This sample is provided as part of the OSS ASN.1 Tools trial and commercial shipments. The source code and related files are listed below for reference.
The sample demonstrates how to use the OSS ASN.1 Tools to process messages for the 3GPP 5G RRC standard (TS 38.331 version 19.0.0).
It runs on Linux on x86-64 as an example and illustrates how to decode, inspect, and create PER Unaligned 5G RRC messages using the OSS ASN.1 Tools API.
The sample reads RRC messages from .uper files, decodes and prints them, accesses message components, and creates and encodes a response message.
A makefile is included for building and running the test program using the OSS ASN.1/C++ runtime libraries.
To explore more samples (LTE RRC, 5G RRC, S1AP, X2AP, NGAP), visit the main Sample Code page.
This sample shows how to:
The files listed below are included in the OSS ASN.1 Tools trial and commercial shipments and are used by this sample program:
| File | Description |
|---|---|
| README.TXT | Instructions and sample overview. |
| rrc.asn | ASN.1 source file that describes the 3GPP 5G RRC protocol (TS 38.331), used with this program example. |
| RRCRelease.uper | Valid RRCRelease message encoded with PER Unaligned. |
| SecurityModeCommand.uper | Valid SecurityModeCommand message encoded with PER Unaligned. |
| trrc.cpp | Simple C++ program that shows how to work with 5G RRC protocol data. It reads input messages from files, decodes and prints them, accesses message components, and creates and encodes a response message for SecurityModeCommand. |
| makefile | Makefile that builds and runs the sample test. |
| makefile.rtoed | Makefile that builds and runs the sample test using RTOED. |
| gui/ | ASN.1 Studio project for viewing/compiling schema and generating sample data. |
(Installation instructions for the OSS ASN.1 Tools are included in all trial and commercial shipments.)
To build and run this sample, install a trial or licensed version of the OSS ASN.1 Tools for C++.
If your shipment is runtime-only, generate the .cpp and .h files by running:
make cross
This creates a samples/cross directory with:
make
This command compiles the sample C++ source, links it with the OSS ASN.1/C++ static library, and executes the test. To use another type of library in the shipment (such as a shared library), set the A makefile variable, for example:
make A=so
make clean
Note: The C++ source code in this sample is platform-independent. Linux commands are shown as an example, but equivalent samples and build instructions are available for Windows, macOS, and other platforms. For help with platform-specific instructions, please contact OSS Support.
The following listing shows the main C++ source file for this sample test program, trrc.cpp. It demonstrates how to read PER-encoded 5G RRC messages from files, decode and print them, and create and encode a response message using the OSS ASN.1 Tools API.
/*****************************************************************************/
/* Copyright (C) 2025 OSS Nokalva, Inc. All rights reserved. */
/*****************************************************************************/
/* THIS FILE IS PROPRIETARY MATERIAL OF OSS NOKALVA, INC. */
/* AND MAY BE USED ONLY BY DIRECT LICENSEES OF OSS NOKALVA, INC. */
/* THIS FILE MAY NOT BE DISTRIBUTED. */
/* THIS COPYRIGHT STATEMENT MAY NOT BE REMOVED. */
/* THIS SAMPLE PROGRAM IS PROVIDED AS IS. THE SAMPLE PROGRAM AND ANY RESULTS */
/* OBTAINED FROM IT ARE PROVIDED WITHOUT ANY WARRANTIES OR REPRESENTATIONS, */
/* EXPRESS, IMPLIED OR STATUTORY. */
/*****************************************************************************/
/*
* $Id: 5G-rrc-cpp.html 3327 2026-01-06 10:07:48Z macra $
*/
/*
* A demonstrative example for handling 3GPP 5G RRC protocol data in ASN.1/C++
*/
#include <errno.h>
#include "rrc.h"
/*
* Auxiliary function to make output prettier.
*/
void printBorder()
{
printf("-------------------------------------------------------\n");
}
char *filesDir; /* The directory where input files reside. It is set to the
* command line parameter if any and to the directory where
* the sample is started, otherwise.
*/
/*
* A simple class used to report non-ASN.1/C++ errors (such as a failure to
* create a socket) to the application. You may use any alternative error
* handling if you wish.
*/
class NonASN1Exception {
private:
const char *message; /* can contain C format specifications */
const char *extra_data; /* data handled by fmt. specifications (if any) */
int errcode;
public:
NonASN1Exception(const char *msg, int code = 0,
const char *extd = NULL);
NonASN1Exception(const NonASN1Exception & that);
const char *get_message() const;
const char *get_extra_data() const;
int get_errcode() const;
};
NonASN1Exception::NonASN1Exception(const char *msg, int code, const char *extd)
{
message = msg;
extra_data = extd;
errcode = code;
}
NonASN1Exception::NonASN1Exception(const NonASN1Exception & that)
{
message = that.message;
errcode = that.errcode;
extra_data = that.extra_data;
}
const char *NonASN1Exception::get_message() const
{
return message;
}
const char *NonASN1Exception::get_extra_data() const
{
return extra_data;
}
int NonASN1Exception::get_errcode() const
{
return errcode;
}
/*
* FUNCTION printHexString() prints data (octet string) as a sequence of
* hexadecimal digits 'XXXX...'H
*
* PARAMETERS
* ctl ASN.1/C++ control object
* value reference to input data (OssString object)
*/
void printHexString(OssControl *ctl, const OssString &value)
{
ctl->printHex(value.get_buffer(), value.length());
}
/*
* FUNCTION printHexBitString() prints data (bit string) as a sequence of
* hexadecimal digits 'XXXX...'H
*
* PARAMETERS
* ctl ASN.1/C++ control object
* value reference to input data (OssBitString object)
*/
void printHexBitString(OssControl *ctl, const OssBitString &value)
{
ctl->printHex((const char *)value.get_buffer(), (value.length() + 7) / 8);
}
/*
* FUNCTION Helper function to open file in the specified directory.
*
* PARAMETERS
* directoryName directory where fileName resides
* fileName file to open
*
* RETURNS pointer to file on success, NULL on failure
*/
FILE * openInputFile(char *directoryName, const char *fileName)
{
char *path;
FILE *fv;
const char kPathSeparator =
#ifdef _WIN32
'\\';
#else
'/';
#endif
if (directoryName) {
size_t dLength = strlen(directoryName);
if (NULL == (path = (char *)asn1Malloc(dLength + strlen(fileName) + 2))) {
return NULL;
}
memcpy(path, directoryName, dLength);
if (path[dLength - 1] != kPathSeparator)
path[dLength++] = kPathSeparator;
strcpy(path+dLength, fileName);
} else {
path = (char *) fileName;
}
if (!(fv = fopen(path, "rb"))) {
printf("Failed to open the '%s' file. Restart the sample program using the file "
"location as the input parameter.\n", path);
}
if (path != fileName)
asn1Free(path);
return fv;
}
/*
* FUNCTION readEncodingFromFile() reads serialized message from specified
* file
*
* PARAMETERS
* filename name of file containing serialized message
*
* RETURNS a pointer to a newly allocated EncodedBuffer object containing
* the encoding read from the file
*/
static EncodedBuffer *readEncodingFromFile(const char *filename)
{
long length;
FILE *in = NULL;
unsigned char *data = NULL;
EncodedBuffer *result = new EncodedBuffer();
try {
/* Open the file and determine its length */
in = openInputFile(filesDir, filename);
if (!in)
throw NonASN1Exception(strerror(errno), errno);
if (fseek(in, 0, SEEK_END))
throw NonASN1Exception(strerror(errno), errno);
length = ftell(in);
if (length < 0)
throw NonASN1Exception(strerror(errno), errno);
/*
* Allocate memory. We need to use asn1Malloc so we can later pass
* the ownership of the allocated memory to the EncodedBuffer object.
*/
data = (unsigned char *)asn1Malloc(length);
if (!data)
throw NonASN1Exception("No memory", OUT_MEMORY);
/* Read the file */
rewind(in);
if (length != (long)fread(data, 1, (size_t)length, in))
throw NonASN1Exception("Error reading the file", 0);
fclose(in);
/* Pass the read buffer to the EncodedBuffer */
result->grab_buffer(length, (char *)data);
return result;
} catch (...) {
if (in)
fclose(in);
if (data)
asn1Free(data);
delete result;
throw;
}
}
/*
* FUNCTION createSecurityModeCommandResponse() creates an RRC successful
* outcome message for a given SecurityModeCommand request.
*
* PARAMETERS
* ctl OSS control object
* sec_mode input message (request)
* response pointer to the variable that is to hold the resulting
* successful outcome message created by the function
*/
void createSecurityModeCommandResponse(OssControl *ctl,
SecurityModeCommand *sec_mode, UL_DCCH_Message **resp)
{
SecurityModeCommand::criticalExtensions & crit_ext =
sec_mode->get_criticalExtensions();
SecurityModeCommand::criticalExtensions::securityModeCommand *crit_ext_c1;
SecurityAlgorithmConfig::integrityProtAlgorithm ipalg;
SecurityAlgorithmConfig::cipheringAlgorithm calg;
UL_DCCH_Message *response;
if (!(crit_ext_c1 = crit_ext.get_securityModeCommand()))
throw NonASN1Exception("Unsupported SecurityModeCommand", -1, NULL);
// if (!(ies = crit_ext_c1->get_securityModeCommand_r8()))
// throw NonASN1Exception("Unsupported SecurityModeCommand", -1, NULL);
/* Getting integrity protection algorithm and ciphering algorithm */
SecurityAlgorithmConfig & sec_conf =
crit_ext_c1->get_securityConfigSMC().get_securityAlgorithmConfig();
/* Checking validity of the values */
if (sec_conf.get_integrityProtAlgorithm()) {
ipalg = *sec_conf.get_integrityProtAlgorithm();
if (ipalg != nia0 && ipalg != nia1 && ipalg != nia2 && ipalg != nia3)
throw NonASN1Exception("Unknown integrity protection", -1, NULL);
}
calg = sec_conf.get_cipheringAlgorithm();
if (calg != nea0 && calg != nea1 && calg != nea2 && calg != nea3)
throw NonASN1Exception("Unknown ciphering", -1, NULL);
try {
/* Allocate response message */
response = new UL_DCCH_Message();
UL_DCCH_MessageType content;
UL_DCCH_MessageType::c1 inner_content;
SecurityModeComplete sec_mode_comp;
/* Copy transaction identifier from the request */
sec_mode_comp.set_rrc_TransactionIdentifier(
sec_mode->get_rrc_TransactionIdentifier());
/*
* SecurityModeComplete_IEs has no mandatory components. Here
* we are creating an empty SecurityModeComplete_IEs object.
*/
sec_mode_comp.get_criticalExtensions().set_securityModeComplete(
SecurityModeComplete_IEs());
/* Construct a response message */
inner_content.set_securityModeComplete(sec_mode_comp);
content.set_c1(inner_content);
response->set_message(content);
*resp = response;
} catch (...) {
/* In case of any error, delete the response object */
delete response;
}
}
/*
* FUNCTION testDL_DCCH_Message() is used to test an RRC message received
* by the downlink DCCH channel. The input serialized
* pdu is deserialized and printed, then an outcome message
* is created, printed and encoded. This version processes
* only securityModeCommand messages and rrcConnectionRelease
* messages (only partially, just printing the input message).
* However, it can be easily extended to other message types.
*
* PARAMETERS
* ctl OSS control object
* filename name of file containing serialized message
*/
static void testDL_DCCH_Message(OssControl *ctl, const char *filename)
{
EncodedBuffer *encoded_request;
EncodedBuffer encoded_response;
DL_DCCH_Message_PDU req_pdu;
UL_DCCH_Message_PDU resp_pdu;
DL_DCCH_Message *request;
UL_DCCH_Message *response;
printf("======================================================"
"======================\n");
printf("Read encoding from file: %s\n\n", filename);
/* Read serialized message from file */
encoded_request = readEncodingFromFile(filename);
try {
DL_DCCH_MessageType::c1 *content;
RRCRelease *conn_rel;
SecurityModeCommand *sec_mode;
printf("Decoding the input downlink DCCH message\n");
printBorder();
/* Decode the input message */
req_pdu.decode(*ctl, *encoded_request);
/* Free memory allocated for encoded message */
delete encoded_request;
encoded_request = NULL;
request = req_pdu.get_data();
content = request->get_message().get_c1();
if (!content)
throw NonASN1Exception("Incorrect message", -1, NULL);
printf("PDU decoded\n");
printBorder();
/* Depending on the message type... */
if ((conn_rel = content->get_rrcRelease())) {
/* rrcConnectionRelease */
/* Print deserialized message */
req_pdu.print(*ctl);
} else if ((sec_mode = content->get_securityModeCommand())) {
/* securityModeCommand */
/* Print deserialized message */
req_pdu.print(*ctl);
printf("Creating response\n");
printBorder();
/* Create response */
createSecurityModeCommandResponse(ctl, sec_mode, &response);
printf("\nResponse created successfully\n");
/* Print the response message */
resp_pdu.set_data(*response);
resp_pdu.print(*ctl);
/* Serialize response */
printf("\nEncoding response\n");
printBorder();
resp_pdu.encode(*ctl, encoded_response);
printf("Encoded response (%lu bytes):\n",
encoded_response.get_length());
printBorder();
encoded_response.print_hex(*ctl);
printf("\n");
/* Free memory allocated for outcome message */
delete response;
} else {
/* Other message types */
printf("Unimplemented\n");
}
/* Free the decoded input message */
req_pdu.free_data(*ctl);
} catch (...) {
/* Cleanup in case of any error */
delete encoded_request;
throw;
}
}
/*
* Main application routine
*/
int main(int argc, char *argv[])
{
int retcode;
filesDir = argc > 1 ? argv[1] : (char *) ".";
try {
rrc_Control ctl;
/* Set the encoding rules and encoding/decoding flags */
ctl.setEncodingRules(OSS_PER_UNALIGNED);
ctl.setEncodingFlags(AUTOMATIC_ENCDEC | STRICT_PER_ENCODING_OF_DEFAULT_VALUES);
ctl.setDecodingFlags(AUTOMATIC_ENCDEC);
ctl.setDebugFlags(PRINT_ERROR_MESSAGES | PRINT_DECODING_DETAILS |
PRINT_ENCODING_DETAILS);
testDL_DCCH_Message(&ctl, "RRCRelease.uper");
testDL_DCCH_Message(&ctl, "SecurityModeCommand.uper");
retcode = 0;
} catch (ASN1RuntimeException &exc) {
retcode = exc.get_code();
printf("An error occurred: code = %d.\n", retcode);
} catch (NonASN1Exception &exc) {
printf("NonASN1Exception: ");
retcode = exc.get_errcode();
printf(exc.get_message(), exc.get_extra_data(), retcode);
printf("\n");
if (!retcode)
retcode = -1;
} catch (...) {
printf("An unexpected exception occurred.\n");
retcode = -1;
}
if (!retcode)
printf("\nTesting successful.\n");
return retcode;
}
This is the expected output when running the sample:
Decoded RRCRelease: ... Decoded SecurityModeCommand: ... Encoding response message... Encoding successful.
-- Excerpt from rrc.asn 3GPP TS 38.331 V19.0.0 (2025-09)
-- TAG-NR-RRC-DEFINITIONS-START
NR-RRC-Definitions DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
-- TAG-NR-RRC-DEFINITIONS-STOP
-- TAG-BCCH-BCH-MESSAGE-START
BCCH-BCH-Message ::= SEQUENCE {
message BCCH-BCH-MessageType
}
BCCH-BCH-MessageType ::= CHOICE {
mib MIB,
messageClassExtension SEQUENCE {}
}
-- TAG-BCCH-BCH-MESSAGE-STOP
-- TAG-BCCH-DL-SCH-MESSAGE-START
BCCH-DL-SCH-Message ::= SEQUENCE {
message BCCH-DL-SCH-MessageType
}
BCCH-DL-SCH-MessageType ::= CHOICE {
c1 CHOICE {
systemInformation SystemInformation,
systemInformationBlockType1 SIB1
},
messageClassExtension SEQUENCE {}
}
-- TAG-BCCH-DL-SCH-MESSAGE-STOP
-- TAG-DL-CCCH-MESSAGE-START
DL-CCCH-Message ::= SEQUENCE {
message DL-CCCH-MessageType
}
DL-CCCH-MessageType ::= CHOICE {
c1 CHOICE {
rrcReject RRCReject,
rrcSetup RRCSetup,
spare2 NULL,
spare1 NULL
},
messageClassExtension SEQUENCE {}
}
-- TAG-DL-CCCH-MESSAGE-STOP
-- TAG-DL-DCCH-MESSAGE-START
DL-DCCH-Message ::= SEQUENCE {
message DL-DCCH-MessageType
}
DL-DCCH-MessageType ::= CHOICE {
c1 CHOICE {
rrcReconfiguration RRCReconfiguration,
rrcResume RRCResume,
rrcRelease RRCRelease,
rrcReestablishment RRCReestablishment,
securityModeCommand SecurityModeCommand,
dlInformationTransfer DLInformationTransfer,
ueCapabilityEnquiry UECapabilityEnquiry,
counterCheck CounterCheck,
mobilityFromNRCommand MobilityFromNRCommand,
dlDedicatedMessageSegment-r16 DLDedicatedMessageSegment-r16,
ueInformationRequest-r16 UEInformationRequest-r16,
dlInformationTransferMRDC-r16 DLInformationTransferMRDC-r16,
loggedMeasurementConfiguration-r16 LoggedMeasurementConfiguration-r16,
spare3 NULL, spare2 NULL, spare1 NULL
},
messageClassExtension SEQUENCE {}
}
-- TAG-DL-DCCH-MESSAGE-STOP
-- TAG-MCCH-MESSAGE-START
MCCH-Message-r17 ::= SEQUENCE {
message MCCH-MessageType-r17
}
MCCH-MessageType-r17 ::= CHOICE {
c1 CHOICE {
mbsBroadcastConfiguration-r17 MBSBroadcastConfiguration-r17,
spare1 NULL
},
messageClassExtension SEQUENCE {}
}
-- TAG-MCCH-MESSAGE-STOP
-- TAG-MULTICASTMCCH-MESSAGE-START
MulticastMCCH-Message-r18 ::= SEQUENCE {
message MulticastMCCH-MessageType-r18
}
MulticastMCCH-MessageType-r18 ::= CHOICE {
c1 CHOICE {
mbsMulticastConfiguration-r18 MBSMulticastConfiguration-r18,
spare1 NULL
},
messageClassExtension SEQUENCE {}
}
-- TAG-MULTICASTMCCH-MESSAGE-STOP
-- TAG-PCCH-PCH-MESSAGE-START
PCCH-Message ::= SEQUENCE {
message PCCH-MessageType
}
PCCH-MessageType ::= CHOICE {
c1 CHOICE {
paging Paging,
spare1 NULL
},
messageClassExtension SEQUENCE {}
}
-- TAG-PCCH-PCH-MESSAGE-STOP
-- TAG-UL-CCCH-MESSAGE-START
UL-CCCH-Message ::= SEQUENCE {
message UL-CCCH-MessageType
}
UL-CCCH-MessageType ::= CHOICE {
c1 CHOICE {
rrcSetupRequest RRCSetupRequest,
rrcResumeRequest RRCResumeRequest,
rrcReestablishmentRequest RRCReestablishmentRequest,
rrcSystemInfoRequest RRCSystemInfoRequest
},
messageClassExtension SEQUENCE {}
}
-- TAG-UL-CCCH-MESSAGE-STOP
-- TAG-UL-CCCH1-MESSAGE-START
UL-CCCH1-Message ::= SEQUENCE {
message UL-CCCH1-MessageType
}
UL-CCCH1-MessageType ::= CHOICE {
c1 CHOICE {
rrcResumeRequest1 RRCResumeRequest1,
spare3 NULL,
spare2 NULL,
spare1 NULL
},
messageClassExtension SEQUENCE {}
}
The gui/ subdirectory contains an ASN.1 Studio project for this sample. With ASN.1 Studio you can:
This sample is provided solely for illustration purposes, for example to demonstrate usage of the OSS ASN.1 Tools API with 3GPP 5G RRC messages. It does not represent a complete application. To build and run this sample, you must use a trial or licensed version of the appropriate OSS ASN.1 Tools. The copyright and license statements included in each source file remain fully applicable.
If you have questions about using this sample, contact OSS Nokalva Support.