forked from jormenjanssen/go-canopen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdic_eds_parser.go
170 lines (139 loc) · 3.63 KB
/
dic_eds_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package canopen
import (
"fmt"
"regexp"
"strconv"
"strings"
"gopkg.in/ini.v1"
)
// DicEDSParse If in is string, it must be a path to a file
// else if in must be eds data as []byte
func DicEDSParse(in interface{}) (*DicObjectDic, error) {
// Load ini file
iniData, err := ini.Load(in)
if err != nil {
return nil, nil
}
// Create object dictionary
ddic := NewDicObjectDic()
// Get NodeID & Baudrate
if sec, err := iniData.GetSection("DeviceComissioning"); err == nil {
// Get NodeID
if key, err := sec.GetKey("NodeId"); err == nil {
ab, _ := strconv.ParseInt(key.String(), 0, 0)
ddic.NodeID = int(ab)
}
// Get Baudrate
if key, err := sec.GetKey("Baudrate"); err == nil {
ab, _ := strconv.ParseInt(key.String(), 0, 0)
ddic.Baudrate = int(ab)
}
}
matchIdxRegexp := regexp.MustCompile(`^[0-9A-Fa-f]{4}$`)
matchSubIdxRegexp := regexp.MustCompile(`^([0-9A-Fa-f]{4})sub([0-9A-Fa-f]+)$`)
// Iterate over sections
for _, sec := range iniData.Sections() {
sectionName := sec.Name()
// Match index
if matchIdxRegexp.MatchString(sectionName) {
idx, err := strconv.ParseUint(sectionName, 16, 16)
if err != nil {
return nil, err
}
index := uint16(idx)
name := sec.Key("ParameterName").String()
objectType, _ := strconv.ParseUint(sec.Key("ObjectType").String(), 0, 8)
// Object type == VARIABLE
if byte(objectType) == DicVar || byte(objectType) == Domain {
variable, err := buildVariable(index, 0, name, sec, iniData)
if err != nil {
return nil, err
}
ddic.AddObject(variable)
}
// Object type == ARRAY
if byte(objectType) == DicArr {
array := &DicArray{Index: index, Name: name}
ddic.AddObject(array)
}
// Object type == RECORD
if byte(objectType) == DicRec {
record := &DicRecord{Index: index, Name: name}
ddic.AddObject(record)
}
continue
}
// Match sub-indexs
if matchSubIdxRegexp.MatchString(sectionName) {
idx, err := strconv.ParseUint(sectionName[0:4], 16, 16)
if err != nil {
return nil, err
}
index := uint16(idx)
sidx, err := strconv.ParseUint(sectionName[7:], 16, 8)
if err != nil {
return nil, err
}
subIndex := uint8(sidx)
name := sec.Key("ParameterName").String()
object := ddic.FindIndex(index)
if object == nil {
return nil, fmt.Errorf("index with id %d not found", index)
}
variable, err := buildVariable(index, subIndex, name, sec, iniData)
if err != nil {
return nil, err
}
object.AddMember(variable)
}
// @TODO: Match [index]Name
}
return ddic, nil
}
// @TODO: check working
func buildVariable(
index uint16,
subIndex uint8,
name string,
sec *ini.Section,
iniData *ini.File,
) (*DicVariable, error) {
variable := &DicVariable{
Index: index,
SubIndex: subIndex,
Name: name,
AccessType: strings.ToLower(sec.Key("AccessType").String()),
}
// Get & set DataType
i, err := sec.Key("DataType").Int()
if err != nil {
return nil, err
}
variable.DataType = byte(i)
if variable.DataType > 0x1B {
dTypeStr := fmt.Sprintf("%dsub1", variable.DataType)
i, err := iniData.Section(dTypeStr).Key("DefaultValue").Uint()
if err != nil {
return nil, err
}
variable.DataType = byte(i)
}
if lowl, err := sec.GetKey("LowLimit"); err == nil && lowl.String() != "" {
i, err := lowl.Int()
if err != nil {
return nil, err
}
variable.Min = i
}
if howl, err := sec.GetKey("HighLimit"); err == nil && howl.String() != "" {
i, err := howl.Int()
if err != nil {
return nil, err
}
variable.Max = i
}
if def, err := sec.GetKey("DefaultValue"); err == nil {
variable.Default = []byte(def.Value())
}
return variable, nil
}