-
Notifications
You must be signed in to change notification settings - Fork 1
/
apci.go
53 lines (44 loc) · 1.17 KB
/
apci.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
package vv104
import (
"bytes"
)
// Apci header of the iec 104 frame
const (
startbyte byte = 0x68
)
type SeqNumber int
type Apci struct {
length uint8
FrameFormat FrameFormat
Rsn SeqNumber
Ssn SeqNumber
UFormat UFormat
}
func (apci *Apci) serialize(state State, buf *bytes.Buffer, asduLength uint8) {
buf.WriteByte(startbyte)
apci.length = asduLength + 4
buf.WriteByte(byte(apci.length)) // todo check for overfow
apci.Ssn = state.sendAck.seqNumber
apci.Rsn = state.recvAck.seqNumber
apci.writeCtrlFields(state, buf)
}
func (apci Apci) writeCtrlFields(state State, buf *bytes.Buffer) {
// var b byte
switch apci.FrameFormat {
case IFormatFrame:
buf.WriteByte(byte(apci.Ssn) << 1) // & 0xFF)
buf.WriteByte(byte(apci.Ssn >> 7))
buf.WriteByte(byte(apci.Rsn) << 1) //& 0xFF
buf.WriteByte(byte(apci.Rsn >> 7))
case SFormatFrame:
buf.WriteByte(0b00000001)
buf.WriteByte(0)
buf.WriteByte(byte(apci.Rsn) << 1) //& 0xFF
buf.WriteByte(byte(apci.Rsn >> 7))
case UFormatFrame:
buf.WriteByte(byte(apci.UFormat) | 0b00000011)
buf.WriteByte(0)
buf.WriteByte(0)
buf.WriteByte(0)
}
}