CCCPaste

"Random CAN Spoofing" rev_05

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Random CAN Spoofing"
	- Source Code NOT compiled for: Arduino Pro Mini 5V
	- Source Code created on: 2024-06-15 17:35:31

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read CAN matrix and perform a spoofing on a random */
	/* message. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include   //https://github.com/autowp/arduino-mcp2515

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t can_MCP2515_INT_PIN_D5 = 5;

/***** DEFINITION OF SPI PINS *****/
const uint8_t can_MCP2515_SPI_PIN_MOSI_D11 = 11;
const uint8_t can_MCP2515_SPI_PIN_MISO_D12 = 12;
const uint8_t can_MCP2515_SPI_PIN_SCLK_D13 = 13;
const uint8_t can_MCP2515_SPI_PIN_CS_D10 = 10;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MCP2515 mcp2515(can_MCP2515_SPI_PIN_CS_D10);  // Initialize MCP2515 with CS pin

struct can_frame canMsg;  // Define CAN message structure

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(can_MCP2515_INT_PIN_D5, INPUT_PULLUP);

  pinMode(can_MCP2515_SPI_PIN_CS_D10, OUTPUT);
  // start the SPI library:
  SPI.begin();

  // Initialize MCP2515
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS);
  mcp2515.setNormalMode();

  Serial.begin(115200);
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    Serial.print(canMsg.can_id, HEX);
    Serial.print(" ");
    Serial.print(canMsg.can_dlc, HEX);
    Serial.print(" ");
    for (int i = 0; i < canMsg.can_dlc; i++) {
      Serial.print(canMsg.data[i], HEX);
      Serial.print(" ");
    }
    Serial.println();

    // Perform spoofing on a random message
    if (random(0, 10) > 5) {  // Randomly decide whether to spoof the message
      canMsg.can_id = random(0x100, 0x200);  // Randomly change the CAN ID
      for (int i = 0; i < canMsg.can_dlc; i++) {
        canMsg.data[i] = random(0, 256);  // Randomly change the data
      }
      mcp2515.sendMessage(&canMsg);  // Send the spoofed message
      Serial.println("Spoofed message sent!");
    }
  }
}

/* END CODE */