summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2017-07-10 04:02:40 +0000
committerMike Crute <mcrute@gmail.com>2017-07-10 04:02:40 +0000
commit48f5e1fae0258aa64bd7648b31edf142b2e6a9c0 (patch)
treecc985d5d0aafb17d48fdd4027328d082d09fbcf9
downloadmfi_agent-master.tar.bz2
mfi_agent-master.tar.xz
mfi_agent-master.zip
Initial importHEADmaster
-rw-r--r--.gitignore14
-rw-r--r--Makefile.am13
-rwxr-xr-xautogen.sh11
-rw-r--r--configure.ac84
-rw-r--r--main.c93
-rwxr-xr-xmake-mips.sh9
6 files changed, 224 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4526eed
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,14 @@
1/aclocal.m4
2/autom4te.cache/
3/compile
4/configure
5/depcomp
6/install-sh
7/Makefile.in
8/missing
9/.deps/
10/Makefile
11/config.log
12/config.status
13/mfi-agent
14*.o
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..a7f3405
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,13 @@
1bin_PROGRAMS = mfi-agent
2
3mfi_agent_SOURCES = \
4 main.c
5
6mfi_agent_CFLAGS = \
7 $(JSON_C_CFLAGS) \
8 $(WEBSOCKETS_CFLAGS) \
9 $(AM_CFLAGS)
10
11mfi_agent_LDADD = \
12 $(JSON_C_LIBS) \
13 $(WEBSOCKETS_LIBS)
diff --git a/autogen.sh b/autogen.sh
new file mode 100755
index 0000000..e3e1d42
--- /dev/null
+++ b/autogen.sh
@@ -0,0 +1,11 @@
1#!/bin/sh
2
3die()
4{
5 echo "$@" >&2
6 exit 1
7}
8
9aclocal || die "aclocal failed"
10automake --add-missing --force-missing --copy --foreign || die "automake failed"
11autoreconf || die "autoreconf failed"
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..2577a04
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,84 @@
1AC_INIT([mfi_agent], [1.0], [mike@crute.us])
2AC_PREREQ([2.60])
3
4AM_INIT_AUTOMAKE([foreign -Wall -Werror])
5AM_SILENT_RULES([yes])
6
7AC_PROG_CC
8AC_LANG_WERROR
9AC_PROG_CC_C99
10PKG_PROG_PKG_CONFIG
11AC_CHECK_HEADER_STDBOOL
12
13AC_SEARCH_LIBS(
14 [ev_run],
15 [ev],
16 [found_libev=yes],
17 [found_libev=no]
18)
19AC_CHECK_HEADER(
20 [ev.h],
21 ,
22 found_libev=no
23)
24if test "x$found_libev" = xno; then
25 AC_MSG_ERROR("libev not found")
26fi
27
28PKG_CHECK_MODULES(
29 JSON_C,
30 json-c,
31 [found_json_c=yes],
32 [found_json_c=no]
33)
34if test "x$found_json_c" = xno; then
35 AC_MSG_ERROR("libjson-c not found")
36fi
37
38PKG_CHECK_MODULES(
39 WEBSOCKETS,
40 libwebsockets,
41 [found_libwebsockets=yes],
42 [found_libwebsockets=no]
43)
44if test "x$found_libwebsockets" = xno; then
45 AC_MSG_ERROR("libwebsockets not found")
46fi
47
48AC_ARG_ENABLE(
49 debug,
50 AC_HELP_STRING([--enable-debug], [create a debug build])
51)
52AS_IF([test "x$enable_debug" = xyes], [
53 AM_CFLAGS="-g -DDEBUG $AM_CFLAGS"
54 AM_CFLAGS="$AM_CFLAGS -Wno-long-long -Wall -W -Wformat=2"
55 AM_CFLAGS="$AM_CFLAGS -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations"
56 AM_CFLAGS="$AM_CFLAGS -Wwrite-strings -Wshadow -Wpointer-arith -Wsign-compare"
57 AM_CFLAGS="$AM_CFLAGS -Wundef -Wbad-function-cast -Winline -Wcast-align"
58 AM_CFLAGS="$AM_CFLAGS -Wdeclaration-after-statement -Wno-pointer-sign -Wno-attributes"
59 AM_CFLAGS="$AM_CFLAGS -Wno-unused-result"
60])
61
62AC_ARG_ENABLE(
63 [production],
64 AC_HELP_STRING([--enable-production], [produce a production-ready build])
65)
66AS_IF([test "x$enable_production" = xyes], [
67 AM_CFLAGS="-s -Os $AM_CFLAGS"
68])
69
70if test "x$enable_debug" = xyes && test "x$enable_production" = xyes; then
71 AC_MSG_ERROR([Production and Debug are mutually exclusive options])
72fi
73
74# Always build static binaries to make it easier to deploy
75AM_LDFLAGS="-static $AM_LDFLAGS"
76
77AM_CFLAGS="-Wall -Werror $AM_CFLAGS"
78
79AC_SUBST(AM_CFLAGS)
80AC_SUBST(AM_LDFLAGS)
81
82AC_CONFIG_FILES([Makefile])
83
84AC_OUTPUT
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..b57fe33
--- /dev/null
+++ b/main.c
@@ -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
11struct 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
20int parse_output(int id, struct output_status *ret);
21void print_output(struct output_status *status);
22
23int 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
56void 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
66int 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}
diff --git a/make-mips.sh b/make-mips.sh
new file mode 100755
index 0000000..a6f64e9
--- /dev/null
+++ b/make-mips.sh
@@ -0,0 +1,9 @@
1#!/bin/sh
2
3export PATH="/home/mcrute/buildroot-2017.05.1/output/host/usr/bin/:$PATH"
4
5if ! grep 'CC = mips-buildroot-linux-uclibc-gcc' Makefile >/dev/null 2>&1; then
6 make clean
7 ./configure --build x86_64-linux-gnu --host mips-buildroot-linux-uclibc --enable-debug
8fi
9make