forked from howerj/dbcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2json.c
201 lines (184 loc) · 5.35 KB
/
2json.c
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
/* @brief Convert the Abstract Syntax Tree generated by mpc for the DBC file
* into an equivalent JSON file.
* @copyright Richard James Howe (2018)
* @license MIT *
*/
#include "2json.h"
#include "util.h"
#include <assert.h>
#include <time.h>
static int print_escaped(FILE *o, const char *string)
{
assert(o);
assert(string);
char c;
int r = 0;
while((c = *(string)++)) {
switch(c) {
case '"': r = fputs(""", o); break;
case '\'': r = fputs("'", o); break;
case '<': r = fputs("<", o); break;
case '>': r = fputs(">", o); break;
case '&': r = fputs("&", o); break;
default:
r = fputc(c, o);
}
if (r < 0)
return -1;
}
return 0;
}
static int indent(FILE *o, unsigned depth)
{
assert(o);
while(depth--)
if (fputc('\t', o) != '\t')
return -1;
return 0;
}
enum { INT, STRING, BOOL, FLOAT };
static int pfield(FILE *o, unsigned depth, bool last, int type, const char *node, const char *fmt, ...)
{
assert(o);
assert(node);
assert(fmt);
va_list args;
assert(o && node && fmt);
errno = 0;
if (indent(o, depth) < 0)
goto warn;
if (fprintf(o, "\"%s\" : %s", node, type == STRING ? "\"" : "") < 0)
goto warn;
assert(fmt);
va_start(args, fmt);
int r = vfprintf(o, fmt, args);
va_end(args);
if (r < 0)
goto warn;
if (fprintf(o, "%s%s\n", type == STRING ? "\"" : "", !last ? "," : "") < 0)
goto warn;
return 0;
warn:
warning("XML node generation, problem writing to FILE* <%p>: %s", o, emsg());
return -1;
}
static int signal2json(signal_t *sig, FILE *o, unsigned depth)
{
assert(sig);
assert(o);
indent(o, depth);
fprintf(o, "\"signal\" : {\n");
pfield(o, depth+1, false, STRING, "name", "%s", sig->name);
pfield(o, depth+1, false, INT, "startbit", "%u", sig->start_bit);
pfield(o, depth+1, false, INT, "bitlength", "%u", sig->bit_length);
pfield(o, depth+1, false, STRING, "endianess", "%s", sig->endianess == endianess_motorola_e ? "motorola" : "intel");
pfield(o, depth+1, false, FLOAT, "scaling", "%g", sig->scaling);
pfield(o, depth+1, false, FLOAT, "offset", "%g", sig->offset);
pfield(o, depth+1, false, FLOAT, "minimum", "%g", sig->minimum);
pfield(o, depth+1, false, FLOAT, "maximum", "%g", sig->maximum);
pfield(o, depth+1, false, BOOL, "signed", "%s", sig->is_signed ? "true" : "false");
pfield(o, depth+1, false, INT, "floating", "%u", sig->is_floating ? sig->sigval : 0);
indent(o, depth+1);
fprintf(o, "\"units\" : \"");
print_escaped(o, sig->units);
fprintf(o, "\"\n");
indent(o, depth);
if (fprintf(o, "}") < 0)
return -1;
return 0;
}
static int msg2json(can_msg_t *msg, FILE *o, unsigned depth)
{
assert(msg);
assert(o);
indent(o, depth);
fprintf(o, "{\n");
pfield(o, depth+1, false, STRING, "name", "%s", msg->name);
pfield(o, depth+1, false, INT, "id", "%u", msg->id);
pfield(o, depth+1, !(msg->signal_count), INT, "dlc", "%u", msg->dlc);
signal_t *multiplexor = NULL;
for (size_t i = 0; i < msg->signal_count; i++) {
signal_t *sig = msg->sigs[i];
if (sig->is_multiplexor) {
if (multiplexor) {
error("multiple multiplexor values detected (only one per CAN msg is allowed) for %s", msg->name);
return -1;
}
multiplexor = sig;
continue;
}
if (sig->is_multiplexed)
continue;
if (signal2json(sig, o, depth+1) < 0)
return -1;
if ((msg->signal_count && i < (msg->signal_count - 1)) || multiplexor)
fprintf(o, ",");
fprintf(o, "\n");
}
if (multiplexor) {
indent(o, depth+1);
fprintf(o, "\"multiplexor-group\" : {\n");
indent(o, depth+2);
fprintf(o, "\"multiplexor\" : {\n");
if (signal2json(multiplexor, o, depth+3) < 0)
return -1;
fprintf(o, "\n");
indent(o, depth+2);
fprintf(o, "}%s\n", msg->signal_count ? "," : "");
size_t multiplexed_count = 0;
for (size_t i = 0; i < msg->signal_count; i++) {
signal_t *sig = msg->sigs[i];
if (sig->is_multiplexed)
multiplexed_count++;
}
for (size_t i = 0, j = 0; i < msg->signal_count; i++) {
signal_t *sig = msg->sigs[i];
if (!(sig->is_multiplexed))
continue;
j++;
indent(o, depth+2);
fprintf(o, "\"multiplexed\" : {\n");
pfield(o, depth+3, false, INT, "multiplexed-on", "%u", sig->switchval);
if (signal2json(sig, o, depth+3) < 0)
return -1;
fprintf(o, "\n");
indent(o, depth+2);
fprintf(o, "}");
if (multiplexed_count && j < multiplexed_count)
fprintf(o, ",");
fprintf(o, "\n");
}
indent(o, depth+1);
fprintf(o, "}\n");
}
indent(o, depth);
if (fprintf(o, "}") < 0)
return -1;
return 0;
}
int dbc2json(dbc_t *dbc, FILE *output, bool use_time_stamps)
{
assert(dbc);
assert(output);
/**@todo print out ECU node information, and the standard XML header */
time_t rawtime = time(NULL);
struct tm *timeinfo = localtime(&rawtime);
fprintf(output, "{\n");
fprintf(output, "\t\"description\" : \"JSON generated from a CAN DBC file\",\n");
fprintf(output, "\t\"compiler\" : \"dbcc\",\n");
fprintf(output, "\t\"site\" : \"https://github.com/howerj/dbcc\",\n");
if (use_time_stamps)
fprintf(output, "\t\"generated-on\": %s,", asctime(timeinfo));
fprintf(output, "\t\"messages\" : [\n");
for (size_t i = 0; i < dbc->message_count; i++) {
if (msg2json(dbc->messages[i], output, 2) < 0)
return -1;
if (dbc->message_count && i < (dbc->message_count - 1))
fprintf(output, ",");
fprintf(output, "\n");
}
fprintf(output, "\t]\n");
if (fprintf(output, "}\n") < 0)
return -1;
return 0;
}