Subversion Repositories HomeAutomation

Rev

Rev 1046 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1046 Rev 1047
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
 
2
 
3
# Written by Mattias Runge 2008
3
# Written by Mattias Runge 2008
4
 
4
 
5
use XML::Twig;
5
use XML::Twig;
6
use Getopt::Long;
6
use Getopt::Long;
7
 
7
 
8
$binaryname = "makeCanId.pl";
8
$binaryname = "makeCanId.pl";
9
if (@ARGV > 0)
9
if (@ARGV > 0)
10
{
10
{
11
    GetOptions( "xmlfile=s"     => \$xmlfile,
11
    GetOptions( "xmlfile=s"     => \$xmlfile,
12
            "cfile=s"   => \$cfile);
12
            "cfile=s"   => \$cfile);
13
}
13
}
14
else
14
else
15
{
15
{
16
    $help = 1;
16
    $help = 1;
17
}
17
}
18
 
18
 
19
if ($help)
19
if ($help)
20
{
20
{
21
    print "Usage ./$binaryname [options]\n";
21
    print "Usage ./$binaryname [options]\n";
22
    print "Options:\n";
22
    print "Options:\n";
23
    print "  -x <xmlfile>           Choose xmlfile to parse\n";
23
    print "  -x <xmlfile>           Choose xmlfile to parse\n";
24
    print "  -c <headerfile>        Choose header file to write\n";
24
    print "  -c <headerfile>        Choose header file to write\n";
25
    print "  -h                 Shows this usage\n";
25
    print "  -h                 Shows this usage\n";
26
    print "\n";
26
    print "\n";
27
    print "example: ./$binaryname -x data.xml -c moduleid.h\n";
27
    print "example: ./$binaryname -x data.xml -c moduleid.h\n";
28
 
28
 
29
    exit 0;
29
    exit 0;
30
}
30
}
31
 
31
 
32
my $now = localtime time;
32
my $now = localtime time;
33
my $twig = XML::Twig->new();
33
my $twig = XML::Twig->new();
34
 
34
 
35
$twig->parsefile($xmlfile);
35
$twig->parsefile($xmlfile);
36
 
36
 
37
my $root = $twig->root;
37
my $root = $twig->root;
38
 
38
 
39
open (MYFILE, '>' . $cfile);
39
open (MYFILE, '>' . $cfile);
40
 
40
 
41
print MYFILE "// Built on $now by $binaryname\n";
41
print MYFILE "// Built on $now by $binaryname\n";
42
print MYFILE "// DO NOT CHANGE MANUALLY\n\n";
42
print MYFILE "// DO NOT CHANGE MANUALLY\n\n";
43
 
43
 
44
 
44
 
45
print MYFILE "#ifndef MODULEID_H_\n";
45
print MYFILE "#ifndef MODULEID_H_\n";
46
print MYFILE "#define MODULEID_H_\n\n";
46
print MYFILE "#define MODULEID_H_\n\n";
47
 
47
 
48
print MYFILE "//------------------ //\n";
48
print MYFILE "//------------------ //\n";
49
print MYFILE "// Class definitions //\n";
49
print MYFILE "// Class definitions //\n";
50
print MYFILE "//------------------ //\n";
50
print MYFILE "//------------------ //\n";
51
print MYFILE "\n";
51
print MYFILE "\n";
52
 
52
 
53
foreach my $class ($root->first_child('classes')->children('class'))
53
foreach my $class ($root->first_child('classes')->children('class'))
54
{
54
{
55
    my $className = $class->att('name');
55
    my $className = $class->att('name');
56
    $className =~ tr/a-z/A-Z/;
56
    $className =~ tr/a-z/A-Z/;
57
    print MYFILE "#define CAN_MODULE_CLASS_$className " . $class->att('id') . "\n";
57
    print MYFILE "#define CAN_MODULE_CLASS_$className " . $class->att('id') . "\n";
58
}
58
}
59
 
59
 
60
print MYFILE "\n";
60
print MYFILE "\n";
61
print MYFILE "//-------------------- //\n";
61
print MYFILE "//-------------------- //\n";
62
print MYFILE "// Command definitions //\n";
62
print MYFILE "// Command definitions //\n";
63
print MYFILE "//-------------------- //\n";
63
print MYFILE "//-------------------- //\n";
64
print MYFILE "\n";
64
print MYFILE "\n";
65
 
65
 
66
foreach my $command ($root->first_child('commands')->children('command'))
66
foreach my $command ($root->first_child('commands')->children('command'))
67
{
67
{
68
    my $commandName = $command->att('name');
68
    my $commandName = $command->att('name');
69
    $commandName =~ tr/a-z/A-Z/;
69
    $commandName =~ tr/a-z/A-Z/;
70
    my $commandType = $command->att('type');
70
    my $commandType = $command->att('type');
71
    $commandType =~ tr/a-z/A-Z/;
71
    $commandType =~ tr/a-z/A-Z/;
72
   
72
   
73
    if ($commandType eq "SPECIFIC")
73
    if ($commandType eq "SPECIFIC")
74
    {
74
    {
75
        $commandType = $command->att('module');
75
        $commandType = $command->att('module');
76
        $commandType =~ tr/a-z/A-Z/;
76
        $commandType =~ tr/a-z/A-Z/;
77
    }
77
    }
78
   
78
   
79
    print MYFILE "#define CAN_MODULE_CMD_" . $commandType . "_" . $commandName . " " . $command->att('id') . "\n";
79
    print MYFILE "#define CAN_MODULE_CMD_" . $commandType . "_" . $commandName . " " . $command->att('id') . "\n";
80
}
80
}
81
 
81
 
82
print MYFILE "\n";
82
print MYFILE "\n";
83
print MYFILE "//------------------------------- //\n";
83
print MYFILE "//------------------------------- //\n";
84
print MYFILE "// Command enum value definitions //\n";
84
print MYFILE "// Command enum value definitions //\n";
85
print MYFILE "//------------------------------- //\n";
85
print MYFILE "//------------------------------- //\n";
86
print MYFILE "\n";
86
print MYFILE "\n";
87
 
87
 
88
foreach my $command ($root->first_child('commands')->children('command'))
88
foreach my $command ($root->first_child('commands')->children('command'))
89
{
89
{
90
    my $commandName = $command->att('name');
90
    my $commandName = $command->att('name');
91
    $commandName =~ tr/a-z/A-Z/;
91
    $commandName =~ tr/a-z/A-Z/;
92
    my $commandType = $command->att('type');
92
    my $commandType = $command->att('type');
93
    $commandType =~ tr/a-z/A-Z/;
93
    $commandType =~ tr/a-z/A-Z/;
94
   
94
   
95
    if ($commandType eq "SPECIFIC")
95
    if ($commandType eq "SPECIFIC")
96
    {
96
    {
97
        $commandType = $command->att('module');
97
        $commandType = $command->att('module');
98
        $commandType =~ tr/a-z/A-Z/;
98
        $commandType =~ tr/a-z/A-Z/;
99
    }
99
    }
100
   
100
   
101
    if ($command->first_child('variables'))
101
    if ($command->first_child('variables'))
102
    {
102
    {
103
        foreach my $variable ($command->first_child('variables')->children('variable'))
103
        foreach my $variable ($command->first_child('variables')->children('variable'))
104
        {
104
        {
105
            my $variableName = $variable->att('name');
105
            my $variableName = $variable->att('name');
106
            $variableName =~ tr/a-z/A-Z/;
106
            $variableName =~ tr/a-z/A-Z/;
107
            my $variableType = $variable->att('type');
107
            my $variableType = $variable->att('type');
108
            $variableType =~ tr/a-z/A-Z/;
108
            $variableType =~ tr/a-z/A-Z/;
109
           
109
           
110
            if ($variableType eq "ENUM")
110
            if ($variableType eq "ENUM")
111
            {
111
            {
112
                foreach my $value ($variable->children('value'))
112
                foreach my $value ($variable->children('value'))
113
                {
113
                {
114
                    my $valueName = $value->att('name');
114
                    my $valueName = $value->att('name');
115
                    $valueName =~ tr/a-z/A-Z/;
115
                    $valueName =~ tr/a-z/A-Z/;
116
               
116
               
117
                    print MYFILE "#define CAN_MODULE_CMD_ENUM_" . $commandType . "_" . $commandName . "_" . $variableName . "_" . $valueName . " " . $value->att('id') . "\n";
117
                    print MYFILE "#define CAN_MODULE_ENUM_" . $commandType . "_" . $commandName . "_" . $variableName . "_" . $valueName . " " . $value->att('id') . "\n";
118
                }
118
                }
119
            }
119
            }
120
        }
120
        }
121
    }
121
    }
122
}
122
}
123
 
123
 
124
print MYFILE "\n";
124
print MYFILE "\n";
125
print MYFILE "//------------------- //\n";
125
print MYFILE "//------------------- //\n";
126
print MYFILE "// Module definitions //\n";
126
print MYFILE "// Module definitions //\n";
127
print MYFILE "//------------------- //\n";
127
print MYFILE "//------------------- //\n";
128
print MYFILE "\n";
128
print MYFILE "\n";
129
 
129
 
130
foreach my $module ($root->first_child('modules')->children('module'))
130
foreach my $module ($root->first_child('modules')->children('module'))
131
{
131
{
132
    my $className = $module->att('class');
132
    my $className = $module->att('class');
133
    $className =~ tr/a-z/A-Z/;
133
    $className =~ tr/a-z/A-Z/;
134
    my $moduleName = $module->att('name');
134
    my $moduleName = $module->att('name');
135
    $moduleName =~ tr/a-z/A-Z/;
135
    $moduleName =~ tr/a-z/A-Z/;
136
    print MYFILE "#define CAN_MODULE_TYPE_" . $className . "_" . $moduleName . " " . $module->att('id') . "\n";
136
    print MYFILE "#define CAN_MODULE_TYPE_" . $className . "_" . $moduleName . " " . $module->att('id') . "\n";
137
}
137
}
138
 
138
 
139
print MYFILE "\n";
139
print MYFILE "\n";
140
print MYFILE "//----------------- //\n";
140
print MYFILE "//----------------- //\n";
141
print MYFILE "// Misc definitions //\n";
141
print MYFILE "// Misc definitions //\n";
142
print MYFILE "//----------------- //\n";
142
print MYFILE "//----------------- //\n";
143
print MYFILE "\n";
143
print MYFILE "\n";
144
 
144
 
145
foreach my $define ($root->first_child('defines')->children('define'))
145
foreach my $define ($root->first_child('defines')->children('define'))
146
{
146
{
147
    my $groupName = $define->att('group');
147
    my $groupName = $define->att('group');
148
    $groupName =~ tr/a-z/A-Z/;
148
    $groupName =~ tr/a-z/A-Z/;
149
    my $defineName = $define->att('name');
149
    my $defineName = $define->att('name');
150
    $defineName =~ tr/a-z/A-Z/;
150
    $defineName =~ tr/a-z/A-Z/;
151
    print MYFILE "#define " . $groupName . "_" . $defineName . " " . $define->att('id') . "\n";
151
    print MYFILE "#define " . $groupName . "_" . $defineName . " " . $define->att('id') . "\n";
152
}
152
}
153
 
153
 
154
print MYFILE "\n";
154
print MYFILE "\n";
155
 
155
 
156
print MYFILE "#endif /*MODULEID_H_*/\n";
156
print MYFILE "#endif /*MODULEID_H_*/\n";
157
 
157
 
158
close (MYFILE);
158
close (MYFILE);
159
 
159
 
160
print "Successfully wrote " . $cfile . "\n";
160
print "Successfully wrote " . $cfile . "\n";
161
 
161