|
| 1 | +#include <can.h> |
| 2 | +#include <mcp_can.h> |
| 3 | + |
| 4 | +/* |
| 5 | + * CanHacker.h |
| 6 | + * |
| 7 | + * Created on: 17 ���. 2015 �. |
| 8 | + * Author: Dmitry |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef CANHACKER_H_ |
| 12 | +#define CANHACKER_H_ |
| 13 | + |
| 14 | +#include <can.h> |
| 15 | + |
| 16 | +#define CAN_MIN_DLEN 1 |
| 17 | +#define HEX_PER_BYTE 2 |
| 18 | +#define MIN_MESSAGE_DATA_HEX_LENGTH CAN_MIN_DLEN * HEX_PER_BYTE |
| 19 | +#define MAX_MESSAGE_DATA_HEX_LENGTH CAN_MAX_DLEN * HEX_PER_BYTE |
| 20 | +#define MIN_MESSAGE_LENGTH 5 |
| 21 | + |
| 22 | +#define CANHACKER_CMD_MAX_LENGTH 26 |
| 23 | + |
| 24 | +#define CANHACKER_SERIAL_RESPONSE "N0001\r" |
| 25 | +#define CANHACKER_SW_VERSION_RESPONSE "v0107\r" |
| 26 | +#define CANHACKER_VERSION_RESPONSE "V1010\r" |
| 27 | + |
| 28 | +class CanHacker { |
| 29 | + public: |
| 30 | + enum ERROR { |
| 31 | + ERROR_OK, |
| 32 | + ERROR_CONNECTED, |
| 33 | + ERROR_NOT_CONNECTED, |
| 34 | + ERROR_UNKNOWN_COMMAND, |
| 35 | + ERROR_INVALID_COMMAND, |
| 36 | + ERROR_ERROR_FRAME_NOT_SUPPORTED, |
| 37 | + ERROR_BUFFER_OVERFLOW, |
| 38 | + ERROR_SERIAL_TX_OVERRUN, |
| 39 | + ERROR_LISTEN_ONLY, |
| 40 | + ERROR_MCP2515_INIT, |
| 41 | + ERROR_MCP2515_INIT_CONFIG, |
| 42 | + ERROR_MCP2515_INIT_BITRATE, |
| 43 | + ERROR_MCP2515_INIT_SET_MODE, |
| 44 | + ERROR_MCP2515_SEND, |
| 45 | + ERROR_MCP2515_READ, |
| 46 | + ERROR_MCP2515_FILTER, |
| 47 | + ERROR_MCP2515_ERRIF, |
| 48 | + ERROR_MCP2515_MERRF |
| 49 | + }; |
| 50 | + |
| 51 | + CanHacker(Stream *stream, Stream *debugStream, uint8_t cs); |
| 52 | + ~CanHacker(); |
| 53 | + ERROR receiveCommand(const char *buffer, const int length); |
| 54 | + ERROR receiveCanFrame(const struct can_frame *frame); |
| 55 | + ERROR sendFrame(const struct can_frame *); |
| 56 | + ERROR setLoopbackEnabled(const bool value); |
| 57 | + ERROR pollReceiveCan(); |
| 58 | + ERROR receiveCan(const MCP_CAN::RXBn rxBuffer); |
| 59 | + MCP_CAN *getMcp2515(); |
| 60 | + ERROR processInterrupt(); |
| 61 | + Stream *getInterfaceStream(); |
| 62 | + |
| 63 | + private: |
| 64 | + |
| 65 | + static const char CR = '\r'; |
| 66 | + static const char BEL = 7; |
| 67 | + static const uint16_t TIMESTAMP_LIMIT = 0xEA60; |
| 68 | + |
| 69 | + bool _timestampEnabled = false; |
| 70 | + bool _listenOnly = false; |
| 71 | + bool _loopback = false; |
| 72 | + uint8_t _cs; |
| 73 | + MCP_CAN *mcp2515; |
| 74 | + CAN_SPEED bitrate; |
| 75 | + bool _isConnected = false; |
| 76 | + Stream *_stream; |
| 77 | + Stream *_debugStream; |
| 78 | + |
| 79 | + enum /*class*/ COMMAND : char { |
| 80 | + COMMAND_SET_BITRATE = 'S', // set CAN bit rate |
| 81 | + COMMAND_SET_BTR = 's', // set CAN bit rate via |
| 82 | + COMMAND_OPEN_CAN_CHAN = 'O', // open CAN channel |
| 83 | + COMMAND_CLOSE_CAN_CHAN = 'C', // close CAN channel |
| 84 | + COMMAND_SEND_11BIT_ID = 't', // send CAN message with 11bit ID |
| 85 | + COMMAND_SEND_29BIT_ID = 'T', // send CAN message with 29bit ID |
| 86 | + COMMAND_SEND_R11BIT_ID = 'r', // send CAN remote message with 11bit ID |
| 87 | + COMMAND_SEND_R29BIT_ID = 'R', // send CAN remote message with 29bit ID |
| 88 | + COMMAND_READ_STATUS = 'F', // read status flag byte |
| 89 | + COMMAND_SET_ACR = 'M', // set Acceptance Code Register |
| 90 | + COMMAND_SET_AMR = 'm', // set Acceptance Mask Register |
| 91 | + COMMAND_GET_VERSION = 'V', // get hardware and software version |
| 92 | + COMMAND_GET_SW_VERSION = 'v', // get software version only |
| 93 | + COMMAND_GET_SERIAL = 'N', // get device serial number |
| 94 | + COMMAND_TIME_STAMP = 'Z', // toggle time stamp setting |
| 95 | + COMMAND_READ_ECR = 'E', // read Error Capture Register |
| 96 | + COMMAND_READ_ALCR = 'A', // read Arbritation Lost Capture Register |
| 97 | + COMMAND_READ_REG = 'G', // read register conten from SJA1000 |
| 98 | + COMMAND_WRITE_REG = 'W', // write register content to SJA1000 |
| 99 | + COMMAND_LISTEN_ONLY = 'L' // switch to listen only mode |
| 100 | + }; |
| 101 | + |
| 102 | + ERROR parseTransmit(const char *buffer, int length, struct can_frame *frame); |
| 103 | + ERROR createTransmit(const struct can_frame *frame, char *buffer, const int length); |
| 104 | + |
| 105 | + uint16_t getTimestamp(); |
| 106 | + ERROR setFilter(const uint32_t filter); |
| 107 | + ERROR setFilterMask(const uint32_t mask); |
| 108 | + |
| 109 | + ERROR connectCan(); |
| 110 | + ERROR disconnectCan(); |
| 111 | + bool isConnected(); |
| 112 | + ERROR writeCan(const struct can_frame *); |
| 113 | + ERROR writeStream(const char character); |
| 114 | + ERROR writeStream(const char *buffer); |
| 115 | + ERROR writeDebugStream(const char character); |
| 116 | + ERROR writeDebugStream(const char *buffer); |
| 117 | + ERROR writeDebugStream(const int buffer); |
| 118 | + ERROR writeDebugStream(const uint8_t *buffer, size_t size); |
| 119 | + |
| 120 | + ERROR receiveSetBitrateCommand(const char *buffer, const int length); |
| 121 | + ERROR receiveTransmitCommand(const char *buffer, const int length); |
| 122 | + ERROR receiveTimestampCommand(const char *buffer, const int length); |
| 123 | + ERROR receiveCloseCommand(const char *buffer, const int length); |
| 124 | + ERROR receiveOpenCommand(const char *buffer, const int length); |
| 125 | + ERROR receiveListenOnlyCommand(const char *buffer, const int length); |
| 126 | + ERROR receiveSetAcrCommand(const char *buffer, const int length); |
| 127 | + ERROR receiveSetAmrCommand(const char *buffer, const int length); |
| 128 | +}; |
| 129 | + |
| 130 | +#endif /* CANHACKER_H_ */ |
0 commit comments