2 * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <json-c/json.h>
22 #include "afb-msg-json.h"
23 #include "afb-context.h"
25 static const char _success_[] = "success";
27 struct json_object *afb_msg_json_reply(struct json_object *resp, const char *error, const char *info, struct afb_context *context)
29 json_object *msg, *request;
30 const char *token, *uuid;
31 json_object *type_reply = NULL;
33 msg = json_object_new_object();
35 json_object_object_add(msg, "response", resp);
37 type_reply = json_object_new_string("afb-reply");
38 json_object_object_add(msg, "jtype", type_reply);
40 request = json_object_new_object();
41 json_object_object_add(msg, "request", request);
42 json_object_object_add(request, "status", json_object_new_string(error ?: _success_));
45 json_object_object_add(request, "info", json_object_new_string(info));
47 if (context != NULL) {
48 token = afb_context_sent_token(context);
50 json_object_object_add(request, "token", json_object_new_string(token));
52 uuid = afb_context_sent_uuid(context);
54 json_object_object_add(request, "uuid", json_object_new_string(uuid));
60 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
63 json_object *type_event = NULL;
65 msg = json_object_new_object();
67 json_object_object_add(msg, "event", json_object_new_string(event));
70 json_object_object_add(msg, "data", object);
72 type_event = json_object_new_string("afb-event");
73 json_object_object_add(msg, "jtype", type_event);