• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.4
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# This script generates vhal_consts_x_y.py files for use in vhal_emulator
19# They are generated from corresponding data in Vehicle HAL types.hal files
20# To run, invoke at a shell by saying:
21# $ packages/services/Car/tools/emulator/vhal_const_generate.py
22# The script will automatically locate itself and the required HAL files and will write next
23# to itself vhal_consts_x.y.py for any version of Vehicle HAL that it knows about
24# Those files can then be used with vhal_emulator.py as per that script's documentation
25
26from __future__ import print_function
27
28import datetime
29
30def printHeader(dest):
31    year = datetime.datetime.now().year
32    print("# Copyright (C) %s The Android Open Source Project" % year, file=dest)
33    print("#", file=dest)
34    print("# Licensed under the Apache License, Version 2.0 (the \"License\");", file=dest)
35    print("# you may not use this file except in compliance with the License.", file=dest)
36    print("# You may obtain a copy of the License at", file=dest)
37    print("#", file=dest)
38    print("#      http://www.apache.org/licenses/LICENSE-2.0", file=dest)
39    print("#", file=dest)
40    print("# Unless required by applicable law or agreed to in writing, software", file=dest)
41    print("# distributed under the License is distributed on an \"AS IS\" BASIS,", file=dest)
42    print("# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", file=dest)
43    print("# See the License for the specific language governing permissions and", file=dest)
44    print("# limitations under the License.", file=dest)
45    print("#", file=dest)
46    print("# DO NOT EDIT MANUALLY", file=dest)
47    print("# This file was autogenerated by vhal_const_generate.py", file=dest)
48
49def printEnum(doc, name, dest, postprocess=lambda x: x):
50    # Construct a value name prefix from the group name
51    valueNamePrefix = name.upper() + '_'
52
53    enum_object = doc['enums'][name]
54    print("\n# %s" % name, file=dest)
55    for case in enum_object.cases:
56        print('%s%s = %s' % (valueNamePrefix, case.name,
57            postprocess(case.value.resolve(enum_object, doc))),
58            file=dest)
59
60import os, os.path
61import sys
62
63script_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)))
64parent_location = os.path.abspath(os.path.join(script_directory, '..'))
65sys.path.append(parent_location)
66
67# hidl_parser depends on a custom Python package that requires installation
68# give user guidance should the import fail
69try:
70    from hidl_parser import parser
71except ImportError as e:
72    isPly = False
73    pipTool = "pip%s" % ("3" if sys.version_info > (3,0) else "")
74    if hasattr(e, 'name'):
75        if e.name == 'ply': isPly = True
76    elif hasattr(e, 'message'):
77        if e.message.endswith('ply'): isPly = True
78    if isPly:
79        print('could not import ply.')
80        print('ply is available as part of an Android checkout in external/ply')
81        print('or it can be installed via "sudo %s install ply"' % pipTool)
82        sys.exit(1)
83    else:
84        raise e
85
86android_build_top = os.environ.get("ANDROID_BUILD_TOP", None)
87if android_build_top is not None:
88    vhal_location = os.path.join(android_build_top, 'hardware','interfaces','automotive','vehicle')
89else:
90    vhal_location = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)),
91        '..','..','..','..','..','hardware','interfaces','automotive','vehicle'
92    ))
93if not(os.path.exists(vhal_location) and os.path.isdir(vhal_location)):
94    print("Vehicle HAL was not found at %s. lunch may provide a correct environment, or files moved" % vhal_location)
95    sys.exit(1)
96
97vhal_20_file = os.path.join(vhal_location, '2.0', 'types.hal')
98vhal_21_file = os.path.join(vhal_location, '2.1', 'types.hal')
99
100print("Generating content from Vehicle HAL 2.0 (%s) and 2.1 (%s)" % (vhal_20_file, vhal_21_file))
101
102vhal_20_doc = parser.parse(vhal_20_file)
103vhal_21_doc = parser.parse(vhal_21_file)
104
105# Work around the fact that the parser doesn't (yet?) deal with inheritance.
106# WARNING:  This pattern is rather unsafe since we're not merging the lists as we should!
107# vhal_21_doc['enums']['VehiclePropertyGroup'] = vhal_20_doc['enums']['VehiclePropertyGroup']
108# vhal_21_doc['enums']['VehiclePropertyType'] = vhal_20_doc['enums']['VehiclePropertyType']
109# vhal_21_doc['enums']['VehicleArea'] = vhal_20_doc['enums']['VehicleArea']
110
111def generateHal20():
112    print("********************************")
113    print("Generating VHal 2.0 constants...")
114    vhal_20_file = open(os.path.join(script_directory, 'vhal_consts_2_0.py'), 'w')
115
116    printHeader(vhal_20_file)
117
118    for group in vhal_20_doc['enums']:
119        print(group)
120        printEnum(vhal_20_doc, group, vhal_20_file, lambda x : hex(x))
121
122    print("\n# Create a container of value_type constants to be used by vhal_emulator", file=vhal_20_file)
123    print("class vhal_types_2_0:", file=vhal_20_file)
124    print("    TYPE_STRING  = [VEHICLEPROPERTYTYPE_STRING]", file=vhal_20_file)
125    print("    TYPE_BYTES   = [VEHICLEPROPERTYTYPE_BYTES]", file=vhal_20_file)
126    print("    TYPE_INT32   = [VEHICLEPROPERTYTYPE_BOOLEAN,", file=vhal_20_file)
127    print("                    VEHICLEPROPERTYTYPE_INT32]", file=vhal_20_file)
128    print("    TYPE_INT64   = [VEHICLEPROPERTYTYPE_INT64]", file=vhal_20_file)
129    print("    TYPE_FLOAT   = [VEHICLEPROPERTYTYPE_FLOAT]", file=vhal_20_file)
130    print("    TYPE_INT32S  = [VEHICLEPROPERTYTYPE_INT32_VEC]", file=vhal_20_file)
131    print("    TYPE_FLOATS  = [VEHICLEPROPERTYTYPE_FLOAT_VEC]", file=vhal_20_file)
132    print("    TYPE_COMPLEX = [VEHICLEPROPERTYTYPE_COMPLEX]", file=vhal_20_file)
133
134def generateHal21():
135    print("********************************")
136    print("Generating VHal 2.1 constants...")
137    vhal_21_file = open(os.path.join(script_directory, 'vhal_consts_2_1.py'), 'w')
138    printHeader(vhal_21_file)
139    print('from vhal_consts_2_0 import *', file=vhal_21_file)
140
141    for group in vhal_21_doc['enums']:
142        print(group)
143        printEnum(vhal_21_doc, group, vhal_21_file, lambda x : hex(x))
144
145
146generateHal20()
147# generateHal21()
148