mqtt/mqtt_template.c
changeset 4016 9e4a3a3ea5c0
parent 4015 ab96d56f9ae9
child 4017 3977d55217ec
equal deleted inserted replaced
4015:ab96d56f9ae9 4016:9e4a3a3ea5c0
    72 #define format_separator ", "
    72 #define format_separator ", "
    73 
    73 
    74 #define format_SIMPLE(C_type, name, _A) #name " : " format_##C_type
    74 #define format_SIMPLE(C_type, name, _A) #name " : " format_##C_type
    75 #define format_OBJECT(C_type, name, _A) #name " : {{ " TYPE_##C_type(format, _A) " }}"
    75 #define format_OBJECT(C_type, name, _A) #name " : {{ " TYPE_##C_type(format, _A) " }}"
    76 
    76 
    77 #define arg_separator ,
    77 #define scanf_args_separator ,
    78 
    78 
    79 #define arg_SIMPLE(C_type, name, data_ptr) data_ptr->name
    79 #define scanf_args_SIMPLE(C_type, name, data_ptr) &data_ptr->name
    80 #define arg_OBJECT(C_type, name, data_ptr) TYPE_##C_type(arg, (&data_ptr->name))
    80 #define scanf_args_OBJECT(C_type, name, data_ptr) TYPE_##C_type(scanf_args, (&data_ptr->name))
       
    81 
       
    82 #define printf_args_separator ,
       
    83 
       
    84 #define printf_args_SIMPLE(C_type, name, data_ptr) data_ptr->name
       
    85 #define printf_args_OBJECT(C_type, name, data_ptr) TYPE_##C_type(printf_args, (&data_ptr->name))
    81 
    86 
    82 #define DECL_JSON_INPUT(C_type, c_loc_name) \
    87 #define DECL_JSON_INPUT(C_type, c_loc_name) \
    83 int json_parse_##c_loc_name(char *json, const int len, void *void_ptr) {{ \
    88 int json_parse_##c_loc_name(char *json, const int len, void *void_ptr) {{ \
    84     C_type *struct_ptr = (C_type *)void_ptr; \
    89     C_type *struct_ptr = (C_type *)void_ptr; \
    85     return json_scanf(json, len, "{{" TYPE_##C_type(format,) "}}", TYPE_##C_type(arg, struct_ptr)); \
    90     return json_scanf(json, len, "{{" TYPE_##C_type(format,) "}}", TYPE_##C_type(scanf_args, struct_ptr)); \
    86 }}
    91 }}
    87 
    92 
    88 /* Pre-allocated json output buffer for json_printf */
    93 /* Pre-allocated json output buffer for json_printf */
    89 #define json_out_size 1<<12 // 4K
    94 #define json_out_size 1<<12 // 4K
    90 static char json_out_buf[json_out_size] = {{0,}};
    95 static char json_out_buf[json_out_size] = {{0,}};
    91 static int json_out_len = 0;
    96 static int json_out_len = 0;
    92 
    97 
    93 #define DECL_JSON_OUTPUT(C_type, c_loc_name) \
    98 #define DECL_JSON_OUTPUT(C_type, c_loc_name) \
    94 int json_gen_##c_loc_name(C_type *struct_ptr) {{ \
    99 int json_gen_##c_loc_name(C_type *struct_ptr) {{ \
    95     struct json_out out = JSON_OUT_BUF(json_out_buf, json_out_size); \
   100     struct json_out out = JSON_OUT_BUF(json_out_buf, json_out_size); \
    96     json_out_len = json_printf(&out, "{{" TYPE_##C_type(format,) "}}", TYPE_##C_type(arg, struct_ptr)); \
   101     json_out_len = json_printf(&out, "{{" TYPE_##C_type(format,) "}}", TYPE_##C_type(printf_args, struct_ptr)); \
    97     if(json_out_len > json_out_size){{ \
   102     if(json_out_len > json_out_size){{ \
    98         json_out_len = 0; \
   103         json_out_len = 0; \
    99         return -EOVERFLOW; \
   104         return -EOVERFLOW; \
   100     }} \
   105     }} \
   101     return 0; \
   106     return 0; \