Created
April 5, 2017 11:58
-
-
Save carldunham/23d53832c620a2e04aefcc03db8f30b3 to your computer and use it in GitHub Desktop.
Issue with json_scanf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "common/cs_dbg.h" | |
#include "common/json_utils.h" | |
#include "common/platform.h" | |
#include "fw/src/mgos_app.h" | |
#include "fw/src/mgos_rpc.h" | |
static void mangle_params(struct mg_rpc_request_info *ri, void *cb_arg, | |
struct mg_rpc_frame_info *fi, | |
struct mg_str args) { | |
LOG(LL_DEBUG, ("args.p=%s, args.len=%d, ri->args_fmt=%s", args.p, args.len, ri->args_fmt)); | |
struct mbuf fb; | |
struct json_out out = JSON_OUT_MBUF(&fb); | |
mbuf_init(&fb, 50); | |
int num = -1; | |
bool fact1 = false, fact2 = false; | |
json_scanf(args.p, args.len, ri->args_fmt, &num, &fact1, &fact2); | |
if (num < 0) { | |
mg_rpc_send_errorf(ri, 400, "Invalid num"); | |
ri = NULL; | |
goto clean; | |
} | |
json_printf(&out, "{num: %d, fact1: %B, fact2: %B, or: %B, and: %B}", | |
num + 1, | |
fact1, | |
fact2, | |
fact1 || fact2, | |
fact1 && fact2 | |
); | |
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); | |
ri = NULL; | |
clean: | |
mbuf_free(&fb); | |
(void) cb_arg; | |
(void) fi; | |
} | |
enum mgos_app_init_result mgos_app_init(void) { | |
struct mg_rpc *c = mgos_rpc_get_global(); | |
mg_rpc_add_handler(c, "My.Test", "{num: %d, fact1:%B, fact2:%B}", mangle_params, NULL); | |
return MGOS_APP_INIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment