summaryrefslogtreecommitdiff
path: root/cethcan
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2013-03-25 23:29:15 +0100
committerDavid Lamparter <equinox@diac24.net>2013-03-25 23:29:15 +0100
commitac8beb0cb06e8aef35d9cfa134111e5c6ae09935 (patch)
tree3c9b6aba5fda143ad9aee2c0d36976d15c1540f5 /cethcan
parenta69a211a4a95e5c4ac0055a8f2de8ec1c56a7d1a (diff)
cethcan: json output
Diffstat (limited to 'cethcan')
-rw-r--r--cethcan/can.c8
-rw-r--r--cethcan/cethcan.h7
-rw-r--r--cethcan/http.c1
-rw-r--r--cethcan/light.c20
4 files changed, 36 insertions, 0 deletions
diff --git a/cethcan/can.c b/cethcan/can.c
index a36fd6c..5145871 100644
--- a/cethcan/can.c
+++ b/cethcan/can.c
@@ -50,6 +50,14 @@ void can_broadcast(struct can_user *origin, struct can_message *msg)
u->handler(u->arg, msg);
}
+void can_json(json_t *json, enum json_subtype type)
+{
+ struct can_user *u;
+ for (u = users; u; u = u->next)
+ if (u->json)
+ u->json(u->arg, json, type);
+}
+
void can_init(void)
{
/* nothing to do */
diff --git a/cethcan/cethcan.h b/cethcan/cethcan.h
index aa03d8b..64d185b 100644
--- a/cethcan/cethcan.h
+++ b/cethcan/cethcan.h
@@ -50,7 +50,12 @@ struct can_message {
uint8_t bytes[8];
};
+enum json_subtype {
+ JSON_NORMAL = 0,
+};
+
typedef void (*can_handler)(void *arg, struct can_message *msg);
+typedef void (*json_handler)(void *arg, json_t *json, enum json_subtype type);
struct can_user {
struct can_user *next;
@@ -59,12 +64,14 @@ struct can_user {
void *arg;
can_handler handler;
+ json_handler json;
};
extern void can_register(struct can_user *user);
extern struct can_user *can_register_alloc(void *arg, can_handler handler,
const char *fmt, ...);
extern void can_broadcast(struct can_user *origin, struct can_message *msg);
+extern void can_json(json_t *json, enum json_subtype type);
extern void can_init(void);
extern int ether_init(json_t *config);
diff --git a/cethcan/http.c b/cethcan/http.c
index 907ec08..e5f7ddb 100644
--- a/cethcan/http.c
+++ b/cethcan/http.c
@@ -19,6 +19,7 @@ static void http_json_basic(struct evhttp_request *req, void *arg)
evhttp_add_header(outhdr, "Content-Type", "text/plain; charset=utf-8");
json_t *jsout = json_object();
+ can_json(jsout, JSON_NORMAL);
json_dump_callback(jsout, evb_json_add, out, JSON_SORT_KEYS | JSON_INDENT(4));
evhttp_send_reply(req, 200, "OK", out);
evbuffer_free(out);
diff --git a/cethcan/light.c b/cethcan/light.c
index 2bc6b2f..cb3e94f 100644
--- a/cethcan/light.c
+++ b/cethcan/light.c
@@ -14,6 +14,25 @@ struct light {
struct value set, actual;
};
+static void light_json_handler(void *arg, json_t *json, enum json_subtype type)
+{
+ struct light *l = arg;
+ json_t *lobj = json_object();
+
+ json_object_set_new(lobj, "klass", json_string("light"));
+ json_object_set_new(lobj, "addr", json_integer(l->logical_addr));
+
+ json_object_set_new(lobj, "actual", json_integer(l->actual.val));
+ json_object_set_new(lobj, "actual_ts", json_integer(l->actual.valid));
+ json_object_set_new(lobj, "actual_tschg", json_integer(l->actual.change));
+
+ json_object_set_new(lobj, "set", json_integer(l->set.val));
+ json_object_set_new(lobj, "set_ts", json_integer(l->set.valid));
+ json_object_set_new(lobj, "set_tschg", json_integer(l->set.change));
+
+ json_object_set_new(json, l->name, lobj);
+}
+
static void light_can_handler(void *arg, struct can_message *msg)
{
struct light *l = arg;
@@ -65,5 +84,6 @@ int light_init_conf(json_t *config)
l->logical_addr = json_integer_value(json_object_get(config, "addr"));
l->u = can_register_alloc(l, light_can_handler, "light[%s]", l->name);
+ l->u->json = light_json_handler;
return 0;
}