From 447b9a6abbdc9fc8fd35867f2a40d9c26647d7aa Mon Sep 17 00:00:00 2001 From: Shane Jordan Date: Sun, 29 Nov 2026 23:24:49 +0000 Subject: [PATCH 1/4] feat(opentag3d): support spec v2.004 two-byte MFI load field --- lib/opentag3d/opentag3d-spec-v2.json | 655 ++++++++++++++++++++++++++- lib/opentag3d/opentag3d_lib.c | 50 +- lib/opentag3d/opentag3d_lib.h | 4 +- lib/opentag3d/opentag3d_v2_map.h | 8 +- test/native/test_opentag3d_runner.c | 196 +++++++- 5 files changed, 887 insertions(+), 26 deletions(-) diff --git a/lib/opentag3d/opentag3d-spec-v2.json b/lib/opentag3d/opentag3d-spec-v2.json index 2e352d51..b85c2a47 100644 --- a/lib/opentag3d/opentag3d-spec-v2.json +++ b/lib/opentag3d/opentag3d-spec-v2.json @@ -1 +1,654 @@ -{"version":"2.000","mime_type":"application/opentag3d","core":{"address_range":{"start":"0x00","end":"0xDF"},"fields":[{"name":"Tag Version","id":"tag_version","added":"1.000","unit":"version","type":"int","scaling":0.001,"start":"0x00","length":2,"usage":"operational","examples":[1234],"required":true,"description":"RFID tag data format version, with 3 implied decimal points. Eg `1000` → version `1.000`."},{"name":"Base Material Name","id":"material","added":"1.000","type":"utf8","start":"0x02","length":5,"usage":"display","examples":["PLA","PETG","PCTFE","TPU"],"required":true,"description":"Material name in plain text, excluding any modifiers."},{"name":"Material Modifiers","id":"material_mod","added":"1.000","type":"utf8","start":"0x07","length":5,"usage":"display","examples":["CF","HF","Pro","Silk","95A"],"description":"Material subcategory or modifier in plain text. Long modifiers may need to be abbreviated."},{"name":"Filament Manufacturer","id":"manufacturer","added":"1.000","type":"utf8","start":"0x0C","length":16,"usage":"display","examples":["Example Brand","Polar Filament"],"required":true,"description":"Name of filament manufacturer. Long names should be abbreviated or truncated."},{"name":"Color Name","id":"color_name","added":"1.000","type":"utf8","start":"0x1C","length":32,"usage":"display","examples":["Orange","White","Blue","Electric Watermelon"],"description":"Color in plain text."},{"name":"Color 1 Hex","id":"color_1","added":"1.000","unit":"RGBA","type":"rgba","start":"0x3C","length":4,"usage":"display","examples":[[255,166,77,255]],"required":true,"description":"Primary filament color stored as 4 separate 1-byte integers for red, green, blue and alpha, in the sRGB color space."},{"name":"Color 2 Hex","id":"color_2","added":"1.000","unit":"RGBA","type":"rgba","start":"0x40","length":4,"usage":"display","examples":[[0,0,0,0]],"description":"Second filament color, if the filament is multi-color. Set to transparent black if the filament is single color."},{"name":"Color 3 Hex","id":"color_3","added":"1.000","unit":"RGBA","type":"rgba","start":"0x44","length":4,"usage":"display","examples":[[0,0,0,0]],"description":"Third filament color, if the filament is multi-color. Set to transparent black if the filament is dual color."},{"name":"Color 4 Hex","id":"color_4","added":"1.000","unit":"RGBA","type":"rgba","start":"0x48","length":4,"usage":"display","examples":[[0,0,0,0]],"description":"Fourth filament color, if the filament is multi-color. Set to transparent black if the filament is tri color."},{"name":"Serial Number / Batch ID","id":"serial","added":"1.000","type":"utf8","start":"0x4C","length":32,"usage":"inventory","examples":["1234-ABCD","2024-01-23-1234"],"description":"Manufacturer's identifier for a spool batch or serial number."},{"name":"SKU","id":"sku","added":"2.000","type":"utf8","start":"0x6C","length":16,"usage":"inventory","examples":["G00-A01"],"description":"Product SKU for the material and color."},{"name":"Barcode","id":"barcode","added":"2.000","type":"int","start":"0x7C","length":6,"usage":"inventory","examples":[12345543210],"description":"The barcode number for the spool. This can be UPC12, UPC13, GS1/GTIN. (Recommended: UPC13)"},{"name":"Manufacture Date","id":"mfg_date","added":"1.000","unit":"YYYY,MM,DD","type":"date","start":"0x84","length":4,"usage":"inventory","examples":[[2024,1,23]],"description":"Stored as 2 bytes for year, then 1 byte for month and 1 byte for day."},{"name":"Manufacture Time","id":"mfg_time","added":"1.000","unit":"UTC hh:mm:ss","type":"time","start":"0x88","length":3,"usage":"inventory","examples":[[10,30,45]],"description":"Stored as 1 byte each for hour, minute, and second in 24-hour UTC."},{"name":"Filament Diameter","id":"diameter","added":"1.000","unit":"mm","type":"int","scaling":0.001,"start":"0x8C","length":2,"usage":"operational","examples":[1750,2850],"required":true,"description":"Filament (target) diameter in µm (micrometers). Eg `1750` → `1.750mm`."},{"name":"Measured Tolerance","id":"tolerance","added":"1.000","unit":"mm","type":"int","scaling":0.01,"start":"0x8E","length":1,"usage":"operational","examples":[2,10],"description":"Measured tolerance in millimeters with two implied decimal points. Eg `1` → `±0.01mm` tolerance"},{"name":"Minimum Nozzle Diameter","id":"nozzle_diameter","added":"2.000","unit":"mm","type":"int","scaling":0.1,"start":"0x8F","length":1,"usage":"operational","examples":[2,4,6],"description":"Minimum nozzle diameter in mm (millimeters) with an implied decimal point. Eg `2` → `0.2mm`"},{"name":"Target Print Temperature","id":"print_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x90","length":1,"usage":"operational","examples":[42],"required":true,"description":"Recommended print temperature in degrees Celsius, divided by 5. For example, `42` = `210°C`."},{"name":"Minimum Print Temperature","id":"min_print_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x91","length":1,"usage":"operational","examples":[38],"description":"Minimum nozzle temperature, divided by 5. For example, `38` = `190ºC`."},{"name":"Maximum Print Temperature","id":"max_print_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x92","length":1,"usage":"operational","examples":[45],"description":"Maximum nozzle temperature, divided by 5."},{"name":"Target Chamber Temperature","id":"chamber_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x93","length":1,"usage":"operational","examples":[12,16],"required":true,"description":"Recommended chamber temperature in degrees Celsius, divided by 5. For example, `12` = `60°C`."},{"name":"Target Bed Temperature","id":"bed_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x94","length":1,"usage":"operational","examples":[12,16],"required":true,"description":"Recommended bed temperature in degrees Celsius, divided by 5. For example, `12` = `60°C`."},{"name":"Minimum Bed Temperature","id":"min_bed_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x95","length":1,"usage":"operational","examples":[8],"description":"Minimum bed temperature, divided by 5. For example, `8` = `40ºC`."},{"name":"Maximum Bed Temperature","id":"max_bed_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x96","length":1,"usage":"operational","examples":[12],"description":"Maximum bed temperature, divided by 5."},{"name":"Target Volumetric Speed","id":"target_vso","added":"1.000","unit":"mm³/s","type":"int","start":"0x97","length":1,"usage":"operational","examples":[80],"description":"Default recommended speed."},{"name":"Minimum Volumetric Speed","id":"min_vso","added":"1.000","unit":"mm³/s","type":"int","start":"0x98","length":1,"usage":"operational","examples":[20],"description":"Mininum speed recommendation."},{"name":"Maximum Volumetric Speed","id":"max_vso","added":"1.000","unit":"mm³/s","type":"int","start":"0x99","length":1,"usage":"operational","examples":[120],"description":"Maximum safe speed."},{"name":"Maximum Dry Temperature","id":"max_dry_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0x9A","length":1,"usage":"operational","examples":[10,11],"description":"Maximum safe drying temperature, divided by 5."},{"name":"Dry Time","id":"dry_time","added":"1.000","unit":"hr","type":"int","start":"0x9B","length":1,"usage":"operational","examples":[4,8,12],"description":"Recommended drying time."},{"name":"Density","id":"density","added":"1.000","unit":"g/cm³","type":"int","scaling":0.001,"start":"0x9C","length":2,"usage":"operational","examples":[1240,3900],"required":true,"description":"Filament density in µg (micrograms) per cubic centimeter. Eg `1240` → `1.240g/cm³`. (Recommendations: 1.24 for PLA, 1.07 for ABS, 1.27 for PETG)"},{"name":"Target Weight","id":"weight","added":"1.000","unit":"g","type":"int","start":"0x9E","length":2,"usage":"operational","examples":[1000,5000,750],"required":true,"description":"Filament weight in grams, excluding spool weight. This is the TARGET weight (e.g., 1kg). Actual measured weight is stored in a different field."},{"name":"Empty Spool Weight","id":"empty_spool_weight","added":"1.000","unit":"g","type":"int","start":"0xA0","length":2,"usage":"operational","examples":[105],"description":"Weight of empty spool in grams."},{"name":"Measured Filament Length","id":"measured_length","added":"1.000","unit":"m","type":"int","start":"0xA2","length":2,"usage":"operational","examples":[336],"description":"Length in meters."},{"name":"Measured Filament Weight","id":"measured_weight","added":"1.000","unit":"g","type":"int","start":"0xA4","length":2,"usage":"operational","examples":[1002],"description":"Weight of filament only. This should be the actual measurement taken"},{"name":"Spool Core Diameter","id":"spool_core_diameter","added":"1.000","unit":"mm","type":"int","start":"0xA6","length":1,"usage":"operational","examples":[100,80],"description":"Core diameter in mm (millimeters)."},{"name":"Transmission Distance (TD)","id":"td","added":"1.000","unit":"mm","type":"int","scaling":0.1,"start":"0xA7","length":1,"usage":"operational","examples":[118],"description":"Opaque thickness in tens of millimeters Eg. `118` → `11.8`."},{"name":"MFI Temp","id":"mfi_temp","added":"1.000","unit":"ºC","type":"int","scaling":5,"start":"0xA8","length":1,"usage":"operational","examples":[210],"description":"MFI test temperature, divided by 5. For example, `42` = `210ºC`."},{"name":"MFI Load","id":"mfi_load","added":"1.000","unit":"g","type":"int","scaling":10,"start":"0xA9","length":1,"usage":"operational","examples":[216],"description":"MFI test load grams, divided by 10. For example, `216` = `2.16kg`."},{"name":"MFI Value","id":"mfi_value","added":"1.000","unit":"g/min","type":"int","start":"0xAA","length":1,"usage":"operational","examples":[63],"description":"MFI value."},{"name":"Online Data URL","id":"data_url","added":"1.000","type":"ascii","start":"0xB8","length":32,"usage":"operational","examples":["pfil.us?i=8078-RQSR"],"description":"URL to access online JSON additional parameters. Formatted without `https` to save space."}]}} \ No newline at end of file +{ + "version": "2.004", + "mime_type": "application/opentag3d", + "core": { + "address_range": { "start": "0x00", "end": "0xDF" }, + "fields": [ + { + "name": "Tag Version", + "id": "tag_version", + "required": true, + "added": "1.000", + "unit": "version", + "type": "int", + "scaling": 0.001, + "start": "0x00", + "length": 2, + "usage": "operational", + "examples": [1234], + "description": "RFID tag data format version, with 3 implied decimal points. Eg `1000` → version `1.000`." + }, + { + "name": "Base Material Name", + "id": "material", + "required": true, + "added": "1.000", + "type": "utf8", + "start": "0x02", + "length": 5, + "usage": "display", + "examples": ["PLA", "PETG", "PCTFE", "TPU"], + "description": "Material name in plain text, excluding any modifiers." + }, + { + "name": "Material Modifiers", + "id": "material_mod", + "required": false, + "added": "1.000", + "type": "utf8", + "start": "0x07", + "length": 5, + "usage": "display", + "examples": ["CF", "HF", "Pro", "Silk", "95A"], + "description": "Material subcategory or modifier in plain text. Long modifiers may need to be abbreviated." + }, + { + "name": "Filament Brand", + "id": "manufacturer", + "required": true, + "added": "1.000", + "type": "utf8", + "start": "0x0C", + "length": 16, + "usage": "display", + "examples": ["Example Brand", "Polar Filament"], + "description": "Name of filament brand (such as the manufacturer or product family). Long names should be abbreviated or truncated." + }, + { + "name": "Color Name", + "id": "color_name", + "required": false, + "added": "1.000", + "type": "utf8", + "start": "0x1C", + "length": 32, + "usage": "display", + "examples": ["Orange", "White", "Blue", "Electric Watermelon"], + "description": "Color in plain text." + }, + { + "name": "Color 1 Hex", + "id": "color_1", + "required": true, + "added": "1.000", + "unit": "RGBA", + "type": "rgba", + "start": "0x3C", + "length": 4, + "usage": "display", + "examples": [[255, 166, 77, 255]], + "description": "Primary filament color stored as 4 separate 1-byte integers for red, green, blue and alpha, in the sRGB color space." + }, + { + "name": "Color 2 Hex", + "id": "color_2", + "required": false, + "added": "1.000", + "unit": "RGBA", + "type": "rgba", + "start": "0x40", + "length": 4, + "usage": "display", + "examples": [[0, 0, 0, 0]], + "description": "Second filament color, if the filament is multi-color. Set to transparent black if the filament is single color." + }, + { + "name": "Color 3 Hex", + "id": "color_3", + "required": false, + "added": "1.000", + "unit": "RGBA", + "type": "rgba", + "start": "0x44", + "length": 4, + "usage": "display", + "examples": [[0, 0, 0, 0]], + "description": "Third filament color, if the filament is multi-color. Set to transparent black if the filament is dual color." + }, + { + "name": "Color 4 Hex", + "id": "color_4", + "required": false, + "added": "1.000", + "unit": "RGBA", + "type": "rgba", + "start": "0x48", + "length": 4, + "usage": "display", + "examples": [[0, 0, 0, 0]], + "description": "Fourth filament color, if the filament is multi-color. Set to transparent black if the filament is tri color." + }, + { + "name": "Serial Number / Batch ID", + "id": "serial", + "required": false, + "added": "1.000", + "type": "utf8", + "start": "0x4C", + "length": 32, + "usage": "inventory", + "examples": ["1234-ABCD", "2024-01-23-1234"], + "description": "Brand's identifier for a spool batch or serial number." + }, + { + "name": "SKU", + "id": "sku", + "required": false, + "added": "2.000", + "type": "utf8", + "start": "0x6C", + "length": 16, + "usage": "inventory", + "examples": ["G00-A01"], + "description": "Product SKU for the material and color." + }, + { + "name": "Barcode", + "id": "barcode", + "required": false, + "added": "2.000", + "type": "int", + "start": "0x7C", + "length": 6, + "usage": "inventory", + "examples": [12345543210], + "description": "The barcode number for the spool. This can be UPC12, UPC13, GS1/GTIN. (Recommended: UPC13)" + }, + { + "name": "Manufacture Date", + "id": "mfg_date", + "required": false, + "added": "1.000", + "unit": "YYYY,MM,DD", + "type": "date", + "start": "0x84", + "length": 4, + "usage": "inventory", + "examples": [[2024, 1, 23]], + "description": "Stored as 2 bytes for year, then 1 byte for month and 1 byte for day." + }, + { + "name": "Manufacture Time", + "id": "mfg_time", + "required": false, + "added": "1.000", + "unit": "UTC hh:mm:ss", + "type": "time", + "start": "0x88", + "length": 3, + "usage": "inventory", + "examples": [[10, 30, 45]], + "description": "Stored as 1 byte each for hour, minute, and second in 24-hour UTC." + }, + { + "name": "Filament Diameter", + "id": "diameter", + "required": true, + "added": "1.000", + "unit": "mm", + "type": "int", + "scaling": 0.001, + "start": "0x8C", + "length": 2, + "usage": "operational", + "examples": [1750, 2850], + "description": "Filament (target) diameter in µm (micrometers). Eg `1750` → `1.750mm`." + }, + { + "name": "Measured Tolerance", + "id": "tolerance", + "required": false, + "added": "1.000", + "unit": "mm", + "type": "int", + "scaling": 0.01, + "start": "0x8E", + "length": 1, + "usage": "operational", + "examples": [2, 10], + "description": "Measured tolerance in millimeters with two implied decimal points. Eg `1` → `±0.01mm` tolerance" + }, + { + "name": "Minimum Nozzle Diameter", + "id": "nozzle_diameter", + "required": false, + "added": "2.000", + "unit": "mm", + "type": "int", + "scaling": 0.1, + "start": "0x8F", + "length": 1, + "usage": "operational", + "examples": [2, 4, 6], + "description": "Minimum nozzle diameter in mm (millimeters) with an implied decimal point. Eg `2` → `0.2mm`" + }, + { + "name": "Target Print Temperature", + "id": "print_temp", + "required": true, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x90", + "length": 1, + "usage": "operational", + "examples": [42], + "description": "Recommended print temperature in degrees Celsius, divided by 5. For example, `42` = `210°C`." + }, + { + "name": "Minimum Print Temperature", + "id": "min_print_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x91", + "length": 1, + "usage": "operational", + "examples": [38], + "description": "Minimum nozzle temperature, divided by 5. For example, `38` = `190ºC`." + }, + { + "name": "Maximum Print Temperature", + "id": "max_print_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x92", + "length": 1, + "usage": "operational", + "examples": [45], + "description": "Maximum nozzle temperature, divided by 5." + }, + { + "name": "Target Chamber Temperature", + "id": "chamber_temp", + "required": true, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x93", + "length": 1, + "usage": "operational", + "examples": [0, 12, 16], + "description": "Recommended chamber temperature in degrees Celsius, divided by 5. For example, `12` = `60°C`. If the chamber does not need to be heated, set it to `0`." + }, + { + "name": "Target Bed Temperature", + "id": "bed_temp", + "required": true, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x94", + "length": 1, + "usage": "operational", + "examples": [12, 16], + "description": "Recommended bed temperature in degrees Celsius, divided by 5. For example, `12` = `60°C`." + }, + { + "name": "Minimum Bed Temperature", + "id": "min_bed_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x95", + "length": 1, + "usage": "operational", + "examples": [8], + "description": "Minimum bed temperature, divided by 5. For example, `8` = `40ºC`." + }, + { + "name": "Maximum Bed Temperature", + "id": "max_bed_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x96", + "length": 1, + "usage": "operational", + "examples": [12], + "description": "Maximum bed temperature, divided by 5." + }, + { + "name": "Target Volumetric Speed", + "id": "target_vso", + "required": false, + "added": "1.000", + "unit": "mm³/s", + "type": "int", + "start": "0x97", + "length": 1, + "usage": "operational", + "examples": [80], + "description": "Default recommended speed." + }, + { + "name": "Minimum Volumetric Speed", + "id": "min_vso", + "required": false, + "added": "1.000", + "unit": "mm³/s", + "type": "int", + "start": "0x98", + "length": 1, + "usage": "operational", + "examples": [20], + "description": "Mininum speed recommendation." + }, + { + "name": "Maximum Volumetric Speed", + "id": "max_vso", + "required": false, + "added": "1.000", + "unit": "mm³/s", + "type": "int", + "start": "0x99", + "length": 1, + "usage": "operational", + "examples": [120], + "description": "Maximum safe speed." + }, + { + "name": "Maximum Dry Temperature", + "id": "max_dry_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0x9A", + "length": 1, + "usage": "operational", + "examples": [10, 11], + "description": "Maximum safe drying temperature, divided by 5." + }, + { + "name": "Dry Time", + "id": "dry_time", + "required": false, + "added": "1.000", + "unit": "hr", + "type": "int", + "start": "0x9B", + "length": 1, + "usage": "operational", + "examples": [4, 8, 12], + "description": "Recommended drying time." + }, + { + "name": "Density", + "id": "density", + "required": true, + "added": "1.000", + "unit": "g/cm³", + "type": "int", + "scaling": 0.001, + "start": "0x9C", + "length": 2, + "usage": "operational", + "examples": [1240, 3900], + "description": "Filament density in µg (micrograms) per cubic centimeter. Eg `1240` → `1.240g/cm³`." + }, + { + "name": "Target Weight", + "id": "weight", + "required": true, + "added": "1.000", + "unit": "g", + "type": "int", + "start": "0x9E", + "length": 2, + "usage": "operational", + "examples": [1000, 5000, 750], + "description": "Filament weight in grams, excluding spool weight. This is the TARGET weight (e.g., 1kg). Actual measured weight is stored in a different field." + }, + { + "name": "Empty Spool Weight", + "id": "empty_spool_weight", + "required": false, + "added": "1.000", + "unit": "g", + "type": "int", + "start": "0xA0", + "length": 2, + "usage": "operational", + "examples": [105], + "description": "Weight of empty spool in grams." + }, + { + "name": "Measured Filament Length", + "id": "measured_length", + "required": false, + "added": "1.000", + "unit": "m", + "type": "int", + "start": "0xA2", + "length": 2, + "usage": "operational", + "examples": [336], + "description": "The measured length of the filament, in meters. This is the actual filament length measured during production of the filament (not a realtime value updated during printing)." + }, + { + "name": "Measured Filament Weight", + "id": "measured_weight", + "required": false, + "added": "1.000", + "unit": "g", + "type": "int", + "start": "0xA4", + "length": 2, + "usage": "operational", + "examples": [1002], + "description": "The measured weight of the entire roll of filament, in grams, including the filament and the spool. This is the actual measurement taken right after production of a spool is completed (not a realtime value updated during printing)." + }, + { + "name": "Spool Core Diameter", + "id": "spool_core_diameter", + "required": false, + "added": "1.000", + "unit": "mm", + "type": "int", + "start": "0xA6", + "length": 1, + "usage": "operational", + "examples": [100, 80], + "description": "Core diameter in mm (millimeters)." + }, + { + "name": "Transmission Distance (TD)", + "id": "td", + "required": false, + "added": "1.000", + "unit": "mm", + "type": "int", + "scaling": 0.1, + "start": "0xA7", + "length": 1, + "usage": "operational", + "examples": [118], + "description": "Opaque thickness in tens of millimeters Eg. `118` → `11.8`." + }, + { + "name": "MFI Temp", + "id": "mfi_temp", + "required": false, + "added": "1.000", + "unit": "ºC", + "type": "int", + "scaling": 5, + "start": "0xA8", + "length": 1, + "usage": "operational", + "examples": [42], + "description": "MFI test temperature, divided by 5. For example, `42` = `210ºC`." + }, + { + "name": "MFI Load (Deprecated Reservation)", + "id": "deprecated_mfi_load", + "required": false, + "added": "1.000", + "unit": "kg", + "type": "int", + "scaling": 0.01, + "start": "0xA9", + "length": 1, + "usage": "operational", + "examples": [216], + "description": "**This version of the field is deprecated; for new tags, please use the new field at 0xAB. If both fields are specified, prefer the new field.** MFI test load in tens of grams. For example, `216` = `2.16kg`." + }, + { + "name": "MFI Value", + "id": "mfi_value", + "required": false, + "added": "1.000", + "unit": "g/min", + "type": "int", + "start": "0xAA", + "length": 1, + "usage": "operational", + "examples": [63], + "description": "MFI value." + }, + { + "name": "MFI Load", + "id": "mfi_load", + "required": false, + "added": "2.004", + "unit": "kg", + "type": "int", + "scaling": 0.001, + "start": "0xAB", + "length": 2, + "usage": "operational", + "examples": [2160], + "description": "MFI test load in grams. For example, `2160` = `2.16kg`, `10000` = `10kg`, `325` = `0.325kg`." + }, + { + "name": "Online Data URL", + "id": "data_url", + "required": false, + "added": "1.000", + "type": "ascii", + "start": "0xB8", + "length": 32, + "usage": "operational", + "examples": ["pfil.us?i=8078-RQSR"], + "description": "URL to access online JSON additional parameters. Formatted without `https` to save space." + } + ] + }, + "web_api": { + "fields": [ + { + "name": "OpenTag3D Version", + "id": "opentag_version", + "required": true, + "added": "1.000", + "type": "string", + "examples": ["2.001"], + "description": "Must be set as the current OpenTag3D version the API has been updated to support. This field is the only required web API field, if a web API is implemented." + }, + { + "name": "Has UI", + "id": "has_ui", + "required": false, + "added": "1.001", + "type": "bool", + "examples": [true], + "description": "A boolean field to indicate whether the API URL has a user-friendly UI available. This lets implementers know that they can see a friendly webpage if they access the API URL without using the `application/json` header." + }, + { + "name": "Tag Data", + "id": "tag_data", + "required": false, + "added": "2.002", + "type": "object{string, any}", + "examples": [ + { + "tag_version": "2.001", + "material": "PLA", + "manufacturer": "Example Brand", + "color_name": "Marionette Brown" + } + ], + "description": "A copy of the properties that populate the tag data. This can be used to write new tags or update existing tags as needed. **This property, along with any other web API properties, will NEVER be relied upon for printer functionality.**" + }, + { + "name": "Price", + "id": "price", + "required": false, + "added": "1.000", + "type": "object", + "examples": [ + { + "us": "$15.99", + "eu": "€14.99", + "uk": "£16.99", + "global": "$15.99" + } + ], + "description": "The current prices for the material and color, separated by country or region. Each country or region should be represented by its two-letter ISO 3166-1 code, including any exceptional reservations such as EU for European Union. A `global` area may be defined as well." + }, + { + "name": "Product URL", + "id": "product_url", + "required": false, + "added": "1.000", + "type": "object{string, array[string]}", + "examples": [ + { + "us": [ + "https://www.amazon.com/dp/*", + "https://example.com/filament-brand-website" + ], + "eu": ["https://example.com"] + } + ], + "description": "Links to product pages where the user can repurchase the filament, separated by country or region. The representations of countries/regions will be identical to that of the `price` field. For each country/region, a list of URLs may be specified in order to provide multiple places the user can buy new filament. The order of URLs may be specified however the filament maker desires. (Implementers should honor the filament maker's ordering.)" + }, + { + "name": "Product Photos", + "id": "product_photos", + "required": false, + "added": "2.002", + "type": "array[string]", + "examples": [ + [ + "https://example.com/my-example-image.png", + "https://example.com/product-demo.jpg" + ] + ], + "description": "Links to images of the product." + }, + { + "name": "QA Status", + "id": "qa_status", + "required": false, + "added": "2.003", + "type": "string", + "examples": ["Pass", "Fail"], + "description": "Useful for experimental or B-stock spools. States whether the spool meets the quality assurance guidelines of production. Can be a simple Pass/Fail, a letter grade, or any other grading system the filament manufacturer uses." + }, + { + "name": "Notes", + "id": "notes", + "required": false, + "added": "2.002", + "type": "string", + "examples": ["Lorem ipsum color sit print-it"], + "description": "Manufacturer notes, if any. This is helpful for adding notes to experimental, pre-production or other special spools." + } + ] + } +} diff --git a/lib/opentag3d/opentag3d_lib.c b/lib/opentag3d/opentag3d_lib.c index 8132bfa0..50740be7 100644 --- a/lib/opentag3d/opentag3d_lib.c +++ b/lib/opentag3d/opentag3d_lib.c @@ -92,6 +92,15 @@ static void read_u8_as_u16_field(const uint8_t *payload, size_t payload_len, } } +/* Decode a one-byte field stored in 10 g steps into grams, provided the byte + * lies inside the declared NDEF payload. Example: 216 becomes 2160 g. */ +static void read_u8_x10_field(const uint8_t *payload, size_t payload_len, + size_t offset, uint16_t *dst) { + if (available_field_bytes(payload_len, offset, sizeof(uint8_t)) == sizeof(uint8_t)) { + *dst = (uint16_t)(payload[offset] * 10); + } +} + /* Decode a six-byte, big-endian numeric field only when the complete field is * inside the declared NDEF payload. Example: the v2 six-byte barcode field * can hold GTIN value 12345543210; a truncated barcode remains zero. */ @@ -155,7 +164,10 @@ static void opentag3d_decode_v2_fields(const uint8_t *payload, size_t payload_le read_u8_field(payload, payload_len, OT3D_V2_OFF_SPOOL_CORE_DIAMETER, &out->spool_core_diameter_mm); read_u8_as_u16_field(payload, payload_len, OT3D_V2_OFF_TD, &out->transmission_distance); read_u8_field(payload, payload_len, OT3D_V2_OFF_MFI_TEMP, &out->mfi_temp_encoded); - read_u8_field(payload, payload_len, OT3D_V2_OFF_MFI_LOAD, &out->mfi_load); + read_u16_field(payload, payload_len, OT3D_V2_OFF_MFI_LOAD, &out->mfi_load_g); + if (out->mfi_load_g == 0) { + read_u8_x10_field(payload, payload_len, OT3D_V2_OFF_DEPRECATED_MFI_LOAD, &out->mfi_load_g); + } read_u8_field(payload, payload_len, OT3D_V2_OFF_MFI_VALUE, &out->mfi_value); read_str_field(payload, payload_len, OT3D_V2_OFF_DATA_URL, OT3D_V2_LEN_DATA_URL, out->online_url, sizeof(out->online_url)); } @@ -243,7 +255,7 @@ opentag3d_result_t opentag3d_decode(const uint8_t *payload, size_t len, opentag3 read_u8_field(payload, len, OT3D_V1_OFF_SPOOL_CORE_DIAMETER, &out->spool_core_diameter_mm); read_u8_field(payload, len, OT3D_V1_OFF_MFI_TEMP, &out->mfi_temp_encoded); - read_u8_field(payload, len, OT3D_V1_OFF_MFI_LOAD, &out->mfi_load); + read_u8_x10_field(payload, len, OT3D_V1_OFF_MFI_LOAD, &out->mfi_load_g); read_u8_field(payload, len, OT3D_V1_OFF_MFI_VALUE, &out->mfi_value); read_u8_field(payload, len, OT3D_V1_OFF_TOLERANCE, &out->measured_tolerance_um); read_u16_field(payload, len, OT3D_V1_OFF_EMPTY_SPOOL_WEIGHT, &out->empty_spool_weight_g); @@ -268,6 +280,11 @@ static void write_u16(uint8_t *p, uint16_t val) { p[1] = (uint8_t)(val & 0xFF); } +/* Grams to the one-byte 10 g-step encoding, clamped to 255 (2550 g). */ +static uint8_t ot3d_mfi_load_ticks(uint16_t grams) { + return grams / 10 > 255 ? 255 : (uint8_t)(grams / 10); +} + /* Write fixed-length field, pad with spaces */ static void write_str(const char *src, uint8_t *dst, size_t field_len) { size_t slen = strlen(src); @@ -337,7 +354,7 @@ static int opentag3d_encode_v2(const opentag3d_t *tag, uint8_t *buf, size_t bufl * spec caps TD at 25.0mm (250), so clamp to avoid silent byte truncation. */ buf[OT3D_V2_OFF_TD] = (uint8_t)(tag->transmission_distance > 250 ? 250 : tag->transmission_distance); buf[OT3D_V2_OFF_MFI_TEMP] = tag->mfi_temp_encoded; - buf[OT3D_V2_OFF_MFI_LOAD] = tag->mfi_load; + write_u16(buf + OT3D_V2_OFF_MFI_LOAD, tag->mfi_load_g); /* deprecated 0xA9 stays 0 from the memset */ buf[OT3D_V2_OFF_MFI_VALUE] = tag->mfi_value; write_str(tag->online_url, buf + OT3D_V2_OFF_DATA_URL, OT3D_V2_LEN_DATA_URL); @@ -389,7 +406,7 @@ static int opentag3d_encode_v1(const opentag3d_t *tag, uint8_t *buf, size_t bufl buf[OT3D_V1_OFF_SPOOL_CORE_DIAMETER] = tag->spool_core_diameter_mm; buf[OT3D_V1_OFF_MFI_TEMP] = tag->mfi_temp_encoded; - buf[OT3D_V1_OFF_MFI_LOAD] = tag->mfi_load; + buf[OT3D_V1_OFF_MFI_LOAD] = ot3d_mfi_load_ticks(tag->mfi_load_g); buf[OT3D_V1_OFF_MFI_VALUE] = tag->mfi_value; buf[OT3D_V1_OFF_TOLERANCE] = tag->measured_tolerance_um; write_u16(buf + OT3D_V1_OFF_EMPTY_SPOOL_WEIGHT, tag->empty_spool_weight_g); @@ -442,7 +459,7 @@ static void ot3d_norm_str(const char *s, size_t field_len, char *dst) { dst[n] = '\0'; } -typedef enum { OT3D_F_STR, OT3D_F_U8, OT3D_F_U16, OT3D_F_U48, OT3D_F_RGBA } ot3d_field_kind_t; +typedef enum { OT3D_F_STR, OT3D_F_U8, OT3D_F_U16, OT3D_F_U48, OT3D_F_RGBA, OT3D_F_MFI_LOAD } ot3d_field_kind_t; typedef struct { uint64_t bit; @@ -499,7 +516,7 @@ static const ot3d_field_t ot3d_fields[] = { OT3D_U8 (OT3D_PATCH_MFG_SECOND, manufacture_second, OT3D_V2_OFF_MFG_TIME + 2, OT3D_V1_OFF_MFG_TIME + 2), OT3D_U8 (OT3D_PATCH_SPOOL_CORE, spool_core_diameter_mm, OT3D_V2_OFF_SPOOL_CORE_DIAMETER, OT3D_V1_OFF_SPOOL_CORE_DIAMETER), OT3D_U8 (OT3D_PATCH_MFI_TEMP, mfi_temp_encoded, OT3D_V2_OFF_MFI_TEMP, OT3D_V1_OFF_MFI_TEMP), - OT3D_U8 (OT3D_PATCH_MFI_LOAD, mfi_load, OT3D_V2_OFF_MFI_LOAD, OT3D_V1_OFF_MFI_LOAD), + { OT3D_PATCH_MFI_LOAD, OT3D_V2_OFF_MFI_LOAD, 2, OT3D_V1_OFF_MFI_LOAD, 1, OT3D_F_MFI_LOAD, OT3D_MEMOFF(mfi_load_g) }, OT3D_U8 (OT3D_PATCH_MFI_VALUE, mfi_value, OT3D_V2_OFF_MFI_VALUE, OT3D_V1_OFF_MFI_VALUE), OT3D_U8 (OT3D_PATCH_TOLERANCE, measured_tolerance_um, OT3D_V2_OFF_TOLERANCE, OT3D_V1_OFF_TOLERANCE), OT3D_U16(OT3D_PATCH_EMPTY_SPOOL_WEIGHT, empty_spool_weight_g, OT3D_V2_OFF_EMPTY_SPOOL_WEIGHT, OT3D_V1_OFF_EMPTY_SPOOL_WEIGHT), @@ -592,6 +609,20 @@ static int ot3d_field_differs(const ot3d_span_t *want, const ot3d_span_t *cur, u return 1; } return 0; + case OT3D_F_MFI_LOAD: { + uint16_t w, c; + memcpy(&w, want->val, sizeof(w)); + memcpy(&c, cur->val, sizeof(c)); + if (want->len == 1) { /* v1: one byte in 10 g steps */ + uint8_t wt = ot3d_mfi_load_ticks(w); + if (wt == ot3d_mfi_load_ticks(c)) return 0; + if (out_bytes) out_bytes[0] = wt; + return 1; + } + if (w == c) return 0; + if (out_bytes) write_u16(out_bytes, w); + return 1; + } } return -1; } @@ -671,6 +702,13 @@ opentag3d_result_t opentag3d_patch_payload(uint8_t *raw, size_t *len, size_t cap if (ot3d_field_differs(&want, &cur, bytes) != 1) continue; memcpy(raw + want.off, bytes, want.len); } + /* A v2 clear must also zero the deprecated byte or decode falls back to + * it. A nonzero baseline means the field differed, so required_end + * already covers 0xA9. */ + if (is_v2 && (patch->present & OT3D_PATCH_MFI_LOAD) && + patch->values.mfi_load_g == 0 && baseline.mfi_load_g != 0) { + raw[OT3D_V2_OFF_DEPRECATED_MFI_LOAD] = 0; + } *len = required_end; return OT3D_OK; } diff --git a/lib/opentag3d/opentag3d_lib.h b/lib/opentag3d/opentag3d_lib.h index f3dff63c..14ed5f4c 100644 --- a/lib/opentag3d/opentag3d_lib.h +++ b/lib/opentag3d/opentag3d_lib.h @@ -12,7 +12,7 @@ extern "C" { #define OT3D_SUPPORTED_MAJOR 2 #define OT3D_SUPPORTED_MINOR 0 #define OT3D_SUPPORTED_V1 1003 /* newest v1.x we can fully parse */ -#define OT3D_SUPPORTED_V2 2003 /* newest v2.x we can fully parse */ +#define OT3D_SUPPORTED_V2 2004 /* newest v2.x we can fully parse */ #define OT3D_SUPPORTED_VERSION OT3D_SUPPORTED_V2 /* NDEF MIME type for detection */ @@ -67,7 +67,7 @@ typedef struct { uint8_t manufacture_second; uint8_t spool_core_diameter_mm; uint8_t mfi_temp_encoded; /* °C ÷ 5 */ - uint8_t mfi_load; /* g ÷ 10 */ + uint16_t mfi_load_g; /* grams; v1 and the deprecated v2 byte store g ÷ 10 */ uint8_t mfi_value; /* g/10min ÷ 10 */ uint8_t measured_tolerance_um; uint16_t empty_spool_weight_g; diff --git a/lib/opentag3d/opentag3d_v2_map.h b/lib/opentag3d/opentag3d_v2_map.h index 159908f0..67c1207c 100644 --- a/lib/opentag3d/opentag3d_v2_map.h +++ b/lib/opentag3d/opentag3d_v2_map.h @@ -1,7 +1,7 @@ #ifndef OPENTAG3D_V2_MAP_H #define OPENTAG3D_V2_MAP_H -/* OpenTag3D spec v2.000 core memory map, addresses 0x00..0xDF. +/* OpenTag3D spec v2.004 core memory map, addresses 0x00..0xDF. * Offsets and lengths derived from opentag3d-spec-v2.json. * Trailing comments: type, unit, scaling, required (as per spec). */ @@ -86,10 +86,12 @@ #define OT3D_V2_LEN_TD 1 #define OT3D_V2_OFF_MFI_TEMP 0xA8 /* int, unit=C, scaling=5 */ #define OT3D_V2_LEN_MFI_TEMP 1 -#define OT3D_V2_OFF_MFI_LOAD 0xA9 /* int, unit=g, scaling=10 */ -#define OT3D_V2_LEN_MFI_LOAD 1 +#define OT3D_V2_OFF_DEPRECATED_MFI_LOAD 0xA9 /* int, unit=kg, scaling=0.01; deprecated in 2.004, fallback only */ +#define OT3D_V2_LEN_DEPRECATED_MFI_LOAD 1 #define OT3D_V2_OFF_MFI_VALUE 0xAA /* int, unit=g/min */ #define OT3D_V2_LEN_MFI_VALUE 1 +#define OT3D_V2_OFF_MFI_LOAD 0xAB /* int, unit=kg, scaling=0.001 (raw == grams), added 2.004 */ +#define OT3D_V2_LEN_MFI_LOAD 2 #define OT3D_V2_OFF_DATA_URL 0xB8 /* ascii */ #define OT3D_V2_LEN_DATA_URL 32 diff --git a/test/native/test_opentag3d_runner.c b/test/native/test_opentag3d_runner.c index bd47a8de..2da4adcc 100644 --- a/test/native/test_opentag3d_runner.c +++ b/test/native/test_opentag3d_runner.c @@ -83,7 +83,7 @@ static void build_v2_nominal(uint8_t *buf) { buf[OT3D_V2_OFF_SPOOL_CORE_DIAMETER] = 100; buf[OT3D_V2_OFF_TD] = 118; buf[OT3D_V2_OFF_MFI_TEMP] = 210; - buf[OT3D_V2_OFF_MFI_LOAD] = 216; + put_u16(buf + OT3D_V2_OFF_MFI_LOAD, 2160); buf[OT3D_V2_OFF_MFI_VALUE] = 63; put_str(buf + OT3D_V2_OFF_DATA_URL, "pfil.us?i=8078-RQSR", OT3D_V2_LEN_DATA_URL); } @@ -154,7 +154,7 @@ static void check_v2_fields(const opentag3d_t *o) { CHECK(o->measured_filament_weight_g == 1002, "v2: measured weight"); CHECK(o->spool_core_diameter_mm == 100, "v2: spool core diameter"); CHECK(o->transmission_distance == 118, "v2: transmission distance (1 byte)"); - CHECK(o->mfi_temp_encoded == 210 && o->mfi_load == 216 && o->mfi_value == 63, "v2: mfi triple"); + CHECK(o->mfi_temp_encoded == 210 && o->mfi_load_g == 2160 && o->mfi_value == 63, "v2: mfi triple"); CHECK(strcmp(o->online_url, "pfil.us?i=8078-RQSR") == 0, "v2: online_url"); } @@ -230,6 +230,8 @@ int main(void) { CHECK(out.print_temp_encoded == 42 && out.bed_temp_encoded == 16, "E: temps"); CHECK(out.density_ugcm3 == 1240, "E: density"); CHECK(out.transmission_distance == 118, "E: transmission distance (u16)"); + CHECK(out.mfi_load_g == 2160 && out.measured_tolerance_um == 10, + "E: v1 mfi load byte 216 decodes as 2160 g, tolerance at 0xAB intact"); CHECK(out.manufacture_year == 2024 && out.manufacture_month == 1 && out.manufacture_day == 23, "E: date"); CHECK(strcmp(out.serial_number, "SN-V1") == 0, "E: serial (16B field)"); CHECK(strcmp(out.online_url, "pfil.us?i=8078-RQSR") == 0, "E: online_url"); @@ -353,7 +355,7 @@ int main(void) { src.measured_filament_length_m = 400; src.measured_filament_weight_g = 810; src.spool_core_diameter_mm = 55; src.transmission_distance = 118; - src.mfi_temp_encoded = 200; src.mfi_load = 210; src.mfi_value = 60; + src.mfi_temp_encoded = 200; src.mfi_load_g = 2165; src.mfi_value = 60; strcpy(src.online_url, "pfil.us/roundtrip"); n = opentag3d_encode(&src, buf, OT3D_V2_MAP_SIZE); @@ -393,7 +395,7 @@ int main(void) { CHECK(out.measured_filament_weight_g == 810, "K: measured weight"); CHECK(out.spool_core_diameter_mm == 55, "K: spool core diameter"); CHECK(out.transmission_distance == 118, "K: transmission distance"); - CHECK(out.mfi_temp_encoded == 200 && out.mfi_load == 210 && out.mfi_value == 60, "K: mfi triple"); + CHECK(out.mfi_temp_encoded == 200 && out.mfi_load_g == 2165 && out.mfi_value == 60, "K: mfi triple (gram precision)"); CHECK(strcmp(out.online_url, "pfil.us/roundtrip") == 0, "K: online_url"); /* L: raw byte spot-checks on the K-encoded buffer — guards against a @@ -411,6 +413,8 @@ int main(void) { static const uint8_t want_bc[6] = {0x00, 0x02, 0xDF, 0xDA, 0x0A, 0x2A}; CHECK(memcmp(buf + OT3D_V2_OFF_BARCODE, want_bc, 6) == 0, "L: barcode bytes == BE(12345543210)"); } + CHECK(buf[0xAB] == 0x08 && buf[0xAC] == 0x75, "L: mfi load bytes {08 75} (2165 g) at 0xAB"); + CHECK(buf[0xA9] == 0x00, "L: deprecated mfi load byte 0xA9 left at 0"); /* M: version preservation (deduction write-back keeps a tag's layout). */ printf("[M] version preservation\n"); @@ -426,6 +430,8 @@ int main(void) { out2.transmission_distance == 118 && out2.density_ugcm3 == 1240, "M: v1 numerics survive"); CHECK(strcmp(out2.serial_number, "SN-V1") == 0 && strcmp(out2.online_url, "pfil.us?i=8078-RQSR") == 0, "M: v1 strings survive"); CHECK(out2.measured_filament_length_m == 336 && out2.measured_filament_weight_g == 1002, "M: v1 extended numerics survive"); + CHECK(out2.mfi_load_g == 2160 && buf[0xA9] == 216 && buf[0xAB] == 10, + "M: v1 mfi load re-encodes as byte 216, tolerance byte kept"); build_v2_nominal(buf); r = opentag3d_decode(buf, OT3D_V2_MAP_SIZE, &out); @@ -462,9 +468,13 @@ int main(void) { * 2000 must still encode (regression guard). */ printf("[P] minor-ahead encode refusal\n"); memset(&src, 0, sizeof(src)); + src.tag_version = 2005; + n = opentag3d_encode(&src, buf, OT3D_V2_MAP_SIZE); + CHECK(n == -1, "P: encode refuses v2 minor ahead (2005)"); + memset(&src, 0, sizeof(src)); src.tag_version = 2004; n = opentag3d_encode(&src, buf, OT3D_V2_MAP_SIZE); - CHECK(n == -1, "P: encode refuses v2 minor ahead (2004)"); + CHECK(n == OT3D_V2_MAP_SIZE, "P: v2 2004 encodes (current ceiling)"); memset(&src, 0, sizeof(src)); src.tag_version = 1004; n = opentag3d_encode(&src, buf, OT3D_V2_MAP_SIZE); @@ -478,12 +488,12 @@ int main(void) { printf("[Q] version predicate helpers\n"); CHECK(opentag3d_major(2000) == 2 && opentag3d_major(1999) == 1 && opentag3d_major(999) == 0, "Q: opentag3d_major splits correctly"); - CHECK(OT3D_SUPPORTED_V1 == 1003 && OT3D_SUPPORTED_V2 == 2003, + CHECK(OT3D_SUPPORTED_V1 == 1003 && OT3D_SUPPORTED_V2 == 2004, "Q: supported ceilings pinned — bumping them must update these tests"); CHECK(opentag3d_can_encode(1000) && opentag3d_can_encode(1003) && - opentag3d_can_encode(2000) && opentag3d_can_encode(2003), + opentag3d_can_encode(2000) && opentag3d_can_encode(2004), "Q: can_encode accepts supported versions"); - CHECK(!opentag3d_can_encode(1004) && !opentag3d_can_encode(2004) && + CHECK(!opentag3d_can_encode(1004) && !opentag3d_can_encode(2005) && !opentag3d_can_encode(3000), "Q: can_encode refuses minor-ahead and future majors"); CHECK(opentag3d_can_encode(0), "Q: can_encode accepts legacy version 0 (v1 path)"); @@ -529,8 +539,9 @@ int main(void) { CHECK(OT3D_V2_OFF_SPOOL_CORE_DIAMETER == 0xA6 && OT3D_V2_LEN_SPOOL_CORE_DIAMETER == 1, "R: spool_core_diameter @0xA6/1"); CHECK(OT3D_V2_OFF_TD == 0xA7 && OT3D_V2_LEN_TD == 1, "R: td @0xA7/1"); CHECK(OT3D_V2_OFF_MFI_TEMP == 0xA8 && OT3D_V2_LEN_MFI_TEMP == 1, "R: mfi_temp @0xA8/1"); - CHECK(OT3D_V2_OFF_MFI_LOAD == 0xA9 && OT3D_V2_LEN_MFI_LOAD == 1, "R: mfi_load @0xA9/1"); + CHECK(OT3D_V2_OFF_DEPRECATED_MFI_LOAD == 0xA9 && OT3D_V2_LEN_DEPRECATED_MFI_LOAD == 1, "R: deprecated_mfi_load @0xA9/1"); CHECK(OT3D_V2_OFF_MFI_VALUE == 0xAA && OT3D_V2_LEN_MFI_VALUE == 1, "R: mfi_value @0xAA/1"); + CHECK(OT3D_V2_OFF_MFI_LOAD == 0xAB && OT3D_V2_LEN_MFI_LOAD == 2, "R: mfi_load @0xAB/2 (2.004)"); CHECK(OT3D_V2_OFF_DATA_URL == 0xB8 && OT3D_V2_LEN_DATA_URL == 32, "R: data_url @0xB8/32"); CHECK(OT3D_V2_MAP_SIZE == 0xE0 && OT3D_V2_VERSION == 2000, "R: map size + version constants"); @@ -551,9 +562,11 @@ int main(void) { put_u16(raw + OT3D_V2_OFF_TAG_VERSION, 2000); put_u16(raw + OT3D_V2_OFF_MEASURED_WEIGHT, 1002); put_u16(raw + OT3D_V2_OFF_WEIGHT, 1000); + put_u16(raw + OT3D_V2_OFF_MFI_LOAD, 2160); /* Sentinel bytes where the encoder would memset: reserved gaps and * the whole tail past the canonical 224-byte map. */ - memset(raw + OT3D_V2_OFF_MFI_VALUE + 1, 0xEE, OT3D_V2_OFF_DATA_URL - (OT3D_V2_OFF_MFI_VALUE + 1)); /* 0xAB..0xB7 reserved run */ + memset(raw + OT3D_V2_OFF_MFI_LOAD + OT3D_V2_LEN_MFI_LOAD, 0xEE, + OT3D_V2_OFF_DATA_URL - (OT3D_V2_OFF_MFI_LOAD + OT3D_V2_LEN_MFI_LOAD)); /* 0xAD..0xB7 reserved run */ memset(raw + OT3D_V2_MIN_SIZE, 0xEE, OT3D_MAX_PAYLOAD_SIZE - OT3D_V2_MIN_SIZE); memset(&p, 0, sizeof(p)); @@ -565,13 +578,13 @@ int main(void) { CHECK(read_be16(raw + OT3D_V2_OFF_MEASURED_WEIGHT) == 834, "S: measured weight patched to 834"); opentag3d_t chk; r = opentag3d_decode(raw, len, &chk); - CHECK(r == OT3D_OK && chk.measured_filament_weight_g == 834 && chk.target_weight_g == 1000, - "S: decode sees new weight, target weight untouched"); + CHECK(r == OT3D_OK && chk.measured_filament_weight_g == 834 && chk.target_weight_g == 1000 && + chk.mfi_load_g == 2160, "S: decode sees new weight, target weight and mfi load untouched"); int tail_ok = 1, res_ok = 1; for (size_t i = OT3D_V2_MIN_SIZE; i < OT3D_MAX_PAYLOAD_SIZE; i++) if (raw[i] != 0xEE) tail_ok = 0; - for (size_t i = 0xAB; i < OT3D_V2_OFF_DATA_URL; i++) if (raw[i] != 0xEE) res_ok = 0; + for (size_t i = 0xAD; i < OT3D_V2_OFF_DATA_URL; i++) if (raw[i] != 0xEE) res_ok = 0; CHECK(tail_ok, "S: all 284 sentinel bytes past 224 survive verbatim"); - CHECK(res_ok, "S: reserved 0xAB..0xB7 sentinel run survives verbatim"); + CHECK(res_ok, "S: reserved 0xAD..0xB7 sentinel run survives verbatim"); } /* T: v1 187-byte record with reserved-gap and trailing sentinels — a @@ -854,6 +867,161 @@ int main(void) { "AB: only the selected 4-byte RGBA range changed"); } + /* AC: MFI load decode — the 2.004 field at 0xAB wins; a zero new field + * falls back to the deprecated 10 g-step byte at 0xA9. v1 never reads 0xAB. */ + printf("[AC] mfi load decode + deprecated fallback\n"); + { + uint8_t raw[OT3D_V2_MAP_SIZE]; + opentag3d_t chk; + + build_v2_nominal(raw); + raw[0xA9] = 216; raw[0xAB] = 0x0B; raw[0xAC] = 0xB8; + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 3000, "AC: both set -> new field wins (3000 g)"); + + raw[0xAB] = 0; raw[0xAC] = 0; + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2160, "AC: new zero -> deprecated 216 read as 2160 g"); + + raw[0xA9] = 255; + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2550, "AC: deprecated 255 -> 2550 g"); + + raw[0xA9] = 0; + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, "AC: both zero -> 0"); + + raw[0xAB] = 0x00; raw[0xAC] = 0x01; + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 1, "AC: new field 1 g, big-endian, unscaled"); + + raw[0xA9] = 200; raw[0xAB] = 0x08; raw[0xAC] = 0x70; + r = opentag3d_decode(raw, 0xAC, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2000, "AC: record cut inside 0xAB..0xAC falls back to 0xA9"); + r = opentag3d_decode(raw, 0xAA, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2000, "AC: record ending after 0xA9 reads the deprecated byte"); + r = opentag3d_decode(raw, 0xA9, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, "AC: record ending before 0xA9 has no mfi load"); + + uint8_t v1raw[OT3D_EXTENDED_MIN]; + build_v1_nominal(v1raw, 1000); + v1raw[0xA9] = 0; + r = opentag3d_decode(v1raw, OT3D_EXTENDED_MIN, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0 && chk.measured_tolerance_um == 10, + "AC: v1 never reads tolerance at 0xAB as mfi load"); + } + + /* AD: MFI load encode — v2 two bytes at 0xAB with 0xA9 zero; v1 one byte + * in 10 g steps, clamped. */ + printf("[AD] mfi load encode\n"); + { + opentag3d_t enc; + uint8_t ebuf[OT3D_V2_MAP_SIZE]; + + memset(&enc, 0, sizeof(enc)); + enc.tag_version = 2004; + enc.mfi_load_g = 65535; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_V2_MAP_SIZE && ebuf[0xAB] == 0xFF && ebuf[0xAC] == 0xFF && ebuf[0xA9] == 0, + "AD: v2 65535 g -> {FF FF} at 0xAB, 0xA9 zero"); + + memset(&enc, 0, sizeof(enc)); + enc.tag_version = 1000; + enc.mfi_load_g = 2169; + n = opentag3d_encode(&enc, ebuf, OT3D_EXTENDED_MIN); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 216 && ebuf[0xAA] == 0 && ebuf[0xAB] == 0, + "AD: v1 2169 g -> byte 216, neighbours untouched"); + enc.mfi_load_g = 9999; + n = opentag3d_encode(&enc, ebuf, OT3D_EXTENDED_MIN); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 255, "AD: v1 9999 g clamps to byte 255"); + } + + /* AE: MFI load patch — v2 writes only 0xAB..0xAC (0xA9 verbatim) except a + * clear, which also zeroes 0xA9; v1 writes one 10 g-step byte at 0xA9. */ + printf("[AE] mfi load patch\n"); + { + uint8_t raw[OT3D_MAX_PAYLOAD_SIZE]; + uint8_t snapshot[OT3D_MAX_PAYLOAD_SIZE]; + size_t len; + opentag3d_patch_t p; + opentag3d_t chk; + int isolated; + + memset(raw, 0, sizeof(raw)); + build_v2_nominal(raw); + raw[0xA9] = 216; + len = OT3D_V2_MAP_SIZE; + memcpy(snapshot, raw, sizeof(raw)); + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD; + p.values.mfi_load_g = 3000; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + isolated = 1; + for (size_t i = 0; i < OT3D_V2_MAP_SIZE; i++) { + if (i == 0xAB || i == 0xAC) continue; + if (raw[i] != snapshot[i]) isolated = 0; + } + CHECK(r == OT3D_OK && len == OT3D_V2_MAP_SIZE && raw[0xAB] == 0x0B && raw[0xAC] == 0xB8 && isolated, + "AE: v2 patch rewrites only 0xAB..0xAC, deprecated 0xA9 kept verbatim"); + r = opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 3000, "AE: patched v2 decodes 3000 g"); + + memset(raw, 0, sizeof(raw)); + build_v2_nominal(raw); + raw[0xAB] = 0; raw[0xAC] = 0; raw[0xA9] = 216; + len = OT3D_V2_MAP_SIZE; + memcpy(snapshot, raw, sizeof(raw)); + p.values.mfi_load_g = 2160; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && len == OT3D_V2_MAP_SIZE && memcmp(raw, snapshot, len) == 0, + "AE: legacy 216 == desired 2160 g is a no-op"); + + p.values.mfi_load_g = 0; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && raw[0xA9] == 0 && raw[0xAB] == 0 && raw[0xAC] == 0 && + raw[0xAA] == snapshot[0xAA] && chk.mfi_load_g == 0, + "AE: clearing mfi load zeroes 0xA9 and 0xAB..0xAC"); + + memset(raw, 0, sizeof(raw)); + build_v2_nominal(raw); + len = 0xA9; + memset(raw + len, 0x77, sizeof(raw) - len); + p.values.mfi_load_g = 2160; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && len == 0xAD && raw[0xA9] == 0 && raw[0xAA] == 0 && + raw[0xAB] == 0x08 && raw[0xAC] == 0x70, "AE: short v2 grows to 0xAD with zero-filled gap"); + + memset(raw, 0, sizeof(raw)); + build_v2_nominal(raw); + len = 0xA9; + memcpy(snapshot, raw, sizeof(raw)); + r = opentag3d_patch_payload(raw, &len, 0xAC, &p); + CHECK(r == OT3D_PARSE_ERROR && len == 0xA9 && memcmp(raw, snapshot, 0xAD) == 0, + "AE: growth past capacity rejected atomically"); + + memset(raw, 0, sizeof(raw)); + build_v1_nominal(raw, 1000); + len = OT3D_EXTENDED_MIN; + memcpy(snapshot, raw, sizeof(raw)); + p.values.mfi_load_g = 2165; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && len == OT3D_EXTENDED_MIN && memcmp(raw, snapshot, len) == 0, + "AE: v1 2165 g == stored 216 ticks, no-op"); + p.values.mfi_load_g = 2170; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + isolated = 1; + for (size_t i = 0; i < OT3D_EXTENDED_MIN; i++) { + if (i == 0xA9) continue; + if (raw[i] != snapshot[i]) isolated = 0; + } + CHECK(r == OT3D_OK && len == OT3D_EXTENDED_MIN && raw[0xA9] == 217 && + raw[0xAA] == 63 && raw[0xAB] == 10 && isolated, "AE: v1 2170 g writes only byte 217 at 0xA9"); + p.values.mfi_load_g = 9999; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && raw[0xA9] == 255, "AE: v1 9999 g clamps to 255"); + } + printf("%s: %d failure(s)\n", failures ? "FAILED" : "OK", failures); return failures ? 1 : 0; } From c2e64cd9ed39159f5fa174133e01db9fb93ffe33 Mon Sep 17 00:00:00 2001 From: Shane Jordan Date: Sun, 29 Nov 2026 23:33:07 +0000 Subject: [PATCH 2/4] test: cover OpenTag3D v2.004 MFI load boundaries and cross-stage patch cases --- test/native/Makefile | 12 +- test/native/test_opentag3d_mfi_load.c | 256 ++++++++++++++++++++++++++ 2 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 test/native/test_opentag3d_mfi_load.c diff --git a/test/native/Makefile b/test/native/Makefile index cd85ef63..0bab8f33 100644 --- a/test/native/Makefile +++ b/test/native/Makefile @@ -16,7 +16,7 @@ FWB_VARIANTS = test_firmware_build test_firmware_build_xiao test_firmware_build_ .PHONY: all clean test test_deduction_manager_compile -all: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidecar test_printer_manager test_opt_ndef_bounds test_diagnostics test_tft_landscape_layout test_tigertag_parser test_bambutag_parser test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) +all: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidecar test_printer_manager test_opt_ndef_bounds test_diagnostics test_tft_landscape_layout test_tigertag_parser test_bambutag_parser test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_opentag3d_mfi_load test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) openprinttag_lib.o: ../../lib/openprinttag/openprinttag_lib.c $(CC) $(CFLAGS) -c -o $@ $< @@ -66,6 +66,9 @@ test_spool_cache: test_spool_cache.cpp ../../src/SpoolCacheJson.h test_opentag3d_runner: test_opentag3d_runner.c $(OT3D_OBJS) $(CC) $(CFLAGS) -I../../lib/opentag3d -o $@ test_opentag3d_runner.c $(OT3D_OBJS) +test_opentag3d_mfi_load: test_opentag3d_mfi_load.c $(OT3D_OBJS) + $(CC) $(CFLAGS) -I../../lib/opentag3d -o $@ test_opentag3d_mfi_load.c $(OT3D_OBJS) + test_raw_write_slot: test_raw_write_slot.cpp ../../src/RawWriteSlot.h ../../src/RequestId.h $(CXX) $(CXXFLAGS) -pthread -o $@ test_raw_write_slot.cpp @@ -93,7 +96,7 @@ test_firmware_build_s3devkitc: $(FWB_DEPS) test_firmware_build_s3zero: $(FWB_DEPS) $(CXX) $(CXXFLAGS) -DBOARD_ESP32_S3=1 -DBOARD_S3_ZERO=1 -DEXPECT_BOARD=\"esp32s3zero\" -o $@ $(FWB_SRCS) -test: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidecar test_printer_manager test_opt_ndef_bounds test_diagnostics test_tft_landscape_layout test_tigertag_parser test_bambutag_parser test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) +test: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidecar test_printer_manager test_opt_ndef_bounds test_diagnostics test_tft_landscape_layout test_tigertag_parser test_bambutag_parser test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_opentag3d_mfi_load test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) @echo "" @echo "=== Running PrinterManager tests ===" @./test_printer_manager @@ -131,6 +134,9 @@ test: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidec @echo "=== Running OpenTag3D parser tests ===" @./test_opentag3d_runner @echo "" + @echo "=== Running OpenTag3D MFI load boundary tests ===" + @./test_opentag3d_mfi_load + @echo "" @echo "=== Running NDEF MIME builder tests ===" @./test_ndef_mime_builder @echo "" @@ -153,4 +159,4 @@ test: test_deduction_tracker test_deduction_manager_compile test_raw_write_sidec clean: rm -f test_printer_manager test_opt_ndef_bounds test_diagnostics \ test_tft_landscape_layout test_tigertag_parser test_bambutag_parser \ - test_deduction_tracker test_raw_write_sidecar test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) *.o + test_deduction_tracker test_raw_write_sidecar test_write_uid_guard test_cc_sizing test_spool_cache test_opentag3d_runner test_opentag3d_mfi_load test_ndef_mime_builder test_raw_write_slot $(FWB_VARIANTS) *.o diff --git a/test/native/test_opentag3d_mfi_load.c b/test/native/test_opentag3d_mfi_load.c new file mode 100644 index 00000000..144f6a3e --- /dev/null +++ b/test/native/test_opentag3d_mfi_load.c @@ -0,0 +1,256 @@ +/* Boundary and cross-stage tests for the OpenTag3D v2.004 MFI Load field. + * + * The v2.004 spec puts MFI Load in a 2-byte gram field at 0xAB (scaling + * 0.001 kg), deprecates the 1-byte 10 g-step field at 0xA9 (scaling + * 0.01 kg) as fallback-only, and requires the new field to win when both + * are set. v1 keeps its 1-byte 10 g-step field at 0xA9; 0xAB stays the + * diameter tolerance. Expected values here come from the spec text and + * examples (216 -> 2.16 kg, 2160 -> 2.16 kg, 325 -> 0.325 kg), not from + * the codec. Plain C, PASS/FAIL per check, non-zero exit on any failure. */ +#include +#include +#include +#include "opentag3d_lib.h" +#include "opentag3d_v1_map.h" +#include "opentag3d_v2_map.h" + +static int failures = 0; + +#define CHECK(cond, name) \ + do { \ + if (cond) { \ + printf(" PASS %s\n", name); \ + } else { \ + printf(" FAIL %s\n", name); \ + failures++; \ + } \ + } while (0) + +static void put_u16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)(v & 0xFF); } + +/* Minimal v2 record: version + the bytes the MFI decode reads. */ +static void build_v2_mfi(uint8_t *buf, uint16_t version) { + memset(buf, 0, OT3D_V2_MAP_SIZE); + put_u16(buf + OT3D_V2_OFF_TAG_VERSION, version); +} + +/* Minimal 187-byte v1 record with the mfi triple and tolerance set. */ +static void build_v1_mfi(uint8_t *buf) { + memset(buf, 0, OT3D_EXTENDED_MIN); + put_u16(buf + 0x00, 1000); + buf[OT3D_V1_OFF_MFI_LOAD] = 216; /* 2160 g */ + buf[OT3D_V1_OFF_MFI_VALUE] = 63; + buf[OT3D_V1_OFF_TOLERANCE] = 10; +} + +int main(void) { + printf("=== OpenTag3D v2.004 MFI load boundaries ===\n"); + opentag3d_t chk; + opentag3d_result_t r; + uint8_t raw[OT3D_MAX_PAYLOAD_SIZE]; + uint8_t snapshot[OT3D_MAX_PAYLOAD_SIZE]; + size_t len; + int isolated; + + /* A: literal pins the codec must agree with — v1 map unchanged by 2.004, + * and the patch presence bit unchanged by the field resize. */ + printf("[A] map + presence-bit literal pins\n"); + CHECK(OT3D_V1_OFF_MFI_LOAD == 0xA9 && OT3D_V1_LEN_MFI_LOAD == 1, + "A: v1 mfi_load stays @0xA9/1"); + CHECK(OT3D_V1_OFF_TOLERANCE == 0xAB && OT3D_V1_LEN_TOLERANCE == 1, + "A: v1 tolerance stays @0xAB/1"); + CHECK(OT3D_PATCH_MFI_LOAD == (1ULL << 25), "A: OT3D_PATCH_MFI_LOAD is bit 25"); + + /* B: v2 decode record-cut boundaries around the 2-byte field. + * len 0xAB: 0xAB itself is missing -> fallback. len 0xAD: field ends + * exactly at the record end -> complete, new field wins, no fallback. */ + printf("[B] v2 decode cut at 0xAB and exact field end 0xAD\n"); + build_v2_mfi(raw, 2000); + raw[0xA9] = 200; raw[0xAB] = 0x08; raw[0xAC] = 0x70; + r = opentag3d_decode(raw, 0xAB, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2000, + "B: record ending before 0xAB falls back to 0xA9 (200 -> 2000 g)"); + raw[0xA9] = 0; + r = opentag3d_decode(raw, 0xAB, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, "B: cut before both fields -> 0"); + raw[0xA9] = 216; raw[0xAB] = 0x0B; raw[0xAC] = 0xB8; + r = opentag3d_decode(raw, 0xAD, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 3000, + "B: field complete at exact record end 0xAD wins over 0xA9 (3000 g)"); + + /* C: spec example 325 = 0.325 kg decodes unscaled. */ + printf("[C] spec example 325 g\n"); + build_v2_mfi(raw, 2004); + put_u16(raw + OT3D_V2_OFF_MFI_LOAD, 325); + r = opentag3d_decode(raw, OT3D_V2_MAP_SIZE, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 325, "C: {01 45} at 0xAB -> 325 g"); + + /* D: v1 decode step and cut boundaries. */ + printf("[D] v1 decode step + cut boundaries\n"); + build_v1_mfi(raw); + raw[0xA9] = 255; + r = opentag3d_decode(raw, OT3D_EXTENDED_MIN, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2550, "D: v1 byte 255 -> 2550 g"); + raw[0xA9] = 1; + r = opentag3d_decode(raw, OT3D_EXTENDED_MIN, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 10, "D: v1 byte 1 -> 10 g"); + raw[0xA9] = 216; + r = opentag3d_decode(raw, 0xA9, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, "D: v1 record ending before 0xA9 -> 0"); + r = opentag3d_decode(raw, 0xAA, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2160, "D: v1 record ending at 0xA9 reads the byte"); + + /* E: v2 encode — 2004 stamps its version bytes, and the 2-byte field + * round-trips at both extremes without waking the fallback. */ + printf("[E] v2 encode stamp + round-trip extremes\n"); + { + opentag3d_t enc; + uint8_t ebuf[OT3D_V2_MAP_SIZE]; + int n; + + memset(&enc, 0, sizeof(enc)); + enc.tag_version = 2004; + enc.mfi_load_g = 65535; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_V2_MAP_SIZE && ebuf[0x00] == 0x07 && ebuf[0x01] == 0xD4, + "E: v2 2004 stamps version bytes {07 D4}"); + r = opentag3d_decode(ebuf, n, &chk); + CHECK(r == OT3D_OK && chk.tag_version == 2004 && chk.mfi_load_g == 65535, + "E: 65535 g round-trips (no fallback at 0xA9)"); + + memset(&enc, 0, sizeof(enc)); + enc.tag_version = 2004; + enc.mfi_load_g = 1; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_V2_MAP_SIZE && ebuf[0xAB] == 0x00 && ebuf[0xAC] == 0x01, + "E: 1 g encodes {00 01} big-endian at 0xAB"); + r = opentag3d_decode(ebuf, n, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 1, "E: 1 g round-trips"); + } + + /* F: v1 encode step boundaries — exact top step, truncation into a + * step, and values below one step. */ + printf("[F] v1 encode step boundaries\n"); + { + opentag3d_t enc; + uint8_t ebuf[OT3D_EXTENDED_MIN]; + int n; + + memset(&enc, 0, sizeof(enc)); + enc.tag_version = 1000; + enc.mfi_load_g = 2550; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 255, "F: v1 2550 g -> byte 255"); + r = opentag3d_decode(ebuf, n, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2550, "F: v1 2550 g round-trips"); + + enc.mfi_load_g = 2551; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 255, "F: v1 2551 g truncates to byte 255"); + + enc.mfi_load_g = 9; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 0, "F: v1 9 g truncates below one step -> 0"); + + enc.mfi_load_g = 10; + n = opentag3d_encode(&enc, ebuf, sizeof(ebuf)); + r = opentag3d_decode(ebuf, n, &chk); + CHECK(n == OT3D_EXTENDED_MIN && ebuf[0xA9] == 1 && r == OT3D_OK && chk.mfi_load_g == 10, + "F: v1 10 g -> byte 1 round-trips"); + } + + /* G: cross-stage patch cases the nominal-table tests do not reach. */ + printf("[G] mfi load patch cross-stage cases\n"); + { + opentag3d_patch_t p; + + /* G1: legacy-only v2 tag (0xAB zero, 0xA9 = 216 -> baseline 2160 g), + * patch 2165 differs from the baseline -> the new field is written + * and then wins over the untouched fallback byte on decode. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + len = OT3D_V2_MAP_SIZE; + memcpy(snapshot, raw, sizeof(raw)); + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD; + p.values.mfi_load_g = 2165; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + isolated = 1; + for (size_t i = 0; i < OT3D_V2_MAP_SIZE; i++) { + if (i == 0xAB || i == 0xAC) continue; + if (raw[i] != snapshot[i]) isolated = 0; + } + CHECK(r == OT3D_OK && len == OT3D_V2_MAP_SIZE && + raw[0xAB] == 0x08 && raw[0xAC] == 0x75 && raw[0xA9] == 216 && isolated, + "G1: legacy-only tag patched 2165 writes {08 75}, keeps 0xA9"); + r = opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2165, + "G1: patched new field wins over the fallback byte (2165 g)"); + + /* G2: clear on a tag where BOTH fields are set (decode 3000 g) — + * both ranges zero and decode returns 0. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + put_u16(raw + OT3D_V2_OFF_MFI_LOAD, 3000); + len = OT3D_V2_MAP_SIZE; + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD; + p.values.mfi_load_g = 0; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && raw[0xA9] == 0 && raw[0xAB] == 0 && raw[0xAC] == 0 && + chk.mfi_load_g == 0, "G2: clear on both-set tag zeroes 0xA9 and 0xAB..0xAC"); + + /* G3: multi-field patch — clearing mfi load alongside a measured + * weight change applies both and still zeroes the deprecated byte. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + put_u16(raw + OT3D_V2_OFF_MEASURED_WEIGHT, 1000); + len = OT3D_V2_MAP_SIZE; + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD | OT3D_PATCH_MEASURED_WEIGHT; + p.values.mfi_load_g = 0; + p.values.measured_filament_weight_g = 500; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && raw[0xA9] == 0 && chk.mfi_load_g == 0 && + chk.measured_filament_weight_g == 500, + "G3: mfi clear + weight change apply together, 0xA9 zeroed"); + + /* G4: v1 no-op window edges — stored byte 216 covers 2160..2169 g. + * 2159 must rewrite down to 215; 2169 must rewrite nothing. */ + memset(raw, 0, sizeof(raw)); + build_v1_mfi(raw); + len = OT3D_EXTENDED_MIN; + memcpy(snapshot, raw, sizeof(raw)); + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD; + p.values.mfi_load_g = 2159; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && len == OT3D_EXTENDED_MIN && raw[0xA9] == 215 && + raw[0xAA] == 63 && raw[0xAB] == 10, + "G4: v1 2159 g (below the stored step) rewrites 0xA9 to 215"); + r = opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 2150, "G4: v1 patched byte decodes 2150 g"); + + p.values.mfi_load_g = 2169; + raw[0xA9] = 216; + memcpy(snapshot, raw, sizeof(raw)); + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + CHECK(r == OT3D_OK && len == OT3D_EXTENDED_MIN && memcmp(raw, snapshot, len) == 0, + "G4: v1 2169 g (top of the stored step) is a no-op"); + + /* G5: v1 patch to the exact clamp edge — 2550 g writes byte 255. */ + p.values.mfi_load_g = 2550; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && raw[0xA9] == 255 && chk.mfi_load_g == 2550, + "G5: v1 patch 2550 g writes byte 255, decodes 2550"); + } + + printf("%s: %d failure(s)\n", failures ? "FAILED" : "OK", failures); + return failures ? 1 : 0; +} From 1427b2c61feb952c04689d6d2c3dedede7fcc4a6 Mon Sep 17 00:00:00 2001 From: Shane Jordan Date: Mon, 30 Nov 2026 00:51:18 +0000 Subject: [PATCH 3/4] fix: clear OpenTag3D v2 MFI load in place when the record cannot hold the 2-byte field --- lib/opentag3d/opentag3d_lib.c | 15 ++++++- test/native/test_opentag3d_mfi_load.c | 58 +++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/opentag3d/opentag3d_lib.c b/lib/opentag3d/opentag3d_lib.c index 50740be7..c5cdf7b8 100644 --- a/lib/opentag3d/opentag3d_lib.c +++ b/lib/opentag3d/opentag3d_lib.c @@ -674,6 +674,15 @@ opentag3d_result_t opentag3d_patch_payload(uint8_t *raw, size_t *len, size_t cap } } + /* A v2 clear on a record too short to hold 0xAB..0xAC cannot grow: the + * nonzero baseline load then lives only in the deprecated 0xA9 byte + * (inside *len), so zeroing that byte in place clears the field. + * Nonzero writes keep the normal growth and capacity check. */ + const int mfi_clear_in_place = is_v2 && + (patch->present & OT3D_PATCH_MFI_LOAD) && + patch->values.mfi_load_g == 0 && baseline.mfi_load_g != 0 && + OT3D_V2_OFF_MFI_LOAD + OT3D_V2_LEN_MFI_LOAD > capacity; + /* Pass 1 — preflight: classify every present field, find the highest * changed-field end. No mutation happens until this loop succeeds. */ size_t required_end = *len; @@ -684,6 +693,7 @@ opentag3d_result_t opentag3d_patch_payload(uint8_t *raw, size_t *len, size_t cap ot3d_span_for(f, is_v2, &patch->values, &want); ot3d_span_for(f, is_v2, &baseline, &cur); if (ot3d_field_differs(&want, &cur, NULL) != 1) continue; /* 0 = no-op */ + if (mfi_clear_in_place && f->bit == OT3D_PATCH_MFI_LOAD) continue; if (want.off + want.len > required_end) required_end = want.off + want.len; } if (required_end > capacity) return OT3D_PARSE_ERROR; @@ -700,11 +710,12 @@ opentag3d_result_t opentag3d_patch_payload(uint8_t *raw, size_t *len, size_t cap ot3d_span_for(f, is_v2, &baseline, &cur); uint8_t bytes[33]; if (ot3d_field_differs(&want, &cur, bytes) != 1) continue; + if (mfi_clear_in_place && f->bit == OT3D_PATCH_MFI_LOAD) continue; memcpy(raw + want.off, bytes, want.len); } /* A v2 clear must also zero the deprecated byte or decode falls back to - * it. A nonzero baseline means the field differed, so required_end - * already covers 0xA9. */ + * it. A nonzero baseline means 0xA9 lies inside the record, whether the + * patch grew to 0xAD or cleared in place below capacity. */ if (is_v2 && (patch->present & OT3D_PATCH_MFI_LOAD) && patch->values.mfi_load_g == 0 && baseline.mfi_load_g != 0) { raw[OT3D_V2_OFF_DEPRECATED_MFI_LOAD] = 0; diff --git a/test/native/test_opentag3d_mfi_load.c b/test/native/test_opentag3d_mfi_load.c index 144f6a3e..b28a0ee3 100644 --- a/test/native/test_opentag3d_mfi_load.c +++ b/test/native/test_opentag3d_mfi_load.c @@ -251,6 +251,64 @@ int main(void) { "G5: v1 patch 2550 g writes byte 255, decodes 2550"); } + /* H: v2 clear on a record too short to hold 0xAB..0xAC. The baseline + * load lives only in the deprecated 0xA9 byte, so the clear zeroes that + * byte in place instead of growing the record past capacity; a nonzero + * write past capacity is still rejected. */ + printf("[H] v2 mfi clear below capacity (short record)\n"); + { + opentag3d_patch_t p; + + memset(&p, 0, sizeof(p)); + p.present = OT3D_PATCH_MFI_LOAD; + p.values.mfi_load_g = 0; + + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; /* baseline decodes 2160 g */ + len = 0xAA; + memset(raw + 0xAA, 0x77, 4); /* past capacity: must stay */ + memcpy(snapshot, raw, sizeof(raw)); + r = opentag3d_patch_payload(raw, &len, 0xAA, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && len == 0xAA && raw[0xA9] == 0 && + chk.mfi_load_g == 0 && memcmp(raw + 0xAA, snapshot + 0xAA, 4) == 0, + "H1: clear at capacity 0xAA zeroes 0xA9 in place, no growth"); + + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + len = 0xAA; + r = opentag3d_patch_payload(raw, &len, 0xAC, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && len == 0xAA && raw[0xA9] == 0 && chk.mfi_load_g == 0, + "H2: clear at capacity 0xAC stays in place, len unchanged"); + + /* Nonzero writes keep the capacity check: growth to 0xAD refused. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + len = 0xA9; + memcpy(snapshot, raw, sizeof(raw)); + p.values.mfi_load_g = 2160; + r = opentag3d_patch_payload(raw, &len, 0xAC, &p); + CHECK(r == OT3D_PARSE_ERROR && len == 0xA9 && memcmp(raw, snapshot, 0xAD) == 0, + "H3: nonzero write past capacity 0xAC still rejected atomically"); + p.values.mfi_load_g = 0; + + /* A short clear with room to grow still extends to 0xAD and zeroes + * both the fallback byte and the new field. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; + len = 0xAA; + r = opentag3d_patch_payload(raw, &len, OT3D_MAX_PAYLOAD_SIZE, &p); + r = r == OT3D_OK ? opentag3d_decode(raw, len, &chk) : r; + CHECK(r == OT3D_OK && len == 0xAD && raw[0xA9] == 0 && raw[0xAB] == 0 && + raw[0xAC] == 0 && chk.mfi_load_g == 0, + "H4: short clear with capacity grows to 0xAD, zeroes 0xA9..0xAC"); + } + printf("%s: %d failure(s)\n", failures ? "FAILED" : "OK", failures); return failures ? 1 : 0; } From 581a232189727aa17e57c1facc946301c03b3869 Mon Sep 17 00:00:00 2001 From: Shane Jordan Date: Mon, 30 Nov 2026 02:00:07 +0000 Subject: [PATCH 4/4] fix: zero the partial MFI load byte when an in-place clear leaves it in the record --- lib/opentag3d/opentag3d_lib.c | 8 +++++--- test/native/test_opentag3d_mfi_load.c | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/opentag3d/opentag3d_lib.c b/lib/opentag3d/opentag3d_lib.c index c5cdf7b8..2c8d2e58 100644 --- a/lib/opentag3d/opentag3d_lib.c +++ b/lib/opentag3d/opentag3d_lib.c @@ -713,12 +713,14 @@ opentag3d_result_t opentag3d_patch_payload(uint8_t *raw, size_t *len, size_t cap if (mfi_clear_in_place && f->bit == OT3D_PATCH_MFI_LOAD) continue; memcpy(raw + want.off, bytes, want.len); } - /* A v2 clear must also zero the deprecated byte or decode falls back to - * it. A nonzero baseline means 0xA9 lies inside the record, whether the - * patch grew to 0xAD or cleared in place below capacity. */ + /* A v2 clear must also zero the deprecated byte, or decode falls back to + * it, and any half of the two-byte field that already lies inside the + * record, or a later growth would decode it as a bogus load. A nonzero + * baseline means 0xA9 lies inside the record. */ if (is_v2 && (patch->present & OT3D_PATCH_MFI_LOAD) && patch->values.mfi_load_g == 0 && baseline.mfi_load_g != 0) { raw[OT3D_V2_OFF_DEPRECATED_MFI_LOAD] = 0; + if (*len > OT3D_V2_OFF_MFI_LOAD) raw[OT3D_V2_OFF_MFI_LOAD] = 0; } *len = required_end; return OT3D_OK; diff --git a/test/native/test_opentag3d_mfi_load.c b/test/native/test_opentag3d_mfi_load.c index b28a0ee3..ee473652 100644 --- a/test/native/test_opentag3d_mfi_load.c +++ b/test/native/test_opentag3d_mfi_load.c @@ -307,6 +307,25 @@ int main(void) { CHECK(r == OT3D_OK && len == 0xAD && raw[0xA9] == 0 && raw[0xAB] == 0 && raw[0xAC] == 0 && chk.mfi_load_g == 0, "H4: short clear with capacity grows to 0xAD, zeroes 0xA9..0xAC"); + + /* H5: len = capacity = 0xAC with a stale nonzero 0xAB. The two-byte + * field is incomplete, so the baseline comes from 0xA9; the clear must + * also zero the 0xAB byte already inside the record, or a later growth + * through 0xAC decodes it as a bogus load. */ + memset(raw, 0, sizeof(raw)); + build_v2_mfi(raw, 2000); + raw[0xA9] = 216; /* baseline decodes 2160 g */ + raw[0xAB] = 0x08; /* stale half of the field */ + len = 0xAC; + r = opentag3d_patch_payload(raw, &len, 0xAC, &p); + CHECK(r == OT3D_OK && len == 0xAC && raw[0xA9] == 0 && raw[0xAB] == 0, + "H5: clear at capacity 0xAC zeroes 0xA9 and the in-record 0xAB byte"); + r = opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, "H5: short record decodes 0 after the clear"); + len = 0xAD; /* a later patch grows the record */ + r = opentag3d_decode(raw, len, &chk); + CHECK(r == OT3D_OK && chk.mfi_load_g == 0, + "H5: growth through 0xAC still decodes 0 (no stale 0x08 00)"); } printf("%s: %d failure(s)\n", failures ? "FAILED" : "OK", failures);