#include <ctype.h>
#include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
#define AFB_BINDING_VERSION 0
#include <afb/afb-binding.h>
#include "jobs.h"
#include "verbose.h"
#include "sig-monitor.h"
+#include "wrap-json.h"
/*************************************************************************
* internal types
/* event handler list */
struct event_handler *event_handlers;
+ /* settings */
+ struct json_object *settings;
+
/* internal descriptors */
union {
#if defined(WITH_LEGACY_BINDING_V1)
return to_api_x3(export);
}
+/******************************************************************************
+ ******************************************************************************
+ ******************************************************************************
+ ******************************************************************************
+ SETTINGS
+ ******************************************************************************
+ ******************************************************************************
+ ******************************************************************************
+ ******************************************************************************/
+
+static struct json_object *configuration;
+
+void afb_export_set_config(struct json_object *config)
+{
+ struct json_object *save = configuration;
+ configuration = json_object_get(config);
+ json_object_put(save);
+}
+
+static struct json_object *get_settings(const char *name)
+{
+ struct json_object *result;
+ struct json_object *obj;
+
+ if (json_object_object_get_ex(configuration, "*", &obj))
+ result = wrap_json_clone(obj);
+ else
+ result = json_object_new_object();
+
+ if (json_object_object_get_ex(configuration, name, &obj))
+ wrap_json_object_add(result, obj);
+
+ return result;
+}
+
/******************************************************************************
******************************************************************************
******************************************************************************
/* check daemon state */
if (export->state == Api_State_Pre_Init) {
- ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", export->api.apiname, name, json_object_to_json_string(object));
+ ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit",
+ export->api.apiname, name, json_object_to_json_string_ext(object, JSON_C_TO_STRING_NOSLASHESCAPE));
errno = EINVAL;
return 0;
}
return 0;
}
+static struct json_object *settings_cb(struct afb_api_x3 *api)
+{
+ struct afb_export *export = from_api_x3(api);
+ struct json_object *result = export->settings;
+ if (!result) {
+ result = get_settings(export->name);
+ export->settings = result;
+ }
+ return result;
+}
+
static int hooked_api_set_verbs_v2_cb(
struct afb_api_x3 *api,
const struct afb_verb_v2 *verbs)
return result;
}
+static struct json_object *hooked_settings_cb(struct afb_api_x3 *api)
+{
+ struct afb_export *export = from_api_x3(api);
+ struct json_object *result = settings_cb(api);
+ result = afb_hook_api_settings(export, result);
+ return result;
+}
+
static const struct afb_api_x3_itf api_x3_itf = {
.vverbose = (void*)vverbose_cb,
.class_require = class_require_cb,
.delete_api = delete_api_cb,
+ .settings = settings_cb,
};
static const struct afb_api_x3_itf hooked_api_x3_itf = {
.class_require = hooked_class_require_cb,
.delete_api = hooked_delete_api_cb,
+ .settings = hooked_settings_cb,
};
/******************************************************************************
}
/* create the event */
- handler = malloc(strlen(pattern) + strlen(pattern));
+ handler = malloc(strlen(pattern) + sizeof * handler);
if (!handler) {
ERROR("[API %s] can't allocate event handler %s", export->api.apiname, pattern);
errno = ENOMEM;
handler->callback = callback;
handler->closure = closure;
strcpy(handler->pattern, pattern);
- export->event_handlers = handler;
+ *previous = handler;
return 0;
}
afb_apiset_unref(export->call_set);
if (export->api.apiname != export->name)
free((void*)export->api.apiname);
+ json_object_put(export->settings);
free(export);
}
}
void afb_export_context_init(struct afb_export *export, struct afb_context *context)
{
afb_context_init(context, export->session, NULL);
+ context->validated = 1;
}