Steve Paik | a053ccb | 2016-07-14 14:56:54 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2016 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 | """Generate vns_policy.xml from vehicle.h""" |
| 18 | import os |
| 19 | import re |
| 20 | import sys |
| 21 | import datetime |
| 22 | |
| 23 | curYear = datetime.datetime.now().year |
| 24 | |
| 25 | |
| 26 | XML_HEADER = \ |
| 27 | """<!-- Copyright (C) """ + str(curYear) + """ The Android Open Source Project |
| 28 | |
| 29 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 30 | you may not use this file except in compliance with the License. |
| 31 | You may obtain a copy of the License at |
| 32 | |
| 33 | http://www.apache.org/licenses/LICENSE-2.0 |
| 34 | |
| 35 | Unless required by applicable law or agreed to in writing, software |
| 36 | distributed under the License is distributed on an "AS IS" BASIS, |
| 37 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 38 | See the License for the specific language governing permissions and |
| 39 | limitations under the License. |
| 40 | / |
| 41 | |
| 42 | //Autogenerated from vehicle.h using tools/vns_policy_gen.py. |
| 43 | //Do not modify manually. |
| 44 | --> |
| 45 | |
| 46 | <ALLOW> |
| 47 | <PROPERTY name="VEHICLE_PROPERTY_INVALID" value = "0x0"> |
| 48 | <!-- Property 0 write is used to allow hal mocking --> |
| 49 | <UID name="AID_SYSTEM" access="rw" value="1000"/> |
| 50 | <UID name="AID_AUDIOSERVER" access="r" value="1041"/> |
| 51 | </PROPERTY> |
| 52 | |
| 53 | """ |
| 54 | |
| 55 | XML_TRAIL = \ |
| 56 | """</ALLOW> |
| 57 | """ |
| 58 | |
| 59 | RE_PROPERTY_PATTERN = r'/\*\*(.*?)\*/\n\#define\s+VEHICLE_PROPERTY_(\S+)\s+(\(0x\S+\))' |
| 60 | AUDIOSERVER_RO_PARAMS = ['VEHICLE_PROPERTY_AUDIO_FOCUS'] |
| 61 | AUDIOSERVER_WO_PARAMS = ['VEHICLE_PROPERTY_INTERNAL_AUDIO_STREAM_STATE'] |
| 62 | NO_AUTO_GET_PARAMS = ['VEHICLE_PROPERTY_AUDIO_VOLUME', 'VEHICLE_PROPERTY_AUDIO_VOLUME_LIMIT'] |
| 63 | |
| 64 | class PropertyInfo(object): |
| 65 | def __init__(self, value, name): |
| 66 | self.value = value |
| 67 | self.name = name |
| 68 | self.type = "" |
| 69 | self.changeMode = "" |
| 70 | self.access = "" |
| 71 | self.unit = "" |
| 72 | self.startEnd = 0 # _START/END property |
| 73 | self.aid_audioserver = "" |
| 74 | self.no_auto_get = 0 |
| 75 | # Check if this property requires special handling |
| 76 | if name in AUDIOSERVER_RO_PARAMS: |
| 77 | self.aid_audioserver = 'r' |
| 78 | if name in AUDIOSERVER_WO_PARAMS: |
| 79 | self.aid_audioserver = 'w' |
| 80 | if name in NO_AUTO_GET_PARAMS: |
| 81 | self.no_auto_get = 1 |
| 82 | |
| 83 | def printProperties(props, f): |
| 84 | for p in props: |
| 85 | if p.startEnd == 0: |
| 86 | if p.no_auto_get == 0: |
| 87 | f.write(""" <PROPERTY name=\"""" + p.name + "\" value=\"" + p.value + "\">\n") |
| 88 | else: |
| 89 | f.write(""" <PROPERTY name=\"""" + p.name + "\" value=\"" + p.value + """" no_auto_get="true">\n""") |
| 90 | f.write(""" <UID name="AID_SYSTEM" access=\"""" + p.access + """" value="1000"/>\n""") |
| 91 | if p.aid_audioserver: |
| 92 | f.write(""" <UID name="AID_AUDIOSERVER" access=\"""" + p.aid_audioserver + """" value="1041"/>\n""") |
| 93 | f.write(" </PROPERTY>\n\n") |
| 94 | elif p.name == 'VEHICLE_PROPERTY_INTERNAL_START': |
| 95 | f.write("<!-- Internal Vehicle Properties -->\n\n") |
| 96 | |
| 97 | def main(argv): |
| 98 | # If user hasn't called build/envsetup.sh, use relative path |
| 99 | base_path = os.getenv('ANDROID_BUILD_TOP', "../../../..") |
| 100 | |
| 101 | # Parse vehicle.h file |
| 102 | vehicle_h_path = base_path + "/hardware/libhardware/include/hardware/vehicle.h" |
| 103 | #print vehicle_h_path |
| 104 | f = open(vehicle_h_path, 'r') |
| 105 | text = f.read() |
| 106 | f.close() |
| 107 | |
| 108 | # Parse vehicle_internal.h |
| 109 | vehicle_internal_h_path = base_path + "/packages/services/Car/libvehiclenetwork/include/vehicle-internal.h" |
| 110 | f = open(vehicle_internal_h_path, 'r') |
| 111 | text = text + f.read() |
| 112 | f.close() |
| 113 | |
| 114 | # Open the vns_policy.xml file |
| 115 | vns_policy_path = base_path + "/packages/services/Car/vns_policy/vns_policy.xml" |
| 116 | f = open(vns_policy_path, 'w') |
| 117 | |
| 118 | props = [] |
| 119 | property_re = re.compile(RE_PROPERTY_PATTERN, re.MULTILINE | re.DOTALL) |
| 120 | for match in property_re.finditer(text): |
| 121 | words = match.group(1).split() |
| 122 | name = "VEHICLE_PROPERTY_" + match.group(2) |
| 123 | value = match.group(3) |
| 124 | if (value[0] == "(" and value[-1] == ")"): |
| 125 | value = value[1:-1] |
| 126 | prop = PropertyInfo(value, name) |
| 127 | i = 0 |
| 128 | while i < len(words): |
| 129 | if words[i] == "@access": |
| 130 | i += 1 |
| 131 | if words[i].find("READ") > 0: |
| 132 | if words[i].find("WRITE") > 0: |
| 133 | prop.access = 'rw' |
| 134 | else: |
| 135 | prop.access = 'r' |
| 136 | else: |
| 137 | prop.access = 'w' |
| 138 | elif words[i] == "@range_start" or words[i] == "@range_end": |
| 139 | prop.startEnd = 1 |
| 140 | i += 1 |
| 141 | props.append(prop) |
| 142 | #print prop |
| 143 | |
| 144 | f.write(XML_HEADER) |
| 145 | printProperties(props, f) |
| 146 | f.write(XML_TRAIL) |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | main(sys.argv) |
| 150 | |