Update date of copyright notices
[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 struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
26 {
27         json_object *msg, *request;
28         const char *token, *uuid;
29         json_object *type_reply = NULL;
30
31         msg = json_object_new_object();
32         if (resp != NULL)
33                 json_object_object_add(msg, "response", resp);
34
35         type_reply = json_object_new_string("afb-reply");
36         json_object_object_add(msg, "jtype", type_reply);
37
38         request = json_object_new_object();
39         json_object_object_add(msg, "request", request);
40         json_object_object_add(request, "status", json_object_new_string(status));
41
42         if (info != NULL)
43                 json_object_object_add(request, "info", json_object_new_string(info));
44
45         if (reqid != NULL)
46                 json_object_object_add(request, "reqid", json_object_new_string(reqid));
47
48         if (context != NULL) {
49                 token = afb_context_sent_token(context);
50                 if (token != NULL)
51                         json_object_object_add(request, "token", json_object_new_string(token));
52
53                 uuid = afb_context_sent_uuid(context);
54                 if (uuid != NULL)
55                         json_object_object_add(request, "uuid", json_object_new_string(uuid));
56         }
57
58         return msg;
59 }
60
61 struct json_object *afb_msg_json_reply_ok(const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
62 {
63         return afb_msg_json_reply("success", info, resp, context, reqid);
64 }
65
66 struct json_object *afb_msg_json_reply_error(const char *status, const char *info, struct afb_context *context, const char *reqid)
67 {
68         return afb_msg_json_reply(status, info, NULL, context, reqid);
69 }
70
71 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
72 {
73         json_object *msg;
74         json_object *type_event = NULL;
75
76         msg = json_object_new_object();
77
78         json_object_object_add(msg, "event", json_object_new_string(event));
79
80         if (object != NULL)
81                 json_object_object_add(msg, "data", object);
82
83         type_event = json_object_new_string("afb-event");
84         json_object_object_add(msg, "jtype", type_event);
85
86         return msg;
87 }
88
89 struct json_object *afb_msg_json_internal_error()
90 {
91         return afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
92 }
93
94