/src/open5gs/lib/sbi/openapi/model/mov_behav.c
Line | Count | Source |
1 | | |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <stdio.h> |
5 | | #include "mov_behav.h" |
6 | | |
7 | | OpenAPI_mov_behav_t *OpenAPI_mov_behav_create( |
8 | | char *ts_start, |
9 | | int ts_duration, |
10 | | bool is_num_of_ue, |
11 | | int num_of_ue, |
12 | | bool is_ratio, |
13 | | int ratio, |
14 | | bool is_avr_speed, |
15 | | float avr_speed, |
16 | | OpenAPI_list_t *speed_thresd_infos, |
17 | | OpenAPI_list_t *direction_ue_infos |
18 | | ) |
19 | 0 | { |
20 | 0 | OpenAPI_mov_behav_t *mov_behav_local_var = ogs_malloc(sizeof(OpenAPI_mov_behav_t)); |
21 | 0 | ogs_assert(mov_behav_local_var); |
22 | | |
23 | 0 | mov_behav_local_var->ts_start = ts_start; |
24 | 0 | mov_behav_local_var->ts_duration = ts_duration; |
25 | 0 | mov_behav_local_var->is_num_of_ue = is_num_of_ue; |
26 | 0 | mov_behav_local_var->num_of_ue = num_of_ue; |
27 | 0 | mov_behav_local_var->is_ratio = is_ratio; |
28 | 0 | mov_behav_local_var->ratio = ratio; |
29 | 0 | mov_behav_local_var->is_avr_speed = is_avr_speed; |
30 | 0 | mov_behav_local_var->avr_speed = avr_speed; |
31 | 0 | mov_behav_local_var->speed_thresd_infos = speed_thresd_infos; |
32 | 0 | mov_behav_local_var->direction_ue_infos = direction_ue_infos; |
33 | |
|
34 | 0 | return mov_behav_local_var; |
35 | 0 | } |
36 | | |
37 | | void OpenAPI_mov_behav_free(OpenAPI_mov_behav_t *mov_behav) |
38 | 0 | { |
39 | 0 | OpenAPI_lnode_t *node = NULL; |
40 | |
|
41 | 0 | if (NULL == mov_behav) { |
42 | 0 | return; |
43 | 0 | } |
44 | 0 | if (mov_behav->ts_start) { |
45 | 0 | ogs_free(mov_behav->ts_start); |
46 | 0 | mov_behav->ts_start = NULL; |
47 | 0 | } |
48 | 0 | if (mov_behav->speed_thresd_infos) { |
49 | 0 | OpenAPI_list_for_each(mov_behav->speed_thresd_infos, node) { |
50 | 0 | OpenAPI_speed_threshold_info_free(node->data); |
51 | 0 | } |
52 | 0 | OpenAPI_list_free(mov_behav->speed_thresd_infos); |
53 | 0 | mov_behav->speed_thresd_infos = NULL; |
54 | 0 | } |
55 | 0 | if (mov_behav->direction_ue_infos) { |
56 | 0 | OpenAPI_list_for_each(mov_behav->direction_ue_infos, node) { |
57 | 0 | OpenAPI_direction_info_free(node->data); |
58 | 0 | } |
59 | 0 | OpenAPI_list_free(mov_behav->direction_ue_infos); |
60 | 0 | mov_behav->direction_ue_infos = NULL; |
61 | 0 | } |
62 | 0 | ogs_free(mov_behav); |
63 | 0 | } |
64 | | |
65 | | cJSON *OpenAPI_mov_behav_convertToJSON(OpenAPI_mov_behav_t *mov_behav) |
66 | 0 | { |
67 | 0 | cJSON *item = NULL; |
68 | 0 | OpenAPI_lnode_t *node = NULL; |
69 | |
|
70 | 0 | if (mov_behav == NULL) { |
71 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [MovBehav]"); |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 0 | item = cJSON_CreateObject(); |
76 | 0 | if (!mov_behav->ts_start) { |
77 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [ts_start]"); |
78 | 0 | return NULL; |
79 | 0 | } |
80 | 0 | if (cJSON_AddStringToObject(item, "tsStart", mov_behav->ts_start) == NULL) { |
81 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [ts_start]"); |
82 | 0 | goto end; |
83 | 0 | } |
84 | | |
85 | 0 | if (cJSON_AddNumberToObject(item, "tsDuration", mov_behav->ts_duration) == NULL) { |
86 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [ts_duration]"); |
87 | 0 | goto end; |
88 | 0 | } |
89 | | |
90 | 0 | if (mov_behav->is_num_of_ue) { |
91 | 0 | if (cJSON_AddNumberToObject(item, "numOfUe", mov_behav->num_of_ue) == NULL) { |
92 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [num_of_ue]"); |
93 | 0 | goto end; |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | 0 | if (mov_behav->is_ratio) { |
98 | 0 | if (cJSON_AddNumberToObject(item, "ratio", mov_behav->ratio) == NULL) { |
99 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [ratio]"); |
100 | 0 | goto end; |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | 0 | if (mov_behav->is_avr_speed) { |
105 | 0 | if (cJSON_AddNumberToObject(item, "avrSpeed", mov_behav->avr_speed) == NULL) { |
106 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [avr_speed]"); |
107 | 0 | goto end; |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | 0 | if (mov_behav->speed_thresd_infos) { |
112 | 0 | cJSON *speed_thresd_infosList = cJSON_AddArrayToObject(item, "speedThresdInfos"); |
113 | 0 | if (speed_thresd_infosList == NULL) { |
114 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [speed_thresd_infos]"); |
115 | 0 | goto end; |
116 | 0 | } |
117 | 0 | OpenAPI_list_for_each(mov_behav->speed_thresd_infos, node) { |
118 | 0 | cJSON *itemLocal = OpenAPI_speed_threshold_info_convertToJSON(node->data); |
119 | 0 | if (itemLocal == NULL) { |
120 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [speed_thresd_infos]"); |
121 | 0 | goto end; |
122 | 0 | } |
123 | 0 | cJSON_AddItemToArray(speed_thresd_infosList, itemLocal); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | 0 | if (mov_behav->direction_ue_infos) { |
128 | 0 | cJSON *direction_ue_infosList = cJSON_AddArrayToObject(item, "directionUeInfos"); |
129 | 0 | if (direction_ue_infosList == NULL) { |
130 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [direction_ue_infos]"); |
131 | 0 | goto end; |
132 | 0 | } |
133 | 0 | OpenAPI_list_for_each(mov_behav->direction_ue_infos, node) { |
134 | 0 | cJSON *itemLocal = OpenAPI_direction_info_convertToJSON(node->data); |
135 | 0 | if (itemLocal == NULL) { |
136 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed [direction_ue_infos]"); |
137 | 0 | goto end; |
138 | 0 | } |
139 | 0 | cJSON_AddItemToArray(direction_ue_infosList, itemLocal); |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 0 | end: |
144 | 0 | return item; |
145 | 0 | } |
146 | | |
147 | | OpenAPI_mov_behav_t *OpenAPI_mov_behav_parseFromJSON(cJSON *mov_behavJSON) |
148 | 0 | { |
149 | 0 | OpenAPI_mov_behav_t *mov_behav_local_var = NULL; |
150 | 0 | OpenAPI_lnode_t *node = NULL; |
151 | 0 | cJSON *ts_start = NULL; |
152 | 0 | cJSON *ts_duration = NULL; |
153 | 0 | cJSON *num_of_ue = NULL; |
154 | 0 | cJSON *ratio = NULL; |
155 | 0 | cJSON *avr_speed = NULL; |
156 | 0 | cJSON *speed_thresd_infos = NULL; |
157 | 0 | OpenAPI_list_t *speed_thresd_infosList = NULL; |
158 | 0 | cJSON *direction_ue_infos = NULL; |
159 | 0 | OpenAPI_list_t *direction_ue_infosList = NULL; |
160 | 0 | ts_start = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "tsStart"); |
161 | 0 | if (!ts_start) { |
162 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [ts_start]"); |
163 | 0 | goto end; |
164 | 0 | } |
165 | 0 | if (!cJSON_IsString(ts_start) && !cJSON_IsNull(ts_start)) { |
166 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [ts_start]"); |
167 | 0 | goto end; |
168 | 0 | } |
169 | | |
170 | 0 | ts_duration = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "tsDuration"); |
171 | 0 | if (!ts_duration) { |
172 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [ts_duration]"); |
173 | 0 | goto end; |
174 | 0 | } |
175 | 0 | if (!cJSON_IsNumber(ts_duration)) { |
176 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [ts_duration]"); |
177 | 0 | goto end; |
178 | 0 | } |
179 | | |
180 | 0 | num_of_ue = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "numOfUe"); |
181 | 0 | if (num_of_ue) { |
182 | 0 | if (!cJSON_IsNumber(num_of_ue)) { |
183 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [num_of_ue]"); |
184 | 0 | goto end; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | ratio = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "ratio"); |
189 | 0 | if (ratio) { |
190 | 0 | if (!cJSON_IsNumber(ratio)) { |
191 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [ratio]"); |
192 | 0 | goto end; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | avr_speed = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "avrSpeed"); |
197 | 0 | if (avr_speed) { |
198 | 0 | if (!cJSON_IsNumber(avr_speed)) { |
199 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [avr_speed]"); |
200 | 0 | goto end; |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | 0 | speed_thresd_infos = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "speedThresdInfos"); |
205 | 0 | if (speed_thresd_infos) { |
206 | 0 | cJSON *speed_thresd_infos_local = NULL; |
207 | 0 | if (!cJSON_IsArray(speed_thresd_infos)) { |
208 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [speed_thresd_infos]"); |
209 | 0 | goto end; |
210 | 0 | } |
211 | | |
212 | 0 | speed_thresd_infosList = OpenAPI_list_create(); |
213 | |
|
214 | 0 | cJSON_ArrayForEach(speed_thresd_infos_local, speed_thresd_infos) { |
215 | 0 | if (!cJSON_IsObject(speed_thresd_infos_local)) { |
216 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [speed_thresd_infos]"); |
217 | 0 | goto end; |
218 | 0 | } |
219 | 0 | OpenAPI_speed_threshold_info_t *speed_thresd_infosItem = OpenAPI_speed_threshold_info_parseFromJSON(speed_thresd_infos_local); |
220 | 0 | if (!speed_thresd_infosItem) { |
221 | 0 | ogs_error("No speed_thresd_infosItem"); |
222 | 0 | goto end; |
223 | 0 | } |
224 | 0 | OpenAPI_list_add(speed_thresd_infosList, speed_thresd_infosItem); |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | direction_ue_infos = cJSON_GetObjectItemCaseSensitive(mov_behavJSON, "directionUeInfos"); |
229 | 0 | if (direction_ue_infos) { |
230 | 0 | cJSON *direction_ue_infos_local = NULL; |
231 | 0 | if (!cJSON_IsArray(direction_ue_infos)) { |
232 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [direction_ue_infos]"); |
233 | 0 | goto end; |
234 | 0 | } |
235 | | |
236 | 0 | direction_ue_infosList = OpenAPI_list_create(); |
237 | |
|
238 | 0 | cJSON_ArrayForEach(direction_ue_infos_local, direction_ue_infos) { |
239 | 0 | if (!cJSON_IsObject(direction_ue_infos_local)) { |
240 | 0 | ogs_error("OpenAPI_mov_behav_parseFromJSON() failed [direction_ue_infos]"); |
241 | 0 | goto end; |
242 | 0 | } |
243 | 0 | OpenAPI_direction_info_t *direction_ue_infosItem = OpenAPI_direction_info_parseFromJSON(direction_ue_infos_local); |
244 | 0 | if (!direction_ue_infosItem) { |
245 | 0 | ogs_error("No direction_ue_infosItem"); |
246 | 0 | goto end; |
247 | 0 | } |
248 | 0 | OpenAPI_list_add(direction_ue_infosList, direction_ue_infosItem); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 0 | mov_behav_local_var = OpenAPI_mov_behav_create ( |
253 | 0 | ogs_strdup(ts_start->valuestring), |
254 | | |
255 | 0 | ts_duration->valuedouble, |
256 | 0 | num_of_ue ? true : false, |
257 | 0 | num_of_ue ? num_of_ue->valuedouble : 0, |
258 | 0 | ratio ? true : false, |
259 | 0 | ratio ? ratio->valuedouble : 0, |
260 | 0 | avr_speed ? true : false, |
261 | 0 | avr_speed ? avr_speed->valuedouble : 0, |
262 | 0 | speed_thresd_infos ? speed_thresd_infosList : NULL, |
263 | 0 | direction_ue_infos ? direction_ue_infosList : NULL |
264 | 0 | ); |
265 | |
|
266 | 0 | return mov_behav_local_var; |
267 | 0 | end: |
268 | 0 | if (speed_thresd_infosList) { |
269 | 0 | OpenAPI_list_for_each(speed_thresd_infosList, node) { |
270 | 0 | OpenAPI_speed_threshold_info_free(node->data); |
271 | 0 | } |
272 | 0 | OpenAPI_list_free(speed_thresd_infosList); |
273 | 0 | speed_thresd_infosList = NULL; |
274 | 0 | } |
275 | 0 | if (direction_ue_infosList) { |
276 | 0 | OpenAPI_list_for_each(direction_ue_infosList, node) { |
277 | 0 | OpenAPI_direction_info_free(node->data); |
278 | 0 | } |
279 | 0 | OpenAPI_list_free(direction_ue_infosList); |
280 | 0 | direction_ue_infosList = NULL; |
281 | 0 | } |
282 | 0 | return NULL; |
283 | 0 | } |
284 | | |
285 | | OpenAPI_mov_behav_t *OpenAPI_mov_behav_copy(OpenAPI_mov_behav_t *dst, OpenAPI_mov_behav_t *src) |
286 | 0 | { |
287 | 0 | cJSON *item = NULL; |
288 | 0 | char *content = NULL; |
289 | |
|
290 | 0 | ogs_assert(src); |
291 | 0 | item = OpenAPI_mov_behav_convertToJSON(src); |
292 | 0 | if (!item) { |
293 | 0 | ogs_error("OpenAPI_mov_behav_convertToJSON() failed"); |
294 | 0 | return NULL; |
295 | 0 | } |
296 | | |
297 | 0 | content = cJSON_Print(item); |
298 | 0 | cJSON_Delete(item); |
299 | |
|
300 | 0 | if (!content) { |
301 | 0 | ogs_error("cJSON_Print() failed"); |
302 | 0 | return NULL; |
303 | 0 | } |
304 | | |
305 | 0 | item = cJSON_Parse(content); |
306 | 0 | ogs_free(content); |
307 | 0 | if (!item) { |
308 | 0 | ogs_error("cJSON_Parse() failed"); |
309 | 0 | return NULL; |
310 | 0 | } |
311 | | |
312 | 0 | OpenAPI_mov_behav_free(dst); |
313 | 0 | dst = OpenAPI_mov_behav_parseFromJSON(item); |
314 | 0 | cJSON_Delete(item); |
315 | |
|
316 | 0 | return dst; |
317 | 0 | } |
318 | | |