From bc6ee5e94d7338bebd2e8f2893111ed36b1f41e7 Mon Sep 17 00:00:00 2001 From: fastium Date: Sat, 6 Jun 2026 17:04:48 +0200 Subject: [PATCH] feat(MP/cli): refactor main --- src/06-mini-project/cli/main.c | 73 ++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/06-mini-project/cli/main.c b/src/06-mini-project/cli/main.c index 27f70db..4688dfe 100644 --- a/src/06-mini-project/cli/main.c +++ b/src/06-mini-project/cli/main.c @@ -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 : Set frequency in Hz (only works in Manual mode)\n", prog_name); + printf(" %s period : 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));