can_controllers
Macros | Enumerations | Functions | Variables
main.cpp File Reference

Contains FreeRTOS tasks and a pthread for sending and receiving messages between WASM app and CAN controllers. More...

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "NMEA_msg.h"
#include "esp_log.h"
#include <N2kMsg.h>
#include <NMEA2000_esp32-c6.h>
#include <NMEA2000.h>
#include <N2kMessages.h>
#include "esp_err.h"
#include "esp_pthread.h"
#include <NMEA2000_mcp.h>
#include "driver/gpio.h"
#include "bi-inc/attr_container.h"
#include "wasm_export.h"
#include "bh_read_file.h"
#include "bh_getopt.h"
#include "bh_platform.h"
#include <queue>
#include <string>
#include <iomanip>
#include <chrono>
#include "driver/spi_master.h"
#include "nmea_attack.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define NATIVE_STACK_SIZE   (32*1024)
 
#define NATIVE_HEAP_SIZE   (32*1024)
 
#define PTHREAD_STACK_SIZE   4096
 
#define MAX_DATA_LENGTH_BTYES   223
 
#define MSG_BUFFER_SIZE   (10 + 223*2)
 
#define MODE_BUFFER_SIZE   1
 
#define MY_ESP_LOG_LEVEL   ESP_LOG_INFO
 
#define STATS_TASK_PRIO   tskIDLE_PRIORITY
 
#define STATS_TICKS   pdMS_TO_TICKS(1000)
 
#define ARRAY_SIZE_OFFSET   5
 
#define TX_QUEUE_SIZE   100
 
#define RX_QUEUE_SIZE   100
 
#define MCP0_TX   GPIO_NUM_22
 
#define MCP0_RX   GPIO_NUM_23
 
#define MCP1_CS   16
 
#define MCP1_INT   10
 
#define MCP2_CS   17
 
#define MCP2_INT   11
 
#define ESP_INTR_FLAG_DEFAULT   0
 
#define MODE_SETTING_PIN_LSB   GPIO_NUM_18
 
#define MODE_SETTING_PIN_MSB   GPIO_NUM_19
 
#define MODE_SETTING_MASK   ((1ULL<<MODE_SETTING_PIN_LSB) | (1ULL<<MODE_SETTING_PIN_MSB))
 
#define PRINT_STATS
 

Enumerations

enum  CONTROLLER { C0_NUM = 0 , C1_NUM = 1 , C2_NUM = 2 }
 enum to store identifiers for each controller
 

Functions

tNMEA2000_esp32c6 C0 (MCP0_TX, MCP0_RX)
 Controller 0 -> TWAI, (TX_PIN, RX_PIN)
 
void HandleNMEA2000Msg (const tN2kMsg &N2kMsg)
 Creates a NMEA_msg object and adds it to.data the received messages queue. More...
 
std::string nmea_to_string (NMEA_msg &msg)
 converts a NMEA_msg to a string More...
 
void uintArrToCharrArray (uint8_t(&data_uint8_arr)[MAX_DATA_LENGTH_BTYES], unsigned char(&data_char_arr)[MAX_DATA_LENGTH_BTYES])
 
void PrintStr (wasm_exec_env_t exec_env, uint8_t *input, int32_t length)
 prints a uint8_t array to terminal More...
 
void PrintInt32 (wasm_exec_env_t exec_env, int32_t number, int32_t hex)
 prints a integer to terminal More...
 
int32_t SendMsg (wasm_exec_env_t exec_env, int32_t controller_number, int32_t priority, int32_t PGN, int32_t source, uint8_t *data, int32_t data_length_bytes)
 
void uint8ArrayToCharrArray (uint8_t(&data_uint8_arr)[MAX_DATA_LENGTH_BTYES], unsigned char(&data_char_arr)[MAX_DATA_LENGTH_BTYES])
 Fills a char array with values from a uint8_t array. More...
 
void configTConnectorModes ()
 Configures GPIO used to read mode settings from the Raspberry Pi.
 
void get_mode_task (void *pvParameters)
 Task to get the Controller Mode set by the Raspberry Pi. More...
 
bool SendN2kMsg (NMEA_msg msg, int controller_num)
 Sends a message. More...
 
void GetStatus (const char *TAG)
 Retrieves twai status and alerts. More...
 
void C0_receive_task (void *pvParameters)
 FreeRTOS task for receiving messages from CAN controller. More...
 
void C0_send_task (void *pvParameters)
 FreeRTOS task for processing and sending messages from CAN controller with NMEA2000 library. More...
 
void C1_receive_task (void *pvParameters)
 FreeRTOS task for receiving messages from MCP CAN controller 1. More...
 
void C1_send_task (void *pvParameters)
 FreeRTOS task for processing and sending messages from MCP CAN controller 1 with NMEA2000 library. More...
 
void C2_receive_task (void *pvParameters)
 FreeRTOS task for receiving messages from MCP CAN controller 2. More...
 
void C2_send_task (void *pvParameters)
 FreeRTOS task for processing and sending messages from MCP CAN controller 2 with NMEA2000 library. More...
 
void * iwasm_main (void *arg)
 WASM pthread to host wasm app. More...
 
int app_main (void)
 Creates a FreeRTOS task for sending and receiving to and from CAN Controller and creates a pthread to run WASM app. More...
 

Variables

spi_device_handle_t spi1
 MCP controller 1 spi handle. More...
 
spi_device_handle_t spi2
 MCP controller 2 spi handle. More...
 
QueueHandle_t C0_tx_queue
 Queue that stores messages to be sent out on controller 0.
 
QueueHandle_t C1_tx_queue
 Queue that stores messages to be sent out on controller 1.
 
QueueHandle_t C2_tx_queue
 Queue that stores messages to be sent out on controller 2.
 
QueueHandle_t rx_queue
 Queue that stores all messages received on all controllers.
 
SemaphoreHandle_t x_sem_mcp1
 Semaphore handle for MCP1.
 
SemaphoreHandle_t x_sem_mcp2
 Semaphore handle for MCP2.
 
char * wasm_buffer = NULL
 buffer allocated for wasm app, used to hold received messages so app can access them
 
char * wasm_mode_buffer = NULL
 buffer allocated for wasm app, used to hold current t connector mode set by Raspberry Pi
 
int read_msg_count = 0
 Used to track messages read.
 
int send_msg_count = 0
 Used to track messages sent.
 
std::string tc_mode = "1"
 Contains the T Connector mode-> 0 - OFF, 1 - PASSIVE, 2 - GPS_ATTACK, 3 - TBD.
 
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_TX_FAILED | TWAI_ALERT_RX_QUEUE_FULL
 Sets which alerts to enable for TWAI controller.
 
int C0_rx_task_count = 0
 
int C0_tx_task_count = 0
 
int C1_rx_task_count = 0
 
int C1_tx_task_count = 0
 
int C2_rx_task_count = 0
 
int C2_tx_task_count = 0
 
int wasm_pthread_count = 0
 
int stats_task_count = 0
 
double wasm_main_duration
 

Detailed Description

Contains FreeRTOS tasks and a pthread for sending and receiving messages between WASM app and CAN controllers.

Todo:
convert unessesary ESP_LOGI messages to ESP_LOGD

Definition in file main.cpp.

Function Documentation

◆ app_main()

int app_main ( void  )

Creates a FreeRTOS task for sending and receiving to and from CAN Controller and creates a pthread to run WASM app.

In ESP-IDF, a pthread is just a wrapper on FreeRTOS

  • Controller 1 Receiving task *‍/
  • Controller 2 Receiving task *‍/

Definition at line 1107 of file main.cpp.

◆ C0_receive_task()

void C0_receive_task ( void *  pvParameters)

FreeRTOS task for receiving messages from CAN controller.

Parameters
pvParametersNMEA2000 Library is designed so that message receiving and sending is handled within the same task. In the NMEA2000_ESP32 library, this is made possible by letting the twai rx interrupt handle receiving CAN frames. In this library, the receiving is separated from the processing and sending of messages. This is done because I was unable to trigger recieving a CAN frame from the twai rx interrupt, so CAN_read_frame() must be called explicitly here.

Definition at line 613 of file main.cpp.

◆ C0_send_task()

void C0_send_task ( void *  pvParameters)

FreeRTOS task for processing and sending messages from CAN controller with NMEA2000 library.

Tries to receive a message from the controller 0 tx queue, and sends it if available

Todo:
frame buffer should be 32 - see if this works
Parameters
pvParameters

Definition at line 641 of file main.cpp.

◆ C1_receive_task()

void C1_receive_task ( void *  pvParameters)

FreeRTOS task for receiving messages from MCP CAN controller 1.

Sets up a NMEA2000 Object with the MCP class. Initializes the SPI bus and adds a device to the bus. Semaphore is used so that send and receive tasks don't access the same device at the same time.

Parameters
pvParameters

Definition at line 675 of file main.cpp.

◆ C1_send_task()

void C1_send_task ( void *  pvParameters)

FreeRTOS task for processing and sending messages from MCP CAN controller 1 with NMEA2000 library.

Tries to receive a message from the controller 1 tx queue, and sends it if available Semaphore is used so that send and receive tasks don't access the same device at the same time.

Parameters
pvParameters

Definition at line 711 of file main.cpp.

◆ C2_receive_task()

void C2_receive_task ( void *  pvParameters)

FreeRTOS task for receiving messages from MCP CAN controller 2.

Sets up a NMEA2000 Object with the MCP class. Adds a device to the bus. It does not need to initialize the SPI bus because the MCP1 recieve task has already done that. Semaphore is used so that send and receive tasks don't access the same device at the same time.

Parameters
pvParameters

Definition at line 753 of file main.cpp.

◆ C2_send_task()

void C2_send_task ( void *  pvParameters)

FreeRTOS task for processing and sending messages from MCP CAN controller 2 with NMEA2000 library.

Tries to receive a message from the controller 2 tx queue, and sends it if available Semaphore is used so that send and receive tasks don't access the same device at the same time.

Parameters
pvParameters

Definition at line 789 of file main.cpp.

◆ get_mode_task()

void get_mode_task ( void *  pvParameters)

Task to get the Controller Mode set by the Raspberry Pi.

00 - Off 01 - Passive 10 - GPS Opposite Direction Attack 11 - GPS Translation Attack

Definition at line 351 of file main.cpp.

◆ GetStatus()

void GetStatus ( const char *  TAG)

Retrieves twai status and alerts.

Used to display information regarding queue's filling up, and how many messages have been sent/received.

Parameters
[in]TAG

Definition at line 543 of file main.cpp.

◆ HandleNMEA2000Msg()

void HandleNMEA2000Msg ( const tN2kMsg &  N2kMsg)

Creates a NMEA_msg object and adds it to.data the received messages queue.

Todo:
handle out of range data
Parameters
N2kMsgReference to the N2KMs being handled
Returns
void

Definition at line 824 of file main.cpp.

◆ iwasm_main()

void* iwasm_main ( void *  arg)

WASM pthread to host wasm app.

Sets up the wasm environment. Links native function to be exported. Calls wasm app function to link allocated wasm buffer. Runs main function in wasm app.

Parameters
argunused - I don't know why this is required

Definition at line 887 of file main.cpp.

◆ nmea_to_string()

std::string nmea_to_string ( NMEA_msg &  msg)

converts a NMEA_msg to a string

Todo:
make sure that if pgn is only 4 digits in hex it still takes 5
Parameters
[in]msgreference to a NMEA_msg object
Returns
std::string representing the message

Definition at line 272 of file main.cpp.

◆ PrintInt32()

void PrintInt32 ( wasm_exec_env_t  exec_env,
int32_t  number,
int32_t  hex 
)

prints a integer to terminal

Native function to be exported to WASM app. Used for debugging purposes.

Parameters
exec_env
numberinteger to print
hexprints the number in hex format if hex is 1, else prints it in decimal format

Definition at line 189 of file main.cpp.

◆ PrintStr()

void PrintStr ( wasm_exec_env_t  exec_env,
uint8_t *  input,
int32_t  length 
)

prints a uint8_t array to terminal

Native function to be exported to WASM app. Used for debugging purposes.

Parameters
exec_env
inputpointer to array
lengthlength of the array

Definition at line 171 of file main.cpp.

◆ SendN2kMsg()

bool SendN2kMsg ( NMEA_msg  msg,
int  controller_num 
)

Sends a message.

Converts NMEA-msg to the NMEA2000 Library N2kMsg format and sends the message

Todo:

update time to take time from message

update for multiple controllers

Definition at line 376 of file main.cpp.

◆ uint8ArrayToCharrArray()

void uint8ArrayToCharrArray ( uint8_t(&)  data_uint8_arr[MAX_DATA_LENGTH_BTYES],
unsigned char(&)  data_char_arr[MAX_DATA_LENGTH_BTYES] 
)

Fills a char array with values from a uint8_t array.

Parameters
[in]data_uint8_arr
[out]data_char_arr

Definition at line 296 of file main.cpp.

Variable Documentation

◆ spi1

tNMEA2000_mcp C1 & spi1

MCP controller 1 spi handle.

Controller 1 -> MCP, (spi_handle, CS_PIN, mcp_clk_freq, INT_PIN, _rx_frame_buf_size)

Definition at line 90 of file main.cpp.

◆ spi2

tNMEA2000_mcp C2 & spi2

MCP controller 2 spi handle.

Controller 2 -> MCP, (spi_handle, CS_PIN, mcp_clk_freq, INT_PIN, _rx_frame_buf_size)

Definition at line 91 of file main.cpp.