Skip to content

Commit 4376f1d

Browse files
PeterK-endclaude
andcommitted
writes invalid values for null array entries on FIT export
Since @garmin/fitsdk 21.218 the encoder throws on values that are not a number, string, boolean or date, including null entries inside array fields. The decoder returns exactly such nulls for unset array slots, which Garmin files carry in e.g. device settings, so these files could no longer be exported at all ("Failed to encode FIT file"). Before encoding, null array entries are now replaced with the invalid value of the field's base type, as the encoder did itself before. Scale and offset are reversed so the encoder lands on the raw invalid value. fixes #56 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5a22ca0 commit 4376f1d

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

‎src/js/editView.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,50 @@ function canonicalizeFields(mesgNum, fields) {
960960
return ordered;
961961
}
962962

963+
// FIT "invalid" (= unset) value per base type
964+
const INVALID_BY_BASE_TYPE = {
965+
enum: 0xFF, sint8: 0x7F, uint8: 0xFF, sint16: 0x7FFF, uint16: 0xFFFF,
966+
sint32: 0x7FFFFFFF, uint32: 0xFFFFFFFF, string: '', float32: 0xFFFFFFFF,
967+
float64: 0xFFFFFFFFFFFFFFFF, uint8z: 0, uint16z: 0, uint32z: 0, byte: 0xFF,
968+
sint64: 0x7FFFFFFFFFFFFFFFn, uint64: 0xFFFFFFFFFFFFFFFFn, uint64z: 0n,
969+
};
970+
971+
// Replaces null entries inside array fields with the field's invalid value.
972+
//
973+
// The decoder returns unset slots of an array as null (e.g. Garmin's
974+
// partly filled device settings), but the encoder rejects null since SDK
975+
// 21.218 ("Could not convert null to enum"), so such files could not be
976+
// exported at all. Writing the invalid value is what the encoder did
977+
// itself before. Scale/offset are reversed here because the encoder
978+
// applies them to every numeric value it gets.
979+
function fillNullArrayEntries(mesgNum, fields) {
980+
const fieldsProfile = Profile.messages[mesgNum]?.fields;
981+
if (!fieldsProfile) return fields;
982+
983+
let filled = null;
984+
for (const [name, value] of Object.entries(fields)) {
985+
if (!Array.isArray(value) || !value.includes(null)) continue;
986+
987+
const profile = Object.values(fieldsProfile).find(f => f.name === name);
988+
if (!profile || !(profile.baseType in INVALID_BY_BASE_TYPE)) continue;
989+
990+
// Same scale/offset choice as the SDK's MesgDefinition
991+
const multiComponent = (profile.components?.length ?? 0) > 1;
992+
const first = v => (Array.isArray(v) ? v[0] : v);
993+
const scale = multiComponent ? 1 : first(profile.scale) ?? 1;
994+
const offset = multiComponent ? 0 : first(profile.offset) ?? 0;
995+
996+
let invalid = INVALID_BY_BASE_TYPE[profile.baseType];
997+
if (typeof invalid === 'number' && (scale !== 1 || offset !== 0)) {
998+
invalid = invalid / scale - offset;
999+
}
1000+
1001+
filled ??= { ...fields };
1002+
filled[name] = value.map(v => (v === null ? invalid : v));
1003+
}
1004+
return filled ?? fields;
1005+
}
1006+
9631007
// Converts modifiedData into a flat message list for export
9641008
function prepareExportData(modifiedData) {
9651009

@@ -981,7 +1025,7 @@ function prepareExportData(modifiedData) {
9811025
const mesgNum = getMesgNumByMessagesKey(messageName);
9821026
if (mesgNum == null) continue;
9831027
for (const fields of messages) {
984-
allMessages.push({ mesgNum, ...canonicalizeFields(mesgNum, fields) });
1028+
allMessages.push({ mesgNum, ...canonicalizeFields(mesgNum, fillNullArrayEntries(mesgNum, fields)) });
9851029
}
9861030
}
9871031
return allMessages;

0 commit comments

Comments
 (0)