Draft progress implementing single frame rx.

This commit is contained in:
Christopher Peplin 2013-12-27 19:04:27 -05:00
parent 34a7c0ca08
commit 451ee4faa4
7 changed files with 65 additions and 20 deletions

View File

@ -16,10 +16,9 @@ ifneq ($(OSTYPE),Darwin)
endif
SRC = $(wildcard src/**/*.c)
SRC += $(wildcard deps/**/*.c)
SRC += $(wildcard deps/canutil/src/**/*.c)
OBJS = $(SRC:.c=.o)
TEST_SRC = $(wildcard $(TEST_DIR)/test_*.c)
TEST_OBJS = $(TEST_SRC:.c=.o)
TESTS=$(patsubst %.c,%.bin,$(TEST_SRC))
TEST_SUPPORT_SRC = $(TEST_DIR)/common.c
TEST_SUPPORT_OBJS = $(TEST_SUPPORT_SRC:.c=.o)

View File

@ -1,15 +1,32 @@
#include <isotp/isotp.h>
#include <isotp/receive.h>
#include <bitfield/bitfield.h>
const uint16_t MAX_ISO_TP_MESSAGE_SIZE = 4096;
const uint16_t MAX_CAN_FRAME_SIZE = 8;
const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT = 100;
const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS = true;
void isotp_receive_can_frame(const uint16_t arbitration_id, const uint8_t* data,
const uint8_t length) {
//match with any request we made
//handle flow control if necessary
//call callback if message completed
void isotp_receive_can_frame(IsoTpHandler* handler,
const uint16_t arbitration_id, const uint8_t* data, const uint8_t length) {
if(arbitration_id != handler->arbitration_id){
return;
}
// TODO use CanMessage struct from canutil library - allocate payload buffer
// on stack, 8 bytes
// TODO this function should receive uint64_t...
IsoTpProtocolControlInformation pci = (IsoTpProtocolControlInformation)
getBitField((uint64_t)data, 0, 2, false);
switch(pci) {
case PCI_SINGLE:
isotp_handle_single_frame(handler, arbitration_id, data, length);
break;
default:
handler->shims->log("Only single frame messages are supported");
break;
}
}
bool isotp_send(const uint8_t* payload, uint16_t payload_size) {
@ -36,6 +53,7 @@ IsoTpHandler isotp_init(IsoTpShims* shims, uint16_t arbitration_id,
IsoTpCanFrameSentHandler can_frame_sent_callback) {
IsoTpHandler handler = {
shims: shims,
arbitration_id: arbitration_id,
message_received_callback: message_received_callback,
message_sent_callback: message_sent_callback,
can_frame_sent_callback: can_frame_sent_callback,

View File

@ -42,7 +42,7 @@ typedef struct {
typedef struct {
IsoTpShims* shims;
uint16_t arb_id;
uint16_t arbitration_id;
IsoTpMessageReceivedHandler message_received_callback;
IsoTpMessageSentHandler message_sent_callback;
IsoTpCanFrameSentHandler can_frame_sent_callback;
@ -58,16 +58,16 @@ typedef struct {
} IsoTpHandler;
typedef enum {
PCI_SINGLE,
PCI_FIRST_FRAME,
PCI_CONSECUTIVE_FRAME,
PCI_FLOW_CONTROL_FRAME
PCI_SINGLE = 0x0,
PCI_FIRST_FRAME = 0x1,
PCI_CONSECUTIVE_FRAME = 0x2,
PCI_FLOW_CONTROL_FRAME = 0x3
} IsoTpProtocolControlInformation;
typedef enum {
PCI_FLOW_STATUS_CONTINUE,
PCI_FLOW_STATUS_WAIT,
PCI_FLOW_STATUS_OVERFLOW
PCI_FLOW_STATUS_CONTINUE = 0x0,
PCI_FLOW_STATUS_WAIT = 0x1,
PCI_FLOW_STATUS_OVERFLOW = 0x2
} IsoTpFlowStatus;
IsoTpShims isotp_init_shims(LogShim log,
@ -93,7 +93,8 @@ void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms);
// frame, the soure could go out of scope
bool isotp_send(const uint8_t* payload, uint16_t payload_size);
void isotp_receive_can_frame(const uint16_t arbitration_id, const uint8_t* data,
void isotp_receive_can_frame(IsoTpHandler* handler,
const uint16_t arbitration_id, const uint8_t* data,
const uint8_t length);
void isotp_destroy(IsoTpHandler* handler);

7
src/isotp/receive.c Normal file
View File

@ -0,0 +1,7 @@
#include <isotp/receive.h>
void isotp_handle_single_frame(IsoTpHandler* handler,
const uint16_t arbitration_id, const uint8_t* data,
const uint8_t length) {
handler->message_received_callback(arbitration_id, data, length);
}

19
src/isotp/receive.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __ISOTP_RECEIVE_H__
#define __ISOTP_RECEIVE_H__
#include <isotp/isotp.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
void isotp_handle_single_frame(IsoTpHandler* handler, const uint16_t arbitration_id,
const uint8_t* data, const uint8_t length);
#ifdef __cplusplus
}
#endif
#endif // __ISOTP_RECEIVE_H__

View File

@ -26,6 +26,7 @@ void debug(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\r\n");
va_end(args);
}
@ -39,6 +40,7 @@ void mock_set_timer(uint16_t time_ms, void (*callback)) {
void message_received(const uint16_t arbitration_id, const uint8_t* payload,
const uint16_t size) {
debug("Received ISO-TP message:");
message_was_received = true;
log_isotp_message(arbitration_id, payload, size);
last_message_received_arb_id = arbitration_id;
last_message_received_payload_size = size;
@ -54,7 +56,6 @@ void message_sent(const bool success, const uint16_t arbitration_id,
}
log_isotp_message(arbitration_id, payload, size);
message_was_received = true;
last_message_sent_arb_id = arbitration_id;
last_message_sent_payload_size = size;
memcpy(last_message_sent_payload, payload, size);

View File

@ -28,7 +28,7 @@ extern void setup();
START_TEST (test_receive_wrong_id)
{
const uint8_t data[8] = {0};
isotp_receive_can_frame(0x100, data, sizeof(data));
isotp_receive_can_frame(&ISOTP_HANDLER, 0x100, data, sizeof(data));
fail_if(message_was_received);
}
END_TEST
@ -37,7 +37,7 @@ START_TEST (test_receive_bad_pci)
{
// 4 is a reserved number for the PCI field - only 0-3 are allowed
const uint8_t data[8] = {0x40};
isotp_receive_can_frame(0x2a, data, sizeof(data));
isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, sizeof(data));
fail_if(message_was_received);
}
END_TEST
@ -45,7 +45,7 @@ END_TEST
START_TEST (test_receive_single_frame)
{
const uint8_t data[8] = {0x0, 0x12, 0x34};
isotp_receive_can_frame(0x2a, data, sizeof(data));
isotp_receive_can_frame(&ISOTP_HANDLER, 0x2a, data, sizeof(data));
fail_unless(message_was_received);
ck_assert_int_eq(last_message_received_arb_id, 0x2a);
ck_assert_int_eq(last_message_received_payload_size, 2);