| 1 | package RPi::SysInfo; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | our $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 11 | require XSLoader; | |||||||||||||||||||||||||||
| 11 | 11 | XSLoader::load('RPi::SysInfo', $VERSION); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 11 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 16 | core_temp | ||||||||||||||||||||||||||||
| 17 | cpu_percent | ||||||||||||||||||||||||||||
| 18 | mem_percent | ||||||||||||||||||||||||||||
| 19 | gpio_info | ||||||||||||||||||||||||||||
| 20 | raspi_config | ||||||||||||||||||||||||||||
| 21 | network_info | ||||||||||||||||||||||||||||
| 22 | file_system | ||||||||||||||||||||||||||||
| 23 | pi_details | ||||||||||||||||||||||||||||
| 24 | pi_model | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | our %EXPORT_TAGS; | |||||||||||||||||||||||||||
| 28 | 11 | $EXPORT_TAGS{all} = [@EXPORT_OK]; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | return bless {}, shift; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 19 | ▶ | sub core_temp { | ||||||||||||||||||||||||||
|
Subroutine
core_temp: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 19 | ▶ | shift if $_[0] && $_[0] =~ /RPi::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and $_[0] =~ /RPi::/
MC/DC0%0/2
$_[0] and $_[0] =~ /RPi::/
$_[0]
$_[0] =~ /RPi::/
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /RPi::/
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 19 | my ($degree) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | ▶ | $degree //= 'c'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$degree //= "c"
MC/DC100%1/1
$degree //= "c"
$degree
Truth table100%2/2
A
A= $degree
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 19 | my $temp = _core_temp_c(); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 19 | ▶ | return '' if ! defined $temp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 18 | ▶ | if ($degree eq 'f' || $degree eq 'F'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$degree eq "f" or $degree eq "F"
MC/DC100%2/2
$degree eq "f" or $degree eq "F"
$degree eq "f"
$degree eq "F"
Truth table100%3/3
A or B
A= $degree eq "f"B= $degree eq "F"
| |||||||||||||||||||||||||||||
| 45 | 9 | $temp = ($temp * 1.8) + 32; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 18 | chomp $temp; | |||||||||||||||||||||||||||
| 49 | 18 | return $temp; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub cpu_percent { | ||||||||||||||||||||||||||
|
Subroutine
cpu_percent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | return _format(cpuPercent()); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 11 | ▶ | sub gpio_info { | ||||||||||||||||||||||||||
|
Subroutine
gpio_info: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 11 | ▶ | shift if $_[0] && $_[0] =~ /RPi::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and $_[0] =~ /RPi::/
MC/DC0%0/2
$_[0] and $_[0] =~ /RPi::/
$_[0]
$_[0] =~ /RPi::/
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /RPi::/
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | my ($pins) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 11 | ▶ | $pins = ! defined $pins | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | ? '' | ||||||||||||||||||||||||||||
| 61 | : join ",", @$pins; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # raspi-gpio was removed from current Raspberry Pi OS in favour of pinctrl, | ||||||||||||||||||||||||||||
| 64 | # and never existed for the Pi 5 / RP1. Prefer pinctrl, falling back to | ||||||||||||||||||||||||||||
| 65 | # raspi-gpio on older systems that still ship it. Both accept the same | ||||||||||||||||||||||||||||
| 66 | # "get [pin[,pin...]]" invocation. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 11 | my $tool = _gpio_tool(); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | ▶ | return '' if ! defined $tool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 10 | my $info = _run("$tool get $pins"); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 10 | chomp $info; | |||||||||||||||||||||||||||
| 75 | 10 | return $info; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | sub file_system { | ||||||||||||||||||||||||||
|
Subroutine
file_system: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 3 | my $fs_info = _run('df') . "\n"; | |||||||||||||||||||||||||||
| 79 | 3 | ▶ | $fs_info .= _slurp('/proc/swaps') // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
_slurp("/proc/swaps") // ""
MC/DC0%0/1
_slurp("/proc/swaps") // ""
_slurp("/proc/swaps")
Truth table50%1/2
A
A= _slurp("/proc/swaps")
| |||||||||||||||||||||||||||||
| 80 | 3 | return $fs_info; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub mem_percent { | ||||||||||||||||||||||||||
|
Subroutine
mem_percent: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | return _format(memPercent()); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 4 | ▶ | sub network_info { | ||||||||||||||||||||||||||
|
Subroutine
network_info: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # ifconfig (net-tools) is the legacy default, but modern Raspberry Pi OS | ||||||||||||||||||||||||||||
| 88 | # Lite ships without it, so fall back to `ip addr`, which is always present. | ||||||||||||||||||||||||||||
| 89 | # Both forms carry inet/inet6 lines. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | my $tool = _net_tool(); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | return '' if ! defined $tool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | my $netinfo = _run($tool); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | chomp $netinfo; | |||||||||||||||||||||||||||
| 98 | 3 | return $netinfo; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 3 | ▶ | sub pi_details { | ||||||||||||||||||||||||||
|
Subroutine
pi_details: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | my $details; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 3 | $details = "\n" | |||||||||||||||||||||||||||
| 105 | . _run('cat /sys/firmware/devicetree/base/model') | ||||||||||||||||||||||||||||
| 106 | . "\n\n" | ||||||||||||||||||||||||||||
| 107 | . _run('cat /etc/os-release | head -4') | ||||||||||||||||||||||||||||
| 108 | . "\n" | ||||||||||||||||||||||||||||
| 109 | . _run('uname -a') | ||||||||||||||||||||||||||||
| 110 | . "\n" | ||||||||||||||||||||||||||||
| 111 | . _run('cat /proc/cpuinfo | tail -3') | ||||||||||||||||||||||||||||
| 112 | . "Board : " . pi_model() . "\n" | ||||||||||||||||||||||||||||
| 113 | . "SoC / RAM : " . _board_summary() . "\n" | ||||||||||||||||||||||||||||
| 114 | . "Throttled flag : " . _run('vcgencmd get_throttled') | ||||||||||||||||||||||||||||
| 115 | . "Camera : " . _camera_info() . "\n"; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | return $details; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 9 | ▶ | sub pi_model { | ||||||||||||||||||||||||||
|
Subroutine
pi_model: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 9 | ▶ | shift if $_[0] && $_[0] =~ /RPi::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and $_[0] =~ /RPi::/
MC/DC0%0/2
$_[0] and $_[0] =~ /RPi::/
$_[0]
$_[0] =~ /RPi::/
Truth table33%1/3
A and B
A= $_[0]B= $_[0] =~ /RPi::/
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Normalized Raspberry Pi marketing name, e.g. "Raspberry Pi 5 Model B Rev | ||||||||||||||||||||||||||||
| 123 | # 1.1". The devicetree model is authoritative on the Pi 0-5, so prefer it, | ||||||||||||||||||||||||||||
| 124 | # falling back to a /proc/cpuinfo Revision-code decode, then to 'Unknown'. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 9 | my $model = _slurp('/sys/firmware/devicetree/base/model'); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if (defined $model){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 7 | $model =~ s/\0//g; # Devicetree strings are NUL-terminated. | |||||||||||||||||||||||||||
| 130 | 7 | $model =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 131 | 7 | ▶ | return $model if length $model; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | my $info = _decode_revision(_cpuinfo_field('Revision')); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | return $info->{name} if defined $info->{name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | return 'Unknown'; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 3 | ▶ | sub raspi_config { | ||||||||||||||||||||||||||
|
Subroutine
raspi_config: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 3 | my $config = _run('vcgencmd get_config int'); | |||||||||||||||||||||||||||
| 142 | 3 | $config .= _run('vcgencmd get_config str'); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # config.txt moved from /boot to /boot/firmware on Bookworm and later (the | ||||||||||||||||||||||||||||
| 145 | # old path now holds only a "this file has moved" stub), so resolve the | ||||||||||||||||||||||||||||
| 146 | # real location before appending the user's non-comment directives. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | my $config_file = _config_file(); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 3 | ▶ | if (defined $config_file){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 3 | $config .= _run("grep -E -v '^\\s*(#|^\$)' $config_file"); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | chomp $config; | |||||||||||||||||||||||||||
| 155 | 3 | return $config; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | sub _board_summary { | ||||||||||||||||||||||||||
|
Subroutine
_board_summary: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||
| 159 | # Human-readable decode of the SoC, RAM and RP1 presence pulled from the | ||||||||||||||||||||||||||||
| 160 | # /proc/cpuinfo Revision code. Used to enrich pi_details(). Returns | ||||||||||||||||||||||||||||
| 161 | # 'unknown' when the revision can't be decoded. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | my $info = _decode_revision(_cpuinfo_field('Revision')); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 6 | my @parts; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 6 | ▶ | push @parts, $info->{soc} if defined $info->{soc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 6 | ▶ | push @parts, $info->{mem} if defined $info->{mem}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 6 | ▶ | push @parts, 'RP1' if $info->{rp1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 6 | ▶ | push @parts, $info->{manufacturer} if defined $info->{manufacturer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 6 | ▶ | return @parts ? join(', ', @parts) : 'unknown'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 7 | ▶ | sub _camera_info { | ||||||||||||||||||||||||||
|
Subroutine
_camera_info: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 175 | # Legacy firmware (Pi 0-4 on Bullseye and earlier) answered | ||||||||||||||||||||||||||||
| 176 | # `vcgencmd get_camera` with "supported=N detected=N". On Bookworm and the | ||||||||||||||||||||||||||||
| 177 | # Pi 5 that command was removed and camera support moved to libcamera, so | ||||||||||||||||||||||||||||
| 178 | # fall back to a libcamera probe. Returns a one-line string (no newline). | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | local $SIG{__WARN__} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 181 | 0 | my $warning = shift; | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | warn $warning if $warning !~ /Can't exec "vcgencmd"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 7 | }; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 7 | my $legacy = _run('vcgencmd get_camera'); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 7 | ▶ | if ($legacy =~ /supported=/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 2 | $legacy =~ s/\s+$//; | |||||||||||||||||||||||||||
| 189 | 2 | return $legacy; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 5 | my $tool = _camera_tool(); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 5 | ▶ | if (defined $tool){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 4 | my $list = _run("$tool --list-cameras 2>/dev/null"); | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 4 | ▶ | return 'detected (libcamera)' if $list =~ /Available cameras/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 2 | return 'none detected (libcamera)'; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | return 'not detected'; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 3 | ▶ | sub _camera_tool { | ||||||||||||||||||||||||||
|
Subroutine
_camera_tool: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 204 | # libcamera's listing utility: rpicam-hello on Bookworm and later, renamed | ||||||||||||||||||||||||||||
| 205 | # from the Bullseye-era libcamera-hello. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 3 | return _first_tool(qw(rpicam-hello libcamera-hello)); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub _config_file { | ||||||||||||||||||||||||||
|
Subroutine
_config_file: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 210 | # Locate the active config.txt. Bookworm and later moved it to | ||||||||||||||||||||||||||||
| 211 | # /boot/firmware/config.txt; older systems keep it at /boot/config.txt. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | for my $file ('/boot/firmware/config.txt', '/boot/config.txt'){ | |||||||||||||||||||||||||||
| 214 | 0 | ▶ | return $file if -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | return undef; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 22 | ▶ | sub _core_temp_c { | ||||||||||||||||||||||||||
|
Subroutine
_core_temp_c: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 220 | # Core temperature in Celsius as a number, or undef if unavailable. Prefers | ||||||||||||||||||||||||||||
| 221 | # vcgencmd (the value the Pi tooling reports), falling back to the kernel | ||||||||||||||||||||||||||||
| 222 | # thermal zone on systems without vcgencmd. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | local $SIG{__WARN__} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 225 | 0 | my $warning = shift; | |||||||||||||||||||||||||||
| 226 | 0 | ▶ | warn $warning if $warning !~ /Can't exec "vcgencmd"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 22 | }; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 22 | my $temp = _run('vcgencmd measure_temp'); | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 22 | ▶ | return $1 if $temp =~ /temp=([\d.]+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # /sys/class/thermal/thermal_zone0/temp reports millidegrees Celsius. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 4 | my $milli = _slurp('/sys/class/thermal/thermal_zone0/temp'); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 4 | ▶ | return $1 / 1000 if defined $milli && $milli =~ /^(\d+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $milli and $milli =~ /^(\d+)/
MC/DC50%1/2
defined $milli and $milli =~ /^(\d+)/
defined $milli
$milli =~ /^(\d+)/
Truth table66%2/3
A and B
A= defined $milliB= $milli =~ /^(\d+)/
| |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 2 | return undef; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 15 | ▶ | sub _cpuinfo_field { | ||||||||||||||||||||||||||
|
Subroutine
_cpuinfo_field: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 242 | 15 | my ($field) = @_; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 15 | ▶ | croak "_cpuinfo_field() requires a field name\n" if ! defined $field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 14 | my $cpuinfo = _slurp('/proc/cpuinfo'); | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 14 | ▶ | return undef if ! defined $cpuinfo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 12 | for my $line (split /\n/, $cpuinfo){ | |||||||||||||||||||||||||||
| 251 | 141 | ▶ | return $1 if $line =~ /^\Q$field\E\s*:\s*(.+?)\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 1 | return undef; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 24 | ▶ | sub _decode_revision { | ||||||||||||||||||||||||||
|
Subroutine
_decode_revision: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 257 | 24 | my ($rev) = @_; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 24 | ▶ | return {} if ! defined $rev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 22 | $rev =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 22 | ▶ | return {} if $rev !~ /^[0-9a-fA-F]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 19 | $rev = hex($rev); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 19 | my %info; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 19 | ▶ | if ($rev & 0x800000){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | # New-style revision code (Pi 2 and later, plus late Pi 1 boards). | ||||||||||||||||||||||||||||
| 271 | 17 | my $type = ($rev >> 4) & 0xff; | |||||||||||||||||||||||||||
| 272 | 17 | my $proc = ($rev >> 12) & 0x0f; | |||||||||||||||||||||||||||
| 273 | 17 | my $mfr = ($rev >> 16) & 0x0f; | |||||||||||||||||||||||||||
| 274 | 17 | my $mem = ($rev >> 20) & 0x07; | |||||||||||||||||||||||||||
| 275 | 17 | my $minor = $rev & 0x0f; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 17 | my %types = ( | |||||||||||||||||||||||||||
| 278 | 0x00 => 'A', 0x01 => 'B', 0x02 => 'A+', | ||||||||||||||||||||||||||||
| 279 | 0x03 => 'B+', 0x04 => '2 Model B', 0x06 => 'Compute Module 1', | ||||||||||||||||||||||||||||
| 280 | 0x08 => '3 Model B', 0x09 => 'Zero', 0x0a => 'Compute Module 3', | ||||||||||||||||||||||||||||
| 281 | 0x0c => 'Zero W', 0x0d => '3 Model B+', 0x0e => '3 Model A+', | ||||||||||||||||||||||||||||
| 282 | 0x10 => 'Compute Module 3+', 0x11 => '4 Model B', | ||||||||||||||||||||||||||||
| 283 | 0x12 => 'Zero 2 W', 0x13 => '400', 0x14 => 'Compute Module 4', | ||||||||||||||||||||||||||||
| 284 | 0x15 => 'Compute Module 4S', 0x17 => '5 Model B', | ||||||||||||||||||||||||||||
| 285 | 0x18 => 'Compute Module 5', 0x19 => '500', | ||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||
| 287 | 17 | my %procs = ( | |||||||||||||||||||||||||||
| 288 | 0 => 'BCM2835', 1 => 'BCM2836', 2 => 'BCM2837', | ||||||||||||||||||||||||||||
| 289 | 3 => 'BCM2711', 4 => 'BCM2712', | ||||||||||||||||||||||||||||
| 290 | ); | ||||||||||||||||||||||||||||
| 291 | 17 | my %mfrs = ( | |||||||||||||||||||||||||||
| 292 | 0 => 'Sony UK', 1 => 'Egoman', 2 => 'Embest', | ||||||||||||||||||||||||||||
| 293 | 3 => 'Sony Japan', 4 => 'Embest', 5 => 'Stadium', | ||||||||||||||||||||||||||||
| 294 | ); | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 17 | $info{new_style} = 1; | |||||||||||||||||||||||||||
| 297 | 17 | $info{type} = $types{$type}; | |||||||||||||||||||||||||||
| 298 | 17 | $info{soc} = $procs{$proc}; | |||||||||||||||||||||||||||
| 299 | 17 | $info{manufacturer} = $mfrs{$mfr}; | |||||||||||||||||||||||||||
| 300 | 17 | $info{revision} = $minor; | |||||||||||||||||||||||||||
| 301 | 17 | $info{mem} = _mem_human(256 << $mem); | |||||||||||||||||||||||||||
| 302 | 17 | ▶ | $info{rp1} = $proc == 4 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 17 | ▶ | $info{name} = defined $types{$type} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 304 | ? "Raspberry Pi $types{$type}" | ||||||||||||||||||||||||||||
| 305 | : undef; | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | else { | ||||||||||||||||||||||||||||
| 308 | # Old-style revision code (original Pi 1 / early boards). A small | ||||||||||||||||||||||||||||
| 309 | # lookup of the common ones; everything else is left to the devicetree | ||||||||||||||||||||||||||||
| 310 | # model. All old-style boards are BCM2835, pre-RP1. | ||||||||||||||||||||||||||||
| 311 | 2 | my %old = ( | |||||||||||||||||||||||||||
| 312 | 0x0002 => ['B', '256MB'], 0x0003 => ['B', '256MB'], | ||||||||||||||||||||||||||||
| 313 | 0x0004 => ['B', '256MB'], 0x0005 => ['B', '256MB'], | ||||||||||||||||||||||||||||
| 314 | 0x0006 => ['B', '256MB'], 0x0007 => ['A', '256MB'], | ||||||||||||||||||||||||||||
| 315 | 0x0008 => ['A', '256MB'], 0x0009 => ['A', '256MB'], | ||||||||||||||||||||||||||||
| 316 | 0x000d => ['B', '512MB'], 0x000e => ['B', '512MB'], | ||||||||||||||||||||||||||||
| 317 | 0x000f => ['B', '512MB'], 0x0010 => ['B+', '512MB'], | ||||||||||||||||||||||||||||
| 318 | 0x0011 => ['Compute Module 1', '512MB'], | ||||||||||||||||||||||||||||
| 319 | 0x0012 => ['A+', '256MB'], 0x0013 => ['B+', '512MB'], | ||||||||||||||||||||||||||||
| 320 | 0x0014 => ['Compute Module 1', '512MB'], | ||||||||||||||||||||||||||||
| 321 | 0x0015 => ['A+', '256MB'], | ||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 2 | $info{new_style} = 0; | |||||||||||||||||||||||||||
| 325 | 2 | $info{soc} = 'BCM2835'; | |||||||||||||||||||||||||||
| 326 | 2 | $info{rp1} = 0; | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 2 | ▶ | if (my $entry = $old{$rev}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | 1 | $info{type} = $entry->[0]; | |||||||||||||||||||||||||||
| 330 | 1 | $info{mem} = $entry->[1]; | |||||||||||||||||||||||||||
| 331 | 1 | $info{name} = "Raspberry Pi $entry->[0]"; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 19 | return \%info; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | 4 | ▶ | sub _first_tool { | ||||||||||||||||||||||||||
|
Subroutine
_first_tool: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 338 | # Returns the first of the given executable names found on PATH, else undef. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 4 | for my $tool (@_){ | |||||||||||||||||||||||||||
| 341 | 5 | ▶ | for my $dir (split /:/, $ENV{PATH} // ''){ | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"PATH"} // ""
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
| |||||||||||||||||||||||||||||
| 342 | 4 | ▶ | return $tool if -x "$dir/$tool"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 2 | return undef; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | 5 | ▶ | sub _format { | ||||||||||||||||||||||||||
|
Subroutine
_format: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 349 | 5 | ▶ | croak "_format() requires a float/double sent in\n" if ! defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 4 | return sprintf("%.2f", $_[0]); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | 3 | ▶ | sub _gpio_tool { | ||||||||||||||||||||||||||
|
Subroutine
_gpio_tool: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 353 | # Locate a GPIO query tool on PATH. pinctrl is the current Raspberry Pi OS | ||||||||||||||||||||||||||||
| 354 | # utility; raspi-gpio is the legacy one kept here as a fallback. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 3 | return _first_tool(qw(pinctrl raspi-gpio)); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | 25 | ▶ | sub _mem_human { | ||||||||||||||||||||||||||
|
Subroutine
_mem_human: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 359 | 25 | my ($mb) = @_; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 25 | ▶ | return undef if ! defined $mb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 24 | ▶ | return $mb % 1024 == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | ? ($mb / 1024) . 'GB' | ||||||||||||||||||||||||||||
| 365 | : $mb . 'MB'; | ||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | 3 | ▶ | sub _net_tool { | ||||||||||||||||||||||||||
|
Subroutine
_net_tool: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 368 | # Locate a network-interface query command. ifconfig (net-tools) is the | ||||||||||||||||||||||||||||
| 369 | # legacy default; fall back to `ip addr` on systems without it. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 3 | ▶ | return 'ifconfig' if defined _first_tool('ifconfig'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 2 | ▶ | return 'ip addr' if defined _first_tool('ip'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 1 | return undef; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | 4 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 377 | 4 | my ($cmd) = @_; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 4 | ▶ | croak "_run() requires a command string\n" if ! defined $cmd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 3 | my $out = `$cmd`; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 3 | ▶ | return defined $out ? $out : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | 3 | ▶ | sub _slurp { | ||||||||||||||||||||||||||
|
Subroutine
_slurp: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 386 | 3 | my ($file) = @_; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 3 | ▶ | croak "_slurp() requires a file path\n" if ! defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 2 | ▶ | open my $fh, '<', $file or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 1 | local $/; | |||||||||||||||||||||||||||
| 392 | 1 | my $data = <$fh>; | |||||||||||||||||||||||||||
| 393 | 1 | close $fh; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 1 | return $data; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 11 | 1; | |||||||||||||||||||||||||||
| 399 | __END__ | ||||||||||||||||||||||||||||