File: qdltconnection.cpp

package info (click to toggle)
dlt-viewer 2.23.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,432 kB
  • sloc: cpp: 27,832; ansic: 4,454; xml: 491; sh: 154; makefile: 75
file content (260 lines) | stat: -rw-r--r-- 6,517 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
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
/**
 * @licence app begin@
 * Copyright (C) 2011-2012  BMW AG
 *
 * This file is part of COVESA Project Dlt Viewer.
 *
 * Contributions are licensed to the COVESA Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> 2011-2012
 *
 * \file qdlt.cpp
 * For further information see http://www.covesa.global/.
 * @licence end@
 */

#include <QtDebug>

#include "qdlt.h"

extern "C"
{
#include "dlt_common.h"
}

QDltConnection::QDltConnection()
{
    sendSerialHeader = false;
    syncSerialHeader = false;

    clear();
}

QDltConnection::~QDltConnection()
{

}

void QDltConnection::setSendSerialHeader(bool _sendSerialHeader)
{
    sendSerialHeader = _sendSerialHeader;
}

bool QDltConnection::getSendSerialHeader() const
{
    return sendSerialHeader;
}

void QDltConnection::setSyncSerialHeader(bool _syncSerialHeader)
{
    syncSerialHeader = _syncSerialHeader;
}

bool QDltConnection::getSyncSerialHeader() const
{
    return syncSerialHeader;
}

void QDltConnection::clear()
{
    data.clear();
    dataView.align(data);
    bytesReceived = 0;
    bytesError = 0;
    syncFound = 0;
    messageCounter = 0;
}

void QDltConnection::add(const QByteArray &bytes)
{
    bytesReceived += bytes.size();

    data = dataView + bytes;

    dataView.align(data);
}

bool QDltConnection::parseDlt(QDltMsg &msg)
{
    /* if sync to serial header search for header */
    int found = 0;
    int firstPos = 0;
    int secondPos = 0;

    char lastFound = 0;

    /* Use primitive buffer for faster access */
    int cbuf_sz = dataView.size();
    const char *cbuf = dataView.constData();

    /* find marker in buffer */
    for(int num=0;num<cbuf_sz;num++) {
        if(cbuf[num] == 'D')
        {
            lastFound = 'D';
        }
        else if(lastFound == 'D' && cbuf[num] == 'L')
        {
            lastFound = 'L';
        }
        else if(lastFound == 'L' && cbuf[num] == 'S')
        {
            lastFound = 'S';
        }
        else if(lastFound == 'S' && cbuf[num] == 0x01)
        {
            /* header found */
            found++;
            if(found==1)
            {
                firstPos = num+1;
                syncFound++;
            }
            if(found==2)
            {
                secondPos = num+1;
                break;
            }
            lastFound = 0;
        }
        else
        {
            lastFound = 0;
        }
        if((!syncSerialHeader) && num==3)
            break;
    }

    if(syncSerialHeader && !found)
    {
        /* complete sync header not found */
        if(!lastFound)
        {
            /* clear buffer if even not start of sync header found */
            bytesError += dataView.size();
            dataView.clear();
        }
        return false;
    }

    //qDebug() << "found " << found << " firstPos " << firstPos << " secondPos " << secondPos;

    if(found==2)
    {
        /* two sync headers found */
        /* try to read msg */
        if(!msg.setMsg(dataView.mid(firstPos,secondPos-firstPos-4),false))
        {
            /* no valid msg found, perhaps to short */
            dataView.advance(secondPos-4);
            /* errors found */
            bytesError += secondPos-4;
            return false;
        }
        else
        {
            /* msg read successful */
            dataView.advance(secondPos-4);
            if(firstPos>4)
                /* errors found */
                bytesError += firstPos-4;
            return true;
        }

    }

    if(firstPos>4)
        /* errors found */
        bytesError += firstPos-4;

    /* try to read msg */
    if(!msg.setMsg(dataView.mid(firstPos),false))
    {
        /* no complete msg found */
        /* perhaps not completely received */
        /* check valid size */
        if(dataView.size()>DLT_MAX_MESSAGE_LEN)
        {
            /* size exceeds max DLT message size */
            /* clear buffer */
            /* errors found */
            bytesError += dataView.size();
            dataView.clear();
        }
        return false;
    }

    /* msg read successful */
    dataView.advance(firstPos+msg.getHeaderSize()+msg.getPayloadSize());
    return true;
}

bool QDltConnection::parseAscii(QDltMsg &msg)
{
    bool success = false;

    /* Use primitive buffer for faster access */
    int cbuf_sz = dataView.size();
    const char *cbuf = dataView.constData();

    /* find end of line in buffer */
    for(int num=0;num<cbuf_sz;num++) {
        if(cbuf[num] == '\r' || cbuf[num] == '\n')
        {
            // end of line found

            // check if line is empty, do not store empty lines
            if(num!=0)
            {
                // set parameters of DLT message to be generated
                msg.clear();
                msg.setEcuid("");
                msg.setApid("SER");
                msg.setCtid("ASC");
                msg.setMode(QDltMsg::DltModeVerbose);
                msg.setType(QDltMsg::DltTypeLog);
                msg.setSubtype(QDltMsg::DltLogInfo);
                msg.setMessageCounter(messageCounter++);
                msg.setNumberOfArguments(1);

                // add one argument as String
                QDltArgument arg;
                arg.setTypeInfo(QDltArgument::DltTypeInfoStrg);
                arg.setEndianness(QDltArgument::DltEndiannessLittleEndian);
                arg.setOffsetPayload(0);
                arg.setData(QByteArray(cbuf,num)+QByteArray("",1));
                msg.addArgument(arg);

                // generate binary payload and header of DLT message
                msg.genMsg();

                // succesful found a new line to be written as DLT message
                success = true;
            }

            // remove parsed line from buffer
            if( (num < (cbuf_sz-1)) && (cbuf[num+1] == '\n' || cbuf[num+1] == '\r'))
            {
                // \n and \r found, remove two additional characters
                dataView.advance(num+2);
            }
            else
            {
                // only \n or \r found, remove only one character
                dataView.advance(num+1);
            }

            // msg read successful
            return success;
        }
    }

    // no message found
    return success;
}