-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
530 lines (462 loc) · 12.5 KB
/
Copy pathservice.go
File metadata and controls
530 lines (462 loc) · 12.5 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package usdl
import (
"sync"
"github.com/ravisuhag/astro/pkg/sdl"
)
// Service is the interface for all USLP Data Link services.
type Service = sdl.Service
// PacketSizer returns the total length in bytes of the packet starting
// at data[0], or -1 if the data is too short to determine length.
type PacketSizer = sdl.PacketSizer
// ServiceType defines the types of USLP services available.
type ServiceType int
const (
MAPP ServiceType = iota // MAP Packet Service
MAPA // MAP Access Service
MAPO // MAP Octet Stream Service
)
// FrameCounter manages 16-bit per-VC frame sequence counts per CCSDS 732.1-B-2.
type FrameCounter struct {
mu sync.Mutex
vcCounts map[uint8]uint16
}
// NewFrameCounter creates a new FrameCounter.
func NewFrameCounter() *FrameCounter {
return &FrameCounter{vcCounts: make(map[uint8]uint16)}
}
// Next returns the current VC frame count for the given VCID,
// then increments the counter.
func (fc *FrameCounter) Next(vcid uint8) uint16 {
fc.mu.Lock()
defer fc.mu.Unlock()
seq := fc.vcCounts[vcid]
fc.vcCounts[vcid] = seq + 1
return seq
}
// stampFrame applies the frame sequence number and recomputes FECF.
func stampFrame(frame *TransferFrame, counter *FrameCounter, vcid uint8) error {
if counter != nil {
seq := counter.Next(vcid)
frame.DataFieldHeader.SequenceNumber = seq
}
return recomputeFECF(frame)
}
// isIdleFill checks if all bytes are 0xFF (idle fill pattern).
func isIdleFill(data []byte) bool {
for _, b := range data {
if b != 0xFF {
return false
}
}
return true
}
// MAPPacketService implements the MAPP service for USLP.
// Packets are multiplexed into USLP frames using FirstHeaderOffset
// for boundary detection, with FHO-based resync on frame loss.
type MAPPacketService struct {
scid uint16
vcid uint8
mapid uint8
config ChannelConfig
counter *FrameCounter
vc *VirtualChannel
// Send-side buffer for multi-packet packing
sendBuf []byte
packetOffsets []int
// Receive-side state for FHO-based extraction
recvBuf []byte
synced bool
sizer PacketSizer
gapDetector *FrameGapDetector
}
// NewMAPPacketService creates a new MAPP service instance.
func NewMAPPacketService(scid uint16, vcid, mapid uint8, vc *VirtualChannel, config ChannelConfig, counter *FrameCounter) *MAPPacketService {
return &MAPPacketService{
scid: scid,
vcid: vcid,
mapid: mapid,
config: config,
counter: counter,
vc: vc,
}
}
// SetPacketSizer configures the PacketSizer used by Receive() to detect
// packet boundaries.
func (s *MAPPacketService) SetPacketSizer(sizer PacketSizer) {
s.sizer = sizer
}
// Send appends packet data to the send buffer and generates full frames.
// When ChannelConfig.FrameLength is 0, creates one frame per packet.
// When set, packs packets into fixed-length frames with proper FirstHeaderOffset.
// Call Flush() after the last Send() to emit any remaining partial frame.
func (s *MAPPacketService) Send(data []byte) error {
if len(data) == 0 {
return ErrEmptyData
}
if s.config.FrameLength == 0 {
return s.sendVariableLength(data)
}
// Record packet boundary and buffer data
s.packetOffsets = append(s.packetOffsets, len(s.sendBuf))
s.sendBuf = append(s.sendBuf, data...)
return s.emitFullFrames()
}
func (s *MAPPacketService) sendVariableLength(data []byte) error {
opts := s.frameOpts()
frame, err := NewTransferFrame(s.scid, s.vcid, s.mapid, data, opts...)
if err != nil {
return err
}
if err := stampFrame(frame, s.counter, s.vcid); err != nil {
return err
}
return s.vc.Add(frame)
}
func (s *MAPPacketService) frameOpts() []FrameOption {
opts := []FrameOption{
WithConstructionRule(RulePacketSpanning),
}
if s.config.FrameLength > 0 {
opts = append(opts, WithEndOfFPH())
}
if s.config.HasOCF {
opts = append(opts, WithOCF(make([]byte, 4)))
}
if s.config.UseCRC32 {
opts = append(opts, WithCRC32())
}
if s.config.InsertZoneLen > 0 {
opts = append(opts, WithInsertZone(make([]byte, s.config.InsertZoneLen)))
}
return opts
}
// Flush pads and emits any remaining buffered data as a final frame.
func (s *MAPPacketService) Flush() error {
if s.config.FrameLength == 0 || len(s.sendBuf) == 0 {
return nil
}
capacity := s.config.DataFieldCapacity(0)
if capacity <= 0 {
return ErrDataFieldTooSmall
}
chunk := padDataField(s.sendBuf, capacity)
fho := FHONoPacketStart
for _, off := range s.packetOffsets {
if off < len(s.sendBuf) {
fho = uint16(off)
break
}
}
s.sendBuf = nil
s.packetOffsets = nil
return s.emitFrame(chunk, fho)
}
// emitFullFrames generates frames from sendBuf while it has >= capacity bytes.
func (s *MAPPacketService) emitFullFrames() error {
capacity := s.config.DataFieldCapacity(0)
if capacity <= 0 {
return ErrDataFieldTooSmall
}
for len(s.sendBuf) >= capacity {
chunk := make([]byte, capacity)
copy(chunk, s.sendBuf[:capacity])
// Find first packet start in this chunk
fho := FHONoPacketStart
var remaining []int
for _, off := range s.packetOffsets {
if off < capacity {
if fho == FHONoPacketStart {
fho = uint16(off)
}
} else {
remaining = append(remaining, off-capacity)
}
}
s.packetOffsets = remaining
s.sendBuf = s.sendBuf[capacity:]
if err := s.emitFrame(chunk, fho); err != nil {
return err
}
}
return nil
}
func (s *MAPPacketService) emitFrame(dataField []byte, fho uint16) error {
opts := s.frameOpts()
opts = append(opts, WithFirstHeaderOffset(fho))
frame, err := NewTransferFrame(s.scid, s.vcid, s.mapid, dataField, opts...)
if err != nil {
return err
}
if err := stampFrame(frame, s.counter, s.vcid); err != nil {
return err
}
return s.vc.Add(frame)
}
// Receive extracts the next complete packet from frame data.
func (s *MAPPacketService) Receive() ([]byte, error) {
if s.config.FrameLength == 0 {
frame, err := s.vc.Next()
if err != nil {
return nil, err
}
return frame.DataField, nil
}
if s.sizer == nil {
return nil, ErrNoPacketSizer
}
sizer := s.sizer
if s.gapDetector == nil {
s.gapDetector = NewFrameGapDetector()
}
for {
// Try to extract a complete packet from buffer
if s.synced && len(s.recvBuf) > 0 && !isIdleFill(s.recvBuf) {
pktLen := sizer(s.recvBuf)
if pktLen > 0 && pktLen <= len(s.recvBuf) {
pkt := make([]byte, pktLen)
copy(pkt, s.recvBuf[:pktLen])
s.recvBuf = s.recvBuf[pktLen:]
if isIdleFill(s.recvBuf) {
s.recvBuf = nil
}
return pkt, nil
}
}
// Pull next frame
frame, err := s.vc.Next()
if err != nil {
return nil, err
}
if IsIdleFrame(frame) {
continue
}
// VC gap detection
vcGap := s.gapDetector.Track(frame)
if s.counter != nil && vcGap > 0 {
s.recvBuf = nil
s.synced = false
}
fho := frame.DataFieldHeader.FirstHeaderOffset
data := frame.DataField
switch fho {
case FHOAllIdle:
continue // idle
case FHONoPacketStart:
// Continuation only
if s.synced {
s.recvBuf = append(s.recvBuf, data...)
}
default:
if int(fho) >= len(data) {
// Corrupted FHO — discard and resync
s.recvBuf = nil
s.synced = false
continue
}
// New packet starts at offset fho
if s.synced && int(fho) > 0 && len(s.recvBuf) > 0 {
// Append tail of previous packet
s.recvBuf = append(s.recvBuf, data[:fho]...)
// Try to extract completed previous packet
pktLen := sizer(s.recvBuf)
if pktLen > 0 && pktLen <= len(s.recvBuf) {
pkt := make([]byte, pktLen)
copy(pkt, s.recvBuf[:pktLen])
s.recvBuf = make([]byte, len(data)-int(fho))
copy(s.recvBuf, data[fho:])
if isIdleFill(s.recvBuf) {
s.recvBuf = nil
}
return pkt, nil
}
}
// Sync/resync from FHO
s.recvBuf = make([]byte, len(data)-int(fho))
copy(s.recvBuf, data[fho:])
s.synced = true
if isIdleFill(s.recvBuf) {
s.recvBuf = nil
}
}
}
}
// MAPAccessService implements the MAPA service for USLP.
// Provides fixed-length SDU transfer.
type MAPAccessService struct {
scid uint16
vcid uint8
mapid uint8
sduSize int
config ChannelConfig
counter *FrameCounter
vc *VirtualChannel
}
// NewMAPAccessService creates a new MAPA service instance.
func NewMAPAccessService(scid uint16, vcid, mapid uint8, sduSize int, vc *VirtualChannel, config ChannelConfig, counter *FrameCounter) *MAPAccessService {
return &MAPAccessService{
scid: scid,
vcid: vcid,
mapid: mapid,
sduSize: sduSize,
config: config,
counter: counter,
vc: vc,
}
}
// Send wraps a fixed-length SDU into a USLP Transfer Frame.
func (s *MAPAccessService) Send(data []byte) error {
if s.config.FrameLength == 0 {
if len(data) != s.sduSize {
return ErrSizeMismatch
}
} else {
capacity := s.config.DataFieldCapacity(0)
if len(data) == 0 {
return ErrEmptyData
}
if len(data) > capacity {
return ErrDataTooLarge
}
data = padDataField(data, capacity)
}
opts := []FrameOption{
WithConstructionRule(RuleVCASDU),
WithFirstHeaderOffset(FHOAllIdle),
}
if s.config.FrameLength > 0 {
opts = append(opts, WithEndOfFPH())
}
if s.config.HasOCF {
opts = append(opts, WithOCF(make([]byte, 4)))
}
if s.config.UseCRC32 {
opts = append(opts, WithCRC32())
}
frame, err := NewTransferFrame(s.scid, s.vcid, s.mapid, data, opts...)
if err != nil {
return err
}
if err := stampFrame(frame, s.counter, s.vcid); err != nil {
return err
}
return s.vc.Add(frame)
}
// Receive retrieves the next frame and returns its data field.
func (s *MAPAccessService) Receive() ([]byte, error) {
frame, err := s.vc.Next()
if err != nil {
return nil, err
}
if s.config.FrameLength > 0 && len(frame.DataField) >= s.sduSize {
return frame.DataField[:s.sduSize], nil
}
return frame.DataField, nil
}
// Flush is a no-op for MAPA service.
func (s *MAPAccessService) Flush() error { return nil }
// MAPOctetStreamService implements the MAPO service for USLP.
// Provides unreliable octet stream transfer without packet boundaries.
type MAPOctetStreamService struct {
scid uint16
vcid uint8
mapid uint8
config ChannelConfig
counter *FrameCounter
vc *VirtualChannel
sendBuf []byte
}
// NewMAPOctetStreamService creates a new MAPO service instance.
func NewMAPOctetStreamService(scid uint16, vcid, mapid uint8, vc *VirtualChannel, config ChannelConfig, counter *FrameCounter) *MAPOctetStreamService {
return &MAPOctetStreamService{
scid: scid,
vcid: vcid,
mapid: mapid,
config: config,
counter: counter,
vc: vc,
}
}
// Send buffers octet data and emits full frames.
func (s *MAPOctetStreamService) Send(data []byte) error {
if len(data) == 0 {
return ErrEmptyData
}
if s.config.FrameLength == 0 {
opts := []FrameOption{
WithConstructionRule(RuleOctetStream),
WithFirstHeaderOffset(FHONoPacketStart),
}
if s.config.UseCRC32 {
opts = append(opts, WithCRC32())
}
frame, err := NewTransferFrame(s.scid, s.vcid, s.mapid, data, opts...)
if err != nil {
return err
}
if err := stampFrame(frame, s.counter, s.vcid); err != nil {
return err
}
return s.vc.Add(frame)
}
s.sendBuf = append(s.sendBuf, data...)
return s.emitFullFrames()
}
func (s *MAPOctetStreamService) emitFullFrames() error {
capacity := s.config.DataFieldCapacity(0)
if capacity <= 0 {
return ErrDataFieldTooSmall
}
for len(s.sendBuf) >= capacity {
chunk := make([]byte, capacity)
copy(chunk, s.sendBuf[:capacity])
s.sendBuf = s.sendBuf[capacity:]
if err := s.emitFrame(chunk); err != nil {
return err
}
}
return nil
}
func (s *MAPOctetStreamService) emitFrame(dataField []byte) error {
opts := []FrameOption{
WithConstructionRule(RuleOctetStream),
WithFirstHeaderOffset(FHONoPacketStart),
}
if s.config.FrameLength > 0 {
opts = append(opts, WithEndOfFPH())
}
if s.config.HasOCF {
opts = append(opts, WithOCF(make([]byte, 4)))
}
if s.config.UseCRC32 {
opts = append(opts, WithCRC32())
}
frame, err := NewTransferFrame(s.scid, s.vcid, s.mapid, dataField, opts...)
if err != nil {
return err
}
if err := stampFrame(frame, s.counter, s.vcid); err != nil {
return err
}
return s.vc.Add(frame)
}
// Receive retrieves the next frame's data field.
func (s *MAPOctetStreamService) Receive() ([]byte, error) {
frame, err := s.vc.Next()
if err != nil {
return nil, err
}
return frame.DataField, nil
}
// Flush pads and emits any remaining buffered data.
func (s *MAPOctetStreamService) Flush() error {
if s.config.FrameLength == 0 || len(s.sendBuf) == 0 {
return nil
}
capacity := s.config.DataFieldCapacity(0)
if capacity <= 0 {
return ErrDataFieldTooSmall
}
chunk := padDataField(s.sendBuf, capacity)
s.sendBuf = nil
return s.emitFrame(chunk)
}