From 48f5e1fae0258aa64bd7648b31edf142b2e6a9c0 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 10 Jul 2017 04:02:40 +0000 Subject: Initial import --- .gitignore | 14 +++++++++ Makefile.am | 13 +++++++++ autogen.sh | 11 +++++++ configure.ac | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ make-mips.sh | 9 ++++++ 6 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile.am create mode 100755 autogen.sh create mode 100644 configure.ac create mode 100644 main.c create mode 100755 make-mips.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4526eed --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +/aclocal.m4 +/autom4te.cache/ +/compile +/configure +/depcomp +/install-sh +/Makefile.in +/missing +/.deps/ +/Makefile +/config.log +/config.status +/mfi-agent +*.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 @@ +bin_PROGRAMS = mfi-agent + +mfi_agent_SOURCES = \ + main.c + +mfi_agent_CFLAGS = \ + $(JSON_C_CFLAGS) \ + $(WEBSOCKETS_CFLAGS) \ + $(AM_CFLAGS) + +mfi_agent_LDADD = \ + $(JSON_C_LIBS) \ + $(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 @@ +#!/bin/sh + +die() +{ + echo "$@" >&2 + exit 1 +} + +aclocal || die "aclocal failed" +automake --add-missing --force-missing --copy --foreign || die "automake failed" +autoreconf || 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 @@ +AC_INIT([mfi_agent], [1.0], [mike@crute.us]) +AC_PREREQ([2.60]) + +AM_INIT_AUTOMAKE([foreign -Wall -Werror]) +AM_SILENT_RULES([yes]) + +AC_PROG_CC +AC_LANG_WERROR +AC_PROG_CC_C99 +PKG_PROG_PKG_CONFIG +AC_CHECK_HEADER_STDBOOL + +AC_SEARCH_LIBS( + [ev_run], + [ev], + [found_libev=yes], + [found_libev=no] +) +AC_CHECK_HEADER( + [ev.h], + , + found_libev=no +) +if test "x$found_libev" = xno; then + AC_MSG_ERROR("libev not found") +fi + +PKG_CHECK_MODULES( + JSON_C, + json-c, + [found_json_c=yes], + [found_json_c=no] +) +if test "x$found_json_c" = xno; then + AC_MSG_ERROR("libjson-c not found") +fi + +PKG_CHECK_MODULES( + WEBSOCKETS, + libwebsockets, + [found_libwebsockets=yes], + [found_libwebsockets=no] +) +if test "x$found_libwebsockets" = xno; then + AC_MSG_ERROR("libwebsockets not found") +fi + +AC_ARG_ENABLE( + debug, + AC_HELP_STRING([--enable-debug], [create a debug build]) +) +AS_IF([test "x$enable_debug" = xyes], [ + AM_CFLAGS="-g -DDEBUG $AM_CFLAGS" + AM_CFLAGS="$AM_CFLAGS -Wno-long-long -Wall -W -Wformat=2" + AM_CFLAGS="$AM_CFLAGS -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations" + AM_CFLAGS="$AM_CFLAGS -Wwrite-strings -Wshadow -Wpointer-arith -Wsign-compare" + AM_CFLAGS="$AM_CFLAGS -Wundef -Wbad-function-cast -Winline -Wcast-align" + AM_CFLAGS="$AM_CFLAGS -Wdeclaration-after-statement -Wno-pointer-sign -Wno-attributes" + AM_CFLAGS="$AM_CFLAGS -Wno-unused-result" +]) + +AC_ARG_ENABLE( + [production], + AC_HELP_STRING([--enable-production], [produce a production-ready build]) +) +AS_IF([test "x$enable_production" = xyes], [ + AM_CFLAGS="-s -Os $AM_CFLAGS" +]) + +if test "x$enable_debug" = xyes && test "x$enable_production" = xyes; then + AC_MSG_ERROR([Production and Debug are mutually exclusive options]) +fi + +# Always build static binaries to make it easier to deploy +AM_LDFLAGS="-static $AM_LDFLAGS" + +AM_CFLAGS="-Wall -Werror $AM_CFLAGS" + +AC_SUBST(AM_CFLAGS) +AC_SUBST(AM_LDFLAGS) + +AC_CONFIG_FILES([Makefile]) + +AC_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 @@ +#include +#include +#include + +#ifdef DEBUG +#define DEBUG_LOG(e) printf("DEBUG: %s\n", e); +#else +#define DEBUG_LOG(e) +#endif + +struct output_status { + char value[4]; + float active_power; + float current; + float voltage; + float power_factor; + float energy_sum; +}; + +int parse_output(int id, struct output_status *ret); +void print_output(struct output_status *status); + +int parse_output(int id, struct output_status *ret) +{ + int done; + FILE *fp; + char filename[13] = {0}; + + if (id > 9 || id < 1) { + DEBUG_LOG("Output identifier greater out of range"); + return 1; + } + + snprintf(filename, 12, "/dev/power%d", id); + + fp = fopen(filename, "r"); + if (fp == NULL) { + DEBUG_LOG("Error opening output device"); + return 1; + } + + done = fscanf(fp, "%s %f\n %f\n %f\n %f\n %f", + ret->value, &ret->active_power, + &ret->current, &ret->voltage, + &ret->power_factor, &ret->energy_sum); + fclose(fp); + + if (done != 6) { + DEBUG_LOG("parse_output did not receive enough fields"); + return 1; + } + + return 0; +} + +void print_output(struct output_status *status) +{ + printf("Value: %s\n", status->value); + printf("Active Power: %f\n", status->active_power); + printf("Current: %f\n", status->current); + printf("Voltage: %f\n", status->voltage); + printf("Power Factor: %f\n", status->power_factor); + printf("Energy Sum: %f\n", status->energy_sum); +} + +int main(int argc, char **argv) +{ + int ret; + int output; + struct output_status status; + + memset(&status, 0, sizeof(status)); + + if (argc != 2) { + printf("Pass output number\n"); + return 1; + } + + output = atoi(argv[1]); + if (output > 9 || output <= 0) { + printf("Output out of range\n"); + return 1; + } + + if ((ret = parse_output(output, &status)) != 0) { + printf("Failed to parse output\n"); + return 1; + } + + print_output(&status); + + return 0; +} 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 @@ +#!/bin/sh + +export PATH="/home/mcrute/buildroot-2017.05.1/output/host/usr/bin/:$PATH" + +if ! grep 'CC = mips-buildroot-linux-uclibc-gcc' Makefile >/dev/null 2>&1; then + make clean + ./configure --build x86_64-linux-gnu --host mips-buildroot-linux-uclibc --enable-debug +fi +make -- cgit v1.2.3