File: BusmasterRestbus.py

package info (click to toggle)
python-canmatrix 1.2~github-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,012 kB
  • sloc: xml: 30,201; python: 14,631; makefile: 31; sh: 7
file content (218 lines) | stat: -rwxr-xr-x 6,981 bytes parent folder | download
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
#!/usr/bin/env python2
# Copyright (c) 2013, Eduard Broecker
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that
# the following conditions are met:
#
#    Redistributions of source code must retain the above copyright notice, this list of conditions and the
#    following disclaimer.
#    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
#    following disclaimer in the documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

import math
from struct import *
import zipfile
import sys
sys.path.append('..')
import canmatrix.formats

import os

import glob
import string


def genString(string):
    retStr = "%c" % len(string)
    retStr += string
    return retStr


def genZeros(count):
    retStr = ""
    for i in range(count):
        retStr += "\x00"
    return retStr


def genSimulatonFile(nodes):
    # header
    retStr = "ff\xA6\x3F"
    retStr += genString("RBEI_FRAME")
    retStr += genString("CAN")
    retStr += genString("1.8.0")

    # count of nodes:
    retStr += "%c" % len(nodes)
    retStr += genZeros(4)

    # each node
    for (nodename, source) in list(nodes.items()):
        retStr += genString(source).encode('ascii')
        retStr += genString(nodename).encode('ascii')
        retStr += genZeros(10)

    # checksum
    temp = 0
    for c in retStr:
        ibyte = unpack('B', c)
        temp = (temp ^ ibyte[0])
    retStr = retStr[:-1] + "%c" % temp
    return retStr


def createNode(structNames, timedPrototypes, timedCallbacks):
    nodetemplate = """
/* This file is generated by BUSMASTER */
/* VERSION [1.1] */
/* BUSMASTER VERSION [1.8.0] */
/* PROTOCOL [CAN] */

/* Start BUSMASTER include header */
#include <Windows.h>
#include <CANIncludes.h>

/* End BUSMASTER include header */


/* Start BUSMASTER global variable */
"""

    nodetemplate2 = """
/* End BUSMASTER global variable */

/* Start BUSMASTER Function Prototype  */
"""
    return nodetemplate + structNames + nodetemplate2 + timedPrototypes + \
        "/* End BUSMASTER Function Prototype  */\n" + timedCallbacks


def genCallbacks(cycle, bId, db):
    botsch = db.frames.byId(bId).name
    callbacks = "/* Start BUSMASTER generated function - OnTimer_" + \
        botsch + "_" + str(cycle) + " */\n"
    callbacks += "void OnTimer_" + botsch + "_" + str(cycle) + "( )\n"
    callbacks += "{\n"

    canData = db.frames.byId(bId).attributes["GenMsgStartValue"][1:-2]
    dlc = math.floor(len(canData) / 2)
    callbacks += "    SendMsg(" + botsch + ");\n"
    callbacks += "\n} "
    callbacks += "/* End BUSMASTER generated function - OnTimer_" + \
        botsch + "_" + str(cycle) + " */\n\n"
    prototype = "GCC_EXTERN void GCC_EXPORT OnTimer_" + \
        botsch + "_" + str(cycle) + "( );\n"
    structNames = "STCAN_MSG " + botsch + \
        " = { " + hex(bId) + ", 0, 0, " + \
        str(math.floor(len(canData) / 2)) + ", 1,"
    for i in range(dlc):
        structNames += " 0x" + canData[i * 2:i * 2 + 2]
        if i < dlc - 1:
            structNames += ", "
    exports = "OnTimer_" + botsch + "_" + str(cycle) + "\n"

    structNames += "};\n"

    return structNames, prototype, callbacks, exports

exportTemplate = """EXPORTS
vSetEnableLoggingProcAddress
vSetDisableLoggingProcAddress
vSetWriteToLogFileProcAddress
vSetConnectProcAddress
vSetDisconnectProcAddress
vSetGoOnlineProcAddress
vSetGoOfflineProcAddress
vSetStartTimerProcAddress
vSetStopTimerProcAddress
vSetSetTimerValProcAddress
vSetEnableMsgHandlersProcAddress
vSetEnableErrorHandlersProcAddress
vSetEnableKeyHandlersProcAddress
vSetEnableDisableMsgTxProcAddress
vSetSendMsgProcAddress
vSetGetDllHandleProcAddress
vSetTraceProcAddress
vSetResetControllerProcAddress
bGetProgramVersion
vSetKeyPressed
vSetGetMessageName
vSetTimeNow
vSetGetFirstCANdbName

"""


def ticker_ecus(db, dbcname):
    nodeList = {}
    zf = zipfile.ZipFile(dbcname + '_Simulation.zip',
                         mode='w',
                         compression=zipfile.ZIP_DEFLATED,
                         )

    MyBuList = []

    for bu in db.ecus:
        if bu.name not in MyBuList:
            MyBuList.append(bu.name)  # no duplicate Nodes
        else:
            continue
        bu._cycles = {}
        for frame in db.frames:
            if bu.name in frame.transmitters:
                if frame.effective_cycle_time != 0 and "GenMsgStartValue" in frame.attributes:
                    data = frame.attributes["GenMsgStartValue"][1:-2]
                    dlc = (math.floor(len(data) / 2))
                    cycleTime = frame.effective_cycle_time
                    if float(cycleTime) > 0:
                        if cycleTime in bu._cycles:
                            bu._cycles[cycleTime].append(frame.arbitration_id.id)
                        else:
                            bu._cycles[cycleTime] = [frame.arbitration_id.id]
        nodeList[bu.name] = bu.name + ".cpp"

        timedPrototypes = ""
        timedCallbacks = ""
        structNames = ""
        exports = ""
        for cycle in bu._cycles:
            for frame in bu._cycles[cycle]:
                (tempstructNames, tempPrototypes, tempCallbacks,
                 tempExports) = genCallbacks(cycle, frame, db)
                structNames += tempstructNames
                timedPrototypes += tempPrototypes
                timedCallbacks += tempCallbacks
                exports += tempExports

        nodeString = createNode(structNames, timedPrototypes, timedCallbacks)

        zf.writestr(bu.name + ".cpp", nodeString)
        zf.writestr(bu.name + ".def", exportTemplate + exports)
    zf.writestr(dbcname + '.sim', genSimulatonFile(nodeList))
    zf.close()


def main():
    if len(sys.argv) < 3:
        sys.stderr.write('Usage: sys.argv[0] import-file export-file\n')
        sys.stderr.write('import-file: *.dbc|*.dbf|*.kcd\n')
        sys.stderr.write('export-file: somefile.zip\n')
        sys.exit(1)

    infile = sys.argv[1]
    outfile = os.path.splitext(sys.argv[2])[0]

    db = next(iter(canmatrix.formats.loadp(infile).values()))
    ticker_ecus(db, outfile)

main()