summaryrefslogtreecommitdiff
path: root/include/dissect.h
blob: 32df78d7a9f345cebf8b54e9711aa8a865eaa654 (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
/*
 *	Protocol packet dissection
 */
#ifndef DISSECT_H
#define DISSECT_H
#include <string>
#include <sstream>
#include "misc.h"
#include "cxx_semantics.h"
#include "command_id.h"
#include "device_proto.h"

namespace nitrokey {
namespace proto {

template <CommandID id, class HIDPacket>
class QueryDissector : semantics::non_constructible {
public:
	static std::string dissect(const HIDPacket &pod) {
		std::stringstream out;

		out << "Raw HID packet:" << std::endl;
		out << ::nitrokey::misc::hexdump((const char *)(&pod), sizeof pod);

		out << "Contents:" << std::endl;
		out << "Command ID:\t" << commandid_to_string((CommandID)(pod.command_id)) << std::endl;
		out << "CRC:\t" << pod.crc << std::endl;

		out << "Payload:" << std::endl;
		out << pod.payload.dissect();
		return out.str();
	}
};

template <CommandID id, class HIDPacket>
class ResponseDissector : semantics::non_constructible {
public:
	static std::string dissect(const HIDPacket &pod) {
		std::stringstream out;

		out << "Raw HID packet:" << std::endl;
		out << ::nitrokey::misc::hexdump((const char *)(&pod), sizeof pod);

		out << "Device status:\t" << pod.device_status + 0 << std::endl;
		out << "Command ID:\t" << commandid_to_string((CommandID)(pod.command_id)) << std::endl;
		out << "Last command CRC:\t" << pod.last_command_crc << std::endl;
		out << "Last command status:\t" << pod.last_command_status + 0 << std::endl;
		out << "CRC:\t" << pod.crc << std::endl;

		out << "Payload:" << std::endl;
		out << pod.payload.dissect();
		return out.str();
	}
};


}
}

#endif