-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapdu.go
174 lines (144 loc) · 3.9 KB
/
apdu.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
171
172
173
174
package vv104
import (
"bytes"
"errors"
"fmt"
)
type Apdu struct {
Apci Apci
Asdu Asdu
}
// CheckApdu checks all Apdu Fields if they comply to IEC 104 standard
// todo: extend
func (apdu Apdu) CheckApdu() (bool, error) {
if apdu.Apci.FrameFormat == IFormatFrame {
if apdu.Asdu.Num < 1 {
return false, errors.New("Num < 1 for IFormat")
}
if apdu.Asdu.Casdu == 0 {
return false, errors.New("Casdu = 0 is not permitted")
}
// ...
}
return true, nil
}
func (apdu Apdu) String() string {
switch apdu.Apci.FrameFormat {
case IFormatFrame:
return fmt.Sprintf("(%d/%d) ", apdu.Apci.Ssn, apdu.Apci.Rsn) + apdu.Asdu.String()
case SFormatFrame:
return fmt.Sprintf("S-Format (%d)", apdu.Apci.Rsn)
case UFormatFrame:
return apdu.Apci.UFormat.String()
}
return ""
}
func (apdu *Apdu) Serialize(state State) ([]byte, error) { // TODO error
asduBuf := new(bytes.Buffer)
apciBuf := new(bytes.Buffer)
switch apdu.Apci.FrameFormat {
case IFormatFrame:
apdu.Asdu.serialize(state, asduBuf)
apdu.Apci.serialize(state, apciBuf, uint8(asduBuf.Len()))
s := [][]byte{apciBuf.Bytes(), asduBuf.Bytes()}
var emptySep []byte
return bytes.Join(s, emptySep), nil
case UFormatFrame, SFormatFrame:
apdu.Apci.serialize(state, apciBuf, 0)
return apciBuf.Bytes(), nil
}
return []byte{}, nil
}
func NewApdu() Apdu {
apdu := Apdu{
Apci: Apci{
length: 6,
FrameFormat: 0,
Rsn: 0,
Ssn: 0,
UFormat: 0,
},
Asdu: Asdu{
TypeId: 0,
Num: 1,
Sequence: false,
CauseTx: 0,
Negative: false,
Test: false,
OrigAddr: 0,
Casdu: 1,
InfoObj: []InfoObj{},
},
}
return apdu
}
func ParseApdu(buf *bytes.Buffer) ([]Apdu, error) {
var b byte
var b1 byte
var b2 byte
// var err error
var allApdus []Apdu
// fmt.Println("len", buf.Len())
for buf.Len() >= 6 {
apdu := NewApdu()
// if buf.Len() < 6 {
// return allApdus, errors.New("buffer < 6 bytes, can't parse")
// }
// if buf.Len() > 253 { // todo check if 255?
// // hier müssen wahrscheinlich mehrere frames ausgewertet werden
// // todo
// }
b, _ = buf.ReadByte()
if b != startbyte {
fmt.Println(buf.Bytes())
return allApdus, errors.New("startbyte is not first byte, todo")
}
b, _ = buf.ReadByte()
if (uint8(b) > 253) || (uint8(b) < 4) {
return allApdus, errors.New("apdu len is not within range")
}
apdu.Apci.length = uint8(b)
// fmt.Println("apci len:", apdu.Apci.length, "buf.Len", buf.Len())
// ctrl field 1
b, _ = buf.ReadByte()
if (b & 0b0000_0001) == 0b0000_0000 {
// i frame
apdu.Apci.FrameFormat = IFormatFrame
b1 = b
b2, _ = buf.ReadByte()
apdu.Apci.Ssn = SeqNumber((uint16(b1) >> 1) + (uint16(b2) << 7))
b1, _ = buf.ReadByte()
b2, _ = buf.ReadByte()
apdu.Apci.Rsn = SeqNumber((uint16(b1) >> 1) + (uint16(b2) << 7))
apdu.Asdu, _ = parseAsdu(buf)
} else if (b & 0b0000_0011) == 0b0000_0001 {
// s frame
apdu.Apci.FrameFormat = SFormatFrame
//ctrl field 2
_, _ = buf.ReadByte() // empty byte
//ctrl field 3
b1, _ = buf.ReadByte()
//ctrl field 4
b2, _ = buf.ReadByte()
apdu.Apci.Rsn = SeqNumber((uint16(b1) >> 1) + (uint16(b2) << 7))
} else if (b & 0b0000_0011) == 0b0000_0011 {
// u frame
apdu.Apci.FrameFormat = UFormatFrame
if b == byte(StartDTAct) {
apdu.Apci.UFormat = StartDTAct
} else if b == byte(StartDTCon) {
apdu.Apci.UFormat = StartDTCon
} else if b == byte(StopDTAct) {
apdu.Apci.UFormat = StopDTAct
} else if b == byte(StopDTCon) {
apdu.Apci.UFormat = StopDTCon
} else if b == byte(TestFRAct) {
apdu.Apci.UFormat = TestFRAct
} else if b == byte(TestFRCon) {
apdu.Apci.UFormat = TestFRCon
}
}
allApdus = append(allApdus, apdu)
}
return allApdus, nil
}