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 "common.h" 20 21 typedef struct { 22 char name[MAX_NAME]; 23 uint8_t type; 24 uint8_t flags; 25 26 uint32_t offset; /* Offset in the parent struct */ 27 uint32_t elem_size; /* Elem size, e.g. char * for char ** */ 28 uint16_t num_elem; /* Elem num, e.g 3 for int [3] */ 29 uint32_t base_elem_size; /* Base elem size, e.g. char for char ** */ 30 31 int16_t next_index; /* Next child's index of the parent */ 32 33 int16_t struct_index; /* For struct type only */ 34 int16_t enum_index; /* For enum type only */ 35 int16_t len_index; /* For dynamic array only */ 36 } __attribute__((packed)) j2s_obj; 37 38 typedef struct { 39 char name[MAX_NAME]; /* Struct name */ 40 int16_t child_index; /* First child's index */ 41 } __attribute__((packed)) j2s_struct; 42 43 typedef struct { 44 char name[MAX_NAME]; /* Enum value name */ 45 int32_t value; /* Enum value */ 46 } __attribute__((packed)) j2s_enum_value; 47 48 typedef struct { 49 char name[MAX_NAME]; /* Enum name */ 50 int16_t value_index; /* First value's index */ 51 int16_t num_value; /* Number of enum values */ 52 } __attribute__((packed)) j2s_enum; 53 54 typedef struct { 55 /* Random magic number */ 56 int magic; 57 58 /* Parsed members */ 59 int num_obj; 60 j2s_obj* objs; 61 62 /* Parsed structs */ 63 int num_struct; 64 j2s_struct* structs; 65 66 /* Parsed enums */ 67 int num_enum; 68 j2s_enum* enums; 69 70 /* Parsed enum values*/ 71 int num_enum_value; 72 j2s_enum_value* enum_values; 73 74 /* Parsed member desc */ 75 int num_desc; 76 const char** descs; 77 78 int root_index; /* Root struct's index */ 79 80 bool format_json; /* Generate formatted JSON */ 81 bool dump_desc; /* Dump desc when dumping structs */ 82 bool dump_enums; /* Dump enum info when dumping structs */ 83 bool manage_data; /* Free allocated data in deinit stage */ 84 85 void* priv; /* Private data */ 86 } __attribute__((packed)) j2s_ctx; 87 88 /* Helpers for alloc/free ptr */ 89 void* j2s_alloc_data(j2s_ctx* ctx, size_t size); 90 int j2s_add_data(j2s_ctx* ctx, void* ptr, bool freeable); 91 void j2s_release_data(j2s_ctx* ctx, void* ptr); 92 93 /* Init/deinit j2s_ctx */ 94 void j2s_init(j2s_ctx* ctx); 95 void j2s_camgroup_init(j2s_ctx* ctx); 96 void j2s_deinit(j2s_ctx* ctx); 97 98 /* Get size of struct */ 99 int j2s_struct_size(j2s_ctx* ctx, int struct_index); 100 101 /* Get name of j2s type */ 102 const char* j2s_type_name(j2s_type type); 103 104 cJSON* j2s_struct_to_template_json(j2s_ctx* ctx, const char* name); 105 106 /* Dump root struct to template cJSON */ 107 #define j2s_root_struct_to_template_json(ctx) \ 108 j2s_struct_to_template_json(ctx, NULL) 109 110 cJSON* j2s_enums_to_json(j2s_ctx* ctx); 111 cJSON* j2s_struct_to_json(j2s_ctx* ctx, const char* name, void* ptr); 112 int j2s_json_to_struct(j2s_ctx* ctx, cJSON* json, const char* name, void* ptr); 113 int j2s_json_from_struct(j2s_ctx* ctx, cJSON* json, const char* name, 114 void* ptr); 115 void j2s_struct_to_cache(j2s_ctx* ctx, const char* name, int fd, void* ptr); 116 int j2s_struct_from_cache(j2s_ctx* ctx, const char* name, int fd, void* ptr); 117 118 int j2s_struct_free(j2s_ctx* ctx, const char* name, void* ptr); 119 120 /* Dump root struct to cJSON */ 121 #define j2s_root_struct_to_json(ctx, ptr) j2s_struct_to_json(ctx, NULL, ptr) 122 123 /* Apply cJSON to root struct */ 124 #define j2s_json_to_root_struct(ctx, json, ptr) \ 125 j2s_json_to_struct(ctx, json, NULL, ptr) 126 127 /* Query cJSON from root struct */ 128 #define j2s_json_from_root_struct(ctx, json, ptr) \ 129 j2s_json_from_struct(ctx, json, NULL, ptr) 130 131 /* Store root struct to cache fd */ 132 #define j2s_root_struct_to_cache(ctx, fd, ptr) \ 133 j2s_struct_to_cache(ctx, NULL, fd, ptr) 134 135 /* Restore root struct from cache fd */ 136 #define j2s_root_struct_from_cache(ctx, fd, ptr) \ 137 j2s_struct_from_cache(ctx, NULL, fd, ptr) 138 139 /* Read file content to buf */ 140 void* j2s_read_file(const char* file, size_t* size); 141 142 /* Apply JSON file to struct */ 143 int j2s_json_file_to_struct(j2s_ctx* ctx, const char* file, const char* name, 144 void* ptr); 145 146 /* Apply JSON file to root struct */ 147 #define j2s_json_file_to_root_struct(ctx, file, ptr) \ 148 j2s_json_file_to_struct(ctx, file, NULL, ptr) 149 150 char* j2s_dump_struct(j2s_ctx* ctx, const char* name, void* ptr); 151 152 /* Load/save root struct to cache file */ 153 int j2s_load_struct_cache(j2s_ctx* ctx, const char* cache_file, void* ptr, 154 void* auth_data, int auth_size); 155 void j2s_save_struct_cache(j2s_ctx* ctx, const char* cache_file, void* ptr, 156 void* auth_data, int auth_size); 157 #define j2s_load_cache(ctx, cache_file, ptr) \ 158 j2s_load_struct_cache(ctx, cache_file, ptr, NULL, 0) 159 #define j2s_save_cache(ctx, cache_file, ptr) \ 160 j2s_save_struct_cache(ctx, cache_file, ptr, NULL, 0) 161 162 /* Dump root struct to JSON */ 163 #define j2s_dump_root_struct(ctx, ptr) j2s_dump_struct(ctx, NULL, ptr) 164 165 char* j2s_dump_template_struct(j2s_ctx* ctx, const char* name); 166 167 /* Dump root struct to template JSON */ 168 #define j2s_dump_template_root_struct(ctx) j2s_dump_template_struct(ctx, NULL) 169 170 /* Apply JSON to struct */ 171 int j2s_modify_struct(j2s_ctx* ctx, const char* str, const char* name, 172 void* ptr); 173 174 /* Query JSON from root struct */ 175 char* j2s_query_struct(j2s_ctx* ctx, const char* str, void* ptr); 176 177 typedef struct { 178 const char* name; /* Struct name */ 179 void* ptr; /* Struct instance */ 180 } __attribute__((packed)) j2s_struct_info; 181 182 /* Dump structs to JSON, info should end with {NULL, NULL} */ 183 char* j2s_dump_structs(j2s_ctx* ctx, j2s_struct_info* info); 184 185 #endif // J2S_H 186