5b45cb77887fee27722c1da44cf1d7ac0b89dc6e
1 /* Copyright (C) 2017 LambdaConcept */
6 #include <json-c/json.h>
10 static int file_to_js(char *filename
, json_object
**obj
)
13 struct json_tokener
*tok
=NULL
;
14 json_object
*pobj
=NULL
;
15 enum json_tokener_error jerr
;
27 in
= fopen(filename
, "r");
30 eprintf("Can't open configuration file: %s\n", filename
);
35 tok
= json_tokener_new();
39 eprintf("Can't create new tokener\n");
47 len2
= getline(&lineptr
, &len
, in
);
51 eprintf("End of file !\n");
54 pobj
= json_tokener_parse_ex(tok
, lineptr
, len2
);
55 if((jerr
= json_tokener_get_error(tok
)) == json_tokener_success
)
60 if((jerr
= json_tokener_get_error(tok
)) != json_tokener_continue
)
62 fprintf(stderr
, "ERROR in %s:\n", filename
);
63 fprintf(stderr
, "line:%d:\n%s",linenum
, lineptr
);
64 jerr
= json_tokener_get_error(tok
);
65 fprintf(stderr
, "json parse error: %s\n", json_tokener_error_desc(jerr
));
78 json_object_put(pobj
);
82 json_tokener_free(tok
);
95 static int json_to_interface_list(json_object
*interface
, struct interface_s
**iface
)
103 struct interface_s
*t_iface
=NULL
;
105 if(!interface
|| !iface
)
108 eprintf("Invalid argument\n");
112 if(!json_object_is_type(interface
, json_type_array
))
115 eprintf("Interface must be an array\n");
119 n
= json_object_array_length(interface
);
120 t_iface
= (struct interface_s
*)malloc(sizeof(struct interface_s
) * (n
+ 1));
124 eprintf("Not enough memory\n");
127 memset(t_iface
, 0,sizeof(struct interface_s
) * (n
+ 1));
129 for(i
= 0; i
< n
; i
++)
131 obj
= json_object_array_get_idx(interface
, i
);
132 if(json_object_is_type(obj
, json_type_object
))
134 if(!json_object_object_get_ex(obj
, "name", &name
))
137 eprintf("Module interface must have a name (%s)!\n", json_object_to_json_string(obj
));
140 t_iface
[i
].name
= strdup(json_object_get_string(name
));
142 if(json_object_object_get_ex(obj
, "index", &index
))
144 if(!json_object_is_type(index
, json_type_int
))
147 eprintf("Interface Index must be an int ! (%s)\n", json_object_to_json_string(obj
));
149 t_iface
[i
].index
= json_object_get_int(index
);
152 if(json_object_is_type(obj
, json_type_string
))
154 t_iface
[i
].name
= strdup(json_object_get_string(obj
));
167 static int module_list_free(struct module_s
*mod
)
170 struct module_s
*mnext
;
177 for(i
= 0; i
< mod
->niface
; i
++)
179 if(mod
->iface
[i
].name
)
181 free(mod
->iface
[i
].name
);
200 static int json_to_module_list(json_object
*obj
, struct module_s
**mod
)
202 struct module_s
*m
=NULL
;
203 struct module_s
*first
=NULL
;
210 json_object
*interface
;
211 json_object
*tickfirst
;
216 eprintf("Wrong arguments\n");
220 if(!json_object_is_type(obj
, json_type_array
))
223 eprintf("Config file must be an array\n");
227 n
= json_object_array_length(obj
);
228 for(i
= 0; i
< n
; i
++)
230 tobj
= json_object_array_get_idx(obj
, i
);
232 if(!json_object_object_get_ex(tobj
, "module", &name
))
237 if(!json_object_object_get_ex(tobj
, "interface", &interface
))
240 eprintf("expected \"interface\" in object (%s)\n", json_object_to_json_string(tobj
));
245 json_object_object_get_ex(tobj
, "args", &args
);
248 json_object_object_get_ex(tobj
, "tickfirst", &tickfirst
);
253 m
->next
=(struct module_s
*)malloc(sizeof(struct module_s
));
258 m
=(struct module_s
*)malloc(sizeof(struct module_s
));
263 eprintf("Not enough memory\n");
270 memset(m
, 0, sizeof(struct module_s
));
271 ret
= json_to_interface_list(interface
, &m
->iface
);
278 while(m
->iface
[len
++].name
);
280 m
->name
= strdup(json_object_get_string(name
));
283 m
->args
= strdup(json_object_to_json_string(args
));
287 m
->tickfirst
= json_object_get_boolean(tickfirst
);
294 eprintf("No modules found in config file:\n%s\n", json_object_to_json_string(obj
));
304 module_list_free(first
);
309 static int json_get_timebase(json_object
*obj
, uint64_t *timebase
)
314 uint64_t _timebase
= 0;
315 json_object
*json_timebase
;
317 if(!obj
|| !timebase
)
320 eprintf("Wrong arguments\n");
324 if(!json_object_is_type(obj
, json_type_array
))
327 eprintf("Config file must be an array\n");
331 n
= json_object_array_length(obj
);
332 for(i
= 0; i
< n
; i
++)
334 tobj
= json_object_array_get_idx(obj
, i
);
336 if(!json_object_object_get_ex(tobj
, "timebase", &json_timebase
))
344 eprintf("\"timebase\" found multiple times: in object (%s)\n", json_object_to_json_string(tobj
));
348 _timebase
= json_object_get_uint64(json_timebase
);
352 eprintf("\"timebase\" cannot be zero: in object (%s)\n", json_object_to_json_string(tobj
));
360 eprintf("No \"timebase\" found in config:\n%s\n", json_object_to_json_string(obj
));
363 *timebase
= _timebase
;
370 int litex_sim_file_parse(char *filename
, struct module_s
**mod
, uint64_t *timebase
)
372 struct module_s
*m
=NULL
;
373 json_object
*obj
=NULL
;
376 if(!filename
|| !mod
)
379 eprintf("Invalid argument\n");
383 ret
= file_to_js(filename
, &obj
);
389 ret
= json_get_timebase(obj
, timebase
);
395 ret
= json_to_module_list(obj
, &m
);
410 json_object_put(obj
);