Settings.pm 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## @file [Settings.pm]

## @class [FIJI::Settings]
#
# Contains helper functions to deal with FIJI configuration files.
package FIJI::Settings;

use Log::Log4perl qw(get_logger);
use Scalar::Util "looks_like_number";
use Config::Simple;

## @function read_configfile ($fiji_cfg_file)
# @brief Load the configuration file containing design constants.
#
# \param fiji_cfg_file The name of an .ini file with a 'consts' block
#        containing the constants specified by \ref _sanitize_consts.
#
# \returns a reference to the hash containing the read constants.
sub read_configfile {
  my $logger = get_logger();
  my ($fiji_cfg_file) = @_;
22
23
  my $fiji_cfg;
  eval { $fiji_cfg = new Config::Simple($fiji_cfg_file) }; # pesky library tries to die on syntax errors
24
  if (!defined($fiji_cfg)) {
25
    $logger->fatal("Could not read config file \"$fiji_cfg_file\": " . (defined($@) ? $@ : Config::Simple->error()));
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    return undef;
  }

  my $fiji_consts = $fiji_cfg->get_block("consts");
  if (!(%$fiji_consts)) {
    $logger->fatal("Could not fetch consts block from config file \"$fiji_cfg_file\"");
    return undef;
  }

  if (_sanitize_consts($fiji_consts) != 0) {
    $logger->error("Constants in fiji configuration invalid");
    return undef;
  }

  return $fiji_consts;
}


## @function _sanitize_consts (%$consts_ref)
# @brief Do a sanity check on the fiji configuration hash.
#
# \param consts_ref a reference to a hash containing following constants
#   - FIU_NUM: > 0
#   - FIU_CFG_BITS: > 0
#   - ARM_DURATION_WIDTH, INJECTION_DURATION_WIDTH: > 0, multiple of 8
#   - ID: > 0, < 2^15-1
#   - BAUDRATE: > 0
#
# \returns 0 if all required constants exist and seem sane.
sub _sanitize_consts {
  my $logger = get_logger();
  my ($consts_ref) = @_;
  if (ref($consts_ref) ne 'HASH') {
    $logger->error("Parameter is not a reference to a hash (containing fiji constants).");
    return 1;
  }

  foreach my $k ('FIU_NUM', 'FIU_CFG_BITS', 'ARM_DURATION_WIDTH', 'INJECTION_DURATION_WIDTH', 'ID', 'BAUDRATE') {
    if (!exists($consts_ref->{$k})) {
      $logger->error("$k is missing from fiji constants.");
      return 1;
    }

    # convert non-decimal (hexadecimal, binary, octal) values to decimal
    my $orig = $consts_ref->{$k};
    $consts_ref->{$k} = oct($orig) if $orig =~ /^0/;
    $logger->trace("Converted value of $k (\"$orig\") to \"$consts_ref->{$k}\".") if ($orig ne $consts_ref->{$k});
    if (!looks_like_number($consts_ref->{$k})) {
      $logger->error("$orig does not look like a number.");
      return 1;
    }
  }

  if (($consts_ref->{'FIU_NUM'} <= 0)) {
      $logger->error("FIU_NUM is <= 0.");
      return 1;
  }
  if (($consts_ref->{'FIU_CFG_BITS'} <= 0)) {
      $logger->error("FIU_CFG_BITS is <= 0.");
      return 1;
  }
  if (($consts_ref->{'ARM_DURATION_WIDTH'} <= 0) || ($consts_ref->{'ARM_DURATION_WIDTH'} % 8 != 0)) {
      $logger->error("ARM_DURATION_WIDTH is invalid ('$consts_ref->{'ARM_DURATION_WIDTH'}').");
      return 1;
  }
  if (($consts_ref->{'INJECTION_DURATION_WIDTH'} <= 0) || (($consts_ref->{'INJECTION_DURATION_WIDTH'} % 8) != 0)) {
      $logger->error("INJECTION_DURATION_WIDTH is invalid ('$consts_ref->{'INJECTION_DURATION_WIDTH'}').");
      return 1;
  }
  if (($consts_ref->{'ID'} <= 0) || ($consts_ref->{'ID'} > (2**15 - 1))) {
      $logger->error("ID is invalid ('$consts_ref->{'ID'}').");
      return 1;
  }
  if (($consts_ref->{'BAUDRATE'} <= 0)) {
      $logger->error("BAUDRATE missing is <= 0.");
      return 1;
  }
  return 0;
}

1;