feature(routes): Add rtable update
[ric-plt/lib/rmr.git] / src / common / src / wrapper.c
1 // :vi sw=4 ts=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       wrapper.c
23         Abstract:       Functions that may make life easier for wrappers written on top
24                                 of the library which don't have access to header files.
25         Author:         E. Scott Daniels
26         Date:           3 May 2019
27 */
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "../include/rmr.h"
34
35 #define ADD_SEP         1
36 #define NO_SEP          0
37
38 /*
39         Build a "name": value  sequence with an optional trailing
40         seperator. Caller must free the buffer after use.
41 */
42 static char* build_ival( char* name, int val, int add_sep ) {
43         char    wbuf[512];
44
45         snprintf( wbuf, sizeof( wbuf ), "\"%s\": %d%s", name, val, add_sep ? "," : "" );
46         return strdup( wbuf );
47 }
48
49 /*
50         Builds a "name": "string" sequence with an optional trailing
51         separator. Caller must free resulting string.
52 */
53 static char* build_sval( char* name, char* val, int add_sep ) {
54         char    wbuf[512];
55
56         snprintf( wbuf, sizeof( wbuf ), "\"%s\": \"%s\"%s", name, val, add_sep ? "," : "" );
57         return strdup( wbuf );
58 }
59
60
61 /*
62         Similar to strcat, bangs src onto the end of target, but UNLIKE
63         strcat src is freed as a convenience. Max is the max amount
64         that target can accept; we don't bang on if src len is
65         larger than max.  Return is the size of src; 0 if the
66         target was not modified.  If target is not modified, then
67         src is NOT released.
68 */
69 static int bang_on( char* target, char* src, int max ) {
70         int             len;
71         int             rc = 0;         // return code, assume error
72
73         if( src && target ) {
74                 len = strlen( src );
75                 if( (rc = len <= max ? len : 0 ) > 0 ) {        // if it fits, add it.
76                         strcat( target, src );
77                         free( src );
78                 }
79         }
80
81         return rc;
82 }
83
84 /*
85         Returns a set of json with the constants which are set in the header.
86 */
87 extern char* rmr_get_consts( ) {
88         int             remain;                         // bytes remaining in wbuf
89         int             slen = 0;                       // length added
90         char    wbuf[2048];
91         char*   phrase;
92
93         snprintf( wbuf, sizeof( wbuf ), "{ " );
94         remain = sizeof( wbuf ) - strlen( wbuf ) -10;   // reserve some bytes allowing us to close the json
95
96         phrase = build_ival( "RMR_MAX_XID",       RMR_MAX_XID, ADD_SEP );
97         remain -= bang_on( wbuf, phrase, remain );
98         phrase = build_ival( "RMR_MAX_SID",       RMR_MAX_SID, ADD_SEP );
99         remain -= bang_on( wbuf, phrase, remain );
100         phrase = build_ival( "RMR_MAX_MEID",      RMR_MAX_MEID, ADD_SEP );
101         remain -= bang_on( wbuf, phrase, remain );
102         phrase = build_ival( "RMR_MAX_SRC",       RMR_MAX_SRC, ADD_SEP );
103         remain -= bang_on( wbuf, phrase, remain );
104         phrase = build_ival( "RMR_MAX_RCV_BYTES", RMR_MAX_RCV_BYTES, ADD_SEP );
105         remain -= bang_on( wbuf, phrase, remain );
106
107         phrase = build_ival( "RMRFL_NONE",        RMRFL_NONE, ADD_SEP );
108         remain -= bang_on( wbuf, phrase, remain );
109         phrase = build_ival( "RMRFL_AUTO_ALLOC",  RMRFL_AUTO_ALLOC, ADD_SEP );
110         remain -= bang_on( wbuf, phrase, remain );
111
112         phrase = build_ival( "RMR_DEF_SIZE",      RMR_DEF_SIZE, ADD_SEP );
113         remain -= bang_on( wbuf, phrase, remain );
114
115         phrase = build_ival( "RMR_VOID_MSGTYPE",  RMR_VOID_MSGTYPE, ADD_SEP );
116         remain -= bang_on( wbuf, phrase, remain );
117         phrase = build_ival( "RMR_VOID_SUBID",    RMR_VOID_SUBID , ADD_SEP );
118         remain -= bang_on( wbuf, phrase, remain );
119
120         phrase = build_ival( "RMR_OK",            RMR_OK, ADD_SEP );
121         remain -= bang_on( wbuf, phrase, remain );
122         phrase = build_ival( "RMR_ERR_BADARG",    RMR_ERR_BADARG, ADD_SEP );
123         remain -= bang_on( wbuf, phrase, remain );
124         phrase = build_ival( "RMR_ERR_NOENDPT",   RMR_ERR_NOENDPT, ADD_SEP );
125         remain -= bang_on( wbuf, phrase, remain );
126         phrase = build_ival( "RMR_ERR_EMPTY",     RMR_ERR_EMPTY, ADD_SEP );
127         remain -= bang_on( wbuf, phrase, remain );
128         phrase = build_ival( "RMR_ERR_NOHDR",     RMR_ERR_NOHDR, ADD_SEP );
129         remain -= bang_on( wbuf, phrase, remain );
130         phrase = build_ival( "RMR_ERR_SENDFAILED", RMR_ERR_SENDFAILED, ADD_SEP );
131         remain -= bang_on( wbuf, phrase, remain );
132         phrase = build_ival( "RMR_ERR_CALLFAILED", RMR_ERR_CALLFAILED, ADD_SEP );
133         remain -= bang_on( wbuf, phrase, remain );
134         phrase = build_ival( "RMR_ERR_NOWHOPEN",  RMR_ERR_NOWHOPEN, ADD_SEP );
135         remain -= bang_on( wbuf, phrase, remain );
136         phrase = build_ival( "RMR_ERR_WHID",      RMR_ERR_WHID, ADD_SEP );
137         remain -= bang_on( wbuf, phrase, remain );
138         phrase = build_ival( "RMR_ERR_OVERFLOW",  RMR_ERR_OVERFLOW, ADD_SEP );
139         remain -= bang_on( wbuf, phrase, remain );
140         phrase = build_ival( "RMR_ERR_RETRY",     RMR_ERR_RETRY, ADD_SEP );
141         remain -= bang_on( wbuf, phrase, remain );
142         phrase = build_ival( "RMR_ERR_RCVFAILED", RMR_ERR_RCVFAILED, ADD_SEP );
143         remain -= bang_on( wbuf, phrase, remain );
144         phrase = build_ival( "RMR_ERR_TIMEOUT",   RMR_ERR_TIMEOUT, ADD_SEP );
145         remain -= bang_on( wbuf, phrase, remain );
146         phrase = build_ival( "RMR_ERR_UNSET",     RMR_ERR_UNSET, ADD_SEP );
147         remain -= bang_on( wbuf, phrase, remain );
148         phrase = build_ival( "RMR_ERR_TRUNC",     RMR_ERR_TRUNC, ADD_SEP );
149         remain -= bang_on( wbuf, phrase, remain );
150         phrase = build_ival( "RMR_ERR_INITFAILED", RMR_ERR_INITFAILED, NO_SEP );
151         remain -= bang_on( wbuf, phrase, remain );
152
153         strcat( wbuf, " }" );
154         return strdup( wbuf );                  // chop unused space and return
155 }