#!/usr/bin/perl # Copyright 2012-2022, BlueFANG use strict; use warnings; use Carp; use feature 'say'; use File::Find; use Getopt::Long; use Pod::Usage; use Data::Dumper; use Cwd; my $man = 0; my $help = 0; my $initem = ''; my @hex_addresses = (); my $print_consts = ''; my $print_structs = ''; my $print_reg_map = ''; my $print_long = ''; my $reg_idx = 8; my $verbose = ''; my $searchdir = "$ENV{HOME}/projects/bk_ble/sdk/driver/ble"; my $db_file = ''; my $const_name = ''; my $preamble = < \$help, man => \$man, verbose => \$verbose, "regname=s" => sub { if ($verbose) { print "Pushing name $_[1]\n"; } push @hex_addresses, hex $_[1]; }, "db-file=s" => \$db_file, "const=s" => \$const_name, "dir=s" => \$searchdir, ) or pod2usage(2); pod2usage(-exitval => 0, -verbose => 1) if $help; pod2usage(-exitval => 0, -verbose => 2) if $man; sub get_header { my $dir = $File::Find::dir; my $file = $_; if ( $file =~ m/.*\.h/ ) { print "Header: $file\n"; extract_consts($file); } } $SIG{INT} = 'quit_all'; my %const_hash = (); sub extract_consts($) { my $file = shift; open HFILE, $file or die "Where did $file go?\n"; while() { my $line = $_; while ( $line =~ m/\b([A-Z_][A-Z_0-9]*)\b/g ) { my $cname = $1; next if exists $const_hash{$cname}; $const_hash{$cname} = $file; open INFILE, ">$ARGV[0]" or die "Cannot open output file $ARGV[0]\n"; printf INFILE $preamble, $file; printf INFILE $postamble, $cname, $cname; close INFILE; my $sysout; open my $fh, '>>', "output" or die; eval { my $cur_dir = cwd; chdir "$ENV{HOME}/projects/bk_ble"; close(STDERR); my $out; open(STDERR, ">>", \$out) or do { print $fh, "failed to open STDERR ($!)\n"; die }; $sysout = system("make", "-s", "constant_list"); chdir $cur_dir; }; if ($sysout == 0) { my $gen_assembly = "$ENV{HOME}/projects/bk_ble/build/usr/ble_tst/src/diagnostic1.S"; open DIAGCONST, $gen_assembly or die "Cannot find .S file $gen_assembly\n"; while () { if (/^[^#]*###CT\(([^()]*)\)\s*(.*\S)\s*=\s*([0-9]+)$/) { if ($db_file) { print DBFILE "$2=$3\{$file\}{$1}\n"; } print "$2=$3\n"; } } if ($cname eq $const_name) { print "Constant $cname added\n"; } } elsif ($sysout & 0xFF) {# died from a signal so probably was meant for us instead print "Interrupted\n"; die; } else { if ($cname eq $const_name) { print "Constant $cname failed to process\n"; } } } } close HFILE; } sub quit_all{ # in case one is actually quick enough print "Interrupted\n"; close DBFILE if $db_file; die; } if ( $db_file ) { open DBFILE, ">$db_file" or die "Cannot open the db file $db_file\n"; } find(\&get_header, $searchdir); close DBFILE;