Add supoprt for D release use-case.
[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     log_add_verbose(2, "[faults] fault list cleared\n");
38 }
39
40 int faults_fault_list_add(uint16_t delay) {
41     faults_fault_list_len++;
42     faults_fault_list = (uint16_t *)realloc(faults_fault_list, sizeof(uint16_t) * (faults_fault_list_len));
43     if(faults_fault_list == 0) {
44         log_error("realloc failed\n");
45         return NTS_ERR_FAILED;
46     }
47     faults_fault_list[faults_fault_list_len - 1] = delay;
48     log_add_verbose(2, "[faults] added %d\n", delay);
49
50     return NTS_ERR_OK;
51 }
52
53 bool faults_fault_list_not_empty(void) {
54     bool not_empty = (faults_fault_list_len != 0);
55     if(not_empty == true) {
56         int delay_sum = 0;
57         for(int i = 0; i < faults_fault_list_len; i++) {
58             delay_sum += faults_fault_list[i];
59         }
60
61         not_empty = (delay_sum != 0);
62     }
63     return not_empty;
64 }
65
66 uint16_t faults_fault_list_get_next(void) {
67     assert(faults_fault_list_iterator < faults_fault_list_len);
68
69     uint16_t ret = faults_fault_list[faults_fault_list_iterator];
70     faults_fault_list_iterator++;
71     if(faults_fault_list_iterator >= faults_fault_list_len) {
72         faults_fault_list_iterator = 0;
73     }
74
75     return ret;
76 }