Update copyright dates
[src/app-framework-binder.git] / src / afb-msg-json.c
1 /*
2  * Copyright (C) 2015-2020 "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         json_object *type_reply = NULL;
31
32         msg = json_object_new_object();
33         if (resp != NULL)
34                 json_object_object_add(msg, "response", resp);
35
36         type_reply = json_object_new_string("afb-reply");
37         json_object_object_add(msg, "jtype", type_reply);
38
39         request = json_object_new_object();
40         json_object_object_add(msg, "request", request);
41         json_object_object_add(request, "status", json_object_new_string(error ?: _success_));
42
43         if (info != NULL)
44                 json_object_object_add(request, "info", json_object_new_string(info));
45
46         return msg;
47 }
48
49 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
50 {
51         json_object *msg;
52         json_object *type_event = NULL;
53
54         msg = json_object_new_object();
55
56         json_object_object_add(msg, "event", json_object_new_string(event));
57
58         if (object != NULL)
59                 json_object_object_add(msg, "data", object);
60
61         type_event = json_object_new_string("afb-event");
62         json_object_object_add(msg, "jtype", type_event);
63
64         return msg;
65 }
66
67