-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupfunction.go
More file actions
184 lines (167 loc) · 5.56 KB
/
Copy pathgroupfunction.go
File metadata and controls
184 lines (167 loc) · 5.56 KB
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
175
176
177
178
179
180
181
182
183
184
package n2k
import (
"time"
"github.com/open-ships/n2k/internal/framer"
"github.com/open-ships/n2k/pgn"
)
// restoreDefaultIntervalTicks is the reserved transmission-interval value
// (0xFFFFFFFE ms) that commands a device to revert to its default cadence.
const restoreDefaultIntervalTicks = 0xFFFFFFFE
// Heartbeat cadence bounds enforced when a group function retimes PGN 126993.
const (
minHeartbeatInterval = time.Second
maxHeartbeatInterval = 60 * time.Second
)
// handleGroupFunction reacts to decoded group functions (PGN 126208) from
// the system router. Request group functions for PGNs this client transmits
// are honored (including retiming); everything else is refused with an
// acknowledge group function when addressed to us specifically.
func (c *Client) handleGroupFunction(msg pgn.Message) {
switch gf := msg.(type) {
case *pgn.NmeaRequestGroupFunction:
c.handleRequestGF(gf)
case *pgn.NmeaCommandGroupFunction:
if !c.groupFnForUs(gf.Info) {
return
}
c.ackGroupFunction(gf.Pgn)
case *pgn.NmeaReadFieldsGroupFunction:
if !c.groupFnForUs(gf.Info) {
return
}
c.ackGroupFunction(gf.Pgn)
case *pgn.NmeaWriteFieldsGroupFunction:
if !c.groupFnForUs(gf.Info) {
return
}
c.ackGroupFunction(gf.Pgn)
}
}
// groupFnForUs reports whether a group function is addressed to this client
// specifically. Broadcast group functions return false: they are honored for
// requests but never acknowledged, per the group-function usage rules.
func (c *Client) groupFnForUs(info pgn.MessageInfo) bool {
if info.TargetId == nil || *info.TargetId == framer.BroadcastAddr {
return false
}
c.mu.Lock()
defer c.mu.Unlock()
return *info.TargetId == c.sourceAddr
}
// handleRequestGF applies a request group function: retime and/or transmit
// the named PGN when we transmit it, refuse otherwise.
func (c *Client) handleRequestGF(gf *pgn.NmeaRequestGroupFunction) {
broadcast := gf.Info.TargetId == nil || *gf.Info.TargetId == framer.BroadcastAddr
if !broadcast && !c.groupFnForUs(gf.Info) {
return
}
if gf.Pgn == nil {
return
}
requested := uint32(*gf.Pgn)
if !c.transmitsPGN(requested) {
if !broadcast {
c.ackGroupFunctionError(requested, uint64(pgn.PGNNotSupported))
}
return
}
// Parameter-filtered requests are not supported.
if len(gf.Repeating1) > 0 {
if !broadcast {
c.ackGroupFunctionError(requested, uint64(pgn.NotSupported_3))
}
return
}
c.applyRequestedInterval(requested, gf.TransmissionInterval)
}
// transmitsPGN reports whether this client can transmit the given PGN on
// request: its own identity PGNs plus anything with an active broadcaster.
func (c *Client) transmitsPGN(pgnNum uint32) bool {
switch pgnNum {
case 126993, 126996, 126998:
return true
}
return c.broadcasterFor(pgnNum) != nil
}
// applyRequestedInterval retimes and/or transmits a PGN we support.
// interval is in wire ticks (milliseconds): nil = no cadence change (just
// transmit once), 0 = stop periodic transmission, 0xFFFFFFFE = restore the
// default cadence, anything else = the new cadence. Cadence only applies to
// PGNs with a schedule (the heartbeat and active broadcasts); product and
// configuration info have none, so a request simply transmits them.
func (c *Client) applyRequestedInterval(pgnNum uint32, intervalTicks *uint64) {
switch pgnNum {
case 126996:
c.writeProtocol("group-function product information response", protocolRequired, c.productInfo.message())
return
case 126998:
c.writeProtocol("group-function configuration information response", protocolRequired, c.configInfo.message())
return
case 126993:
if intervalTicks == nil {
c.heartbeat.sendNow()
return
}
switch *intervalTicks {
case 0:
c.heartbeat.setInterval(0)
case restoreDefaultIntervalTicks:
// setInterval transmits immediately, covering the request.
c.heartbeat.setInterval(defaultHeartbeatInterval)
default:
c.heartbeat.setInterval(clampHeartbeatInterval(ticksToDuration(*intervalTicks)))
}
return
}
b := c.broadcasterFor(pgnNum)
if b == nil {
return // schedule raced away since the transmitsPGN check
}
if intervalTicks == nil {
b.trigger(c.ctx)
return
}
switch *intervalTicks {
case 0:
b.setInterval(0)
case restoreDefaultIntervalTicks:
b.restoreDefaultInterval()
default:
b.setInterval(ticksToDuration(*intervalTicks))
}
}
func ticksToDuration(ms uint64) time.Duration {
return time.Duration(ms) * time.Millisecond
}
func clampHeartbeatInterval(d time.Duration) time.Duration {
if d < minHeartbeatInterval {
return minHeartbeatInterval
}
if d > maxHeartbeatInterval {
return maxHeartbeatInterval
}
return d
}
// ackGroupFunction refuses a non-request group function for pgnPtr.
func (c *Client) ackGroupFunction(pgnPtr *uint64) {
if pgnPtr == nil {
return
}
c.ackGroupFunctionError(uint32(*pgnPtr), uint64(pgn.NotSupported_3))
}
// ackGroupFunctionError broadcasts an acknowledge group function refusing the
// named PGN with the given PGN error code.
func (c *Client) ackGroupFunctionError(pgnNum uint32, errorCode uint64) {
fc := uint64(2) // acknowledge
refused := uint64(pgnNum)
intervalErr := uint64(0) // acknowledge (no interval/priority complaint)
params := uint64(0)
c.writeProtocol("group-function acknowledgement", protocolRequired, &pgn.NmeaAcknowledgeGroupFunction{
Info: pgn.MessageInfo{Priority: pgn.Priority(3)},
FunctionCode: &fc,
Pgn: &refused,
PgnErrorCode: &errorCode,
TransmissionIntervalPriorityErrorCode: &intervalErr,
NumberOfParameters: ¶ms,
})
}