makes common code unic
[src/app-framework-binder.git] / src / afb-msg-json.c
1 /*
2  * Copyright 2016 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.h>
21
22 #include "afb-msg-json.h"
23
24
25 struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, const char *token, const char *uuid)
26 {
27         json_object *msg, *request;
28
29         request = json_object_new_object();
30         json_object_object_add(request, "status", json_object_new_string(status));
31         if (info != NULL)
32                 json_object_object_add(request, "info", json_object_new_string(info));
33         if (token != NULL)
34                 json_object_object_add(request, "token", json_object_new_string(token));
35         if (uuid != NULL)
36                 json_object_object_add(request, "uuid", json_object_new_string(uuid));
37
38         msg = json_object_new_object();
39         json_object_object_add(msg, "jtype", json_object_new_string("afb-reply"));
40         json_object_object_add(msg, "request", request);
41         if (resp != NULL)
42                 json_object_object_add(msg, "response", resp);
43
44         return msg;
45 }
46
47 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
48 {
49         json_object *msg;
50
51         msg = json_object_new_object();
52         json_object_object_add(msg, "jtype", json_object_new_string("afb-event"));
53         json_object_object_add(msg, "event", json_object_new_string(event));
54         if (object != NULL)
55                 json_object_object_add(msg, "data", object);
56
57         return msg;
58 }
59