forked from Yobol/go-iec104
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapdu.go
74 lines (60 loc) · 1.52 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
package iec104
import (
"fmt"
)
const (
ApduHeaderLen = 4 // non-include startByte and apduLen
AsduHeaderLen = 6
)
/*
APDU (Application Protocol Data Unit).
APDU contains an APCI or an APCI with ASDU.
| <- 8 bits -> | ----- -----
| Start Byte (Ox68) | | |
| Length of APDU | | |
| Control Field 1 | APCI APDU
| Control Field 2 | | |
| Control Field 3 | | |
| Control Field 4 | | |
| <- 8 bits -> | ----- -----
<- APDU with fixed length ->
| <- 8 bits -> | ----- -----
| Start Byte (Ox68) | | |
| Length of APDU | | |
| Control Field 1 | APCI APDU
| Control Field 2 | | |
| Control Field 3 | | |
| Control Field 4 | | |
| ASDU | ASDU |
| <- 8 bits -> | ----- -----
<- APDU with variable length ->
*/
type APDU struct {
*APCI
*ASDU
frame Frame
}
func (apdu *APDU) Parse(data []byte) error {
if len(data) < ApduHeaderLen {
return fmt.Errorf("invalid apdu body: % X", data)
}
// Parse APCI.
apci := new(APCI)
frame, err := apci.Parse(data[:ApduHeaderLen])
if err != nil {
return err
}
apdu.APCI = apci
apdu.frame = frame
switch frame.Type() {
case FrameTypeS, FrameTypeU: // S-format or U-format frame doesn't have ASDU.
return nil
}
// Parse ASDU.
asdu := new(ASDU)
if err = asdu.Parse(data[ApduHeaderLen:]); err != nil {
return err
}
apdu.ASDU = asdu
return nil
}