|
| 1 | +#!/usr/bin/env python |
| 2 | +#Copyright (c) 2013, Eduard Broecker |
| 3 | +#All rights reserved. |
| 4 | +# |
| 5 | +#Redistribution and use in source and binary forms, with or without modification, are permitted provided that |
| 6 | +# the following conditions are met: |
| 7 | +# |
| 8 | +# Redistributions of source code must retain the above copyright notice, this list of conditions and the |
| 9 | +# following disclaimer. |
| 10 | +# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 11 | +# following disclaimer in the documentation and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED |
| 14 | +#WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 15 | +#PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 16 | +#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 17 | +#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 18 | +#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 19 | +#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 20 | +#DAMAGE. |
| 21 | + |
| 22 | +# |
| 23 | +# this script imports sym-files to a canmatrix-object |
| 24 | +# sym-files are the can-matrix-definitions of peak tools |
| 25 | +# |
| 26 | +#TODO : the signalname should be unique. If same signal-name is used in different mux-ids, the signal-name should be unified. maybe this is an dbc-export-issue not an sym-import-issue |
| 27 | +#TODO : I don't know what to do with the "/p"-Options in the signal-definition |
| 28 | +#TODO : what is the differenze between SEND and SENDRECEIVE ? |
| 29 | +# currently only for FormatVersion=5.0 |
| 30 | +#TODO: support for title Title="AFE_CAN_ID0" |
| 31 | + |
| 32 | +from canmatrix import * |
| 33 | +import re |
| 34 | +import codecs |
| 35 | + |
| 36 | + |
| 37 | +def importSym(filename): |
| 38 | + class Mode: |
| 39 | + glob, enums, send, sendReceive = range(4) |
| 40 | + mode = Mode.glob |
| 41 | + valueTables = {} |
| 42 | + |
| 43 | + frameName = "" |
| 44 | + frame = None |
| 45 | + |
| 46 | + db = CanMatrix() |
| 47 | + db.addFrameDefines("GenMsgCycleTime", 'INT 0 65535') |
| 48 | + db.addSignalDefines("GenSigStartValue", 'FLOAT -3.4E+038 3.4E+038') |
| 49 | + f = open(filename) |
| 50 | + |
| 51 | + for line in f: |
| 52 | + l = line.strip() |
| 53 | + #ignore emty line: |
| 54 | + if l.__len__() == 0: |
| 55 | + continue |
| 56 | + |
| 57 | + #switch mode: |
| 58 | + if line[0:7] == "{ENUMS}": |
| 59 | + mode = Mode.enums |
| 60 | + continue; |
| 61 | + if line[0:6] == "{SEND}": |
| 62 | + mode = Mode.send |
| 63 | + continue; |
| 64 | + if line[0:13] == "{SENDRECEIVE}": |
| 65 | + mode = Mode.sendReceive |
| 66 | + continue; |
| 67 | + |
| 68 | + |
| 69 | + if mode == Mode.glob: |
| 70 | + continue |
| 71 | + elif mode == Mode.enums: |
| 72 | + line = line.strip() |
| 73 | + if line.startswith('enum'): |
| 74 | + while not line[5:].strip().endswith(')'): |
| 75 | + line = line.split('//')[0] |
| 76 | + line += f.next().strip() |
| 77 | + line = line.split('//')[0] |
| 78 | + tempArray = line[5:].replace(')','').split('(') |
| 79 | + valtabName = tempArray[0] |
| 80 | + tempArray = tempArray[1].split(',') |
| 81 | + tempValTable = {} |
| 82 | + for entry in tempArray: |
| 83 | + tempValTable[entry.split('=')[0].strip()] = entry.split('=')[1].replace('"','').strip() |
| 84 | + valueTables[valtabName] = tempValTable |
| 85 | + |
| 86 | + elif mode == Mode.send or mode == Mode.sendReceive: |
| 87 | + if line.startswith('['): |
| 88 | + multiplexor = None |
| 89 | + # found new frame: |
| 90 | + if frameName != line.replace('[','').replace(']','').strip(): |
| 91 | + frameName = line.replace('[','').replace(']','').strip() |
| 92 | + if frame is not None: |
| 93 | + if len(multiplexValTable) > 0: |
| 94 | + frame.signalByName(frame._name + "_MUX")._values = multiplexValTable |
| 95 | + db._fl.addFrame(frame) |
| 96 | + |
| 97 | + frame = Frame(0,frameName,8,None) |
| 98 | + multiplexValTable = {} |
| 99 | + |
| 100 | + #key value: |
| 101 | + elif line.startswith('Var') or line.startswith('Mux'): |
| 102 | + tmpMux = line[:3] |
| 103 | + line = line[4:] |
| 104 | + comment = "" |
| 105 | + indexOffset = 1 |
| 106 | + if tmpMux == "Mux": |
| 107 | + indexOffset = 0 |
| 108 | + |
| 109 | + if '//' in line: |
| 110 | + comment = line.split('//')[1].strip() |
| 111 | + line = line.split('//')[0] |
| 112 | + tempArray = line.strip().split(' ') |
| 113 | + sigName = tempArray[0] |
| 114 | + if indexOffset == 1 and tempArray[1][:8] == "unsigned": |
| 115 | + valuetype = "+" |
| 116 | + else: |
| 117 | + valuetype = "-" |
| 118 | + |
| 119 | + startBit = int(tempArray[indexOffset+1].split(',')[0]) |
| 120 | + signalLength = int(tempArray[indexOffset+1].split(',')[1]) |
| 121 | + intel = 1 |
| 122 | + unit = "" |
| 123 | + factor = 1 |
| 124 | + max = 1 |
| 125 | + min = 0 |
| 126 | + startValue = 0 |
| 127 | + offset = 0 |
| 128 | + valueTableName = None |
| 129 | + |
| 130 | + if tmpMux == "Mux": |
| 131 | + multiplexor= tempArray[2] |
| 132 | + multiplexValTable[multiplexor] = sigName |
| 133 | + indexOffset = 2 |
| 134 | + |
| 135 | + for switch in tempArray[indexOffset+2:]: |
| 136 | + if switch == "-m": |
| 137 | + intel = 0 |
| 138 | + startByte = startBit / 8 |
| 139 | + startBit = startBit % 8 |
| 140 | + startBit = (7-startBit) |
| 141 | + startBit += startByte * 8 |
| 142 | + elif switch == "-h": |
| 143 | + #hexadecimal output - not supported |
| 144 | + pass |
| 145 | + elif switch.startswith('/'): |
| 146 | + if switch[1:].split(':')[0] == 'u': |
| 147 | + unit = switch[1:].split(':')[1] |
| 148 | + elif switch[1:].split(':')[0] == 'f': |
| 149 | + factor = switch[1:].split(':')[1] |
| 150 | + elif switch[1:].split(':')[0] == 'd': |
| 151 | + startValue = switch[1:].split(':')[1] |
| 152 | + elif switch[1:].split(':')[0] == 'p': |
| 153 | + pass |
| 154 | + #TODO /p ??? |
| 155 | + elif switch[1:].split(':')[0] == 'o': |
| 156 | + offset = switch[1:].split(':')[1] |
| 157 | + elif switch[1:].split(':')[0] == 'e': |
| 158 | + valueTableName = switch[1:].split(':')[1] |
| 159 | + elif switch[1:].split(':')[0] == 'max': |
| 160 | + max = switch[1:].split(':')[1] |
| 161 | + elif switch[1:].split(':')[0] == 'min': |
| 162 | + min = switch[1:].split(':')[1] |
| 163 | +# else: |
| 164 | +# print switch |
| 165 | +# else: |
| 166 | +# print switch |
| 167 | + startValue = float(startValue) / float(factor) |
| 168 | + if tmpMux == "Mux": |
| 169 | + signal = frame.signalByName(frameName + "_MUX") |
| 170 | + if signal == None: |
| 171 | + signal = Signal(frameName + "_MUX", startBit, signalLength, intel, valuetype, factor, offset, min, max, unit, "", 'Multiplexor') |
| 172 | + frame.addSignal(signal) |
| 173 | + |
| 174 | + else: |
| 175 | + signal = Signal(sigName, startBit, signalLength, intel, valuetype, factor, offset, min, max, unit, "", multiplexor) |
| 176 | + if valueTableName is not None: |
| 177 | + signal._values = valueTables[valueTableName] |
| 178 | + signal.addAttribute("GenSigStartValue", str(startValue)) |
| 179 | + frame.addSignal(signal) |
| 180 | + #variable processing |
| 181 | + elif line.startswith('ID'): |
| 182 | + comment = "" |
| 183 | + if '//' in line: |
| 184 | + comment = line.split('//')[1].strip() |
| 185 | + line = line.split('//')[0] |
| 186 | + frame._Id = int(line.split('=')[1].strip()[:-1], 16) |
| 187 | + frame.addComment(comment) |
| 188 | + elif line.startswith('Type'): |
| 189 | + if line.split('=')[1][:8] == "Extended": |
| 190 | + frame._extended = 1 |
| 191 | + elif line.startswith('DLC'): |
| 192 | + frame._Size = int(line.split('=')[1]) |
| 193 | + |
| 194 | + elif line.startswith('CycleTime'): |
| 195 | + frame.addAttribute("GenMsgCycleTime", line.split('=')[1].strip()) |
| 196 | +# else: |
| 197 | +# print line |
| 198 | +# else: |
| 199 | +# print "Unrecocniced line: " + l + " (%d) " % i |
| 200 | + if frame is not None: |
| 201 | + db._fl.addFrame(frame) |
| 202 | + |
| 203 | + return db |
0 commit comments