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
|
use lib "$Bin/lib";
use JSON;
@ARGV < 2 and die "Usage: $0 <prefix> <file>\n";
my $prefix = shift @ARGV;
our $ctl;
our %tlv_types = (
gint8 => "int8_t",
guint8 => "uint8_t",
gint16 => "int16_t",
guint16 => "uint16_t",
gint32 => "int32_t",
guint32 => "uint32_t",
gint64 => "int64_t",
guint64 => "uint64_t",
gfloat => "float",
gboolean => "bool",
);
our %common_ref = ();
my @c_reserved_keywords = (
"alignas",
"alignof",
"auto",
"bool",
"break",
"case",
"char",
"const",
"constexpr",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extern",
"false",
"float",
"for",
"goto",
"if",
"inline",
"int",
"long",
"nullptr",
"register",
"restrict",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"struct",
"switch",
"thread_local",
"true",
"typedef",
"typeof",
"typeof_unqual",
"union",
"unsigned",
"void",
"volatile",
"while"
);
$prefix eq 'ctl_' and $ctl = 1;
sub get_json() {
local $/;
my $json = <>;
$json =~ s/^\s*\/\/.*$//mg;
return decode_json($json);
}
sub gen_cname($) {
my $name = shift;
$name =~ s/[^a-zA-Z0-9_]/_/g;
$name = "_${name}" if $name =~ /^\d/;
$name = lc($name);
$name = "_${name}" if (grep {$_ eq $name} @c_reserved_keywords);
return $name;
}
sub gen_has_types($) {
my $data = shift;
foreach my $field (@$data) {
$field = gen_common_ref($field);
my $type = $field->{"format"};
$type and return 1;
}
return undef
}
sub gen_tlv_set_func($$) {
my $name = shift;
my $data = shift;
$name = gen_cname($name);
if (gen_has_types($data)) {
return "int qmi_set_$name(struct qmi_msg *msg, struct qmi_$name *req)"
} else {
return "int qmi_set_$name(struct qmi_msg *msg)"
}
}
sub gen_tlv_parse_func($$) {
my $name = shift;
my $data = shift;
$name = gen_cname($name);
if (gen_has_types($data)) {
return "int qmi_parse_$name(struct qmi_msg *msg, struct qmi_$name *res)"
} else {
return "int qmi_parse_$name(struct qmi_msg *msg)"
}
}
sub gen_common_ref($$) {
my $field = shift;
$field = $common_ref{$field->{'common-ref'}} if $field->{'common-ref'} ne '';
return $field;
}
sub gen_foreach_message_type($$$)
{
my $data = shift;
my $req_sub = shift;
my $res_sub = shift;
my $ind_sub = shift;
foreach my $entry (@$data) {
my $args = [];
my $fields = [];
$common_ref{$entry->{'common-ref'}} = $entry if $entry->{'common-ref'} ne '';
next if $entry->{type} ne 'Message';
next if not defined $entry->{input} and not defined $entry->{output};
&$req_sub($prefix.$entry->{name}." Request", $entry->{input}, $entry);
&$res_sub($prefix.$entry->{name}." Response", $entry->{output}, $entry);
}
foreach my $entry (@$data) {
my $args = [];
my $fields = [];
$common_ref{$entry->{'common-ref'}} = $entry if $entry->{'common-ref'} ne '';
next if $entry->{type} ne 'Indication';
next if not defined $entry->{input} and not defined $entry->{output};
&$ind_sub($prefix.$entry->{name}." Indication", $entry->{output}, $entry);
}
}
1;
|