Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fcc_bundle_config_params_utils.c Source File

fcc_bundle_config_params_utils.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //  
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //  
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //  
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 #ifndef USE_TINY_CBOR
00017 #include "fcc_bundle_handler.h"
00018 #include "cn-cbor.h"
00019 #include "pv_error_handling.h"
00020 #include "fcc_bundle_utils.h"
00021 #include "key_config_manager.h"
00022 #include "fcc_defs.h"
00023 #include "fcc_output_info_handler.h"
00024 #include "factory_configurator_client.h"
00025 #include "general_utils.h"
00026 #include "fcc_time_profiling.h"
00027 #include "fcc_utils.h"
00028 
00029 static fcc_status_e  set_time_from_config_param(const fcc_bundle_data_param_s *current_time)
00030 {
00031 
00032     fcc_status_e  status;
00033     uint64_t time = 0;
00034 
00035     SA_PV_ERR_RECOVERABLE_RETURN_IF((current_time == NULL), FCC_STATUS_INVALID_PARAMETER, "Got invalid or corrupted 'current_time' pointer");
00036 
00037     // Check given time length before copying
00038     SA_PV_ERR_RECOVERABLE_RETURN_IF((current_time->data_size > sizeof(uint64_t)), FCC_STATUS_MEMORY_OUT, "Time length (%" PRIu32 "B) too long (corrupted format?)", (uint32_t)current_time->data_size);
00039     memcpy(&time, current_time->data, current_time->data_size);
00040 
00041     status = fcc_time_set(time);
00042     SA_PV_ERR_RECOVERABLE_RETURN_IF((status != FCC_STATUS_SUCCESS), FCC_STATUS_ERROR, "fcc_time_set failed");
00043 
00044 
00045     return FCC_STATUS_SUCCESS;
00046 }
00047 
00048 fcc_status_e  fcc_bundle_process_config_params(const cn_cbor *config_params_list_cb)
00049 {
00050 
00051     bool success = false;
00052     fcc_status_e  fcc_status = FCC_STATUS_SUCCESS;
00053     fcc_status_e  output_info_fcc_status = FCC_STATUS_SUCCESS;
00054     kcm_status_e  kcm_result = KCM_STATUS_SUCCESS;
00055     uint32_t config_param_index = 0;
00056     cn_cbor *config_param_cb;
00057     fcc_bundle_data_param_s config_param;
00058     size_t currentTimeLength = strlen(g_fcc_current_time_parameter_name);
00059 
00060     SA_PV_LOG_TRACE_FUNC_ENTER_NO_ARGS();
00061     SA_PV_ERR_RECOVERABLE_RETURN_IF((config_params_list_cb == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Invalid config_params_list_cb pointer");
00062 
00063     //Initialize data struct
00064     memset(&config_param, 0, sizeof(config_param));
00065 
00066     for (config_param_index = 0; config_param_index < (uint32_t)config_params_list_cb->length; config_param_index++) {
00067 
00068         FCC_SET_START_TIMER(fcc_config_param_timer);
00069 
00070         //fcc_bundle_clean_and_free_data_param(&config_param);
00071 
00072         //Get key CBOR struct at index key_index
00073         config_param_cb = cn_cbor_index(config_params_list_cb, config_param_index);
00074         SA_PV_ERR_RECOVERABLE_RETURN_IF((config_param_cb == NULL), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Failed to get certificate at index (%" PRIu32 ") ", config_param_index);
00075         SA_PV_ERR_RECOVERABLE_RETURN_IF((config_param_cb->type != CN_CBOR_MAP), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Wrong type of config param CBOR struct at index (%" PRIu32 ")", config_param_index);
00076 
00077         success = fcc_bundle_get_data_param(config_param_cb, &config_param);
00078         SA_PV_ERR_RECOVERABLE_RETURN_IF((success != true), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Failed to get config param data at index (%" PRIu32 ") ", config_param_index);
00079 
00080         // Sets the time
00081         if (is_memory_equal(config_param.name, config_param.name_len, g_fcc_current_time_parameter_name, currentTimeLength)) {
00082             fcc_status = set_time_from_config_param(&config_param);
00083             SA_PV_ERR_RECOVERABLE_GOTO_IF((fcc_status != FCC_STATUS_SUCCESS), fcc_status = fcc_status, exit, "set_time_from_config_param failed");
00084         } else {
00085             kcm_result = kcm_item_store(config_param.name, config_param.name_len, KCM_CONFIG_ITEM, true, config_param.data, config_param.data_size, config_param.acl);
00086             SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_result != KCM_STATUS_SUCCESS), fcc_status = fcc_convert_kcm_to_fcc_status(kcm_result), exit, "Failed to store configuration parameter at index (%" PRIu32 ") ", (uint32_t)config_param_index);
00087         }
00088         FCC_END_TIMER((char*)config_param.name, config_param.name_len, fcc_config_param_timer);
00089     }
00090 
00091 exit:
00092     if (kcm_result != KCM_STATUS_SUCCESS) {
00093 
00094         output_info_fcc_status =  fcc_bundle_store_error_info(config_param.name, config_param.name_len, kcm_result);
00095         SA_PV_ERR_RECOVERABLE_RETURN_IF((output_info_fcc_status != FCC_STATUS_SUCCESS),
00096                                         fcc_status = FCC_STATUS_OUTPUT_INFO_ERROR,
00097                                         "Failed to create output kcm_status error %d", kcm_result);
00098     }
00099     fcc_bundle_clean_and_free_data_param(&config_param);
00100     SA_PV_LOG_TRACE_FUNC_EXIT_NO_ARGS();
00101 
00102     return fcc_status;
00103 }
00104 #endif