1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
21 #include "utils/log_utils.h"
22 #include "utils/rand_utils.h"
23 #include "utils/nts_utils.h"
24 #include "utils/sys_utils.h"
29 static fault_settings_t *faults = 0;
30 static int fault_iterator = -1;
32 int faults_init(void) {
35 rc = faults_ves_init();
36 if(rc != NTS_ERR_OK) {
37 log_error("faults_ves_init failed\n");
38 return NTS_ERR_FAILED;
41 faults_counters_clear();
43 char *config_contents = file_read_content("config/config.json");
44 rc = faults_change_settings(config_contents);
45 free(config_contents);
50 void faults_free(void) {
52 faults_settings_free(faults);
55 int faults_change_settings(const char *json) {
58 fault_settings_t *local_faults = faults_settings_read(json);
60 faults_settings_free(faults);
62 faults = local_faults;
67 bool faults_get_present(void) {
71 fault_details_t *faults_generate_fault(void) {
76 switch(faults->choosing_method[0]) {
79 if(fault_iterator >= faults->fault_count) {
86 fault_iterator = rand_uint32() % faults->fault_count;
90 log_error("invalid fault choosing method\n");
95 int rc = faults_settings_process(faults, fault_iterator);
96 if(rc != NTS_ERR_OK) {
97 log_error("faults_settings_process failed\n");
98 faults_settings_free(faults);
102 return &faults->fault[fault_iterator];