Implement initial profile reading and writing with little-endian offsets and CLI tool

This commit is contained in:
Aodhan Collins
2026-02-22 17:53:05 +00:00
parent 224271e639
commit db5c3505da
72 changed files with 7090 additions and 126 deletions

View File

@@ -10,6 +10,7 @@
#include <string.h>
#include <getopt.h>
#include "azeron.h"
#include "../libazeron/internal.h"
#define AZERON_CLI_VERSION "1.0.0"
@@ -23,6 +24,8 @@ int cmd_save_profile(int argc, char *argv[]);
int cmd_load_profile(int argc, char *argv[]);
int cmd_export_config(int argc, char *argv[]);
int cmd_import_config(int argc, char *argv[]);
int cmd_set_stick(int argc, char *argv[]);
int cmd_read_raw(int argc, char *argv[]);
/* Command structure */
struct command {
@@ -41,6 +44,8 @@ static struct command commands[] = {
{"load-profile", "Load configuration from profile", cmd_load_profile},
{"export-config", "Export configuration to file", cmd_export_config},
{"import-config", "Import configuration from file", cmd_import_config},
{"set-stick", "Configure analog stick settings", cmd_set_stick},
{"read-raw", "Read raw memory from device", cmd_read_raw},
{NULL, NULL, NULL}
};
@@ -119,6 +124,7 @@ int cmd_info(int argc, char *argv[])
{
struct azeron_device *device;
struct azeron_device_info info;
struct azeron_stick_config stick;
int ret;
int device_index = 0;
@@ -176,9 +182,20 @@ int cmd_info(int argc, char *argv[])
printf("Serial Number: %s\n", info.serial_number);
printf("USB ID: %04x:%04x\n", info.vendor_id, info.product_id);
printf("Firmware: %d.%d\n", info.firmware_version >> 8, info.firmware_version & 0xFF);
printf("Profiles: %d\n", info.num_profiles);
printf("Active Profile: %d\n", info.active_profile);
ret = azeron_device_get_stick_config(device, &stick);
if (ret == AZERON_SUCCESS) {
printf("\nStick Configuration:\n");
printf("--------------------\n");
printf("Mode: %s\n", azeron_stick_mode_string(stick.mode));
printf("Deadzone: %d%%\n", stick.deadzone);
printf("Sensitivity: %d\n", stick.sensitivity);
printf("Response Curve: %d\n", stick.response_curve);
printf("Invert X: %s\n", stick.invert_x ? "Yes" : "No");
printf("Invert Y: %s\n", stick.invert_y ? "Yes" : "No");
}
azeron_device_close(device);
azeron_exit();
@@ -237,62 +254,277 @@ int cmd_show_mappings(int argc, char *argv[])
printf("%-10s %-15s %s\n", "Button", "Type", "Mapping");
printf("%-10s %-15s %s\n", "------", "----", "-------");
/* For now, show placeholder since protocol isn't implemented yet */
for (i = 1; i <= 24; i++) {
printf("%-10d %-15s %s\n", i, "keyboard", "<not implemented yet>");
for (i = 0; i < 30; i++) {
struct azeron_button_mapping mapping;
ret = azeron_device_get_button_mapping(device, i, &mapping);
if (ret == AZERON_SUCCESS) {
printf("%-10d %-15s 0x%02x\n",
i + 1,
azeron_button_type_string(mapping.type),
mapping.key_code);
} else {
printf("%-10d %-15s <error: %s>\n",
i + 1,
"unknown",
azeron_error_string(ret));
}
}
printf("\nNote: Button mapping functionality requires protocol reverse engineering.\n");
printf("This is a placeholder implementation.\n");
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Map button (placeholder) */
/* Map button */
int cmd_map_button(int argc, char *argv[])
{
struct azeron_device *device;
struct azeron_button_mapping mapping;
int ret;
int device_index = 0;
int button_id;
const char *key_name;
if (argc < 3) {
fprintf(stderr, "Usage: %s map-button <button-id> <key>\n", argv[0]);
fprintf(stderr, "Example: %s map-button 5 KEY_W\n", argv[0]);
fprintf(stderr, "Usage: %s map-button <button-id> <key> [options]\n", argv[0]);
fprintf(stderr, "Example: %s map-button 1 KEY_W\n", argv[0]);
return 1;
}
fprintf(stderr, "Button mapping is not yet implemented.\n");
fprintf(stderr, "Protocol reverse engineering is required for this feature.\n");
button_id = atoi(argv[1]) - 1; /* 1-based to 0-based */
key_name = argv[2];
if (button_id < 0 || button_id >= 30) {
fprintf(stderr, "Error: Invalid button ID %d. Must be 1-30.\n", button_id + 1);
return 1;
}
/* Parse options */
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
optind = 3; /* Skip command, button-id and key */
while ((opt = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) {
switch (opt) {
case 'd':
device_index = atoi(optarg);
break;
case 'h':
printf("Usage: %s map-button <button-id> <key> [options]\n", argv[0]);
return 0;
default:
fprintf(stderr, "Unknown option. Use --help for usage.\n");
return 1;
}
}
ret = azeron_init();
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", azeron_error_string(ret));
return 1;
}
ret = azeron_device_open_index(&device, device_index);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to open device %d: %s\n", device_index, azeron_error_string(ret));
azeron_exit();
return 1;
}
/* Prepare mapping */
mapping.button_id = (uint8_t)button_id;
mapping.type = AZERON_BTN_KEYBOARD;
return 1;
int keycode = azeron_keycode_from_string(key_name);
if (keycode == -1) {
/* Try parsing as hex if string lookup fails */
if (strncmp(key_name, "0x", 2) == 0) {
mapping.key_code = (uint16_t)strtol(key_name, NULL, 16);
} else {
fprintf(stderr, "Error: Unknown key '%s'.\n", key_name);
azeron_device_close(device);
azeron_exit();
return 1;
}
} else {
mapping.key_code = (uint16_t)keycode;
}
printf("Mapping button %d to key 0x%02x (%s)...\n", button_id + 1, mapping.key_code, key_name);
ret = azeron_device_set_button_mapping(device, &mapping);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to set button mapping: %s\n", azeron_error_string(ret));
azeron_device_close(device);
azeron_exit();
return 1;
}
printf("Mapping updated successfully (Note: changes are temporary until saved with save-profile).\n");
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Set active profile (placeholder) */
/* Set active profile */
int cmd_set_profile(int argc, char *argv[])
{
struct azeron_device *device;
int ret;
int device_index = 0;
int profile_id;
if (argc < 2) {
fprintf(stderr, "Usage: %s set-profile <profile-id>\n", argv[0]);
fprintf(stderr, "Usage: %s set-profile <profile-id> [options]\n", argv[0]);
fprintf(stderr, "Example: %s set-profile 1\n", argv[0]);
return 1;
}
fprintf(stderr, "Profile switching is not yet implemented.\n");
fprintf(stderr, "Protocol reverse engineering is required for this feature.\n");
return 1;
}
/* Save profile (placeholder) */
int cmd_save_profile(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage: %s save-profile <profile-name>\n", argv[0]);
profile_id = atoi(argv[1]);
if (profile_id < 0 || profile_id > 2) {
fprintf(stderr, "Error: Invalid profile ID %d. Must be 0, 1 or 2.\n", profile_id);
return 1;
}
fprintf(stderr, "Profile saving is not yet implemented.\n");
fprintf(stderr, "Protocol reverse engineering is required for this feature.\n");
return 1;
/* Parse options */
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
optind = 2; /* Skip command and profile-id */
while ((opt = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) {
switch (opt) {
case 'd':
device_index = atoi(optarg);
break;
case 'h':
printf("Usage: %s set-profile <profile-id> [options]\n", argv[0]);
printf("Options:\n");
printf(" -d, --device <index> Select device by index (default: 0)\n");
printf(" -h, --help Show this help message\n");
return 0;
default:
fprintf(stderr, "Unknown option. Use --help for usage.\n");
return 1;
}
}
ret = azeron_init();
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", azeron_error_string(ret));
return 1;
}
ret = azeron_device_open_index(&device, device_index);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to open device %d: %s\n", device_index, azeron_error_string(ret));
azeron_exit();
return 1;
}
printf("Setting active profile to %d...\n", profile_id);
ret = azeron_device_set_active_profile(device, profile_id);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to set active profile: %s\n", azeron_error_string(ret));
azeron_device_close(device);
azeron_exit();
return 1;
}
printf("Active profile set to %d.\n", profile_id);
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Save profile */
int cmd_save_profile(int argc, char *argv[])
{
struct azeron_device *device;
int ret;
int device_index = 0;
int profile_id;
if (argc < 2) {
fprintf(stderr, "Usage: %s save-profile <profile-id> [options]\n", argv[0]);
fprintf(stderr, "Example: %s save-profile 0\n", argv[0]);
return 1;
}
profile_id = atoi(argv[1]);
if (profile_id < 0 || profile_id > 2) {
fprintf(stderr, "Error: Invalid profile ID %d. Must be 0, 1 or 2.\n", profile_id);
return 1;
}
/* Parse options */
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
optind = 2; /* Skip command and profile-id */
while ((opt = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) {
switch (opt) {
case 'd':
device_index = atoi(optarg);
break;
case 'h':
printf("Usage: %s save-profile <profile-id> [options]\n", argv[0]);
printf("Options:\n");
printf(" -d, --device <index> Select device by index (default: 0)\n");
printf(" -h, --help Show this help message\n");
return 0;
default:
fprintf(stderr, "Unknown option. Use --help for usage.\n");
return 1;
}
}
ret = azeron_init();
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", azeron_error_string(ret));
return 1;
}
ret = azeron_device_open_index(&device, device_index);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to open device %d: %s\n", device_index, azeron_error_string(ret));
azeron_exit();
return 1;
}
printf("Saving configuration to profile %d EEPROM...\n", profile_id);
ret = azeron_device_save_profile(device, profile_id);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to save profile: %s\n", azeron_error_string(ret));
azeron_device_close(device);
azeron_exit();
return 1;
}
printf("Profile %d saved successfully.\n", profile_id);
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Load profile (placeholder) */
@@ -337,6 +569,170 @@ int cmd_import_config(int argc, char *argv[])
return 1;
}
/* Set stick configuration */
int cmd_set_stick(int argc, char *argv[])
{
struct azeron_device *device;
struct azeron_stick_config stick;
int ret;
int device_index = 0;
/* Parse options */
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"deadzone", required_argument, 0, 'z'},
{"sensitivity", required_argument, 0, 's'},
{"curve", required_argument, 0, 'c'},
{"invert-x", no_argument, 0, 'x'},
{"invert-y", no_argument, 0, 'y'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
/* Initialize defaults from current config if possible, or zeros */
memset(&stick, 0, sizeof(stick));
bool dz_set = false, sens_set = false, curve_set = false;
while ((opt = getopt_long(argc, argv, "d:z:s:c:xyh", long_options, &option_index)) != -1) {
switch (opt) {
case 'd': device_index = atoi(optarg); break;
case 'z': stick.deadzone = atoi(optarg); dz_set = true; break;
case 's': stick.sensitivity = atoi(optarg); sens_set = true; break;
case 'c': stick.response_curve = atoi(optarg); curve_set = true; break;
case 'x': stick.invert_x = true; break;
case 'y': stick.invert_y = true; break;
case 'h':
printf("Usage: %s set-stick [options]\n", argv[0]);
printf("Options:\n");
printf(" -d, --device <index> Select device (default: 0)\n");
printf(" -z, --deadzone <0-100> Set deadzone percentage\n");
printf(" -s, --sensitivity <0-255> Set sensitivity\n");
printf(" -c, --curve <0-255> Set response curve\n");
printf(" -x, --invert-x Invert X axis\n");
printf(" -y, --invert-y Invert Y axis\n");
return 0;
default:
fprintf(stderr, "Unknown option. Use --help for usage.\n");
return 1;
}
}
ret = azeron_init();
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", azeron_error_string(ret));
return 1;
}
ret = azeron_device_open_index(&device, device_index);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to open device %d: %s\n", device_index, azeron_error_string(ret));
azeron_exit();
return 1;
}
/* Get current config to fill in unset values */
struct azeron_stick_config current;
ret = azeron_device_get_stick_config(device, &current);
if (ret == AZERON_SUCCESS) {
if (!dz_set) stick.deadzone = current.deadzone;
if (!sens_set) stick.sensitivity = current.sensitivity;
if (!curve_set) stick.response_curve = current.response_curve;
}
printf("Updating stick configuration...\n");
ret = azeron_device_set_stick_config(device, &stick);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to set stick config: %s\n", azeron_error_string(ret));
azeron_device_close(device);
azeron_exit();
return 1;
}
printf("Stick configuration updated successfully.\n");
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Read raw memory */
int cmd_read_raw(int argc, char *argv[])
{
struct azeron_device *device;
int ret;
int device_index = 0;
uint32_t offset;
size_t length = 16;
if (argc < 2) {
fprintf(stderr, "Usage: %s read-raw <offset> [length] [options]\n", argv[0]);
return 1;
}
offset = (uint32_t)strtol(argv[1], NULL, 0);
if (argc > 2) {
length = (size_t)atoi(argv[2]);
}
/* Parse options */
static struct option long_options[] = {
{"device", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
optind = 3;
while ((opt = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) {
switch (opt) {
case 'd':
device_index = atoi(optarg);
break;
case 'h':
printf("Usage: %s read-raw <offset> [length] [options]\n", argv[0]);
return 0;
}
}
ret = azeron_init();
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", azeron_error_string(ret));
return 1;
}
ret = azeron_device_open_index(&device, device_index);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to open device %d: %s\n", device_index, azeron_error_string(ret));
azeron_exit();
return 1;
}
uint8_t buffer[64];
size_t read_size = length > 58 ? 58 : length;
printf("Reading %zu bytes from offset 0x%04x...\n", read_size, offset);
ret = azeron_protocol_read_config(device, offset, buffer, &read_size);
if (ret != AZERON_SUCCESS) {
fprintf(stderr, "Failed to read memory: %s\n", azeron_error_string(ret));
} else {
printf("Data:");
for (size_t i = 0; i < read_size; i++) {
printf(" %02x", buffer[i]);
}
printf("\n");
}
azeron_device_close(device);
azeron_exit();
return 0;
}
/* Main function */
int main(int argc, char *argv[])
{