forked from COVESA/dlt-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdltsegmentedmsg.cpp
205 lines (176 loc) · 5.41 KB
/
qdltsegmentedmsg.cpp
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
/**
* @licence app begin@
* Copyright (C) 2011-2014 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 <[email protected]> 2011-2012
*
* \file qdltsegmentedmsg.cpp
* For further information see http://www.covesa.global/.
* @licence end@
*/
#include <QByteArray>
#include "qdlt.h"
#include "qdltsegmentedmsg.h"
QDltSegmentedMsg::QDltSegmentedMsg()
{
handle = 0;
size = 0;
chunks = 0;
chunkSize = 0;
chunksAdded = 0;
state = DltSegStart;
}
int QDltSegmentedMsg::add(QDltMsg &msg)
{
QDltArgument argument;
if(msg.getNumberOfArguments()<1)
{
error = "Invalid number of arguments in start segment message";
return -1;
}
msg.getArgument(0,argument);
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoStrg)
{
error = "Invalid Type in start segment message";
return -1;
}
QString str = argument.getValue().toString();
if(str=="NWST")
{
// start of segmented message
if(msg.getNumberOfArguments()!=6)
{
error = "Invalid number of arguments in start segment message";
return -1;
}
msg.getArgument(1,argument); // get handle
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in start segment message";
return -1;
}
handle = argument.getValue().toUInt();
msg.getArgument(2,argument); // get header
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoRawd)
{
error = "Invalid Type in start segment message";
return -1;
}
header = argument.getData();
msg.getArgument(3,argument); // get size
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in start segment message";
return -1;
}
size = argument.getValue().toUInt();
msg.getArgument(4,argument); // get chunks
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in start segment message";
return -1;
}
chunks = argument.getValue().toUInt();
msg.getArgument(5,argument); // get chunk size
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in start segment message";
return -1;
}
chunkSize = argument.getValue().toUInt();
if(state != DltSegStart)
{
error = "Start segment received several times";
return -1;
}
state = DltSegChunk;
payload.resize(size);
}
else if(str=="NWCH")
{
// chunk of segmented message
if(msg.getNumberOfArguments()!=4)
{
error = "Invalid number of arguments in chunk segment message";
return -1;
}
msg.getArgument(1,argument); // get handle
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in chunk segment message";
return -1;
}
if(handle != argument.getValue())
{
error = "Handle unequal in chunk message";
return -1;
}
msg.getArgument(2,argument); // get sequence
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in chunk segment message";
return -1;
}
uint32_t sequence = argument.getValue().toUInt();
if(sequence>=chunks)
{
error = QString("Sequence bigger than number of chunks: Sequence = %1, Chunks = %2").arg(sequence).arg(chunks);
return -1;
}
msg.getArgument(3,argument); // get chunk
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoRawd)
{
error = "Invalid Type in chunk segment message";
return -1;
}
payload.replace(sequence*chunkSize,chunkSize,argument.getData());
chunksAdded += 1;
}
else if(str=="NWEN")
{
// end of segmented message
if(msg.getNumberOfArguments()!=2)
{
error = "Invalid number of arguments in end segment message";
return -1;
}
msg.getArgument(1,argument); // get handle
if(argument.getTypeInfo()!=QDltArgument::DltTypeInfoUInt)
{
error = "Invalid Type in end segment message";
return -1;
}
if(handle != argument.getValue())
{
error = "Invalid handle in end chunk";
return -1;
}
if(state != DltSegChunk)
{
error = "End chunk received without start chunk";
return -1;
}
if(chunksAdded!=chunks)
{
error = QString("Number of chunks mismatch: Received = %1, Total = %2").arg(chunksAdded).arg(chunks);
return -1;
}
state = DltSegFinish;
}
else
{
error = "Invalid segmented message string";
return -1;
}
return 0;
}