8000 Add support for large messages by bpg · Pull Request #54 · tormoder/fit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for large messages #54

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 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ const (
localMesgNumMask byte = 0x0F

maxLocalMesgs byte = localMesgNumMask + 1
maxFieldSize byte = 255

littleEndian byte = 0x00
bigEndian byte = 0x01

bytesForCRC byte = 2
headerSizeCRC byte = 14
headerSizeNoCRC byte = (headerSizeCRC - bytesForCRC)
headerSizeNoCRC byte = headerSizeCRC - bytesForCRC

fitDataTypeString string = ".FIT"

Expand Down
4 changes: 2 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type decoder struct {
}

crc dyncrc16.Hash16
tmp [maxFieldSize]byte
tmp [255 * 3]byte
defmsgs [maxLocalMesgs]*defmsg

timestamp uint32
Expand Down Expand Up @@ -434,7 +434,7 @@ func (d *decoder) parseDefinitionMessage(recordHeader byte) (*defmsg, error) {
return &dm, nil
}

if err = d.readFull(d.tmp[0 : 3*dm.fields]); err != nil {
if err = d.readFull(d.tmp[0 : 3*uint16(dm.fields)]); err != nil {
return nil, fmt.Errorf("error parsing fields: %v", err)
}

Expand Down
10 changes: 10 additions & 0 deletions reader_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ var decodeTestFiles = [...]struct {
false,
"",
},
{
"bpg",
"garmin.fit",
false,
8578045933883478770,
true,
tdoNone,
false,
"Has a definition message with number of fields >85",
},
{
"corrupt",
"activity-filecrc.fit",
Expand Down
Binary file added testdata/bpg/garmin.fit
Binary file not shown.
Binary file added testdata/bpg/garmin.fit.golden.gz
Binary file not shown.
29 changes: 24 additions & 5 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,30 @@ func (e *encoder) encodeFile(file reflect.Value) error {
for j := 0; j < v.Len(); j++ {
v2 := reflect.Indirect(v.Index(j))

if j == 0 {
def = getEncodeMesgDef(v2, 0)
err := e.writeDefMesg(def)
if err != nil {
return err
// Not necessary that the first message will have all defined fields that may appear in the following messages
// So we have to build a model first by iterating though all the message and collecting valid definition fields
if def == nil {
// map to collect field definitions
mfields := make(map[byte]*field)
for k := 0; k < v.Len(); k++ {
r := reflect.Indirect(v.Index(k))
def = getEncodeMesgDef(r, 0)
for _, f := range def.fields {
mfields[f.num] = f
}
}
// should not be nil at this point, but just in case
if def != nil {
def.fields = make([]*field, 0, len(mfields))
for _, f := range mfields {
def.fields = append(def.fields, f)
}
err := e.writeDefMesg(def)
if err != nil {
return err
}
} else {
return fmt.Errorf("cannot create definition message for %+v", v.Interface())
}
}

Expand Down
0