-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathapdu.go
52 lines (49 loc) · 1.06 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
package iec104
import "fmt"
//APDU 104数据包
type APDU struct {
APCI *APCI
ASDU *ASDU
Len int
ASDULen int
CtrType byte
CtrFrame interface{}
Signals []*Signal
}
//parseAPDU 解析APDU
func (apdu *APDU) parseAPDU(input []byte) error {
if input == nil || len(input) < 4 {
return fmt.Errorf("APDU报文[%X]非法", input)
}
apci := &APCI{
ApduLen: len(input),
Ctr1: input[0],
Ctr2: input[1],
Ctr3: input[2],
Ctr4: input[3],
}
fType, ctrFrame, err := apci.ParseCtr()
if err != nil {
return fmt.Errorf("APDU报文[%X]解析控制域异常: %v", input, err)
}
asdu := new(ASDU)
var asduLen int
signals := make([]*Signal, 0)
if len(input[4:]) < 1 {
asduLen = 0
} else {
signals, err = asdu.ParseASDU(input[4:])
if err != nil {
return fmt.Errorf("APDU报文[%X]解析ASDU域[%X]异常: %v", input, input[4:], err)
}
asduLen = len(input[6:])
}
apdu.APCI = apci
apdu.ASDU = asdu
apdu.Len = apci.ApduLen
apdu.ASDULen = asduLen
apdu.CtrType = fType
apdu.CtrFrame = ctrFrame
apdu.Signals = signals
return nil
}