diff options
author | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-07-18 14:41:59 +0200 |
---|---|---|
committer | Jonathan Aillet <jonathan.aillet@iot.bzh> | 2019-07-18 17:32:26 +0200 |
commit | 7b37923625667b07ddade3a79c01ddbd358287a3 (patch) | |
tree | 07b6511b4f6c3d10d0a7e78462218466d8f54ded | |
parent | 09f002d060078d53e443e0f5806e147e035ac357 (diff) |
Add hal api handler utilities files
Add utilities files to create/delete hal apis while adding hal data
to hal data list.
Use it to create new hal apis.
BUG-AGL: SPEC-2652
Change-Id: I62e99cda31bb2c6f597da27960102c036a25b733
Signed-off-by: Jonathan Aillet <jonathan.aillet@iot.bzh>
-rw-r--r-- | lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.c | 154 | ||||
-rw-r--r-- | lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.h | 42 | ||||
-rw-r--r-- | lib/4a-hal-utilities/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/4a-internals-hal/4a-internals-hal-api-loader.c | 33 |
4 files changed, 203 insertions, 27 deletions
diff --git a/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.c b/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.c new file mode 100644 index 0000000..ac4b2ba --- /dev/null +++ b/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.c @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2019 "IoT.bzh" + * Author Jonathan Aillet <jonathan.aillet@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE + +#include <stdio.h> +#include <json-c/json.h> + +#include <urcu/list.h> + +#include <afb/afb-binding.h> + +#include <ctl-config.h> + +#include "4a-hal-utilities-data.h" +#include "4a-hal-utilities-hal-api-handler.h" + +/******************************************************************************* + * Add/Remove hal data and create/delete api functions * + ******************************************************************************/ + +int HalUtlAddHalDataAndCreateHalApi(afb_api_t apiHandle, + struct HalMgrData *halMgrData, + CtlConfigT *ctrlConfig, + int (*preinit)(void*, afb_api_t )) +{ + struct HalData *currentHalData; + + if(! apiHandle || ! halMgrData || ! ctrlConfig) { + AFB_API_ERROR(apiHandle, "Invalid argument(s)"); + return -1; + } + + // Allocation of current internal hal data + currentHalData = HalUtlAddHalToHalList(&halMgrData->halDataListHead); + if(! currentHalData) { + AFB_API_ERROR(apiHandle, "Didn't succeed to add hal to hal list"); + return -2; + } + + currentHalData->apiName = (char *) ctrlConfig->api; + + // Stores hal data in controller config + setExternalData(ctrlConfig, (void *) currentHalData); + + // Allocation of the structure that will be used to store internal hal data + currentHalData->internalHalData = calloc(1, sizeof(struct InternalHalData)); + if(! currentHalData->internalHalData) { + AFB_API_ERROR(apiHandle, "Didn't succeed to allocate internal hal data structure"); + return -3; + } + + CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->probedDevicesListHead); + CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->streamsDataListHead); + CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->halMapListHead); + + // Create one API + if(! afb_api_new_api(apiHandle, ctrlConfig->api, ctrlConfig->info, 1, preinit, ctrlConfig)) { + AFB_API_ERROR(apiHandle, "An error occurred at '%s' internal hal api creation", ctrlConfig->api); + return -4; + } + + return 0; +} + +int HalUtlRemoveHalDataAndDeleteHalApi(afb_api_t apiHandle, + struct HalData *halDataToDelete, + struct cds_list_head *halDataListHead) +{ + int err; + + if(! apiHandle || ! halDataToDelete || ! halDataListHead) { + AFB_API_ERROR(apiHandle, "Invalid argument(s)"); + return -1; + } + + afb_api_delete_api(halDataToDelete->internalHalData->apiHandle); + + err = HalUtlRemoveSelectedHalFromList(halDataListHead, halDataToDelete); + if(err) { + AFB_API_ERROR(apiHandle, + "Error %i happened while trying to remove '%s' internal hal data structure", + err, + halDataToDelete->apiName); + return -2; + } + + return 0; +} + +int HalUtlRemoveAllHalDataAndDeleteAllHalApi(afb_api_t apiHandle, + struct cds_list_head *halDataListHead) +{ + int err; + struct HalData *currentHalData, *savedHalData; + + if(! apiHandle || ! halDataListHead) { + AFB_API_ERROR(apiHandle, "Invalid argument(s)"); + return -1; + } + + cds_list_for_each_entry_safe(currentHalData, savedHalData, halDataListHead, node) { + err = HalUtlRemoveHalDataAndDeleteHalApi(apiHandle, currentHalData, halDataListHead); + if(err) { + AFB_API_ERROR(apiHandle, + "Error %i happened while trying to delete '%s' api and to remove its internal hal data structure", + err, + currentHalData->apiName); + return -2; + } + } + + return 0; +} + +int HalUtlRemoveHalMgrDataAndDeleteAllHalApi(afb_api_t apiHandle, + struct HalMgrData *halMgrData) +{ + int err; + + if(! apiHandle || ! halMgrData) { + AFB_API_ERROR(apiHandle, "Invalid argument(s)"); + return -1; + } + + err = HalUtlRemoveAllHalDataAndDeleteAllHalApi(apiHandle, &halMgrData->halDataListHead); + if(err) { + AFB_API_ERROR(apiHandle, + "Error %i happened while trying to remove all internal hal data structure and to delete all hal api", + err); + return -2; + } + + free(halMgrData->apiName); + free(halMgrData->info); + + free(halMgrData); + + return 0; +}
\ No newline at end of file diff --git a/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.h b/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.h new file mode 100644 index 0000000..0350c50 --- /dev/null +++ b/lib/4a-hal-utilities/4a-hal-utilities-hal-api-handler.h @@ -0,0 +1,42 @@ +/* +* Copyright (C) 2019 "IoT.bzh" +* Author Jonathan Aillet <jonathan.aillet@iot.bzh> +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _HAL_UTILITIES_HAL_API_HANDLER_INCLUDE_ +#define _HAL_UTILITIES_HAL_API_HANDLER_INCLUDE_ + +#include <stdio.h> + +#include <afb/afb-binding.h> + +#include <ctl-config.h> + +#include "4a-hal-utilities-data.h" + +// Add/Remove hal data and create/delete api functions +int HalUtlAddHalDataAndCreateHalApi(afb_api_t apiHandle, + struct HalMgrData *halMgrData, + CtlConfigT *ctrlConfig, + int (*preinit)(void*, afb_api_t )); +int HalUtlRemoveHalDataAndDeleteHalApi(afb_api_t apiHandle, + struct HalData *halDataToDelete, + struct cds_list_head *halDataListHead); +int HalUtlRemoveAllHalDataAndDeleteAllHalApi(afb_api_t apiHandle, + struct cds_list_head *halDataListHead); +int HalUtlRemoveHalMgrDataAndDeleteAllHalApi(afb_api_t apiHandle, + struct HalMgrData *halMgrData); + +#endif /* _HAL_UTILITIES_HAL_API_HANDLER_INCLUDE_ */
\ No newline at end of file diff --git a/lib/4a-hal-utilities/CMakeLists.txt b/lib/4a-hal-utilities/CMakeLists.txt index 9689565..48a81c5 100644 --- a/lib/4a-hal-utilities/CMakeLists.txt +++ b/lib/4a-hal-utilities/CMakeLists.txt @@ -23,6 +23,7 @@ PROJECT_TARGET_ADD(4a-hal-utilities) # Define project Targets ADD_LIBRARY(${TARGET_NAME} STATIC 4a-hal-utilities-data.c + 4a-hal-utilities-hal-api-handler.c 4a-hal-utilities-hal-streams-handler.c) # Define target includes for this target client diff --git a/src/4a-internals-hal/4a-internals-hal-api-loader.c b/src/4a-internals-hal/4a-internals-hal-api-loader.c index 34e82ef..71779d7 100644 --- a/src/4a-internals-hal/4a-internals-hal-api-loader.c +++ b/src/4a-internals-hal/4a-internals-hal-api-loader.c @@ -27,6 +27,8 @@ #include <ctl-config.h> +#include "4a-hal-utilities-hal-api-handler.h" + #include "4a-internals-hal-api-loader.h" #include "4a-internals-hal-alsacore-link.h" #include "4a-internals-hal-cb.h" @@ -166,8 +168,8 @@ static int InternalHalLoadOneApi(void *cbdata, afb_api_t apiHandle) int InternalHalCreateApi(afb_api_t apiHandle, char *path, struct HalMgrData *halMgrData) { + int err; CtlConfigT *ctrlConfig; - struct HalData *currentHalData; if(! apiHandle || ! path || ! halMgrData) return -1; @@ -184,35 +186,12 @@ int InternalHalCreateApi(afb_api_t apiHandle, char *path, struct HalMgrData *hal return -3; } - // Allocation of current internal hal data - currentHalData = HalUtlAddHalToHalList(&halMgrData->halDataListHead); - if(! currentHalData) { - AFB_API_ERROR(apiHandle, "Didn't succeed to add hal to hal list"); + err = HalUtlAddHalDataAndCreateHalApi(apiHandle, halMgrData, ctrlConfig, InternalHalLoadOneApi); + if(err) { + AFB_API_ERROR(apiHandle, "Error %i happened while trying to add hal api data and create new api", err); return -4; } - currentHalData->apiName = (char *) ctrlConfig->api; - - // Stores hal data in controller config - setExternalData(ctrlConfig, (void *) currentHalData); - - // Allocation of the structure that will be used to store internal hal data - currentHalData->internalHalData = calloc(1, sizeof(struct InternalHalData)); - if(! currentHalData->internalHalData) { - AFB_API_ERROR(apiHandle, "Didn't succeed to allocate internal hal data structure"); - return -5; - } - - CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->probedDevicesListHead); - CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->streamsDataListHead); - CDS_INIT_LIST_HEAD(¤tHalData->internalHalData->halMapListHead); - - // Create one API - if(! afb_api_new_api(apiHandle, ctrlConfig->api, ctrlConfig->info, 1, InternalHalLoadOneApi, ctrlConfig)) { - AFB_API_ERROR(apiHandle, "An error occurred at '%s' internal hal api creation", ctrlConfig->api); - return -6; - } - return 0; } |