Skip to content

Commit

Permalink
feat: functions to request UDS service finished
Browse files Browse the repository at this point in the history
  • Loading branch information
AdonaiDiazEsparza committed Nov 14, 2024
1 parent 3842ee8 commit b720515
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 11 deletions.
Binary file modified Canbus_app/dist/canbus_app.fap
Binary file not shown.
Binary file modified Canbus_app/dist/debug/canbus_app_d.elf
Binary file not shown.
59 changes: 52 additions & 7 deletions Canbus_app/libraries/uds_library.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "uds_library.h"

// Function to malloc the instance
UDS_SERVICE* uds_service_alloc(MCP2515* CAN, uint32_t id_to_send, uint32_t id_to_received) {
UDS_SERVICE* uds_service_alloc(
uint32_t id_to_send,
uint32_t id_to_received,
MCP_MODE mode,
MCP_CLOCK clk,
MCP_BITRATE bitrate) {
UDS_SERVICE* instance = malloc(sizeof(UDS_SERVICE));
instance->CAN = mcp_alloc(CAN->mode, CAN->clck, CAN->bitRate);
instance->CAN = mcp_alloc(mode, clk, bitrate);
instance->id_to_send = id_to_send;
instance->id_to_received = id_to_received;

Expand Down Expand Up @@ -36,21 +41,61 @@ void free_uds(UDS_SERVICE* uds_instance) {
free(uds_instance);
}

// Get Frames
bool read_frames_uds(MCP2515* CAN, uint8_t id, CANFRAME* frame) {
uint32_t time_delay = 0;

do {
if(read_can_message(CAN, frame) == ERROR_OK) {
if(frame->canId == id) return true;
}
furi_delay_us(1);
time_delay++;

} while((time_delay < 6000));

return false;
}

// Function to send a service
bool manual_uds_service_request(
bool uds_manual_service_request(
UDS_SERVICE* uds_instance,
uint8_t* data_to_send,
CANFRAME* frames_to_received,
uint8_t count_of_frames) {
MCP2515* CAN = uds_instance->CAN;
CANFRAME frame_to_send = {0};
frame_to_send.canId = uds_instance->id_to_send;
frame_to_send.data_lenght = 8;
uint32_t id_to_received = uds_instance->id_to_received;
ERROR_CAN ret = ERROR_OK;

UNUSED(frame_to_send);
UNUSED(id_to_received);
UNUSED(frames_to_received);
UNUSED(count_of_frames);
UNUSED(CAN);

return false;
for(uint8_t i = 0; i < 8; i++)
frame_to_send.buffer[i] = data_to_send[i];

ret = send_can_frame(CAN, &frame_to_send);

if(ret != ERROR_OK) return false;

memset(frame_to_send.buffer, 0, sizeof(frame_to_send.buffer));
frame_to_send.buffer[0] = 0x30;

for(uint8_t i = 0; i < count_of_frames; i++) {
if(i == 1) {
ret = send_can_frame(CAN, &frame_to_send);
if(ret != ERROR_OK) return false;
}

if(!read_frames_uds(CAN, id_to_received, &(frames_to_received[i]))) {
if(i == 0)
return false;
else
break;
}
}

return true;
}
17 changes: 16 additions & 1 deletion Canbus_app/libraries/uds_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@ typedef struct {
uint32_t id_to_received;
} UDS_SERVICE;

UDS_SERVICE* uds_service_alloc(MCP2515* CAN, uint32_t id_to_send, uint32_t id_to_received);
UDS_SERVICE* uds_service_alloc(
uint32_t id_to_send,
uint32_t id_to_received,
MCP_MODE mode,
MCP_CLOCK clk,
MCP_BITRATE bitrate);

// Init Uds
bool uds_init(UDS_SERVICE* uds_instance);

// send a manual request
bool uds_manual_service_request(
UDS_SERVICE* uds_instance,
uint8_t* data_to_send,
CANFRAME* frames_to_received,
uint8_t count_of_frames);

void free_uds(UDS_SERVICE* uds_instance);

#endif
73 changes: 70 additions & 3 deletions Canbus_app/scenes/UDSOptions/UDSManualSender.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void app_scene_uds_set_data_on_enter(void* context) {
ByteInput* scene = app->input_byte_value;

for(int i = count_of_bytes; i < 7; i++) {
data_to_send[i] = 0;
data_to_send[i] = 0xAA;
}

byte_input_set_result_callback(
Expand Down Expand Up @@ -201,9 +201,76 @@ void app_scene_uds_response_sender_on_exit(void* context) {

static int32_t obdii_thread_response_manual_uds_sender_on_work(void* context) {
App* app = context;
UNUSED(app);
MCP2515* CAN = app->mcp_can;

FuriString* text = app->text;

furi_string_reset(text);

UDS_SERVICE* uds_service =
uds_service_alloc(id_request, id_response, CAN->mode, CAN->clck, CAN->bitRate);

bool run = uds_init(uds_service);

uint8_t data[8];

memset(data, 0xaa, sizeof(data));

data[0] = count_of_bytes;

for(uint8_t i = 1; i < (count_of_bytes + 1); i++) {
data[i] = data_to_send[i - 1];
}

CANFRAME canframes[count_of_frames];

memset(canframes, 0, sizeof(canframes));

if(run) {
// Time delay added to initialize
furi_delay_ms(500);
furi_string_printf(text, "DEVICE CONNECTED!...\n");
furi_string_cat_printf(text, "%lx ", id_request);

for(uint8_t i = 0; i < 8; i++) {
furi_string_cat_printf(text, "%x ", data[i]);
}

furi_string_cat_printf(text, "\n");

text_box_set_text(app->textBox, furi_string_get_cstr(text));

if(uds_manual_service_request(uds_service, data, canframes, count_of_frames)) {
for(uint8_t i = 0; i < count_of_frames; i++) {
CANFRAME frame_received = canframes[i];
if(frame_received.canId == 0x00) break;
furi_string_cat_printf(
text,
"<-(%u) %lx %x %x %x %x %x %x %x %x\n",
i,
frame_received.canId,
frame_received.buffer[0],
frame_received.buffer[1],
frame_received.buffer[2],
frame_received.buffer[3],
frame_received.buffer[4],
frame_received.buffer[5],
frame_received.buffer[6],
frame_received.buffer[7]);
}
text_box_reset(app->textBox);
text_box_set_text(app->textBox, furi_string_get_cstr(text));
} else {
text_box_reset(app->textBox);
furi_string_cat_printf(text, "FAILURE TRANSMITION!!\n");
text_box_set_text(app->textBox, furi_string_get_cstr(text));
}
} else {
furi_string_printf(text, "DEVICE NOT CONNECTED!");
text_box_set_text(app->textBox, furi_string_get_cstr(text));
}

log_info("Hola");
free_uds(uds_service);

return 0;
}

0 comments on commit b720515

Please sign in to comment.