1
0

feat(MP/cli): refactor main

This commit is contained in:
2026-06-06 17:04:48 +02:00
committed by Fastium
parent bc9c4aee23
commit bc6ee5e94d

View File

@@ -9,6 +9,12 @@
/* Relative path to your common IPC definitions */
#include "../common/common_ipc.h"
#define MODE "mode"
#define PERIOD "period"
#define INC "inc"
#define DEC "dec"
/**
* print_usage() - Display the help menu for the CLI tool.
*/
@@ -16,48 +22,39 @@ static void print_usage(const char *prog_name) {
printf("Temperature Regulator CLI\n");
printf("Usage:\n");
printf(" %s mode <0|1> : Set mode (0 = Manual, 1 = Auto)\n", prog_name);
printf(" %s freq <hz> : Set frequency in Hz (only works in Manual mode)\n", prog_name);
printf(" %s period <ms> : Set period in ms (only works in Manual mode)\n", prog_name);
printf(" %s inc : Increase frequency (only works in Manual mode)\n", prog_name);
printf(" %s dec : Decrease frequency (only works in Manual mode)\n", prog_name);
printf("\nExamples:\n");
printf(" %s mode 1 (Switch to automatic mode)\n", prog_name);
printf(" %s freq 50 (Set frequency to 50 Hz)\n", prog_name);
printf(" %s period 500 (Set period to 500 ms)\n", prog_name);
}
int main(int argc, char *argv[]) {
int client_fd;
struct sockaddr_un server_addr;
ipc_msg_t msg = {0};
static int process_arguments(int argc, char *argv[], ipc_msg_t *msg) {
/* 1. Parse command line arguments */
if (argc < 2) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
if (strcmp(argv[1], "mode") == 0 && argc == 3) {
msg.command = CMD_SET_MODE;
msg.value = atoi(argv[2]);
if (msg.value != 0 && msg.value != 1) {
if (strcmp(argv[1], MODE) == 0 && argc == 3) {
msg->command = CMD_SET_MODE;
msg->value = atoi(argv[2]);
if (msg->value != 0 && msg->value != 1) {
printf("Error: Mode must be 0 (Manual) or 1 (Auto).\n");
return EXIT_FAILURE;
}
}
else if (strcmp(argv[1], "freq") == 0 && argc == 3) {
msg.command = CMD_SET_FREQ;
msg.value = atoi(argv[2]);
if (msg.value <= 0) {
else if (strcmp(argv[1], PERIOD) == 0 && argc == 3) {
msg->command = CMD_SET_PERIOD;
msg->value = atoi(argv[2]);
if (msg->value <= 0) {
printf("Error: Frequency must be a positive integer.\n");
return EXIT_FAILURE;
}
}
else if (strcmp(argv[1], "inc") == 0 && argc == 2) {
msg.command = CMD_INC_FREQ;
msg.value = 0; /* Ignored by daemon */
else if (strcmp(argv[1], INC) == 0 && argc == 2) {
msg->command = CMD_INC_PERIOD;
msg->value = 0; /* Ignored by daemon */
}
else if (strcmp(argv[1], "dec") == 0 && argc == 2) {
msg.command = CMD_DEC_FREQ;
msg.value = 0; /* Ignored by daemon */
else if (strcmp(argv[1], DEC) == 0 && argc == 2) {
msg->command = CMD_DEC_PERIOD;
msg->value = 0; /* Ignored by daemon */
}
else {
printf("Error: Invalid arguments.\n\n");
@@ -65,19 +62,37 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
/* 2. Create local Unix Domain Socket (Datagram) */
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
int client_fd;
struct sockaddr_un server_addr;
ipc_msg_t msg = {0};
/* Parse command line arguments */
if (argc < 2) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
if (process_arguments(argc, argv, &msg) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
/* Create local Unix Domain Socket (Datagram) */
client_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (client_fd < 0) {
perror("Error: Socket creation failed");
return EXIT_FAILURE;
}
/* 3. Prepare the destination address (the Daemon's socket) */
/* Prepare the destination address */
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);
/* 4. Send the binary structure to the Daemon */
/* Send the binary structure to the Daemon */
ssize_t sent_bytes = sendto(client_fd, &msg, sizeof(msg), 0,
(struct sockaddr *)&server_addr, sizeof(server_addr));