ventoy_http.h 7.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/******************************************************************************
 * ventoy_http.h
 *
 * Copyright (c) 2021, longpanda <admin@ventoy.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */
#ifndef __VENTOY_HTTP_H__
#define __VENTOY_HTTP_H__

#include <civetweb.h>


#define L1 "    "
#define L2 "        "
#define L3 "            "
#define L4 "                "

typedef enum bios_mode
{
    bios_common = 0,
    bios_legacy,
    bios_uefi,
    bios_ia32,
    bios_aa64,
    bios_mips,
    
    bios_max
}bios_mode;


typedef struct data_control
{
    int default_menu_mode;
    int treeview_style;
    int filter_dot_underscore;
    int sort_casesensitive;
    int max_search_level;
    int vhd_no_warning;
    int filter_iso;
    int filter_wim;
    int filter_efi;
    int filter_img;
    int filter_vhd;
    int filter_vtoy;
    int win11_bypass_check;
    int menu_timeout;
    char default_search_root[MAX_PATH];
    char default_image[MAX_PATH];
    char default_kbd_layout[32];
    char help_text_language[32];
}data_control;

#define display_mode_gui            0
#define display_mode_cli            1
#define display_mode_serial         2
#define display_mode_ser_console    3

typedef struct path_node
{
    char path[MAX_PATH];
    struct path_node *next;
}path_node;

typedef struct data_theme
{
    int default_file;
    path_node *filelist;
    int display_mode;
    char gfxmode[32];

    char ventoy_left[32];
    char ventoy_top[32];
    char ventoy_color[32];
    char serial_param[256];
    
    path_node *fontslist;
}data_theme;

#define path_type_file 0
#define path_type_dir  1

typedef struct data_alias_node
{
    int type;
    char path[MAX_PATH];
    char alias[256];
    struct data_alias_node *next;
}data_alias_node;

typedef struct data_alias
{
    data_alias_node *list;
}data_alias;


typedef struct data_tip_node
{
    int type;
    char path[MAX_PATH];
    char tip[256];
    struct data_tip_node *next;
}data_tip_node;

typedef struct data_tip
{
    char left[32];
    char top[32];
    char color[32];
    data_tip_node *list;
}data_tip;


#define class_type_key      0
#define class_type_dir      1
#define class_type_parent   2
typedef struct data_class_node
{
    int type;
    char path[MAX_PATH];
    char class[256];
    struct data_class_node *next;
}data_class_node;

typedef struct data_class
{
    data_class_node *list;
}data_class;


typedef struct data_auto_memdisk
{
    path_node *list;
}data_auto_memdisk;

typedef struct data_image_list
{
    int type;
    path_node *list;
}data_image_list;

typedef struct menu_password
{
    int type;
    char path[MAX_PATH];
    char pwd[256];
    struct menu_password *next;
}menu_password;

typedef struct data_password
{
    char bootpwd[256];
    char isopwd[256];
    char wimpwd[256];
    char vhdpwd[256];
    char imgpwd[256];
    char efipwd[256];
    char vtoypwd[256];

    menu_password *list;
}data_password;

typedef struct conf_replace_node
{
    int image;
    char path[MAX_PATH];
    char org[256];
    char new[MAX_PATH];
    struct conf_replace_node *next;
}conf_replace_node;
typedef struct data_conf_replace
{
    conf_replace_node *list;
}data_conf_replace;

typedef struct injection_node
{
    int type;
    char path[MAX_PATH];
    char archive[MAX_PATH];
    struct injection_node *next;
}injection_node;
typedef struct data_injection
{
    injection_node *list;
}data_injection;



typedef struct dud_node
{
    char path[MAX_PATH];
    path_node *list;

    struct dud_node *next;
}dud_node;

typedef struct data_dud
{
    dud_node *list;
}data_dud;

typedef struct auto_install_node
{
    int timeouten;
    int timeout;
    int autoselen;
    int autosel;
    int type;
    char path[MAX_PATH];
    path_node *list;

    struct auto_install_node *next;
}auto_install_node;

typedef struct data_auto_install
{
    auto_install_node *list;
}data_auto_install;

typedef struct persistence_node
{
    int timeouten;
    int timeout;
    int autoselen;
    int autosel;
    int type;
    char path[MAX_PATH];
    path_node *list;

    struct persistence_node *next;
}persistence_node;

typedef struct data_persistence
{
    persistence_node *list;
}data_persistence;




#define ventoy_save_plug(plug) \
{\
    for (i = 0; i < bios_max; i++) \
    {\
        scnprintf(title, sizeof(title), "%s%s", #plug, g_json_title_postfix[i]);\
        if (ventoy_data_cmp_##plug(g_data_##plug + i, g_data_##plug + bios_max))\
        {\
            pos += ventoy_data_save_##plug(g_data_##plug + i, title, JSON_SAVE_BUFFER + pos, JSON_BUF_MAX - pos);\
        }\
    }\
}



#define api_get_func(conn, json, name) \
{\
    int i = 0; \
    int pos = 0; \
\
    (void)json;\
\
    VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);\
    VTOY_JSON_FMT_ARY_BEGIN();\
\
    for (i = 0; i <= bios_max; i++)\
    {\
        __uiCurPos += ventoy_data_json_##name(g_data_##name + i, JSON_BUFFER + __uiCurPos, JSON_BUF_MAX - __uiCurPos);\
        VTOY_JSON_FMT_COMA();\
    }\
\
    VTOY_JSON_FMT_ARY_END();\
    VTOY_JSON_FMT_END(pos);\
\
    ventoy_json_buffer(conn, JSON_BUFFER, pos);\
}


#define vtoy_list_free(type, list) \
{\
    type *__next = NULL;\
    type *__node = list;\
    while (__node)\
    {\
        __next = __node->next;\
        free(__node);\
        __node = __next;\
    }\
}

#define vtoy_list_del(last, node, LIST, field) \
for (last = node = LIST; node; node = node->next) \
{\
    if (strcmp(node->field, field) == 0)\
    {\
        if (node == LIST)\
        {\
            LIST = LIST->next;\
        }\
        else\
        {\
            last->next = node->next;\
        }\
        free(node);\
        break;\
    }\
\
    last = node;\
}


#define vtoy_list_del_ex(last, node, LIST, field, cb) \
for (last = node = LIST; node; node = node->next) \
{\
    if (strcmp(node->field, field) == 0)\
    {\
        if (node == LIST)\
        {\
            LIST = LIST->next;\
        }\
        else\
        {\
            last->next = node->next;\
        }\
        cb(node->list);\
        free(node);\
        break;\
    }\
\
    last = node;\
}

#define vtoy_list_add(LIST, cur, node) \
if (LIST)\
{\
    cur = LIST;\
    while (cur && cur->next)\
    {\
        cur = cur->next;\
    }\
    cur->next = node;\
}\
else\
{\
    LIST = node;\
}



#define ventoy_parse_json(name) \
{\
    int __loop;\
    int __len = strlen(#name);\
    if (strncmp(#name, node->pcName, __len) == 0)\
    {\
        for (__loop = 0; __loop < bios_max; __loop++)\
        {\
            if (strcmp(g_json_title_postfix[__loop], node->pcName + __len) == 0)\
            {\
                vlog("json parse <%s>\n", node->pcName);\
                ventoy_parse_##name(node, g_data_##name + __loop);\
                break;\
            }\
        }\
    } \
}

#define CONTROL_PARSE_INT(node, val) \
    if (node->unData.pcStrVal[0] == '1') val = 1


#define VTOY_JSON_INT(key, val) vtoy_json_get_int(json, key, &val)
#define VTOY_JSON_STR(key, buf) vtoy_json_get_string(json, key, sizeof(buf), buf)
#define VTOY_JSON_STR_EX(key) vtoy_json_get_string_ex(json, key)

typedef int (*ventoy_json_callback)(struct mg_connection *conn, VTOY_JSON *json);
typedef struct JSON_CB
{
    const char *method;
    ventoy_json_callback callback;
}JSON_CB;

int ventoy_http_init(void);
void ventoy_http_exit(void);
int ventoy_http_start(const char *ip, const char *port);
int ventoy_http_stop(void);
int ventoy_data_save_all(void);

#endif /* __VENTOY_HTTP_H__ */