9d3dd72cc1a8b70864a026e684ed1ded8c9e6e73
[ric-plt/lib/rmr.git] / src / rmr / 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.
67
68         Source is ALWAYS freed!
69 */
70 static int bang_on( char* target, char* src, int max ) {
71         int             len;
72         int             rc = 0;         // return code, assume error
73
74         if( src && target ) {
75                 len = strlen( src );
76                 if( (rc = len <= max ? len : 0 ) > 0 ) {        // if it fits, add it.
77                         strncat( target, src, len );
78                 }
79         }
80
81         if( src ) {
82                 free( src );
83         }
84         return rc;
85 }
86
87 /*
88         Returns a set of json with the constants which are set in the header.
89 */
90 extern char* rmr_get_consts( ) {
91         int             remain;                         // bytes remaining in wbuf
92         int             slen = 0;                       // length added
93         char    wbuf[2048];
94         char*   phrase;
95
96         snprintf( wbuf, sizeof( wbuf ), "{ " );
97         remain = sizeof( wbuf ) - strlen( wbuf ) -10;   // reserve some bytes allowing us to close the json
98
99         phrase = build_ival( "RMR_MAX_XID",       RMR_MAX_XID, ADD_SEP );
100         remain -= bang_on( wbuf, phrase, remain );
101         phrase = build_ival( "RMR_MAX_SID",       RMR_MAX_SID, ADD_SEP );
102         remain -= bang_on( wbuf, phrase, remain );
103         phrase = build_ival( "RMR_MAX_MEID",      RMR_MAX_MEID, ADD_SEP );
104         remain -= bang_on( wbuf, phrase, remain );
105         phrase = build_ival( "RMR_MAX_SRC",       RMR_MAX_SRC, ADD_SEP );
106         remain -= bang_on( wbuf, phrase, remain );
107         phrase = build_ival( "RMR_MAX_RCV_BYTES", RMR_MAX_RCV_BYTES, ADD_SEP );
108         remain -= bang_on( wbuf, phrase, remain );
109
110         phrase = build_ival( "RMRFL_NONE",        RMRFL_NONE, ADD_SEP );
111         remain -= bang_on( wbuf, phrase, remain );
112         phrase = build_ival( "RMRFL_AUTO_ALLOC",  RMRFL_AUTO_ALLOC, ADD_SEP );
113         remain -= bang_on( wbuf, phrase, remain );
114         phrase = build_ival( "RMRFL_MTCALL",      RMRFL_MTCALL, ADD_SEP );
115         remain -= bang_on( wbuf, phrase, remain );
116
117         phrase = build_ival( "RMR_DEF_SIZE",      RMR_DEF_SIZE, ADD_SEP );
118         remain -= bang_on( wbuf, phrase, remain );
119
120         phrase = build_ival( "RMR_VOID_MSGTYPE",  RMR_VOID_MSGTYPE, ADD_SEP );
121         remain -= bang_on( wbuf, phrase, remain );
122         phrase = build_ival( "RMR_VOID_SUBID",    RMR_VOID_SUBID , ADD_SEP );
123         remain -= bang_on( wbuf, phrase, remain );
124
125         phrase = build_ival( "RMR_OK",            RMR_OK, ADD_SEP );
126         remain -= bang_on( wbuf, phrase, remain );
127         phrase = build_ival( "RMR_ERR_BADARG",    RMR_ERR_BADARG, ADD_SEP );
128         remain -= bang_on( wbuf, phrase, remain );
129         phrase = build_ival( "RMR_ERR_NOENDPT",   RMR_ERR_NOENDPT, ADD_SEP );
130         remain -= bang_on( wbuf, phrase, remain );
131         phrase = build_ival( "RMR_ERR_EMPTY",     RMR_ERR_EMPTY, ADD_SEP );
132         remain -= bang_on( wbuf, phrase, remain );
133         phrase = build_ival( "RMR_ERR_NOHDR",     RMR_ERR_NOHDR, ADD_SEP );
134         remain -= bang_on( wbuf, phrase, remain );
135         phrase = build_ival( "RMR_ERR_SENDFAILED", RMR_ERR_SENDFAILED, ADD_SEP );
136         remain -= bang_on( wbuf, phrase, remain );
137         phrase = build_ival( "RMR_ERR_CALLFAILED", RMR_ERR_CALLFAILED, ADD_SEP );
138         remain -= bang_on( wbuf, phrase, remain );
139         phrase = build_ival( "RMR_ERR_NOWHOPEN",  RMR_ERR_NOWHOPEN, ADD_SEP );
140         remain -= bang_on( wbuf, phrase, remain );
141         phrase = build_ival( "RMR_ERR_WHID",      RMR_ERR_WHID, ADD_SEP );
142         remain -= bang_on( wbuf, phrase, remain );
143         phrase = build_ival( "RMR_ERR_OVERFLOW",  RMR_ERR_OVERFLOW, ADD_SEP );
144         remain -= bang_on( wbuf, phrase, remain );
145         phrase = build_ival( "RMR_ERR_RETRY",     RMR_ERR_RETRY, ADD_SEP );
146         remain -= bang_on( wbuf, phrase, remain );
147         phrase = build_ival( "RMR_ERR_RCVFAILED", RMR_ERR_RCVFAILED, ADD_SEP );
148         remain -= bang_on( wbuf, phrase, remain );
149         phrase = build_ival( "RMR_ERR_TIMEOUT",   RMR_ERR_TIMEOUT, ADD_SEP );
150         remain -= bang_on( wbuf, phrase, remain );
151         phrase = build_ival( "RMR_ERR_UNSET",     RMR_ERR_UNSET, ADD_SEP );
152         remain -= bang_on( wbuf, phrase, remain );
153         phrase = build_ival( "RMR_ERR_TRUNC",     RMR_ERR_TRUNC, ADD_SEP );
154         remain -= bang_on( wbuf, phrase, remain );
155         phrase = build_ival( "RMR_ERR_INITFAILED", RMR_ERR_INITFAILED, NO_SEP );
156         remain -= bang_on( wbuf, phrase, remain );
157
158         strncat( wbuf, " }", remain );
159         return strdup( wbuf );                  // chop unused space and return
160 }