aboutsummaryrefslogtreecommitdiff
path: root/printer/src/mipi_syst_message.cpp
blob: f67c2b656a4f6911f9dc02ec5ccb0104e52d4388 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 Copyright (c) 2018, MIPI Alliance, Inc. 
 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.

 * Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

 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
 OWNER 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.
*/

/*
 * Contributors:
 * Norbert Schulz (Intel Corporation) - Initial API and implementation
 */

#include<sstream>
#include <iomanip>
#include "mipi_syst_message.h"
#include "mipi_syst_collateral.h"
#include "mipi_syst_printf.h"

MIPI_SYST_NAMESPACE_BEGIN

// Code for storing and printing out decoded message content
//

void message::setLocAddr32(uint32_t addr)
{
	m_loc.tag |= location_type::ADDRESS32;
	m_loc.tag &= ~location_type::ADDRESS64;
	m_loc.address = addr;
}

void message::setLocAddr64(uint64_t addr)
{
	m_loc.tag |= location_type::ADDRESS64;
	m_loc.tag &= ~location_type::ADDRESS32;
	m_loc.address = addr;
}

void message::setLocFileLine(uint32_t file, uint32_t line)
{
	m_loc.tag |= location_type::IDANDLINE;
	m_loc.file = file;
	m_loc.line = line;
}

// Check type for short message
//
bool message::isShort(message::header hdr) {
	switch (hdr.field.type) {
	case message::type::SHORT32:
	case message::type::SHORT64:
		return true;

	case  message::type::BUILD:
		switch (hdr.field.subtype) {
		case message::subtype_build::BUILD_COMPACT32:
		case message::subtype_build::BUILD_COMPACT64:
			return true;
		}
	}
	return false;
}


// Pretty print decode status value
//
static const char * status2string(const message& msg)
{
	switch (msg.getState()) {
	case message::decode_state::OK:
		return "OK";
	case message::decode_state::UNKNOWN_TYPE:
		return "UNKNOWN_TYPE";
	case message::decode_state::TOO_SHORT:
		return "TOO_SHORT";
	case message::decode_state::CHECKSUM_ERROR:
		return "CHECKSUM_ERROR";
	case message::decode_state::MISSING_COLLATERAL:
		return "MISSING_COLLATERAL";
	default:
		return "UNKNOWN_STATE";
	}
}

// Pretty print sevrity values
//
static const char * severity2string[] = {
	"MAX",
	"FATAL",
	"ERROR",
	"WARNING",
	"INFO",
	"USER1",
	"USER2",
	"DEBUG"
};

// Pretty print type:subtype values
//
static const std::string type2string(
	message::header hdr,
	const collateral * coll)
{
	static const char * typenames[] = {
		"BUILD",
		"SHORT32",
		"STRING",
		"CATALOG",
		"UNKNOWN(4)",
		"UNKNOWN(5)",
		"RAW",
		"SHORT64",
		"CLOCK",
		"UNKNOWN(9)",
		"UNKNOWN(10)",
		"UNKNOWN(11)",
		"UNKNOWN(12)",
		"UNKNOWN(13)",
		"UNKNOWN(14)",
		"UNKNOWN(15)",
	};

	uint32_t type(hdr.field.type);
	std::stringstream sstr;

	sstr << typenames[type];

	// compute subtype string
	//
	std::string subtype("??");

	switch (type) {
	case message::type::SHORT32:
	case message::type::SHORT64:
		subtype.clear(); // no subtype
		break;

	case message::type::BUILD:
		switch (hdr.field.subtype) {
		case  message::subtype_build::BUILD_LONG:
			subtype = "LONG";
			break;
		case  message::subtype_build::BUILD_COMPACT32:
			subtype = "COMPACT32";
			break;
		case  message::subtype_build::BUILD_COMPACT64:
			subtype = "COMPACT64";
			break;
		}
		break;
	case  message::type::STRING:
		switch (hdr.field.subtype) {
		case  message::subtype_string::STRING_GENERIC:
			subtype = "GENERIC";
			break;
		case  message::subtype_string::STRING_FUNCTIONENTER:
			subtype = "ENTER";
			break;
		case  message::subtype_string::STRING_FUNCTIONEXIT:
			subtype = "EXIT";
			break;
		case  message::subtype_string::STRING_INVALIDPARAM:
			subtype = "INVPARAM";
			break;
		case  message::subtype_string::STRING_ASSERT:
			subtype = "ASSERT";
			break;
		case  message::subtype_string::STRING_PRINTF_32:
			subtype = "PRINTF32";
			break;
		case  message::subtype_string::STRING_PRINTF_64:
			subtype = "PRINTF64";
			break;
		}
		break;
	case  message::type::CATALOG:
		switch (hdr.field.subtype) {
		case  message::subtype_catalog::CATALOG_ID32_P32:
			subtype = "ID32P32";
			break;
		case  message::subtype_catalog::CATALOG_ID64_P32:
			subtype = "ID64P32";
			break;
		case  message::subtype_catalog::CATALOG_ID32_P64:
			subtype = "ID32P64";
			break;
		case  message::subtype_catalog::CATALOG_ID64_P64:
			subtype = "ID64P64";
			break;
		}
		break;
	case  message::type::RAW:
	{
		const std::string * name(nullptr);
		if (coll && (name = coll->getWriteType(hdr.field.subtype)) != nullptr)
		{
			std::stringstream dest;
			hostPrintf(dest, *name, hdr.field.subtype, std::vector<int>());
			subtype = dest.str();
		} else {
			subtype = std::to_string(hdr.field.subtype);
		}
	} break;

	case  message::type::CLOCK:
		switch (hdr.field.subtype) {
		case  message::subtype_clock::CLOCK_SYNC:
			subtype = "SYNC";
			break;
		}
		break;
	}

	if (!subtype.empty()) {
		sstr << ':' << subtype;
	}

	return sstr.str();
}

// CSV double-quote escaping, replace " with "" and eliminate newlines
//
void csvQuoteString(std::ostream& os, const std::string& s)
{
	for (auto c : s) {
		if (c == '\n') {
			os << ' ';   // newline not supported in CSV
		} else if (c == '"') {
			os << c << c;
		} else {
			os << c;
		}
	}
}

// location information printing
//
std::string location2string(
	const message::location& loc,
	const collateral * collateral)
{
	bool hasAddress(0!=(loc.tag & (message::ADDRESS32 | message::ADDRESS32)));
	bool hasFileLine(0 != (loc.tag & message::IDANDLINE));

	std::stringstream sstr;

	if (hasFileLine) {
		const std::string * fileName(
			collateral != nullptr ?
				collateral->getSourceFile(loc.file) : nullptr);

		if (fileName != nullptr) {
			sstr << (*fileName) << ':' << loc.line;
		} else {
			sstr << loc.file << ':' << loc.line;
		}
		if (hasAddress) sstr << ' ';
	}

	if (hasAddress) {
		if (loc.tag & message::ADDRESS32) {
			sstr << toHexValue((uint32_t)loc.address);
		} else {
			sstr << toHexValue(loc.address);
		}
	}

	return sstr.str();
}
const char message::csvHeaderString[] = "Decode Status,Payload,Type,Severity,Origin,Unit,Message TimeStamp,Context TimeStamp,Location,Raw Length,Checksum,Collateral";

// CSV line formatting of a message
//
std::ostream& operator << (std::ostream& os, const message& msg)
{
	char delimiter(',');
	const collateral * coll(msg.getCollateral());

	// turn volatile fields like timestamp/crc off when running unit testing
	//
	static bool unit_testing(getenv("SYST_UNITTESTING") != NULL);

	// status
	os << status2string(msg) << delimiter;

	// payload
	os << '"';
	csvQuoteString(os, msg.getPayload());
	os << '"' << delimiter;

	if (msg.getState() != message::OK && msg.getState() != message::MISSING_COLLATERAL)
	{
		// decode failed, report other fields as empty. The data can't be trusted.
		//
		os << ",,,,," << std::endl;
		return os;
	}

	// Type/Subtype
	//
	os << type2string(msg.getHeader(), msg.getCollateral()) << delimiter;

	// Severity
	//
	os << severity2string[msg.getHeader().field.severity] << delimiter;

	// client name and unit
	//
	os << msg.getClientName() << delimiter;
	os << msg.getUnit() << delimiter;

	// time stamps
	if (msg.getHeader().field.timestamp) {
		os << (unit_testing ?
			"<--UNITTEST-HIDE_TS-->" : toHexValue(msg.getMessageTS()));
	}
	os << delimiter;

	os << (unit_testing ?
		"<--UNITTEST-HIDE_TS-->" : toHexValue(msg.getContextTS())) << delimiter;

	// location
	//
	csvQuoteString(os, location2string(msg.getLocation(), coll));
  	os << delimiter;

	//  raw message length
	//
	os << std::to_string(msg.getLength()) << delimiter;

	// crc
	//
	if (msg.getHeader().field.chksum) {
		os << (unit_testing ? "<--UNITTEST-HIDE_CRC-->" : toHexValue(msg.getCrc()));
	}
	os << delimiter;

	if (coll != nullptr) {
		csvQuoteString(os, coll->getFileName());
	}
	os << std::endl;

	return os;
}

MIPI_SYST_NAMESPACE_END