aboutsummaryrefslogtreecommitdiff
path: root/libvehiclenetwork/tool/vehiclehal_code_gen.py
blob: dfabd7dccd420a555c2644341ab46c9ffeb46c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Generate Java code from vehicle.h"""
import os
import re
import sys

JAVA_HEADER = \
"""
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//Autogenerated from vehicle.h using libvehiclenetwork/tool/vehicle_code_gen.py.
//Do not modify manually.

package com.android.car.vehiclenetwork;

public class VehicleNetworkConsts {
"""

JAVA_TRAIL = \
"""
}
"""

RE_PROPERTY_PATTERN = r'/\*\*(.*?)\*/\n\#define\s+VEHICLE_PROPERTY_(\S+)\s+(\(0x\S+\))'
RE_ENUM_PATTERN = r'enum\s+(\S+)\s+\{\S*(.*?)\}'
RE_ENUM_ENTRY_PATTERN = r'(\S+)\s*=\s*(.*?)[,\n]'
RE_AUDIO_EXT_ROUTING_PATTERN = r'\n\#define\s+VEHICLE_PROPERTY_AUDIO_EXT_ROUTING_SOURCE_(\S+)\s+\"(\S+)\"'

class PropertyInfo(object):
  def __init__(self, value, name):
    self.value = value
    self.name = name
    self.type = ""
    self.changeMode = ""
    self.access = ""
    self.unit = ""
    self.startEnd = 0 # _START/END property

  def __str__(self):
    r = ["value:" + self.value]
    r.append("name:" + self.name)
    if self.type != "":
      r.append("type:" + self.type)
    if self.changeMode != "":
      r.append("changeMode:" + self.changeMode)
    if self.access != "":
      r.append("access:" + self.access)
    if self.unit != "":
      r.append("unit:" + self.unit)
    return " ".join(r)

class EnumInfo(object):
  def __init__(self, name):
    self.name = name
    self.enums = [] #(name, value) tuple
  def addEntry(self, name, value):
    self.enums.append((name, value))
  def __str__(self):
    r = [self.name + "\n"]
    for e in self.enums:
      r.append("  " + e[0] + ":" + e[1] + "\n")
    return ''.join(r)

def toJavaStyleName(name):
  # do not convert if 1st letter is already upper
  if name[0].isupper():
    return name
  words = name.split("_")
  #print words
  for i in range(len(words)):
    w = words[i]
    w = w[0].upper() + w[1:]
    words[i] = w
  return ''.join(words)

JAVA_INT_DEF = "public static final int "
def printProperties(props):
  for p in props:
    print JAVA_INT_DEF + p.name + " = " + p.value + ";"

  #now impement getVehicleValueType
  print \
"""public static int getVehicleValueType(int property) {
switch (property) {"""
  for p in props:
    if p.type != "":
      print "case " + p.name + ": return VehicleValueType." + p.type + ";"
  print \
"""default: return VehicleValueType.VEHICLE_VALUE_TYPE_SHOUD_NOT_USE;
}
}
"""
  #now implement getVehiclePropertyName
  print \
"""public static String getVehiclePropertyName(int property) {
switch (property) {"""
  for p in props:
    if (p.startEnd == 0):
      print "case " + p.name + ': return "' + p.name +     '";'
  print \
"""default: return "UNKNOWN_PROPERTY";
}
}
"""
  #now implement getVehicleChangeMode
  print \
"""public static int[] getVehicleChangeMode(int property) {
switch (property) {"""
  for p in props:
    if p.changeMode != "":
      modes = p.changeMode.split('|')
      modesString = []
      for m in modes:
        modesString.append("VehiclePropChangeMode." + m)
      print "case " + p.name + ": return new int[] { " + " , ".join(modesString) + " };"
  print \
"""default: return null;
}
}
"""
  #now implement getVehicleAccess
  print \
"""public static int[] getVehicleAccess(int property) {
switch (property) {"""
  for p in props:
    if p.access != "":
      accesses = p.access.split('|')
      accessesString = []
      for a in accesses:
        accessesString.append("VehiclePropAccess." + a)
      print "case " + p.name + ": return new int[] { " + " , ".join(accessesString) + " };"
  print \
"""default: return null;
}
}
"""

def printEnum(e):
  print "public static class " + toJavaStyleName(e.name) + " {"
  for entry in e.enums:
    print JAVA_INT_DEF + entry[0] + " = " + entry[1] + ";"
  #now implement enumToString
  print \
"""public static String enumToString(int v) {
switch(v) {"""
  valueStore = []
  for entry in e.enums:
    # handling enum with the same value. Print only 1st one.
    if valueStore.count(entry[1]) == 0:
      valueStore.append(entry[1])
      print "case " + entry[0] + ': return "' + entry[0] + '";'
  print \
"""default: return "UNKNOWN";
}
}
}
"""

def printEnums(enums):
  for e in enums:
    printEnum(e)

def printExtAudioRoutingSources(audio_ext_routing):
  for r in audio_ext_routing:
    print "public static final String VEHICLE_PROPERTY_AUDIO_EXT_ROUTING_SOURCE_" + r + ' = "' + r + '";'

def main(argv):
  vehicle_h_path = os.path.dirname(os.path.abspath(__file__)) + "/../../../../../hardware/libhardware/include/hardware/vehicle.h"
  #print vehicle_h_path
  f = open(vehicle_h_path, 'r')
  text = f.read()
  f.close()
  vehicle_internal_h_path = os.path.dirname(os.path.abspath(__file__)) + "/../include/vehicle-internal.h"
  f = open(vehicle_internal_h_path, 'r')
  text = text + f.read()
  f.close()

  props = []
  property_re = re.compile(RE_PROPERTY_PATTERN, re.MULTILINE | re.DOTALL)
  for match in property_re.finditer(text):
    words = match.group(1).split()
    name = "VEHICLE_PROPERTY_" + match.group(2)
    value = match.group(3)
    if (value[0] == "(" and value[-1] == ")"):
      value = value[1:-1]
    prop = PropertyInfo(value, name)
    i = 0
    while i < len(words):
      if words[i] == "@value_type":
        i += 1
        prop.type = words[i]
      elif words[i] == "@change_mode":
        i += 1
        prop.changeMode = words[i]
      elif words[i] == "@access":
        i += 1
        prop.access = words[i]
      elif words[i] == "@unit":
        i += 1
        prop.unit = words[i]
      elif words[i] == "@range_start" or words[i] == "@range_end":
        prop.startEnd = 1
      i += 1
    props.append(prop)
    #for p in props:
    #  print p

  enums = []
  enum_re = re.compile(RE_ENUM_PATTERN, re.MULTILINE | re.DOTALL)
  enum_entry_re = re.compile(RE_ENUM_ENTRY_PATTERN, re.MULTILINE)
  for match in enum_re.finditer(text):
    name = match.group(1)
    info = EnumInfo(name)
    for match_entry in enum_entry_re.finditer(match.group(2)):
      valueName = match_entry.group(1)
      value = match_entry.group(2)
      #print valueName, value
      if value[-1] == ',':
        value = value[:-1]
      info.addEntry(valueName, value)
    enums.append(info)
  #for e in enums:
  #  print e

  audio_ext_routing = []
  audio_ext_routing_re = re.compile(RE_AUDIO_EXT_ROUTING_PATTERN, re.MULTILINE | re.DOTALL)
  for match in audio_ext_routing_re.finditer(text):
    #print match
    name = match.group(1)
    value = match.group(2)
    if name != value:
      print "Warning, AUDIO_EXT_ROUTING_SOURCE_" + name + " does not match " + value
    else:
      audio_ext_routing.append(name)

  print JAVA_HEADER
  printProperties(props)
  print "\n\n"
  printExtAudioRoutingSources(audio_ext_routing)
  print "\n\n"
  printEnums(enums)
  print JAVA_TRAIL

if __name__ == '__main__':
  main(sys.argv)