aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-07-20 19:02:53 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-07-26 09:57:07 +0200
commitac83c0e7fd32403b44d7ee3a9493c13ea89e9b58 (patch)
treeb8d153a0451f7ec2d30c9b1b5c0da4528ced95b5 /src
parentd864062bf79c1c086c1f0ab4f8664d16adabeae0 (diff)
Create new test api from a json filepath
Implement a new verb that start a new controller from a JSON filepath provided as argument. Change-Id: I498a0f24627357d93ac795cb034dc93dd0e84e62 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r--src/aft.c78
1 files changed, 46 insertions, 32 deletions
diff --git a/src/aft.c b/src/aft.c
index 1a5edc1..d72e165 100644
--- a/src/aft.c
+++ b/src/aft.c
@@ -27,6 +27,7 @@
#define CONTROL_PREFIX "aft"
// default api to print log when apihandle not avaliable
afb_dynapi *AFB_default;
+static int CtrlCreateApi(AFB_ApiT apiHandle, const char *configPath);
// Config Section definition
static CtlSectionT ctrlSections[] = {
@@ -45,8 +46,17 @@ static void ctrlapi_ping(AFB_ReqT request) {
AFB_ReqSuccess(request, json_object_new_int(count), NULL);
}
-static void ctrlapi_exit(AFB_ReqT request) {
+static void ctrlapi_load(AFB_ReqT request) {
+ const char *configPath = afb_req_value(request, "filepath");
+ afb_api_t apiHandle = afb_req_get_api(request);
+
+ if(!CtrlCreateApi(apiHandle, configPath))
+ AFB_ReqSuccess(request, NULL, NULL);
+ else
+ AFB_ReqFailF(request, "Error", "Not able to load test API with the configuration file: %s", configPath);
+}
+static void ctrlapi_exit(AFB_ReqT request) {
AFB_ReqNotice(request, "Exiting...");
AFB_ReqSuccess(request, NULL, NULL);
exit(0);
@@ -55,6 +65,7 @@ static void ctrlapi_exit(AFB_ReqT request) {
static AFB_ApiVerbs CtrlApiVerbs[] = {
/* VERB'S NAME FUNCTION TO CALL SHORT DESCRIPTION */
{.verb = "ping", .callback = ctrlapi_ping, .info = "ping test for API"},
+ {.verb = "load", .callback = ctrlapi_load, .info = "load a API meant to launch test for a binding"},
{.verb = "exit", .callback = ctrlapi_exit, .info = "Exit test"},
{.verb = NULL} /* marker for end of the array */
};
@@ -109,36 +120,12 @@ static int CtrlLoadOneApi(void *cbdata, AFB_ApiT apiHandle) {
return err;
}
-int afbBindingEntry(afb_dynapi *apiHandle) {
- int status, err = 0;
- size_t len = 0;
- char *dirList;
- const char *prefix = CONTROL_PREFIX, *envDirList = NULL, *configPath = NULL;
+static int CtrlCreateApi(AFB_ApiT apiHandle, const char *configPath) {
+ int err = 0;
json_object *resourcesJ = NULL, *eventsJ = NULL;
CtlConfigT *ctrlConfig = NULL;
- AFB_default = apiHandle;
-
- AFB_ApiNotice(apiHandle, "Controller in afbBindingEntry");
-
- envDirList = getEnvDirList(prefix, "CONFIG_PATH");
-
- if(envDirList) {
- len = strlen(CONTROL_CONFIG_PATH) + strlen(envDirList);
- dirList = malloc(len + 1);
- snprintf(dirList, len + 1, "%s:%s", envDirList, CONTROL_CONFIG_PATH);
- }
- else {
- dirList = CONTROL_CONFIG_PATH;
- }
-
- configPath = CtlConfigSearch(apiHandle, dirList, prefix);
- if(!configPath) {
- AFB_ApiError(apiHandle, "CtlPreInit: No %s* config found in %s ", GetBinderName(), dirList);
- return ERROR;
- }
-
- // load config file and create API
- ctrlConfig = CtlLoadMetaDataUsingPrefix(apiHandle, configPath, prefix);
+// load config file and create API
+ ctrlConfig = CtlLoadMetaDataUsingPrefix(apiHandle, configPath, CONTROL_PREFIX);
if(!ctrlConfig) {
AFB_ApiError(apiHandle,
"CtrlBindingDyn No valid control config file in:\n-- %s",
@@ -171,8 +158,35 @@ int afbBindingEntry(afb_dynapi *apiHandle) {
wrap_json_object_add(ctrlConfig->configJ, resourcesJ);
wrap_json_object_add(ctrlConfig->configJ, eventsJ);
- // create one API per config file (Pre-V3 return code ToBeChanged)
- status = afb_dynapi_new_api(apiHandle, ctrlConfig->api, ctrlConfig->info, 1, CtrlLoadOneApi, ctrlConfig);
+ err = afb_dynapi_new_api(apiHandle, ctrlConfig->api, ctrlConfig->info, 1, CtrlLoadOneApi, ctrlConfig);
+
+ return err;
+}
+
+int afbBindingEntry(afb_dynapi *apiHandle) {
+ size_t len = 0;
+ char *dirList;
+ const char *envDirList = NULL, *configPath = NULL;
+ AFB_default = apiHandle;
+
+ AFB_ApiNotice(apiHandle, "Controller in afbBindingEntry");
+
+ envDirList = getEnvDirList(CONTROL_PREFIX, "CONFIG_PATH");
+
+ if(envDirList) {
+ len = strlen(CONTROL_CONFIG_PATH) + strlen(envDirList);
+ dirList = malloc(len + 1);
+ snprintf(dirList, len + 1, "%s:%s", envDirList, CONTROL_CONFIG_PATH);
+ }
+ else {
+ dirList = CONTROL_CONFIG_PATH;
+ }
+
+ configPath = CtlConfigSearch(apiHandle, dirList, CONTROL_PREFIX);
+ if(!configPath) {
+ AFB_ApiError(apiHandle, "CtlPreInit: No %s* config found in %s ", GetBinderName(), dirList);
+ return ERROR;
+ }
- return status;
+ return CtrlCreateApi(apiHandle, configPath);
}