Make possible to call a method from a binding
[src/app-framework-binder.git] / src / afb-msg-json.c
1 /*
2  * Copyright (C) 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-c/json.h>
21
22 #include "afb-msg-json.h"
23 #include "afb-context.h"
24
25
26 struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
27 {
28         json_object *msg, *request;
29         const char *token, *uuid;
30         static 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         if (type_reply == NULL)
37                 type_reply = json_object_new_string("afb-reply");
38         json_object_object_add(msg, "jtype", json_object_get(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(status));
43
44         if (info != NULL)
45                 json_object_object_add(request, "info", json_object_new_string(info));
46
47         if (reqid != NULL)
48                 json_object_object_add(request, "reqid", json_object_new_string(reqid));
49
50         if (context != NULL) {
51                 token = afb_context_sent_token(context);
52                 if (token != NULL)
53                         json_object_object_add(request, "token", json_object_new_string(token));
54
55                 uuid = afb_context_sent_uuid(context);
56                 if (uuid != NULL)
57                         json_object_object_add(request, "uuid", json_object_new_string(uuid));
58         }
59
60         return msg;
61 }
62
63 struct json_object *afb_msg_json_reply_ok(const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
64 {
65         return afb_msg_json_reply("success", info, resp, context, reqid);
66 }
67
68 struct json_object *afb_msg_json_reply_error(const char *status, const char *info, struct afb_context *context, const char *reqid)
69 {
70         return afb_msg_json_reply(status, info, NULL, context, reqid);
71 }
72
73 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
74 {
75         json_object *msg;
76         static json_object *type_event = NULL;
77
78         msg = json_object_new_object();
79
80         json_object_object_add(msg, "event", json_object_new_string(event));
81
82         if (object != NULL)
83                 json_object_object_add(msg, "data", object);
84
85         if (type_event == NULL)
86                 type_event = json_object_new_string("afb-event");
87         json_object_object_add(msg, "jtype", json_object_get(type_event));
88
89         return msg;
90 }
91