OpenNSL API Guide and Reference Manual
rpc_utils.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright © 2016 Broadcom. The term "Broadcom" refers
4  * to Broadcom Limited and/or its subsidiaries.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  *
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ***************************************************************************/
19 
20 #ifndef INCLUDE_RPC_UTILS_H
21 #define INCLUDE_RPC_UTILS_H
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
28 #include <stdint.h>
29 #include <sys/socket.h>
30 
31 #define JSON_MAX_NODE_LENGTH 64
32 #define RPC_MAX_BUFFER_LEN 1023
33 #define JSON_MAX_NODE_LENGTH 64
34 
35 #define EXAMPLE_CONFIG_PORT 7891
36 #define DEF_SERVER "127.0.0.1"
37 
38 
39 #define _jsonlog(format,args...) printf(format, ##args)
40 
41 #define JSON_VALIDATE_POINTER(x,y,z) do { \
42  if ((x) == NULL) { \
43  _jsonlog("Invalid (NULL) value for parameter %s ", (y) ); \
44  return (z); \
45  } \
46 } while(0)
47 
48 #define JSON_VALIDATE_JSON_POINTER(x,y,z) do { \
49  if ((x) == NULL) { \
50  _jsonlog("Error parsing JSON %s ", (y) ); \
51  return (z); \
52  } \
53 }while(0)
54 
55 #define JSON_VALIDATE_POINTER_AND_CLEANUP(x,y,z) do { \
56  if ((x) == NULL) { \
57  _jsonlog("Invalid (NULL) value for parameter %s ", (y) ); \
58  if (root != NULL) { \
59  cJSON_Delete(root); \
60  } \
61  return (z); \
62  } \
63 } while(0)
64 
65 #define JSON_VALIDATE_JSON_POINTER_AND_CLEANUP(x,y,z) do { \
66  if ((x) == NULL) { \
67  _jsonlog("Error parsing JSON %s ", (y) ); \
68  if (root != NULL) { \
69  cJSON_Delete(root); \
70  } \
71  return (z); \
72  } \
73 }while(0)
74 
75 
76 #define JSON_VALIDATE_JSON_AS_STRING(x,y,z) do { \
77  if ((x)->type != cJSON_String) { \
78  _jsonlog("Error parsing JSON, %s not a string ", (y) ); \
79  if (root != NULL) { \
80  cJSON_Delete(root); \
81  } \
82  return (z); \
83  } \
84  if((x)->valuestring == NULL) { \
85  _jsonlog("Error parsing JSON, %s not a valid string ", (y) ); \
86  if (root != NULL) { \
87  cJSON_Delete(root); \
88  } \
89  return (z); \
90  } \
91 }while(0)
92 
93 #define JSON_VALIDATE_JSON_AS_NUMBER(x,y) do { \
94  if ((x)->type != cJSON_Number) { \
95  _jsonlog("Error parsing JSON, %s not a integer ", (y) ); \
96  if (root != NULL) { \
97  cJSON_Delete(root); \
98  } \
99  return (-1); \
100  } \
101 }while(0)
102 
103 #define JSON_COMPARE_STRINGS_AND_CLEANUP(x,y,z) do { \
104  if (strcmp((y), (z)) != 0) { \
105  _jsonlog("The JSON contains invalid value for %s (actual %s, required %s) ", (x), (y), (z) ); \
106  if (root != NULL) { \
107  cJSON_Delete(root); \
108  } \
109  return (-1); \
110  } \
111 }while(0)
112 
113 #define JSON_COMPARE_VALUE_AND_CLEANUP(x,y,z) do { \
114  if ((y) != (z)) { \
115  _jsonlog("The JSON contains invalid value for %s (actual %d, required %d) ", (x), (y), (z) ); \
116  if (root != NULL) { \
117  cJSON_Delete(root); \
118  } \
119  return (-1); \
120  } \
121 }while(0)
122 
123 
124 #define JSON_CHECK_VALUE_AND_CLEANUP(x,y,z) do { \
125  if ( ((x) < (y)) || ( (x) > (z)) ) { \
126  _jsonlog("The JSON number out of range %d (min %d, max %d) ", (x), (y), (z) ); \
127  if (root != NULL) { \
128  cJSON_Delete(root); \
129  } \
130  return (-1); \
131  } \
132 }while(0)
133 
134 
135 
136 
137 #ifdef __cplusplus
138 }
139 /* __doxy_func_body_end__ */
140 #endif
141 
142 #endif /* !INCLUDE_RPC_UTILS_H */
143