-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
116 lines (105 loc) · 3.02 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "client.h"
#include "gpio_pin.h"
#include "server.h"
#include "utils.h"
#define DEFAULT_MIN 40
#define DEFAULT_MAX 50
// Protos
void help(char *argv[]);
int main(int argc, char *argv[]) {
bool foreground = false;
bool start_server = false;
int default_min_temp = -1;
int default_max_temp = -1;
int opt;
char socket_path[256];
char *new_socket_path = getenv("RPIFANCTRL_SOCK");
// Set socket_path;
memset(socket_path, 0, 256);
if (new_socket_path == NULL) {
strcpy(socket_path, "/tmp/rpi-fan-ctrl.sock");
} else {
strcpy(socket_path, new_socket_path);
}
// Server arguments
while ((opt = getopt(argc, argv, "hdfu:l:")) != -1) {
switch (opt) {
case 'h':
help(argv);
exit(1);
case 'd':
start_server = true;
break;
case 'f':
foreground = true;
break;
case 'u':
if (!is_integer(optarg)) {
printf("'-u' Input must be an integer.\n");
exit(1);
}
if (strlen(optarg) != 2) {
printf("'-u' Input must be 2 digits.\n");
exit(1);
}
sscanf(optarg, "%2d", &default_max_temp);
break;
case 'l':
if (!is_integer(optarg)) {
printf("'-l' Input must be an integer.\n");
exit(1);
}
if (strlen(optarg) != 2) {
printf("'-l' Input must be 2 digits.\n");
exit(1);
}
sscanf(optarg, "%2d", &default_min_temp);
break;
default:
help(argv);
exit(1);
}
}
// Prevent server arguments from getting into the client arguments.
if (!start_server && foreground) {
printf("'-f' requires '-d'.\n");
exit(1);
}
if (!start_server && default_max_temp != -1) {
printf("'-u' requires '-d'.\n");
exit(1);
}
if (!start_server && default_min_temp != -1) {
printf("'-l' requires '-d'.\n");
exit(1);
}
if (start_server) {
if (default_max_temp == -1) {
default_max_temp = DEFAULT_MAX;
}
if (default_min_temp == -1) {
default_min_temp = DEFAULT_MIN;
}
server(socket_path, foreground, default_max_temp, default_min_temp);
return 0; // not really needed, but just to make sure we exit here.
}
client(socket_path, argc, argv);
}
void help(char *argv[]) {
printf("\nUsage: %s -h,-u <temp> -l <temp>\n\n", argv[0]);
printf(" '-h': Display this help text.\n");
printf(" '-d': Start daemon.\n\n");
printf(" Options that require '-d':\n");
printf(" '-f': Start daemon in foreground. (requires -d)\n");
printf(" '-u <temp>': The Fan will switch on if the\n");
printf(" CPU tempature goes above this value.");
printf(" (default is %d)\n\n", DEFAULT_MAX);
printf(" '-l <temp>': The Fan will turn off if the\n");
printf(" CPU tempature goes below this value.");
printf(" (default is %d)\n\n", DEFAULT_MIN);
}