879fb5ab276d114f2a16f75a256a5f4d8e3da0ce
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / faults / faults_logic.c
1 /*************************************************************************
2 *
3 * Copyright 2020 highstreet technologies GmbH and others
4 *
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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 ***************************************************************************/
17
18 #define _GNU_SOURCE
19
20 #include "faults.h"
21 #include "utils/log_utils.h"
22 #include "utils/rand_utils.h"
23 #include "utils/sys_utils.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27
28 static uint16_t *faults_fault_list = 0;
29 static uint16_t faults_fault_list_len = 0;
30 static uint16_t faults_fault_list_iterator = 0;
31
32 void faults_fault_list_clear(void) {
33     faults_fault_list_len = 0;
34     free(faults_fault_list);
35     faults_fault_list = 0;
36     faults_fault_list_iterator = 0;
37 }
38
39 int faults_fault_list_add(uint16_t delay) {
40     faults_fault_list_len++;
41     faults_fault_list = (uint16_t *)realloc(faults_fault_list, sizeof(uint16_t) * (faults_fault_list_len));
42     if(faults_fault_list == 0) {
43         log_error("realloc failed");
44         return NTS_ERR_FAILED;
45     }
46     faults_fault_list[faults_fault_list_len - 1] = delay;
47
48     return NTS_ERR_OK;
49 }
50
51 bool faults_fault_list_not_empty(void) {
52     bool not_empty = (faults_fault_list_len != 0);
53     if(not_empty == true) {
54         int delay_sum = 0;
55         for(int i = 0; i < faults_fault_list_len; i++) {
56             delay_sum += faults_fault_list[i];
57         }
58
59         not_empty = (delay_sum != 0);
60     }
61     return not_empty;
62 }
63
64 uint16_t faults_fault_list_get_next(void) {
65     assert(faults_fault_list_iterator < faults_fault_list_len);
66
67     uint16_t ret = faults_fault_list[faults_fault_list_iterator];
68     faults_fault_list_iterator++;
69     if(faults_fault_list_iterator >= faults_fault_list_len) {
70         faults_fault_list_iterator = 0;
71     }
72
73     return ret;
74 }