Working send_can_frame args parsing, now we can use it to actually

communicate!
This commit is contained in:
Данила Горнушко 2023-11-24 18:11:06 +03:00
parent 1a60cc4f3c
commit 47a956d458
3 changed files with 31 additions and 17 deletions

View file

@ -1,7 +1,3 @@
//
// Created by okhsunrog on 8/17/23.
//
#include "can.h" #include "can.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/projdefs.h" #include "freertos/projdefs.h"

View file

@ -1,7 +1,3 @@
//
// Created by okhsunrog on 8/17/23.
//
#ifndef MAIN_CAN_H #ifndef MAIN_CAN_H
#define MAIN_CAN_H #define MAIN_CAN_H

View file

@ -7,7 +7,9 @@
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "xvprintf.h" #include "xvprintf.h"
#include "can.h" #include "can.h"
#include <stddef.h>
#include <stdio.h>
#include <ctype.h>
static void register_send_can_frame(void); static void register_send_can_frame(void);
@ -22,25 +24,45 @@ static struct {
} cansend_args; } cansend_args;
static int send_can_frame(int argc, char **argv) { static int send_can_frame(int argc, char **argv) {
twai_message_t msg = {.extd = 1};
char printf_str[50]; char printf_str[50];
char can_msg_str_buf[50];
int nerrors = arg_parse(argc, argv, (void **) &cansend_args); int nerrors = arg_parse(argc, argv, (void **) &cansend_args);
if (nerrors != 0) { if (nerrors != 0) {
arg_print_errors(stderr, cansend_args.end, argv[0]); arg_print_errors(stderr, cansend_args.end, argv[0]);
return 1; return 1;
} }
const char *can_msg_ptr = cansend_args.message->sval[0]; const char *can_msg_ptr = cansend_args.message->sval[0];
strlcpy(can_msg_str_buf, can_msg_ptr, sizeof(can_msg_str_buf)); char *can_msg_str_buf = strdup(can_msg_ptr);
printf("%s\n", can_msg_str_buf); char *id_substr = strtok(can_msg_str_buf, "#");
char *data_substr = strtok(NULL, "#");
twai_message_t msg = {.extd = 1}; if ((id_substr == NULL) || (strtok(NULL, "#") != NULL)) goto invalid_args;
msg.data_length_code = 0; int id_l = strlen(id_substr);
msg.identifier = 0xFF << 8; int dt_l = data_substr == NULL ? 0 : strlen(data_substr);
if ((id_l > 8) || (dt_l > 16) || (id_l % 2) || (dt_l % 2)) goto invalid_args;
for (int i = 0; i < id_l; i++) if(!isxdigit((int) id_substr[i])) goto invalid_args;
for (int i = 0; i < dt_l; i++) if(!isxdigit((int) data_substr[i])) goto invalid_args;
int msg_id;
if (sscanf(id_substr, "%X", &msg_id) < 1) goto invalid_args;
for (int i = 0; i < (dt_l / 2); i++) {
char *byte_to_parse = malloc(3);
strncpy(byte_to_parse, data_substr + i * 2, 2);
int num;
int res = sscanf(byte_to_parse, "%X", &num);
free(byte_to_parse);
if (res < 1) goto invalid_args;
msg.data[i] = num;
}
msg.data_length_code = dt_l / 2;
msg.identifier = msg_id;
twai_transmit(&msg, pdMS_TO_TICKS(1000)); twai_transmit(&msg, pdMS_TO_TICKS(1000));
can_msg_to_str(&msg, printf_str); can_msg_to_str(&msg, printf_str);
printf("sent %s\n", printf_str); printf("sent %s\n", printf_str);
// free(can_msg_str_buf);
return 0; return 0;
invalid_args:
printf("Invalid arguments!");
free(can_msg_str_buf);
return 1;
} }
static void register_send_can_frame(void) { static void register_send_can_frame(void) {