feat(MP/cli): refactor main
This commit is contained in:
@@ -9,6 +9,12 @@
|
|||||||
/* Relative path to your common IPC definitions */
|
/* Relative path to your common IPC definitions */
|
||||||
#include "../common/common_ipc.h"
|
#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.
|
* 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("Temperature Regulator CLI\n");
|
||||||
printf("Usage:\n");
|
printf("Usage:\n");
|
||||||
printf(" %s mode <0|1> : Set mode (0 = Manual, 1 = Auto)\n", prog_name);
|
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 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(" %s dec : Decrease frequency (only works in Manual mode)\n", prog_name);
|
||||||
printf("\nExamples:\n");
|
printf("\nExamples:\n");
|
||||||
printf(" %s mode 1 (Switch to automatic mode)\n", prog_name);
|
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[]) {
|
static int process_arguments(int argc, char *argv[], ipc_msg_t *msg) {
|
||||||
int client_fd;
|
|
||||||
struct sockaddr_un server_addr;
|
|
||||||
ipc_msg_t msg = {0};
|
|
||||||
|
|
||||||
/* 1. Parse command line arguments */
|
if (strcmp(argv[1], MODE) == 0 && argc == 3) {
|
||||||
if (argc < 2) {
|
msg->command = CMD_SET_MODE;
|
||||||
print_usage(argv[0]);
|
msg->value = atoi(argv[2]);
|
||||||
return EXIT_FAILURE;
|
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");
|
printf("Error: Mode must be 0 (Manual) or 1 (Auto).\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (strcmp(argv[1], "freq") == 0 && argc == 3) {
|
else if (strcmp(argv[1], PERIOD) == 0 && argc == 3) {
|
||||||
msg.command = CMD_SET_FREQ;
|
msg->command = CMD_SET_PERIOD;
|
||||||
msg.value = atoi(argv[2]);
|
msg->value = atoi(argv[2]);
|
||||||
if (msg.value <= 0) {
|
if (msg->value <= 0) {
|
||||||
printf("Error: Frequency must be a positive integer.\n");
|
printf("Error: Frequency must be a positive integer.\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (strcmp(argv[1], "inc") == 0 && argc == 2) {
|
else if (strcmp(argv[1], INC) == 0 && argc == 2) {
|
||||||
msg.command = CMD_INC_FREQ;
|
msg->command = CMD_INC_PERIOD;
|
||||||
msg.value = 0; /* Ignored by daemon */
|
msg->value = 0; /* Ignored by daemon */
|
||||||
}
|
}
|
||||||
else if (strcmp(argv[1], "dec") == 0 && argc == 2) {
|
else if (strcmp(argv[1], DEC) == 0 && argc == 2) {
|
||||||
msg.command = CMD_DEC_FREQ;
|
msg->command = CMD_DEC_PERIOD;
|
||||||
msg.value = 0; /* Ignored by daemon */
|
msg->value = 0; /* Ignored by daemon */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Error: Invalid arguments.\n\n");
|
printf("Error: Invalid arguments.\n\n");
|
||||||
@@ -65,19 +62,37 @@ int main(int argc, char *argv[]) {
|
|||||||
return EXIT_FAILURE;
|
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);
|
client_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||||
if (client_fd < 0) {
|
if (client_fd < 0) {
|
||||||
perror("Error: Socket creation failed");
|
perror("Error: Socket creation failed");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. Prepare the destination address (the Daemon's socket) */
|
/* Prepare the destination address */
|
||||||
memset(&server_addr, 0, sizeof(server_addr));
|
memset(&server_addr, 0, sizeof(server_addr));
|
||||||
server_addr.sun_family = AF_UNIX;
|
server_addr.sun_family = AF_UNIX;
|
||||||
strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);
|
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,
|
ssize_t sent_bytes = sendto(client_fd, &msg, sizeof(msg), 0,
|
||||||
(struct sockaddr *)&server_addr, sizeof(server_addr));
|
(struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user