Skip to content
Snippets Groups Projects
Commit b7a07483 authored by Sergey Lyubka's avatar Sergey Lyubka Committed by Cesanta Bot
Browse files

Ditch JSON-RPC from mongoose

PUBLISHED_FROM=89b978c02be2f10eb930ff13673d45249fd67763
parent 421b420a
No related branches found
No related tags found
No related merge requests found
Showing
with 14 additions and 365 deletions
......@@ -36,7 +36,6 @@ If you are looking for a complete solution with firmware and cloud components, c
- plain TCP, plain UDP, SSL/TLS (over TCP, one-way or two-way)
- HTTP client, HTTP server
- WebSocket client, WebSocket server
- JSON-RPC client, JSON-RPC server
- MQTT client, MQTT broker
- CoAP client, CoAP server
- DNS client, DNS server, async DNS resolver
......
......@@ -7,6 +7,5 @@ title: Disabling flags
- `MG_DISABLE_MQTT` disable MQTT support
- `MG_DISABLE_SHA1` disable SHA1 support (used by Websocket)
- `MG_DISABLE_MD5` disable MD5 support (used by HTTP auth)
- `MG_DISABLE_JSON_RPC` disable JSON-RPC support
- `MG_DISABLE_SOCKETPAIR` disable `mg_broadcast()` API
- `MG_DISABLE_HTTP_KEEP_ALIVE` useful for embedded systems to save resources
......@@ -14,9 +14,9 @@ flags. Also, some preprocessor flags can be used to tune internal Mongoose
parameters.
To set a preprocessor flag during compile time, use `-D <PREPROCESSOR_FLAG>`
compiler option. For example, to disable both MQTT and JSON-RPC,
compiler option. For example, to disable both MQTT and COAP,
compile the application `my_app.c` like this (assumed UNIX system):
```
$ cc my_app.c mongoose.c -D MG_DISABLE_MQTT -D MG_DISABLE_JSON_RPC
$ cc my_app.c mongoose.c -D MG_DISABLE_MQTT -D MG_DISABLE_COAP
```
......@@ -3,7 +3,6 @@ items:
- { type: dir, name: mbuf.h }
- { type: dir, name: net.h }
- { type: dir, name: http.h }
- { type: dir, name: json-rpc.h }
- { type: dir, name: dns.h }
- { type: dir, name: dns-server.h }
- { type: dir, name: mqtt.h }
......
---
title: "JSON-RPC"
symbol_kind: "intro"
decl_name: "json-rpc.h"
items:
- { type: file, name: mg_rpc_parse_reply.md }
- { type: file, name: mg_rpc_create_request.md }
- { type: file, name: mg_rpc_create_reply.md }
- { type: file, name: mg_rpc_create_error.md }
- { type: file, name: mg_rpc_create_std_error.md }
- { type: file, name: mg_rpc_dispatch.md }
- { type: file, name: struct_mg_rpc_request.md }
- { type: file, name: struct_mg_rpc_reply.md }
- { type: file, name: struct_mg_rpc_error.md }
---
---
title: "mg_rpc_create_error()"
decl_name: "mg_rpc_create_error"
symbol_kind: "func"
signature: |
int mg_rpc_create_error(char *buf, int len, struct mg_rpc_request *req,
int code, const char *message, const char *fmt, ...);
---
Create JSON-RPC error reply in a given buffer.
Return length of the error, which
can be larger then `len` that indicates an overflow.
`fmt` format string should conform to `json_emit()` API,
see https://github.com/cesanta/frozen
---
title: "mg_rpc_create_reply()"
decl_name: "mg_rpc_create_reply"
symbol_kind: "func"
signature: |
int mg_rpc_create_reply(char *buf, int len, const struct mg_rpc_request *req,
const char *result_fmt, ...);
---
Create JSON-RPC reply in a given buffer.
Return length of the reply, which
can be larger then `len` that indicates an overflow.
`result_fmt` format string should conform to `json_emit()` API,
see https://github.com/cesanta/frozen
---
title: "mg_rpc_create_request()"
decl_name: "mg_rpc_create_request"
symbol_kind: "func"
signature: |
int mg_rpc_create_request(char *buf, int len, const char *method,
const char *id, const char *params_fmt, ...);
---
Create JSON-RPC request in a given buffer.
Return length of the request, which
can be larger then `len` that indicates an overflow.
`params_fmt` format string should conform to `json_emit()` API,
see https://github.com/cesanta/frozen
---
title: "mg_rpc_create_std_error()"
decl_name: "mg_rpc_create_std_error"
symbol_kind: "func"
signature: |
int mg_rpc_create_std_error(char *buf, int len, struct mg_rpc_request *req,
int code);
---
Create JSON-RPC error in a given buffer.
Return length of the error, which
can be larger then `len` that indicates an overflow. See
JSON_RPC_*_ERROR definitions for standard error values:
- `#define JSON_RPC_PARSE_ERROR (-32700)`
- `#define JSON_RPC_INVALID_REQUEST_ERROR (-32600)`
- `#define JSON_RPC_METHOD_NOT_FOUND_ERROR (-32601)`
- `#define JSON_RPC_INVALID_PARAMS_ERROR (-32602)`
- `#define JSON_RPC_INTERNAL_ERROR (-32603)`
- `#define JSON_RPC_SERVER_ERROR (-32000)`
---
title: "mg_rpc_dispatch()"
decl_name: "mg_rpc_dispatch"
symbol_kind: "func"
signature: |
int mg_rpc_dispatch(const char *buf, int, char *dst, int dst_len,
const char **methods, mg_rpc_handler_t *handlers);
---
Dispatches a JSON-RPC request.
Parses JSON-RPC request contained in `buf`, `len`.
Then, dispatches the request to the correct handler method.
Valid method names should be specified in NULL
terminated array `methods`, and corresponding handlers in `handlers`.
Result is put in `dst`, `dst_len`. Return: length of the result, which
can be larger then `dst_len` that indicates an overflow.
Overflown bytes are not written to the buffer.
If method is not found, an error is automatically generated.
---
title: "mg_rpc_parse_reply()"
decl_name: "mg_rpc_parse_reply"
symbol_kind: "func"
signature: |
int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
int max_toks, struct mg_rpc_reply *,
struct mg_rpc_error *);
---
Parse JSON-RPC reply contained in `buf`, `len` into JSON tokens array
`toks`, `max_toks`. If buffer contains valid reply, `reply` structure is
populated. The result of RPC call is located in `reply.result`. On error,
`error` structure is populated. Returns: the result of calling
`parse_json(buf, len, toks, max_toks)`:
On success, an offset inside `json_string` is returned
where parsing has finished. On failure, a negative number is
returned, one of:
- `#define JSON_STRING_INVALID -1`
- `#define JSON_STRING_INCOMPLETE -2`
- `#define JSON_TOKEN_ARRAY_TOO_SMALL -3`
---
title: "struct mg_rpc_error"
decl_name: "struct mg_rpc_error"
symbol_kind: "struct"
signature: |
struct mg_rpc_error {
struct json_token *message; /* Whole RPC message */
struct json_token *id; /* Message ID */
struct json_token *error_code; /* error.code */
struct json_token *error_message; /* error.message */
struct json_token *error_data; /* error.data, can be NULL */
};
---
JSON-RPC error
---
title: "struct mg_rpc_reply"
decl_name: "struct mg_rpc_reply"
symbol_kind: "struct"
signature: |
struct mg_rpc_reply {
struct json_token *message; /* Whole RPC message */
struct json_token *id; /* Message ID */
struct json_token *result; /* Remote call result */
};
---
JSON-RPC response
---
title: "struct mg_rpc_request"
decl_name: "struct mg_rpc_request"
symbol_kind: "struct"
signature: |
struct mg_rpc_request {
struct json_token *message; /* Whole RPC message */
struct json_token *id; /* Message ID */
struct json_token *method; /* Method name */
struct json_token *params; /* Method params */
};
---
JSON-RPC request
......@@ -4,7 +4,7 @@ title: Overview
Mongoose is a swiss army knife for embedded network programming.
It implements event-driven non-blocking API for TCP, UDP, HTTP,
WebSocket, CoAP, MQTT, JSON-RPC for both client and server mode.
WebSocket, CoAP, MQTT for both client and server mode.
Features include:
- Cross-platform: works on Linux/UNIX, MacOS, QNX, eCos, Windows, Android,
......@@ -16,7 +16,6 @@ Features include:
- plain TCP, plain UDP, SSL/TLS (over TCP, one-way or two-way)
- HTTP client and server
- WebSocket client and server
- JSON-RPC client and server
- MQTT client and server
- CoAP client and server
- DNS client and server
......
......@@ -95,27 +95,19 @@ static double send_acc_data_since(struct mg_connection *nc,
static void process_command(struct mg_connection *nc, unsigned char *data,
size_t len) {
struct json_token *toks = parse_json2((const char *) data, len);
if (toks == NULL) {
// TODO(lsm): use proper JSON parser
int cmd, n, val;
double t;
if (sscanf((char *) data, "{\t\": %d, \"ts\": %lf, %n", &cmd, &t, &n) != 2) {
LOG(LL_ERROR, ("Invalid command: %.*s", (int) len, data));
return;
}
struct json_token *t = find_json_token(toks, "t");
if (t == NULL) {
LOG(LL_ERROR, ("Missing type field: %.*s", (int) len, data));
goto out_free;
}
if (t->len == 1 && *t->ptr == '1') {
struct json_token *v = find_json_token(toks, "v");
if (v == NULL) {
if (t == 1) {
if (sscanf((char *) data + n, "\"v\": %d", &val) != 1) {
LOG(LL_ERROR, ("Missing value: %.*s", (int) len, data));
goto out_free;
}
if (v->len != 1) {
LOG(LL_ERROR, ("Invalid value: %.*s", (int) len, data));
goto out_free;
return;
}
switch (*v->ptr) {
switch (val) {
case '0': {
GPIO_IF_LedOff(MCU_RED_LED_GPIO);
break;
......@@ -130,15 +122,13 @@ static void process_command(struct mg_connection *nc, unsigned char *data,
}
default: {
LOG(LL_ERROR, ("Invalid value: %.*s", (int) len, data));
goto out_free;
return;
}
}
} else {
LOG(LL_ERROR, ("Unknown command: %.*s", (int) t->len, t->ptr));
goto out_free;
LOG(LL_ERROR, ("Unknown command: %.*s", (int) len, data));
return;
}
out_free:
free(toks);
}
void data_conn_handler(struct mg_connection *nc, int ev, void *ev_data) {
......
PROG = json_rpc_server
include ../examples.mk
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*
* To test this server, do
* $ curl -d '{"id":1,method:"sum",params:[22,33]}' 127.0.0.1:8000
*/
#include "mongoose.h"
static const char *s_http_port = "8000";
static int rpc_sum(char *buf, int len, struct mg_rpc_request *req) {
double sum = 0;
int i;
if (req->params[0].type != JSON_TYPE_ARRAY) {
return mg_rpc_create_std_error(buf, len, req,
JSON_RPC_INVALID_PARAMS_ERROR);
}
for (i = 0; i < req->params[0].num_desc; i++) {
if (req->params[i + 1].type != JSON_TYPE_NUMBER) {
return mg_rpc_create_std_error(buf, len, req,
JSON_RPC_INVALID_PARAMS_ERROR);
}
sum += strtod(req->params[i + 1].ptr, NULL);
}
return mg_rpc_create_reply(buf, len, req, "f", sum);
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message *) ev_data;
static const char *methods[] = {"sum", NULL};
static mg_rpc_handler_t handlers[] = {rpc_sum, NULL};
char buf[100];
switch (ev) {
case MG_EV_HTTP_REQUEST:
mg_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf), methods,
handlers);
mg_printf(nc,
"HTTP/1.0 200 OK\r\nContent-Length: %d\r\n"
"Content-Type: application/json\r\n\r\n%s",
(int) strlen(buf), buf);
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
default:
break;
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_set_protocol_http_websocket(nc);
printf("Starting JSON-RPC server on port %s\n", s_http_port);
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
NS=../../mongoose.c
FLAGS = ../../mongoose.c -I../..
CFLAGS=-W -Wall -DMG_ENABLE_THREADS -pthread $(CFLAGS_EXTRA)
PROGS = device_side cloud_side
all: $(PROGS)
device_side: Makefile device_side.c $(NS)
$(CC) device_side.c $(FLAGS) -o $@ $(CFLAGS)
cloud_side: Makefile cloud_side.c $(NS)
$(CC) cloud_side.c $(FLAGS) -o $@ $(CFLAGS)
device_side.exe: Makefile device_side.c $(NS)
cl device_side.c $(FLAGS) /MD /Fe$@
clean:
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROGS)
= Raspberry Pi camera/LED demo
== Overview
The link:/[demo] consists of web app providing access to a webcam and a LED attached to a RaspberryPi.
The device is assumed to have a limited bandwidth towards the server hosting the web app.
== Objective
The demo shows how to use websockets to communicate bidirectionally with an embedded device using standard protocols.
It also shows that it's possible to use Smart.c to develop also the cloud endpoint and expose WebSocket and RESTful APIs
easy to integreate with modern web stacks.
== How it works
image::docs/arch.png[]
There are two components, once with runs on the device (`device_side`) and one that runs on a stronger machine
and with more bandwidth (`cloud_side`).
The device app connects to the cloud app via websocket and sends a new jpeg frame as fast as the underlying `raspistill` camera
grabbing application can handle. The device automatically attempts reconnecting.
The cloud side serves the webapp static pages and serves an MPJEG image on `/mpjg`.
The MPJEG image handler blocks all the clients until a JPEG frame arrives via websocket
and then every client will receive a copy of the frame.
The web app can turn on and off the LED via a RESTful api accessible via the `/api` handler.
== Installation
=== Server side
----
git clone https://github.com/cesanta/mongoose
cd mongoose/examples/web_demo
make cloud_side && ./cloud_side 0.0.0.0:8080
----
=== Raspberry Pi
The instructions provided here are tailored for the Raspbian distribution.
==== Dependencies
jpegoptim::
apt-get install jpegoptim
camera::
run raspi-config and enable camera
==== LED
In order to access the led on your link:http://www.qdh.org.uk/wordpress/?page_id=15[HotPi]
board you need to export the gpio pins:
----
for i in 22 23 24; do
echo $i >/sys/class/gpio/export
echo out >/sys/class/gpio/gpio$i/direction
chgrp pi /sys/class/gpio/gpio$i/value
done
----
==== Build and run
----
git clone https://github.com/cesanta/mongoose
cd mongoose/examples/web_demo
make device_side && ./device_side yourserver:8080
----
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment