aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-06-26 17:53:17 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-06-29 19:38:23 +0200
commit974e868702e875fd9d9bdea63fd62ac26bc408db (patch)
tree8c26425c8111f9ac8fcf1c98866d4c5912329431
parent6a107a42571395b1abc9cdaead577a2eb0683c95 (diff)
Be able to dispatch required api at the wanted time
Separate the require api step from ConfigExec and if called from anywhere but CtlConfigExec function, take care that not already initialized or that the previous has failed, so this is a new try. Change-Id: Ic98ef09487f7f58c1b1cb6c417eba261a5a81b13 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--ctl-lib/ctl-config.c37
-rw-r--r--ctl-lib/ctl-config.h1
2 files changed, 28 insertions, 10 deletions
diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c
index 8ef9a85..fcd0e02 100644
--- a/ctl-lib/ctl-config.c
+++ b/ctl-lib/ctl-config.c
@@ -104,28 +104,45 @@ char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefi
return NULL;
}
-static void DispatchRequireOneApi(AFB_ApiT apiHandle, json_object * bindindJ) {
+static int DispatchRequireOneApi(AFB_ApiT apiHandle, json_object * bindindJ) {
const char* requireBinding = json_object_get_string(bindindJ);
int err = AFB_RequireApi(apiHandle, requireBinding, 1);
if (err) {
AFB_ApiWarning(apiHandle, "CTL-LOAD-CONFIG:REQUIRE Fail to get=%s", requireBinding);
}
-}
-
-int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) {
- // best effort to initialise everything before starting
- if (ctlConfig->requireJ) {
+ return err;
+}
- if (json_object_get_type(ctlConfig->requireJ) == json_type_array) {
- for (int idx = 0; idx < json_object_array_length(ctlConfig->requireJ); idx++) {
- DispatchRequireOneApi(apiHandle, json_object_array_get_idx(ctlConfig->requireJ, idx));
+/**
+ * @brief Best effort to initialise everything before starting
+ * Call afb_require_api at the first call or if there was an error because
+ * the CtlConfigExec could be called anywhere and not in binding init.
+ * So you could call this function at init time.
+ *
+ * @param apiHandle : a afb_daemon api handle, see AFB_ApiT in afb_definitions.h
+ * @param requireJ : json_object array of api name required.
+ */
+void DispatchRequireApi(AFB_ApiT apiHandle, json_object * requireJ) {
+ static int init = 0, err = 0;
+ int idx;
+
+ if ( (! init || err) && requireJ) {
+ if (json_object_get_type(requireJ) == json_type_array) {
+ for (idx = 0; idx < json_object_array_length(requireJ); idx++) {
+ err += DispatchRequireOneApi(apiHandle, json_object_array_get_idx(requireJ, idx));
}
} else {
- DispatchRequireOneApi(apiHandle, ctlConfig->requireJ);
+ err += DispatchRequireOneApi(apiHandle, requireJ);
}
}
+ init = 1;
+}
+
+int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) {
+
+ DispatchRequireApi(apiHandle, ctlConfig->requireJ);
#ifdef CONTROL_SUPPORT_LUA
// load static LUA utilities
LuaConfigExec(apiHandle);
diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h
index 2249dad..8d1a770 100644
--- a/ctl-lib/ctl-config.h
+++ b/ctl-lib/ctl-config.h
@@ -111,6 +111,7 @@ json_object* CtlConfigScan(const char *dirList, const char *prefix) ;
*/
char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ);
char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ;
+void DispatchRequiredApi(AFB_ApiT apiHandle, json_object * requireJ);
int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ;
CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ;
int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections);