PageRenderTime 30ms CodeModel.GetById 23ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/firmware/src/Extruder/DebugPacketProcessor.cc

http://github.com/makerbot/G3Firmware
C++ | 82 lines | 61 code | 6 blank | 15 comment | 17 complexity | 814166ad5d48c435adf8916b30515111 MD5 | raw file
 1/*
 2 * DebugPacketProcessor.cc
 3 *
 4 *  Created on: Dec 9, 2009
 5 *      Author: phooky
 6 */
 7
 8#include "DebugPacketProcessor.hh"
 9#include "UART.hh"
10#include "Timeout.hh"
11#include "Configuration.hh"
12#if HAS_SLAVE_UART
13#include "Motherboard.hh"
14#include "Errors.hh"
15#include "Tool.hh"
16#endif
17#if HAS_COMMAND_QUEUE
18#include "Command.hh"
19#endif // HAS_COMMAND_QUEUE
20namespace CommandCode {
21enum {
22	DEBUG_ECHO = 0x70,
23	DEBUG_GENERATE_BAD_PACKET = 0x71,
24	DEBUG_SIMULATE_BAD_PACKET = 0x72,
25	DEBUG_SLAVE_PASSTHRU = 0x73,
26	DEBUG_CLEAR_COMMAND_QUEUE = 0x74,
27	DEBUG_COMMAND_QUEUE_FILLER = 0xF0
28};
29}
30
31namespace BadReceptionCode {
32enum {
33	RECEIVED_NOISE = 1,
34	BAD_PACKET_LENGTH = 2,
35	BAD_CRC = 3,
36	TIMEOUT = 4
37};
38}
39
40namespace BadResponseCode {
41enum {
42	NO_RESPONSE = 1,
43	SKIP_START_BYTE = 2,
44	BAD_PACKET_LENGTH = 3,
45	BAD_CRC = 4,
46	TIMEOUT = 5
47};
48
49}
50
51/// Identify a debug packet, and process it.  If the packet is a debug
52/// packet, return true, indicating that no further processing should
53/// be done.  Otherwise, processing of this packet should drop through
54/// to the next processing level.
55bool processDebugPacket(const InPacket& from_host, OutPacket& to_host) {
56	if (from_host.getLength() == 0) {
57		return false;
58	} // drop through on a nop packet
59	uint8_t command = from_host.read8(0);
60
61	if ((command & 0x70) == 0x70) {
62		// This is a debug packet
63		if (command == CommandCode::DEBUG_ECHO) {
64			// We start from 1 so we can skip the debug command byte.
65			to_host.reset();
66			for (int i = 1; i < from_host.getLength(); i++) {
67				to_host.append8(from_host.read8(i));
68			}
69			return true;
70		} else if (command == CommandCode::DEBUG_GENERATE_BAD_PACKET) {
71			// TODO
72		} else if (command == CommandCode::DEBUG_SIMULATE_BAD_PACKET) {
73			// TODO
74		} else if (command == CommandCode::DEBUG_SLAVE_PASSTHRU) {
75			return true;
76		}
77		return false;
78	} else {
79		// This is not a debug packet
80		return false;
81	}
82}