| 1 | package Parser::FIT; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Carp qw/croak confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use feature 'state'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Parser::FIT::Profile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #require "Profile.pm"; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 12 | our $VERSION = 0.10; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 18 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 18 | my %options = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 18 | my $ref = { | |||||||||||||||||||||||||||
| 19 | _DEBUG => 0, | ||||||||||||||||||||||||||||
| 20 | header => {}, | ||||||||||||||||||||||||||||
| 21 | body => {}, | ||||||||||||||||||||||||||||
| 22 | globalMessages => [], | ||||||||||||||||||||||||||||
| 23 | localMessages => [], | ||||||||||||||||||||||||||||
| 24 | records => 0, | ||||||||||||||||||||||||||||
| 25 | fh => undef, | ||||||||||||||||||||||||||||
| 26 | buffer => "", | ||||||||||||||||||||||||||||
| 27 | headerLength => 0, | ||||||||||||||||||||||||||||
| 28 | totalBytesRead => 0, | ||||||||||||||||||||||||||||
| 29 | messageHandlers => {}, | ||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 18 | ▶ | if(exists $options{on}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 8 | $ref->{messageHandlers} = $options{on}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 18 | ▶ | if(exists $options{debug} && $options{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $options{"debug"} and $options{"debug"}
MC/DC0%0/2
exists $options{"debug"} and $options{"debug"}
exists $options{"debug"}
$options{"debug"}
Truth table33%1/3
A and B
A= exists $options{"debug"}B= $options{"debug"}
| |||||||||||||||||||||||||||||
| 37 | 0 | $ref->{_DEBUG} = 1; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 18 | bless($ref, $class); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | return $ref; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 12 | my $file = shift; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | croak "No file given to parse()!" unless($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 12 | $self->_debug("Parsing '$file'"); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 12 | ▶ | croak "File '$file' doesn't exist!" if(!-f $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 12 | $self->_debug("Opening file"); | |||||||||||||||||||||||||||
| 56 | 12 | ▶ | open(my $input, "<", $file) or croak "Error opening '$file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 12 | binmode($input); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 12 | $self->parse_fh($input); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 14 | ▶ | sub parse_fh { | ||||||||||||||||||||||||||
|
Subroutine
parse_fh: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 14 | my $input = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 14 | ▶ | unless(ref $input eq "GLOB") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | die "parse_fh requires an opened filehandle as param!"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 14 | $self->{fh} = $input; | |||||||||||||||||||||||||||
| 72 | 14 | my $header = $self->_read_header(); | |||||||||||||||||||||||||||
| 73 | 14 | $self->{header} = $self->_parse_header($header); | |||||||||||||||||||||||||||
| 74 | #my $dataBody = $self->_readBytes($self->{header}->{dataLength}); | ||||||||||||||||||||||||||||
| 75 | 14 | $self->_parse_data_records(); | |||||||||||||||||||||||||||
| 76 | #$self->_parse_crc(); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 14 | close($input); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | sub parse_data { | ||||||||||||||||||||||||||
|
Subroutine
parse_data: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 83 | 2 | my $data = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | open(my $fh, "<", \$data) or die "Error opening scalar as file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 2 | binmode($fh); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | return $self->parse_fh($fh); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 14 | ▶ | sub _read_header { | ||||||||||||||||||||||||||
|
Subroutine
_read_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 14 | my $headerLengthByte = $self->_readBytes(1); | |||||||||||||||||||||||||||
| 95 | 14 | my $headerLength = unpack("c", $headerLengthByte); | |||||||||||||||||||||||||||
| 96 | 14 | $self->{headerLength} = $headerLength; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # The 1-Byte headerLength field is included in the total header length | ||||||||||||||||||||||||||||
| 99 | 14 | my $headerWithoutLengthByte = $headerLength - 1; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 14 | my $header = $self->_readBytes($headerWithoutLengthByte); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 14 | return $header; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 23 | ▶ | sub _parse_header { | ||||||||||||||||||||||||||
|
Subroutine
_parse_header: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 107 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 23 | my $header = shift; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 23 | my ($protocolVersion, $profile, $dataLength, $fileMagic, $crc); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 23 | my $headerLength = length $header; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 23 | ▶ | if($headerLength == 13) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | 16 | ($protocolVersion, $profile, $dataLength, $fileMagic, $crc) = unpack("c s I! a4 s", $header); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | elsif($headerLength == 11) { | ||||||||||||||||||||||||||||
| 118 | 1 | ($protocolVersion, $profile, $dataLength, $fileMagic) = unpack("c s I! a4", $header); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Short header has no CRC value | ||||||||||||||||||||||||||||
| 121 | 1 | $crc = undef; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | 6 | croak "Invalid headerLength=${headerLength}! Don't know how to handle this."; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 17 | ▶ | croak "File either corrupted or not a real FIT file! (Missing magic '.FIT' string in header)" unless($fileMagic eq ".FIT"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 16 | $self->_debug("ProtocolVersion: $protocolVersion"); | |||||||||||||||||||||||||||
| 130 | 16 | $self->_debug("Profile: $profile"); | |||||||||||||||||||||||||||
| 131 | 16 | $self->_debug("DataLength: $dataLength Bytes"); | |||||||||||||||||||||||||||
| 132 | 16 | $self->_debug("FileMagic: $fileMagic"); | |||||||||||||||||||||||||||
| 133 | 16 | ▶ | $self->_debug("CRC: " . (defined($crc) ? $crc : "N/A")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $headerInfo = { | ||||||||||||||||||||||||||||
| 136 | protocolVersion => $protocolVersion, | ||||||||||||||||||||||||||||
| 137 | profile => $profile, | ||||||||||||||||||||||||||||
| 138 | dataLength => $dataLength, | ||||||||||||||||||||||||||||
| 139 | crc => $crc, | ||||||||||||||||||||||||||||
| 140 | 16 | eof => $self->{headerLength} + $dataLength, | |||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 16 | return $headerInfo; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 15091 | ▶ | sub _parse_record_header { | ||||||||||||||||||||||||||
|
Subroutine
_parse_record_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 15091 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | 15091 | my $recordHeader = shift; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | return { | ||||||||||||||||||||||||||||
| 151 | # Bit 7 inidcates a normal header (=0) or "something else" | ||||||||||||||||||||||||||||
| 152 | 15091 | isNormalHeader => (($recordHeader & (1<<7)) == 0), | |||||||||||||||||||||||||||
| 153 | # Bit 6 indicates a definition msg | ||||||||||||||||||||||||||||
| 154 | isDefinitionMessage => (($recordHeader & (1<<6)) > 0), | ||||||||||||||||||||||||||||
| 155 | # Bit 5 indicates "developer data flag" | ||||||||||||||||||||||||||||
| 156 | isDeveloperData => (($recordHeader & (1<<5)) > 0), | ||||||||||||||||||||||||||||
| 157 | # Bit 4 is reserved | ||||||||||||||||||||||||||||
| 158 | # Bits 3-0 define the localMessageType | ||||||||||||||||||||||||||||
| 159 | localMessageType => $recordHeader & 0xF, | ||||||||||||||||||||||||||||
| 160 | }; | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 14 | ▶ | sub _parse_data_records { | ||||||||||||||||||||||||||
|
Subroutine
_parse_data_records: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 164 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 14 | $self->_debug("Parsing Data Records"); | |||||||||||||||||||||||||||
| 167 | 14 | ▶ | while($self->{totalBytesRead} < $self->{header}->{eof}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 15076 | my ($recordHeaderByte) = unpack("c", $self->_readBytes(1)); | |||||||||||||||||||||||||||
| 170 | 15076 | $self->_debug("HeaderBytes in Binary: " . sprintf("%08b", $recordHeaderByte)); | |||||||||||||||||||||||||||
| 171 | 15076 | my $header = $self->_parse_record_header($recordHeaderByte); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 15076 | ▶ | if($header->{isNormalHeader}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 15076 | ▶ | if($header->{isDefinitionMessage}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 364 | $self->_debug("Record definition header for LocalMessageType=" . $header->{localMessageType}); | |||||||||||||||||||||||||||
| 176 | 364 | $self->_parse_definition_message($header); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | else { | ||||||||||||||||||||||||||||
| 179 | 14712 | my $parseResult = $self->_parse_local_message_record($header); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 14712 | ▶ | if(!defined $parseResult) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 101 | $self->_debug("Skipping record for unknown LocalMessageType=" . $header->{localMessageType}); | |||||||||||||||||||||||||||
| 183 | 101 | next; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 14611 | $self->_debug("Processed record for LocalMessageType=" . $header->{localMessageType}); | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 14611 | $self->emitRecord($parseResult->{messageType}, $parseResult->{fields}); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 14611 | $self->{records}++; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 14 | $self->_debug("DataRecords finished! Found a total of " . $self->{records} . " Records"); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 15 | ▶ | sub on { | ||||||||||||||||||||||||||
|
Subroutine
on: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 199 | 15 | my $msgType = shift; | |||||||||||||||||||||||||||
| 200 | 15 | my $handler = shift; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 15 | my $msgHandlers = $self->{messageHandlers}; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 15 | ▶ | if($handler) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 9 | $msgHandlers->{$msgType} = $handler; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | else { | ||||||||||||||||||||||||||||
| 208 | 6 | delete $msgHandlers->{$msgType}; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 14611 | ▶ | sub emitRecord { | ||||||||||||||||||||||||||
|
Subroutine
emitRecord: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 213 | 14611 | my $self = shift; | |||||||||||||||||||||||||||
| 214 | 14611 | my ($msgType, $msgData) = @_; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 14611 | ▶ | if($msgType eq 'field_description') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 2 | $self->_debug("Encountered a field_description message"); | |||||||||||||||||||||||||||
| 218 | 2 | $self->attachDeveloperDataToGlobalMessage($msgData); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 14611 | ▶ | if(my $handler = $self->getHandler($msgType)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 10147 | $handler->($msgData); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 14611 | ▶ | if(my $allHandler = $self->getHandler("_any")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 9303 | $allHandler->($msgType, $msgData); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 2 | ▶ | sub attachDeveloperDataToGlobalMessage { | ||||||||||||||||||||||||||
|
Subroutine
attachDeveloperDataToGlobalMessage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 231 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 232 | 2 | my $msgData = shift; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 2 | my $globalMessageid = $msgData->{native_mesg_num}->{value}; | |||||||||||||||||||||||||||
| 235 | 2 | my $fieldId = $msgData->{field_definition_number}->{value}; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 2 | my $globalMessage = $self->_get_global_message_type($globalMessageid); | |||||||||||||||||||||||||||
| 238 | $globalMessage->{developer_data}->{$fieldId} = { | ||||||||||||||||||||||||||||
| 239 | id => $fieldId, | ||||||||||||||||||||||||||||
| 240 | name => $msgData->{field_name}->{value}, | ||||||||||||||||||||||||||||
| 241 | units => $msgData->{units}->{value}, | ||||||||||||||||||||||||||||
| 242 | 2 | baseType => $self->_get_base_type($msgData->{fit_base_type_id}->{value} & 15), | |||||||||||||||||||||||||||
| 243 | type => undef, | ||||||||||||||||||||||||||||
| 244 | # Developer data ist never scaled/offsetted! | ||||||||||||||||||||||||||||
| 245 | offset => undef, | ||||||||||||||||||||||||||||
| 246 | scale => undef, | ||||||||||||||||||||||||||||
| 247 | }; | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 29222 | ▶ | sub getHandler { | ||||||||||||||||||||||||||
|
Subroutine
getHandler: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 251 | 29222 | my $self = shift; | |||||||||||||||||||||||||||
| 252 | 29222 | my $msgType = shift; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 29222 | ▶ | if(!$msgType) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 0 | die "cannot get a handler for an unknown msgType!"; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 29222 | ▶ | if(exists $self->{messageHandlers}->{$msgType}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 19450 | return $self->{messageHandlers}->{$msgType}; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 9772 | return undef; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 364 | ▶ | sub _parse_definition_message { | ||||||||||||||||||||||||||
|
Subroutine
_parse_definition_message: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 266 | 364 | my $self = shift; | |||||||||||||||||||||||||||
| 267 | 364 | my $header = shift; | |||||||||||||||||||||||||||
| 268 | 364 | my $localMessageType = $header->{localMessageType}; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 364 | my $data = $self->_readBytes(5); | |||||||||||||||||||||||||||
| 271 | 364 | my ($reserved, $arch, $globalMessageId, $fields) = unpack("ccsc", $data); | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 364 | my $globalMessageType = $self->_get_global_message_type($globalMessageId); | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 364 | $self->_debug("DefinitionMessageHeader:"); | |||||||||||||||||||||||||||
| 276 | 364 | ▶ | $self->_debug("Arch: $arch - GlobalMessage: " . (defined $globalMessageType ? $globalMessageType->{name} : "<UNKNOWN_GLOBAL_MESSAGE>") . " ($globalMessageId) - #Fields: $fields"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 364 | ▶ | confess "BigEndian isn't supported so far!" if($arch == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 364 | my ($messageFields, $devMsgFields, $recordLength) = ([], [], 0); | |||||||||||||||||||||||||||
| 280 | 364 | my @fields; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 364 | ▶ | my $fieldDefinitions = defined $globalMessageType ? $globalMessageType->{fields} : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 364 | ($messageFields, $recordLength) = $self->_parse_defintion_message_fields($fieldDefinitions, $fields); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 364 | ▶ | if($header->{isDeveloperData}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 2 | ($devMsgFields, my $devRecordLength) = $self->parseDeveloperDataDefinitionMessage($globalMessageType); | |||||||||||||||||||||||||||
| 287 | 2 | $recordLength += $devRecordLength; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 364 | my $combinedDataFields = [@$messageFields, @$devMsgFields]; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 5339 | ▶ | my $buildPackTemplate = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 293 | 5339 | my $dataField = shift; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 5339 | my $templateChar = $dataField->{baseType}->{packTemplate}; | |||||||||||||||||||||||||||
| 296 | 5339 | my $arrayLength = $dataField->{arrayLength}; | |||||||||||||||||||||||||||
| 297 | 5339 | my $fieldName = $dataField->{fieldDescriptor}->{name}; | |||||||||||||||||||||||||||
| 298 | 5339 | ▶ | my $fieldDatatype = $dataField->{fieldDescriptor}->{type} || "<UNKNOWN_DATA_TYPE>"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$dataField->{'fieldDescriptor'}{'type'} || '<UNKNOWN_DATA_TYPE>'
MC/DC100%1/1
$dataField->{'fieldDescriptor'}{'type'} || '<UNKNOWN_DATA_TYPE>'
$dataField->{'fieldDescriptor'}{'type'}
Truth table100%2/2
A
A= $dataField->{'fieldDescriptor'}{'type'}
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 5339 | my $templateString = "${templateChar}"; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 5339 | ▶ | if($arrayLength > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 896 | $templateString .= "[${arrayLength}]"; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 5339 | $templateString .= " # $fieldName baseType=$fieldDatatype baseTypeSize=$dataField->{baseType}->{size}, storageSize=$dataField->{storageSize}, byteOffsetInMessage=" . $dataField->{byteOffsetInMessage}; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 5339 | return $templateString; | |||||||||||||||||||||||||||
| 309 | 364 | }; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | my $localMessage = { | ||||||||||||||||||||||||||||
| 312 | size => $recordLength, | ||||||||||||||||||||||||||||
| 313 | dataFields => $combinedDataFields, | ||||||||||||||||||||||||||||
| 314 | globalMessage => $globalMessageType,, | ||||||||||||||||||||||||||||
| 315 | 5339 | unpackTemplate => join("\n", map { $buildPackTemplate->($_) } @$combinedDataFields), | |||||||||||||||||||||||||||
| 316 | isDeveloperMessage => $header->{isDeveloperData}, | ||||||||||||||||||||||||||||
| 317 | 364 | isUnknownMessage => !defined $globalMessageType, | |||||||||||||||||||||||||||
| 318 | }; | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 364 | $self->{localMessages}->[$localMessageType] = $localMessage; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 364 | $self->_debug("Following Record length: " . $localMessage->{size} . " bytes"); | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 2 | ▶ | sub parseDeveloperDataDefinitionMessage { | ||||||||||||||||||||||||||
|
Subroutine
parseDeveloperDataDefinitionMessage: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 326 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 327 | 2 | my $globalMessageType = shift; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 2 | my $developerFieldCount = unpack("C", $self->_readBytes(1)); | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 2 | my ($devMsgFields, $devRecordLength) = $self->_parse_defintion_message_fields($globalMessageType->{developer_data}, $developerFieldCount); | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 2 | foreach my $field (@$devMsgFields) { | |||||||||||||||||||||||||||
| 334 | # BaseTypes of Dev Data is not included in the 3bytes of the definition message but in the field_description message which introduces the dev data | ||||||||||||||||||||||||||||
| 335 | # Therefore we simply overwrite the wrongly extracted BaseType from the definition message with the correct one. | ||||||||||||||||||||||||||||
| 336 | 2 | $field->{baseType} = $field->{fieldDescriptor}->{baseType}; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 2 | return ($devMsgFields, $devRecordLength); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 366 | ▶ | sub _parse_defintion_message_fields { | ||||||||||||||||||||||||||
|
Subroutine
_parse_defintion_message_fields: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 343 | 366 | my $self = shift; | |||||||||||||||||||||||||||
| 344 | 366 | my $fieldDefinitions = shift; | |||||||||||||||||||||||||||
| 345 | 366 | my $numberOfFields = shift; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 366 | my $recordLength = 0; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 366 | my @dataFields; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 366 | my $byteOffsetInMessage = 0; | |||||||||||||||||||||||||||
| 352 | 366 | foreach my $i (1..$numberOfFields) { | |||||||||||||||||||||||||||
| 353 | 5339 | my $fieldDefinitionData = $self->_readBytes(3); # Every Field has 3 Bytes | |||||||||||||||||||||||||||
| 354 | 5339 | my ($fieldDefinitionId, $size, $baseTypeData) = unpack("CCc", $fieldDefinitionData); | |||||||||||||||||||||||||||
| 355 | 5339 | my ($baseTypeEndian, $baseTypeNumber) = ($baseTypeData & 128, $baseTypeData & 15); | |||||||||||||||||||||||||||
| 356 | 5339 | my $baseType = $self->_get_base_type($baseTypeNumber); | |||||||||||||||||||||||||||
| 357 | 5339 | my $fieldDescriptor = $fieldDefinitions->{$fieldDefinitionId}; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 5339 | ▶ | die "Failed to parse file: Size=$size cannot be 0 or less" if($size <= 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 5339 | ▶ | if(!defined $fieldDescriptor) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | $fieldDescriptor = { | ||||||||||||||||||||||||||||
| 363 | isUnkownField => 1, | ||||||||||||||||||||||||||||
| 364 | name => "<UNKNOWN_FIELD_NAME>", | ||||||||||||||||||||||||||||
| 365 | type => $baseType->{name}, | ||||||||||||||||||||||||||||
| 366 | 574 | }; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 5339 | my $fieldName = $fieldDescriptor->{name}; | |||||||||||||||||||||||||||
| 370 | 5339 | $self->_debug("FieldDefinition[$i]: $fieldName ($fieldDefinitionId), Size: $size, BaseType: " . $baseType->{name} . " ($baseTypeNumber), BaseTypeEndian: $baseTypeEndian"); | |||||||||||||||||||||||||||
| 371 | 5339 | $recordLength += $size; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | my $fieldDefinition = { | ||||||||||||||||||||||||||||
| 374 | baseType => $baseType, | ||||||||||||||||||||||||||||
| 375 | storageSize => $size, | ||||||||||||||||||||||||||||
| 376 | isArray => $size > $baseType->{size}, | ||||||||||||||||||||||||||||
| 377 | arrayLength => $size/$baseType->{size}, | ||||||||||||||||||||||||||||
| 378 | 5339 | fieldDescriptor => $fieldDescriptor, | |||||||||||||||||||||||||||
| 379 | byteOffsetInMessage => $byteOffsetInMessage | ||||||||||||||||||||||||||||
| 380 | }; | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 5339 | push(@dataFields, $fieldDefinition); | |||||||||||||||||||||||||||
| 383 | 5339 | $byteOffsetInMessage += $size; | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 366 | return (\@dataFields, $recordLength); | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 366 | ▶ | sub _global_message_id_to_name { | ||||||||||||||||||||||||||
|
Subroutine
_global_message_id_to_name: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 390 | 366 | my $self = shift; | |||||||||||||||||||||||||||
| 391 | 366 | my $globalMessageId = shift; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # Manufacterer specific message types | ||||||||||||||||||||||||||||
| 394 | 366 | ▶ | if($globalMessageId >= 0xFF00) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 0 | return "mfg_range_min"; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 366 | state $globalMessageNames = { | |||||||||||||||||||||||||||
| 399 | 0 => "file_id", | ||||||||||||||||||||||||||||
| 400 | 1 => "capabilities", | ||||||||||||||||||||||||||||
| 401 | 2 => "device_settings", | ||||||||||||||||||||||||||||
| 402 | 3 => "user_profile", | ||||||||||||||||||||||||||||
| 403 | 4 => "hrm_profile", | ||||||||||||||||||||||||||||
| 404 | 5 => "sdm_profile", | ||||||||||||||||||||||||||||
| 405 | 6 => "bike_profile", | ||||||||||||||||||||||||||||
| 406 | 7 => "zones_target", | ||||||||||||||||||||||||||||
| 407 | 8 => "hr_zone", | ||||||||||||||||||||||||||||
| 408 | 9 => "power_zone", | ||||||||||||||||||||||||||||
| 409 | 10 => "met_zone", | ||||||||||||||||||||||||||||
| 410 | 12 => "sport", | ||||||||||||||||||||||||||||
| 411 | 15 => "goal", | ||||||||||||||||||||||||||||
| 412 | 18 => "session", | ||||||||||||||||||||||||||||
| 413 | 19 => "lap", | ||||||||||||||||||||||||||||
| 414 | 20 => "record", | ||||||||||||||||||||||||||||
| 415 | 21 => "event", | ||||||||||||||||||||||||||||
| 416 | 23 => "device_info", | ||||||||||||||||||||||||||||
| 417 | 26 => "workout", | ||||||||||||||||||||||||||||
| 418 | 27 => "workout_step", | ||||||||||||||||||||||||||||
| 419 | 28 => "schedule", | ||||||||||||||||||||||||||||
| 420 | 30 => "weight_scale", | ||||||||||||||||||||||||||||
| 421 | 31 => "course", | ||||||||||||||||||||||||||||
| 422 | 32 => "course_point", | ||||||||||||||||||||||||||||
| 423 | 33 => "totals", | ||||||||||||||||||||||||||||
| 424 | 34 => "activity", | ||||||||||||||||||||||||||||
| 425 | 35 => "software", | ||||||||||||||||||||||||||||
| 426 | 37 => "file_capabilities", | ||||||||||||||||||||||||||||
| 427 | 38 => "mesg_capabilities", | ||||||||||||||||||||||||||||
| 428 | 39 => "field_capabilities", | ||||||||||||||||||||||||||||
| 429 | 49 => "file_creator", | ||||||||||||||||||||||||||||
| 430 | 51 => "blood_pressure", | ||||||||||||||||||||||||||||
| 431 | 53 => "speed_zone", | ||||||||||||||||||||||||||||
| 432 | 55 => "monitoring", | ||||||||||||||||||||||||||||
| 433 | 72 => "training_file", | ||||||||||||||||||||||||||||
| 434 | 78 => "hrv", | ||||||||||||||||||||||||||||
| 435 | 80 => "ant_rx", | ||||||||||||||||||||||||||||
| 436 | 81 => "ant_tx", | ||||||||||||||||||||||||||||
| 437 | 82 => "ant_channel_id", | ||||||||||||||||||||||||||||
| 438 | 101 => "length", | ||||||||||||||||||||||||||||
| 439 | 103 => "monitoring_info", | ||||||||||||||||||||||||||||
| 440 | 105 => "pad", | ||||||||||||||||||||||||||||
| 441 | 106 => "slave_device", | ||||||||||||||||||||||||||||
| 442 | 127 => "connectivity", | ||||||||||||||||||||||||||||
| 443 | 128 => "weather_conditions", | ||||||||||||||||||||||||||||
| 444 | 129 => "weather_alert", | ||||||||||||||||||||||||||||
| 445 | 131 => "cadence_zone", | ||||||||||||||||||||||||||||
| 446 | 132 => "hr", | ||||||||||||||||||||||||||||
| 447 | 142 => "segment_lap", | ||||||||||||||||||||||||||||
| 448 | 145 => "memo_glob", | ||||||||||||||||||||||||||||
| 449 | 148 => "segment_id", | ||||||||||||||||||||||||||||
| 450 | 149 => "segment_leaderboard_entry", | ||||||||||||||||||||||||||||
| 451 | 150 => "segment_point", | ||||||||||||||||||||||||||||
| 452 | 151 => "segment_file", | ||||||||||||||||||||||||||||
| 453 | 158 => "workout_session", | ||||||||||||||||||||||||||||
| 454 | 159 => "watchface_settings", | ||||||||||||||||||||||||||||
| 455 | 160 => "gps_metadata", | ||||||||||||||||||||||||||||
| 456 | 161 => "camera_event", | ||||||||||||||||||||||||||||
| 457 | 162 => "timestamp_correlation", | ||||||||||||||||||||||||||||
| 458 | 164 => "gyroscope_data", | ||||||||||||||||||||||||||||
| 459 | 165 => "accelerometer_data", | ||||||||||||||||||||||||||||
| 460 | 167 => "three_d_sensor_calibration", | ||||||||||||||||||||||||||||
| 461 | 169 => "video_frame", | ||||||||||||||||||||||||||||
| 462 | 174 => "obdii_data", | ||||||||||||||||||||||||||||
| 463 | 177 => "nmea_sentence", | ||||||||||||||||||||||||||||
| 464 | 178 => "aviation_attitude", | ||||||||||||||||||||||||||||
| 465 | 184 => "video", | ||||||||||||||||||||||||||||
| 466 | 185 => "video_title", | ||||||||||||||||||||||||||||
| 467 | 186 => "video_description", | ||||||||||||||||||||||||||||
| 468 | 187 => "video_clip", | ||||||||||||||||||||||||||||
| 469 | 188 => "ohr_settings", | ||||||||||||||||||||||||||||
| 470 | 200 => "exd_screen_configuration", | ||||||||||||||||||||||||||||
| 471 | 201 => "exd_data_field_configuration", | ||||||||||||||||||||||||||||
| 472 | 202 => "exd_data_concept_configuration", | ||||||||||||||||||||||||||||
| 473 | 206 => "field_description", | ||||||||||||||||||||||||||||
| 474 | 207 => "developer_data_id", | ||||||||||||||||||||||||||||
| 475 | 208 => "magnetometer_data", | ||||||||||||||||||||||||||||
| 476 | 209 => "barometer_data", | ||||||||||||||||||||||||||||
| 477 | 210 => "one_d_sensor_calibration", | ||||||||||||||||||||||||||||
| 478 | 225 => "set", | ||||||||||||||||||||||||||||
| 479 | 227 => "stress_level", | ||||||||||||||||||||||||||||
| 480 | 258 => "dive_settings", | ||||||||||||||||||||||||||||
| 481 | 259 => "dive_gas", | ||||||||||||||||||||||||||||
| 482 | 262 => "dive_alarm", | ||||||||||||||||||||||||||||
| 483 | 264 => "exercise_title", | ||||||||||||||||||||||||||||
| 484 | 268 => "dive_summary", | ||||||||||||||||||||||||||||
| 485 | 285 => "jump", | ||||||||||||||||||||||||||||
| 486 | 317 => "climb_pro", | ||||||||||||||||||||||||||||
| 487 | }; | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 366 | ▶ | if(exists $globalMessageNames->{$globalMessageId}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 350 | return $globalMessageNames->{$globalMessageId}; | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | else { | ||||||||||||||||||||||||||||
| 493 | 16 | return undef; | |||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 14712 | ▶ | sub getLocalMessageById { | ||||||||||||||||||||||||||
|
Subroutine
getLocalMessageById: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 498 | 14712 | my $self = shift; | |||||||||||||||||||||||||||
| 499 | 14712 | my $localMessageId = shift; | |||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 14712 | my $localMessage = $self->{localMessages}->[$localMessageId]; | |||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 14712 | ▶ | if(!defined $localMessage) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 0 | die "Encountered a record localMessageId=$localMessageId which was not introduced by a definition message!"; | |||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 14712 | return $localMessage; | |||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 366 | ▶ | sub _get_global_message_type { | ||||||||||||||||||||||||||
|
Subroutine
_get_global_message_type: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 511 | 366 | my $self = shift; | |||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 366 | my $globalMessageName = $self->_global_message_id_to_name(shift); | |||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 366 | ▶ | if(!defined $globalMessageName) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 516 | 16 | return undef; | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 350 | ▶ | if(exists $Parser::FIT::Profile::PROFILE->{$globalMessageName}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 350 | return $Parser::FIT::Profile::PROFILE->{$globalMessageName}; | |||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||
| 522 | else { | ||||||||||||||||||||||||||||
| 523 | 0 | return undef; | |||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 14712 | ▶ | sub _parse_local_message_record { | ||||||||||||||||||||||||||
|
Subroutine
_parse_local_message_record: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 528 | 14712 | my $self = shift; | |||||||||||||||||||||||||||
| 529 | 14712 | my $header = shift; | |||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 14712 | my $localMessageId = $header->{localMessageType}; | |||||||||||||||||||||||||||
| 532 | 14712 | my $localMessage = $self->getLocalMessageById($localMessageId); | |||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 14712 | my $recordLength = $localMessage->{size}; | |||||||||||||||||||||||||||
| 535 | 14712 | my $record = $self->_readBytes($recordLength); | |||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | # skip unknown messages (the _readBytes above is correct, since we need to "remove" the bytes from the stream) | ||||||||||||||||||||||||||||
| 538 | 14712 | ▶ | if($localMessage->{isUnknownMessage}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 539 | 101 | return undef; | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 14611 | my $unpackTemplate = $localMessage->{unpackTemplate}; | |||||||||||||||||||||||||||
| 543 | 14611 | my @rawFields = unpack($unpackTemplate, $record); | |||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 14611 | my %result; | |||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 14611 | my $fieldCount = scalar @{$localMessage->{dataFields}}; | |||||||||||||||||||||||||||
| 548 | 14611 | my $rawValueIndex = 0; | |||||||||||||||||||||||||||
| 549 | 14611 | ▶ | for(my $i = 0; $i < $fieldCount; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 550 | 200572 | my $localMessageField = $localMessage->{dataFields}->[$i]; | |||||||||||||||||||||||||||
| 551 | 200572 | my $rawValue = $rawFields[$rawValueIndex]; | |||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | # Special case for strings: | ||||||||||||||||||||||||||||
| 554 | # The pack/unpack template for strings looks like an array (e.G. Z[32]) but does not produce | ||||||||||||||||||||||||||||
| 555 | # multiple entries after unpacking, but only a single one. | ||||||||||||||||||||||||||||
| 556 | # Therefore after reading a string we should only proceed to the next entry. | ||||||||||||||||||||||||||||
| 557 | # While reading a uint8 array (e.G. c[4]) we must skip 4 entries to arrive at the next field. | ||||||||||||||||||||||||||||
| 558 | 200572 | ▶ | if($localMessageField->{baseType}->{name} eq "string") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | 11 | $rawValueIndex += 1; | |||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | else { | ||||||||||||||||||||||||||||
| 562 | 200561 | $rawValueIndex += $localMessageField->{arrayLength}; | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 200572 | my $fieldDescriptor = $localMessageField->{fieldDescriptor}; | |||||||||||||||||||||||||||
| 566 | 200572 | my $fieldName = $fieldDescriptor->{name}; | |||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 200572 | ▶ | if($fieldDescriptor->{isUnkownField}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | 937 | next; | |||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 199635 | my $postProcessedValue = undef; | |||||||||||||||||||||||||||
| 573 | 199635 | my $baseType = $localMessageField->{baseType}; | |||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | 199635 | my $isValid = $self->_check_for_valid_value($baseType, $rawValue); | |||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | 199635 | ▶ | if($isValid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 578 | 182127 | $postProcessedValue = $self->postProcessRawValue($rawValue, $fieldDescriptor); | |||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 199635 | $result{$fieldName} = { | |||||||||||||||||||||||||||
| 582 | value => $postProcessedValue, | ||||||||||||||||||||||||||||
| 583 | rawValue => $rawValue, | ||||||||||||||||||||||||||||
| 584 | fieldDescriptor => $fieldDescriptor, | ||||||||||||||||||||||||||||
| 585 | isInvalid => !$isValid, | ||||||||||||||||||||||||||||
| 586 | }; | ||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | return { | ||||||||||||||||||||||||||||
| 590 | messageType => $localMessage->{globalMessage}->{name}, | ||||||||||||||||||||||||||||
| 591 | 14611 | fields => \%result | |||||||||||||||||||||||||||
| 592 | }; | ||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 199635 | ▶ | sub _check_for_valid_value { | ||||||||||||||||||||||||||
|
Subroutine
_check_for_valid_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 596 | 199635 | my $self = shift; | |||||||||||||||||||||||||||
| 597 | 199635 | my $baseType = shift; | |||||||||||||||||||||||||||
| 598 | 199635 | my $rawValue = shift; | |||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | # All strings are good. | ||||||||||||||||||||||||||||
| 601 | 199635 | ▶ | if($baseType->{name} eq "string") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 602 | 7 | return 1; | |||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | 199628 | my $isValid = $baseType->{invalid} != $rawValue; | |||||||||||||||||||||||||||
| 606 | 199628 | return $isValid; | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | 182133 | ▶ | sub postProcessRawValue { | ||||||||||||||||||||||||||
|
Subroutine
postProcessRawValue: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 610 | 182133 | my $self = shift; | |||||||||||||||||||||||||||
| 611 | 182133 | my $rawValue = shift; | |||||||||||||||||||||||||||
| 612 | 182133 | my $fieldDescriptor = shift; | |||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 182133 | ▶ | if(defined $fieldDescriptor->{scale}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 615 | 77773 | $rawValue /= $fieldDescriptor->{scale}; | |||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 182133 | ▶ | if(defined $fieldDescriptor->{offset}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | 13432 | $rawValue -= $fieldDescriptor->{offset}; | |||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | 182133 | ▶ | if(defined $fieldDescriptor->{unit} && $fieldDescriptor->{unit} eq "semicircles") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $fieldDescriptor->{"unit"} and $fieldDescriptor->{"unit"} eq "semicircles"
MC/DC100%2/2
defined $fieldDescriptor->{"unit"} and $fieldDescriptor->{"unit"} eq "semicircles"
defined $fieldDescriptor->{"unit"}
$fieldDescriptor->{"unit"} eq "semicircles"
Truth table100%3/3
A and B
A= defined $fieldDescriptor->{"unit"}B= $fieldDescriptor->{"unit"} eq "semicircles"
| |||||||||||||||||||||||||||||
| 623 | 1394 | state $semicirclesToDegreesConversionRate = 180 / 2**31; | |||||||||||||||||||||||||||
| 624 | 1394 | $rawValue *= $semicirclesToDegreesConversionRate; | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 182133 | ▶ | if(defined $fieldDescriptor->{type} && $fieldDescriptor->{type} eq "date_time") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $fieldDescriptor->{"type"} and $fieldDescriptor->{"type"} eq "date_time"
MC/DC100%2/2
defined $fieldDescriptor->{"type"} and $fieldDescriptor->{"type"} eq "date_time"
defined $fieldDescriptor->{"type"}
$fieldDescriptor->{"type"} eq "date_time"
Truth table100%3/3
A and B
A= defined $fieldDescriptor->{"type"}B= $fieldDescriptor->{"type"} eq "date_time"
| |||||||||||||||||||||||||||||
| 628 | 14709 | state $fitEpocheOffset = 631065600; | |||||||||||||||||||||||||||
| 629 | 14709 | $rawValue += $fitEpocheOffset; | |||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 182133 | return $rawValue; | |||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | 5341 | ▶ | sub _get_base_type { | ||||||||||||||||||||||||||
|
Subroutine
_get_base_type: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 636 | 5341 | my $self = shift; | |||||||||||||||||||||||||||
| 637 | 5341 | my $index = shift; | |||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | # See "Table 7. FIT Base Types and Invalid Values" at https://developer.garmin.com/fit/protocol/ | ||||||||||||||||||||||||||||
| 640 | 5341 | my $types = [ | |||||||||||||||||||||||||||
| 641 | { | ||||||||||||||||||||||||||||
| 642 | name => "enum", | ||||||||||||||||||||||||||||
| 643 | size => 1, | ||||||||||||||||||||||||||||
| 644 | invalid => 0xff, | ||||||||||||||||||||||||||||
| 645 | packTemplate => "c", | ||||||||||||||||||||||||||||
| 646 | }, | ||||||||||||||||||||||||||||
| 647 | { | ||||||||||||||||||||||||||||
| 648 | name => "sint8", | ||||||||||||||||||||||||||||
| 649 | size => 1, | ||||||||||||||||||||||||||||
| 650 | invalid => 0x7f, | ||||||||||||||||||||||||||||
| 651 | packTemplate => "c" | ||||||||||||||||||||||||||||
| 652 | }, | ||||||||||||||||||||||||||||
| 653 | { | ||||||||||||||||||||||||||||
| 654 | name => "uint8", | ||||||||||||||||||||||||||||
| 655 | size => 1, | ||||||||||||||||||||||||||||
| 656 | invalid => 0xff, | ||||||||||||||||||||||||||||
| 657 | packTemplate => "C", | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | }, | ||||||||||||||||||||||||||||
| 660 | { | ||||||||||||||||||||||||||||
| 661 | name => "sint16", | ||||||||||||||||||||||||||||
| 662 | size => 2, | ||||||||||||||||||||||||||||
| 663 | invalid => 0x7fff, | ||||||||||||||||||||||||||||
| 664 | packTemplate => "s", | ||||||||||||||||||||||||||||
| 665 | }, | ||||||||||||||||||||||||||||
| 666 | { | ||||||||||||||||||||||||||||
| 667 | name => "uint16", | ||||||||||||||||||||||||||||
| 668 | size => 2, | ||||||||||||||||||||||||||||
| 669 | invalid => 0xffff, | ||||||||||||||||||||||||||||
| 670 | packTemplate => "S" | ||||||||||||||||||||||||||||
| 671 | }, | ||||||||||||||||||||||||||||
| 672 | { | ||||||||||||||||||||||||||||
| 673 | name => "sint32", | ||||||||||||||||||||||||||||
| 674 | size => 4, | ||||||||||||||||||||||||||||
| 675 | invalid => 0x7fffffff, | ||||||||||||||||||||||||||||
| 676 | packTemplate => "l" | ||||||||||||||||||||||||||||
| 677 | }, | ||||||||||||||||||||||||||||
| 678 | { | ||||||||||||||||||||||||||||
| 679 | name => "uint32", | ||||||||||||||||||||||||||||
| 680 | size => 4, | ||||||||||||||||||||||||||||
| 681 | invalid => 0xffffffff, | ||||||||||||||||||||||||||||
| 682 | packTemplate => "L", | ||||||||||||||||||||||||||||
| 683 | }, | ||||||||||||||||||||||||||||
| 684 | { | ||||||||||||||||||||||||||||
| 685 | name => "string", | ||||||||||||||||||||||||||||
| 686 | size => 1, | ||||||||||||||||||||||||||||
| 687 | invalid => 0x00, | ||||||||||||||||||||||||||||
| 688 | packTemplate => "Z" | ||||||||||||||||||||||||||||
| 689 | }, | ||||||||||||||||||||||||||||
| 690 | { | ||||||||||||||||||||||||||||
| 691 | name => "float32", | ||||||||||||||||||||||||||||
| 692 | size => 4, | ||||||||||||||||||||||||||||
| 693 | invalid => 0xffffffff, | ||||||||||||||||||||||||||||
| 694 | packTemplate => "f" | ||||||||||||||||||||||||||||
| 695 | }, | ||||||||||||||||||||||||||||
| 696 | { | ||||||||||||||||||||||||||||
| 697 | name => "float64", | ||||||||||||||||||||||||||||
| 698 | size => 8, | ||||||||||||||||||||||||||||
| 699 | invalid => Math::BigInt->new("0xffffffffffffffff"), | ||||||||||||||||||||||||||||
| 700 | packTemplate => "d", | ||||||||||||||||||||||||||||
| 701 | }, | ||||||||||||||||||||||||||||
| 702 | { | ||||||||||||||||||||||||||||
| 703 | name => "uint8z", | ||||||||||||||||||||||||||||
| 704 | size => 1, | ||||||||||||||||||||||||||||
| 705 | invalid => 0x00, | ||||||||||||||||||||||||||||
| 706 | # TODO this should be a capital C since it is unsigned! | ||||||||||||||||||||||||||||
| 707 | packTemplate => "c" | ||||||||||||||||||||||||||||
| 708 | }, | ||||||||||||||||||||||||||||
| 709 | { | ||||||||||||||||||||||||||||
| 710 | name => "uint16z", | ||||||||||||||||||||||||||||
| 711 | size => 2, | ||||||||||||||||||||||||||||
| 712 | invalid => 0x0000, | ||||||||||||||||||||||||||||
| 713 | packTemplate => "S", | ||||||||||||||||||||||||||||
| 714 | }, | ||||||||||||||||||||||||||||
| 715 | { | ||||||||||||||||||||||||||||
| 716 | name => "uint32z", | ||||||||||||||||||||||||||||
| 717 | size => 4, | ||||||||||||||||||||||||||||
| 718 | invalid => 0x00000000, | ||||||||||||||||||||||||||||
| 719 | packTemplate => "L" | ||||||||||||||||||||||||||||
| 720 | }, | ||||||||||||||||||||||||||||
| 721 | { | ||||||||||||||||||||||||||||
| 722 | name => "byte", | ||||||||||||||||||||||||||||
| 723 | size => 1, | ||||||||||||||||||||||||||||
| 724 | invalid => 0xFF, | ||||||||||||||||||||||||||||
| 725 | packTemplate => "C", | ||||||||||||||||||||||||||||
| 726 | }, | ||||||||||||||||||||||||||||
| 727 | { | ||||||||||||||||||||||||||||
| 728 | name => "sint64", | ||||||||||||||||||||||||||||
| 729 | size => 8, | ||||||||||||||||||||||||||||
| 730 | invalid => Math::BigInt->new("0x7fffffffffffffff"), | ||||||||||||||||||||||||||||
| 731 | packTemplate => "q", | ||||||||||||||||||||||||||||
| 732 | }, | ||||||||||||||||||||||||||||
| 733 | { | ||||||||||||||||||||||||||||
| 734 | name => "uint64", | ||||||||||||||||||||||||||||
| 735 | size => 8, | ||||||||||||||||||||||||||||
| 736 | invalid => Math::BigInt->new("0xffffffffffffffff"), | ||||||||||||||||||||||||||||
| 737 | packTemplate => "Q", | ||||||||||||||||||||||||||||
| 738 | }, | ||||||||||||||||||||||||||||
| 739 | { | ||||||||||||||||||||||||||||
| 740 | name => "uint64z", | ||||||||||||||||||||||||||||
| 741 | size => 8, | ||||||||||||||||||||||||||||
| 742 | invalid => 0x0000000000000000, | ||||||||||||||||||||||||||||
| 743 | packTemplate => "Q", | ||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||
| 745 | ]; | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | 5341 | ▶ | if($index >= @{$types}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 748 | 0 | die "Invalid index=$index for BaseTypeLookup!"; | |||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | 5341 | return $types->[$index]; | |||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | 0 | ▶ | sub _parse_crc { | ||||||||||||||||||||||||||
|
Subroutine
_parse_crc: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 755 | # TODO implement this one...some time :D | ||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | 36717 | ▶ | sub _debug { | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 759 | 36717 | my $self = shift; | |||||||||||||||||||||||||||
| 760 | 36717 | ▶ | if($self->{_DEBUG}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 761 | 0 | print "[FIT.pm DEBUG] ", @_; | |||||||||||||||||||||||||||
| 762 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | 35521 | ▶ | sub _readBytes { | ||||||||||||||||||||||||||
|
Subroutine
_readBytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 767 | 35521 | my $self = shift; | |||||||||||||||||||||||||||
| 768 | 35521 | my $num = shift; | |||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | 35521 | $self->{totalBytesRead} += $num; | |||||||||||||||||||||||||||
| 771 | 35521 | my $buffer; | |||||||||||||||||||||||||||
| 772 | 35521 | my $bytesRead = read($self->{fh}, $buffer, $num); | |||||||||||||||||||||||||||
| 773 | # TODO error handling based on bytesRead | ||||||||||||||||||||||||||||
| 774 | 35521 | return $buffer; | |||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | 12 | 1; | |||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | __END__ | ||||||||||||||||||||||||||||