Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit d8bed5c

Browse files
committed
exclude invalid record values
1 parent 1acccdd commit d8bed5c

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

php-FIT-File-Analysis.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,23 @@ class phpFITFileAnalysis {
366366
13 => 'Ctmp', // byte
367367
);
368368

369+
private $invalid_values = array(
370+
0 => 255, // 0xFF
371+
1 => 127, // 0x7F
372+
2 => 255, // 0xFF
373+
131 => 32767, // 0x7FFF
374+
132 => 65535, // 0xFFFF
375+
133 => 2147483647, // 0x7FFFFFFF
376+
134 => 4294967295, // 0xFFFFFFFF
377+
7 => 0, // 0x00
378+
136 => 4294967295, // 0xFFFFFFFF
379+
137 => 9223372036854775807, // 0xFFFFFFFFFFFFFFFF
380+
10 => 0, // 0x00
381+
139 => 0, // 0x0000
382+
140 => 0, // 0x00000000
383+
13 => 255, // 0xFF
384+
);
385+
369386
/*
370387
* D00001275 Flexible & Interoperable Data Transfer (FIT) Protocol Rev 1.7.pdf
371388
* 4.4 Scale/Offset
@@ -694,14 +711,18 @@ private function read_data_records() {
694711
// Check that we have information on the Data Message.
695712
if(isset($this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']])) {
696713
$tmp_record_array = []; // Temporary array to store Record data message pieces
714+
$tmp_value = null; // Placeholder for value for checking before inserting into the tmp_record_array
697715

698716
foreach($this->defn_mesgs[$local_mesg_type]['field_defns'] as $field_defn) {
699717
// Check that we have information on the Field Definition and a valud base type exists.
700718
if(isset($this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]) && isset($this->types[$field_defn['base_type']])) {
701719

702720
// If it's a Record data message, store all the pieces in the temporary array as the timestamp may not be first...
703721
if($this->defn_mesgs[$local_mesg_type]['global_mesg_num'] === 20) {
704-
$tmp_record_array[$this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['field_name']] = (unpack($this->types[$field_defn['base_type']], substr($this->file_contents, $this->file_pointer, $field_defn['size']))['tmp'] / $this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['scale']) - $this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['offset'];
722+
$tmp_value = unpack($this->types[$field_defn['base_type']], substr($this->file_contents, $this->file_pointer, $field_defn['size']))['tmp'];
723+
724+
// Check if it's an invalid value for the type
725+
$tmp_record_array[$this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['field_name']] = ($tmp_value !== $this->invalid_values[$field_defn['base_type']]) ? $tmp_value / $this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['scale'] - $this->data_mesg_info[$this->defn_mesgs[$local_mesg_type]['global_mesg_num']]['field_defns'][$field_defn['field_definition_number']]['offset'] : null;
705726
}
706727

707728
else {
@@ -717,7 +738,9 @@ private function read_data_records() {
717738
unset($tmp_record_array['timestamp']);
718739

719740
foreach($tmp_record_array as $key => $value) {
720-
$this->data_mesgs['record'][$key][$timestamp] = $value;
741+
if($value !== null) {
742+
$this->data_mesgs['record'][$key][$timestamp] = $value;
743+
}
721744
}
722745
}
723746
}
@@ -760,6 +783,7 @@ private function fix_data($options) {
760783
$missing_distance_keys = [];
761784
$missing_hr_keys = [];
762785
$missing_lat_keys = [];
786+
763787
$missing_lon_keys = [];
764788
$missing_speed_keys = [];
765789
$missing_power_keys = [];

0 commit comments

Comments
 (0)