Fedora 30 packaging fix issu
[src/app-framework-binder.git] / src / afb-msg-json.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <json-c/json.h>
21
22 #include "afb-msg-json.h"
23 #include "afb-context.h"
24
25 static const char _success_[] = "success";
26
27 struct json_object *afb_msg_json_reply(struct json_object *resp, const char *error, const char *info, struct afb_context *context)
28 {
29         json_object *msg, *request;
30         const char *token, *uuid;
31         json_object *type_reply = NULL;
32
33         msg = json_object_new_object();
34         if (resp != NULL)
35                 json_object_object_add(msg, "response", resp);
36
37         type_reply = json_object_new_string("afb-reply");
38         json_object_object_add(msg, "jtype", type_reply);
39
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_));
43
44         if (info != NULL)
45                 json_object_object_add(request, "info", json_object_new_string(info));
46
47         if (context != NULL) {
48                 token = afb_context_sent_token(context);
49                 if (token != NULL)
50                         json_object_object_add(request, "token", json_object_new_string(token));
51
52                 uuid = afb_context_sent_uuid(context);
53                 if (uuid != NULL)
54                         json_object_object_add(request, "uuid", json_object_new_string(uuid));
55         }
56
57         return msg;
58 }
59
60 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
61 {
62         json_object *msg;
63         json_object *type_event = NULL;
64
65         msg = json_object_new_object();
66
67         json_object_object_add(msg, "event", json_object_new_string(event));
68
69         if (object != NULL)
70                 json_object_object_add(msg, "data", object);
71
72         type_event = json_object_new_string("afb-event");
73         json_object_object_add(msg, "jtype", type_event);
74
75         return msg;
76 }
77
78