gen-header.pl 3.1 KB

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