+
+#endif
+
+static void jsoninit()
+{
+ int idx, verbosesav;
+
+ if (jTypeStatic)
+ return;
+
+ // initialise JSON constant messages and increase reference count to make them permanent
+ verbosesav = verbose;
+ verbose = 0; // run initialisation in silent mode
+ jTypeStatic = json_object_new_string ("AFB_message");
+ for (idx = 0; idx <= AFB_UNAUTH; idx++) {
+ AFBerr[idx].level = idx;
+ AFBerr[idx].label = ERROR_LABEL [idx];
+ AFBerr[idx].json = jsonNewMessage (idx, NULL);
+ }
+ verbose = verbosesav;
+}
+
+
+// build an ERROR message and return it as a valid json object
+json_object *json_add_status (json_object *obj, const char *status, const char *info)
+{
+ if (obj == NULL)
+ obj = json_object_new_object();
+ json_object_object_add(obj, "status", json_object_new_string(status));
+ if (info)
+ json_object_object_add(obj, "info", json_object_new_string(info));
+ return obj;
+}
+
+// build an ERROR message and return it as a valid json object
+json_object *json_add_status_v (json_object *obj, const char *status, const char *info, va_list args)
+{
+ char *message;
+ if (info == NULL || vasprintf(&message, info, args) < 0)
+ message = NULL;
+ obj = json_add_status(obj, status, message);
+ free(message);
+ return obj;
+}
+
+
+// build an ERROR message and return it as a valid json object
+json_object *json_add_status_f (json_object *obj, const char *status, const char *info, ...)
+{
+ va_list args;
+ va_start(args, info);
+ obj = json_add_status_v(obj, status, info, args);
+ va_end(args);
+ return obj;
+}
+
+
+
+// build an ERROR message and return it as a valid json object
+struct json_object *jsonNewMessage (AFB_error level, char* format, ...) {
+ static int count = 0;
+ json_object * AFBResponse;
+ va_list args;
+ char message [512];
+
+ jsoninit();
+
+ // format message
+ if (format != NULL) {
+ va_start(args, format);
+ vsnprintf (message, sizeof (message), format, args);
+ va_end(args);
+ }
+
+ AFBResponse = json_object_new_object();
+ json_object_object_add (AFBResponse, "jtype", json_object_get (jTypeStatic));
+ json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
+ if (format != NULL) {
+ json_object_object_add (AFBResponse, "info" , json_object_new_string (message));
+ }
+ if (verbose) {
+ fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
+ if (format != NULL) {
+ fprintf (stderr, "%s", message);
+ } else {
+ fprintf (stderr, "No Message");
+ }
+ fprintf (stderr, "\n");
+ }
+
+ return (AFBResponse);
+}
+
+#if 0
+{
+ jtype: "AFB_message"
+ request:
+ {
+ prefix: "",
+ api: "",
+ status: "", /* exist, fail, empty, null, processed */
+ info: "",
+ uuid: "",
+ token: "",
+ timeout: ""
+ }
+ response: ...
+}
+#endif
+