Repository navigation
feat(tagxl): verify mixed getter and setter TLV dialects #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
494c16c
feat(tagxl): verify mixed getter and setter TLV dialects
niko-kriznik-globtim 7fe6add
test(tagxl): cover remaining Smart Label v2 command mappings
niko-kriznik-globtim d54a974
fix(tagxl): reject invalid getter data rates
niko-kriznik-globtim 5720cea
fix(tagxl): reject out-of-range interval and acceleration setters
niko-kriznik-globtim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package tagxl | ||
|
|
||
| import "github.com/truvami/decoder/pkg/common" | ||
|
|
||
| // ConfigurationDialect selects the fPort-151 command map for a device family. | ||
| type ConfigurationDialect int | ||
|
|
||
| const ( | ||
| ConfigurationDialectUnspecified ConfigurationDialect = iota | ||
| ConfigurationDialectTagXL | ||
| ConfigurationDialectSmartLabelV2 | ||
| ) | ||
|
|
||
| func (d ConfigurationDialect) String() string { | ||
| switch d { | ||
| case ConfigurationDialectTagXL: | ||
| return "tagxl" | ||
| case ConfigurationDialectSmartLabelV2: | ||
| return "smartlabel-v2" | ||
| default: | ||
| return "unspecified" | ||
| } | ||
| } | ||
|
|
||
| type commandSpec struct { | ||
| setterTag byte | ||
| getterTag byte | ||
| setterLen int | ||
| responseLen int | ||
| mask byte | ||
| prefixLen int | ||
| dataRate bool | ||
| validate func([]byte) error | ||
| } | ||
|
|
||
| type dialectLimits struct { | ||
| maxCommands int | ||
| maxResponseBytes int | ||
| } | ||
|
|
||
| type dialectSpec struct { | ||
| limits dialectLimits | ||
| setters map[byte]commandSpec | ||
| getters map[byte]commandSpec | ||
| } | ||
|
|
||
| func newDialectSpec(limits dialectLimits, commands []commandSpec) dialectSpec { | ||
| spec := dialectSpec{ | ||
| limits: limits, | ||
| setters: make(map[byte]commandSpec, len(commands)), | ||
| getters: make(map[byte]commandSpec, len(commands)), | ||
| } | ||
| for _, command := range commands { | ||
| if command.setterTag != 0 { | ||
| spec.setters[command.setterTag] = command | ||
| } | ||
| if command.getterTag != 0 { | ||
| spec.getters[command.getterTag] = command | ||
| } | ||
| } | ||
| return spec | ||
| } | ||
|
|
||
| func tagXLHeartbeatValidate(value []byte) error { | ||
| if len(value) == 1 && value[0] > 168 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func tagXLAdvertisementValidate(value []byte) error { | ||
| if len(value) == 1 && value[0] == 0 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func tagXLMovingIntervalsValidate(value []byte) error { | ||
| if len(value) != 4 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| packed := common.BytesToUint32(value) | ||
| moving := packed >> 16 | ||
| steady := packed & 0xffff | ||
| if moving < 60 || moving > 86400 || steady < 120 || steady > 86400 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func tagXLAccelerationValidate(value []byte) error { | ||
| if len(value) != 4 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| packed := common.BytesToUint32(value) | ||
| threshold := packed >> 16 | ||
| delay := packed & 0xffff | ||
| if threshold < 10 || threshold > 8000 || delay < 1000 || delay > 10000 { | ||
| return common.ErrValidationFailed | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| var tagXLDialect = newDialectSpec(dialectLimits{maxCommands: 32, maxResponseBytes: 255}, []commandSpec{ | ||
| {setterTag: setterTagDeviceFlags, getterTag: tlvTagDeviceFlags, setterLen: 1, responseLen: 1, mask: 0x0f}, | ||
| {setterTag: setterTagMovingIntervals, getterTag: tlvTagMovingIntervals, setterLen: 4, responseLen: 4, validate: tagXLMovingIntervalsValidate}, | ||
| {setterTag: setterTagAccelerationThreshold, getterTag: tlvTagAccelerationThreshold, setterLen: 4, responseLen: 4, validate: tagXLAccelerationValidate}, | ||
| {setterTag: setterTagHeartbeatInterval, getterTag: tlvTagHeartbeatInterval, setterLen: 1, responseLen: 1, validate: tagXLHeartbeatValidate}, | ||
| {setterTag: setterTagAdvertisementInterval, getterTag: tlvTagAdvertisementInterval, setterLen: 1, responseLen: 1, validate: tagXLAdvertisementValidate}, | ||
| {setterTag: setterTagRotationFlags, getterTag: tlvTagRotationFlags, setterLen: 1, responseLen: 1, mask: 0x03}, | ||
| {setterTag: 0x27, getterTag: 0x4d, setterLen: 2, responseLen: 2}, | ||
| {setterTag: setterTagDataRate, getterTag: tlvTagDataRate, setterLen: 1, responseLen: 1, dataRate: true}, | ||
| {setterTag: 0x29, getterTag: 0x4f, setterLen: 1, responseLen: 1}, | ||
| {setterTag: 0x2a, getterTag: 0x50, setterLen: 1, responseLen: 1, mask: 0x03}, | ||
| {setterTag: 0x2b, getterTag: 0x51, setterLen: 1, responseLen: 1, mask: 0x01}, | ||
| {getterTag: tlvTagBattery, responseLen: 2}, | ||
| {getterTag: tlvTagFirmwareHash, responseLen: 4}, | ||
| {getterTag: tlvTagResetCount, responseLen: 2}, | ||
| {getterTag: tlvTagResetCause, responseLen: 4}, | ||
| {getterTag: tlvTagScanCounts, responseLen: 4}, | ||
| {getterTag: 0x4c, responseLen: 4}, | ||
| {getterTag: 0x52, responseLen: 6}, | ||
| }) | ||
|
|
||
| var smartLabelV2Dialect = newDialectSpec(dialectLimits{maxCommands: 30, maxResponseBytes: 51}, []commandSpec{ | ||
| {setterTag: 0x20, getterTag: 0x40, setterLen: 1, responseLen: 1, mask: 0x1f}, | ||
| {setterTag: 0x21, getterTag: 0x41, setterLen: 4, responseLen: 4}, | ||
| {setterTag: 0x22, getterTag: 0x42, setterLen: 4, responseLen: 4, prefixLen: 2}, | ||
| {setterTag: 0x23, getterTag: 0x43, setterLen: 1, responseLen: 1}, | ||
| {setterTag: 0x24, getterTag: 0x44, setterLen: 1, responseLen: 1}, | ||
| {setterTag: 0x28, getterTag: 0x4e, setterLen: 1, responseLen: 1, dataRate: true}, | ||
| {setterTag: 0x29, getterTag: 0x4c, setterLen: 2, responseLen: 2}, | ||
| {setterTag: 0x2a, getterTag: 0x4d, setterLen: 1, responseLen: 1}, | ||
| {setterTag: 0x2b, getterTag: 0x50, setterLen: 1, responseLen: 1}, | ||
| {setterTag: 0x2c, getterTag: 0x51, setterLen: 16, responseLen: 16}, | ||
| {setterTag: 0x30, getterTag: 0x60, setterLen: 7, responseLen: 7}, | ||
| {getterTag: 0x45, responseLen: 2}, | ||
| {getterTag: 0x46, responseLen: 4}, | ||
| {getterTag: 0x49, responseLen: 2}, | ||
| {getterTag: 0x4a, responseLen: 4}, | ||
| {getterTag: 0x4b, responseLen: 4}, | ||
| {getterTag: 0x61, responseLen: 1}, | ||
| {getterTag: 0x63, responseLen: 2}, | ||
| }) | ||
|
|
||
| func dialectSpecFor(dialect ConfigurationDialect) (dialectSpec, error) { | ||
| switch dialect { | ||
| case ConfigurationDialectTagXL: | ||
| return tagXLDialect, nil | ||
| case ConfigurationDialectSmartLabelV2: | ||
| return smartLabelV2Dialect, nil | ||
| default: | ||
| return dialectSpec{}, errConfigurationUnknownDialect | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.