X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-wsj1.c;h=c8572a5075d9dd996c38ef5e6c283bb6ebebdef1;hb=e39610f8c9b2e6bbb8a460f7d7ccccbc5161b4ed;hp=5859bd3682d343eda1c5fa258d6c30c134430ba2;hpb=80551a0405e8a208af587b6fff7f784a1a2ffbf4;p=src%2Fapp-framework-binder.git diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c index 5859bd36..c8572a50 100644 --- a/src/afb-wsj1.c +++ b/src/afb-wsj1.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 "IoT.bzh" + * Copyright (C) 2016, 2017, 2018 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,11 +23,16 @@ #include #include #include +#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 @@ -77,13 +82,16 @@ struct afb_wsj1 struct afb_ws *ws; struct afb_wsj1_msg *messages; struct wsj1_call *calls; + pthread_mutex_t mutex; }; -struct afb_wsj1 *afb_wsj1_create(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) @@ -92,12 +100,13 @@ struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure result->refcount = 1; result->itf = itf; result->closure = closure; + pthread_mutex_init(&result->mutex, NULL); result->tokener = json_tokener_new(); if (result->tokener == NULL) goto error2; - result->ws = afb_ws_create(fd, &wsj1_itf, result); + result->ws = afb_ws_create(fdev, &wsj1_itf, result); if (result->ws == NULL) goto error3; @@ -108,19 +117,19 @@ error3: error2: free(result); error: - close(fd); + fdev_unref(fdev); return NULL; } void afb_wsj1_addref(struct afb_wsj1 *wsj1) { - if (wsj1 != NULL) - wsj1->refcount++; + if (wsj1) + __atomic_add_fetch(&wsj1->refcount, 1, __ATOMIC_RELAXED); } void afb_wsj1_unref(struct afb_wsj1 *wsj1) { - if (wsj1 != NULL && !--wsj1->refcount) { + if (wsj1 && !__atomic_sub_fetch(&wsj1->refcount, 1, __ATOMIC_RELAXED)) { afb_ws_destroy(wsj1->ws); json_tokener_free(wsj1->tokener); free(wsj1); @@ -134,9 +143,10 @@ static void wsj1_on_hangup(struct afb_wsj1 *wsj1) } -static struct wsj1_call *wsj1_call_search(struct afb_wsj1 *wsj1, const char *id, int remove) +static struct wsj1_call *wsj1_locked_call_search(struct afb_wsj1 *wsj1, const char *id, int remove) { struct wsj1_call *r, **p; + p = &wsj1->calls; while((r = *p) != NULL) { if (strcmp(r->id, id) == 0) { @@ -146,6 +156,18 @@ static struct wsj1_call *wsj1_call_search(struct afb_wsj1 *wsj1, const char *id, } p = &r->next; } + + return r; +} + +static struct wsj1_call *wsj1_call_search(struct afb_wsj1 *wsj1, const char *id, int remove) +{ + struct wsj1_call *r; + + pthread_mutex_lock(&wsj1->mutex); + r = wsj1_locked_call_search(wsj1, id, remove); + pthread_mutex_unlock(&wsj1->mutex); + return r; } @@ -155,15 +177,17 @@ static struct wsj1_call *wsj1_call_create(struct afb_wsj1 *wsj1, void (*on_reply if (call == NULL) errno = ENOMEM; else { + pthread_mutex_lock(&wsj1->mutex); do { if (wsj1->genid == 0) wsj1->genid = 999999; sprintf(call->id, "%d", wsj1->genid--); - } while (wsj1_call_search(wsj1, call->id, 0) != NULL); + } while (wsj1_locked_call_search(wsj1, call->id, 0) != NULL); call->callback = on_reply; call->closure = closure; call->next = wsj1->calls; wsj1->calls = call; + pthread_mutex_unlock(&wsj1->mutex); } return call; } @@ -301,10 +325,12 @@ static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size) msg->refcount = 1; afb_wsj1_addref(wsj1); msg->wsj1 = wsj1; + pthread_mutex_lock(&wsj1->mutex); msg->next = wsj1->messages; if (msg->next != NULL) msg->next->previous = msg; wsj1->messages = msg; + pthread_mutex_unlock(&wsj1->mutex); /* incoke the handler */ switch (msg->code) { @@ -317,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); @@ -333,19 +360,21 @@ alloc_error: void afb_wsj1_msg_addref(struct afb_wsj1_msg *msg) { if (msg != NULL) - msg->refcount++; + __atomic_add_fetch(&msg->refcount, 1, __ATOMIC_RELAXED); } void afb_wsj1_msg_unref(struct afb_wsj1_msg *msg) { - if (msg != NULL && --msg->refcount == 0) { + if (msg != NULL && !__atomic_sub_fetch(&msg->refcount, 1, __ATOMIC_RELAXED)) { /* unlink the message */ + pthread_mutex_lock(&msg->wsj1->mutex); if (msg->next != NULL) msg->next->previous = msg->previous; if (msg->previous == NULL) msg->wsj1->messages = msg->next; else msg->previous->next = msg->next; + pthread_mutex_unlock(&msg->wsj1->mutex); /* free ressources */ afb_wsj1_unref(msg->wsj1); json_object_put(msg->object_j); @@ -361,11 +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); - if (object == NULL) { + 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 (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); } @@ -424,6 +457,11 @@ struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg) return msg->wsj1; } +int afb_wsj1_close(struct afb_wsj1 *wsj1, uint16_t code, const char *text) +{ + return afb_ws_close(wsj1->ws, code, text); +} + static int wsj1_send_isot(struct afb_wsj1 *wsj1, int i1, const char *s1, const char *o1, const char *t1) { char code[2] = { (char)('0' + i1), 0 }; @@ -438,7 +476,10 @@ 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) { - return afb_wsj1_send_event_s(wsj1, event, 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; } int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object) @@ -448,7 +489,10 @@ 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) { - return afb_wsj1_call_s(wsj1, api, verb, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN), on_reply, closure); + 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; } int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure) @@ -479,8 +523,10 @@ 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); - return afb_wsj1_reply_s(msg, objstr, token, iserror); + 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; } int afb_wsj1_reply_s(struct afb_wsj1_msg *msg, const char *object, const char *token, int iserror)