3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
11 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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
33 #include "../include/rmr.h"
39 Build a "name": value sequence with an optional trailing
40 seperator. Caller must free the buffer after use.
42 static char* build_ival( char* name, int val, int add_sep ) {
45 snprintf( wbuf, sizeof( wbuf ), "\"%s\": %d%s", name, val, add_sep ? "," : "" );
46 return strdup( wbuf );
50 Builds a "name": "string" sequence with an optional trailing
51 separator. Caller must free resulting string.
53 static char* build_sval( char* name, char* val, int add_sep ) {
56 snprintf( wbuf, sizeof( wbuf ), "\"%s\": \"%s\"%s", name, val, add_sep ? "," : "" );
57 return strdup( wbuf );
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
69 static int bang_on( char* target, char* src, int max ) {
71 int rc = 0; // return code, assume error
75 if( (rc = len <= max ? len : 0 ) > 0 ) { // if it fits, add it.
76 strcat( target, src );
85 Returns a set of json with the constants which are set in the header.
87 extern char* rmr_get_consts( ) {
88 int remain; // bytes remaining in wbuf
89 int slen = 0; // length added
93 snprintf( wbuf, sizeof( wbuf ), "{ " );
94 remain = sizeof( wbuf ) - strlen( wbuf ) -10; // reserve some bytes allowing us to close the json
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 );
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 phrase = build_ival( "RMRFL_MTCALL", RMRFL_MTCALL, ADD_SEP );
112 remain -= bang_on( wbuf, phrase, remain );
114 phrase = build_ival( "RMR_DEF_SIZE", RMR_DEF_SIZE, ADD_SEP );
115 remain -= bang_on( wbuf, phrase, remain );
117 phrase = build_ival( "RMR_VOID_MSGTYPE", RMR_VOID_MSGTYPE, ADD_SEP );
118 remain -= bang_on( wbuf, phrase, remain );
119 phrase = build_ival( "RMR_VOID_SUBID", RMR_VOID_SUBID , ADD_SEP );
120 remain -= bang_on( wbuf, phrase, remain );
122 phrase = build_ival( "RMR_OK", RMR_OK, ADD_SEP );
123 remain -= bang_on( wbuf, phrase, remain );
124 phrase = build_ival( "RMR_ERR_BADARG", RMR_ERR_BADARG, ADD_SEP );
125 remain -= bang_on( wbuf, phrase, remain );
126 phrase = build_ival( "RMR_ERR_NOENDPT", RMR_ERR_NOENDPT, ADD_SEP );
127 remain -= bang_on( wbuf, phrase, remain );
128 phrase = build_ival( "RMR_ERR_EMPTY", RMR_ERR_EMPTY, ADD_SEP );
129 remain -= bang_on( wbuf, phrase, remain );
130 phrase = build_ival( "RMR_ERR_NOHDR", RMR_ERR_NOHDR, ADD_SEP );
131 remain -= bang_on( wbuf, phrase, remain );
132 phrase = build_ival( "RMR_ERR_SENDFAILED", RMR_ERR_SENDFAILED, ADD_SEP );
133 remain -= bang_on( wbuf, phrase, remain );
134 phrase = build_ival( "RMR_ERR_CALLFAILED", RMR_ERR_CALLFAILED, ADD_SEP );
135 remain -= bang_on( wbuf, phrase, remain );
136 phrase = build_ival( "RMR_ERR_NOWHOPEN", RMR_ERR_NOWHOPEN, ADD_SEP );
137 remain -= bang_on( wbuf, phrase, remain );
138 phrase = build_ival( "RMR_ERR_WHID", RMR_ERR_WHID, ADD_SEP );
139 remain -= bang_on( wbuf, phrase, remain );
140 phrase = build_ival( "RMR_ERR_OVERFLOW", RMR_ERR_OVERFLOW, ADD_SEP );
141 remain -= bang_on( wbuf, phrase, remain );
142 phrase = build_ival( "RMR_ERR_RETRY", RMR_ERR_RETRY, ADD_SEP );
143 remain -= bang_on( wbuf, phrase, remain );
144 phrase = build_ival( "RMR_ERR_RCVFAILED", RMR_ERR_RCVFAILED, ADD_SEP );
145 remain -= bang_on( wbuf, phrase, remain );
146 phrase = build_ival( "RMR_ERR_TIMEOUT", RMR_ERR_TIMEOUT, ADD_SEP );
147 remain -= bang_on( wbuf, phrase, remain );
148 phrase = build_ival( "RMR_ERR_UNSET", RMR_ERR_UNSET, ADD_SEP );
149 remain -= bang_on( wbuf, phrase, remain );
150 phrase = build_ival( "RMR_ERR_TRUNC", RMR_ERR_TRUNC, ADD_SEP );
151 remain -= bang_on( wbuf, phrase, remain );
152 phrase = build_ival( "RMR_ERR_INITFAILED", RMR_ERR_INITFAILED, NO_SEP );
153 remain -= bang_on( wbuf, phrase, remain );
155 strcat( wbuf, " }" );
156 return strdup( wbuf ); // chop unused space and return