X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fcommon%2Fsrc%2Fmbuf_api.c;h=ef0c28f90c079faebb63bbbb2858f025e7f14154;hb=d710957ed5d73bf2da2ceea3f5a1a3c509275c30;hp=930d74d5059608c5ec480bd91a247e1a45e3ec8b;hpb=9e0c1e70ff256eb05ad29d2fa8252b6219cb3659;p=ric-plt%2Flib%2Frmr.git diff --git a/src/common/src/mbuf_api.c b/src/common/src/mbuf_api.c index 930d74d..ef0c28f 100644 --- a/src/common/src/mbuf_api.c +++ b/src/common/src/mbuf_api.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "rmr.h" // things the users see #include "rmr_agnostic.h" // agnostic things (must be included before private) @@ -215,3 +216,103 @@ extern unsigned char* rmr_get_meid( rmr_mbuf_t* mbuf, unsigned char* dest ) { return dest; } + +// ------------------- trace related access functions -------------------------------------- +/* + The set_trace function will copy the supplied data for size bytes into the + header. If the header trace area is not large enough, a new one will be allocated + which will cause a payload copy based on the msg->len value (if 0 no payload + data is copied). + + The return value is the number of bytes actually coppied. If 0 bytes are coppied + errno should indicate the reason. If 0 is returned and errno is 0, then size + passed was 0. The state in the message is left UNCHANGED. +*/ +extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size ) { + uta_mhdr_t* hdr; + rmr_mbuf_t* nm; // new message if larger is needed + int len; + void* old_tp_buf; // if we need to realloc, must hold old to free + void* old_hdr; + + if( msg == NULL ) { + errno = EINVAL; + return 0; + } + + errno = 0; + if( size <= 0 ) { + return 0; + } + + hdr = (uta_mhdr_t *) msg->header; + len = RMR_TR_LEN( hdr ); + + if( len != size ) { // different sized trace data, must realloc the buffer + nm = rmr_realloc_msg( msg, size ); // realloc with changed trace size + old_tp_buf = msg->tp_buf; + old_hdr = msg->header; + + msg->tp_buf = nm->tp_buf; // reference the reallocated buffer + msg->header = nm->header; + msg->id = NULL; // currently unused + msg->xaction = nm->xaction; + msg->payload = nm->payload; + + nm->tp_buf = old_tp_buf; // set to free + nm->header = old_hdr; // nano frees on hdr, so must set both + rmr_free_msg( nm ); + + hdr = (uta_mhdr_t *) msg->header; // header WILL be different + len = RMR_TR_LEN( hdr ); + } + + memcpy( TRACE_ADDR( hdr ), data, size ); + + return size; +} + + +/* + Copies the trace bytes from the message header into the buffer provided by + the user. If the trace data in the header is less than size, then only + that number of bytes are copied, else exactly size bytes are copied. The + number actually copied is returned. +*/ +extern int rmr_get_trace( rmr_mbuf_t* msg, unsigned char* dest, int size ) { + uta_mhdr_t* hdr = NULL; + int n2copy = 0; + + if( msg == NULL ) { + return 0; + } + + if( size <= 0 || dest == NULL ) { + return 0; + } + + hdr = msg->header; + if( (n2copy = size < RMR_TR_LEN( hdr ) ? size : RMR_TR_LEN( hdr )) <= 0 ) { + return 0; + } + + memcpy( dest, TRACE_ADDR( hdr ), n2copy ); + + return n2copy; +} + +/* + Returns the number of bytes currently allocated for trace data in the message + buffer. +*/ +extern int rmr_get_trlen( rmr_mbuf_t* msg ) { + uta_mhdr_t* hdr; + + if( msg == NULL ) { + return 0; + } + + hdr = msg->header; + + return RMR_TR_LEN( hdr ); +}