xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/tools/j2s4b/src/j2s4b/j2s.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *  Copyright (c) 2020, Rockchip Electronics Co., Ltd
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  */
14 
15 #ifndef J2S_H
16 #define J2S_H
17 
18 #include "cJSON.h"
19 #include "j2s_common.h"
20 #include "BinRecord.hpp"
21 
22 typedef struct {
23   char name[MAX_NAME];
24   uint8_t type;
25   uint8_t flags;
26 
27   uint32_t offset;         /* Offset in the parent struct */
28   uint32_t elem_size;      /* Elem size, e.g. char * for char ** */
29   uint16_t num_elem;       /* Elem num, e.g 3 for int [3] */
30   uint32_t base_elem_size; /* Base elem size, e.g. char for char ** */
31 
32   int16_t next_index; /* Next child's index of the parent */
33 
34   int16_t struct_index; /* For struct type only */
35   int16_t enum_index;   /* For enum type only */
36   int16_t len_index;    /* For dynamic array only */
37 } __attribute__((packed)) j2s_obj;
38 
39 typedef struct {
40   char name[MAX_NAME]; /* Struct name */
41   int16_t child_index; /* First child's index */
42 } __attribute__((packed)) j2s_struct;
43 
44 typedef struct {
45   char name[MAX_NAME]; /* Enum value name */
46   int32_t value;       /* Enum value */
47 } __attribute__((packed)) j2s_enum_value;
48 
49 typedef struct {
50   char name[MAX_NAME]; /* Enum name */
51   int16_t value_index; /* First value's index */
52   int16_t num_value;   /* Number of enum values */
53 } __attribute__((packed)) j2s_enum;
54 
55 #if 0
56 typedef struct __map_index {
57   void *dst_offset;
58   void *ptr_offset;
59   size_t len;
60 } map_index_t;
61 #endif
62 
63 typedef struct _j2s_pool {
64   uint8_t *data;
65   size_t size;
66   size_t used;
67   map_index_t *maps_list;
68   size_t map_len;
69 } j2s_pool_t;
70 
71 typedef struct {
72   /* Random magic number */
73   int magic;
74 
75   /* Parsed members */
76   int num_obj;
77   j2s_obj *objs;
78 
79   /* Parsed structs */
80   int num_struct;
81   j2s_struct *structs;
82 
83   /* Parsed enums */
84   int num_enum;
85   j2s_enum *enums;
86 
87   /* Parsed enum values*/
88   int num_enum_value;
89   j2s_enum_value *enum_values;
90 
91   /* Parsed member desc */
92   int num_desc;
93   const char **descs;
94 
95   int root_index; /* Root struct's index */
96 
97   bool format_json; /* Generate formatted JSON */
98   bool dump_desc;   /* Dump desc when dumping structs */
99   bool dump_enums;  /* Dump enum info when dumping structs */
100   bool manage_data; /* Free allocated data in deinit stage */
101 
102   void *priv; /* Private data */
103 } __attribute__((packed)) j2s_ctx;
104 
105 /* Helpers for alloc/free ptr */
106 void *j2s_alloc_data(j2s_ctx *ctx, size_t size, size_t* real_size);
107 
108 int j2s_alloc_map_record(j2s_ctx *ctx, void *dst, void *ptr, size_t len);
109 
110 void j2s_release_data(j2s_ctx *ctx, void *ptr);
111 
112 /* Init/deinit j2s_ctx */
113 void j2s_init(j2s_ctx *ctx);
114 void j2s_camgroup_init(j2s_ctx *ctx);
115 void j2s_deinit(j2s_ctx *ctx);
116 
117 /* Get size of struct */
118 int j2s_struct_size(j2s_ctx *ctx, int struct_index);
119 
120 /* Get name of j2s type */
121 const char *j2s_type_name(j2s_type type);
122 
123 cJSON *j2s_struct_to_template_json(j2s_ctx *ctx, const char *name);
124 
125 /* Dump root struct to template cJSON */
126 #define j2s_root_struct_to_template_json(ctx)                                  \
127   j2s_struct_to_template_json(ctx, NULL)
128 
129 cJSON *j2s_enums_to_json(j2s_ctx *ctx);
130 cJSON *j2s_struct_to_json(j2s_ctx *ctx, const char *name, void *ptr);
131 int j2s_json_to_struct(j2s_ctx *ctx, cJSON *json, const char *name, void *ptr);
132 
133 int j2s_json_to_bin(j2s_ctx *ctx, cJSON *json, const char *name, void **ptr,
134                     size_t struct_size, const char *ofname);
135 
136 int j2s_json_from_struct(j2s_ctx *ctx, cJSON *json, const char *name,
137                          void *ptr);
138 
139 int j2s_struct_free(j2s_ctx *ctx, const char *name, void *ptr);
140 
141 int j2s_struct_to_bin(j2s_ctx *ctx, const char *name, void *ptr, size_t len,
142                       const char *bin_path);
143 
144 /* Dump root struct to cJSON */
145 #define j2s_root_struct_to_json(ctx, ptr) j2s_struct_to_json(ctx, NULL, ptr)
146 
147 /* Apply cJSON to root struct */
148 #define j2s_json_to_root_struct(ctx, json, ptr)                                \
149   j2s_json_to_struct(ctx, json, NULL, ptr)
150 
151 /* Apply cJSON to root bin */
152 #define j2s_json_to_bin_root(ctx, json, ptr, ptr_size, ofname)                 \
153   j2s_json_to_bin(ctx, json, NULL, ptr, ptr_size, ofname)
154 
155 /* Query cJSON from root struct */
156 #define j2s_json_from_root_struct(ctx, json, ptr)                              \
157   j2s_json_from_struct(ctx, json, NULL, ptr)
158 
159 /* Read file content to buf */
160 void *j2s_read_file(const char *file, size_t *size);
161 
162 /* Apply JSON file to struct */
163 int j2s_json_file_to_struct(j2s_ctx *ctx, const char *file, const char *name,
164                             void *ptr);
165 
166 /* Apply JSON file to root struct */
167 #define j2s_json_file_to_root_struct(ctx, file, ptr)                           \
168   j2s_json_file_to_struct(ctx, file, NULL, ptr)
169 
170 char *j2s_dump_struct(j2s_ctx *ctx, const char *name, void *ptr);
171 
172 /* Dump root struct to JSON */
173 #define j2s_dump_root_struct(ctx, ptr) j2s_dump_struct(ctx, NULL, ptr)
174 
175 char *j2s_dump_template_struct(j2s_ctx *ctx, const char *name);
176 
177 /* Dump root struct to template JSON */
178 #define j2s_dump_template_root_struct(ctx) j2s_dump_template_struct(ctx, NULL)
179 
180 /* Apply JSON to struct */
181 int j2s_modify_struct(j2s_ctx *ctx, const char *str, const char *name,
182                       void *ptr);
183 
184 /* Query JSON from root struct */
185 char *j2s_query_struct(j2s_ctx *ctx, const char *str, void *ptr);
186 
187 typedef struct {
188   const char *name; /* Struct name */
189   void *ptr;        /* Struct instance */
190 } __attribute__((packed)) j2s_struct_info;
191 
192 /* Dump structs to JSON, info should end with {NULL, NULL} */
193 char *j2s_dump_structs(j2s_ctx *ctx, j2s_struct_info *info);
194 
195 typedef struct __struct_map {
196   const char *fpath;
197   const uint8_t *root_ptr;
198   FILE *file;
199   FILE *map_file;
200   FILE *block_file;
201   size_t len;
202   size_t offset;
203   size_t block_offset;
204   size_t map_offset;
205 } struct_map_t;
206 
207 extern struct_map_t *struct_map_create(const char *fpath);
208 extern int struct_map_close(struct_map_t *struct_map);
209 
210 #endif // J2S_H
211