1#!/usr/bin/env python3
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
97def generateHal20():
98    vhal_20_file = os.path.join(vhal_location, '2.0', 'types.hal')
99    if not(os.path.exists(vhal_20_file)):
100        print("Vehicle HAL was not found at %s. lunch may provide a correct environment, or files moved" % vhal_location)
101        sys.exit(1)
102
103    print("Generating constants from Vehicle HAL 2.0 (%s)" % (vhal_20_file))
104    vhal_20_doc = parser.parse(vhal_20_file)
105
106    vhal_20_file = open(os.path.join(script_directory, 'vhal_consts_2_0.py'), 'w')
107
108    printHeader(vhal_20_file)
109
110    for group in vhal_20_doc['enums']:
111        print(group)
112        printEnum(vhal_20_doc, group, vhal_20_file, lambda x : hex(x))
113
114    print("\n# Create a container of value_type constants to be used by vhal_emulator", file=vhal_20_file)
115    print("class vhal_types_2_0:", file=vhal_20_file)
116    print("    TYPE_STRING  = [VEHICLEPROPERTYTYPE_STRING]", file=vhal_20_file)
117    print("    TYPE_BYTES   = [VEHICLEPROPERTYTYPE_BYTES]", file=vhal_20_file)
118    print("    TYPE_INT32   = [VEHICLEPROPERTYTYPE_BOOLEAN,", file=vhal_20_file)
119    print("                    VEHICLEPROPERTYTYPE_INT32]", file=vhal_20_file)
120    print("    TYPE_INT64   = [VEHICLEPROPERTYTYPE_INT64]", file=vhal_20_file)
121    print("    TYPE_FLOAT   = [VEHICLEPROPERTYTYPE_FLOAT]", file=vhal_20_file)
122    print("    TYPE_INT32S  = [VEHICLEPROPERTYTYPE_INT32_VEC]", file=vhal_20_file)
123    print("    TYPE_INT64S  = [VEHICLEPROPERTYTYPE_INT64_VEC]", file=vhal_20_file)
124    print("    TYPE_FLOATS  = [VEHICLEPROPERTYTYPE_FLOAT_VEC]", file=vhal_20_file)
125    print("    TYPE_MIXED   = [VEHICLEPROPERTYTYPE_MIXED]", file=vhal_20_file)
126
127generateHal20()
128