diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 93 |
1 files changed, 93 insertions, 0 deletions
@@ -0,0 +1,93 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <string.h> | ||
4 | |||
5 | #ifdef DEBUG | ||
6 | #define DEBUG_LOG(e) printf("DEBUG: %s\n", e); | ||
7 | #else | ||
8 | #define DEBUG_LOG(e) | ||
9 | #endif | ||
10 | |||
11 | struct output_status { | ||
12 | char value[4]; | ||
13 | float active_power; | ||
14 | float current; | ||
15 | float voltage; | ||
16 | float power_factor; | ||
17 | float energy_sum; | ||
18 | }; | ||
19 | |||
20 | int parse_output(int id, struct output_status *ret); | ||
21 | void print_output(struct output_status *status); | ||
22 | |||
23 | int parse_output(int id, struct output_status *ret) | ||
24 | { | ||
25 | int done; | ||
26 | FILE *fp; | ||
27 | char filename[13] = {0}; | ||
28 | |||
29 | if (id > 9 || id < 1) { | ||
30 | DEBUG_LOG("Output identifier greater out of range"); | ||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | snprintf(filename, 12, "/dev/power%d", id); | ||
35 | |||
36 | fp = fopen(filename, "r"); | ||
37 | if (fp == NULL) { | ||
38 | DEBUG_LOG("Error opening output device"); | ||
39 | return 1; | ||
40 | } | ||
41 | |||
42 | done = fscanf(fp, "%s %f\n %f\n %f\n %f\n %f", | ||
43 | ret->value, &ret->active_power, | ||
44 | &ret->current, &ret->voltage, | ||
45 | &ret->power_factor, &ret->energy_sum); | ||
46 | fclose(fp); | ||
47 | |||
48 | if (done != 6) { | ||
49 | DEBUG_LOG("parse_output did not receive enough fields"); | ||
50 | return 1; | ||
51 | } | ||
52 | |||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | void print_output(struct output_status *status) | ||
57 | { | ||
58 | printf("Value: %s\n", status->value); | ||
59 | printf("Active Power: %f\n", status->active_power); | ||
60 | printf("Current: %f\n", status->current); | ||
61 | printf("Voltage: %f\n", status->voltage); | ||
62 | printf("Power Factor: %f\n", status->power_factor); | ||
63 | printf("Energy Sum: %f\n", status->energy_sum); | ||
64 | } | ||
65 | |||
66 | int main(int argc, char **argv) | ||
67 | { | ||
68 | int ret; | ||
69 | int output; | ||
70 | struct output_status status; | ||
71 | |||
72 | memset(&status, 0, sizeof(status)); | ||
73 | |||
74 | if (argc != 2) { | ||
75 | printf("Pass output number\n"); | ||
76 | return 1; | ||
77 | } | ||
78 | |||
79 | output = atoi(argv[1]); | ||
80 | if (output > 9 || output <= 0) { | ||
81 | printf("Output out of range\n"); | ||
82 | return 1; | ||
83 | } | ||
84 | |||
85 | if ((ret = parse_output(output, &status)) != 0) { | ||
86 | printf("Failed to parse output\n"); | ||
87 | return 1; | ||
88 | } | ||
89 | |||
90 | print_output(&status); | ||
91 | |||
92 | return 0; | ||
93 | } | ||