X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-wsj1.c;h=c8572a5075d9dd996c38ef5e6c283bb6ebebdef1;hb=refs%2Fchanges%2F43%2F16243%2F1;hp=7a8e023422bd1a6959ac82296b37c97b4a6bb53b;hpb=c4b603b2b28aec3ae58ce5e9500b07a1382d08f9;p=src%2Fapp-framework-binder.git diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c index 7a8e0234..c8572a50 100644 --- a/src/afb-wsj1.c +++ b/src/afb-wsj1.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 "IoT.bzh" + * Copyright (C) 2016, 2017, 2018 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,9 +26,13 @@ #include #include +#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE) +#define JSON_C_TO_STRING_NOSLASHESCAPE 0 +#endif #include "afb-ws.h" #include "afb-wsj1.h" +#include "fdev.h" #define CALL 2 #define RETOK 3 @@ -81,11 +85,13 @@ struct afb_wsj1 pthread_mutex_t mutex; }; -struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure) +struct afb_wsj1 *afb_wsj1_create(struct fdev *fdev, struct afb_wsj1_itf *itf, void *closure) { struct afb_wsj1 *result; - assert(fd >= 0); + assert(fdev); + assert(itf); + assert(itf->on_call); result = calloc(1, sizeof * result); if (result == NULL) @@ -100,7 +106,7 @@ struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1 if (result->tokener == NULL) goto error2; - result->ws = afb_ws_create(eloop, fd, &wsj1_itf, result); + result->ws = afb_ws_create(fdev, &wsj1_itf, result); if (result->ws == NULL) goto error3; @@ -111,7 +117,7 @@ error3: error2: free(result); error: - close(fd); + fdev_unref(fdev); return NULL; } @@ -337,7 +343,8 @@ static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size) free(call); break; case EVENT: - wsj1->itf->on_event(wsj1->closure, msg->event, msg); + if (wsj1->itf->on_event != NULL) + wsj1->itf->on_event(wsj1->closure, msg->event, msg); break; } afb_wsj1_msg_unref(msg); @@ -383,13 +390,15 @@ const char *afb_wsj1_msg_object_s(struct afb_wsj1_msg *msg) struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg) { + enum json_tokener_error jerr; struct json_object *object = msg->object_j; if (object == NULL) { pthread_mutex_lock(&msg->wsj1->mutex); json_tokener_reset(msg->wsj1->tokener); - object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, (int)msg->object_s_length); + object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, 1 + (int)msg->object_s_length); + jerr = json_tokener_get_error(msg->wsj1->tokener); pthread_mutex_unlock(&msg->wsj1->mutex); - if (object == NULL) { + if (jerr != json_tokener_success) { /* lazy error detection of json request. Is it to improve? */ object = json_object_new_string_len(msg->object_s, (int)msg->object_s_length); } @@ -467,7 +476,7 @@ static int wsj1_send_issot(struct afb_wsj1 *wsj1, int i1, const char *s1, const int afb_wsj1_send_event_j(struct afb_wsj1 *wsj1, const char *event, struct json_object *object) { - const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN); + const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE); int rc = afb_wsj1_send_event_s(wsj1, event, objstr); json_object_put(object); return rc; @@ -480,7 +489,7 @@ int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char * int afb_wsj1_call_j(struct afb_wsj1 *wsj1, const char *api, const char *verb, struct json_object *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure) { - const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN); + const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE); int rc = afb_wsj1_call_s(wsj1, api, verb, objstr, on_reply, closure); json_object_put(object); return rc; @@ -514,7 +523,7 @@ int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, co int afb_wsj1_reply_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token, int iserror) { - const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN); + const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE); int rc = afb_wsj1_reply_s(msg, objstr, token, iserror); json_object_put(object); return rc;