summaryrefslogtreecommitdiff
path: root/cethcan
diff options
context:
space:
mode:
Diffstat (limited to 'cethcan')
-rw-r--r--cethcan/Makefile2
-rw-r--r--cethcan/cethcan.h5
-rw-r--r--cethcan/http.c26
m---------cethcan/jsonrpc0
-rw-r--r--cethcan/rpc.c31
5 files changed, 63 insertions, 1 deletions
diff --git a/cethcan/Makefile b/cethcan/Makefile
index 37af4d6..6acbbd7 100644
--- a/cethcan/Makefile
+++ b/cethcan/Makefile
@@ -5,7 +5,7 @@ PKGS="libevent jansson"
L_CFLAGS=-g -O0 -Wall -Wextra -Wshadow -pedantic -Wno-unused-parameter -Wno-format -std=gnu99 `pkg-config --cflags $(PKGS)` $(CFLAGS)
L_LDFLAGS=-g `pkg-config --libs $(PKGS)` -lcrypto $(LDFLAGS)
-cethcan: main.o can.o ether.o light.o beanctr.o http.o socketcan.o jsonrpc.o
+cethcan: main.o can.o ether.o light.o beanctr.o http.o socketcan.o jsonrpc.o rpc.o
gcc $(L_LDFLAGS) -o $@ $^
clean:
diff --git a/cethcan/cethcan.h b/cethcan/cethcan.h
index 4777f5f..73492cc 100644
--- a/cethcan/cethcan.h
+++ b/cethcan/cethcan.h
@@ -21,6 +21,7 @@
#include <assert.h>
#include <event2/event.h>
+#include <event2/buffer.h>
#include <jansson.h>
#include "protocol.h"
@@ -75,6 +76,10 @@ 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 void rpc_perform(struct evbuffer *request,
+ void (*response_handler)(void *arg, struct evbuffer *data),
+ void *handler_arg);
+
extern void json_bump_longpoll(void);
extern int socan_init(json_t *config);
diff --git a/cethcan/http.c b/cethcan/http.c
index f82d576..60e457c 100644
--- a/cethcan/http.c
+++ b/cethcan/http.c
@@ -66,6 +66,31 @@ out_inval:
evbuffer_free(out);
}
+static void http_jsonrpc_response(void *arg, struct evbuffer *data)
+{
+ struct evhttp_request *req = arg;
+ evhttp_send_reply_chunk(req, data);
+ evhttp_send_reply_end(req);
+}
+
+static void http_jsonrpc(struct evhttp_request *req, void *arg)
+{
+ struct evkeyvalq *outhdr = evhttp_request_get_output_headers(req);
+ struct evbuffer *inp = evhttp_request_get_input_buffer(req);
+
+ if (evhttp_request_get_command(req) != EVHTTP_REQ_POST) {
+ evhttp_send_error(req, 405, "JSON-RPC request must be POSTed");
+ return;
+ }
+
+ evhttp_add_header(outhdr, "Content-Type", "application/json; charset=utf-8");
+ evhttp_send_reply_start(req, 200, "OK");
+ evhttp_request_own(req);
+
+ rpc_perform(inp, http_jsonrpc_response, req);
+ return;
+}
+
static void http_json_bump(struct evhttp_request *req, void *arg)
{
struct evkeyvalq *outhdr = evhttp_request_get_output_headers(req);
@@ -182,6 +207,7 @@ void http_init(void)
evhttp_set_cb(evhttp, "/set", http_json_set, NULL);
evhttp_set_cb(evhttp, "/longpoll", http_json_longpoll, NULL);
evhttp_set_cb(evhttp, "/bump", http_json_bump, NULL);
+ evhttp_set_cb(evhttp, "/jsonrpc", http_jsonrpc, NULL);
evhttp_bind_socket(evhttp, "127.0.0.1", 34999);
longpoll_updatedata();
diff --git a/cethcan/jsonrpc b/cethcan/jsonrpc
-Subproject f1129ceb84c27f9d8c4ffb9b83786f76e501db8
+Subproject 29f49063f7c496a55945d1e8dfe6cb6f9744648
diff --git a/cethcan/rpc.c b/cethcan/rpc.c
new file mode 100644
index 0000000..57cb117
--- /dev/null
+++ b/cethcan/rpc.c
@@ -0,0 +1,31 @@
+#include "cethcan.h"
+
+#include <event2/buffer.h>
+#include "jsonrpc/jsonrpc.h"
+
+static int rpc_ping(void *apparg, json_t *json_params, json_t **result)
+{
+ *result = json_string("pong");
+ return 0;
+}
+
+struct jsonrpc_method_entry_t method_table[] = {
+ { "ping", rpc_ping, "" },
+ { NULL, NULL, NULL },
+};
+
+void rpc_perform(struct evbuffer *request,
+ void (*response_handler)(void *arg, struct evbuffer *data),
+ void *handler_arg)
+{
+ size_t len = evbuffer_get_length(request);
+ char *data = (char *)evbuffer_pullup(request, len);
+ struct evbuffer *outbuf = evbuffer_new();
+
+ /* TODO: asynchronous calls */
+ char *output = jsonrpc_handler(NULL, data, len, method_table);
+ if (output)
+ evbuffer_add(outbuf, output, strlen(output));
+ response_handler(handler_arg, outbuf);
+ evbuffer_free(outbuf);
+}