1 #!/usr/bin/perl 2 3 use lib "./lib"; 4 use JSON; 5 6 use strict; 7 use warnings; 8 9 binmode STDOUT, ":utf8"; 10 use utf8; 11 12 if (!@ARGV) { 13 die("gen-headers.pl <mbim_foo.json>\n"); 14 } 15 16 my $json; 17 { 18 local $/; #Enable 'slurp' mode 19 open my $fh, "<", $ARGV[0]; 20 $json = <$fh>; 21 close $fh; 22 $json =~ s/^\s*\/\/.*$//mg; 23 } 24 25 my $data = decode_json($json); 26 27 28 my $id = 1; 29 30 sub gen_foreach_field($) 31 { 32 my $field = shift; 33 my $format; 34 35 if ($field->{format} eq "guint32") { 36 $format = "uint32_t"; 37 } elsif ($field->{format} eq "guint64") { 38 $format = "uint64_t"; 39 } elsif ($field->{format} eq "struct") { 40 $format = "struct ". lc $field->{"struct-type"}; 41 } elsif ($field->{format} eq "uuid") { 42 print "\tuint8_t " . lc $field->{name} . "[16];\n"; 43 return; 44 } elsif ($field->{format} eq "ipv4") { 45 print "\tuint8_t " . lc $field->{name} . "[4];\n"; 46 return; 47 } elsif ($field->{format} eq "ipv6") { 48 print "\tuint8_t " . lc $field->{name} . "[16];\n"; 49 return; 50 } elsif ($field->{format} eq "struct-array") { 51 print "\t/* struct " . lc $field->{"struct-type"} . " */\n"; 52 $format = "uint32_t"; 53 } elsif ($field->{format} eq "string") { 54 $format = "struct mbim_string"; 55 } else { 56 print "\t/* array type: " . $field->{format} . " */\n"; 57 $format = "uint32_t"; 58 } 59 if ($field->{"public-format"}) { 60 print "\t/* enum " . $field->{"public-format"} . " */\n"; 61 } 62 print "\t" . $format . " " . lc $field->{name} . ";\n"; 63 } 64 65 sub gen_struct($$) 66 { 67 my $struct = shift; 68 my $entry = shift; 69 70 $struct =~ s/ /_/g; 71 print "struct " . lc $struct . " {\n"; 72 foreach my $field (@{$entry}) { 73 gen_foreach_field($field); 74 } 75 print "} __attribute__((packed));\n\n"; 76 } 77 78 sub gen_foreach_struct($) 79 { 80 my $entry = shift; 81 82 if ($entry->{contents} && @{$entry->{contents}} > 0) { 83 my $struct = $entry->{name}; 84 gen_struct($struct, $entry->{contents}); 85 return; 86 } 87 88 print "/*\n * ID: " . $id . "\n * Command: " . $entry->{name} . "\n */\n\n"; 89 my $define = "mbim_cmd_" . $entry->{service} . "_" . $entry->{name}; 90 $define =~ s/ /_/g; 91 print "#define " . uc $define . "\t" . $id . "\n\n"; 92 93 $id = $id + 1; 94 # basic connect has no sequential numbering. ugly hack alert 95 if ($id == 17) { 96 $id = 19; 97 } 98 99 if ($entry->{query} && @{$entry->{query}} > 0) { 100 my $struct = "mbim_" . $entry->{service} . "_" . $entry->{name} . "_q"; 101 gen_struct($struct, $entry->{query}); 102 } 103 104 if ($entry->{response} && @{$entry->{response}} > 0) { 105 my $struct = "mbim_" . $entry->{service} . "_" . $entry->{name} . "_r"; 106 gen_struct($struct, $entry->{response}); 107 } 108 109 if ($entry->{set} && @{$entry->{set}} > 0) { 110 my $struct = "mbim_" . $entry->{service} . "_" . $entry->{name} . "_s"; 111 gen_struct($struct, $entry->{set}); 112 } 113 114 if ($entry->{notification} && @{$entry->{notification}} > 0) { 115 my $struct = "mbim_" . $entry->{service} . "_" . $entry->{name} . "_n"; 116 gen_struct($struct, $entry->{notification}); 117 } 118 } 119 120 sub gen_foreach_command($) 121 { 122 my $data = shift; 123 124 foreach my $entry (@$data) { 125 my $args = []; 126 my $fields = []; 127 128 if ($entry->{type} eq 'Command') { 129 gen_foreach_struct($entry); 130 } 131 if ($entry->{type} eq 'Struct') { 132 gen_foreach_struct($entry); 133 } 134 } 135 } 136 137 gen_foreach_command($data);
This page was automatically generated by LXR 0.3.1. • OpenWrt