From f4f5d32e478140b02adfda2a1d85b7f0aaea0778 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 5 Dec 2023 01:06:31 +0100 Subject: [PATCH] hidraw test --- platform.c | 29 ++++++++++++++++++++++++++++- rogue_enemy.h | 3 ++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/platform.c b/platform.c index 3ad036f..e94ca5a 100644 --- a/platform.c +++ b/platform.c @@ -438,9 +438,10 @@ static const uint8_t rc71l_mode_switch_commands[][23][64] = { }; static int hidraw_cycle_to_mode(const char* const path, int controller_mode) { - int res = -EINVAL; + int res = 0; if ((controller_mode < 0) || (controller_mode > 2)) { + res = -EINVAL; goto hidraw_cycle_to_mode_err_mode; } @@ -470,6 +471,32 @@ static int hidraw_cycle_to_mode(const char* const path, int controller_mode) { strcat(hidraw_path, "/dev"); printf("Using hidraw located at: %s\n", hidraw_path); + + int fd = open(hidraw_path, O_RDWR); + if (fd == -1) { + fprintf(stderr, "Error opening hidraw device\n"); + + res = -EIO; + + goto hidraw_cycle_to_mode_err; + } + + for (int i = 0; i < 23; ++i) { + const int write_res = write(fd, &rc71l_mode_switch_commands[controller_mode][i][0], 64); + if (write_res != 64) { + fprintf(stderr, "Error writing packet %d/23: %d bytes sent, 64 expected\n", i, write_res); + break; + } + } + + close(fd); + + if (res == 0) { + printf("Control messages sent successfully.\n"); + } else { + goto hidraw_cycle_to_mode_err; + } + } } diff --git a/rogue_enemy.h b/rogue_enemy.h index 1b08412..a700852 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -15,7 +15,8 @@ #include #include #include - +#include +#include #include #include