Skip to content

Commit b237644

Browse files
committed
started to implement reading characteristics values
added record manager.go to get characteristics data. added toHashmap method to a2l.A2L.
1 parent 0f4a9f6 commit b237644

File tree

8 files changed

+106
-115
lines changed

8 files changed

+106
-115
lines changed

a2l/a2l.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package a2l
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"os"
@@ -32,6 +33,23 @@ type A2L struct {
3233
Project Project
3334
}
3435

36+
// toHashMap recursively generates a HashMap of all structs and their values in the a2l struct
37+
// potentially useful if goPy is used to build a python library of calibrationReader
38+
func (a *A2L) toHashMap() (map[string]interface{}, error) {
39+
var m map[string]interface{}
40+
bytes, err := json.Marshal(a)
41+
if err != nil {
42+
log.Err(err).Msg("could not serialize a2l struct into json")
43+
return m, err
44+
}
45+
err = json.Unmarshal(bytes, &m)
46+
if err != nil {
47+
log.Err(err).Msg("could not deserialize json into hashmap")
48+
return m, err
49+
}
50+
return m, err
51+
}
52+
3553
// ParseFromFile is the main exported function to be called from a2l package.
3654
// it takes an .a2l file and parses it
3755
func ParseFromFile(filepath string) (A2L, error) {

a2l/a2l_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ func TestParseFromFile(t *testing.T) {
2626
log.Info().Msg("time for parsing a2l test file: " + fmt.Sprint(elapsed.Milliseconds()) + "[ms]")
2727
}
2828

29+
func TestMapify(t *testing.T) {
30+
configureLogger()
31+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
32+
a2lPath := "testing/ASAP2_Demo_V171_allKeywords.a2l"
33+
a, err := ParseFromFile(a2lPath)
34+
if err != nil {
35+
t.Fatalf("failed parsing with error: %s.", err)
36+
}
37+
startTime := time.Now()
38+
hm, err := a.toHashMap()
39+
if err != nil {
40+
t.Fatalf("failed to convert a2l struct into hashmap %s.", err)
41+
}
42+
endTime := time.Now()
43+
elapsed := endTime.Sub(startTime)
44+
_, err = fmt.Println(hm)
45+
if err != nil {
46+
log.Err(err).Msg("failed to print hashmap")
47+
t.Fatalf("failed to print a2l struct hashmap %s.", err)
48+
}
49+
log.Info().Str("project name", a.Project.Name).Msg("finished parsing:")
50+
log.Info().Msg("time for parsing a2l test file: " + fmt.Sprint(elapsed.Milliseconds()) + "[ms]")
51+
}
52+
2953
func FuzzParseA2L(f *testing.F) {
3054
configureLogger()
3155
zerolog.SetGlobalLevel(zerolog.WarnLevel)

a2l/a2ml_version.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"github.com/rs/zerolog/log"
88
)
99

10-
/*a2mlVersion exists in order to declare what kind of BLOBs should be generated from
10+
/*
11+
a2mlVersion exists in order to declare what kind of BLOBs should be generated from
1112
the AML parts. Since ASAP2 version 1.3.1 a specification for the storage layout of the
1213
BLOBs exist. The keyword is optional. When the keyword is omitted, or the version
1314
number is below 1.3.1 then the old BLOB format is used. When the A2ML version number
@@ -17,13 +18,14 @@ The A2ML version can be expressed by two numerals:
1718
- UpgradeNo
1819
where ‘VersionNo’ represents the main version number and ‘UpgradeNo’ the upgrade
1920
number (fractional part of version number).
20-
This keyword will not be interpreted semantically anymore.*/
21+
This keyword will not be interpreted semantically anymore.
22+
*/
2123
type a2mlVersion struct {
22-
//versionNo contains the Version number of AML part
23-
versionNo uint16
24+
//VersionNo contains the Version number of AML part
25+
VersionNo uint16
2426
versionNoSet bool
25-
//upgradeNo contains the Upgrade number of AML part
26-
upgradeNo uint16
27+
//UpgradeNo contains the Upgrade number of AML part
28+
UpgradeNo uint16
2729
upgradeNoSet bool
2830
}
2931

@@ -48,7 +50,7 @@ forLoop:
4850
log.Err(err).Msg("a2mlVersion versionNo could not be parsed")
4951
break forLoop
5052
}
51-
av.versionNo = uint16(buf)
53+
av.VersionNo = uint16(buf)
5254
av.versionNoSet = true
5355
log.Info().Msg("a2mlVersion versionNo successfully parsed")
5456
} else if !av.upgradeNoSet {
@@ -58,7 +60,7 @@ forLoop:
5860
log.Err(err).Msg("a2mlVersion upgradeNo could not be parsed")
5961
break forLoop
6062
}
61-
av.upgradeNo = uint16(buf)
63+
av.UpgradeNo = uint16(buf)
6264
av.upgradeNoSet = true
6365
log.Info().Msg("a2mlVersion upgradeNo successfully parsed")
6466
break forLoop

a2l/mod_par.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ type modPar struct {
2222
noOfInterfaces noOfInterfaces
2323
phoneNo phoneNo
2424
supplier supplier
25-
SystemConstants map[string]systemConstant
25+
SystemConstants map[string]SystemConstant
2626
user user
2727
version version
2828
}
2929

3030
func parseModPar(tok *tokenGenerator) (modPar, error) {
3131
mp := modPar{}
32-
mp.SystemConstants = make(map[string]systemConstant, 2000)
32+
mp.SystemConstants = make(map[string]SystemConstant, 2000)
3333
var err error
3434
forLoop:
3535
for {
@@ -134,7 +134,7 @@ forLoop:
134134
}
135135
log.Info().Msg("modPar supplier successfully parsed")
136136
case systemConstantToken:
137-
var buf systemConstant
137+
var buf SystemConstant
138138
buf, err = parseSystemConstant(tok)
139139
if err != nil {
140140
log.Err(err).Msg("modPar systemConstant could not be parsed")

a2l/system_constant.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"github.com/rs/zerolog/log"
77
)
88

9-
type systemConstant struct {
9+
type SystemConstant struct {
1010
name string
1111
nameSet bool
1212
value string
1313
valueSet bool
1414
}
1515

16-
func parseSystemConstant(tok *tokenGenerator) (systemConstant, error) {
17-
sc := systemConstant{}
16+
func parseSystemConstant(tok *tokenGenerator) (SystemConstant, error) {
17+
sc := SystemConstant{}
1818
var err error
1919
forLoop:
2020
for {

calib_data.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ import (
2929
// CalibrationData contains the parsed structs from the a2l as well as the byte data from the hex file
3030
// that are parsed by ReadCalibration()
3131
type CalibrationData struct {
32-
A2l a2l.A2L
33-
Hex map[uint32]byte
32+
A2l a2l.A2L
33+
ModuleIndex uint8
34+
Hex map[uint32]byte
3435
}
3536

3637
// ReadCalibration takes filepaths to the a2l file and the hex file,
3738
// parses them in parallel and returns a CalibrationData struct
3839
func ReadCalibration(a2lFilePath string, hexFilePath string) (CalibrationData, error) {
3940
var err error
4041
var cd CalibrationData
42+
cd.ModuleIndex = 0
4143
var errChan = make(chan error, 2)
4244
var a2lChan = make(chan a2l.A2L, 1)
4345
var hexChan = make(chan map[uint32]byte, 1)

characteristic_types.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

record_manager.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,58 @@ import (
44
"errors"
55

66
"github.com/asap2Go/calibrationReader/a2l"
7+
"github.com/rs/zerolog/log"
78
)
89

9-
func getValue(m *a2l.Module, c *a2l.Characteristic) (error, interface{}) {
10+
func (cd *CalibrationData) getSystemConstant(ident string) (a2l.SystemConstant, error) {
11+
modPar := cd.A2l.Project.Modules[cd.ModuleIndex].ModPar
12+
s, exists := modPar.SystemConstants[ident]
13+
if !exists {
14+
err := errors.New("no system constant with name " + ident)
15+
log.Err(err).Msg("system constant not found")
16+
return s, err
17+
}
18+
return s, nil
19+
}
20+
21+
func (cd *CalibrationData) getCharacteristicValueBinary(c a2l.Characteristic) (interface{}, error) {
1022
var err error
23+
rl, err := cd.getRecordLayout(c)
24+
if err != nil {
25+
return nil, err
26+
}
27+
log.Debug().Msg("record layout " + rl.Name + "found")
28+
return err, nil
29+
}
30+
31+
func (cd *CalibrationData) getCharacteristicValueDecimal() (interface{}, error) {
32+
return nil, nil
33+
}
1134

12-
//get record layout for characteristic
35+
func (cd *CalibrationData) getCharacteristicValueDisplay() (interface{}, error) {
36+
return nil, nil
37+
}
38+
39+
func (cd *CalibrationData) getCharacteristicValueHex() (interface{}, error) {
40+
return nil, nil
41+
}
42+
43+
// get record layout for a specified characteristic
44+
func (cd *CalibrationData) getRecordLayout(c a2l.Characteristic) (a2l.RecordLayout, error) {
45+
var err error
46+
var rl a2l.RecordLayout
47+
module := cd.A2l.Project.Modules[cd.ModuleIndex]
1348
if !c.DepositSet {
1449
err = errors.New("no deposit set in characteristic " + c.Name)
15-
return err, nil
50+
log.Err(err).Msg("record layout not found")
51+
return rl, err
1652
}
17-
18-
rl, exists := m.RecordLayouts[c.Deposit]
53+
var exists bool
54+
rl, exists = module.RecordLayouts[c.Deposit]
1955
if !exists {
2056
err = errors.New("no record layout found for deposit identifier" + c.Deposit + " of characteristic " + c.Name)
21-
return err, nil
57+
log.Err(err).Msg("record layout not found")
58+
return rl, err
2259
}
23-
24-
return err, nil
60+
return rl, nil
2561
}

0 commit comments

Comments
 (0)