Revision 887df302 utils.h

View differences:

utils.h
15 15
#include <stdint.h>
16 16
#include <glib.h>
17 17
#include <netinet/in.h>
18
#include <jansson.h>
19

  
20
/* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
21
#define JANUS_JSON_BOOL JSON_TRUE
22
#define JANUS_JSON_PARAM_REQUIRED 1
23
#define JANUS_JSON_PARAM_POSITIVE 2
24
#define JANUS_JSON_PARAM_NONEMPTY 4
25

  
26
struct janus_json_parameter {
27
	const gchar *name;
28
	json_type jtype;
29
	unsigned int flags;
30
};
18 31

  
19 32
/*! \brief Helper to retrieve the system monotonic time, as Glib's
20 33
 * g_get_monotonic_time may not be available (only since 2.28)
......
139 152
/*! \brief Unlock and remove a previously created PID file
140 153
 * @returns 0 if successful, a negative integer otherwise */
141 154
int janus_pidfile_remove(void);
155

  
156
/*! \brief Creates a string describing the JSON type and constraint
157
 * @param jtype The JSON type, e.g., JSON_STRING
158
 * @param flags Indicates constraints for the described type
159
 * @param[out] The type description, e.g., "a positive integer"; required size is 19 characters
160
 * @returns 0 if successful, a negative integer otherwise */
161
void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
162

  
163
/*! \brief Checks whether the JSON value matches the type and constraint
164
 * @param val The JSON value to be checked
165
 * @param jtype The JSON type, e.g., JSON_STRING
166
 * @param flags Indicates constraints for the described type
167
 * @returns TRUE if the value is valid */
168
gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
169

  
170
/*! \brief Validates the JSON object against the description of its parameters
171
 * @param missing_format printf format to indicate a missing required parameter; needs one %s for the parameter name
172
 * @param invalid_format printf format to indicate an invalid parameter; needs two %s for parameter name and type description from janus_get_json_type_name
173
 * @param obj The JSON object to be validated
174
 * @param params Array of struct janus_json_parameter to describe the parameters; the array has to be a global or stack variable to make sizeof work
175
 * @param[out] error_code int to return error code
176
 * @param[out] error_cause Array of char or NULL to return the error descriptions; the array has to be a global or stack variable to make sizeof work; the required size is the length of the format string plus the length of the longest parameter name plus 19 for the type description
177
 * @param log_error If TRUE, log any error with JANUS_LOG(LOG_ERR)
178
 * @param missing_code The code to be returned in error_code if a parameter is missing
179
 * @param invalid_code The code to be returned in error_code if a parameter is invalid */
180
#define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
181
	do { \
182
		error_code = 0; \
183
		unsigned int i; \
184
		for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
185
			json_t *val = json_object_get(obj, params[i].name); \
186
			if(!val) { \
187
				if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) {	\
188
					error_code = (missing_code); \
189
					if(log_error) \
190
						JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
191
					if(error_cause != NULL) \
192
						g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
193
					break; \
194
				} \
195
				continue; \
196
			} \
197
			if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
198
				error_code = (invalid_code); \
199
				char type_name[20]; \
200
				janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
201
				if(log_error) \
202
					JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
203
				if(error_cause != NULL) \
204
					g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
205
				break; \
206
			} \
207
		} \
208
	} while(0)
209

  
210
/*! \brief Validates the JSON object against the description of its parameters
211
 * @param obj The JSON object to be validated
212
 * @param params Array of struct janus_json_parameter to describe the parameters; the array has to be a global or stack variable to make sizeof work
213
 * @param[out] error_code int to return error code
214
 * @param[out] error_cause Array of char or NULL to return the error descriptions; the array has to be a global or stack variable to make sizeof work; the required size is the length of the longest parameter name plus 54 for the format string and type description
215
 * @param log_error If TRUE, log any error with JANUS_LOG(LOG_ERR)
216
 * @param missing_code The code to be returned in error_code if a parameter is missing
217
 * @param invalid_code The code to be returned in error_code if a parameter is invalid */
218
#define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
219
	JANUS_VALIDATE_JSON_OBJECT_FORMAT("Missing mandatory element (%s)", "Invalid element type (%s should be %s)", obj, params, error_code, error_cause, log_error, missing_code, invalid_code)
220

  
142 221
#endif

Also available in: Unified diff