Make implementation multithread
[src/app-framework-binder.git] / src / afb-msg-json.c
1 /*
2  * Copyright (C) 2016, 2017 "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/afb-req-itf.h>
23
24 #include "afb-msg-json.h"
25 #include "afb-context.h"
26
27
28 struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
29 {
30         json_object *msg, *request;
31         const char *token, *uuid;
32         json_object *type_reply = NULL;
33
34         msg = json_object_new_object();
35         if (resp != NULL)
36                 json_object_object_add(msg, "response", resp);
37
38         type_reply = json_object_new_string("afb-reply");
39         json_object_object_add(msg, "jtype", type_reply);
40
41         request = json_object_new_object();
42         json_object_object_add(msg, "request", request);
43         json_object_object_add(request, "status", json_object_new_string(status));
44
45         if (info != NULL)
46                 json_object_object_add(request, "info", json_object_new_string(info));
47
48         if (reqid != NULL)
49                 json_object_object_add(request, "reqid", json_object_new_string(reqid));
50
51         if (context != NULL) {
52                 token = afb_context_sent_token(context);
53                 if (token != NULL)
54                         json_object_object_add(request, "token", json_object_new_string(token));
55
56                 uuid = afb_context_sent_uuid(context);
57                 if (uuid != NULL)
58                         json_object_object_add(request, "uuid", json_object_new_string(uuid));
59         }
60
61         return msg;
62 }
63
64 struct json_object *afb_msg_json_reply_ok(const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
65 {
66         return afb_msg_json_reply("success", info, resp, context, reqid);
67 }
68
69 struct json_object *afb_msg_json_reply_error(const char *status, const char *info, struct afb_context *context, const char *reqid)
70 {
71         return afb_msg_json_reply(status, info, NULL, context, reqid);
72 }
73
74 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
75 {
76         json_object *msg;
77         json_object *type_event = NULL;
78
79         msg = json_object_new_object();
80
81         json_object_object_add(msg, "event", json_object_new_string(event));
82
83         if (object != NULL)
84                 json_object_object_add(msg, "data", object);
85
86         type_event = json_object_new_string("afb-event");
87         json_object_object_add(msg, "jtype", type_event);
88
89         return msg;
90 }
91
92 struct afb_arg afb_msg_json_get_arg(struct json_object *object, const char *name)
93 {
94         struct afb_arg arg;
95         struct json_object *value;
96
97         if (json_object_object_get_ex(object, name, &value)) {
98                 arg.name = name;
99                 arg.value = json_object_get_string(value);
100         } else {
101                 arg.name = NULL;
102                 arg.value = NULL;
103         }
104         arg.path = NULL;
105         return arg;
106 }
107
108 struct json_object *afb_msg_json_internal_error()
109 {
110         static struct json_object *obj;
111
112         if (obj == NULL)
113                 obj = afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
114
115         return obj;
116 }
117
118