[collectd] [PATCH 2/5] sigrok: bare bones plugin

Bert Vermeulen bert at biot.com
Mon Jul 22 18:21:18 CEST 2013


---
 src/sigrok.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 109 insertions(+)
 create mode 100644 src/sigrok.c

diff --git a/src/sigrok.c b/src/sigrok.c
new file mode 100644
index 0000000..7189366
--- /dev/null
+++ b/src/sigrok.c
@@ -0,0 +1,109 @@
+/*
+ * collectd - src/sigrok.c
+ * Copyright (C) 2013 Bert Vermeulen <bert at biot.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libsigrok/libsigrok.h>
+
+static char *conn = NULL;
+static char *serialcomm = NULL;
+static int loglevel = SR_LOG_WARN;
+static struct sr_context *sr_ctx;
+
+static const char *config_keys[] = {
+	"loglevel",
+	"conn",
+	"serialcomm",
+};
+
+
+static int cd_logger(void *cb_data, int loglevel, const char *format, va_list args)
+{
+	char s[512];
+
+	vsnprintf(s, 512, format, args);
+	plugin_log(LOG_INFO, "sigrok: %s", s);
+
+	return 0;
+}
+
+static int plugin_config(const char *key, const char *value)
+{
+
+	if (!strcmp(key, "loglevel"))
+		loglevel = atoi(value);
+	else if (!strcmp(key, "conn"))
+		conn = strdup(value);
+	else if (!strcmp(key, "serialcomm"))
+		serialcomm = strdup(value);
+	else
+		return 1;
+
+	return 0;
+}
+
+static int plugin_init(void)
+{
+	int ret;
+
+	sr_log_callback_set(cd_logger, NULL);
+	sr_log_loglevel_set(loglevel);
+
+	if ((ret = sr_init(&sr_ctx)) != SR_OK) {
+		INFO("Failed to initialize libsigrok: %s.", sr_strerror(ret));
+		return 1;
+	}
+
+	return 0;
+}
+
+static int plugin_read(user_data_t *ud)
+{
+
+	return 0;
+}
+
+static int plugin_shutdown(void)
+{
+	if (conn)
+		free(conn);
+	if (serialcomm)
+		free(serialcomm);
+
+	sr_exit(sr_ctx);
+
+	return 0;
+}
+
+void module_register(void)
+{
+	struct timespec ts;
+
+	plugin_register_config("sigrok", plugin_config, config_keys,
+			STATIC_ARRAY_SIZE(config_keys));
+	plugin_register_init("sigrok", plugin_init);
+	plugin_register_shutdown("sigrok", plugin_shutdown);
+
+	ts.tv_sec = 0;
+	ts.tv_nsec = 1000000000L;
+	plugin_register_complex_read("sigrok", "sigrok", plugin_read, &ts, NULL);
+}
-- 
1.8.1.2




More information about the collectd mailing list