Fix CMake to reference unit test in correct dir
[ric-app/mc.git] / sidecars / listener / src / rdc_extract.c
1
2 /*
3         Mnemonic:       rdc_extract.c
4         Abstract:       Read a raw data capture file from the mc-listener
5                                 and tease out one message type.
6
7                                         0000000 40 52 44 43                             << delim
8                                                                                 30 30 31 30 30 35 30 2a  << mtype
9                                                                                                                                 30 30 30 30 << msg len
10                                         0000020 30 37 34 00 
11                                                                                 40 4d 43 4c 30 30 30 30 30 34 36 00 << raw message
12                                                         :
13                                                         :
14
15                                 This is a very quick and dirty thing, so it might be flakey.
16
17                                 Parms from command line are file to read, and the msg type to extract.
18                                 If mtype given is 0, then message type of each record is written to
19                                 stdout (can be sorted -u for a list of messages in the file).
20
21                                 For capture mode, a file MT_<mtype> is created for the extracted
22                                 records.
23
24         Date:           11 October 2019
25         Author:         E. Scott Daniels
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <string.h>
34
35 int main( int argc, char** argv ) {
36         char rbuf[1024 * 5];
37         int fd;
38         int wfd = 1;            // write file des; default to stdout
39         int mtype;
40         int mlen;
41         char*   nxt;
42         int     remain = 0;
43         int     need = 20;
44         int desired;
45         int captured = 0;
46         int     wlen = 0;
47
48         if( argc < 3 ) {
49                 fprintf( stderr, "bad args.\nUsage: %s file mtype [output-file]\n", argv[0] );
50                 exit( 1 ) ;
51         }
52
53         fd = open( argv[1], O_RDONLY );
54         if( fd < 0 ) {
55                 fprintf( stderr, "bad open: %s\n", strerror(errno) );
56                 exit( 1 );
57         }
58
59         desired = atoi( argv[2] );
60
61         if( argc > 3 ) {
62                 wfd = open( argv[4], O_WRONLY | O_CREAT | O_TRUNC, 0644 );
63         } 
64
65         remain = read( fd, rbuf, sizeof( rbuf ) );
66         nxt = rbuf;
67         while( remain > 0 ) {
68                 if( remain < 20 ) {                                     // not enough stuff
69                         memcpy( rbuf, nxt, remain );    // copy remaining up front
70                         nxt = rbuf;
71                         remain += read( fd, nxt + remain, sizeof( rbuf ) - remain );
72                 }
73
74                 if( remain < 20 ) {                                     // truncated or a record > rbuf
75                         fprintf( stderr, "abort: @header check, truncated file, or record > read buffer size\n" );
76                         exit( 1 );
77                 }
78         
79                 if( strncmp( nxt, "@RDC", 4 ) == 0 ) {
80                         mtype = atoi( nxt+4 );
81                         mlen = atoi( nxt+12 );
82                         nxt += 20;
83                         remain -= 20;
84
85                         if( remain < mlen ) {                           // not enough stuff
86                                 memcpy( rbuf, nxt, remain );    // copy remaining up front
87                                 nxt = rbuf;
88                                 remain += read( fd, nxt + remain, sizeof( rbuf ) - remain );
89                         }
90
91                         if( remain < mlen ) {           // truncated or a record > rbuf
92                                 fprintf( stderr, "abort: truncated file, or record > read buffer size\n" );
93                                 exit( 1 );
94                         }
95                         
96                         if( desired == 0 ) {                            // just dump mtypes
97                                 captured++;
98                                 fprintf( stdout, "%d\n", mtype );
99                         } else {
100
101                                 if( mtype == desired ) {
102                                         wlen += mlen;
103                                         write( wfd, nxt, mlen );
104                                         captured++;
105                                 }
106                         }
107
108                         nxt += mlen;
109                         remain -= mlen;
110                 } else {
111                         fprintf( stderr, "didn't find rdc header!?! @ %ld\n", (long) (nxt - rbuf) );
112                         exit( 1 );
113                 }
114         }
115
116         fprintf( stderr, "done, captured %d messages (%d bytes)\n", captured, wlen );
117         close( fd );
118 }
119